mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-16 02:38:47 +00:00
Merge pull request #41 from ClaytonWWilson/APIUnitTests
Add Core Lambda Unit tests
This commit is contained in:
commit
38d954fb28
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
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
@ -8,18 +6,16 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ItemGetter implements CallHandler {
|
||||
private final DBConnector connector;
|
||||
private final Connection connection;
|
||||
|
||||
private final String GET_ITEM = "SELECT * FROM Product WHERE productID = ?;";
|
||||
|
||||
public ItemGetter(DBConnector connector, String cognitoID) {
|
||||
this.connector = connector;
|
||||
public ItemGetter(Connection connection, String cognitoID) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryMap, String cognitoID) throws SQLException {
|
||||
Connection connection = connector.getConnection();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement(GET_ITEM);
|
||||
statement.setInt(1, Integer.parseInt(queryMap.get("id")));
|
||||
System.out.println(statement);
|
||||
@ -29,8 +25,5 @@ public class ItemGetter implements CallHandler{
|
||||
Item retrievedItem = new Item(queryResults);
|
||||
System.out.println(retrievedItem);
|
||||
return retrievedItem;
|
||||
} finally {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
55
Lambdas/Lists/Item/test/TestItemGetter.java
Normal file
55
Lambdas/Lists/Item/test/TestItemGetter.java
Normal file
@ -0,0 +1,55 @@
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
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 TestItemGetter {
|
||||
|
||||
@Test
|
||||
public void testItemGetterValid() {
|
||||
conductItemGetterTest(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testItemGetterError() {
|
||||
conductItemGetterTest(true);
|
||||
}
|
||||
|
||||
public void conductItemGetterTest(boolean shouldThrow) {
|
||||
ArrayList<Object> rsReturns = new ArrayList<>();
|
||||
rsReturns.add(1);//ProductID
|
||||
rsReturns.add(2);//chainID
|
||||
rsReturns.add("aupc");
|
||||
rsReturns.add("adescription");
|
||||
rsReturns.add(BigDecimal.valueOf(.03));//Price
|
||||
rsReturns.add("animageurl");
|
||||
rsReturns.add("adepartment");
|
||||
rsReturns.add(Timestamp.from(Instant.ofEpochMilli(1602192528688L)));//retrievedDate
|
||||
rsReturns.add(5); // fetchCounts
|
||||
StatementInjector injector = null;
|
||||
try {
|
||||
injector = new StatementInjector(null, rsReturns, shouldThrow);
|
||||
} catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
ItemGetter getter = new ItemGetter(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() == Item.class);
|
||||
Item itemReturn = (Item) conductReturn;
|
||||
assert (itemReturn.toString().equals("Item{productID=1, chainID=2, upc='aupc', description='adescription', price=0.03, imageURL='animageurl', department='adepartment', retrievedDate=1602192528688, fetchCounts=5}"));
|
||||
} catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
assert shouldThrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -34,6 +34,17 @@ public class ItemEntry {
|
||||
this.listID = listID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ItemEntry{" +
|
||||
"listID=" + listID +
|
||||
", productID=" + productID +
|
||||
", quantity=" + quantity +
|
||||
", addedDate=" + addedDate +
|
||||
", purchased=" + purchased +
|
||||
'}';
|
||||
}
|
||||
|
||||
public Integer getProductID() {
|
||||
return productID;
|
||||
}
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class List {
|
||||
|
||||
@ -1,23 +1,21 @@
|
||||
import java.sql.*;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ListAdder implements CallHandler {
|
||||
|
||||
private DBConnector connector;
|
||||
private Connection connection;
|
||||
private String cognitoID;
|
||||
|
||||
private final String LIST_CREATE = "INSERT INTO List (name, owner, lastUpdated) VALUES (?, ?, ?)";
|
||||
|
||||
public ListAdder(DBConnector connector, String cognitoID) {
|
||||
this.connector = connector;
|
||||
public ListAdder(Connection connection, String cognitoID) {
|
||||
this.connection = connection;
|
||||
this.cognitoID = cognitoID;
|
||||
}
|
||||
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
|
||||
Connection connection = connector.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(LIST_CREATE, Statement.RETURN_GENERATED_KEYS);
|
||||
statement.setString(1, bodyMap.get("name").toString());//Needs safe checking
|
||||
statement.setString(2, cognitoID);
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
@ -9,21 +7,20 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ListGetter implements CallHandler{
|
||||
private final DBConnector connector;
|
||||
private final Connection connection;
|
||||
private final String cognitoID;
|
||||
|
||||
private final String GET_LIST = "SELECT * FROM List WHERE listID = ?;";
|
||||
private final String GET_LISTS = "SELECT listID FROM List WHERE owner = ?;";
|
||||
private final String GET_ENTRIES = "SELECT * FROM ListProduct WHERE listID = ?;";
|
||||
|
||||
public ListGetter(DBConnector connector, String cognitoID) {
|
||||
this.connector = connector;
|
||||
public ListGetter(Connection connection, String cognitoID) {
|
||||
this.connection = connection;
|
||||
this.cognitoID = cognitoID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryMap, String cognitoID) throws SQLException {
|
||||
try (Connection connection = connector.getConnection()) {
|
||||
Integer id = Integer.parseInt(queryMap.get("id"));
|
||||
if (id == -1) {
|
||||
PreparedStatement getLists = connection.prepareStatement(GET_LISTS);
|
||||
@ -55,4 +52,3 @@ public class ListGetter implements CallHandler{
|
||||
return retrievedList;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
49
Lambdas/Lists/List/test/TestListAdder.java
Normal file
49
Lambdas/Lists/List/test/TestListAdder.java
Normal file
@ -0,0 +1,49 @@
|
||||
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, rsReturns, 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;
|
||||
if (!(rawIDReturn.getClass() == Integer.class)) {
|
||||
assert false;
|
||||
return;
|
||||
}
|
||||
assert (((Integer) rawIDReturn) == 1);
|
||||
assert (injector.getStatementString().contains("INSERT INTO List (name, owner, lastUpdated) VALUES (?, ?, ?)[aname, cognitoID,"));
|
||||
} catch (SQLException throwables) {
|
||||
assert shouldThrow;
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
105
Lambdas/Lists/List/test/TestListGetter.java
Normal file
105
Lambdas/Lists/List/test/TestListGetter.java
Normal file
@ -0,0 +1,105 @@
|
||||
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 testListIDGetterValid() {
|
||||
conductListIDGetterTest(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListIDGetterError() {
|
||||
conductListIDGetterTest(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;
|
||||
}
|
||||
}
|
||||
|
||||
public void conductListIDGetterTest(boolean shouldThrow) {
|
||||
|
||||
|
||||
ArrayList<Object> rsReturns = new ArrayList<>();
|
||||
rsReturns.add(1);
|
||||
rsReturns.add(2);
|
||||
rsReturns.add(3);
|
||||
rsReturns.add(4);
|
||||
|
||||
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() == ArrayList.class);
|
||||
ArrayList<Integer> listIDsReturn = (ArrayList<Integer>) conductReturn;
|
||||
System.out.println(listIDsReturn.toString());
|
||||
assert (listIDsReturn.toString().equals("[1, 2, 3, 4]"));
|
||||
} catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
assert shouldThrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -9,19 +9,17 @@ import java.util.Map;
|
||||
|
||||
public class ListEntryAdder implements CallHandler {
|
||||
|
||||
private DBConnector connector;
|
||||
private Connection connection;
|
||||
private String cognitoID;
|
||||
|
||||
private final String ITEM_TO_LIST = "INSERT INTO ListProduct (productID, listID, quantity, addedDate, purchased) VALUES (?, ?, ?, ?, ?)";
|
||||
|
||||
public ListEntryAdder(DBConnector connector, String cognitoID) {
|
||||
this.connector = connector;
|
||||
public ListEntryAdder(Connection connection, String cognitoID) {
|
||||
this.connection = connection;
|
||||
this.cognitoID = cognitoID;
|
||||
}
|
||||
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
|
||||
Connection connection = connector.getConnection();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement(ITEM_TO_LIST);
|
||||
statement.setInt(1, (Integer) bodyMap.get("productID"));
|
||||
statement.setInt(2, (Integer) bodyMap.get("listID"));
|
||||
@ -31,9 +29,6 @@ public class ListEntryAdder implements CallHandler {
|
||||
System.out.println(statement);
|
||||
statement.executeUpdate();
|
||||
connection.commit();
|
||||
} finally {
|
||||
connection.close();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,28 +6,23 @@ import java.util.Map;
|
||||
|
||||
public class ListEntryDeleter implements CallHandler {
|
||||
|
||||
private DBConnector connector;
|
||||
private Connection connection;
|
||||
private String cognitoID;
|
||||
|
||||
private final String REMOVE_FROM_LIST = "DELETE FROM ListProduct WHERE (ProductID = ? AND ListID = ?);";
|
||||
|
||||
public ListEntryDeleter(DBConnector connector, String cognitoID) {
|
||||
this.connector = connector;
|
||||
public ListEntryDeleter(Connection connection, String cognitoID) {
|
||||
this.connection = connection;
|
||||
this.cognitoID = cognitoID;
|
||||
}
|
||||
|
||||
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
|
||||
Connection connection = connector.getConnection();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement(REMOVE_FROM_LIST);
|
||||
statement.setInt(1, (Integer) bodyMap.get("ProductID"));
|
||||
statement.setInt(2, (Integer) bodyMap.get("ListID"));
|
||||
statement.setInt(1, (Integer) bodyMap.get("productID"));
|
||||
statement.setInt(2, (Integer) bodyMap.get("listID"));
|
||||
System.out.println(statement);
|
||||
statement.executeUpdate();
|
||||
connection.commit();
|
||||
} finally {
|
||||
connection.close();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
47
Lambdas/Lists/ListEntry/test/TestListEntryAdder.java
Normal file
47
Lambdas/Lists/ListEntry/test/TestListEntryAdder.java
Normal file
@ -0,0 +1,47 @@
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TestListEntryAdder {
|
||||
|
||||
@Test
|
||||
public void testListEntryAdderValid() {
|
||||
testListEntryAdderCore(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListEntryAdderError() {
|
||||
testListEntryAdderCore(true);
|
||||
}
|
||||
|
||||
public void testListEntryAdderCore(boolean shouldThrow) {
|
||||
StatementInjector injector;
|
||||
try {
|
||||
injector = new StatementInjector(null, null, shouldThrow);
|
||||
} catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
assert false; //Error in test infrastructure
|
||||
return;
|
||||
}
|
||||
ListEntryAdder listEntryAdder = new ListEntryAdder(injector, "cognitoID");
|
||||
Map<String, Object> ignore = new HashMap<>();
|
||||
Map<String, Object> body = TestInputUtils.addBody(ignore);
|
||||
body.put("productID", 16);
|
||||
body.put("listID", 15);
|
||||
body.put("quantity", 14);
|
||||
body.put("purchased", false);
|
||||
|
||||
try {
|
||||
Object rawIDReturn = listEntryAdder.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID");
|
||||
assert !shouldThrow;
|
||||
assert (rawIDReturn == null);
|
||||
assert (injector.getStatementString().contains("INSERT INTO ListProduct (productID, listID, quantity, addedDate, purchased) VALUES (?, ?, ?, ?, ?)[16, 15, 14, "));
|
||||
assert (injector.getStatementString().contains(", false]"));
|
||||
} catch (SQLException throwables) {
|
||||
assert shouldThrow;
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
44
Lambdas/Lists/ListEntry/test/TestListEntryDeleter.java
Normal file
44
Lambdas/Lists/ListEntry/test/TestListEntryDeleter.java
Normal file
@ -0,0 +1,44 @@
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TestListEntryDeleter {
|
||||
|
||||
@Test
|
||||
public void testListEntryDeleterValid() {
|
||||
testListEntryDeleterCore(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListEntryDeleterError() {
|
||||
testListEntryDeleterCore(true);
|
||||
}
|
||||
|
||||
public void testListEntryDeleterCore(boolean shouldThrow) {
|
||||
StatementInjector injector;
|
||||
try {
|
||||
injector = new StatementInjector(null, null, shouldThrow);
|
||||
} catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
assert false; //Error in test infrastructure
|
||||
return;
|
||||
}
|
||||
ListEntryDeleter listEntryDeleter = new ListEntryDeleter(injector, "cognitoID");
|
||||
Map<String, Object> ignore = new HashMap<>();
|
||||
Map<String, Object> body = TestInputUtils.addBody(ignore);
|
||||
body.put("productID", 16);
|
||||
body.put("listID", 15);
|
||||
|
||||
try {
|
||||
Object rawIDReturn = listEntryDeleter.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID");
|
||||
assert !shouldThrow;
|
||||
assert (rawIDReturn == null);
|
||||
assert (injector.getStatementString().equals("DELETE FROM ListProduct WHERE (ProductID = ? AND ListID = ?);[16, 15]"));
|
||||
} catch (SQLException throwables) {
|
||||
assert shouldThrow;
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -4,6 +4,7 @@ import com.amazonaws.services.cognitoidp.model.AdminDeleteUserRequest;
|
||||
import com.amazonaws.services.cognitoidp.model.AdminUserGlobalSignOutRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -11,12 +12,12 @@ import java.util.Properties;
|
||||
|
||||
public class UserDeleter implements CallHandler {
|
||||
|
||||
private DBConnector connector;
|
||||
private Connection connector;
|
||||
private String cognitoID;
|
||||
|
||||
//private final String REMOVE_FROM_LIST = "DELETE FROM ListProduct WHERE (ProductID = ? AND ListID = ?);";
|
||||
|
||||
public UserDeleter(DBConnector connector, String cognitoID) {
|
||||
public UserDeleter(Connection connector, String cognitoID) {
|
||||
this.connector = connector;
|
||||
this.cognitoID = cognitoID;
|
||||
}
|
||||
|
||||
@ -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>
|
||||
@ -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,15 +14,17 @@ public class BasicHandler {
|
||||
HashMap<String, String> queryMap = InputUtils.getQueryParams(inputMap);
|
||||
try {
|
||||
DBConnector connector = new DBConnector();
|
||||
try (Connection connection = connector.getConnection()) {
|
||||
try {
|
||||
Constructor<T> constructor = toCall.getConstructor(DBConnector.class, String.class);
|
||||
CallHandler callHandler = constructor.newInstance(connector, cognitoID);
|
||||
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();
|
||||
throw new RuntimeException(e.getLocalizedMessage());
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
34
Lambdas/Lists/src/test/java/BadCallListener.java
Normal file
34
Lambdas/Lists/src/test/java/BadCallListener.java
Normal file
@ -0,0 +1,34 @@
|
||||
import java.sql.Connection;
|
||||
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(Connection connection, String cognitoID) {
|
||||
super(connection, 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;
|
||||
}
|
||||
|
||||
}
|
||||
35
Lambdas/Lists/src/test/java/CallListener.java
Normal file
35
Lambdas/Lists/src/test/java/CallListener.java
Normal file
@ -0,0 +1,35 @@
|
||||
import java.sql.Connection;
|
||||
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(Connection connection, 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;
|
||||
}
|
||||
|
||||
}
|
||||
1627
Lambdas/Lists/src/test/java/StatementInjector.java
Normal file
1627
Lambdas/Lists/src/test/java/StatementInjector.java
Normal file
File diff suppressed because it is too large
Load Diff
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 static 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, Object> 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 HashMap<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, Object> addBody(Map<String, Object> testMap) {
|
||||
HashMap<String, Object> 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