diff --git a/.gitignore b/.gitignore index 837a882..55ac339 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Lambdas/Lists/pom.xml b/Lambdas/Lists/pom.xml index 9fb5565..c80c2c7 100644 --- a/Lambdas/Lists/pom.xml +++ b/Lambdas/Lists/pom.xml @@ -89,9 +89,27 @@ jackson-databind 2.11.3 + + junit + junit + 4.13 + test + 1.11 1.11 + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 11 + + + + \ No newline at end of file diff --git a/Lambdas/Lists/src/main/java/DBConnector.java b/Lambdas/Lists/src/main/java/DBConnector.java index 798cfc3..c7782ed 100644 --- a/Lambdas/Lists/src/main/java/DBConnector.java +++ b/Lambdas/Lists/src/main/java/DBConnector.java @@ -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; - } } diff --git a/Lambdas/Lists/src/main/java/InputUtils.java b/Lambdas/Lists/src/main/java/InputUtils.java index 2a4e8bc..d007009 100644 --- a/Lambdas/Lists/src/main/java/InputUtils.java +++ b/Lambdas/Lists/src/main/java/InputUtils.java @@ -10,12 +10,7 @@ public class InputUtils { public static String getCognitoIDFromBody(Map inputMap) { System.out.println(inputMap.keySet()); System.out.println(inputMap.entrySet()); - Map contextMap; - if ((inputMap.get("context") != null) && (inputMap.get("context") instanceof Map)) { - contextMap = ((Map) inputMap.get("context")); - } else { - throw new IllegalArgumentException("The key \"context\" must exist and be a map"); - } + Map 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 inputMap) { - return (String) (getMap(inputMap, "params").get("querystring")); - } - public static HashMap getQueryParams(Map inputMap) { return (HashMap) (getMap(inputMap, "params").get("querystring")); - -// String queryString = getQueryString(inputMap); -// List queryparams = URLEncodedUtils.parse(queryString, StandardCharsets.UTF_8); -// HashMap mappedParams = new HashMap<>(); -// for (NameValuePair param : queryparams) { -// mappedParams.put(param.getName(), param.getValue()); -// } -// return mappedParams; } diff --git a/Lambdas/Lists/src/test/java/BadCallListener.java b/Lambdas/Lists/src/test/java/BadCallListener.java new file mode 100644 index 0000000..f1bdadb --- /dev/null +++ b/Lambdas/Lists/src/test/java/BadCallListener.java @@ -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 bodyMap, HashMap 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; + } + +} diff --git a/Lambdas/Lists/src/test/java/CallListener.java b/Lambdas/Lists/src/test/java/CallListener.java new file mode 100644 index 0000000..3bd40ff --- /dev/null +++ b/Lambdas/Lists/src/test/java/CallListener.java @@ -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 bodyMap, HashMap 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; + } + +} \ No newline at end of file diff --git a/Lambdas/Lists/src/test/java/TestBasicHandler.java b/Lambdas/Lists/src/test/java/TestBasicHandler.java new file mode 100644 index 0000000..0357bf6 --- /dev/null +++ b/Lambdas/Lists/src/test/java/TestBasicHandler.java @@ -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 buildFullSampleMap() { + Map testMap = new HashMap<>(); + TestInputUtils.addBody(testMap); + TestInputUtils.addCognitoID(testMap); + TestInputUtils.addQueryParams(testMap); + return testMap; + } + private class Hidden implements CallHandler{ + + @Override + public Object conductAction(Map bodyMap, HashMap queryString, String cognitoID) throws SQLException { + return null; + } + } +} diff --git a/Lambdas/Lists/src/test/java/TestDBConnector.java b/Lambdas/Lists/src/test/java/TestDBConnector.java new file mode 100644 index 0000000..a9258b4 --- /dev/null +++ b/Lambdas/Lists/src/test/java/TestDBConnector.java @@ -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)); + } + +} diff --git a/Lambdas/Lists/src/test/java/TestInputUtils.java b/Lambdas/Lists/src/test/java/TestInputUtils.java new file mode 100644 index 0000000..b082f8a --- /dev/null +++ b/Lambdas/Lists/src/test/java/TestInputUtils.java @@ -0,0 +1,62 @@ + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class TestInputUtils { + + @Test + public void testGetBody() { + Map testMap = new HashMap<>(); + Map bodyMap = addBody(testMap); + assert (InputUtils.getBody(testMap).equals(bodyMap)); + } + + @Test + public void testGetQueryParams() { + Map testMap = new HashMap<>(); + Map queryMap = addQueryParams(testMap); + assert (InputUtils.getQueryParams(testMap).equals(queryMap)); + } + + @Test + public void testGetCognitoID() { + Map testMap = new HashMap<>(); + String cognitoID = addCognitoID(testMap); + assert (InputUtils.getCognitoIDFromBody(testMap).equals(cognitoID)); + } + + @Test + public void testGetBadMapKey() { + Map testMap = new HashMap<>(); + try { + InputUtils.getMap(testMap, "missing"); + assert (false); + } catch (IllegalArgumentException e) { + + } + } + + public static String addCognitoID(Map testMap) { + HashMap contextMap = new HashMap<>(); + String cognitoID = "Example id"; + contextMap.put("sub", cognitoID); + testMap.put("context", contextMap); + return cognitoID; + } + + public static Map addQueryParams(Map testMap) { + HashMap paramsMap = new HashMap<>(); + HashMap queryMap = new HashMap<>(); + paramsMap.put("querystring", queryMap); + testMap.put("params", paramsMap); + return queryMap; + } + + public static Map addBody(Map testMap) { + HashMap bodyMap = new HashMap<>(); + testMap.put("body", bodyMap); + return bodyMap; + } +} diff --git a/Lambdas/Lists/src/test/resources/testDBProperties.json b/Lambdas/Lists/src/test/resources/testDBProperties.json new file mode 100644 index 0000000..e653a42 --- /dev/null +++ b/Lambdas/Lists/src/test/resources/testDBProperties.json @@ -0,0 +1,5 @@ +{ + "url": "jdbc:mariadb://listify.id.us-east-2.rds.amazonaws.com:3306/ListifySchema", + "user": "auser", + "password": "apassword" +} \ No newline at end of file diff --git a/Lambdas/Lists/target/test-classes/testDBProperties.json b/Lambdas/Lists/target/test-classes/testDBProperties.json new file mode 100644 index 0000000..e653a42 --- /dev/null +++ b/Lambdas/Lists/target/test-classes/testDBProperties.json @@ -0,0 +1,5 @@ +{ + "url": "jdbc:mariadb://listify.id.us-east-2.rds.amazonaws.com:3306/ListifySchema", + "user": "auser", + "password": "apassword" +} \ No newline at end of file