mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2026-03-11 02:55:04 +00:00
Add Core Lambda Unit tests
This commit is contained in:
33
Lambdas/Lists/src/test/java/BadCallListener.java
Normal file
33
Lambdas/Lists/src/test/java/BadCallListener.java
Normal file
@@ -0,0 +1,33 @@
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class BadCallListener extends CallListener {
|
||||
|
||||
static SQLException toThrowSQL = null;
|
||||
static RuntimeException toThrowRuntime = null;
|
||||
|
||||
|
||||
public BadCallListener(DBConnector connector, String cognitoID) {
|
||||
super(connector, cognitoID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
|
||||
if (toThrowSQL != null) {
|
||||
throw toThrowSQL;
|
||||
}
|
||||
throw toThrowRuntime;
|
||||
}
|
||||
|
||||
public static void setRuntimeException(RuntimeException newException) {
|
||||
toThrowRuntime = newException;
|
||||
toThrowSQL = null;
|
||||
}
|
||||
|
||||
public static void setSQLException(SQLException newException) {
|
||||
toThrowRuntime = null;
|
||||
toThrowSQL = newException;
|
||||
}
|
||||
|
||||
}
|
||||
34
Lambdas/Lists/src/test/java/CallListener.java
Normal file
34
Lambdas/Lists/src/test/java/CallListener.java
Normal file
@@ -0,0 +1,34 @@
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CallListener implements CallHandler {
|
||||
|
||||
static int calls = 0;
|
||||
|
||||
static int creates = 0;
|
||||
|
||||
public CallListener(DBConnector connector, String cognitoID) {
|
||||
creates++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
|
||||
calls++;
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int getCalls() {
|
||||
return calls;
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
creates = 0;
|
||||
calls = 0;
|
||||
}
|
||||
|
||||
public static int getCreates() {
|
||||
return creates;
|
||||
}
|
||||
|
||||
}
|
||||
55
Lambdas/Lists/src/test/java/TestBasicHandler.java
Normal file
55
Lambdas/Lists/src/test/java/TestBasicHandler.java
Normal file
@@ -0,0 +1,55 @@
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TestBasicHandler {
|
||||
|
||||
@Test
|
||||
public void testClassCreatedAndCalled() {
|
||||
BasicHandler.handleRequest(buildFullSampleMap(), null, CallListener.class);
|
||||
assert (CallListener.getCreates() == 1);
|
||||
assert (CallListener.getCalls() == 1);
|
||||
CallListener.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadCalls() {
|
||||
BadCallListener.setRuntimeException(new RuntimeException());
|
||||
try {
|
||||
BasicHandler.handleRequest(buildFullSampleMap(), null, BadCallListener.class);
|
||||
assert false;
|
||||
} catch (RuntimeException ignored) {
|
||||
|
||||
}
|
||||
BadCallListener.setSQLException(new SQLException());
|
||||
Object handlerReturn = BasicHandler.handleRequest(buildFullSampleMap(), null, Hidden.class);
|
||||
assert (handlerReturn == null);
|
||||
BadCallListener.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBadClass() {
|
||||
try {
|
||||
assert (BasicHandler.handleRequest(buildFullSampleMap(), null, Hidden.class) == null);
|
||||
} catch (Exception ignored) {
|
||||
assert false;
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Object> buildFullSampleMap() {
|
||||
Map<String, Object> testMap = new HashMap<>();
|
||||
TestInputUtils.addBody(testMap);
|
||||
TestInputUtils.addCognitoID(testMap);
|
||||
TestInputUtils.addQueryParams(testMap);
|
||||
return testMap;
|
||||
}
|
||||
private class Hidden implements CallHandler{
|
||||
|
||||
@Override
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Lambdas/Lists/src/test/java/TestDBConnector.java
Normal file
25
Lambdas/Lists/src/test/java/TestDBConnector.java
Normal file
@@ -0,0 +1,25 @@
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class TestDBConnector {
|
||||
|
||||
@Test
|
||||
public void testLoadProperties() {
|
||||
Properties testProperties;
|
||||
try {
|
||||
testProperties = DBConnector.loadProperties(getClass().getResource("testDBProperties.json").getPath());
|
||||
} catch (IOException e) {
|
||||
System.out.println(e);
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
Properties testPropertiesKey = new Properties();
|
||||
testPropertiesKey.put("url", "jdbc:mariadb://listify.id.us-east-2.rds.amazonaws.com:3306/ListifySchema");
|
||||
testPropertiesKey.put("user", "auser");
|
||||
testPropertiesKey.put("password", "apassword");
|
||||
assert (testProperties.equals(testPropertiesKey));
|
||||
}
|
||||
|
||||
}
|
||||
62
Lambdas/Lists/src/test/java/TestInputUtils.java
Normal file
62
Lambdas/Lists/src/test/java/TestInputUtils.java
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TestInputUtils {
|
||||
|
||||
@Test
|
||||
public void testGetBody() {
|
||||
Map<String, Object> testMap = new HashMap<>();
|
||||
Map<String, String> bodyMap = addBody(testMap);
|
||||
assert (InputUtils.getBody(testMap).equals(bodyMap));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetQueryParams() {
|
||||
Map<String, Object> testMap = new HashMap<>();
|
||||
Map<String, String> queryMap = addQueryParams(testMap);
|
||||
assert (InputUtils.getQueryParams(testMap).equals(queryMap));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCognitoID() {
|
||||
Map<String, Object> testMap = new HashMap<>();
|
||||
String cognitoID = addCognitoID(testMap);
|
||||
assert (InputUtils.getCognitoIDFromBody(testMap).equals(cognitoID));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBadMapKey() {
|
||||
Map<String, Object> testMap = new HashMap<>();
|
||||
try {
|
||||
InputUtils.getMap(testMap, "missing");
|
||||
assert (false);
|
||||
} catch (IllegalArgumentException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static String addCognitoID(Map<String, Object> testMap) {
|
||||
HashMap<String, String> contextMap = new HashMap<>();
|
||||
String cognitoID = "Example id";
|
||||
contextMap.put("sub", cognitoID);
|
||||
testMap.put("context", contextMap);
|
||||
return cognitoID;
|
||||
}
|
||||
|
||||
public static Map<String, String> addQueryParams(Map<String, Object> testMap) {
|
||||
HashMap<String, Object> paramsMap = new HashMap<>();
|
||||
HashMap<String, String> queryMap = new HashMap<>();
|
||||
paramsMap.put("querystring", queryMap);
|
||||
testMap.put("params", paramsMap);
|
||||
return queryMap;
|
||||
}
|
||||
|
||||
public static Map<String, String> addBody(Map<String, Object> testMap) {
|
||||
HashMap<String, String> bodyMap = new HashMap<>();
|
||||
testMap.put("body", bodyMap);
|
||||
return bodyMap;
|
||||
}
|
||||
}
|
||||
5
Lambdas/Lists/src/test/resources/testDBProperties.json
Normal file
5
Lambdas/Lists/src/test/resources/testDBProperties.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"url": "jdbc:mariadb://listify.id.us-east-2.rds.amazonaws.com:3306/ListifySchema",
|
||||
"user": "auser",
|
||||
"password": "apassword"
|
||||
}
|
||||
Reference in New Issue
Block a user