From 337c2e117b9b886ec6b022b8fc7ebfbea18840cf Mon Sep 17 00:00:00 2001 From: Adam Ding Date: Tue, 1 Dec 2020 21:11:25 -0500 Subject: [PATCH] Picture tests addec --- Lambdas/Lists/Picture/src/PictureGetter.java | 2 +- Lambdas/Lists/Picture/src/PicturePutter.java | 3 ++ .../Lists/Picture/test/TestPictureGetter.java | 43 +++++++++++++++++++ .../Lists/Picture/test/TestPicturePutter.java | 40 +++++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 Lambdas/Lists/Picture/test/TestPictureGetter.java create mode 100644 Lambdas/Lists/Picture/test/TestPicturePutter.java diff --git a/Lambdas/Lists/Picture/src/PictureGetter.java b/Lambdas/Lists/Picture/src/PictureGetter.java index 136e926..33a331f 100644 --- a/Lambdas/Lists/Picture/src/PictureGetter.java +++ b/Lambdas/Lists/Picture/src/PictureGetter.java @@ -19,7 +19,7 @@ public class PictureGetter implements CallHandler { @Override public Object conductAction(Map bodyMap, HashMap queryMap, String cognitoID) throws SQLException { PreparedStatement statement = connection.prepareStatement(GET_ITEM); - if (!queryMap.get("id").toString().equals("profile")) { + if (!queryMap.containsKey("id") || !queryMap.get("id").toString().equals("profile")) { throw new IllegalArgumentException("Only profile pictures are currently supported."); } statement.setString(1, cognitoID); diff --git a/Lambdas/Lists/Picture/src/PicturePutter.java b/Lambdas/Lists/Picture/src/PicturePutter.java index fadff15..3f14a87 100644 --- a/Lambdas/Lists/Picture/src/PicturePutter.java +++ b/Lambdas/Lists/Picture/src/PicturePutter.java @@ -18,6 +18,9 @@ public class PicturePutter implements CallHandler { public Object conductAction(Map bodyMap, HashMap queryString, String cognitoID) throws SQLException { PreparedStatement storePicture = connection.prepareStatement(STORE_PICTURE_SQL); + if(!bodyMap.containsKey("base64EncodedImage")) { + throw new IllegalArgumentException("Base64EncodedImage not found"); + } storePicture.setString(1, cognitoID); storePicture.setString(2, bodyMap.get("base64EncodedImage").toString()); System.out.println(storePicture); diff --git a/Lambdas/Lists/Picture/test/TestPictureGetter.java b/Lambdas/Lists/Picture/test/TestPictureGetter.java new file mode 100644 index 0000000..2af43e3 --- /dev/null +++ b/Lambdas/Lists/Picture/test/TestPictureGetter.java @@ -0,0 +1,43 @@ +import org.junit.Test; +import org.mockito.Mockito; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class TestPictureGetter { + + @Test + public void testPictureGetterValid() { + testPictureGetter(false); + } + + @Test + public void testPictureGetterError() { + testPictureGetter(true); + } + + public void testPictureGetter(boolean shouldThrow) { + StatementInjector injector; + try { + injector = new StatementInjector(null, null, shouldThrow); + } catch (SQLException throwables) { + throwables.printStackTrace(); + assert false; //Error in test infrastructure + return; + } + PictureGetter pictureGetter = Mockito.spy(new PictureGetter(injector, "cognitoID")); + Map ignore = new HashMap<>(); + Map body = TestInputUtils.addBody(ignore); + body.put("id", 1); + body.put("profile", 1); + + try { + Object rawIDReturn = pictureGetter.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); + assert (rawIDReturn != null); + } catch (SQLException throwables) { + assert shouldThrow; + throwables.printStackTrace(); + } + } +} diff --git a/Lambdas/Lists/Picture/test/TestPicturePutter.java b/Lambdas/Lists/Picture/test/TestPicturePutter.java new file mode 100644 index 0000000..fc30141 --- /dev/null +++ b/Lambdas/Lists/Picture/test/TestPicturePutter.java @@ -0,0 +1,40 @@ +import org.junit.Test; +import org.mockito.Mockito; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class TestPicturePutter { + + @Test + public void testPicturePutterValid() { + testPicturePutter(false); + } + + @Test + public void testPicturePutterError() { + testPicturePutter(true); + } + + public void testPicturePutter(boolean shouldThrow) { + StatementInjector injector; + try { + injector = new StatementInjector(null, null, shouldThrow); + } catch (SQLException throwables) { + throwables.printStackTrace(); + assert false; //Error in test infrastructure + return; + } + PicturePutter picturePutter = Mockito.spy(new PicturePutter(injector, "cognitoID")); + Map ignore = new HashMap<>(); + Map body = TestInputUtils.addBody(ignore); + try { + Object rawIDReturn = picturePutter.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); + assert (rawIDReturn == null); + } catch (SQLException throwables) { + assert shouldThrow; + throwables.printStackTrace(); + } + } +}