aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java
diff options
context:
space:
mode:
authorGunther6070 <haydenhartman10@yahoo.com>2025-03-17 17:10:38 -0400
committerGunther6070 <haydenhartman10@yahoo.com>2025-03-17 17:10:38 -0400
commitc4316368ccd2e724de838fa6648ff390d4f417a6 (patch)
treeda20406da93c33f1fab350802cbdf5be5131560e /ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java
parentb998a4e7ca580f19ab10862de61d7458031027f2 (diff)
parented3d0e5e5f9c58c3926ea55a422212f4da1c8849 (diff)
downloadJellySolutions-c4316368ccd2e724de838fa6648ff390d4f417a6.tar.gz
JellySolutions-c4316368ccd2e724de838fa6648ff390d4f417a6.tar.bz2
JellySolutions-c4316368ccd2e724de838fa6648ff390d4f417a6.zip
Merge branch 'main' of https://github.com/RIT-SWEN-261-02/team-project-2245-swen-261-02-2b
Diffstat (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java')
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java118
1 files changed, 87 insertions, 31 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 e2c959a..b6367ad 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,15 +1,15 @@
package com.ufund.api.ufundapi.controller;
import java.io.IOException;
+import java.security.InvalidParameterException;
import java.util.Map;
import static java.util.Map.entry;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
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;
@@ -25,21 +25,27 @@ 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
public void testGetUser() throws IOException { // getUser may throw IOException
// Setup
String username = "Test";
- User user = new User(username);
- String key = UserAuth.generate(username).getKey();
+ User user = User.create(username, "pass");
+ String key = UserAuth.generate(username).getKey( );
// When the same id is passed in, our mock User DAO will return the User object
when(mockUserService.getUser(username)).thenReturn(user);
@@ -58,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);
@@ -71,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
@@ -85,25 +107,17 @@ public class UserControllerTest {
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
}
- /*****************************************************************
- * The following tests will fail until all userController methods
- * are implemented.
- ****************************************************************/
-
@Test
public void testCreateUser() throws IOException, DuplicateKeyException { // createUser may throw IOException
// Setup
String username = "Test";
String password = "Pass";
- User user = new User(username);
+ User user = User.create(username, "pass");
// when createUser is called, return true simulating successful
// 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);
@@ -122,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);
@@ -140,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);
@@ -159,7 +183,7 @@ public class UserControllerTest {
public void testUpdateUser() throws IOException { // updateUser may throw IOException
// Setup
String username = "Test";
- User user = new User("Bob");
+ User user = User.create(username, "pass");
String key = UserAuth.generate(username).getKey();
// when updateUser is called, return true simulating successful
// update and save
@@ -177,7 +201,7 @@ public class UserControllerTest {
public void testUpdateUserFailed() throws IOException { // updateUser may throw IOException
// Setup
String username = "Test";
- User user = new User("Bob");
+ User user = User.create(username, "pass");
String key = UserAuth.generate(username).getKey();
// when updateUser is called, return true simulating successful
// update and save
@@ -191,10 +215,10 @@ public class UserControllerTest {
}
@Test
- public void testUpdateUserHandleException() throws IOException { // updateUser may throw IOException
+ public void testUpdateUserInvalidParameter() throws IOException { // updateUser may throw IOException
// Setup
String username = "Test";
- User user = new User("Bob");
+ User user = User.create(username, "pass");
String key = UserAuth.generate(username).getKey();
// When updateUser is called on the Mock User DAO, throw an IOException
doThrow(new IOException()).when(mockUserService).updateUser(user, username);
@@ -207,6 +231,23 @@ public class UserControllerTest {
}
@Test
+ public void testUpdateUserUnauthorized() throws IOException, IllegalAccessException { // updateUser may throw IOException
+ // Setup
+ String username = "Test";
+ User user = User.create(username, "pass");
+ 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";
@@ -241,7 +282,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
@@ -251,4 +292,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());
+ }
+
}