aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsowgro <tpoke.ferrari@gmail.com>2025-03-25 10:17:55 -0400
committersowgro <tpoke.ferrari@gmail.com>2025-03-25 10:17:55 -0400
commit5f03e80712f7a18370b5118fde5327bde1b6fbbf (patch)
tree17ff94c8ba74ab2bfe6195cb6dc6ca06e71f2ea3
parent9aa8a8e80caaad9196752e2e101525a584f35a08 (diff)
downloadJellySolutions-5f03e80712f7a18370b5118fde5327bde1b6fbbf.tar.gz
JellySolutions-5f03e80712f7a18370b5118fde5327bde1b6fbbf.tar.bz2
JellySolutions-5f03e80712f7a18370b5118fde5327bde1b6fbbf.zip
Fix tests and more cleanup
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java3
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java4
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java7
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java11
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java1
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/controller/AuthControllerTest.java7
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/controller/UserControllerTest.java2
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/CupboardFileDAOTest.java5
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java16
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/service/AuthServiceTest.java29
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/service/CupboardServiceTest.java27
-rw-r--r--ufund-api/src/test/java/com/ufund/api/ufundapi/service/UserServiceTest.java13
12 files changed, 56 insertions, 69 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
index d2f3f28..c2d9e06 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
@@ -1,7 +1,6 @@
package com.ufund.api.ufundapi.controller;
import java.io.IOException;
-import java.security.InvalidParameterException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -117,7 +116,7 @@ public class UserController {
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
- } catch (InvalidParameterException ex) {
+ } catch (IllegalArgumentException ex) {
LOG.log(Level.WARNING, ex.getLocalizedMessage());
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} catch (IllegalAccessException ex) {
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java
index d04d8b7..58b62df 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/User.java
@@ -71,8 +71,8 @@ public class User {
return basket.toArray(Integer[]::new);
}
- public boolean removeBasketNeed(Integer needID) {
- return basket.remove(needID);
+ public void removeBasketNeed(Integer needID) {
+ basket.remove(needID);
}
/**
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java
index 3115204..7efda83 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/CupboardFileDAO.java
@@ -55,14 +55,12 @@ public class CupboardFileDAO implements CupboardDAO {
/**
* Saves the needs to json
*
- * @return True if the save was successful, false otherwise
* @throws IOException If there was an IO issue saving the file
*/
- private boolean save() throws IOException {
+ private void save() throws IOException {
Need[] needArray = needs.values().toArray(Need[]::new);
objectMapper.writeValue(new File(filename), needArray);
- return true;
}
@Override
@@ -109,7 +107,8 @@ public class CupboardFileDAO implements CupboardDAO {
synchronized (needs) {
if (needs.containsKey(id)) {
needs.remove(id);
- return save();
+ save();
+ return true;
} else {
return false;
}
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java
index 63d864a..0d9b9e4 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java
@@ -45,12 +45,10 @@ public class UserFileDAO implements UserDAO {
/**
* Saves the needs to json
*
- * @return True if the save was successful, false otherwise
* @throws IOException If there was an IO issue saving the file
*/
- private boolean save() throws IOException {
+ private void save() throws IOException {
objectMapper.writeValue(new File(filename), users.values());
- return true;
}
@Override
@@ -83,9 +81,7 @@ public class UserFileDAO implements UserDAO {
public User updateUser(User user) throws IOException {
synchronized (users) {
if (users.containsKey(user.getUsername())) {
- // var old = users.put(user.getUsername(), user);
- // user.copyPassword(old);
- if (user.getBasket() == null || user.getType() == null) {
+ if (user.getBasket() == null || user.getType() == null) { // TODO clean this up
User oldData = users.get(user.getUsername());
User crutch = new User(oldData.getUsername(), 0, new ArrayList<>(), oldData.getType());
crutch.copyPassword(oldData);
@@ -107,7 +103,8 @@ public class UserFileDAO implements UserDAO {
synchronized (users) {
if (users.containsKey(username)) {
users.remove(username);
- return save();
+ save();
+ return true;
} else {
return false;
}
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java
index 51283fc..6e27f50 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java
@@ -2,7 +2,6 @@ package com.ufund.api.ufundapi.service;
import java.io.IOException;
-import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import com.ufund.api.ufundapi.DuplicateKeyException;
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
index 3d4637d..f4b5980 100644
--- 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
@@ -8,7 +8,6 @@ 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;
@@ -26,7 +25,7 @@ public class AuthControllerTest {
private Map<String, String> authMap;
@BeforeEach
- private void setupAuthController() {
+ public void setupAuthController() {
mockAuthService = mock(AuthService.class);
authController = new AuthController(mockAuthService);
@@ -76,7 +75,7 @@ public class AuthControllerTest {
}
@Test
- public void testLogout() throws IllegalAccessException, IOException {
+ public void testLogout() {
// Setup
String key = "123";
@@ -88,7 +87,7 @@ public class AuthControllerTest {
}
@Test
- public void testLogoutIOException() throws IllegalAccessException, IOException {
+ public void testLogoutIOException() throws IOException {
// Setup
String key = "123";
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 5542f49..cc7df40 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
@@ -244,7 +244,7 @@ public class UserControllerTest {
ResponseEntity<User> response = userController.updateUser(user, username, key);
// Analyze
- assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
+ assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
}
@Test
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 0ebbeca..d83e825 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
@@ -23,11 +23,10 @@ import com.ufund.api.ufundapi.model.Need.GoalType;
public class CupboardFileDAOTest {
private CupboardFileDAO cupboardFileDao;
private Need[] testNeeds;
- private ObjectMapper mockObjectMapper;
- @BeforeEach
+ @BeforeEach
public void setupCupboardFileDao() throws IOException {
- mockObjectMapper = mock(ObjectMapper.class);
+ ObjectMapper mockObjectMapper = mock(ObjectMapper.class);
testNeeds = new Need[] {
new Need("one", 0, 100, Need.GoalType.MONETARY),
new Need("two", 1, 100, Need.GoalType.MONETARY),
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
index f7db747..5e92deb 100644
--- 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
@@ -2,6 +2,7 @@ package com.ufund.api.ufundapi.persistence;
import java.io.File;
import java.io.IOException;
+import java.time.LocalDateTime;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -18,22 +19,21 @@ import com.ufund.api.ufundapi.model.UserAuth;
public class UserAuthFileDAOTest {
private UserAuthFIleDAO userAuthFIleDAO;
- private ObjectMapper mockObjectMapper;
private UserAuth[] userAuths;
@BeforeEach
public void setupUserAuthFileDAO() throws IOException {
- mockObjectMapper = mock(ObjectMapper.class);
+ ObjectMapper mockObjectMapper = mock(ObjectMapper.class);
userAuths = new UserAuth[]{
- new UserAuth("123", "Phil", null),
- new UserAuth("456", "Bob", null),
- new UserAuth("789", "Steve", null)
+ new UserAuth("123", "Phil", LocalDateTime.MAX),
+ new UserAuth("456", "Bob", LocalDateTime.MAX),
+ new UserAuth("789", "Steve", LocalDateTime.MAX)
};
// 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))
+ .readValue(new File("doesnt_matter.txt"),UserAuth[].class))
.thenReturn(userAuths);
userAuthFIleDAO = new UserAuthFIleDAO(mockObjectMapper, "doesnt_matter.txt");
}
@@ -47,14 +47,14 @@ public class UserAuthFileDAOTest {
}
@Test
- public void addUserAuthTest() throws IOException {
+ public void addUserAuthTest() {
UserAuth auth = new UserAuth("999", "Fish", null);
assertDoesNotThrow(() -> userAuthFIleDAO.addUserAuth(auth));
}
@Test
- public void removeUserAuthTest() throws IOException {
+ public void removeUserAuthTest() {
String key = "123";
assertDoesNotThrow(() -> userAuthFIleDAO.removeUserAuth(key));
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..d3085e5 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;
@@ -51,16 +50,16 @@ public class AuthServiceTest {
}
-// @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 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 {
@@ -73,7 +72,7 @@ public class AuthServiceTest {
}
@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 59f5b1b..05ea2e8 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
@@ -23,12 +23,11 @@ public class CupboardServiceTest {
private CupboardDAO mockCupboardDAO;
private CupboardService cupboardService;
- private AuthService mockAuthService;
@BeforeEach
public void setupCupboardService() {
mockCupboardDAO = mock(CupboardDAO.class);
- mockAuthService = mock(AuthService.class);
+ AuthService mockAuthService = mock(AuthService.class);
cupboardService = new CupboardService(mockAuthService, mockCupboardDAO);
}
@@ -54,7 +53,7 @@ public class CupboardServiceTest {
}
@Test
- public void testCreateNeedBadGoal() throws IOException, DuplicateKeyException {
+ public void testCreateNeedBadGoal() throws IOException {
// Setup
String name = "Jellyfish";
double maxGoal = -100.00;
@@ -69,13 +68,12 @@ 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, maxGoal, type));
}
@Test
- public void testCreateNeedDuplicate() throws IOException, DuplicateKeyException {
+ public void testCreateNeedDuplicate() throws IOException {
// Setup
String name = "Jellyfish";
double maxGoal = 100.00;
@@ -91,13 +89,12 @@ 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, maxGoal, type));
}
@Test
- public void testSearchNeeds() throws IOException, DuplicateKeyException {
+ public void testSearchNeeds() throws IOException {
// Setup
String name = "Jellyfish";
double maxGoal = 100.00;
@@ -117,7 +114,7 @@ public class CupboardServiceTest {
}
@Test
- public void testSearchNeedsFail() throws IOException, DuplicateKeyException {
+ public void testSearchNeedsFail() throws IOException {
// Setup
String name = "Jellyfish";
double maxGoal = 100.00;
@@ -136,7 +133,7 @@ public class CupboardServiceTest {
}
@Test
- public void testGetNeed() throws IOException, DuplicateKeyException {
+ public void testGetNeed() throws IOException {
// Setup
String name = "Jellyfish";
double maxGoal = 100.00;
@@ -155,7 +152,7 @@ public class CupboardServiceTest {
}
@Test
- public void testUpdateNeed() throws IOException, DuplicateKeyException {
+ public void testUpdateNeed() throws IOException {
// Setup
String name = "Jellyfish";
double maxGoal = 100.00;
@@ -175,7 +172,7 @@ public class CupboardServiceTest {
}
@Test
- public void testDeleteNeed() throws IOException, DuplicateKeyException {
+ public void testDeleteNeed() throws IOException {
// Setup
String name = "Jellyfish";
double maxGoal = 100.00;
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";