aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/test/java/com/ufund
diff options
context:
space:
mode:
authorGunther6070 <haydenhartman10@yahoo.com>2025-02-27 12:30:56 -0500
committerGunther6070 <haydenhartman10@yahoo.com>2025-02-27 12:30:56 -0500
commit3b51b8585cc1bf84da5cfe8c9b6791becfab2064 (patch)
tree5203de042733100b04c0503fa382ed7ade470943 /ufund-api/src/test/java/com/ufund
parent4a4ed73fa6e3da1ec152786731044e7a8c8b1163 (diff)
downloadJellySolutions-3b51b8585cc1bf84da5cfe8c9b6791becfab2064.tar.gz
JellySolutions-3b51b8585cc1bf84da5cfe8c9b6791becfab2064.tar.bz2
JellySolutions-3b51b8585cc1bf84da5cfe8c9b6791becfab2064.zip
Created UserController tests
Diffstat (limited to 'ufund-api/src/test/java/com/ufund')
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java218
1 files changed, 218 insertions, 0 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
new file mode 100644
index 0000000..1bd2e8e
--- /dev/null
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java
@@ -0,0 +1,218 @@
+package com.ufund.api.ufundapi.controller;
+
+
+import java.io.IOException;
+
+import org.junit.jupiter.api.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.*;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+
+import com.ufund.api.ufundapi.model.User;
+import com.ufund.api.ufundapi.persistence.UserFileDAO;
+
+@Tag("Controller-tier")
+public class UserControllerTest {
+ private UserController userController;
+ private UserFileDAO mockUserDAO;
+
+ @BeforeEach
+ public void setupUserController() {
+ mockUserDAO = mock(UserFileDAO.class);
+ userController = new UserController(mockUserDAO);
+
+
+ }
+
+ @Test
+ public void testGetUser() throws IOException { // getHero may throw IOException
+ // Setup
+ String username = "Test";
+ User user = new User(username);
+ // When the same id is passed in, our mock Hero DAO will return the Hero object
+ when(mockUserDAO.getUser(username)).thenReturn(user);
+
+ // Invoke
+ ResponseEntity<User> response = userController.getUser(username);
+
+ // Analyze
+ assertEquals(HttpStatus.OK, response.getStatusCode());
+ assertEquals(user, response.getBody());
+ }
+
+ @Test
+ public void testGetUserNotFound() throws Exception { // createHero may throw IOException
+ // Setup
+ String username = "Test";
+ // When the same id is passed in, our mock Hero DAO will return null, simulating
+ // no hero found
+ when(mockUserDAO.getUser(username)).thenReturn(null);
+
+ // Invoke
+ ResponseEntity<User> response = userController.getUser(username);
+
+ // Analyze
+ assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
+ }
+
+ @Test
+ public void testGetHeroHandleException() throws Exception { // createHero may throw IOException
+ // Setup
+ String username = "Test";
+ // When getHero is called on the Mock Hero DAO, throw an IOException
+ doThrow(new IOException()).when(mockUserDAO).getUser(username);
+
+ // Invoke
+ ResponseEntity<User> response = userController.getUser(username);
+
+ // Analyze
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
+ }
+
+ /*****************************************************************
+ * The following tests will fail until all userController methods
+ * are implemented.
+ ****************************************************************/
+
+ @Test
+ public void testCreateHero() throws IOException { // createHero may throw IOException
+ // Setup
+ String username = "Test";
+ User user = new User(username);
+ // when createHero is called, return true simulating successful
+ // creation and save
+ when(mockUserDAO.createUser(user)).thenReturn(user);
+
+ // Invoke
+ ResponseEntity<User> response = userController.createUser(user);
+
+ // Analyze
+ assertEquals(HttpStatus.CREATED, response.getStatusCode());
+ assertEquals(user, response.getBody());
+ }
+
+ @Test
+ public void testCreateHeroFailed() throws IOException { // createHero may throw IOException
+ // Setup
+ String username = "Test";
+ User user = new User(username);
+ // when createHero is called, return false simulating failed
+ // creation and save
+ when(mockUserDAO.createUser(user)).thenReturn(null);
+
+ // Invoke
+ ResponseEntity<User> response = userController.createUser(user);
+
+ // Analyze
+ assertEquals(HttpStatus.CONFLICT, response.getStatusCode());
+ }
+
+ @Test
+ public void testCreateHeroHandleException() throws IOException { // createHero may throw IOException
+ // Setup
+ String username = "Test";
+ User user = new User(username);
+
+ // When createHero is called on the Mock Hero DAO, throw an IOException
+ doThrow(new IOException()).when(mockUserDAO).createUser(user);
+
+ // Invoke
+ ResponseEntity<User> response = userController.createUser(user);
+
+ // Analyze
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
+ }
+
+ @Test
+ public void testUpdateHero() throws IOException { // updateHero may throw IOException
+ // Setup
+ String username = "Test";
+ User user = new User("Bob");
+ // when updateHero is called, return true simulating successful
+ // update and save
+ when(mockUserDAO.updateUser(user, username)).thenReturn(user);
+ ResponseEntity<User> response = userController.updateUser(user, username);
+
+ // Invoke
+ response = userController.updateUser(user, username);
+
+ // Analyze
+ assertEquals(HttpStatus.OK, response.getStatusCode());
+ assertEquals(user, response.getBody());
+ }
+
+ @Test
+ public void testUpdateHeroFailed() throws IOException { // updateHero may throw IOException
+ // Setup
+ String username = "Test";
+ User user = new User("Bob");
+ // when updateHero is called, return true simulating successful
+ // update and save
+ when(mockUserDAO.updateUser(user, username)).thenReturn(null);
+
+ // Invoke
+ ResponseEntity<User> response = userController.updateUser(user, username);
+
+ // Analyze
+ assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
+ }
+
+ @Test
+ public void testUpdateHeroHandleException() throws IOException { // updateHero may throw IOException
+ // Setup
+ String username = "Test";
+ User user = new User("Bob");
+ // When updateHero is called on the Mock Hero DAO, throw an IOException
+ doThrow(new IOException()).when(mockUserDAO).updateUser(user, username);
+
+ // Invoke
+ ResponseEntity<User> response = userController.updateUser(user, username);
+
+ // Analyze
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
+ }
+
+ @Test
+ public void testDeleteHero() throws IOException { // deleteHero may throw IOException
+ // Setup
+ String username = "Test";
+ // when deleteHero is called return true, simulating successful deletion
+ when(mockUserDAO.deleteUser(username)).thenReturn(true);
+
+ // Invoke
+ ResponseEntity<User> response = userController.deleteUser(username);
+
+ // Analyze
+ assertEquals(HttpStatus.OK, response.getStatusCode());
+ }
+
+ @Test
+ public void testDeleteHeroNotFound() throws IOException { // deleteHero may throw IOException
+ // Setup
+ String username = "Test";
+ // when deleteHero is called return false, simulating failed deletion
+ when(mockUserDAO.deleteUser(username)).thenReturn(false);
+
+ // Invoke
+ ResponseEntity<User> response = userController.deleteUser(username);
+
+ // Analyze
+ assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
+ }
+
+ @Test
+ public void testDeleteHeroHandleException() throws IOException { // deleteHero may throw IOException
+ // Setup
+ String username = "Test";
+ // When deleteHero is called on the Mock Hero DAO, throw an IOException
+ doThrow(new IOException()).when(mockUserDAO).deleteUser(username);
+
+ // Invoke
+ ResponseEntity<User> response = userController.deleteUser(username);
+
+ // Analyze
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
+ }
+
+}