diff options
author | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-17 16:11:26 -0400 |
---|---|---|
committer | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-17 16:11:26 -0400 |
commit | 60dd2db634de6146671be9546fb3e4bdf6d9b7d9 (patch) | |
tree | 2f03768b5053ab796dec8421ee1d3a6d9de7ac46 | |
parent | 4d9fe6c96f487d75a03e3a680cc80fa3f2ad5e4f (diff) | |
download | JellySolutions-60dd2db634de6146671be9546fb3e4bdf6d9b7d9.tar.gz JellySolutions-60dd2db634de6146671be9546fb3e4bdf6d9b7d9.tar.bz2 JellySolutions-60dd2db634de6146671be9546fb3e4bdf6d9b7d9.zip |
Added additional tests to further coverage
-rw-r--r-- | ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java | 116 |
1 files changed, 97 insertions, 19 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 efe639e..a25ec8a 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,6 +1,7 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; +import java.security.InvalidParameterException; import java.util.Map; import static java.util.Map.entry; @@ -24,13 +25,19 @@ import com.ufund.api.ufundapi.service.UserService; @Tag("Controller-tier") public class UserControllerTest { private UserController userController; + private AuthService mockAuthService; private UserService mockUserService; + private Map<String, String> userMap; @BeforeEach public void setupUserController() { mockUserService = mock(UserService.class); - AuthService mockAuthService = mock(AuthService.class); + mockAuthService = mock(AuthService.class); userController = new UserController(mockUserService, mockAuthService); + userMap = Map.ofEntries( + entry("username", "Test"), + entry("password", "Pass") + ); } @Test @@ -39,7 +46,7 @@ public class UserControllerTest { 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 the same id is passed in, our mock User service will return the User object when(mockUserService.getUser(username)).thenReturn(user); @@ -57,7 +64,7 @@ public class UserControllerTest { // 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 + // When the same id is passed in, our mock User service will return null, simulating // no User found when(mockUserService.getUser(username)).thenReturn(null); @@ -70,11 +77,27 @@ public class UserControllerTest { } @Test + public void testGetUserUnauthorized() throws Exception { // createUser may throw IOException + // Setup + String username = "Test"; + String key = UserAuth.generate(username).getKey(); + // When getUser is called on the Mock User service, throw an IOException + // doThrow(new IllegalAccessException()).when(mockUserService).getUser(username); + doThrow(new IllegalAccessException()).when(mockAuthService).authenticate(username, key); + + // Invoke + ResponseEntity<User> response = userController.getUser(username, key); + + // Analyze + assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); + } + + @Test 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 + // When getUser is called on the Mock User service, throw an IOException doThrow(new IOException()).when(mockUserService).getUser(username); // Invoke @@ -94,10 +117,7 @@ public class UserControllerTest { // creation and save 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(userMap); @@ -116,10 +136,23 @@ public class UserControllerTest { // creation and save 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(userMap); + + // Analyze + assertEquals(HttpStatus.CONFLICT, response.getStatusCode()); + } + + @Test + public void testCreateUserDuplicate() throws IOException, DuplicateKeyException { // createUser may throw IOException + // Setup + String username = "Test"; + String password = "Pass"; + // when createUser is called, return false simulating failed + // creation and save + when(mockUserService.createUser(username, password)).thenThrow(DuplicateKeyException.class); // Invoke ResponseEntity<User> response = userController.createUser(userMap); @@ -134,13 +167,10 @@ public class UserControllerTest { String username = "Test"; String password = "Pass"; - // When createUser is called on the Mock User DAO, throw an IOException + // When createUser is called on the Mock User service, throw an IOException 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(userMap); @@ -185,12 +215,28 @@ public class UserControllerTest { } @Test + public void testUpdateUserInvalidParameter() throws IOException { // updateUser may throw IOException + // Setup + String username = "Test"; + User user = new User("Bob"); + String key = UserAuth.generate(username).getKey(); + // When updateUser is called on the Mock User service, throw a Invalid Parameter exception + doThrow(new InvalidParameterException()).when(mockUserService).updateUser(user, username); + + // Invoke + ResponseEntity<User> response = userController.updateUser(user, username, key); + + // Analyze + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + } + + @Test public void testUpdateUserHandleException() throws IOException { // updateUser may throw IOException // 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 + // When updateUser is called on the Mock User service, throw an IOException doThrow(new IOException()).when(mockUserService).updateUser(user, username); // Invoke @@ -201,6 +247,23 @@ public class UserControllerTest { } @Test + public void testUpdateUserUnauthorized() throws IOException, IllegalAccessException { // updateUser may throw IOException + // Setup + String username = "Test"; + User user = new User("Bob"); + String key = UserAuth.generate(username).getKey(); + // When updateUser is called on the Mock User service, throw a Invalid Parameter exception + // exception + doThrow(new IllegalAccessException()).when(mockAuthService).authenticate(username, key); + + // Invoke + ResponseEntity<User> response = userController.updateUser(user, username, key); + + // Analyze + assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); + } + + @Test public void testDeleteUser() throws IOException { // deleteUser may throw IOException // Setup String username = "Test"; @@ -235,7 +298,7 @@ public class UserControllerTest { // Setup String username = "Test"; String key = UserAuth.generate(username).getKey(); - // When deleteUser is called on the Mock User DAO, throw an IOException + // When deleteUser is called on the Mock User service, throw an IOException doThrow(new IOException()).when(mockUserService).deleteUser(username); // Invoke @@ -245,4 +308,19 @@ public class UserControllerTest { assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); } + @Test + public void testDeleteUserUnauthorized() throws IOException, IllegalAccessException { // deleteUser may throw IOException + // Setup + String username = "Test"; + String key = UserAuth.generate(username).getKey(); + // When deleteUser is called on the Mock User service, throw an IOException + doThrow(new IllegalAccessException()).when(mockAuthService).authenticate(username, key); + + // Invoke + ResponseEntity<Boolean> response = userController.deleteUser(username, key); + + // Analyze + assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); + } + } |