aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/test/java/com
diff options
context:
space:
mode:
Diffstat (limited to 'ufund-api/src/test/java/com')
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java104
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java114
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java191
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/model/NeedTest.java25
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java50
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/CupboardFileDAOTest.java (renamed from ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/CupboardFileDaoTest.java)58
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java63
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserFileDAOTest.java51
-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.java197
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java126
11 files changed, 901 insertions, 188 deletions
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java
new file mode 100644
index 0000000..3d4637d
--- /dev/null
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java
@@ -0,0 +1,104 @@
+package com.ufund.api.ufundapi.controller;
+
+import java.io.IOException;
+import java.util.Map;
+import static java.util.Map.entry;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import static org.mockito.ArgumentMatchers.any;
+import org.mockito.Mockito;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import com.ufund.api.ufundapi.service.AuthService;
+
+@RequestMapping("auth")
+public class AuthControllerTest {
+
+ private AuthController authController;
+ private AuthService mockAuthService;
+ private Map<String, String> authMap;
+
+ @BeforeEach
+ private void setupAuthController() {
+ mockAuthService = mock(AuthService.class);
+ authController = new AuthController(mockAuthService);
+
+ authMap = Map.ofEntries(
+ entry("Bob", "123")
+ );
+ }
+
+ @Test
+ public void testLogin() throws IllegalAccessException, IOException {
+ // Setup
+ String key = "123";
+
+ // Mock
+ when(mockAuthService.login(any(), any())).thenReturn(key);
+
+ // Invoke
+ ResponseEntity<String> response = authController.login(authMap);
+
+ // Analyze
+ assertEquals(HttpStatus.OK, response.getStatusCode());
+ assertEquals(key, response.getBody());
+ }
+
+ @Test
+ public void testLoginUnauthorized() throws IllegalAccessException, IOException {
+ // Mock
+ when(mockAuthService.login(any(), any())).thenThrow(IllegalAccessException.class);
+
+ // Invoke
+ ResponseEntity<String> response = authController.login(authMap);
+
+ // Analyze
+ assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
+ }
+
+ @Test
+ public void testLoginIOException() throws IllegalAccessException, IOException {
+ // Mock
+ when(mockAuthService.login(any(), any())).thenThrow(IOException.class);
+
+ // Invoke
+ ResponseEntity<String> response = authController.login(authMap);
+
+ // Analyze
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
+ }
+
+ @Test
+ public void testLogout() throws IllegalAccessException, IOException {
+ // Setup
+ String key = "123";
+
+ // Invoke
+ ResponseEntity<Object> response = authController.logout(key);
+
+ // Analyze
+ assertEquals(HttpStatus.OK, response.getStatusCode());
+ }
+
+ @Test
+ public void testLogoutIOException() throws IllegalAccessException, IOException {
+ // Setup
+ String key = "123";
+
+ // Mock
+ doThrow(new IOException()).when(mockAuthService).logout(key);
+
+ // Invoke
+ ResponseEntity<Object> response = authController.logout(key);
+
+ // Analyze
+ assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
+ }
+}
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java
index 839c518..94f93cb 100644
--- a/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/controller/CupboardControllerTest.java
@@ -1,6 +1,8 @@
package com.ufund.api.ufundapi.controller;
import java.io.IOException;
+import java.util.Map;
+import static java.util.Map.entry;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -11,54 +13,74 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.springframework.http.HttpStatus;
+import com.ufund.api.ufundapi.DuplicateKeyException;
import com.ufund.api.ufundapi.model.Need;
-import com.ufund.api.ufundapi.persistence.CupboardFileDao;
+import com.ufund.api.ufundapi.model.Need.GoalType;
+import com.ufund.api.ufundapi.service.CupboardService;
public class CupboardControllerTest {
private CupboardController cupboardController;
- private CupboardFileDao mockCupboardDAO;
+ private CupboardService mockCupboardService;
@BeforeEach
public void setupCupboardDAO() {
- mockCupboardDAO = mock(CupboardFileDao.class);
- cupboardController = new CupboardController(mockCupboardDAO);
+ mockCupboardService = mock(CupboardService.class);
+ cupboardController = new CupboardController(mockCupboardService);
}
@Test
- public void createNeed() throws IOException {
- var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.createNeed(need)).thenReturn(need);
+ public void createNeed() throws IOException, DuplicateKeyException {
+ String name = "Test";
+ int maxGoal = 100;
+ GoalType type = Need.GoalType.MONETARY;
+ var need = new Need(name, type, maxGoal);
+ when(mockCupboardService.createNeed(name, maxGoal, type)).thenReturn(need);
+
- var res = cupboardController.createNeed(need);
+ Map<String, Object> needMap = Map.ofEntries(
+ entry("name", "Test"),
+ entry("maxGoal", 100),
+ entry("type", "MONETARY")
+ );
+
+ var res = cupboardController.createNeed(needMap);
assertEquals(HttpStatus.OK, res.getStatusCode());
assertEquals(need, res.getBody());
}
@Test
- public void createNeedBadMaxGoal() throws IOException {
- var need = new Need("Name", 1, -100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.createNeed(need)).thenReturn(need);
+ public void createNeedBadMaxGoal() throws IOException, DuplicateKeyException {
+ when(mockCupboardService.createNeed("Name", -100, Need.GoalType.MONETARY)).thenThrow(new IllegalArgumentException());
+
+ Map<String, Object> needMap = Map.ofEntries(
+ entry("name", "Name"),
+ entry("maxGoal", -100),
+ entry("type", "MONETARY"));
- var res = cupboardController.createNeed(need);
+ var res = cupboardController.createNeed(needMap);
- assertEquals(HttpStatus.BAD_REQUEST, res.getStatusCode());
+ assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, res.getStatusCode());
}
@Test
- public void createNeedIOException() throws IOException {
- var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.createNeed(need)).thenThrow(new IOException());
+ public void createNeedIOException() throws IOException, DuplicateKeyException {
+ when(mockCupboardService.createNeed("Name", 100, Need.GoalType.MONETARY)).thenThrow(new IOException());
+
+ Map<String, Object> needMap = Map.ofEntries(
+ entry("name", "Name"),
+ entry("maxGoal", 100),
+ entry("type", "MONETARY"));
- var res = cupboardController.createNeed(need);
+ var res = cupboardController.createNeed(needMap);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());
}
@Test
- public void getNeeds() {
+ public void getNeeds() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.getNeeds()).thenReturn(new Need[]{need});
+ when(mockCupboardService.getNeeds()).thenReturn(new Need[]{need});
var res = cupboardController.getNeeds();
@@ -67,9 +89,8 @@ public class CupboardControllerTest {
}
@Test
- public void getNeedsIOException() {
- var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.getNeeds()).thenThrow(new IOException());
+ public void getNeedsIOException() throws IOException {
+ when(mockCupboardService.getNeeds()).thenThrow(new IOException());
var res = cupboardController.getNeeds();
@@ -77,8 +98,8 @@ public class CupboardControllerTest {
}
@Test
- public void getNeedsEmpty() {
- when(mockCupboardDAO.getNeeds()).thenReturn(new Need[]{});
+ public void getNeedsEmpty() throws IOException {
+ when(mockCupboardService.getNeeds()).thenReturn(new Need[]{});
var res = cupboardController.getNeeds();
@@ -87,9 +108,9 @@ public class CupboardControllerTest {
}
@Test
- public void searchNeeds() {
+ public void searchNeeds() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.findNeeds("Na")).thenReturn(new Need[]{need});
+ when(mockCupboardService.searchNeeds("Na")).thenReturn(new Need[]{need});
var res = cupboardController.searchNeeds("Na");
@@ -98,9 +119,8 @@ public class CupboardControllerTest {
}
@Test
- public void searchNeedsIOException() {
- var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.findNeeds("Na")).thenThrow(new IOException());
+ public void searchNeedsIOException() throws IOException {
+ when(mockCupboardService.searchNeeds("Na")).thenThrow(new IOException());
var res = cupboardController.searchNeeds("Na");
@@ -108,8 +128,8 @@ public class CupboardControllerTest {
}
@Test
- public void searchNeedsEmpty() {
- when(mockCupboardDAO.findNeeds("Na")).thenReturn(new Need[]{});
+ public void searchNeedsEmpty() throws IOException {
+ when(mockCupboardService.searchNeeds("Na")).thenReturn(new Need[]{});
var res = cupboardController.searchNeeds("Na");
@@ -118,9 +138,9 @@ public class CupboardControllerTest {
}
@Test
- public void getNeed() {
+ public void getNeed() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.getNeed(need.getId())).thenReturn(need);
+ when(mockCupboardService.getNeed(need.getId())).thenReturn(need);
var res = cupboardController.getNeed(need.getId());
@@ -129,9 +149,9 @@ public class CupboardControllerTest {
}
@Test
- public void getNeedIOException() {
+ public void getNeedIOException() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.getNeed(need.getId())).thenThrow(new IOException());
+ when(mockCupboardService.getNeed(need.getId())).thenThrow(new IOException());
var res = cupboardController.getNeed(need.getId());
@@ -139,9 +159,9 @@ public class CupboardControllerTest {
}
@Test
- public void getNeedFail() {
+ public void getNeedFail() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.getNeed(need.getId())).thenReturn(null);
+ when(mockCupboardService.getNeed(need.getId())).thenReturn(null);
var res = cupboardController.getNeed(need.getId());
@@ -152,9 +172,9 @@ public class CupboardControllerTest {
@Test
public void updateNeeds() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.updateNeed(need)).thenReturn(need);
+ when(mockCupboardService.updateNeed(need, 1)).thenReturn(need);
- var res = cupboardController.updateNeed(need);
+ var res = cupboardController.updateNeed(need, 1);
assertEquals(HttpStatus.OK, res.getStatusCode());
assertEquals(need, res.getBody());
@@ -163,9 +183,9 @@ public class CupboardControllerTest {
@Test
public void updateNeedsIOException() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.updateNeed(need)).thenThrow(new IOException());
+ when(mockCupboardService.updateNeed(need, 1)).thenThrow(new IOException());
- var res = cupboardController.updateNeed(need);
+ var res = cupboardController.updateNeed(need, 1);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode());
}
@@ -173,8 +193,8 @@ public class CupboardControllerTest {
@Test
public void deleteNeed() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.getNeed(1)).thenReturn(need);
- when(mockCupboardDAO.deleteNeed(1)).thenReturn(true);
+ when(mockCupboardService.getNeed(1)).thenReturn(need);
+ when(mockCupboardService.deleteNeed(1)).thenReturn(true);
var res = cupboardController.deleteNeed(1);
@@ -183,8 +203,8 @@ public class CupboardControllerTest {
@Test
public void deleteNeedFail() throws IOException {
- when(mockCupboardDAO.getNeed(1)).thenReturn(null);
- when(mockCupboardDAO.deleteNeed(1)).thenReturn(false);
+ when(mockCupboardService.getNeed(1)).thenReturn(null);
+ when(mockCupboardService.deleteNeed(1)).thenReturn(false);
var res = cupboardController.deleteNeed(1);
@@ -194,8 +214,8 @@ public class CupboardControllerTest {
@Test
public void deleteNeedIOException() throws IOException {
var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);
- when(mockCupboardDAO.getNeed(1)).thenReturn(need);
- when(mockCupboardDAO.deleteNeed(1)).thenThrow(new IOException());
+ when(mockCupboardService.getNeed(1)).thenReturn(need);
+ when(mockCupboardService.deleteNeed(1)).thenThrow(new IOException());
var res = cupboardController.deleteNeed(1);
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
index 681f47c..b6367ad 100644
--- 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
@@ -1,8 +1,12 @@
package com.ufund.api.ufundapi.controller;
import java.io.IOException;
+import java.security.InvalidParameterException;
+import java.util.Map;
+import static java.util.Map.entry;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
@@ -12,82 +16,111 @@ import static org.mockito.Mockito.when;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
+import com.ufund.api.ufundapi.DuplicateKeyException;
import com.ufund.api.ufundapi.model.User;
-import com.ufund.api.ufundapi.persistence.UserFileDAO;
+import com.ufund.api.ufundapi.model.UserAuth;
+import com.ufund.api.ufundapi.service.AuthService;
+import com.ufund.api.ufundapi.service.UserService;
@Tag("Controller-tier")
public class UserControllerTest {
private UserController userController;
- private UserFileDAO mockUserDAO;
+ private AuthService mockAuthService;
+ private UserService mockUserService;
+ private Map<String, String> userMap;
@BeforeEach
public void setupUserController() {
- mockUserDAO = mock(UserFileDAO.class);
- userController = new UserController(mockUserDAO);
-
+ mockUserService = mock(UserService.class);
+ mockAuthService = mock(AuthService.class);
+ userController = new UserController(mockUserService, mockAuthService);
+ userMap = Map.ofEntries(
+ entry("username", "Test"),
+ entry("password", "Pass")
+ );
}
@Test
public void testGetUser() throws IOException { // getUser may throw IOException
// Setup
String username = "Test";
- User user = new User(username);
+ User user = User.create(username, "pass");
+ String key = UserAuth.generate(username).getKey( );
// When the same id is passed in, our mock User DAO will return the User object
- when(mockUserDAO.getUser(username)).thenReturn(user);
+ when(mockUserService.getUser(username)).thenReturn(user);
+
// Invoke
- ResponseEntity<User> response = userController.getUser(username);
+ ResponseEntity<User> response = userController.getUser(username, key);
// Analyze
assertEquals(HttpStatus.OK, response.getStatusCode());
- assertEquals(user, response.getBody());
+ assertNotNull(response.getBody());
+ assertEquals(user.getUsername(), response.getBody().getUsername());
}
@Test
public void testGetUserNotFound() throws Exception { // createUser may throw IOException
// Setup
String username = "Test";
- // When the same id is passed in, our mock User DAO will return null, simulating
+ String key = UserAuth.generate(username).getKey();
+ // When the same id is passed in, our mock User service will return null, simulating
// no User found
- when(mockUserDAO.getUser(username)).thenReturn(null);
+ when(mockUserService.getUser(username)).thenReturn(null);
+
// Invoke
- ResponseEntity<User> response = userController.getUser(username);
+ ResponseEntity<User> response = userController.getUser(username, key);
// Analyze
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}
@Test
+ public void testGetUserUnauthorized() throws Exception { // createUser may throw IOException
+ // Setup
+ String username = "Test";
+ String key = UserAuth.generate(username).getKey();
+ // When getUser is called on the Mock User service, throw an IOException
+ // doThrow(new IllegalAccessException()).when(mockUserService).getUser(username);
+ doThrow(new IllegalAccessException()).when(mockAuthService).authenticate(username, key);
+
+ // Invoke
+ ResponseEntity<User> response = userController.getUser(username, key);
+
+ // Analyze
+ assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
+ }
+
+ @Test
public void testGetUserHandleException() throws Exception { // createUser may throw IOException
// Setup
String username = "Test";
- // When getUser is called on the Mock User DAO, throw an IOException
- doThrow(new IOException()).when(mockUserDAO).getUser(username);
+ String key = UserAuth.generate(username).getKey();
+ // When getUser is called on the Mock User service, throw an IOException
+ doThrow(new IOException()).when(mockUserService).getUser(username);
// Invoke
- ResponseEntity<User> response = userController.getUser(username);
+ ResponseEntity<User> response = userController.getUser(username, key);
// Analyze
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
}
- /*****************************************************************
- * The following tests will fail until all userController methods
- * are implemented.
- ****************************************************************/
-
@Test
- public void testCreateUser() throws IOException { // createUser may throw IOException
+ public void testCreateUser() throws IOException, DuplicateKeyException { // createUser may throw IOException
// Setup
String username = "Test";
- User user = new User(username);
+ String password = "Pass";
+ User user = User.create(username, "pass");
// when createUser is called, return true simulating successful
// creation and save
- when(mockUserDAO.createUser(user)).thenReturn(user);
+ when(mockUserService.createUser(username, password)).thenReturn(user);
+
+
// Invoke
- ResponseEntity<User> response = userController.createUser(user);
+ ResponseEntity<User> response = userController.createUser(userMap);
// Analyze
assertEquals(HttpStatus.CREATED, response.getStatusCode());
@@ -95,32 +128,52 @@ public class UserControllerTest {
}
@Test
- public void testCreateUserFailed() throws IOException { // createUser may throw IOException
+ public void testCreateUserFailed() throws IOException, DuplicateKeyException { // createUser may throw IOException
// Setup
String username = "Test";
- User user = new User(username);
+ String password = "Pass";
// when createUser is called, return false simulating failed
// creation and save
- when(mockUserDAO.createUser(user)).thenReturn(null);
+ when(mockUserService.createUser(username, password)).thenReturn(null);
+
+
// Invoke
- ResponseEntity<User> response = userController.createUser(user);
+ ResponseEntity<User> response = userController.createUser(userMap);
// Analyze
assertEquals(HttpStatus.CONFLICT, response.getStatusCode());
}
@Test
- public void testCreateUserHandleException() throws IOException { // createUser may throw IOException
+ public void testCreateUserDuplicate() throws IOException, DuplicateKeyException { // createUser may throw IOException
// Setup
String username = "Test";
- User user = new User(username);
+ String password = "Pass";
+ // when createUser is called, return false simulating failed
+ // creation and save
+ when(mockUserService.createUser(username, password)).thenThrow(DuplicateKeyException.class);
- // When createUser is called on the Mock User DAO, throw an IOException
- doThrow(new IOException()).when(mockUserDAO).createUser(user);
+ // Invoke
+ ResponseEntity<User> response = userController.createUser(userMap);
+
+ // Analyze
+ assertEquals(HttpStatus.CONFLICT, response.getStatusCode());
+ }
+
+ @Test
+ public void testCreateUserHandleException() throws IOException, DuplicateKeyException { // createUser may throw IOException
+ // Setup
+ String username = "Test";
+ String password = "Pass";
+
+ // When createUser is called on the Mock User service, throw an IOException
+ doThrow(new IOException()).when(mockUserService).createUser(username, password);
+
+
// Invoke
- ResponseEntity<User> response = userController.createUser(user);
+ ResponseEntity<User> response = userController.createUser(userMap);
// Analyze
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
@@ -130,13 +183,14 @@ public class UserControllerTest {
public void testUpdateUser() throws IOException { // updateUser may throw IOException
// Setup
String username = "Test";
- User user = new User("Bob");
+ User user = User.create(username, "pass");
+ String key = UserAuth.generate(username).getKey();
// when updateUser is called, return true simulating successful
// update and save
- when(mockUserDAO.updateUser(user, username)).thenReturn(user);
+ when(mockUserService.updateUser(user, username)).thenReturn(user);
// Invoke
- ResponseEntity<User> response = userController.updateUser(user, username);
+ ResponseEntity<User> response = userController.updateUser(user, username, key);
// Analyze
assertEquals(HttpStatus.OK, response.getStatusCode());
@@ -147,42 +201,62 @@ public class UserControllerTest {
public void testUpdateUserFailed() throws IOException { // updateUser may throw IOException
// Setup
String username = "Test";
- User user = new User("Bob");
+ User user = User.create(username, "pass");
+ String key = UserAuth.generate(username).getKey();
// when updateUser is called, return true simulating successful
// update and save
- when(mockUserDAO.updateUser(user, username)).thenReturn(null);
+ when(mockUserService.updateUser(user, username)).thenReturn(null);
// Invoke
- ResponseEntity<User> response = userController.updateUser(user, username);
+ ResponseEntity<User> response = userController.updateUser(user, username, key);
// Analyze
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}
@Test
- public void testUpdateUserHandleException() throws IOException { // updateUser may throw IOException
+ public void testUpdateUserInvalidParameter() throws IOException { // updateUser may throw IOException
// Setup
String username = "Test";
- User user = new User("Bob");
+ User user = User.create(username, "pass");
+ String key = UserAuth.generate(username).getKey();
// When updateUser is called on the Mock User DAO, throw an IOException
- doThrow(new IOException()).when(mockUserDAO).updateUser(user, username);
+ doThrow(new IOException()).when(mockUserService).updateUser(user, username);
// Invoke
- ResponseEntity<User> response = userController.updateUser(user, username);
+ ResponseEntity<User> response = userController.updateUser(user, username, key);
// Analyze
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
}
@Test
+ public void testUpdateUserUnauthorized() throws IOException, IllegalAccessException { // updateUser may throw IOException
+ // Setup
+ String username = "Test";
+ User user = User.create(username, "pass");
+ String key = UserAuth.generate(username).getKey();
+ // When updateUser is called on the Mock User service, throw a Invalid Parameter exception
+ // exception
+ doThrow(new IllegalAccessException()).when(mockAuthService).authenticate(username, key);
+
+ // Invoke
+ ResponseEntity<User> response = userController.updateUser(user, username, key);
+
+ // Analyze
+ assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
+ }
+
+ @Test
public void testDeleteUser() throws IOException { // deleteUser may throw IOException
// Setup
String username = "Test";
+ String key = UserAuth.generate(username).getKey();
// when deleteUser is called return true, simulating successful deletion
- when(mockUserDAO.deleteUser(username)).thenReturn(true);
+ when(mockUserService.deleteUser(username)).thenReturn(true);
// Invoke
- ResponseEntity<User> response = userController.deleteUser(username);
+ ResponseEntity<Boolean> response = userController.deleteUser(username, key);
// Analyze
assertEquals(HttpStatus.OK, response.getStatusCode());
@@ -192,11 +266,12 @@ public class UserControllerTest {
public void testDeleteUserNotFound() throws IOException { // deleteUser may throw IOException
// Setup
String username = "Test";
+ String key = UserAuth.generate(username).getKey();
// when deleteUser is called return false, simulating failed deletion
- when(mockUserDAO.deleteUser(username)).thenReturn(false);
+ when(mockUserService.deleteUser(username)).thenReturn(false);
// Invoke
- ResponseEntity<User> response = userController.deleteUser(username);
+ ResponseEntity<Boolean> response = userController.deleteUser(username, key);
// Analyze
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
@@ -206,14 +281,30 @@ public class UserControllerTest {
public void testDeleteUserHandleException() throws IOException { // deleteUser may throw IOException
// Setup
String username = "Test";
- // When deleteUser is called on the Mock User DAO, throw an IOException
- doThrow(new IOException()).when(mockUserDAO).deleteUser(username);
+ String key = UserAuth.generate(username).getKey();
+ // When deleteUser is called on the Mock User service, throw an IOException
+ doThrow(new IOException()).when(mockUserService).deleteUser(username);
// Invoke
- ResponseEntity<User> response = userController.deleteUser(username);
+ ResponseEntity<Boolean> response = userController.deleteUser(username, key);
// Analyze
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
}
+ @Test
+ public void testDeleteUserUnauthorized() throws IOException, IllegalAccessException { // deleteUser may throw IOException
+ // Setup
+ String username = "Test";
+ String key = UserAuth.generate(username).getKey();
+ // When deleteUser is called on the Mock User service, throw an IOException
+ doThrow(new IllegalAccessException()).when(mockAuthService).authenticate(username, key);
+
+ // Invoke
+ ResponseEntity<Boolean> response = userController.deleteUser(username, key);
+
+ // Analyze
+ assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
+ }
+
}
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/model/NeedTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/model/NeedTest.java
index ffcd808..6b4ddfc 100644
--- a/ufund-api/src/test/java/com/ufund/api/ufundapi/model/NeedTest.java
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/model/NeedTest.java
@@ -14,13 +14,10 @@ public class NeedTest {
public void createNeed() {
String name = "Jellyfish";
- int id = 0;
double maxGoal = 100.00;
GoalType type = GoalType.MONETARY;
- Need need = new Need(name, id, maxGoal, type);
-
+ Need need = new Need(name, type, maxGoal);
assertNotNull(need);
-
}
@Test
@@ -29,7 +26,7 @@ public class NeedTest {
int id = 0;
double maxGoal = 100.00;
GoalType type = GoalType.MONETARY;
- Need need = new Need(name, id, maxGoal, type);
+ Need need = new Need(name, type, maxGoal);
assertEquals(name, need.getName());
@@ -41,22 +38,21 @@ public class NeedTest {
@Test
public void testCurrentGoal() {
String name = "Jellyfish";
- int id = 0;
double maxGoal = 100.00;
GoalType type = GoalType.MONETARY;
- Need need = new Need(name, id, maxGoal, type);
+ Need need = new Need(name, type, maxGoal);
double current = 0.00;
need.setCurrent(current);
- assertEquals(need.getCurrent(), current);
+ assertEquals(current, need.getCurrent());
current = 100.00;
need.setCurrent(current);
- assertEquals(need.getCurrent(), current);
+ assertEquals(current, need.getCurrent());
current = -100.00;
need.setCurrent(current);
- assertEquals(need.getCurrent(), current);
+ assertEquals(current, need.getCurrent());
}
@@ -64,10 +60,9 @@ public class NeedTest {
public void testFilterAttributes() {
String name = "Jellyfish";
- int id = 0;
double maxGoal = 100.00;
GoalType type = GoalType.MONETARY;
- Need need = new Need(name, id, maxGoal, type);
+ Need need = new Need(name, type, maxGoal);
String[] filterAttributes = {"seaweed", "divers", "pacific", "plankton"};
@@ -80,10 +75,9 @@ public class NeedTest {
public void testSetMaxGoal() {
String name = "Jellyfish";
- int id = 0;
double maxGoal = 100.00;
GoalType type = GoalType.MONETARY;
- Need need = new Need(name, id, maxGoal, type);
+ Need need = new Need(name, type, maxGoal);
double newGoal = 200.00;
need.setMaxGoal(newGoal);
@@ -96,10 +90,9 @@ public class NeedTest {
public void testSetName() {
String name = "Jellyfish";
- int id = 0;
double maxGoal = 100.00;
GoalType type = GoalType.MONETARY;
- Need need = new Need(name, id, maxGoal, type);
+ Need need = new Need(name, type, maxGoal);
String newName = "TESTINGFUN";
need.setName(newName);
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java
index 716fbfd..54aa4d1 100644
--- a/ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java
@@ -1,19 +1,34 @@
package com.ufund.api.ufundapi.model;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.mock;
+
+import java.io.IOException;
+
+import org.junit.jupiter.api.BeforeEach;
+import static org.mockito.Mockito.when;
+
+import com.ufund.api.ufundapi.service.CupboardService;
+
@Tag("Model-tier")
public class UserTest {
+ private CupboardService cupboardService;
+
+ @BeforeEach
+ public void setup() {
+ cupboardService = mock(CupboardService.class);
+ }
+
@Test
public void createUser() {
String name = "Bob";
- User user = new User(name);
+ User user = User.create(name, "pass");
assertNotNull(user);
@@ -23,42 +38,51 @@ public class UserTest {
public void testUsername() {
String expectedName = "Bob";
+ String password = "password";
- User user = new User(expectedName);
+ User user = User.create(expectedName, password);
- assertEquals(expectedName, user.getName());
+ assertEquals(expectedName, user.getUsername());
}
@Test
- public void addNeedToBasket() {
+ public void addNeedToBasket() throws IOException {
String expectedName = "Bob";
- User user = new User(expectedName);
+ User user = User.create(expectedName, "pass");
Need need = new Need("Test", 0, 100, Need.GoalType.MONETARY);
Need[] needs = { need };
+ when(cupboardService.getNeed(0)).thenReturn(need);
+
user.addToBasket(need);
- assertEquals(needs[0], user.getBasketNeeds()[0]);
+ Need getNeed = cupboardService.getNeed(user.getBasketNeeds()[0]);
+
+ assertEquals(needs[0], getNeed);
}
@Test
- public void testRemoveBasketNeed() {
+ public void testRemoveBasketNeed() throws IOException {
String expectedName = "Bob";
- User user = new User(expectedName);
+ User user = User.create(expectedName, "pass");
Need need = new Need("Test", 0, 100, Need.GoalType.MONETARY);
Need need2 = new Need("Test2", 0, 100, Need.GoalType.MONETARY);
+ when(cupboardService.getNeed(0)).thenReturn(need2);
+
user.addToBasket(need);
user.removeBasketNeed(need);
user.addToBasket(need2);
- assertEquals(need2, user.getBasketNeeds()[0]);
+ Need getNeed = cupboardService.getNeed(user.getBasketNeeds()[0]);
+
+ assertEquals(need2, getNeed);
}
@@ -67,9 +91,9 @@ public class UserTest {
String expectedName = "Bob";
- User user = new User(expectedName);
+ User user = User.create(expectedName, "pass");
- assertEquals(false, user.verifyPassword(expectedName));
+ assertFalse(user.verifyPassword(expectedName));
}
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/CupboardFileDaoTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/CupboardFileDAOTest.java
index 8aa6fe0..f786a8c 100644
--- a/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/CupboardFileDaoTest.java
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/CupboardFileDAOTest.java
@@ -1,79 +1,65 @@
package com.ufund.api.ufundapi.persistence;
+import java.io.File;
+import java.io.IOException;
+
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import java.io.File;
-import java.io.IOException;
-
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ufund.api.ufundapi.model.Need;
-import com.ufund.api.ufundapi.model.User;
-
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Tag;
-import org.junit.jupiter.api.Test;
-
import com.ufund.api.ufundapi.model.Need.GoalType;
@Tag("Persistence-tier")
-public class CupboardFileDaoTest {
- CupboardFileDao cupboardFileDao;
- Need[] testNeeds;
- ObjectMapper mockObjectMapper;
+public class CupboardFileDAOTest {
+ private CupboardFileDAO cupboardFileDao;
+ private Need[] testNeeds;
+ private ObjectMapper mockObjectMapper;
@BeforeEach
public void setupCupboardFileDao() throws IOException {
mockObjectMapper = mock(ObjectMapper.class);
- testNeeds = new Need[3];
- testNeeds[0] = new Need("one", 0, 100, Need.GoalType.MONETARY);
- testNeeds[1] = new Need("two", 1, 100, Need.GoalType.MONETARY);
- testNeeds[2] = new Need("three", 2, 100, Need.GoalType.MONETARY);
+ testNeeds = new Need[]{
+ new Need("one", 0, 100, Need.GoalType.MONETARY),
+ new Need("two", 1, 100, Need.GoalType.MONETARY),
+ new Need("three", 2, 100, Need.GoalType.MONETARY)
+ };
// When the object mapper is supposed to read from the file
// the mock object mapper will return the hero array above
when(mockObjectMapper
.readValue(new File("doesnt_matter.txt"),Need[].class))
.thenReturn(testNeeds);
- cupboardFileDao = new CupboardFileDao("doesnt_matter.txt",mockObjectMapper);
+ cupboardFileDao = new CupboardFileDAO("doesnt_matter.txt",mockObjectMapper);
}
@Test
- public void GetNeedsTest() throws IOException {
+ public void getNeedsTest() {
Need[] needs = cupboardFileDao.getNeeds();
assertEquals(needs.length,testNeeds.length);
assertEquals(needs[0].getName(), testNeeds[0].getName());
}
@Test
- public void GetNeedTest() throws IOException {
+ public void getNeedTest() {
Need need1 = cupboardFileDao.getNeed(0);
assertEquals(testNeeds[0], need1);
}
@Test
- public void Fet() throws IOException {
- String targetName1 = "one";
- String targetName2 = "two";
-
- Need need1 = cupboardFileDao.findNeeds(targetName1)[0];
- Need need2 = cupboardFileDao.findNeeds(targetName2)[0];
-
- assertEquals(testNeeds[0], need1);
- assertEquals(testNeeds[1], need2);
- }
-
- @Test
- public void CreateNeedTest() throws IOException {
+ public void createNeedTest() throws IOException {
Need newNeed = new Need("sea urchin hats", 3, 100, GoalType.PHYSICAL);
- Need actualNeed = cupboardFileDao.createNeed(newNeed);
+ Need actualNeed = cupboardFileDao.addNeed(newNeed);
assertNotNull(actualNeed);
@@ -81,7 +67,7 @@ public class CupboardFileDaoTest {
}
@Test
- public void DeleteNeedTest() throws IOException {
+ public void deleteNeedTest() throws IOException {
Need undeletedNeed = cupboardFileDao.getNeed(0);
assertNotNull(undeletedNeed);
@@ -93,7 +79,7 @@ public class CupboardFileDaoTest {
}
@Test
- public void UpdateNeedTest() throws IOException {
+ public void updateNeedTest() throws IOException {
Need[] needs = cupboardFileDao.getNeeds();
Need unupdatedNeed = needs[needs.length - 1];
assertNotNull(unupdatedNeed);
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java
new file mode 100644
index 0000000..f7db747
--- /dev/null
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java
@@ -0,0 +1,63 @@
+package com.ufund.api.ufundapi.persistence;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.ufund.api.ufundapi.model.UserAuth;
+
+@Tag("Persistence-tier")
+public class UserAuthFileDAOTest {
+
+ private UserAuthFIleDAO userAuthFIleDAO;
+ private ObjectMapper mockObjectMapper;
+ private UserAuth[] userAuths;
+
+ @BeforeEach
+ public void setupUserAuthFileDAO() throws IOException {
+
+ mockObjectMapper = mock(ObjectMapper.class);
+ userAuths = new UserAuth[]{
+ new UserAuth("123", "Phil", null),
+ new UserAuth("456", "Bob", null),
+ new UserAuth("789", "Steve", null)
+ };
+ // When the object mapper is supposed to read from the file
+ // the mock object mapper will return the hero array above
+ when(mockObjectMapper
+ .readValue(new File("doesnt_matter.txt"),UserAuth[].class))
+ .thenReturn(userAuths);
+ userAuthFIleDAO = new UserAuthFIleDAO(mockObjectMapper, "doesnt_matter.txt");
+ }
+
+ @Test
+ public void getUserAuthTest() {
+ String key = "123";
+ UserAuth auth = userAuthFIleDAO.getUserAuth(key);
+
+ assertEquals(auth, userAuths[0]);
+ }
+
+ @Test
+ public void addUserAuthTest() throws IOException {
+ UserAuth auth = new UserAuth("999", "Fish", null);
+
+ assertDoesNotThrow(() -> userAuthFIleDAO.addUserAuth(auth));
+ }
+
+ @Test
+ public void removeUserAuthTest() throws IOException {
+ String key = "123";
+
+ assertDoesNotThrow(() -> userAuthFIleDAO.removeUserAuth(key));
+ }
+
+}
diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserFileDAOTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserFileDAOTest.java
index dfe9b10..9361188 100644
--- a/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserFileDAOTest.java
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserFileDAOTest.java
@@ -1,24 +1,22 @@
package com.ufund.api.ufundapi.persistence;
+import java.io.File;
+import java.io.IOException;
+
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import java.io.File;
-import java.io.IOException;
-
import com.fasterxml.jackson.databind.ObjectMapper;
-
import com.ufund.api.ufundapi.model.User;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Tag;
-import org.junit.jupiter.api.Test;
-
@Tag("Persistence-tier")
public class UserFileDAOTest {
UserFileDAO userFileDAO;
@@ -29,9 +27,9 @@ public class UserFileDAOTest {
public void setupHeroFileDAO() throws IOException {
mockObjectMapper = mock(ObjectMapper.class);
testUsers = new User[3];
- testUsers[0] = new User("bob");
- testUsers[1] = new User("admin");
- testUsers[2] = new User("jelly12");
+ testUsers[0] = User.create("bob", "pass");
+ testUsers[1] = User.create("admin", "pass");
+ testUsers[2] = User.create("jelly12", "pass");
// When the object mapper is supposed to read from the file
// the mock object mapper will return the hero array above
@@ -42,7 +40,7 @@ public class UserFileDAOTest {
}
@Test
- public void GetUsersTest() throws IOException {
+ public void getUsersTest() {
User[] users = userFileDAO.getUsers();
assertEquals(users.length,testUsers.length);
@@ -50,16 +48,17 @@ public class UserFileDAOTest {
for (int i = 0; i < testUsers.length;++i) {
boolean isInArray = false;
for (User user : testUsers) {
- if (users[i].getName().equals(user.getName())) {
- isInArray = true;
- }
+ if (users[i].getUsername().equals(user.getUsername())) {
+ isInArray = true;
+ break;
+ }
}
assertTrue(isInArray);
}
}
@Test
- public void FindUsersTest() throws IOException {
+ public void findUsersTest() {
User realUser1 = userFileDAO.getUser("bob");
User realUser2 = userFileDAO.getUser("admin");
@@ -68,25 +67,25 @@ public class UserFileDAOTest {
}
@Test
- public void FindUsersNullTest() throws IOException {
+ public void findUsersNullTest() {
User fakeUser = userFileDAO.getUser("phil.n.thropist");
assertNull(fakeUser);
}
@Test
- public void CreateUserTest() throws IOException {
- User newUser = new User("keshey");
- userFileDAO.createUser(newUser);
+ public void createUserTest() throws IOException {
+ User newUser = User.create("keshey", "pass");
+ userFileDAO.addUser(newUser);
User actualUser = userFileDAO.getUser("keshey");
assertNotNull(actualUser);
- assertEquals(actualUser.getName(), newUser.getName());
+ assertEquals(actualUser.getUsername(), newUser.getUsername());
}
@Test
- public void DeleteUserTest() throws IOException {
+ public void deleteUserTest() throws IOException {
User notDeletedUser = userFileDAO.getUser("jelly12");
assertNotNull(notDeletedUser);
@@ -98,15 +97,15 @@ public class UserFileDAOTest {
}
@Test
- public void UpdateUserTest() throws IOException {
+ public void updateUserTest() throws IOException {
User toBeUpdatedUser = userFileDAO.getUser("admin");
assertNotNull(toBeUpdatedUser);
- User updatedUser = new User("jellinadmin");
+ User updatedUser = User.create("admin", "newPass");
- updatedUser = userFileDAO.updateUser(updatedUser, "admin");
+ updatedUser = userFileDAO.updateUser(updatedUser);
assertNotEquals(toBeUpdatedUser, updatedUser);
- assertEquals("jellinadmin", updatedUser.getName());
+ assertEquals("admin", updatedUser.getUsername());
}
}
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..55cf7a9
--- /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
new file mode 100644
index 0000000..99ca23c
--- /dev/null
+++ b/ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java
@@ -0,0 +1,197 @@
+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;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+import com.ufund.api.ufundapi.DuplicateKeyException;
+import com.ufund.api.ufundapi.model.Need;
+import com.ufund.api.ufundapi.model.Need.GoalType;
+import com.ufund.api.ufundapi.persistence.CupboardDAO;
+
+@Tag("Service-tier")
+public class CupboardServiceTest {
+
+ private CupboardDAO mockCupboardDAO;
+ private CupboardService cupboardService;
+
+ @BeforeEach
+ public void setupCupboardService() {
+ mockCupboardDAO = mock(CupboardDAO.class);
+ cupboardService = new CupboardService(mockCupboardDAO);
+
+ }
+
+ @Test
+ 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);
+
+ // 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
+ assertNotNull(response);
+ 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));
+ }
+
+}