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

@ -1,7 +1,6 @@
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
public class Item {
Integer productID;
@ -11,7 +10,7 @@ public class Item {
BigDecimal price;
String imageURL;
String department;
LocalDateTime retrievedDate;
long retrievedDate;
Integer fetchCounts;
Item(ResultSet itemRow) throws SQLException {
@ -29,7 +28,7 @@ public class Item {
System.out.println(imageURL);
this.department = itemRow.getString(7);
System.out.println(department);
this.retrievedDate = itemRow.getObject(8, LocalDateTime.class);
this.retrievedDate = itemRow.getTimestamp(8).toInstant().toEpochMilli();
System.out.println(retrievedDate);
this.fetchCounts = itemRow.getInt(9);
System.out.println(fetchCounts);
@ -106,11 +105,11 @@ public class Item {
this.department = department;
}
public LocalDateTime getRetrievedDate() {
public long getRetrievedDate() {
return retrievedDate;
}
public void setRetrievedDate(LocalDateTime retrievedDate) {
public void setRetrievedDate(long retrievedDate) {
this.retrievedDate = retrievedDate;
}

View File

@ -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();
}
}
}

View 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;
}
}
}

View File

@ -1,21 +1,31 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
public class ItemEntry {
Integer listID;
Integer productID;
Integer quantity;
LocalDateTime addedDate;
long addedDate;
Boolean purchased;
public ItemEntry(Integer listID, ResultSet listRow) throws SQLException {
this.listID = listID;
productID = listRow.getInt(1);
quantity = listRow.getInt(2);
addedDate = listRow.getObject(3, LocalDateTime.class);
addedDate = listRow.getTimestamp(3).toInstant().toEpochMilli();
purchased = listRow.getBoolean(4);
}
@Override
public String toString() {
return "ItemEntry{" +
"listID=" + listID +
", productID=" + productID +
", quantity=" + quantity +
", addedDate=" + addedDate +
", purchased=" + purchased +
'}';
}
public Integer getProductID() {
return productID;
}
@ -32,11 +42,11 @@ public class ItemEntry {
this.quantity = quantity;
}
public LocalDateTime getAddedDate() {
public long getAddedDate() {
return addedDate;
}
public void setAddedDate(LocalDateTime addedDate) {
public void setAddedDate(long addedDate) {
this.addedDate = addedDate;
}

View File

@ -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 {

View File

@ -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);

View File

@ -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;
}
}
}

View File

@ -0,0 +1,45 @@
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, null, 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;
assert (rawIDReturn.getClass() == Integer.class);
assert (injector.getStatementString().contains("INSERT INTO List (name, owner, lastUpdated) VALUES (?, ?, ?)[aname, cognitoID,"));
} catch (SQLException throwables) {
assert shouldThrow;
throwables.printStackTrace();
}
}
}

View File

@ -0,0 +1,63 @@
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 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;
}
}
}

View File

@ -8,18 +8,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"));

View File

@ -6,18 +6,17 @@ 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"));

View File

@ -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;
}

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,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());

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;
}

View File

@ -1,7 +1,6 @@
package com.example.listify.data;
import java.math.BigDecimal;
import java.time.LocalDateTime;
public class Item {
Integer productID;
@ -11,11 +10,11 @@ public class Item {
BigDecimal price;
String imageURL;
String department;
LocalDateTime retrievedDate;
long retrievedDate;
Integer fetchCounts;
public Item(Integer productID, Integer chainID, String upc, String description, BigDecimal price,
String imageURL, String department, LocalDateTime retrievedDate, Integer fetchCounts) {
String imageURL, String department, long retrievedDate, Integer fetchCounts) {
this.productID = productID;
this.chainID = chainID;
this.upc = upc;
@ -37,7 +36,7 @@ public class Item {
", price=" + price +
", imageURL='" + imageURL + '\'' +
", department='" + department + '\'' +
", retrievedDate=" + (retrievedDate == null ? null : retrievedDate) +
", retrievedDate=" + retrievedDate +
", fetchCounts=" + fetchCounts +
'}';
}
@ -98,11 +97,11 @@ public class Item {
this.department = department;
}
public LocalDateTime getRetrievedDate() {
public long getRetrievedDate() {
return retrievedDate;
}
public void setRetrievedDate(LocalDateTime retrievedDate) {
public void setRetrievedDate(long retrievedDate) {
this.retrievedDate = retrievedDate;
}