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

@@ -3,6 +3,7 @@ import com.amazonaws.services.lambda.runtime.Context;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
@@ -13,14 +14,16 @@ public class BasicHandler {
HashMap<String, String> queryMap = InputUtils.getQueryParams(inputMap);
try {
DBConnector connector = new DBConnector();
try {
Constructor<T> constructor = toCall.getConstructor(DBConnector.class, String.class);
CallHandler callHandler = constructor.newInstance(connector, cognitoID);
return callHandler.conductAction(InputUtils.getBody(inputMap), queryMap, cognitoID);
} catch (SQLException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
} finally {
connector.close();
try (Connection connection = connector.getConnection()) {
try {
Constructor<T> constructor = toCall.getConstructor(Connection.class, String.class);
CallHandler callHandler = constructor.newInstance(connection, cognitoID);
return callHandler.conductAction(InputUtils.getBody(inputMap), queryMap, cognitoID);
} catch (SQLException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
} finally {
connector.close();
}
}
} catch (IOException |SQLException|ClassNotFoundException e) {
e.printStackTrace();

View File

@@ -1,3 +1,4 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
@@ -8,8 +9,8 @@ public class BadCallListener extends CallListener {
static RuntimeException toThrowRuntime = null;
public BadCallListener(DBConnector connector, String cognitoID) {
super(connector, cognitoID);
public BadCallListener(Connection connection, String cognitoID) {
super(connection, cognitoID);
}
@Override

View File

@@ -1,3 +1,4 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
@@ -8,7 +9,7 @@ public class CallListener implements CallHandler {
static int creates = 0;
public CallListener(DBConnector connector, String cognitoID) {
public CallListener(Connection connection, String cognitoID) {
creates++;
}

File diff suppressed because it is too large Load Diff

View File

@@ -38,7 +38,7 @@ public class TestBasicHandler {
}
}
public Map<String, Object> buildFullSampleMap() {
public static Map<String, Object> buildFullSampleMap() {
Map<String, Object> testMap = new HashMap<>();
TestInputUtils.addBody(testMap);
TestInputUtils.addCognitoID(testMap);

View File

@@ -9,7 +9,7 @@ public class TestInputUtils {
@Test
public void testGetBody() {
Map<String, Object> testMap = new HashMap<>();
Map<String, String> bodyMap = addBody(testMap);
Map<String, Object> bodyMap = addBody(testMap);
assert (InputUtils.getBody(testMap).equals(bodyMap));
}
@@ -46,7 +46,7 @@ public class TestInputUtils {
return cognitoID;
}
public static Map<String, String> addQueryParams(Map<String, Object> testMap) {
public static HashMap<String, String> addQueryParams(Map<String, Object> testMap) {
HashMap<String, Object> paramsMap = new HashMap<>();
HashMap<String, String> queryMap = new HashMap<>();
paramsMap.put("querystring", queryMap);
@@ -54,8 +54,8 @@ public class TestInputUtils {
return queryMap;
}
public static Map<String, String> addBody(Map<String, Object> testMap) {
HashMap<String, String> bodyMap = new HashMap<>();
public static Map<String, Object> addBody(Map<String, Object> testMap) {
HashMap<String, Object> bodyMap = new HashMap<>();
testMap.put("body", bodyMap);
return bodyMap;
}