diff options
-rw-r--r-- | ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java | 95 |
1 files changed, 51 insertions, 44 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 c7a5584..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,21 +1,21 @@ package com.ufund.api.ufundapi.controller; -import com.ufund.api.ufundapi.model.Need; -import com.ufund.api.ufundapi.model.Need.GoalType; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.http.HttpStatus; - import java.io.IOException; -import java.util.HashMap; 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 { @@ -29,7 +29,7 @@ public class CupboardControllerTest { } @Test - public void createNeed() throws IOException, CupboardService.DuplicateKeyException { + public void createNeed() throws IOException, DuplicateKeyException { String name = "Test"; int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; @@ -37,9 +37,10 @@ public class CupboardControllerTest { when(mockCupboardService.createNeed(name, maxGoal, type)).thenReturn(need); - Map<String, Object> needMap = Map.ofEntries( - entry("id", need.getId()), - entry("need", need) + Map<String, String> needMap = Map.ofEntries( + entry("name", "Test"), + entry("maxGoal", "100"), + entry("goalType", "MONETARY") ); var res = cupboardController.createNeed(needMap); @@ -49,27 +50,35 @@ public class CupboardControllerTest { } @Test - public void createNeedBadMaxGoal() throws IOException { - var need = new Need("Name", 1, -100, Need.GoalType.MONETARY); - when(mockCupboardDAO.createNeed(need)).thenReturn(need); + public void createNeedBadMaxGoal() throws IOException, DuplicateKeyException { + when(mockCupboardService.createNeed("Name", -100, Need.GoalType.MONETARY)).thenThrow(new IllegalArgumentException()); - var res = cupboardController.createNeed(need); + Map<String, String> needMap = Map.ofEntries( + entry("name", "Name"), + entry("maxGoal", "-100"), + entry("goalType", "MONETARY")); - assertEquals(HttpStatus.BAD_REQUEST, res.getStatusCode()); + var res = cupboardController.createNeed(needMap); + + assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, res.getStatusCode()); } @Test - public void createNeedIOException() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); - when(mockCupboardDAO.createNeed(need)).thenThrow(new IOException()); + public void createNeedIOException() throws IOException, DuplicateKeyException { + when(mockCupboardService.createNeed("Name", 100, Need.GoalType.MONETARY)).thenThrow(new IOException()); - var res = cupboardController.createNeed(need); + 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() { + public void getNeeds() throws IOException { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); when(mockCupboardService.getNeeds()).thenReturn(new Need[]{need}); @@ -80,9 +89,8 @@ public class CupboardControllerTest { } @Test - public void getNeedsIOException() { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); - when(mockCupboardDAO.getNeeds()).thenThrow(new IOException()); + public void getNeedsIOException() throws IOException { + when(mockCupboardService.getNeeds()).thenThrow(new IOException()); var res = cupboardController.getNeeds(); @@ -90,7 +98,7 @@ public class CupboardControllerTest { } @Test - public void getNeedsEmpty() { + public void getNeedsEmpty() throws IOException { when(mockCupboardService.getNeeds()).thenReturn(new Need[]{}); var res = cupboardController.getNeeds(); @@ -100,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(mockCupboardService.findNeeds("Na")).thenReturn(new Need[]{need}); + when(mockCupboardService.searchNeeds("Na")).thenReturn(new Need[]{need}); var res = cupboardController.searchNeeds("Na"); @@ -111,9 +119,8 @@ public class CupboardControllerTest { } @Test - public void searchNeedsIOException() { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); - when(mockCupboardDAO.findNeeds("Na")).thenThrow(new IOException()); + public void searchNeedsIOException() throws IOException { + when(mockCupboardService.searchNeeds("Na")).thenThrow(new IOException()); var res = cupboardController.searchNeeds("Na"); @@ -121,8 +128,8 @@ public class CupboardControllerTest { } @Test - public void searchNeedsEmpty() { - when(mockCupboardService.findNeeds("Na")).thenReturn(new Need[]{}); + public void searchNeedsEmpty() throws IOException { + when(mockCupboardService.searchNeeds("Na")).thenReturn(new Need[]{}); var res = cupboardController.searchNeeds("Na"); @@ -131,7 +138,7 @@ public class CupboardControllerTest { } @Test - public void getNeed() { + public void getNeed() throws IOException { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); when(mockCupboardService.getNeed(need.getId())).thenReturn(need); @@ -142,9 +149,9 @@ public class CupboardControllerTest { } @Test - public void getNeedIOException() { + public void getNeedIOException() throws IOException { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); - when(mockCupboardDAO.getNeed(need.getId())).thenThrow(new IOException()); + when(mockCupboardService.getNeed(need.getId())).thenThrow(new IOException()); var res = cupboardController.getNeed(need.getId()); @@ -152,7 +159,7 @@ public class CupboardControllerTest { } @Test - public void getNeedFail() { + public void getNeedFail() throws IOException { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); when(mockCupboardService.getNeed(need.getId())).thenReturn(null); @@ -165,9 +172,9 @@ public class CupboardControllerTest { @Test public void updateNeeds() throws IOException { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); - when(mockCupboardService.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()); @@ -176,9 +183,9 @@ public class CupboardControllerTest { @Test public void updateNeedsIOException() throws IOException { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); - when(mockCupboardDAO.updateNeed(need)).thenThrow(new IOException()); + when(mockCupboardService.updateNeed(need, 1)).thenThrow(new IOException()); - var res = cupboardController.updateNeed(need); + var res = cupboardController.updateNeed(need, 1); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); } @@ -207,8 +214,8 @@ public class CupboardControllerTest { @Test public void deleteNeedIOException() throws IOException { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); - when(mockCupboardDAO.getNeed(1)).thenReturn(need); - when(mockCupboardDAO.deleteNeed(1)).thenThrow(new IOException()); + when(mockCupboardService.getNeed(1)).thenReturn(need); + when(mockCupboardService.deleteNeed(1)).thenThrow(new IOException()); var res = cupboardController.deleteNeed(1); |