Lambda unit test infrastructure

Unit test infrastructure for testing GET and POST Lambdas
This commit is contained in:
NMerz
2020-10-08 18:34:06 -04:00
parent c9751d4f95
commit 508818c700
19 changed files with 1890 additions and 103 deletions

View File

@@ -0,0 +1,45 @@
import org.junit.Test;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class TestListAdder {
@Test
public void testListAdderValid() {
testListAdderCore(false);
}
@Test
public void testListAdderError() {
testListAdderCore(true);
}
public void testListAdderCore(boolean shouldThrow) {
StatementInjector injector;
ArrayList<Object> rsReturns = new ArrayList<>();
rsReturns.add(1); //new listID
try {
injector = new StatementInjector(null, null, shouldThrow);
} catch (SQLException throwables) {
throwables.printStackTrace();
assert false; //Error in test infrastructure
return;
}
ListAdder listAdder = new ListAdder(injector, "cognitoID");
Map<String, Object> ignore = new HashMap<>();
Map<String, Object> body = TestInputUtils.addBody(ignore);
body.put("name", "aname");
try {
Object rawIDReturn = listAdder.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID");
assert !shouldThrow;
assert (rawIDReturn.getClass() == Integer.class);
assert (injector.getStatementString().contains("INSERT INTO List (name, owner, lastUpdated) VALUES (?, ?, ?)[aname, cognitoID,"));
} catch (SQLException throwables) {
assert shouldThrow;
throwables.printStackTrace();
}
}
}

View File

@@ -0,0 +1,63 @@
import org.junit.Test;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class TestListGetter {
@Test
public void testListGetterValid() {
conductListGetterTest(false);
}
@Test
public void testListGetterError() {
conductListGetterTest(true);
}
public void conductListGetterTest(boolean shouldThrow) {
Integer listID = 1;
String name = "aname";
String owner = "anowner";
Timestamp lastUpdated = Timestamp.from(Instant.ofEpochMilli(1602192528688L));
Integer productID = 2;
Integer quantity = 3;
Timestamp addedDate = Timestamp.from(Instant.ofEpochMilli(1602192528689L));;
Boolean purchased = false;
ArrayList<Object> rsReturns = new ArrayList<>();
rsReturns.add(listID);
rsReturns.add(name);
rsReturns.add(owner);
rsReturns.add(lastUpdated);
rsReturns.add(productID);
rsReturns.add(quantity);
rsReturns.add(addedDate);
rsReturns.add(purchased);
StatementInjector injector = null;
try {
injector = new StatementInjector(null, rsReturns, shouldThrow);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
ListGetter getter = new ListGetter(injector, "id");
Map<String, Object> ignore = new HashMap<>();
HashMap<String, String> queryParams = TestInputUtils.addQueryParams(ignore);
queryParams.put("id", "1");
try {
Object conductReturn = getter.conductAction(TestInputUtils.addBody(ignore), queryParams, "cognitoID");
assert !shouldThrow;
assert (conductReturn.getClass() == List.class);
List listReturn = (List) conductReturn;
assert (listReturn.toString().equals("List{itemID=1, name='aname', owner='anowner', lastUpdated=1602192528688, entries=[ItemEntry{listID=1, productID=2, quantity=3, addedDate=1602192528689, purchased=false}]}"));
} catch (SQLException throwables) {
throwables.printStackTrace();
assert shouldThrow;
}
}
}