diff options
Diffstat (limited to 'ufund-api/src/test/java/com')
| -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..884b8b0 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  | 
