package com.ufund.api.ufundapi.service; import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.ufund.api.ufundapi.DuplicateKeyException; import com.ufund.api.ufundapi.model.User; import com.ufund.api.ufundapi.persistence.UserDAO; public class UserServiceTest { private UserService userService; private UserDAO mockUserDAO; @BeforeEach public void setupUserService() { mockUserDAO = mock(UserDAO.class); CupboardService mockCupboardService = mock(CupboardService.class); userService = new UserService(mockUserDAO, mockCupboardService); } @Test public void testCreateUser() throws IOException, DuplicateKeyException { // Setup String username = "Jelly"; String password = "Fish"; User user = User.create(username, password); // Mock when(mockUserDAO.getUser(username)).thenReturn(null); when(mockUserDAO.addUser(any())).thenReturn(user); // Invoke // Analyze assertEquals(user, userService.createUser(username, password)); } @Test public void testCreateUserDuplicate() throws IOException { // Setup String username = "Jelly"; String password = "Fish"; User user = User.create(username, password); // Mock when(mockUserDAO.getUser(username)).thenReturn(User.create("Phil", "Phil")); when(mockUserDAO.addUser(any())).thenReturn(user); // Analyze assertThrows(DuplicateKeyException.class, () -> userService.createUser(username, password)); } @Test public void testGetUser() throws IOException { // Setup String username = "Jelly"; String password = "Fish"; User user = User.create(username, password); // Mock when(mockUserDAO.getUser(username)).thenReturn(user); // Analyze assertEquals(user, userService.getUser(username)); } @Test public void testUpdateUser() throws IOException { // Setup String username = "Jelly"; String password = "Fish"; User oldUser = User.create(username, password); String newUsername = "Jelly"; String newPassword = "Dog"; User newUser = User.create(newUsername, newPassword); // Mock when(mockUserDAO.updateUser(newUser)).thenReturn(newUser); // Analyze assertEquals(newUser, userService.updateUser(newUser, oldUser.getUsername())); } @Test public void testUpdateUserDifferentUsernames() throws IOException { // Setup String username = "Jelly"; String password = "Fish"; User oldUser = User.create(username, password); String newUsername = "Cat"; String newPassword = "Fish"; User newUser = User.create(newUsername, newPassword); // Mock when(mockUserDAO.updateUser(newUser)).thenReturn(newUser); // Analyze assertThrows(IllegalArgumentException.class, () -> userService.updateUser(newUser, oldUser.getUsername())); } @Test public void testDeleteUser() throws IOException { // Setup String username = "Jelly"; String password = "Fish"; User user = User.create(username, password); // Mock when(mockUserDAO.deleteUser(username)).thenReturn(true); // Analyze assertTrue(userService.deleteUser(username)); } }