diff options
author | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-17 17:10:38 -0400 |
---|---|---|
committer | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-17 17:10:38 -0400 |
commit | c4316368ccd2e724de838fa6648ff390d4f417a6 (patch) | |
tree | da20406da93c33f1fab350802cbdf5be5131560e /ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java | |
parent | b998a4e7ca580f19ab10862de61d7458031027f2 (diff) | |
parent | ed3d0e5e5f9c58c3926ea55a422212f4da1c8849 (diff) | |
download | JellySolutions-c4316368ccd2e724de838fa6648ff390d4f417a6.tar.gz JellySolutions-c4316368ccd2e724de838fa6648ff390d4f417a6.tar.bz2 JellySolutions-c4316368ccd2e724de838fa6648ff390d4f417a6.zip |
Merge branch 'main' of https://github.com/RIT-SWEN-261-02/team-project-2245-swen-261-02-2b
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 | 153 |
1 files changed, 147 insertions, 6 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 ceef215..99ca23c 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 @@ -2,8 +2,11 @@ package com.ufund.api.ufundapi.service; import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.*; import org.junit.jupiter.api.BeforeEach; @@ -19,13 +22,11 @@ import com.ufund.api.ufundapi.persistence.CupboardDAO; public class CupboardServiceTest { private CupboardDAO mockCupboardDAO; - private AuthService mockAuthService; private CupboardService cupboardService; @BeforeEach public void setupCupboardService() { mockCupboardDAO = mock(CupboardDAO.class); - mockAuthService = mock(AuthService.class); cupboardService = new CupboardService(mockCupboardDAO); } @@ -35,15 +36,12 @@ public class CupboardServiceTest { // Setup String name = "Jellyfish"; double maxGoal = 100.00; - int id = 0; GoalType type = GoalType.MONETARY; Need need = new Need(name, type, maxGoal); // When the same id is passed in, our mock User DAO will return the User object - when(mockCupboardDAO.getNeed(id)).thenReturn(need); when(mockCupboardDAO.addNeed(any())).thenReturn(need); when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]); - // Invoke Need response = cupboardService.createNeed(name, maxGoal, type); @@ -53,4 +51,147 @@ public class CupboardServiceTest { assertEquals(need, response); } -} + @Test + public void testCreateNeedBadGoal() throws IOException, DuplicateKeyException { + // Setup + String name = "Jellyfish"; + double maxGoal = -100.00; + GoalType type = GoalType.MONETARY; + Need need = new Need(name, type, maxGoal); + + // 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, maxGoal, type); + }); + } + + @Test + public void testCreateNeedDuplicate() throws IOException, DuplicateKeyException { + // Setup + String name = "Jellyfish"; + double maxGoal = 100.00; + GoalType type = GoalType.MONETARY; + Need need = new Need(name, type, maxGoal); + Need[] needs = { need }; + + // 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(needs); + + // Invoke + // Need response = cupboardService.createNeed(name, maxGoal, type); + + // Analyze + assertThrows(DuplicateKeyException.class, () -> { + cupboardService.createNeed(name, maxGoal, type); + }); + } + + @Test + public void testSearchNeeds() throws IOException, DuplicateKeyException { + // Setup + String name = "Jellyfish"; + double maxGoal = 100.00; + GoalType type = GoalType.MONETARY; + Need need = new Need(name, type, maxGoal); + Need[] needs = { need }; + + // When the same id is passed in, our mock User DAO will return the User object + when(mockCupboardDAO.getNeeds()).thenReturn(needs); + + // Invoke + Need[] response = cupboardService.searchNeeds("Jelly"); + + // Analyze + assertEquals(need, response[0]); + assertEquals(need.getName(), response[0].getName()); + } + + @Test + public void testSearchNeedsFail() throws IOException, DuplicateKeyException { + // Setup + String name = "Jellyfish"; + double maxGoal = 100.00; + GoalType type = GoalType.MONETARY; + Need need = new Need(name, type, maxGoal); + Need[] needs = { need }; + + // When the same id is passed in, our mock User DAO will return the User object + when(mockCupboardDAO.getNeeds()).thenReturn(needs); + + // Invoke + Need[] response = cupboardService.searchNeeds("Octopus"); + + // Analyze + assertArrayEquals(new Need[0], response); + } + + @Test + public void testGetNeed() throws IOException, DuplicateKeyException { + // Setup + String name = "Jellyfish"; + double maxGoal = 100.00; + int id = 0; + GoalType type = GoalType.MONETARY; + Need need = new Need(name, type, maxGoal); + + // When the same id is passed in, our mock User DAO will return the User object + when(mockCupboardDAO.getNeed(id)).thenReturn(need); + + // Invoke + Need response = cupboardService.getNeed(id); + + // Analyze + assertEquals(need, response); + } + + @Test + public void testUpdateNeed() throws IOException, DuplicateKeyException { + // Setup + String name = "Jellyfish"; + double maxGoal = 100.00; + int id = 0; + GoalType type = GoalType.MONETARY; + Need need = new Need(name, type, maxGoal); + Need newNeed = new Need("Octopus", type, maxGoal); + + // When the same id is passed in, our mock User DAO will return the User object + when(mockCupboardDAO.updateNeed(any())).thenReturn(newNeed); + + // Invoke + Need response = cupboardService.updateNeed(newNeed, id); + + // Analyze + assertEquals(newNeed, response); + } + + @Test + public void testDeleteNeed() throws IOException, DuplicateKeyException { + // Setup + String name = "Jellyfish"; + double maxGoal = 100.00; + int id = 0; + GoalType type = GoalType.MONETARY; + Need need = new Need(name, type, maxGoal); + + // When the same id is passed in, our mock User DAO will return the User object + when(mockCupboardDAO.deleteNeed(id)).thenReturn(true); + when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]); + + // Invoke + boolean response = cupboardService.deleteNeed(id); + Need[] responseNeeds = cupboardService.getNeeds(); + + // Analyze + assertTrue(response); + assertArrayEquals(new Need[0], responseNeeds); + } + +}
\ No newline at end of file |