Lambda Delete Tests

Also include a contract fix for ListEntryDeleter
This commit is contained in:
NMerz 2020-10-08 19:01:09 -04:00
parent 508818c700
commit b07fead480
5 changed files with 112 additions and 25 deletions

View File

@ -22,7 +22,7 @@ public class TestListAdder {
ArrayList<Object> rsReturns = new ArrayList<>(); ArrayList<Object> rsReturns = new ArrayList<>();
rsReturns.add(1); //new listID rsReturns.add(1); //new listID
try { try {
injector = new StatementInjector(null, null, shouldThrow); injector = new StatementInjector(null, rsReturns, shouldThrow);
} catch (SQLException throwables) { } catch (SQLException throwables) {
throwables.printStackTrace(); throwables.printStackTrace();
assert false; //Error in test infrastructure assert false; //Error in test infrastructure
@ -35,7 +35,11 @@ public class TestListAdder {
try { try {
Object rawIDReturn = listAdder.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); Object rawIDReturn = listAdder.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID");
assert !shouldThrow; 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,")); assert (injector.getStatementString().contains("INSERT INTO List (name, owner, lastUpdated) VALUES (?, ?, ?)[aname, cognitoID,"));
} catch (SQLException throwables) { } catch (SQLException throwables) {
assert shouldThrow; assert shouldThrow;

View File

@ -19,19 +19,15 @@ public class ListEntryAdder implements CallHandler {
} }
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException { public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
try { PreparedStatement statement = connection.prepareStatement(ITEM_TO_LIST);
PreparedStatement statement = connection.prepareStatement(ITEM_TO_LIST); statement.setInt(1, (Integer) bodyMap.get("productID"));
statement.setInt(1, (Integer) bodyMap.get("productID")); statement.setInt(2, (Integer) bodyMap.get("listID"));
statement.setInt(2, (Integer) bodyMap.get("listID")); statement.setInt(3, (Integer) bodyMap.get("quantity"));
statement.setInt(3, (Integer) bodyMap.get("quantity")); statement.setObject(4, Instant.now().atZone(ZoneOffset.UTC).toLocalDateTime());
statement.setObject(4, Instant.now().atZone(ZoneOffset.UTC).toLocalDateTime()); statement.setBoolean(5, (Boolean) bodyMap.get("purchased"));
statement.setBoolean(5, (Boolean) bodyMap.get("purchased")); System.out.println(statement);
System.out.println(statement); statement.executeUpdate();
statement.executeUpdate(); connection.commit();
connection.commit();
} finally {
connection.close();
}
return null; return null;
} }
} }

View File

@ -17,16 +17,12 @@ public class ListEntryDeleter implements CallHandler {
} }
public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException { public Object conductAction(Map<String, Object> bodyMap, HashMap<String, String> queryString, String cognitoID) throws SQLException {
try { PreparedStatement statement = connection.prepareStatement(REMOVE_FROM_LIST);
PreparedStatement statement = connection.prepareStatement(REMOVE_FROM_LIST); statement.setInt(1, (Integer) bodyMap.get("productID"));
statement.setInt(1, (Integer) bodyMap.get("ProductID")); statement.setInt(2, (Integer) bodyMap.get("listID"));
statement.setInt(2, (Integer) bodyMap.get("ListID")); System.out.println(statement);
System.out.println(statement); statement.executeUpdate();
statement.executeUpdate(); connection.commit();
connection.commit();
} finally {
connection.close();
}
return null; return null;
} }
} }

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

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