diff options
| author | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-30 16:47:00 -0400 | 
|---|---|---|
| committer | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-30 16:47:00 -0400 | 
| commit | 81f22a575f26a17ff3845772b33647ad34d39550 (patch) | |
| tree | ea46aef2897c3a46f3346116d47e740d0516b540 /ufund-api/src/test/java/com | |
| parent | 01667682d27a7f3a094cd24fe4fe2c2a2f503660 (diff) | |
| parent | c6bbb29f42eaea7d0c8aebdb7b95be0287cbf4f9 (diff) | |
| download | JellySolutions-81f22a575f26a17ff3845772b33647ad34d39550.tar.gz JellySolutions-81f22a575f26a17ff3845772b33647ad34d39550.tar.bz2 JellySolutions-81f22a575f26a17ff3845772b33647ad34d39550.zip  | |
Merge branch 'main' into list-and-cupboard-component-refactor
# Conflicts:
#	ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java
Diffstat (limited to 'ufund-api/src/test/java/com')
| -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()); +    }  }  | 
