diff options
Diffstat (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/controller')
-rw-r--r-- | ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java | 114 | ||||
-rw-r--r-- | ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java | 105 |
2 files changed, 137 insertions, 82 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 839c518..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,6 +1,8 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; +import java.util.Map; +import static java.util.Map.entry; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -11,54 +13,74 @@ 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.persistence.CupboardFileDao; +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 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()); + + Map<String, String> needMap = Map.ofEntries( + entry("name", "Name"), + entry("maxGoal", "-100"), + entry("goalType", "MONETARY")); - var res = cupboardController.createNeed(need); + var res = cupboardController.createNeed(needMap); - assertEquals(HttpStatus.BAD_REQUEST, res.getStatusCode()); + 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()); + + Map<String, String> needMap = Map.ofEntries( + entry("name", "Name"), + entry("maxGoal", "100"), + entry("goalType", "MONETARY")); - var res = cupboardController.createNeed(need); + 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(mockCupboardDAO.getNeeds()).thenReturn(new Need[]{need}); + when(mockCupboardService.getNeeds()).thenReturn(new Need[]{need}); var res = cupboardController.getNeeds(); @@ -67,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(); @@ -77,8 +98,8 @@ public class CupboardControllerTest { } @Test - public void getNeedsEmpty() { - when(mockCupboardDAO.getNeeds()).thenReturn(new Need[]{}); + public void getNeedsEmpty() throws IOException { + when(mockCupboardService.getNeeds()).thenReturn(new Need[]{}); var res = cupboardController.getNeeds(); @@ -87,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"); @@ -98,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"); @@ -108,8 +128,8 @@ public class CupboardControllerTest { } @Test - public void searchNeedsEmpty() { - when(mockCupboardDAO.findNeeds("Na")).thenReturn(new Need[]{}); + public void searchNeedsEmpty() throws IOException { + when(mockCupboardService.searchNeeds("Na")).thenReturn(new Need[]{}); var res = cupboardController.searchNeeds("Na"); @@ -118,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()); @@ -129,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()); @@ -139,9 +159,9 @@ public class CupboardControllerTest { } @Test - public void getNeedFail() { + 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()); @@ -152,9 +172,9 @@ 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()); @@ -163,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()); } @@ -173,8 +193,8 @@ public class CupboardControllerTest { @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); @@ -183,8 +203,8 @@ 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); @@ -194,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); diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java index 681f47c..e2c959a 100644 --- a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java +++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java @@ -1,30 +1,37 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; +import java.util.Map; +import static java.util.Map.entry; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import com.ufund.api.ufundapi.DuplicateKeyException; import com.ufund.api.ufundapi.model.User; -import com.ufund.api.ufundapi.persistence.UserFileDAO; +import com.ufund.api.ufundapi.model.UserAuth; +import com.ufund.api.ufundapi.service.AuthService; +import com.ufund.api.ufundapi.service.UserService; @Tag("Controller-tier") public class UserControllerTest { private UserController userController; - private UserFileDAO mockUserDAO; + private UserService mockUserService; @BeforeEach public void setupUserController() { - mockUserDAO = mock(UserFileDAO.class); - userController = new UserController(mockUserDAO); - + mockUserService = mock(UserService.class); + AuthService mockAuthService = mock(AuthService.class); + userController = new UserController(mockUserService, mockAuthService); } @Test @@ -32,27 +39,32 @@ public class UserControllerTest { // Setup String username = "Test"; User user = new User(username); + String key = UserAuth.generate(username).getKey(); // When the same id is passed in, our mock User DAO will return the User object - when(mockUserDAO.getUser(username)).thenReturn(user); + when(mockUserService.getUser(username)).thenReturn(user); + // Invoke - ResponseEntity<User> response = userController.getUser(username); + ResponseEntity<User> response = userController.getUser(username, key); // Analyze assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(user, response.getBody()); + assertNotNull(response.getBody()); + assertEquals(user.getUsername(), response.getBody().getUsername()); } @Test public void testGetUserNotFound() throws Exception { // createUser may throw IOException // Setup String username = "Test"; + String key = UserAuth.generate(username).getKey(); // When the same id is passed in, our mock User DAO will return null, simulating // no User found - when(mockUserDAO.getUser(username)).thenReturn(null); + when(mockUserService.getUser(username)).thenReturn(null); + // Invoke - ResponseEntity<User> response = userController.getUser(username); + ResponseEntity<User> response = userController.getUser(username, key); // Analyze assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); @@ -62,11 +74,12 @@ public class UserControllerTest { public void testGetUserHandleException() throws Exception { // createUser may throw IOException // Setup String username = "Test"; + String key = UserAuth.generate(username).getKey(); // When getUser is called on the Mock User DAO, throw an IOException - doThrow(new IOException()).when(mockUserDAO).getUser(username); + doThrow(new IOException()).when(mockUserService).getUser(username); // Invoke - ResponseEntity<User> response = userController.getUser(username); + ResponseEntity<User> response = userController.getUser(username, key); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); @@ -78,16 +91,22 @@ public class UserControllerTest { ****************************************************************/ @Test - public void testCreateUser() throws IOException { // createUser may throw IOException + public void testCreateUser() throws IOException, DuplicateKeyException { // createUser may throw IOException // Setup String username = "Test"; + String password = "Pass"; User user = new User(username); // when createUser is called, return true simulating successful // creation and save - when(mockUserDAO.createUser(user)).thenReturn(user); + when(mockUserService.createUser(username, password)).thenReturn(user); + + Map<String, String> userMap = Map.ofEntries( + entry("username", "Test"), + entry("password", "Pass") + ); // Invoke - ResponseEntity<User> response = userController.createUser(user); + ResponseEntity<User> response = userController.createUser(userMap); // Analyze assertEquals(HttpStatus.CREATED, response.getStatusCode()); @@ -95,32 +114,42 @@ public class UserControllerTest { } @Test - public void testCreateUserFailed() throws IOException { // createUser may throw IOException + public void testCreateUserFailed() throws IOException, DuplicateKeyException { // createUser may throw IOException // Setup String username = "Test"; - User user = new User(username); + String password = "Pass"; // when createUser is called, return false simulating failed // creation and save - when(mockUserDAO.createUser(user)).thenReturn(null); + when(mockUserService.createUser(username, password)).thenReturn(null); + + Map<String, String> userMap = Map.ofEntries( + entry("username", "Test"), + entry("password", "Pass") + ); // Invoke - ResponseEntity<User> response = userController.createUser(user); + ResponseEntity<User> response = userController.createUser(userMap); // Analyze assertEquals(HttpStatus.CONFLICT, response.getStatusCode()); } @Test - public void testCreateUserHandleException() throws IOException { // createUser may throw IOException + public void testCreateUserHandleException() throws IOException, DuplicateKeyException { // createUser may throw IOException // Setup String username = "Test"; - User user = new User(username); + String password = "Pass"; // When createUser is called on the Mock User DAO, throw an IOException - doThrow(new IOException()).when(mockUserDAO).createUser(user); + doThrow(new IOException()).when(mockUserService).createUser(username, password); + + Map<String, String> userMap = Map.ofEntries( + entry("username", "Test"), + entry("password", "Pass") + ); // Invoke - ResponseEntity<User> response = userController.createUser(user); + ResponseEntity<User> response = userController.createUser(userMap); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); @@ -131,12 +160,13 @@ public class UserControllerTest { // Setup String username = "Test"; User user = new User("Bob"); + String key = UserAuth.generate(username).getKey(); // when updateUser is called, return true simulating successful // update and save - when(mockUserDAO.updateUser(user, username)).thenReturn(user); + when(mockUserService.updateUser(user, username)).thenReturn(user); // Invoke - ResponseEntity<User> response = userController.updateUser(user, username); + ResponseEntity<User> response = userController.updateUser(user, username, key); // Analyze assertEquals(HttpStatus.OK, response.getStatusCode()); @@ -148,12 +178,13 @@ public class UserControllerTest { // Setup String username = "Test"; User user = new User("Bob"); + String key = UserAuth.generate(username).getKey(); // when updateUser is called, return true simulating successful // update and save - when(mockUserDAO.updateUser(user, username)).thenReturn(null); + when(mockUserService.updateUser(user, username)).thenReturn(null); // Invoke - ResponseEntity<User> response = userController.updateUser(user, username); + ResponseEntity<User> response = userController.updateUser(user, username, key); // Analyze assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); @@ -164,11 +195,12 @@ public class UserControllerTest { // Setup String username = "Test"; User user = new User("Bob"); + String key = UserAuth.generate(username).getKey(); // When updateUser is called on the Mock User DAO, throw an IOException - doThrow(new IOException()).when(mockUserDAO).updateUser(user, username); + doThrow(new IOException()).when(mockUserService).updateUser(user, username); // Invoke - ResponseEntity<User> response = userController.updateUser(user, username); + ResponseEntity<User> response = userController.updateUser(user, username, key); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); @@ -178,11 +210,12 @@ public class UserControllerTest { public void testDeleteUser() throws IOException { // deleteUser may throw IOException // Setup String username = "Test"; + String key = UserAuth.generate(username).getKey(); // when deleteUser is called return true, simulating successful deletion - when(mockUserDAO.deleteUser(username)).thenReturn(true); + when(mockUserService.deleteUser(username)).thenReturn(true); // Invoke - ResponseEntity<User> response = userController.deleteUser(username); + ResponseEntity<Boolean> response = userController.deleteUser(username, key); // Analyze assertEquals(HttpStatus.OK, response.getStatusCode()); @@ -192,11 +225,12 @@ public class UserControllerTest { public void testDeleteUserNotFound() throws IOException { // deleteUser may throw IOException // Setup String username = "Test"; + String key = UserAuth.generate(username).getKey(); // when deleteUser is called return false, simulating failed deletion - when(mockUserDAO.deleteUser(username)).thenReturn(false); + when(mockUserService.deleteUser(username)).thenReturn(false); // Invoke - ResponseEntity<User> response = userController.deleteUser(username); + ResponseEntity<Boolean> response = userController.deleteUser(username, key); // Analyze assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); @@ -206,11 +240,12 @@ public class UserControllerTest { public void testDeleteUserHandleException() throws IOException { // deleteUser may throw IOException // Setup String username = "Test"; + String key = UserAuth.generate(username).getKey(); // When deleteUser is called on the Mock User DAO, throw an IOException - doThrow(new IOException()).when(mockUserDAO).deleteUser(username); + doThrow(new IOException()).when(mockUserService).deleteUser(username); // Invoke - ResponseEntity<User> response = userController.deleteUser(username); + ResponseEntity<Boolean> response = userController.deleteUser(username, key); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); |