From a12b1ae8700fdcb8a86b8e9789616a94258d72e6 Mon Sep 17 00:00:00 2001 From: NMerz Date: Sun, 1 Nov 2020 12:46:03 -0500 Subject: [PATCH] Update lambda tests Update lambda tests for new and altered lambdas --- Lambdas/Lists/List/test/TestListAdder.java | 2 +- Lambdas/Lists/List/test/TestListDelete.java | 57 +++++++++++++++++++ .../Lists/ListShare/test/TestListSharer.java | 45 +++++++++++++++ Lambdas/Lists/User/test/TestUserDeleter.java | 17 ++++-- 4 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 Lambdas/Lists/List/test/TestListDelete.java create mode 100644 Lambdas/Lists/ListShare/test/TestListSharer.java diff --git a/Lambdas/Lists/List/test/TestListAdder.java b/Lambdas/Lists/List/test/TestListAdder.java index 3e3c5cc..9b5f667 100644 --- a/Lambdas/Lists/List/test/TestListAdder.java +++ b/Lambdas/Lists/List/test/TestListAdder.java @@ -40,7 +40,7 @@ public class TestListAdder { 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 (?, ?, ?);INSERT INTO ListSharee(listID, userID) VALUES(?, ?);[1, cognitoID]")); } catch (SQLException throwables) { assert shouldThrow; throwables.printStackTrace(); diff --git a/Lambdas/Lists/List/test/TestListDelete.java b/Lambdas/Lists/List/test/TestListDelete.java new file mode 100644 index 0000000..70ab8e3 --- /dev/null +++ b/Lambdas/Lists/List/test/TestListDelete.java @@ -0,0 +1,57 @@ +import org.junit.Test; + +import java.security.AccessControlException; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public class TestListDelete { + + @Test + public void testListDeleterValid() { + testListDeleterCore(false, true); + } + + @Test + public void testListDeleterWOAccess() { + testListDeleterCore(false, false); + } + + @Test + public void testListDeleterError() { + testListDeleterCore(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; + } + } +} diff --git a/Lambdas/Lists/ListShare/test/TestListSharer.java b/Lambdas/Lists/ListShare/test/TestListSharer.java new file mode 100644 index 0000000..77ce5be --- /dev/null +++ b/Lambdas/Lists/ListShare/test/TestListSharer.java @@ -0,0 +1,45 @@ +import org.junit.Test; + +import java.security.AccessControlException; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class TestListSharer { + + @Test + public void testListSharerWOAccess() { + testListEntryAdderCore(false, false); + } + + @Test + public void testListSharerError() { + testListEntryAdderCore(true, true); + } + + public void testListEntryAdderCore(boolean shouldThrow, boolean hasAccess) { + StatementInjector injector; + try { + injector = new StatementInjector(null, null, shouldThrow); + } catch (SQLException throwables) { + throwables.printStackTrace(); + assert false; //Error in test infrastructure + return; + } + ListSharer listSharer = new ListSharer(injector, "cognitoID"); + Map ignore = new HashMap<>(); + Map body = TestInputUtils.addBody(ignore); + body.put("listID", 49); + + try { + listSharer.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); + assert !shouldThrow; + 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; + } catch (AccessControlException accessControlException) { + assert !hasAccess; + } + } +} diff --git a/Lambdas/Lists/User/test/TestUserDeleter.java b/Lambdas/Lists/User/test/TestUserDeleter.java index 89b3a72..9ec14f3 100644 --- a/Lambdas/Lists/User/test/TestUserDeleter.java +++ b/Lambdas/Lists/User/test/TestUserDeleter.java @@ -1,14 +1,17 @@ import org.junit.Test; +import java.nio.file.NoSuchFileException; import java.sql.SQLException; public class TestUserDeleter { @Test - public void TestUserDelete(){ - try { - testUserDeleter(false); - assert(false); - } catch (Exception e) {} + public void testUserDeleteFileUsage(){ + testUserDeleter(false); + } + + @Test + public void testUserDeleteInvalid(){ + testUserDeleter(true); } public void testUserDeleter(boolean shouldThrow) { @@ -24,7 +27,9 @@ public class TestUserDeleter { try { userDeleter.conductAction(null, null, cognitoID); } catch (SQLException e) { - e.printStackTrace(); + assert shouldThrow; + } catch (Exception e) { + assert e.getClass().equals(NoSuchFileException.class); } } }