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.java37
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java124
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java13
3 files changed, 100 insertions, 74 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
index 55cf7a9..4f58b12 100644
--- 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
@@ -11,7 +11,6 @@ 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;
@@ -41,39 +40,39 @@ public class AuthServiceTest {
}
@Test
- public void testAuthenticate() throws IOException {
+ public void testKeyIsValid() 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));
+ assertDoesNotThrow(() -> authService.keyHasAccessToUser(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 testKeyIsValidMismatchName() throws IOException {
+ // Mock
+ when(mockAuthDAO.getUserAuth(key)).thenReturn(new UserAuth(key, "EvilFish", null));
+ when(mockUserService.getUser("EvilFish")).thenReturn(user);
+
+ // Analyze
+ assertThrows(IllegalAccessException.class, () -> authService.keyHasAccessToUser(username, key));
+
+ }
@Test
- public void testAuthenticateMissingUserAuth() throws IOException {
+ public void testKeyIsValidMissingUserAuth() throws IOException {
// Mock
when(mockAuthDAO.getUserAuth(key)).thenReturn(null);
// Analyze
- assertThrows(IllegalAccessException.class, () -> authService.authenticate(username, key));
+ assertThrows(IllegalAccessException.class, () -> authService.keyHasAccessToUser(username, key));
}
@Test
- public void testLogin() throws IOException, DuplicateKeyException, IllegalAccessException {
+ public void testLogin() throws IOException {
// Mock
when(mockUserService.getUser(username)).thenReturn(user);
@@ -83,7 +82,7 @@ public class AuthServiceTest {
}
@Test
- public void testLoginNullUser() throws IOException, DuplicateKeyException, IllegalAccessException {
+ public void testLoginNullUser() throws IOException {
// Mock
when(mockUserService.getUser(username)).thenReturn(null);
@@ -92,7 +91,7 @@ public class AuthServiceTest {
}
@Test
- public void testLoginMismatchPasswords() throws IOException, DuplicateKeyException, IllegalAccessException {
+ public void testLoginMismatchPasswords() throws IOException {
// Mock
when(mockUserService.getUser(username)).thenReturn(User.create(username, "fries"));
@@ -101,7 +100,7 @@ public class AuthServiceTest {
}
@Test
- public void testLogout() throws IOException, DuplicateKeyException, IllegalAccessException {
+ public void testLogout() {
// 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 99ca23c..2a3c8ee 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
@@ -27,7 +27,8 @@ public class CupboardServiceTest {
@BeforeEach
public void setupCupboardService() {
mockCupboardDAO = mock(CupboardDAO.class);
- cupboardService = new CupboardService(mockCupboardDAO);
+ AuthService mockAuthService = mock(AuthService.class);
+ cupboardService = new CupboardService(mockAuthService, mockCupboardDAO);
}
@@ -35,16 +36,20 @@ public class CupboardServiceTest {
public void testCreateNeed() throws IOException, DuplicateKeyException {
// Setup
String name = "Jellyfish";
- double maxGoal = 100.00;
- GoalType type = GoalType.MONETARY;
- Need need = new Need(name, type, maxGoal);
+ String location = "Atlantis";
+ double maxGoal = 100;
+ GoalType type = Need.GoalType.MONETARY;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
// 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);
+ Need response = cupboardService.createNeed(name, location, image, maxGoal, type, urgent, description);
// Analyze
assertNotNull(response);
@@ -52,12 +57,16 @@ public class CupboardServiceTest {
}
@Test
- public void testCreateNeedBadGoal() throws IOException, DuplicateKeyException {
+ public void testCreateNeedBadGoal() throws IOException {
// Setup
String name = "Jellyfish";
- double maxGoal = -100.00;
- GoalType type = GoalType.MONETARY;
- Need need = new Need(name, type, maxGoal);
+ String location = "Atlantis";
+ double maxGoal = -100.0;
+ GoalType type = Need.GoalType.MONETARY;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
// When the same id is passed in, our mock User DAO will return the User object
when(mockCupboardDAO.addNeed(any())).thenReturn(need);
@@ -67,18 +76,21 @@ public class CupboardServiceTest {
// Need response = cupboardService.createNeed(name, maxGoal, type);
// Analyze
- assertThrows(IllegalArgumentException.class, () -> {
- cupboardService.createNeed(name, maxGoal, type);
- });
+ assertThrows(IllegalArgumentException.class, () ->
+ cupboardService.createNeed(name, location, image, maxGoal, type, urgent, description));
}
@Test
- public void testCreateNeedDuplicate() throws IOException, DuplicateKeyException {
+ public void testCreateNeedDuplicate() throws IOException {
// Setup
String name = "Jellyfish";
- double maxGoal = 100.00;
- GoalType type = GoalType.MONETARY;
- Need need = new Need(name, type, maxGoal);
+ String location = "Atlantis";
+ double maxGoal = 100.0;
+ GoalType type = Need.GoalType.MONETARY;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
Need[] needs = { need };
// When the same id is passed in, our mock User DAO will return the User object
@@ -89,18 +101,21 @@ public class CupboardServiceTest {
// Need response = cupboardService.createNeed(name, maxGoal, type);
// Analyze
- assertThrows(DuplicateKeyException.class, () -> {
- cupboardService.createNeed(name, maxGoal, type);
- });
+ assertThrows(DuplicateKeyException.class, () ->
+ cupboardService.createNeed(name, location, image, maxGoal, type, urgent, description));
}
@Test
- public void testSearchNeeds() throws IOException, DuplicateKeyException {
+ public void testSearchNeeds() throws IOException {
// Setup
String name = "Jellyfish";
- double maxGoal = 100.00;
- GoalType type = GoalType.MONETARY;
- Need need = new Need(name, type, maxGoal);
+ String location = "Atlantis";
+ double maxGoal = 100.0;
+ GoalType type = Need.GoalType.MONETARY;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
Need[] needs = { need };
// When the same id is passed in, our mock User DAO will return the User object
@@ -115,12 +130,16 @@ public class CupboardServiceTest {
}
@Test
- public void testSearchNeedsFail() throws IOException, DuplicateKeyException {
+ public void testSearchNeedsFail() throws IOException {
// Setup
String name = "Jellyfish";
- double maxGoal = 100.00;
- GoalType type = GoalType.MONETARY;
- Need need = new Need(name, type, maxGoal);
+ String location = "Atlantis";
+ double maxGoal = 100.0;
+ GoalType type = Need.GoalType.MONETARY;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
Need[] needs = { need };
// When the same id is passed in, our mock User DAO will return the User object
@@ -134,59 +153,68 @@ public class CupboardServiceTest {
}
@Test
- public void testGetNeed() throws IOException, DuplicateKeyException {
+ public void testGetNeed() throws IOException {
// Setup
String name = "Jellyfish";
- double maxGoal = 100.00;
- int id = 0;
- GoalType type = GoalType.MONETARY;
- Need need = new Need(name, type, maxGoal);
+ String location = "Atlantis";
+ double maxGoal = 100.0;
+ GoalType type = Need.GoalType.MONETARY;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
// When the same id is passed in, our mock User DAO will return the User object
- when(mockCupboardDAO.getNeed(id)).thenReturn(need);
+ when(mockCupboardDAO.getNeed(0)).thenReturn(need);
// Invoke
- Need response = cupboardService.getNeed(id);
+ Need response = cupboardService.getNeed(need.getId());
// Analyze
assertEquals(need, response);
}
@Test
- public void testUpdateNeed() throws IOException, DuplicateKeyException {
+ public void testUpdateNeed() throws IOException {
// 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);
+ String location = "Atlantis";
+ double maxGoal = 100.0;
+ GoalType type = Need.GoalType.MONETARY;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
+ Need newNeed = new Need("Octopus", image, location, maxGoal, type, urgent, description);
// 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);
+ Need response = cupboardService.updateNeed(newNeed, need.getId());
// Analyze
assertEquals(newNeed, response);
}
@Test
- public void testDeleteNeed() throws IOException, DuplicateKeyException {
+ public void testDeleteNeed() throws IOException {
// Setup
String name = "Jellyfish";
- double maxGoal = 100.00;
- int id = 0;
- GoalType type = GoalType.MONETARY;
- Need need = new Need(name, type, maxGoal);
+ String location = "Atlantis";
+ double maxGoal = 100.0;
+ GoalType type = Need.GoalType.MONETARY;
+ boolean urgent = false;
+ String image = "";
+ String description = "";
+ var need = new Need(name, image, location, maxGoal, type, urgent, description);
// When the same id is passed in, our mock User DAO will return the User object
- when(mockCupboardDAO.deleteNeed(id)).thenReturn(true);
+ when(mockCupboardDAO.deleteNeed(0)).thenReturn(true);
when(mockCupboardDAO.getNeeds()).thenReturn(new Need[0]);
// Invoke
- boolean response = cupboardService.deleteNeed(id);
+ boolean response = cupboardService.deleteNeed(need.getId());
Need[] responseNeeds = cupboardService.getNeeds();
// Analyze
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
index e57c5a3..5adabf1 100644
--- 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
@@ -19,13 +19,12 @@ public class UserServiceTest {
private UserService userService;
private UserDAO mockUserDAO;
- private CupboardService mockCupboardService;
@BeforeEach
public void setupUserService() {
mockUserDAO = mock(UserDAO.class);
- mockCupboardService = mock(CupboardService.class);
+ CupboardService mockCupboardService = mock(CupboardService.class);
userService = new UserService(mockUserDAO, mockCupboardService);
}
@@ -47,7 +46,7 @@ public class UserServiceTest {
}
@Test
- public void testCreateUserDuplicate() throws IOException, DuplicateKeyException {
+ public void testCreateUserDuplicate() throws IOException {
// Setup
String username = "Jelly";
String password = "Fish";
@@ -62,7 +61,7 @@ public class UserServiceTest {
}
@Test
- public void testGetUser() throws IOException, DuplicateKeyException {
+ public void testGetUser() throws IOException {
// Setup
String username = "Jelly";
String password = "Fish";
@@ -76,7 +75,7 @@ public class UserServiceTest {
}
@Test
- public void testUpdateUser() throws IOException, DuplicateKeyException {
+ public void testUpdateUser() throws IOException {
// Setup
String username = "Jelly";
String password = "Fish";
@@ -94,7 +93,7 @@ public class UserServiceTest {
}
@Test
- public void testUpdateUserDifferentUsernames() throws IOException, DuplicateKeyException {
+ public void testUpdateUserDifferentUsernames() throws IOException {
// Setup
String username = "Jelly";
String password = "Fish";
@@ -112,7 +111,7 @@ public class UserServiceTest {
}
@Test
- public void testDeleteUser() throws IOException, DuplicateKeyException {
+ public void testDeleteUser() throws IOException {
// Setup
String username = "Jelly";
String password = "Fish";