diff options
| author | sowgro <tpoke.ferrari@gmail.com> | 2025-03-30 16:26:12 -0400 | 
|---|---|---|
| committer | sowgro <tpoke.ferrari@gmail.com> | 2025-03-30 16:26:12 -0400 | 
| commit | 27b2b418cc330e37f5802a81d678dd27259ee1e0 (patch) | |
| tree | 893231478cb132731546ef7eb6b5c94d0c748091 /ufund-api/src/test/java/com/ufund/api | |
| parent | e1eb3f16e10042c2539b56d6c2d2e33f07abf7d9 (diff) | |
| parent | c6bbb29f42eaea7d0c8aebdb7b95be0287cbf4f9 (diff) | |
| download | JellySolutions-27b2b418cc330e37f5802a81d678dd27259ee1e0.tar.gz JellySolutions-27b2b418cc330e37f5802a81d678dd27259ee1e0.tar.bz2 JellySolutions-27b2b418cc330e37f5802a81d678dd27259ee1e0.zip  | |
Merge branch 'main' into css
# Conflicts:
#	ufund-ui/src/app/app-routing.module.ts
#	ufund-ui/src/app/app.module.ts
#	ufund-ui/src/app/components/login/login.component.html
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 | 137 | 
1 files changed, 134 insertions, 3 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 89697bf..d775d14 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); @@ -63,7 +64,8 @@ public class CupboardControllerTest {          Map<String, Object> needMap = Map.ofEntries(                  entry("name", "Name"),                  entry("maxGoal", -100.0), -                entry("type", "MONETARY")); +                entry("type", "MONETARY") +        );          var res = cupboardController.createNeed(needMap, key); @@ -77,7 +79,8 @@ public class CupboardControllerTest {          Map<String, Object> needMap = Map.ofEntries(                  entry("name", "Name"),                  entry("maxGoal", 100.0), -                entry("type", "MONETARY")); +                entry("type", "MONETARY") +        );          var res = cupboardController.createNeed(needMap, key); @@ -85,6 +88,36 @@ public class CupboardControllerTest {      }      @Test +    public void createNeedConflict() throws IOException, DuplicateKeyException { +        when(mockCupboardService.createNeed("Name", 100, Need.GoalType.MONETARY)).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 {          var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);          when(mockCupboardService.getNeeds()).thenReturn(new Need[]{need}); @@ -198,6 +231,36 @@ public class CupboardControllerTest {      }      @Test +    public void updateNeedMissing() throws IOException { +        var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); +        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 { +        var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); +        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 { +        var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); +        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 {          var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);          when(mockCupboardService.getNeed(1)).thenReturn(need); @@ -219,6 +282,17 @@ public class CupboardControllerTest {      }      @Test +    public void deleteNeedUnauthorized() throws IOException, IllegalAccessException { +        var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); +        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 {          var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);          when(mockCupboardService.getNeed(1)).thenReturn(need); @@ -228,4 +302,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()); +    }  }  | 
