diff --git a/Lambdas/Lists/List/test/TestListAdder.java b/Lambdas/Lists/List/test/TestListAdder.java index f933421..e31dc6f 100644 --- a/Lambdas/Lists/List/test/TestListAdder.java +++ b/Lambdas/Lists/List/test/TestListAdder.java @@ -1,4 +1,5 @@ import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import org.mockito.configuration.IMockitoConfiguration; import static org.mockito.Mockito.*; @@ -11,8 +12,8 @@ import java.util.Map; public class TestListAdder { @Test - public void testListAdderValid() { - testListAdderCore(false); + public void testListAdderValid() throws SQLException { + testListAdderCoreMock(false); } @Test @@ -20,7 +21,7 @@ public class TestListAdder { testListAdderCoreMock(true); } - public void testListAdderCore(boolean shouldThrow) { + public void testListAdderCoreMock(boolean shouldThrow) throws SQLException { StatementInjector injector; ArrayList rsReturns = new ArrayList<>(); rsReturns.add(1); //new listID @@ -31,35 +32,8 @@ public class TestListAdder { assert false; //Error in test infrastructure return; } - ListAdder listAdder = new ListAdder(injector, "cognitoID"); - Map ignore = new HashMap<>(); - Map 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 (?, ?, ?);INSERT INTO ListSharee(listID, userID) VALUES(?, ?);[1, cognitoID]")); - } catch (SQLException throwables) { - assert shouldThrow; - throwables.printStackTrace(); - } - } - public void testListAdderCoreMock(boolean shouldThrow) throws SQLException { - StatementInjector injector; - ArrayList rsReturns = new ArrayList<>(); - rsReturns.add(1); //new listID - injector = mock(StatementInjector.class); - injector.returnedStatement = null; - injector.rsReturns = new ArrayList(rsReturns); - injector.shouldThrow = shouldThrow; - - ListAdder listAdder = new ListAdder(injector, "cognitoID"); + ListAdder listAdder = Mockito.spy(new ListAdder(injector, "cognitoID")); Map ignore = new HashMap<>(); Map body = TestInputUtils.addBody(ignore); body.put("name", "aname"); diff --git a/Lambdas/Lists/List/test/TestListDelete.java b/Lambdas/Lists/List/test/TestListDelete.java index f9cd0f3..d021fee 100644 --- a/Lambdas/Lists/List/test/TestListDelete.java +++ b/Lambdas/Lists/List/test/TestListDelete.java @@ -16,7 +16,7 @@ public class TestListDelete { @Test public void testListDeleterWOAccess() { - testListDeleterCore(false, false); + testListDeleterCoreMock(false, false); } @Test @@ -24,38 +24,6 @@ public class TestListDelete { testListDeleterCoreMock(true, true); } - public void testListDeleterCore(boolean shouldThrow, boolean hasAccess) { - StatementInjector injector; - ArrayList rsReturns = new ArrayList<>(); - rsReturns.add("cognitoID"); - try { - if (!hasAccess) { - rsReturns = null; - } - injector = new StatementInjector(null, rsReturns, shouldThrow); - } catch (SQLException throwables) { - throwables.printStackTrace(); - assert false; //Error in test infrastructure - return; - } - ListDeleter listDeleter = new ListDeleter(injector, "cognitoID"); - Map body = (Map) TestBasicHandler.buildFullSampleMap().get("body"); - HashMap queryParams = (HashMap) TestBasicHandler.buildFullSampleMap().get("body"); - queryParams.put("id", "30"); - - try { - Object rawIDReturn = listDeleter.conductAction(body, queryParams, "cognitoID"); - assert !shouldThrow; - assert (rawIDReturn == null); - System.out.println(injector.getStatementString()); - assert (injector.getStatementString().equals("SELECT * FROM List WHERE (owner = ? AND listID = ?);DELETE FROM ListSharee where listID = ?;DELETE FROM ListProduct where listID = ?;DELETE FROM List WHERE listID = ?;[30]")); - } catch (SQLException throwables) { - assert shouldThrow ; - } catch (AccessControlException accessControlException) { - assert !hasAccess; - } - } - public void testListDeleterCoreMock(boolean shouldThrow, boolean hasAccess) { StatementInjector injector; ArrayList rsReturns = new ArrayList<>(); diff --git a/Lambdas/Lists/List/test/TestListGetter.java b/Lambdas/Lists/List/test/TestListGetter.java index 0f2fa95..eae7862 100644 --- a/Lambdas/Lists/List/test/TestListGetter.java +++ b/Lambdas/Lists/List/test/TestListGetter.java @@ -27,79 +27,6 @@ public class TestListGetter { @Test public void testListGetterError() { conductListGetterTestMock(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 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 ignore = new HashMap<>(); - HashMap 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 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 ignore = new HashMap<>(); - HashMap 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 listIDsReturn = (ArrayList) conductReturn; - System.out.println(listIDsReturn.toString()); - assert (listIDsReturn.toString().equals("[1, 2, 3, 4]")); - } catch (SQLException throwables) { - throwables.printStackTrace(); - assert shouldThrow; - } - } - public void conductListGetterTestMock(boolean shouldThrow) { Integer listID = 1; String name = "aname";