diff options
| -rw-r--r-- | ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java | 93 | 
1 files changed, 57 insertions, 36 deletions
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 d189836..11fa6a4 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,7 +1,8 @@  package com.ufund.api.ufundapi.controller;  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.assertEquals;  import org.junit.jupiter.api.BeforeEach; @@ -13,22 +14,23 @@ 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.model.UserAuth; -import com.ufund.api.ufundapi.persistence.UserAuthFIleDAO; -import com.ufund.api.ufundapi.persistence.UserFileDAO; +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 UserAuthFIleDAO mockAuthUserDAO; +    private UserService mockUserService; +    private AuthService mockAuthService;      @BeforeEach      public void setupUserController() { -        mockUserDAO = mock(UserFileDAO.class); -        mockAuthUserDAO = mock(UserAuthFIleDAO.class); -        userController = new UserController(mockUserDAO, mockAuthUserDAO); +        mockUserService = mock(UserService.class); +        mockAuthService = mock(AuthService.class); +        userController = new UserController(mockUserService, mockAuthService);      }      @Test @@ -38,7 +40,7 @@ public class UserControllerTest {          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 @@ -56,7 +58,7 @@ public class UserControllerTest {          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 @@ -72,7 +74,7 @@ public class UserControllerTest {          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, key); @@ -87,19 +89,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); -        String key = UserAuth.generate(username).getKey();          // when createUser is called, return true simulating successful          // creation and save -        when(mockUserDAO.addUser(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(params); +        ResponseEntity<User> response = userController.createUser(userMap);          // Analyze          assertEquals(HttpStatus.CREATED, response.getStatusCode()); @@ -107,32 +112,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.addUser(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).addUser(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()); @@ -143,12 +158,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()); @@ -160,12 +176,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()); @@ -176,11 +193,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()); @@ -190,11 +208,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()); @@ -204,11 +223,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()); @@ -218,11 +238,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());  | 
