diff options
Diffstat (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java')
-rw-r--r-- | ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java | 323 |
1 files changed, 287 insertions, 36 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 6ef6710..8572ec6 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 @@ -7,10 +7,11 @@ import static java.util.Map.entry; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.*; + +import com.ufund.api.ufundapi.service.AuthService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import org.springframework.http.HttpStatus; import com.ufund.api.ufundapi.DuplicateKeyException; @@ -21,29 +22,44 @@ import com.ufund.api.ufundapi.service.CupboardService; public class CupboardControllerTest { private CupboardController cupboardController; private CupboardService mockCupboardService; + private final String key = "dummyKey"; + private AuthService mockAuthService; @BeforeEach public void setupCupboardDAO() { + mockAuthService = mock(AuthService.class); mockCupboardService = mock(CupboardService.class); - cupboardController = new CupboardController(mockCupboardService); + cupboardController = new CupboardController(mockCupboardService, mockAuthService); + + try { + doThrow().when(mockAuthService).keyHasAccessToCupboard(key); + } catch (Exception ignored) {} } @Test public void createNeed() throws IOException, DuplicateKeyException { String name = "Test"; + String location = "Atlantis"; int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; - var need = new Need(name, type, maxGoal); - when(mockCupboardService.createNeed(name, maxGoal, type)).thenReturn(need); + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); + when(mockCupboardService.createNeed(name, image, location, maxGoal, type, urgent, description)).thenReturn(need); Map<String, Object> needMap = Map.ofEntries( entry("name", "Test"), - entry("maxGoal", 100.0), - entry("type", "MONETARY") + entry("image", ""), + entry("location", "Atlantis"), + entry("maxGoal", 100), + entry("type", "MONETARY"), + entry("urgent", false), + entry("description", "") ); - var res = cupboardController.createNeed(needMap); + var res = cupboardController.createNeed(needMap, key); assertEquals(HttpStatus.OK, res.getStatusCode()); assertEquals(need, res.getBody()); @@ -51,41 +67,94 @@ public class CupboardControllerTest { @Test public void createNeedBadMaxGoal() throws IOException, DuplicateKeyException { - when(mockCupboardService.createNeed("Name", -100, Need.GoalType.MONETARY)).thenThrow(new IllegalArgumentException()); + when(mockCupboardService.createNeed("Test", "", "Atlantis", -100, Need.GoalType.MONETARY, false, "")).thenThrow(new IllegalArgumentException()); Map<String, Object> needMap = Map.ofEntries( - entry("name", "Name"), - entry("maxGoal", -100.0), - entry("type", "MONETARY")); + entry("name", "Test"), + entry("image", ""), + entry("location", "Atlantis"), + entry("maxGoal", -100), + entry("type", "MONETARY"), + entry("urgent", false), + entry("description", "") + ); - var res = cupboardController.createNeed(needMap); + var res = cupboardController.createNeed(needMap, key); assertEquals(HttpStatus.BAD_REQUEST, res.getStatusCode()); } @Test public void createNeedIOException() throws IOException, DuplicateKeyException { - when(mockCupboardService.createNeed("Name", 100, Need.GoalType.MONETARY)).thenThrow(new IOException()); + when(mockCupboardService.createNeed("Test", "", "Atlantis", 100, Need.GoalType.MONETARY, false, "")).thenThrow(new IOException()); Map<String, Object> needMap = Map.ofEntries( - entry("name", "Name"), - entry("maxGoal", 100.0), - entry("type", "MONETARY")); + entry("name", "Test"), + entry("image", ""), + entry("location", "Atlantis"), + entry("maxGoal", 100), + entry("type", "MONETARY"), + entry("urgent", false), + entry("description", "") + ); - var res = cupboardController.createNeed(needMap); + var res = cupboardController.createNeed(needMap, key); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); } @Test + public void createNeedConflict() throws IOException, DuplicateKeyException { + when(mockCupboardService.createNeed("Test", "", "Atlantis", 100, Need.GoalType.MONETARY, false, "")).thenThrow(new DuplicateKeyException("")); + + Map<String, Object> needMap = Map.ofEntries( + entry("name", "Test"), + entry("image", ""), + entry("location", "Atlantis"), + entry("maxGoal", 100), + entry("type", "MONETARY"), + entry("urgent", false), + entry("description", "") + ); + + 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", "Test"), + entry("location", "Atlantis"), + entry("maxGoal", 100), + entry("type", "MONETARY"), + entry("urgent", false) + ); + + 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); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.getNeeds()).thenReturn(new Need[]{need}); var res = cupboardController.getNeeds(); assertEquals(HttpStatus.OK, res.getStatusCode()); - assertArrayEquals(new Need[]{need}, res.getBody()); + assertArrayEquals(new Need[]{need}, (Need[]) res.getBody()); } @Test @@ -104,18 +173,25 @@ public class CupboardControllerTest { var res = cupboardController.getNeeds(); assertEquals(HttpStatus.OK, res.getStatusCode()); - assertArrayEquals(new Need[]{}, res.getBody()); + assertArrayEquals(new Need[]{}, (Need[]) res.getBody()); } @Test public void searchNeeds() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.searchNeeds("Na")).thenReturn(new Need[]{need}); var res = cupboardController.searchNeeds("Na"); assertEquals(HttpStatus.OK, res.getStatusCode()); - assertArrayEquals(new Need[]{need}, res.getBody()); + assertArrayEquals(new Need[]{need}, (Need[]) res.getBody()); } @Test @@ -134,12 +210,19 @@ public class CupboardControllerTest { var res = cupboardController.searchNeeds("Na"); assertEquals(HttpStatus.OK, res.getStatusCode()); - assertArrayEquals(new Need[]{}, res.getBody()); + assertArrayEquals(new Need[]{}, (Need[]) res.getBody()); } @Test public void getNeed() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.getNeed(need.getId())).thenReturn(need); var res = cupboardController.getNeed(need.getId()); @@ -150,7 +233,14 @@ public class CupboardControllerTest { @Test public void getNeedIOException() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.getNeed(need.getId())).thenThrow(new IOException()); var res = cupboardController.getNeed(need.getId()); @@ -160,7 +250,14 @@ public class CupboardControllerTest { @Test public void getNeedFail() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.getNeed(need.getId())).thenReturn(null); var res = cupboardController.getNeed(need.getId()); @@ -171,10 +268,17 @@ public class CupboardControllerTest { @Test public void updateNeeds() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.updateNeed(need, 1)).thenReturn(need); - var res = cupboardController.updateNeed(need, 1); + var res = cupboardController.updateNeed(need, 1, key); assertEquals(HttpStatus.OK, res.getStatusCode()); assertEquals(need, res.getBody()); @@ -182,21 +286,86 @@ public class CupboardControllerTest { @Test public void updateNeedsIOException() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.updateNeed(need, 1)).thenThrow(new IOException()); - var res = cupboardController.updateNeed(need, 1); + var res = cupboardController.updateNeed(need, 1, key); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); } @Test + public void updateNeedMissing() throws IOException { + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); + 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; + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); + 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; + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); + 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); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.getNeed(1)).thenReturn(need); when(mockCupboardService.deleteNeed(1)).thenReturn(true); - var res = cupboardController.deleteNeed(1); + var res = cupboardController.deleteNeed(1, key); assertEquals(HttpStatus.OK, res.getStatusCode()); } @@ -206,18 +375,100 @@ public class CupboardControllerTest { when(mockCupboardService.getNeed(1)).thenReturn(null); when(mockCupboardService.deleteNeed(1)).thenReturn(false); - var res = cupboardController.deleteNeed(1); + var res = cupboardController.deleteNeed(1, key); assertEquals(HttpStatus.NOT_FOUND, res.getStatusCode()); } @Test + public void deleteNeedUnauthorized() throws IOException, IllegalAccessException { + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); + 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); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.getNeed(1)).thenReturn(need); when(mockCupboardService.deleteNeed(1)).thenThrow(new IOException()); - var res = cupboardController.deleteNeed(1); + var res = cupboardController.deleteNeed(1, key); + + 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()); } |