diff --git a/Lambdas/Lists/List/test/TestListAdder.java b/Lambdas/Lists/List/test/TestListAdder.java index 1cdef59..3e3c5cc 100644 --- a/Lambdas/Lists/List/test/TestListAdder.java +++ b/Lambdas/Lists/List/test/TestListAdder.java @@ -22,7 +22,7 @@ public class TestListAdder { ArrayList rsReturns = new ArrayList<>(); rsReturns.add(1); //new listID try { - injector = new StatementInjector(null, null, shouldThrow); + injector = new StatementInjector(null, rsReturns, shouldThrow); } catch (SQLException throwables) { throwables.printStackTrace(); assert false; //Error in test infrastructure @@ -35,7 +35,11 @@ public class TestListAdder { try { Object rawIDReturn = listAdder.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); assert !shouldThrow; - assert (rawIDReturn.getClass() == Integer.class); + 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; diff --git a/Lambdas/Lists/ListEntry/src/ListEntryAdder.java b/Lambdas/Lists/ListEntry/src/ListEntryAdder.java index c447385..c94cf54 100644 --- a/Lambdas/Lists/ListEntry/src/ListEntryAdder.java +++ b/Lambdas/Lists/ListEntry/src/ListEntryAdder.java @@ -19,19 +19,15 @@ public class ListEntryAdder implements CallHandler { } public Object conductAction(Map bodyMap, HashMap queryString, String cognitoID) throws SQLException { - try { - PreparedStatement statement = connection.prepareStatement(ITEM_TO_LIST); - statement.setInt(1, (Integer) bodyMap.get("productID")); - statement.setInt(2, (Integer) bodyMap.get("listID")); - statement.setInt(3, (Integer) bodyMap.get("quantity")); - statement.setObject(4, Instant.now().atZone(ZoneOffset.UTC).toLocalDateTime()); - statement.setBoolean(5, (Boolean) bodyMap.get("purchased")); - System.out.println(statement); - statement.executeUpdate(); - connection.commit(); - } finally { - connection.close(); - } + PreparedStatement statement = connection.prepareStatement(ITEM_TO_LIST); + statement.setInt(1, (Integer) bodyMap.get("productID")); + statement.setInt(2, (Integer) bodyMap.get("listID")); + statement.setInt(3, (Integer) bodyMap.get("quantity")); + statement.setObject(4, Instant.now().atZone(ZoneOffset.UTC).toLocalDateTime()); + statement.setBoolean(5, (Boolean) bodyMap.get("purchased")); + System.out.println(statement); + statement.executeUpdate(); + connection.commit(); return null; } } diff --git a/Lambdas/Lists/ListEntry/src/ListEntryDeleter.java b/Lambdas/Lists/ListEntry/src/ListEntryDeleter.java index 71af283..2cb63cd 100644 --- a/Lambdas/Lists/ListEntry/src/ListEntryDeleter.java +++ b/Lambdas/Lists/ListEntry/src/ListEntryDeleter.java @@ -17,16 +17,12 @@ public class ListEntryDeleter implements CallHandler { } public Object conductAction(Map bodyMap, HashMap queryString, String cognitoID) throws SQLException { - try { - PreparedStatement statement = connection.prepareStatement(REMOVE_FROM_LIST); - 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(); - } + PreparedStatement statement = connection.prepareStatement(REMOVE_FROM_LIST); + statement.setInt(1, (Integer) bodyMap.get("productID")); + statement.setInt(2, (Integer) bodyMap.get("listID")); + System.out.println(statement); + statement.executeUpdate(); + connection.commit(); return null; } } diff --git a/Lambdas/Lists/ListEntry/test/TestListEntryAdder.java b/Lambdas/Lists/ListEntry/test/TestListEntryAdder.java new file mode 100644 index 0000000..c14232b --- /dev/null +++ b/Lambdas/Lists/ListEntry/test/TestListEntryAdder.java @@ -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 ignore = new HashMap<>(); + Map 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(); + } + } +} diff --git a/Lambdas/Lists/ListEntry/test/TestListEntryDeleter.java b/Lambdas/Lists/ListEntry/test/TestListEntryDeleter.java new file mode 100644 index 0000000..a0d0c14 --- /dev/null +++ b/Lambdas/Lists/ListEntry/test/TestListEntryDeleter.java @@ -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 ignore = new HashMap<>(); + Map 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(); + } + } +}