aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java
diff options
context:
space:
mode:
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.java137
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());
+ }
}