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 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 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 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 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 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 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 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 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 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 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 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 response = userController.deleteUser(username); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); } }