diff options
Diffstat (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java')
-rw-r--r-- | ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java index 2a3c8ee..da098d0 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java @@ -81,6 +81,78 @@ public class CupboardServiceTest { } @Test + public void testCreateNeedBadPhysicalGoal() throws IOException { + // Setup + String name = "Jellyfish"; + String location = "Atlantis"; + double maxGoal = 1.23; + GoalType type = Need.GoalType.PHYSICAL; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, image, location, maxGoal, type, urgent, description); + + // When the same id is passed in, our mock User DAO will return the User object + when(mockCupboardDAO.addNeed(any())).thenReturn(need); + when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]); + + // Invoke + // Need response = cupboardService.createNeed(name, maxGoal, type); + + // Analyze + assertThrows(IllegalArgumentException.class, () -> + cupboardService.createNeed(name, location, image, maxGoal, type, urgent, description)); + } + + @Test + public void testCreateNeedBadBlankFields() throws IOException { + // Setup + String name = ""; + String location = "Atlantis"; + double maxGoal = 1.23; + GoalType type = Need.GoalType.PHYSICAL; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, image, location, maxGoal, type, urgent, description); + + // When the same id is passed in, our mock User DAO will return the User object + when(mockCupboardDAO.addNeed(any())).thenReturn(need); + when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]); + + // Invoke + // Need response = cupboardService.createNeed(name, maxGoal, type); + + // Analyze + assertThrows(IllegalArgumentException.class, () -> + cupboardService.createNeed(name, location, image, maxGoal, type, urgent, description)); + } + + @Test + public void testCreateNeedNullFields() throws IOException { + // Setup + String name = ""; + String location = "Atlantis"; + double maxGoal = 100.0; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, image, location, maxGoal, type, urgent, description); + + // When the same id is passed in, our mock User DAO will return the User object + when(mockCupboardDAO.addNeed(any())).thenReturn(need); + when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]); + + // Invoke + // Need response = cupboardService.createNeed(name, maxGoal, type); + + // Analyze + assertThrows(IllegalArgumentException.class, () -> + cupboardService.createNeed(name, location, image, maxGoal, type, urgent, description)); + } + + @Test public void testCreateNeedDuplicate() throws IOException { // Setup String name = "Jellyfish"; @@ -198,6 +270,94 @@ public class CupboardServiceTest { } @Test + public void testUpdateNeedBadGoal() throws IOException { + // Setup + String name = "Jellyfish"; + String location = "Atlantis"; + double maxGoal = 100.0; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, image, location, maxGoal, type, urgent, description); + // goal should not be 0, should throw error + Need newNeed = new Need(name, image, location, 0, type, urgent, description); + + // When the same id is passed in, our mock User DAO will return the User object + when(mockCupboardDAO.updateNeed(any())).thenReturn(newNeed); + + // Invoke and Analyze + assertThrows(IllegalArgumentException.class, () -> + cupboardService.updateNeed(newNeed, need.getId())); + } + + @Test + public void testUpdateNeedBadPhysicalGoal() throws IOException { + // Setup + String name = "Jellyfish"; + String location = "Atlantis"; + double maxGoal = 10; + GoalType type = Need.GoalType.PHYSICAL; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, image, location, maxGoal, type, urgent, description); + // goal should be integer, should throw error + Need newNeed = new Need(name, image, location, 1.23, type, urgent, description); + + // When the same id is passed in, our mock User DAO will return the User object + when(mockCupboardDAO.updateNeed(any())).thenReturn(newNeed); + + // Invoke and Analyze + assertThrows(IllegalArgumentException.class, () -> + cupboardService.updateNeed(newNeed, need.getId())); + } + + @Test + public void testUpdateNeedBlankFields() throws IOException { + // Setup + String name = "Jellyfish"; + String location = "Atlantis"; + double maxGoal = 10; + GoalType type = Need.GoalType.PHYSICAL; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, image, location, maxGoal, type, urgent, description); + // passed in blank name, should throw error + Need newNeed = new Need("", image, location, maxGoal, type, urgent, description); + + // When the same id is passed in, our mock User DAO will return the User object + when(mockCupboardDAO.updateNeed(any())).thenReturn(newNeed); + + // Invoke and Analyze + assertThrows(IllegalArgumentException.class, () -> + cupboardService.updateNeed(newNeed, need.getId())); + } + + @Test + public void testUpdateNeedNotMatchingIDs() throws IOException { + // Setup + String name = "Jellyfish"; + String location = "Atlantis"; + double maxGoal = 10; + GoalType type = Need.GoalType.PHYSICAL; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, image, location, maxGoal, type, urgent, description); + // passed in blank name, should throw error + Need newNeed = new Need(name, image, location, maxGoal, type, urgent, description); + + // When the same id is passed in, our mock User DAO will return the User object + when(mockCupboardDAO.updateNeed(any())).thenReturn(newNeed); + + // Invoke and Analyze + assertThrows(IllegalArgumentException.class, () -> + cupboardService.updateNeed(newNeed, -1)); + } + + @Test public void testDeleteNeed() throws IOException { // Setup String name = "Jellyfish"; |