diff options
Diffstat (limited to 'ufund-api/src/test/java/com/ufund/api')
| -rw-r--r-- | ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java | 151 | 
1 files changed, 150 insertions, 1 deletions
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java index 31e085b..4702771 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java @@ -23,10 +23,11 @@ public class CupboardControllerTest {      private CupboardController cupboardController;      private CupboardService mockCupboardService;      private final String key = "dummyKey"; +    private AuthService mockAuthService;      @BeforeEach      public void setupCupboardDAO() { -        AuthService mockAuthService = mock(AuthService.class); +        mockAuthService = mock(AuthService.class);          mockCupboardService = mock(CupboardService.class);          cupboardController = new CupboardController(mockCupboardService, mockAuthService); @@ -95,6 +96,36 @@ public class CupboardControllerTest {      }      @Test +    public void createNeedConflict() throws IOException, DuplicateKeyException { +        when(mockCupboardService.createNeed("Name", "Atlantis", 100, Need.GoalType.MONETARY, false)).thenThrow(new DuplicateKeyException("")); + +        Map<String, Object> needMap = Map.ofEntries( +                entry("name", "Name"), +                entry("maxGoal", 100.0), +                entry("type", "MONETARY") +        ); + +        var res = cupboardController.createNeed(needMap, key); + +        assertEquals(HttpStatus.CONFLICT, res.getStatusCode()); +    } + +    @Test +    public void createNeedUnauthorized() throws IOException, IllegalAccessException { +        doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToCupboard(key); + +        Map<String, Object> needMap = Map.ofEntries( +                entry("name", "Name"), +                entry("maxGoal", 100.0), +                entry("type", "MONETARY") +        ); + +        var res = cupboardController.createNeed(needMap, key); + +        assertEquals(HttpStatus.UNAUTHORIZED, res.getStatusCode()); +    } + +    @Test      public void getNeeds() throws IOException {          String name = "Test";          String location = "Atlantis"; @@ -243,6 +274,51 @@ public class CupboardControllerTest {      }      @Test +    public void updateNeedMissing() throws IOException { +        String name = "Test"; +        String location = "Atlantis"; +        int maxGoal = 100; +        GoalType type = Need.GoalType.MONETARY; +        boolean urgent = false; +        var need = new Need(name, location, maxGoal, type, urgent); +        when(mockCupboardService.updateNeed(need, 1)).thenReturn(null); + +        var res = cupboardController.updateNeed(need, 1, key); + +        assertEquals(HttpStatus.NOT_FOUND, res.getStatusCode()); +    } + +    @Test +    public void updateNeedBadRequest() throws IOException { +        String name = "Test"; +        String location = "Atlantis"; +        int maxGoal = 100; +        GoalType type = Need.GoalType.MONETARY; +        boolean urgent = false; +        var need = new Need(name, location, maxGoal, type, urgent); +        when(mockCupboardService.updateNeed(need, 1)).thenThrow(new IllegalArgumentException()); + +        var res = cupboardController.updateNeed(need, 1, key); + +        assertEquals(HttpStatus.BAD_REQUEST, res.getStatusCode()); +    } + +    @Test +    public void updateNeedUnauthorized() throws IOException, IllegalAccessException { +        String name = "Test"; +        String location = "Atlantis"; +        int maxGoal = 100; +        GoalType type = Need.GoalType.MONETARY; +        boolean urgent = false; +        var need = new Need(name, location, maxGoal, type, urgent); +        doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToCupboard(key); + +        var res = cupboardController.updateNeed(need, 1, key); + +        assertEquals(HttpStatus.UNAUTHORIZED, res.getStatusCode()); +    } + +    @Test      public void deleteNeed() throws IOException {          String name = "Test";          String location = "Atlantis"; @@ -269,6 +345,22 @@ public class CupboardControllerTest {      }      @Test +    public void deleteNeedUnauthorized() throws IOException, IllegalAccessException { +        String name = "Test"; +        String location = "Atlantis"; +        int maxGoal = 100; +        GoalType type = Need.GoalType.MONETARY; +        boolean urgent = false; +        var need = new Need(name, location, maxGoal, type, urgent); +        when(mockCupboardService.getNeed(1)).thenReturn(need); +        doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToCupboard(key); + +        var res = cupboardController.deleteNeed(1, key); + +        assertEquals(HttpStatus.UNAUTHORIZED, res.getStatusCode()); +    } + +    @Test      public void deleteNeedIOException() throws IOException {          String name = "Test";          String location = "Atlantis"; @@ -283,4 +375,61 @@ public class CupboardControllerTest {          assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());      } + +    @Test +    public void checkoutNeeds() throws IOException, IllegalAccessException { +        doNothing().when(mockCupboardService).checkoutNeed(0, 20, key); + + +        Map<String, Integer> needMap = Map.ofEntries( +                entry("needID", 0), +                entry("amount", 20) +        ); + +        var res = cupboardController.checkoutNeeds(needMap, key); + +        assertEquals(HttpStatus.OK, res.getStatusCode()); +    } + +    @Test +    public void checkoutNeedsBadRequest() throws IOException, IllegalAccessException { +        doThrow(new IllegalArgumentException()).when(mockCupboardService).checkoutNeed(0, 20, key); + +        Map<String, Integer> needMap = Map.ofEntries( +                entry("needID", 0), +                entry("amount", 20) +        ); + +        var res = cupboardController.checkoutNeeds(needMap, key); + +        assertEquals(HttpStatus.BAD_REQUEST, res.getStatusCode()); +    } + +    @Test +    public void checkoutNeedsUnauthorized() throws IOException, IllegalAccessException { +        doThrow(new IllegalAccessException()).when(mockCupboardService).checkoutNeed(0, 20, key); + +        Map<String, Integer> needMap = Map.ofEntries( +                entry("needID", 0), +                entry("amount", 20) +        ); + +        var res = cupboardController.checkoutNeeds(needMap, key); + +        assertEquals(HttpStatus.UNAUTHORIZED, res.getStatusCode()); +    } + +    @Test +    public void checkoutNeedsInternalError() throws IOException, IllegalAccessException { +        doThrow(new IOException()).when(mockCupboardService).checkoutNeed(0, 20, key); + +        Map<String, Integer> needMap = Map.ofEntries( +                entry("needID", 0), +                entry("amount", 20) +        ); + +        var res = cupboardController.checkoutNeeds(needMap, key); + +        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); +    }  }  | 
