aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java
diff options
context:
space:
mode:
authorsowgro <tpoke.ferrari@gmail.com>2025-03-17 17:17:06 -0400
committersowgro <tpoke.ferrari@gmail.com>2025-03-17 17:17:06 -0400
commitbaf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754 (patch)
treee9213224b8f1b35b860f016a6a3d1318def8aae2 /ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java
parentbf33fa3ca9f29b1e75cc077ae2eaaf4f5725e4b3 (diff)
parentd737551fba5617843f3014be6994490dd4328183 (diff)
downloadJellySolutions-baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754.tar.gz
JellySolutions-baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754.tar.bz2
JellySolutions-baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754.zip
Merge remote-tracking branch 'origin/main' into cupboard-component
# Conflicts: # ufund-api/data/cupboard.json # ufund-ui/src/app/app.module.ts
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.java157
1 files changed, 125 insertions, 32 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 04ce41d..100bf09 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
@@ -1,42 +1,86 @@
package com.ufund.api.ufundapi.controller;
-import com.ufund.api.ufundapi.model.Need;
-import com.ufund.api.ufundapi.persistence.CupboardFileDao;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.springframework.http.HttpStatus;
-
import java.io.IOException;
+import java.util.Map;
+import static java.util.Map.entry;
-import static org.junit.jupiter.api.Assertions.*;
+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 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;
+import com.ufund.api.ufundapi.model.Need;
+import com.ufund.api.ufundapi.model.Need.GoalType;
+import com.ufund.api.ufundapi.service.CupboardService;
public class CupboardControllerTest {
private CupboardController cupboardController;
- private CupboardFileDao mockCupboardDAO;
+ private CupboardService mockCupboardService;
@BeforeEach
public void setupCupboardDAO() {
- mockCupboardDAO = mock(CupboardFileDao.class);
- cupboardController = new CupboardController(mockCupboardDAO);
+ mockCupboardService = mock(CupboardService.class);
+ cupboardController = new CupboardController(mockCupboardService);
}
@Test
- public void createNeed() throws IOException {
- var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.createNeed(need)).thenReturn(need);
+ public void createNeed() throws IOException, DuplicateKeyException {
+ String name = "Test";
+ int maxGoal = 100;
+ GoalType type = Need.GoalType.MONETARY;
+ var need = new Need(name, type, maxGoal);
+ when(mockCupboardService.createNeed(name, maxGoal, type)).thenReturn(need);
+
- var res = cupboardController.createNeed(need);
+ Map<String, String> needMap = Map.ofEntries(
+ entry("name", "Test"),
+ entry("maxGoal", "100"),
+ entry("goalType", "MONETARY")
+ );
+
+ var res = cupboardController.createNeed(needMap);
assertEquals(HttpStatus.OK, res.getStatusCode());
assertEquals(need, res.getBody());
}
@Test
- public void getNeeds() {
+ public void createNeedBadMaxGoal() throws IOException, DuplicateKeyException {
+ when(mockCupboardService.createNeed("Name", -100, Need.GoalType.MONETARY)).thenThrow(new IllegalArgumentException());
+
+ Map<String, String> needMap = Map.ofEntries(
+ entry("name", "Name"),
+ entry("maxGoal", "-100"),
+ entry("goalType", "MONETARY"));
+
+ var res = cupboardController.createNeed(needMap);
+
+ assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, res.getStatusCode());
+ }
+
+ @Test
+ public void createNeedIOException() throws IOException, DuplicateKeyException {
+ when(mockCupboardService.createNeed("Name", 100, Need.GoalType.MONETARY)).thenThrow(new IOException());
+
+ Map<String, String> needMap = Map.ofEntries(
+ entry("name", "Name"),
+ entry("maxGoal", "100"),
+ entry("goalType", "MONETARY"));
+
+ var res = cupboardController.createNeed(needMap);
+
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());
+ }
+
+ @Test
+ public void getNeeds() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.getNeeds()).thenReturn(new Need[]{need});
+ when(mockCupboardService.getNeeds()).thenReturn(new Need[]{need});
var res = cupboardController.getNeeds();
@@ -45,8 +89,17 @@ public class CupboardControllerTest {
}
@Test
- public void getNeedsEmpty() {
- when(mockCupboardDAO.getNeeds()).thenReturn(new Need[]{});
+ public void getNeedsIOException() throws IOException {
+ when(mockCupboardService.getNeeds()).thenThrow(new IOException());
+
+ var res = cupboardController.getNeeds();
+
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());
+ }
+
+ @Test
+ public void getNeedsEmpty() throws IOException {
+ when(mockCupboardService.getNeeds()).thenReturn(new Need[]{});
var res = cupboardController.getNeeds();
@@ -55,9 +108,9 @@ public class CupboardControllerTest {
}
@Test
- public void searchNeeds() {
+ public void searchNeeds() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.findNeeds("Na")).thenReturn(new Need[]{need});
+ when(mockCupboardService.searchNeeds("Na")).thenReturn(new Need[]{need});
var res = cupboardController.searchNeeds("Na");
@@ -66,8 +119,17 @@ public class CupboardControllerTest {
}
@Test
- public void searchNeedsEmpty() {
- when(mockCupboardDAO.findNeeds("Na")).thenReturn(new Need[]{});
+ public void searchNeedsIOException() throws IOException {
+ when(mockCupboardService.searchNeeds("Na")).thenThrow(new IOException());
+
+ var res = cupboardController.searchNeeds("Na");
+
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());
+ }
+
+ @Test
+ public void searchNeedsEmpty() throws IOException {
+ when(mockCupboardService.searchNeeds("Na")).thenReturn(new Need[]{});
var res = cupboardController.searchNeeds("Na");
@@ -76,9 +138,9 @@ public class CupboardControllerTest {
}
@Test
- public void getNeed() {
+ public void getNeed() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.getNeed(need.getId())).thenReturn(need);
+ when(mockCupboardService.getNeed(need.getId())).thenReturn(need);
var res = cupboardController.getNeed(need.getId());
@@ -87,9 +149,19 @@ public class CupboardControllerTest {
}
@Test
- public void getNeedFail() {
+ public void getNeedIOException() throws IOException {
+ var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
+ when(mockCupboardService.getNeed(need.getId())).thenThrow(new IOException());
+
+ var res = cupboardController.getNeed(need.getId());
+
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());
+ }
+
+ @Test
+ public void getNeedFail() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.getNeed(need.getId())).thenReturn(null);
+ when(mockCupboardService.getNeed(need.getId())).thenReturn(null);
var res = cupboardController.getNeed(need.getId());
@@ -100,19 +172,29 @@ public class CupboardControllerTest {
@Test
public void updateNeeds() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.updateNeed(need)).thenReturn(need);
+ when(mockCupboardService.updateNeed(need, 1)).thenReturn(need);
- var res = cupboardController.updateNeed(need);
+ var res = cupboardController.updateNeed(need, 1);
assertEquals(HttpStatus.OK, res.getStatusCode());
assertEquals(need, res.getBody());
}
@Test
+ public void updateNeedsIOException() throws IOException {
+ var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
+ when(mockCupboardService.updateNeed(need, 1)).thenThrow(new IOException());
+
+ var res = cupboardController.updateNeed(need, 1);
+
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());
+ }
+
+ @Test
public void deleteNeed() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.getNeed(1)).thenReturn(need);
- when(mockCupboardDAO.deleteNeed(1)).thenReturn(true);
+ when(mockCupboardService.getNeed(1)).thenReturn(need);
+ when(mockCupboardService.deleteNeed(1)).thenReturn(true);
var res = cupboardController.deleteNeed(1);
@@ -121,11 +203,22 @@ public class CupboardControllerTest {
@Test
public void deleteNeedFail() throws IOException {
- when(mockCupboardDAO.getNeed(1)).thenReturn(null);
- when(mockCupboardDAO.deleteNeed(1)).thenReturn(false);
+ when(mockCupboardService.getNeed(1)).thenReturn(null);
+ when(mockCupboardService.deleteNeed(1)).thenReturn(false);
var res = cupboardController.deleteNeed(1);
assertEquals(HttpStatus.NOT_FOUND, res.getStatusCode());
}
+
+ @Test
+ public void deleteNeedIOException() throws IOException {
+ var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
+ when(mockCupboardService.getNeed(1)).thenReturn(need);
+ when(mockCupboardService.deleteNeed(1)).thenThrow(new IOException());
+
+ var res = cupboardController.deleteNeed(1);
+
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());
+ }
}