mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-13 09:48:47 +00:00
Add Core Lambda Unit tests
This commit is contained in:
parent
da14953daf
commit
c9751d4f95
2
.gitignore
vendored
2
.gitignore
vendored
@ -88,3 +88,5 @@ Lambdas/Lists/src/main/resources/dbProperties.json
|
||||
Lambdas/Lists/target/classes/dbProperties.json
|
||||
Lambdas/Lists/target/classes/META-INF/Lists.kotlin_module
|
||||
Listify/app/src/main/res/raw/auths.json
|
||||
Lambdas/Lists/target/surefire-reports/TestInputUtils.txt
|
||||
Lambdas/Lists/target/surefire-reports/TEST-TestInputUtils.xml
|
||||
|
||||
@ -89,9 +89,27 @@
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.11.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<maven.compiler.source>1.11</maven.compiler.source>
|
||||
<maven.compiler.target>1.11</maven.compiler.target>
|
||||
</properties>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<release>11</release>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@ -13,13 +13,12 @@ public class DBConnector {
|
||||
Connection connection;
|
||||
|
||||
public DBConnector() throws IOException, SQLException, ClassNotFoundException {
|
||||
this(loadProperties("dbProperties.json"));
|
||||
this(loadProperties(DBConnector.class.getResource("dbProperties.json").getPath()));
|
||||
}
|
||||
|
||||
public DBConnector(Properties dbProperties) throws SQLException, ClassNotFoundException {
|
||||
Class.forName("org.mariadb.jdbc.Driver");
|
||||
System.out.println(dbProperties);
|
||||
System.out.println(DBConnector.buildURL(dbProperties));
|
||||
connection = DriverManager.getConnection(dbProperties.get("url").toString(), dbProperties.get("user").toString(), dbProperties.get("password").toString());
|
||||
System.out.println(connection);
|
||||
}
|
||||
@ -43,11 +42,4 @@ public class DBConnector {
|
||||
propertiesJSON.keys().forEachRemaining(key -> toReturn.setProperty(key, propertiesJSON.get(key).toString()));
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
public static String buildURL(Properties dbProperties) {
|
||||
String dbURL = dbProperties.get("url").toString();
|
||||
dbURL += "?user=" + dbProperties.get("user").toString();
|
||||
dbURL += "&password=" + dbProperties.get("password").toString();
|
||||
return dbURL;
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,12 +10,7 @@ public class InputUtils {
|
||||
public static String getCognitoIDFromBody(Map<String, Object> inputMap) {
|
||||
System.out.println(inputMap.keySet());
|
||||
System.out.println(inputMap.entrySet());
|
||||
Map<String, Object> contextMap;
|
||||
if ((inputMap.get("context") != null) && (inputMap.get("context") instanceof Map<?, ?>)) {
|
||||
contextMap = ((Map<String, Object>) inputMap.get("context"));
|
||||
} else {
|
||||
throw new IllegalArgumentException("The key \"context\" must exist and be a map");
|
||||
}
|
||||
Map<String, Object> contextMap = getMap(inputMap, "context");
|
||||
System.out.println(inputMap.get("context"));
|
||||
System.out.println(contextMap.get("sub"));
|
||||
return contextMap.get("sub").toString();
|
||||
@ -25,20 +20,8 @@ public class InputUtils {
|
||||
return getMap(inputMap, "body");
|
||||
}
|
||||
|
||||
private static String getQueryString(Map<String, Object> inputMap) {
|
||||
return (String) (getMap(inputMap, "params").get("querystring"));
|
||||
}
|
||||
|
||||
public static HashMap<String, String> getQueryParams(Map<String, Object> inputMap) {
|
||||
return (HashMap<String, String>) (getMap(inputMap, "params").get("querystring"));
|
||||
|
||||
// String queryString = getQueryString(inputMap);
|
||||
// List<NameValuePair> queryparams = URLEncodedUtils.parse(queryString, StandardCharsets.UTF_8);
|
||||
// HashMap<String, String> mappedParams = new HashMap<>();
|
||||
// for (NameValuePair param : queryparams) {
|
||||
// mappedParams.put(param.getName(), param.getValue());
|
||||
// }
|
||||
// return mappedParams;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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"
|
||||
}
|
||||
5
Lambdas/Lists/target/test-classes/testDBProperties.json
Normal file
5
Lambdas/Lists/target/test-classes/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"
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user