From 78e9791da675783124c76a20f756886005ffa904 Mon Sep 17 00:00:00 2001 From: Akash Keshav <112591754+domesticchores@users.noreply.github.com> Date: Tue, 8 Apr 2025 00:52:47 -0400 Subject: update designdoc and add more test coverage --- .../api/ufundapi/service/CupboardServiceTest.java | 160 +++++++++++++++++++++ .../api/ufundapi/service/UserServiceTest.java | 39 ++++- 2 files changed, 197 insertions(+), 2 deletions(-) (limited to 'ufund-api') 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 @@ -80,6 +80,78 @@ public class CupboardServiceTest { cupboardService.createNeed(name, location, image, maxGoal, type, urgent, description)); } + @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 @@ -197,6 +269,94 @@ public class CupboardServiceTest { assertEquals(newNeed, response); } + @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 diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java index 5adabf1..6234416 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java @@ -3,6 +3,7 @@ package com.ufund.api.ufundapi.service; import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.BeforeEach; @@ -74,6 +75,40 @@ public class UserServiceTest { assertEquals(user, userService.getUser(username)); } + @Test + public void testGetUserBlank() throws IOException { + // Setup + String username = "Jelly"; + String password = "Fish"; + User user = User.create(username, password); + + // Mock + when(mockUserDAO.getUser("notReal")).thenReturn(user); + + // Analyze + assertNull(userService.getUser(username)); + } + + @Test public void testGetUserCount() throws IOException { + // Setup + String username = "Jelly"; + String password = "Fish"; + User user = User.create(username, password); + + // Mock + when(mockUserDAO.getUser(username)).thenReturn(null); + when(mockUserDAO.addUser(any())).thenReturn(user); + when(mockUserDAO.getUserCount()).thenReturn(1); + + // Invoke + mockUserDAO.addUser(user); + int userCount = userService.getUserCount(); + + // Analyze + assertEquals(userCount, 1); + + } + @Test public void testUpdateUser() throws IOException { // Setup @@ -118,10 +153,10 @@ public class UserServiceTest { User user = User.create(username, password); // Mock - when(mockUserDAO.deleteUser(username)).thenReturn(true); + when(mockUserDAO.deleteUser(user.getUsername())).thenReturn(true); // Analyze - assertTrue(userService.deleteUser(username)); + assertTrue(userService.deleteUser(user.getUsername())); } } -- cgit v1.2.3