aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/test/java/com/ufund/api/ufundapi/service
diff options
context:
space:
mode:
Diffstat (limited to 'ufund-api/src/test/java/com/ufund/api/ufundapi/service')
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/service/AuthServiceTest.java110
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java153
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java126
3 files changed, 383 insertions, 6 deletions
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/AuthServiceTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/AuthServiceTest.java
new file mode 100644
index 0000000..7770c40
--- /dev/null
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/AuthServiceTest.java
@@ -0,0 +1,110 @@
+package com.ufund.api.ufundapi.service;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+import com.ufund.api.ufundapi.DuplicateKeyException;
+import com.ufund.api.ufundapi.model.User;
+import com.ufund.api.ufundapi.model.UserAuth;
+import com.ufund.api.ufundapi.persistence.UserAuthDAO;
+
+@Tag("Service-tier")
+public class AuthServiceTest {
+
+ private UserAuthDAO mockAuthDAO;
+ private UserService mockUserService;
+ private AuthService authService;
+ private String username;
+ private String key;
+ private String password;
+ private User user;
+
+ @BeforeEach
+ public void setupAuthService() {
+ mockAuthDAO = mock(UserAuthDAO.class);
+ mockUserService = mock(UserService.class);
+ authService = new AuthService(mockAuthDAO, mockUserService);
+
+ username = "Fish";
+ password = "sticks";
+ key = UserAuth.generate(username).getKey();
+ user = User.create(username, password);
+
+ }
+
+ @Test
+ public void testAuthenticate() throws IOException {
+ // Mock
+ when(mockAuthDAO.getUserAuth(key)).thenReturn(new UserAuth(key, username, null));
+ when(mockUserService.getUser(username)).thenReturn(user);
+
+ // Analyze
+ assertDoesNotThrow(() -> authService.authenticate(username, key));
+
+ }
+
+ @Test
+ public void testAuthenticateMismatchName() throws IOException {
+ // Mock
+ when(mockAuthDAO.getUserAuth(key)).thenReturn(new UserAuth(key, "EvilFish", null));
+ when(mockUserService.getUser("EvilFish")).thenReturn(user);
+
+ // Analyze
+ assertThrows(IllegalAccessException.class, () -> authService.authenticate(username, key));
+
+ }
+
+ @Test
+ public void testAuthenticateMissingUserAuth() throws IOException {
+ // Mock
+ when(mockAuthDAO.getUserAuth(key)).thenReturn(null);
+
+ // Analyze
+ assertThrows(IllegalAccessException.class, () -> authService.authenticate(username, key));
+
+ }
+
+ @Test
+ public void testLogin() throws IOException, DuplicateKeyException, IllegalAccessException {
+ // Mock
+ when(mockUserService.getUser(username)).thenReturn(user);
+
+
+ // Analyze
+ assertDoesNotThrow(() -> authService.login(username, password));
+ }
+
+ @Test
+ public void testLoginNullUser() throws IOException, DuplicateKeyException, IllegalAccessException {
+ // Mock
+ when(mockUserService.getUser(username)).thenReturn(null);
+
+ // Analyze
+ assertThrows(IllegalAccessException.class, () -> authService.login(username, password));
+ }
+
+ @Test
+ public void testLoginMismatchPasswords() throws IOException, DuplicateKeyException, IllegalAccessException {
+ // Mock
+ when(mockUserService.getUser(username)).thenReturn(User.create(username, "fries"));
+
+ // Analyze
+ assertThrows(IllegalAccessException.class, () -> authService.login(username, password));
+ }
+
+ @Test
+ public void testLogout() throws IOException, DuplicateKeyException, IllegalAccessException {
+ // Analyze
+ assertDoesNotThrow(() -> authService.logout(key));
+
+ }
+
+}
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java
index ceef215..99ca23c 100644
--- a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java
@@ -2,8 +2,11 @@ package com.ufund.api.ufundapi.service;
import java.io.IOException;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.BeforeEach;
@@ -19,13 +22,11 @@ import com.ufund.api.ufundapi.persistence.CupboardDAO;
public class CupboardServiceTest {
private CupboardDAO mockCupboardDAO;
- private AuthService mockAuthService;
private CupboardService cupboardService;
@BeforeEach
public void setupCupboardService() {
mockCupboardDAO = mock(CupboardDAO.class);
- mockAuthService = mock(AuthService.class);
cupboardService = new CupboardService(mockCupboardDAO);
}
@@ -35,15 +36,12 @@ public class CupboardServiceTest {
// Setup
String name = "Jellyfish";
double maxGoal = 100.00;
- int id = 0;
GoalType type = GoalType.MONETARY;
Need need = new Need(name, type, maxGoal);
// When the same id is passed in, our mock User DAO will return the User object
- when(mockCupboardDAO.getNeed(id)).thenReturn(need);
when(mockCupboardDAO.addNeed(any())).thenReturn(need);
when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]);
-
// Invoke
Need response = cupboardService.createNeed(name, maxGoal, type);
@@ -53,4 +51,147 @@ public class CupboardServiceTest {
assertEquals(need, response);
}
-}
+ @Test
+ public void testCreateNeedBadGoal() throws IOException, DuplicateKeyException {
+ // Setup
+ String name = "Jellyfish";
+ double maxGoal = -100.00;
+ GoalType type = GoalType.MONETARY;
+ Need need = new Need(name, type, maxGoal);
+
+ // When the same id is passed in, our mock User DAO will return the User object
+ when(mockCupboardDAO.addNeed(any())).thenReturn(need);
+ when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]);
+
+ // Invoke
+ // Need response = cupboardService.createNeed(name, maxGoal, type);
+
+ // Analyze
+ assertThrows(IllegalArgumentException.class, () -> {
+ cupboardService.createNeed(name, maxGoal, type);
+ });
+ }
+
+ @Test
+ public void testCreateNeedDuplicate() throws IOException, DuplicateKeyException {
+ // Setup
+ String name = "Jellyfish";
+ double maxGoal = 100.00;
+ GoalType type = GoalType.MONETARY;
+ Need need = new Need(name, type, maxGoal);
+ Need[] needs = { need };
+
+ // When the same id is passed in, our mock User DAO will return the User object
+ when(mockCupboardDAO.addNeed(any())).thenReturn(need);
+ when(mockCupboardDAO.getNeeds()).thenReturn(needs);
+
+ // Invoke
+ // Need response = cupboardService.createNeed(name, maxGoal, type);
+
+ // Analyze
+ assertThrows(DuplicateKeyException.class, () -> {
+ cupboardService.createNeed(name, maxGoal, type);
+ });
+ }
+
+ @Test
+ public void testSearchNeeds() throws IOException, DuplicateKeyException {
+ // Setup
+ String name = "Jellyfish";
+ double maxGoal = 100.00;
+ GoalType type = GoalType.MONETARY;
+ Need need = new Need(name, type, maxGoal);
+ Need[] needs = { need };
+
+ // When the same id is passed in, our mock User DAO will return the User object
+ when(mockCupboardDAO.getNeeds()).thenReturn(needs);
+
+ // Invoke
+ Need[] response = cupboardService.searchNeeds("Jelly");
+
+ // Analyze
+ assertEquals(need, response[0]);
+ assertEquals(need.getName(), response[0].getName());
+ }
+
+ @Test
+ public void testSearchNeedsFail() throws IOException, DuplicateKeyException {
+ // Setup
+ String name = "Jellyfish";
+ double maxGoal = 100.00;
+ GoalType type = GoalType.MONETARY;
+ Need need = new Need(name, type, maxGoal);
+ Need[] needs = { need };
+
+ // When the same id is passed in, our mock User DAO will return the User object
+ when(mockCupboardDAO.getNeeds()).thenReturn(needs);
+
+ // Invoke
+ Need[] response = cupboardService.searchNeeds("Octopus");
+
+ // Analyze
+ assertArrayEquals(new Need[0], response);
+ }
+
+ @Test
+ public void testGetNeed() throws IOException, DuplicateKeyException {
+ // Setup
+ String name = "Jellyfish";
+ double maxGoal = 100.00;
+ int id = 0;
+ GoalType type = GoalType.MONETARY;
+ Need need = new Need(name, type, maxGoal);
+
+ // When the same id is passed in, our mock User DAO will return the User object
+ when(mockCupboardDAO.getNeed(id)).thenReturn(need);
+
+ // Invoke
+ Need response = cupboardService.getNeed(id);
+
+ // Analyze
+ assertEquals(need, response);
+ }
+
+ @Test
+ public void testUpdateNeed() throws IOException, DuplicateKeyException {
+ // Setup
+ String name = "Jellyfish";
+ double maxGoal = 100.00;
+ int id = 0;
+ GoalType type = GoalType.MONETARY;
+ Need need = new Need(name, type, maxGoal);
+ Need newNeed = new Need("Octopus", type, maxGoal);
+
+ // When the same id is passed in, our mock User DAO will return the User object
+ when(mockCupboardDAO.updateNeed(any())).thenReturn(newNeed);
+
+ // Invoke
+ Need response = cupboardService.updateNeed(newNeed, id);
+
+ // Analyze
+ assertEquals(newNeed, response);
+ }
+
+ @Test
+ public void testDeleteNeed() throws IOException, DuplicateKeyException {
+ // Setup
+ String name = "Jellyfish";
+ double maxGoal = 100.00;
+ int id = 0;
+ GoalType type = GoalType.MONETARY;
+ Need need = new Need(name, type, maxGoal);
+
+ // When the same id is passed in, our mock User DAO will return the User object
+ when(mockCupboardDAO.deleteNeed(id)).thenReturn(true);
+ when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]);
+
+ // Invoke
+ boolean response = cupboardService.deleteNeed(id);
+ Need[] responseNeeds = cupboardService.getNeeds();
+
+ // Analyze
+ assertTrue(response);
+ assertArrayEquals(new Need[0], responseNeeds);
+ }
+
+} \ No newline at end of file
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java
new file mode 100644
index 0000000..0a0cb71
--- /dev/null
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java
@@ -0,0 +1,126 @@
+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);
+ userService = new UserService(mockUserDAO);
+ }
+
+ @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, DuplicateKeyException {
+ // 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, DuplicateKeyException {
+ // 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, DuplicateKeyException {
+ // 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, DuplicateKeyException {
+ // 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, DuplicateKeyException {
+ // 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));
+ }
+
+}