mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2026-03-10 18:55:03 +00:00
Lambda unit test infrastructure
Unit test infrastructure for testing GET and POST Lambdas
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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,50 +7,48 @@ 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);
|
||||
getLists.setString(1, cognitoID);
|
||||
System.out.println(getLists);
|
||||
ResultSet getListsResults = getLists.executeQuery();
|
||||
System.out.println(getListsResults);
|
||||
ArrayList<Integer> listIds = new ArrayList<>();
|
||||
while (getListsResults.next()) {
|
||||
listIds.add(getListsResults.getInt(1));
|
||||
}
|
||||
return listIds;
|
||||
Integer id = Integer.parseInt(queryMap.get("id"));
|
||||
if (id == -1) {
|
||||
PreparedStatement getLists = connection.prepareStatement(GET_LISTS);
|
||||
getLists.setString(1, cognitoID);
|
||||
System.out.println(getLists);
|
||||
ResultSet getListsResults = getLists.executeQuery();
|
||||
System.out.println(getListsResults);
|
||||
ArrayList<Integer> listIds = new ArrayList<>();
|
||||
while (getListsResults.next()) {
|
||||
listIds.add(getListsResults.getInt(1));
|
||||
}
|
||||
PreparedStatement getList = connection.prepareStatement(GET_LIST);
|
||||
getList.setInt(1, id);
|
||||
System.out.println(getList);
|
||||
ResultSet getListResults = getList.executeQuery();
|
||||
getListResults.first();
|
||||
System.out.println(getListResults);
|
||||
List retrievedList = new List(getListResults);
|
||||
System.out.println(retrievedList);
|
||||
PreparedStatement getListEntries = connection.prepareStatement(GET_ENTRIES);
|
||||
getListEntries.setInt(1, id);
|
||||
ResultSet getEntryResults = getListEntries.executeQuery();
|
||||
while (getEntryResults.next()) {
|
||||
retrievedList.addItemEntry(new ItemEntry(id, getEntryResults));
|
||||
}
|
||||
System.out.println(retrievedList);
|
||||
return retrievedList;
|
||||
return listIds;
|
||||
}
|
||||
PreparedStatement getList = connection.prepareStatement(GET_LIST);
|
||||
getList.setInt(1, id);
|
||||
System.out.println(getList);
|
||||
ResultSet getListResults = getList.executeQuery();
|
||||
getListResults.first();
|
||||
System.out.println(getListResults);
|
||||
List retrievedList = new List(getListResults);
|
||||
System.out.println(retrievedList);
|
||||
PreparedStatement getListEntries = connection.prepareStatement(GET_ENTRIES);
|
||||
getListEntries.setInt(1, id);
|
||||
ResultSet getEntryResults = getListEntries.executeQuery();
|
||||
while (getEntryResults.next()) {
|
||||
retrievedList.addItemEntry(new ItemEntry(id, getEntryResults));
|
||||
}
|
||||
System.out.println(retrievedList);
|
||||
return retrievedList;
|
||||
}
|
||||
}
|
||||
|
||||
45
Lambdas/Lists/List/test/TestListAdder.java
Normal file
45
Lambdas/Lists/List/test/TestListAdder.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
63
Lambdas/Lists/List/test/TestListGetter.java
Normal file
63
Lambdas/Lists/List/test/TestListGetter.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user