From cb3b7710b9e32df408b3a38383aca049fa98214e Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 24 Mar 2025 21:17:33 -0400 Subject: Fixed various bugs and began fixing auth system. Also started implementing checkout method in cupboardService --- .../ufundapi/persistence/CupboardFileDAOTest.java | 79 ++++++++++++++-------- .../api/ufundapi/persistence/UserFileDAOTest.java | 18 +++++ .../api/ufundapi/service/CupboardServiceTest.java | 4 +- 3 files changed, 71 insertions(+), 30 deletions(-) (limited to 'ufund-api/src/test/java') 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 f786a8c..0ebbeca 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 @@ -4,6 +4,7 @@ import java.io.File; import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -20,44 +21,43 @@ import com.ufund.api.ufundapi.model.Need.GoalType; @Tag("Persistence-tier") 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[]{ - new Need("one", 0, 100, Need.GoalType.MONETARY), - new Need("two", 1, 100, Need.GoalType.MONETARY), - new Need("three", 2, 100, Need.GoalType.MONETARY) + private CupboardFileDAO cupboardFileDao; + private Need[] testNeeds; + private ObjectMapper mockObjectMapper; + + @BeforeEach + public void setupCupboardFileDao() throws IOException { + mockObjectMapper = mock(ObjectMapper.class); + 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); - } - - @Test - public void getNeedsTest() { - Need[] needs = cupboardFileDao.getNeeds(); - assertEquals(needs.length,testNeeds.length); + // 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); + } + + @Test + public void getNeedsTest() { + Need[] needs = cupboardFileDao.getNeeds(); + assertEquals(needs.length, testNeeds.length); assertEquals(needs[0].getName(), testNeeds[0].getName()); - } + } - @Test - public void getNeedTest() { + @Test + public void getNeedTest() { Need need1 = cupboardFileDao.getNeed(0); - + assertEquals(testNeeds[0], need1); - } + } @Test public void createNeedTest() throws IOException { Need newNeed = new Need("sea urchin hats", 3, 100, GoalType.PHYSICAL); - Need actualNeed = cupboardFileDao.addNeed(newNeed); @@ -78,6 +78,15 @@ public class CupboardFileDAOTest { assertNull(deletedNeed); } + @Test + public void deleteNeedTestFail() throws IOException { + Need undeletedNeed = cupboardFileDao.getNeed(0); + assertNotNull(undeletedNeed); + + boolean nullNeed = cupboardFileDao.deleteNeed(20); + assertFalse(nullNeed); + } + @Test public void updateNeedTest() throws IOException { Need[] needs = cupboardFileDao.getNeeds(); @@ -91,4 +100,16 @@ public class CupboardFileDAOTest { assertNotEquals(actualNeed, unupdatedNeed); } + @Test + public void updateNeedTestFail() throws IOException { + Need[] needs = cupboardFileDao.getNeeds(); + Need unupdatedNeed = needs[needs.length - 1]; + assertNotNull(unupdatedNeed); + + Need updatedNeed = new Need("sequin sea urchin hats", 20, 100, GoalType.PHYSICAL); + + Need actualNeed = cupboardFileDao.updateNeed(updatedNeed); + assertNull(actualNeed); + } + } 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 9361188..2ee0fc0 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 @@ -39,6 +39,24 @@ public class UserFileDAOTest { userFileDAO = new UserFileDAO("doesnt_matter.txt",mockObjectMapper); } + @Test + public void addUsersTest() throws IOException { + User user = User.create("Name", "Pass"); + + User addedUser = userFileDAO.addUser(user); + + assertEquals(addedUser, user); + } + + @Test + public void addUsersTestFail() throws IOException { + User user = User.create("bob", "test"); + + User existingUser = userFileDAO.addUser(user); + + assertEquals(existingUser, testUsers[0]); + } + @Test public void getUsersTest() { User[] users = userFileDAO.getUsers(); 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..59f5b1b 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,11 +23,13 @@ public class CupboardServiceTest { private CupboardDAO mockCupboardDAO; private CupboardService cupboardService; + private AuthService mockAuthService; @BeforeEach public void setupCupboardService() { mockCupboardDAO = mock(CupboardDAO.class); - cupboardService = new CupboardService(mockCupboardDAO); + mockAuthService = mock(AuthService.class); + cupboardService = new CupboardService(mockAuthService, mockCupboardDAO); } -- cgit v1.2.3 From d31c1aec7f615646553a227c8e235d4ae2679c68 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Tue, 25 Mar 2025 08:20:31 -0400 Subject: Rename user getNeeds to getBasket --- ufund-api/src/test/java/com/ufund/api/ufundapi/model/UserTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ufund-api/src/test/java') 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 55b7f07..517a7e2 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 @@ -59,7 +59,7 @@ public class UserTest { user.addToBasket(need); - Need getNeed = cupboardService.getNeed(user.getNeeds()[0]); + Need getNeed = cupboardService.getNeed(user.getBasket()[0]); assertEquals(needs[0], getNeed); @@ -80,7 +80,7 @@ public class UserTest { user.removeBasketNeed(need.getId()); user.addToBasket(need2); - Need getNeed = cupboardService.getNeed(user.getNeeds()[0]); + Need getNeed = cupboardService.getNeed(user.getBasket()[0]); assertEquals(need2, getNeed); -- cgit v1.2.3 From 5f03e80712f7a18370b5118fde5327bde1b6fbbf Mon Sep 17 00:00:00 2001 From: sowgro Date: Tue, 25 Mar 2025 10:17:55 -0400 Subject: Fix tests and more cleanup --- .../ufundapi/controller/AuthControllerTest.java | 7 +++--- .../ufundapi/controller/UserControllerTest.java | 2 +- .../ufundapi/persistence/CupboardFileDAOTest.java | 5 ++-- .../ufundapi/persistence/UserAuthFileDAOTest.java | 16 ++++++------ .../api/ufundapi/service/AuthServiceTest.java | 29 +++++++++++----------- .../api/ufundapi/service/CupboardServiceTest.java | 27 +++++++++----------- .../api/ufundapi/service/UserServiceTest.java | 13 +++++----- 7 files changed, 46 insertions(+), 53 deletions(-) (limited to 'ufund-api/src/test/java') 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 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 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"; -- cgit v1.2.3 From ab35efb06b926e8a3aee5cfc8d1fa908aa4a4707 Mon Sep 17 00:00:00 2001 From: sowgro Date: Wed, 26 Mar 2025 18:14:47 -0400 Subject: Fix cupboard access checking and logging --- .../controller/CupboardControllerTest.java | 29 ++++++++++++++-------- .../ufundapi/controller/UserControllerTest.java | 6 ++--- .../api/ufundapi/service/AuthServiceTest.java | 12 ++++----- 3 files changed, 27 insertions(+), 20 deletions(-) (limited to 'ufund-api/src/test/java') 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 6ef6710..89697bf 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 @@ -7,10 +7,11 @@ import static java.util.Map.entry; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.*; + +import com.ufund.api.ufundapi.service.AuthService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import org.springframework.http.HttpStatus; import com.ufund.api.ufundapi.DuplicateKeyException; @@ -21,11 +22,17 @@ import com.ufund.api.ufundapi.service.CupboardService; public class CupboardControllerTest { private CupboardController cupboardController; private CupboardService mockCupboardService; + private final String key = "dummyKey"; @BeforeEach public void setupCupboardDAO() { + AuthService mockAuthService = mock(AuthService.class); mockCupboardService = mock(CupboardService.class); - cupboardController = new CupboardController(mockCupboardService); + cupboardController = new CupboardController(mockCupboardService, mockAuthService); + + try { + doThrow().when(mockAuthService).keyHasAccessToCupboard(key); + } catch (Exception ignored) {} } @Test @@ -43,7 +50,7 @@ public class CupboardControllerTest { entry("type", "MONETARY") ); - var res = cupboardController.createNeed(needMap); + var res = cupboardController.createNeed(needMap, key); assertEquals(HttpStatus.OK, res.getStatusCode()); assertEquals(need, res.getBody()); @@ -58,7 +65,7 @@ public class CupboardControllerTest { entry("maxGoal", -100.0), entry("type", "MONETARY")); - var res = cupboardController.createNeed(needMap); + var res = cupboardController.createNeed(needMap, key); assertEquals(HttpStatus.BAD_REQUEST, res.getStatusCode()); } @@ -72,7 +79,7 @@ public class CupboardControllerTest { entry("maxGoal", 100.0), entry("type", "MONETARY")); - var res = cupboardController.createNeed(needMap); + var res = cupboardController.createNeed(needMap, key); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); } @@ -174,7 +181,7 @@ public class CupboardControllerTest { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); when(mockCupboardService.updateNeed(need, 1)).thenReturn(need); - var res = cupboardController.updateNeed(need, 1); + var res = cupboardController.updateNeed(need, 1, key); assertEquals(HttpStatus.OK, res.getStatusCode()); assertEquals(need, res.getBody()); @@ -185,7 +192,7 @@ public class CupboardControllerTest { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); when(mockCupboardService.updateNeed(need, 1)).thenThrow(new IOException()); - var res = cupboardController.updateNeed(need, 1); + var res = cupboardController.updateNeed(need, 1, key); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); } @@ -196,7 +203,7 @@ public class CupboardControllerTest { when(mockCupboardService.getNeed(1)).thenReturn(need); when(mockCupboardService.deleteNeed(1)).thenReturn(true); - var res = cupboardController.deleteNeed(1); + var res = cupboardController.deleteNeed(1, key); assertEquals(HttpStatus.OK, res.getStatusCode()); } @@ -206,7 +213,7 @@ public class CupboardControllerTest { when(mockCupboardService.getNeed(1)).thenReturn(null); when(mockCupboardService.deleteNeed(1)).thenReturn(false); - var res = cupboardController.deleteNeed(1); + var res = cupboardController.deleteNeed(1, key); assertEquals(HttpStatus.NOT_FOUND, res.getStatusCode()); } @@ -217,7 +224,7 @@ public class CupboardControllerTest { when(mockCupboardService.getNeed(1)).thenReturn(need); when(mockCupboardService.deleteNeed(1)).thenThrow(new IOException()); - var res = cupboardController.deleteNeed(1); + var res = cupboardController.deleteNeed(1, key); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); } 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 cc7df40..06fb6cd 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 @@ -82,7 +82,7 @@ public class UserControllerTest { 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); + doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToUser(username, key); // Invoke ResponseEntity response = userController.getUser(username, key); @@ -237,7 +237,7 @@ public class UserControllerTest { 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); + doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToUser(username, key); // Invoke @@ -298,7 +298,7 @@ public class UserControllerTest { 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); + doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToUser(username, key); // Invoke ResponseEntity response = userController.deleteUser(username, 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 d3085e5..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 @@ -40,34 +40,34 @@ 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 { + 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.authenticate(username, key)); + 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)); } -- cgit v1.2.3 From 350a120eb0a578aa468b903a83f47168d6b8db13 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Wed, 26 Mar 2025 19:20:26 -0400 Subject: Fixed expiration and authDAO test --- .../java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ufund-api/src/test/java') 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 5e92deb..a4842c5 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 @@ -43,7 +43,7 @@ public class UserAuthFileDAOTest { String key = "123"; UserAuth auth = userAuthFIleDAO.getUserAuth(key); - assertEquals(auth, userAuths[0]); + assertEquals(userAuths[0], auth); } @Test -- cgit v1.2.3 From 5c49027cbccf913130d864db127f3ebf9dea9d15 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Sun, 30 Mar 2025 15:50:56 -0400 Subject: Added additional tests to bring coverage up to 100# on controllers --- .../controller/CupboardControllerTest.java | 137 ++++++++++++++++++++- 1 file changed, 134 insertions(+), 3 deletions(-) (limited to 'ufund-api/src/test/java') 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 89697bf..d775d14 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 @@ -23,10 +23,11 @@ public class CupboardControllerTest { private CupboardController cupboardController; private CupboardService mockCupboardService; private final String key = "dummyKey"; + private AuthService mockAuthService; @BeforeEach public void setupCupboardDAO() { - AuthService mockAuthService = mock(AuthService.class); + mockAuthService = mock(AuthService.class); mockCupboardService = mock(CupboardService.class); cupboardController = new CupboardController(mockCupboardService, mockAuthService); @@ -63,7 +64,8 @@ public class CupboardControllerTest { Map needMap = Map.ofEntries( entry("name", "Name"), entry("maxGoal", -100.0), - entry("type", "MONETARY")); + entry("type", "MONETARY") + ); var res = cupboardController.createNeed(needMap, key); @@ -77,13 +79,44 @@ public class CupboardControllerTest { Map needMap = Map.ofEntries( entry("name", "Name"), entry("maxGoal", 100.0), - entry("type", "MONETARY")); + entry("type", "MONETARY") + ); var res = cupboardController.createNeed(needMap, key); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); } + @Test + public void createNeedConflict() throws IOException, DuplicateKeyException { + when(mockCupboardService.createNeed("Name", 100, Need.GoalType.MONETARY)).thenThrow(new DuplicateKeyException("")); + + Map needMap = Map.ofEntries( + entry("name", "Name"), + entry("maxGoal", 100.0), + entry("type", "MONETARY") + ); + + var res = cupboardController.createNeed(needMap, key); + + assertEquals(HttpStatus.CONFLICT, res.getStatusCode()); + } + + @Test + public void createNeedUnauthorized() throws IOException, IllegalAccessException { + doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToCupboard(key); + + Map needMap = Map.ofEntries( + entry("name", "Name"), + entry("maxGoal", 100.0), + entry("type", "MONETARY") + ); + + var res = cupboardController.createNeed(needMap, key); + + assertEquals(HttpStatus.UNAUTHORIZED, res.getStatusCode()); + } + @Test public void getNeeds() throws IOException { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); @@ -197,6 +230,36 @@ public class CupboardControllerTest { assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); } + @Test + public void updateNeedMissing() throws IOException { + var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + when(mockCupboardService.updateNeed(need, 1)).thenReturn(null); + + var res = cupboardController.updateNeed(need, 1, key); + + assertEquals(HttpStatus.NOT_FOUND, res.getStatusCode()); + } + + @Test + public void updateNeedBadRequest() throws IOException { + var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + when(mockCupboardService.updateNeed(need, 1)).thenThrow(new IllegalArgumentException()); + + var res = cupboardController.updateNeed(need, 1, key); + + assertEquals(HttpStatus.BAD_REQUEST, res.getStatusCode()); + } + + @Test + public void updateNeedUnauthorized() throws IOException, IllegalAccessException { + var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToCupboard(key); + + var res = cupboardController.updateNeed(need, 1, key); + + assertEquals(HttpStatus.UNAUTHORIZED, res.getStatusCode()); + } + @Test public void deleteNeed() throws IOException { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); @@ -218,6 +281,17 @@ public class CupboardControllerTest { assertEquals(HttpStatus.NOT_FOUND, res.getStatusCode()); } + @Test + public void deleteNeedUnauthorized() throws IOException, IllegalAccessException { + var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + when(mockCupboardService.getNeed(1)).thenReturn(need); + doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToCupboard(key); + + var res = cupboardController.deleteNeed(1, key); + + assertEquals(HttpStatus.UNAUTHORIZED, res.getStatusCode()); + } + @Test public void deleteNeedIOException() throws IOException { var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); @@ -228,4 +302,61 @@ public class CupboardControllerTest { assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); } + + @Test + public void checkoutNeeds() throws IOException, IllegalAccessException { + doNothing().when(mockCupboardService).checkoutNeed(0, 20, key); + + + Map needMap = Map.ofEntries( + entry("needID", 0), + entry("amount", 20) + ); + + var res = cupboardController.checkoutNeeds(needMap, key); + + assertEquals(HttpStatus.OK, res.getStatusCode()); + } + + @Test + public void checkoutNeedsBadRequest() throws IOException, IllegalAccessException { + doThrow(new IllegalArgumentException()).when(mockCupboardService).checkoutNeed(0, 20, key); + + Map needMap = Map.ofEntries( + entry("needID", 0), + entry("amount", 20) + ); + + var res = cupboardController.checkoutNeeds(needMap, key); + + assertEquals(HttpStatus.BAD_REQUEST, res.getStatusCode()); + } + + @Test + public void checkoutNeedsUnauthorized() throws IOException, IllegalAccessException { + doThrow(new IllegalAccessException()).when(mockCupboardService).checkoutNeed(0, 20, key); + + Map needMap = Map.ofEntries( + entry("needID", 0), + entry("amount", 20) + ); + + var res = cupboardController.checkoutNeeds(needMap, key); + + assertEquals(HttpStatus.UNAUTHORIZED, res.getStatusCode()); + } + + @Test + public void checkoutNeedsInternalError() throws IOException, IllegalAccessException { + doThrow(new IOException()).when(mockCupboardService).checkoutNeed(0, 20, key); + + Map needMap = Map.ofEntries( + entry("needID", 0), + entry("amount", 20) + ); + + var res = cupboardController.checkoutNeeds(needMap, key); + + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); + } } -- cgit v1.2.3 From 1abdb1408159796d12ed28c73862dd8d186384f0 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Sun, 30 Mar 2025 16:41:24 -0400 Subject: Fixed broken tests and made them more consistent --- .../controller/CupboardControllerTest.java | 95 +++++++++++++++++----- .../com/ufund/api/ufundapi/model/NeedTest.java | 51 +++++++----- .../com/ufund/api/ufundapi/model/UserTest.java | 7 +- .../ufundapi/persistence/CupboardFileDAOTest.java | 16 ++-- .../api/ufundapi/service/CupboardServiceTest.java | 85 +++++++++++-------- 5 files changed, 165 insertions(+), 89 deletions(-) (limited to 'ufund-api/src/test/java') 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 89697bf..31e085b 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 @@ -38,16 +38,20 @@ public class CupboardControllerTest { @Test public void createNeed() throws IOException, DuplicateKeyException { String name = "Test"; + String location = "Atlantis"; int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; - var need = new Need(name, type, maxGoal); - when(mockCupboardService.createNeed(name, maxGoal, type)).thenReturn(need); + boolean urgent = false; + var need = new Need(name, location, maxGoal, type, urgent); + when(mockCupboardService.createNeed(name, "Atlantis", maxGoal, type, false)).thenReturn(need); Map needMap = Map.ofEntries( entry("name", "Test"), + entry("location", "Atlantis"), entry("maxGoal", 100.0), - entry("type", "MONETARY") + entry("type", "MONETARY"), + entry("urgent", false) ); var res = cupboardController.createNeed(needMap, key); @@ -58,12 +62,15 @@ public class CupboardControllerTest { @Test public void createNeedBadMaxGoal() throws IOException, DuplicateKeyException { - when(mockCupboardService.createNeed("Name", -100, Need.GoalType.MONETARY)).thenThrow(new IllegalArgumentException()); + when(mockCupboardService.createNeed("Test", "Atlantis", -100, Need.GoalType.MONETARY, false)).thenThrow(new IllegalArgumentException()); Map needMap = Map.ofEntries( - entry("name", "Name"), - entry("maxGoal", -100.0), - entry("type", "MONETARY")); + entry("name", "Test"), + entry("location", "Atlantis"), + entry("maxGoal", -100), + entry("type", "MONETARY"), + entry("urgent", false) + ); var res = cupboardController.createNeed(needMap, key); @@ -72,12 +79,15 @@ public class CupboardControllerTest { @Test public void createNeedIOException() throws IOException, DuplicateKeyException { - when(mockCupboardService.createNeed("Name", 100, Need.GoalType.MONETARY)).thenThrow(new IOException()); + when(mockCupboardService.createNeed("Test", "Atlantis", 100, Need.GoalType.MONETARY, false)).thenThrow(new IOException()); Map needMap = Map.ofEntries( - entry("name", "Name"), - entry("maxGoal", 100.0), - entry("type", "MONETARY")); + entry("name", "Test"), + entry("location", "Atlantis"), + entry("maxGoal", 100), + entry("type", "MONETARY"), + entry("urgent", false) + ); var res = cupboardController.createNeed(needMap, key); @@ -86,7 +96,12 @@ public class CupboardControllerTest { @Test public void getNeeds() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + var need = new Need(name, location, maxGoal, type, urgent); when(mockCupboardService.getNeeds()).thenReturn(new Need[]{need}); var res = cupboardController.getNeeds(); @@ -116,7 +131,12 @@ public class CupboardControllerTest { @Test public void searchNeeds() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + var need = new Need(name, location, maxGoal, type, urgent); when(mockCupboardService.searchNeeds("Na")).thenReturn(new Need[]{need}); var res = cupboardController.searchNeeds("Na"); @@ -146,7 +166,12 @@ public class CupboardControllerTest { @Test public void getNeed() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + var need = new Need(name, location, maxGoal, type, urgent); when(mockCupboardService.getNeed(need.getId())).thenReturn(need); var res = cupboardController.getNeed(need.getId()); @@ -157,7 +182,12 @@ public class CupboardControllerTest { @Test public void getNeedIOException() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + var need = new Need(name, location, maxGoal, type, urgent); when(mockCupboardService.getNeed(need.getId())).thenThrow(new IOException()); var res = cupboardController.getNeed(need.getId()); @@ -167,7 +197,12 @@ public class CupboardControllerTest { @Test public void getNeedFail() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + var need = new Need(name, location, maxGoal, type, urgent); when(mockCupboardService.getNeed(need.getId())).thenReturn(null); var res = cupboardController.getNeed(need.getId()); @@ -178,7 +213,12 @@ public class CupboardControllerTest { @Test public void updateNeeds() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + var need = new Need(name, location, maxGoal, type, urgent); when(mockCupboardService.updateNeed(need, 1)).thenReturn(need); var res = cupboardController.updateNeed(need, 1, key); @@ -189,7 +229,12 @@ public class CupboardControllerTest { @Test public void updateNeedsIOException() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + var need = new Need(name, location, maxGoal, type, urgent); when(mockCupboardService.updateNeed(need, 1)).thenThrow(new IOException()); var res = cupboardController.updateNeed(need, 1, key); @@ -199,7 +244,12 @@ public class CupboardControllerTest { @Test public void deleteNeed() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + var need = new Need(name, location, maxGoal, type, urgent); when(mockCupboardService.getNeed(1)).thenReturn(need); when(mockCupboardService.deleteNeed(1)).thenReturn(true); @@ -220,7 +270,12 @@ public class CupboardControllerTest { @Test public void deleteNeedIOException() throws IOException { - var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); + String name = "Test"; + String location = "Atlantis"; + int maxGoal = 100; + GoalType type = Need.GoalType.MONETARY; + boolean urgent = false; + var need = new Need(name, location, maxGoal, type, urgent); when(mockCupboardService.getNeed(1)).thenReturn(need); when(mockCupboardService.deleteNeed(1)).thenThrow(new IOException()); 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 6b4ddfc..c7d17c7 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,23 +14,26 @@ public class NeedTest { public void createNeed() { 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; + var need = new Need(name, location, maxGoal, type, urgent); assertNotNull(need); } @Test public void testFields() { String name = "Jellyfish"; - int id = 0; - 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; + var need = new Need(name, location, maxGoal, type, urgent); assertEquals(name, need.getName()); - assertEquals(id, need.getId()); + assertEquals(0, need.getId()); assertEquals(maxGoal, need.getMaxGoal()); assertEquals(type, need.getType()); } @@ -38,9 +41,11 @@ public class NeedTest { @Test public void testCurrentGoal() { 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; + var need = new Need(name, location, maxGoal, type, urgent); double current = 0.00; need.setCurrent(current); @@ -60,9 +65,11 @@ public class NeedTest { public void testFilterAttributes() { 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; + var need = new Need(name, location, maxGoal, type, urgent); String[] filterAttributes = {"seaweed", "divers", "pacific", "plankton"}; @@ -75,9 +82,11 @@ public class NeedTest { public void testSetMaxGoal() { 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; + var need = new Need(name, location, maxGoal, type, urgent); double newGoal = 200.00; need.setMaxGoal(newGoal); @@ -90,9 +99,11 @@ public class NeedTest { public void testSetName() { 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; + var need = new Need(name, location, maxGoal, type, urgent); 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 517a7e2..01b558c 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 @@ -52,7 +52,7 @@ public class UserTest { String expectedName = "Bob"; User user = User.create(expectedName, "pass"); - Need need = new Need("Test", 0, 100, Need.GoalType.MONETARY); + Need need = new Need("Test", "Atlantis", 0, 100, Need.GoalType.MONETARY, false); Need[] needs = { need }; when(cupboardService.getNeed(0)).thenReturn(need); @@ -71,9 +71,8 @@ public class UserTest { String expectedName = "Bob"; 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); - + Need need = new Need("Test", "Atlantis", 0, 100, Need.GoalType.MONETARY, false); + Need need2 = new Need("Test2", "Atlantis", 0, 100, Need.GoalType.MONETARY, false); when(cupboardService.getNeed(0)).thenReturn(need2); user.addToBasket(need); 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 d83e825..acb759a 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 @@ -28,15 +28,13 @@ public class CupboardFileDAOTest { public void setupCupboardFileDao() throws IOException { ObjectMapper mockObjectMapper = mock(ObjectMapper.class); 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) + new Need("one", "Atlantis", 0, 100, Need.GoalType.MONETARY, false), + new Need("two", "Atlantis", 1, 100, Need.GoalType.MONETARY, false), + new Need("three", "Atlantis", 2, 100, Need.GoalType.MONETARY, false) }; // 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); + when(mockObjectMapper.readValue(new File("doesnt_matter.txt"), Need[].class)).thenReturn(testNeeds); cupboardFileDao = new CupboardFileDAO("doesnt_matter.txt", mockObjectMapper); } @@ -56,7 +54,7 @@ public class CupboardFileDAOTest { @Test public void createNeedTest() throws IOException { - Need newNeed = new Need("sea urchin hats", 3, 100, GoalType.PHYSICAL); + Need newNeed = new Need("sea urchin hats", "Atlantis", 100, GoalType.PHYSICAL, false); Need actualNeed = cupboardFileDao.addNeed(newNeed); @@ -92,7 +90,7 @@ public class CupboardFileDAOTest { Need unupdatedNeed = needs[needs.length - 1]; assertNotNull(unupdatedNeed); - Need updatedNeed = new Need("sequin sea urchin hats", 2, 100, GoalType.PHYSICAL); + Need updatedNeed = new Need("sequin sea urchin hats", "Atlantis", 100, GoalType.PHYSICAL, false); Need actualNeed = cupboardFileDao.updateNeed(updatedNeed); assertEquals(actualNeed, updatedNeed); @@ -105,7 +103,7 @@ public class CupboardFileDAOTest { Need unupdatedNeed = needs[needs.length - 1]; assertNotNull(unupdatedNeed); - Need updatedNeed = new Need("sequin sea urchin hats", 20, 100, GoalType.PHYSICAL); + Need updatedNeed = new Need("sequin sea urchin hats", "Atlantis", 5, 100, GoalType.PHYSICAL, false); Need actualNeed = cupboardFileDao.updateNeed(updatedNeed); assertNull(actualNeed); 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 05ea2e8..f3cbc23 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 @@ -36,16 +36,18 @@ 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; + var need = new Need(name, location, maxGoal, type, urgent); // 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, maxGoal, type, urgent); // Analyze assertNotNull(response); @@ -56,9 +58,11 @@ public class CupboardServiceTest { 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; + var need = new Need(name, location, maxGoal, type, urgent); // When the same id is passed in, our mock User DAO will return the User object when(mockCupboardDAO.addNeed(any())).thenReturn(need); @@ -69,16 +73,18 @@ public class CupboardServiceTest { // Analyze assertThrows(IllegalArgumentException.class, () -> - cupboardService.createNeed(name, maxGoal, type)); + cupboardService.createNeed(name, location, maxGoal, type, urgent)); } @Test 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; + var need = new Need(name, location, maxGoal, type, urgent); Need[] needs = { need }; // When the same id is passed in, our mock User DAO will return the User object @@ -90,16 +96,18 @@ public class CupboardServiceTest { // Analyze assertThrows(DuplicateKeyException.class, () -> - cupboardService.createNeed(name, maxGoal, type)); + cupboardService.createNeed(name, location, maxGoal, type, urgent)); } @Test 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; + var need = new Need(name, location, maxGoal, type, urgent); Need[] needs = { need }; // When the same id is passed in, our mock User DAO will return the User object @@ -117,9 +125,11 @@ public class CupboardServiceTest { 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; + var need = new Need(name, location, maxGoal, type, urgent); Need[] needs = { need }; // When the same id is passed in, our mock User DAO will return the User object @@ -136,16 +146,17 @@ public class CupboardServiceTest { 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; + var need = new Need(name, location, maxGoal, type, urgent); // 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); @@ -155,17 +166,18 @@ public class CupboardServiceTest { 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; + Need need = new Need(name, location, maxGoal, type, urgent); + Need newNeed = new Need("Octopus", location, maxGoal, type, urgent); // 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); @@ -175,17 +187,18 @@ public class CupboardServiceTest { 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; + Need need = new Need(name, location, maxGoal, type, urgent); // 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 -- cgit v1.2.3 From fd3fcd8f0e13e32774c020aee1b46e91d9b96c50 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Sun, 30 Mar 2025 18:52:27 -0400 Subject: Fixed 2 broken tests in CupboardControllerTest --- .../ufundapi/controller/CupboardControllerTest.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'ufund-api/src/test/java') 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 4702771..75dbf84 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 @@ -97,12 +97,14 @@ public class CupboardControllerTest { @Test public void createNeedConflict() throws IOException, DuplicateKeyException { - when(mockCupboardService.createNeed("Name", "Atlantis", 100, Need.GoalType.MONETARY, false)).thenThrow(new DuplicateKeyException("")); + when(mockCupboardService.createNeed("Test", "Atlantis", 100, Need.GoalType.MONETARY, false)).thenThrow(new DuplicateKeyException("")); Map needMap = Map.ofEntries( - entry("name", "Name"), - entry("maxGoal", 100.0), - entry("type", "MONETARY") + entry("name", "Test"), + entry("location", "Atlantis"), + entry("maxGoal", 100), + entry("type", "MONETARY"), + entry("urgent", false) ); var res = cupboardController.createNeed(needMap, key); @@ -115,9 +117,11 @@ public class CupboardControllerTest { doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToCupboard(key); Map needMap = Map.ofEntries( - entry("name", "Name"), - entry("maxGoal", 100.0), - entry("type", "MONETARY") + entry("name", "Test"), + entry("location", "Atlantis"), + entry("maxGoal", 100), + entry("type", "MONETARY"), + entry("urgent", false) ); var res = cupboardController.createNeed(needMap, key); -- cgit v1.2.3 From 197be103d02db808b0e6bf8a1d1369e3d7928c03 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Sun, 30 Mar 2025 20:48:22 -0400 Subject: Modified controllers to return error text when catching errors. Also added additional error checking to CupboardService for physical needs. --- .../controller/CupboardControllerTest.java | 8 ++--- .../ufundapi/controller/UserControllerTest.java | 34 +++++++++++----------- 2 files changed, 21 insertions(+), 21 deletions(-) (limited to 'ufund-api/src/test/java') 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 75dbf84..c159db4 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 @@ -142,7 +142,7 @@ public class CupboardControllerTest { var res = cupboardController.getNeeds(); assertEquals(HttpStatus.OK, res.getStatusCode()); - assertArrayEquals(new Need[]{need}, res.getBody()); + assertArrayEquals(new Need[]{need}, (Need[]) res.getBody()); } @Test @@ -161,7 +161,7 @@ public class CupboardControllerTest { var res = cupboardController.getNeeds(); assertEquals(HttpStatus.OK, res.getStatusCode()); - assertArrayEquals(new Need[]{}, res.getBody()); + assertArrayEquals(new Need[]{}, (Need[]) res.getBody()); } @Test @@ -177,7 +177,7 @@ public class CupboardControllerTest { var res = cupboardController.searchNeeds("Na"); assertEquals(HttpStatus.OK, res.getStatusCode()); - assertArrayEquals(new Need[]{need}, res.getBody()); + assertArrayEquals(new Need[]{need}, (Need[]) res.getBody()); } @Test @@ -196,7 +196,7 @@ public class CupboardControllerTest { var res = cupboardController.searchNeeds("Na"); assertEquals(HttpStatus.OK, res.getStatusCode()); - assertArrayEquals(new Need[]{}, res.getBody()); + assertArrayEquals(new Need[]{}, (Need[]) res.getBody()); } @Test 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 06fb6cd..5870a93 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 @@ -50,12 +50,12 @@ public class UserControllerTest { // Invoke - ResponseEntity response = userController.getUser(username, key); + ResponseEntity response = userController.getUser(username, key); // Analyze assertEquals(HttpStatus.OK, response.getStatusCode()); assertNotNull(response.getBody()); - assertEquals(user.getUsername(), response.getBody().getUsername()); + assertEquals(user.getUsername(), ((User) response.getBody()).getUsername()); } @Test @@ -69,7 +69,7 @@ public class UserControllerTest { // Invoke - ResponseEntity response = userController.getUser(username, key); + ResponseEntity response = userController.getUser(username, key); // Analyze assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); @@ -85,7 +85,7 @@ public class UserControllerTest { doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToUser(username, key); // Invoke - ResponseEntity response = userController.getUser(username, key); + ResponseEntity response = userController.getUser(username, key); // Analyze assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); @@ -100,7 +100,7 @@ public class UserControllerTest { doThrow(new IOException()).when(mockUserService).getUser(username); // Invoke - ResponseEntity response = userController.getUser(username, key); + ResponseEntity response = userController.getUser(username, key); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); @@ -119,7 +119,7 @@ public class UserControllerTest { // Invoke - ResponseEntity response = userController.createUser(userMap); + ResponseEntity response = userController.createUser(userMap); // Analyze assertEquals(HttpStatus.CREATED, response.getStatusCode()); @@ -138,7 +138,7 @@ public class UserControllerTest { // Invoke - ResponseEntity response = userController.createUser(userMap); + ResponseEntity response = userController.createUser(userMap); // Analyze assertEquals(HttpStatus.CONFLICT, response.getStatusCode()); @@ -154,7 +154,7 @@ public class UserControllerTest { when(mockUserService.createUser(username, password)).thenThrow(DuplicateKeyException.class); // Invoke - ResponseEntity response = userController.createUser(userMap); + ResponseEntity response = userController.createUser(userMap); // Analyze assertEquals(HttpStatus.CONFLICT, response.getStatusCode()); @@ -172,7 +172,7 @@ public class UserControllerTest { // Invoke - ResponseEntity response = userController.createUser(userMap); + ResponseEntity response = userController.createUser(userMap); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); @@ -189,7 +189,7 @@ public class UserControllerTest { when(mockUserService.updateUser(user, username)).thenReturn(user); // Invoke - ResponseEntity response = userController.updateUser(user, username, key); + ResponseEntity response = userController.updateUser(user, username, key); // Analyze assertEquals(HttpStatus.OK, response.getStatusCode()); @@ -207,7 +207,7 @@ public class UserControllerTest { when(mockUserService.updateUser(user, username)).thenReturn(null); // Invoke - ResponseEntity response = userController.updateUser(user, username, key); + ResponseEntity response = userController.updateUser(user, username, key); // Analyze assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); @@ -223,7 +223,7 @@ public class UserControllerTest { doThrow(new IOException()).when(mockUserService).updateUser(user, username); // Invoke - ResponseEntity response = userController.updateUser(user, username, key); + ResponseEntity response = userController.updateUser(user, username, key); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); @@ -241,7 +241,7 @@ public class UserControllerTest { // Invoke - ResponseEntity response = userController.updateUser(user, username, key); + ResponseEntity response = userController.updateUser(user, username, key); // Analyze assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); @@ -256,7 +256,7 @@ public class UserControllerTest { when(mockUserService.deleteUser(username)).thenReturn(true); // Invoke - ResponseEntity response = userController.deleteUser(username, key); + ResponseEntity response = userController.deleteUser(username, key); // Analyze assertEquals(HttpStatus.OK, response.getStatusCode()); @@ -271,7 +271,7 @@ public class UserControllerTest { when(mockUserService.deleteUser(username)).thenReturn(false); // Invoke - ResponseEntity response = userController.deleteUser(username, key); + ResponseEntity response = userController.deleteUser(username, key); // Analyze assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); @@ -286,7 +286,7 @@ public class UserControllerTest { doThrow(new IOException()).when(mockUserService).deleteUser(username); // Invoke - ResponseEntity response = userController.deleteUser(username, key); + ResponseEntity response = userController.deleteUser(username, key); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); @@ -301,7 +301,7 @@ public class UserControllerTest { doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToUser(username, key); // Invoke - ResponseEntity response = userController.deleteUser(username, key); + ResponseEntity response = userController.deleteUser(username, key); // Analyze assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); -- cgit v1.2.3 From cd8b846a4455aba7664cc03e219f471d251ff9bf Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Mon, 31 Mar 2025 20:44:22 -0400 Subject: Fixed broken tests due to new need fields --- .../controller/CupboardControllerTest.java | 82 ++++++++++++++++------ .../com/ufund/api/ufundapi/model/NeedTest.java | 24 +++++-- .../com/ufund/api/ufundapi/model/UserTest.java | 6 +- .../ufundapi/persistence/CupboardFileDAOTest.java | 12 ++-- .../api/ufundapi/service/CupboardServiceTest.java | 40 +++++++---- 5 files changed, 114 insertions(+), 50 deletions(-) (limited to 'ufund-api/src/test/java') 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 75dbf84..15353bf 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 @@ -43,16 +43,20 @@ public class CupboardControllerTest { int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); - when(mockCupboardService.createNeed(name, "Atlantis", maxGoal, type, false)).thenReturn(need); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); + when(mockCupboardService.createNeed(name, image, location, maxGoal, type, urgent, description)).thenReturn(need); Map needMap = Map.ofEntries( entry("name", "Test"), + entry("image", ""), entry("location", "Atlantis"), - entry("maxGoal", 100.0), + entry("maxGoal", 100), entry("type", "MONETARY"), - entry("urgent", false) + entry("urgent", false), + entry("description", "") ); var res = cupboardController.createNeed(needMap, key); @@ -63,14 +67,16 @@ public class CupboardControllerTest { @Test public void createNeedBadMaxGoal() throws IOException, DuplicateKeyException { - when(mockCupboardService.createNeed("Test", "Atlantis", -100, Need.GoalType.MONETARY, false)).thenThrow(new IllegalArgumentException()); + when(mockCupboardService.createNeed("Test", "", "Atlantis", -100, Need.GoalType.MONETARY, false, "")).thenThrow(new IllegalArgumentException()); Map needMap = Map.ofEntries( entry("name", "Test"), + entry("image", ""), entry("location", "Atlantis"), entry("maxGoal", -100), entry("type", "MONETARY"), - entry("urgent", false) + entry("urgent", false), + entry("description", "") ); var res = cupboardController.createNeed(needMap, key); @@ -80,14 +86,16 @@ public class CupboardControllerTest { @Test public void createNeedIOException() throws IOException, DuplicateKeyException { - when(mockCupboardService.createNeed("Test", "Atlantis", 100, Need.GoalType.MONETARY, false)).thenThrow(new IOException()); + when(mockCupboardService.createNeed("Test", "", "Atlantis", 100, Need.GoalType.MONETARY, false, "")).thenThrow(new IOException()); Map needMap = Map.ofEntries( entry("name", "Test"), + entry("image", ""), entry("location", "Atlantis"), entry("maxGoal", 100), entry("type", "MONETARY"), - entry("urgent", false) + entry("urgent", false), + entry("description", "") ); var res = cupboardController.createNeed(needMap, key); @@ -97,14 +105,16 @@ public class CupboardControllerTest { @Test public void createNeedConflict() throws IOException, DuplicateKeyException { - when(mockCupboardService.createNeed("Test", "Atlantis", 100, Need.GoalType.MONETARY, false)).thenThrow(new DuplicateKeyException("")); + when(mockCupboardService.createNeed("Test", "", "Atlantis", 100, Need.GoalType.MONETARY, false, "")).thenThrow(new DuplicateKeyException("")); Map needMap = Map.ofEntries( entry("name", "Test"), + entry("image", ""), entry("location", "Atlantis"), entry("maxGoal", 100), entry("type", "MONETARY"), - entry("urgent", false) + entry("urgent", false), + entry("description", "") ); var res = cupboardController.createNeed(needMap, key); @@ -136,7 +146,9 @@ public class CupboardControllerTest { int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.getNeeds()).thenReturn(new Need[]{need}); var res = cupboardController.getNeeds(); @@ -171,7 +183,9 @@ public class CupboardControllerTest { int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.searchNeeds("Na")).thenReturn(new Need[]{need}); var res = cupboardController.searchNeeds("Na"); @@ -206,7 +220,9 @@ public class CupboardControllerTest { int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.getNeed(need.getId())).thenReturn(need); var res = cupboardController.getNeed(need.getId()); @@ -222,7 +238,9 @@ public class CupboardControllerTest { int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.getNeed(need.getId())).thenThrow(new IOException()); var res = cupboardController.getNeed(need.getId()); @@ -237,7 +255,9 @@ public class CupboardControllerTest { int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.getNeed(need.getId())).thenReturn(null); var res = cupboardController.getNeed(need.getId()); @@ -253,7 +273,9 @@ public class CupboardControllerTest { int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.updateNeed(need, 1)).thenReturn(need); var res = cupboardController.updateNeed(need, 1, key); @@ -269,7 +291,9 @@ public class CupboardControllerTest { int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.updateNeed(need, 1)).thenThrow(new IOException()); var res = cupboardController.updateNeed(need, 1, key); @@ -284,7 +308,9 @@ public class CupboardControllerTest { int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.updateNeed(need, 1)).thenReturn(null); var res = cupboardController.updateNeed(need, 1, key); @@ -299,7 +325,9 @@ public class CupboardControllerTest { int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.updateNeed(need, 1)).thenThrow(new IllegalArgumentException()); var res = cupboardController.updateNeed(need, 1, key); @@ -314,7 +342,9 @@ public class CupboardControllerTest { int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToCupboard(key); var res = cupboardController.updateNeed(need, 1, key); @@ -329,7 +359,9 @@ public class CupboardControllerTest { int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.getNeed(1)).thenReturn(need); when(mockCupboardService.deleteNeed(1)).thenReturn(true); @@ -355,7 +387,9 @@ public class CupboardControllerTest { int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.getNeed(1)).thenReturn(need); doThrow(new IllegalAccessException()).when(mockAuthService).keyHasAccessToCupboard(key); @@ -371,7 +405,9 @@ public class CupboardControllerTest { int maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); when(mockCupboardService.getNeed(1)).thenReturn(need); when(mockCupboardService.deleteNeed(1)).thenThrow(new IOException()); 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 c7d17c7..67d2b8f 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 @@ -18,7 +18,9 @@ public class NeedTest { double maxGoal = 100.0; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); assertNotNull(need); } @@ -29,7 +31,9 @@ public class NeedTest { double maxGoal = 100.0; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); assertEquals(name, need.getName()); @@ -45,7 +49,9 @@ public class NeedTest { double maxGoal = 100.0; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); double current = 0.00; need.setCurrent(current); @@ -69,7 +75,9 @@ public class NeedTest { double maxGoal = 100.0; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); String[] filterAttributes = {"seaweed", "divers", "pacific", "plankton"}; @@ -86,7 +94,9 @@ public class NeedTest { double maxGoal = 100.0; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); double newGoal = 200.00; need.setMaxGoal(newGoal); @@ -103,7 +113,9 @@ public class NeedTest { double maxGoal = 100.0; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + String image = ""; + String description = ""; + var need = new Need(name, location, image, maxGoal, type, urgent, description); 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 01b558c..ed48f54 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 @@ -52,7 +52,7 @@ public class UserTest { String expectedName = "Bob"; User user = User.create(expectedName, "pass"); - Need need = new Need("Test", "Atlantis", 0, 100, Need.GoalType.MONETARY, false); + Need need = new Need("Test", "", "Atlantis", 0, 100, Need.GoalType.MONETARY, false, ""); Need[] needs = { need }; when(cupboardService.getNeed(0)).thenReturn(need); @@ -71,8 +71,8 @@ public class UserTest { String expectedName = "Bob"; User user = User.create(expectedName, "pass"); - Need need = new Need("Test", "Atlantis", 0, 100, Need.GoalType.MONETARY, false); - Need need2 = new Need("Test2", "Atlantis", 0, 100, Need.GoalType.MONETARY, false); + Need need = new Need("Test", "", "Atlantis", 0, 100, Need.GoalType.MONETARY, false, ""); + Need need2 = new Need("Test2", "", "Atlantis", 0, 100, Need.GoalType.MONETARY, false, ""); when(cupboardService.getNeed(0)).thenReturn(need2); user.addToBasket(need); 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 acb759a..76a8b40 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 @@ -28,9 +28,9 @@ public class CupboardFileDAOTest { public void setupCupboardFileDao() throws IOException { ObjectMapper mockObjectMapper = mock(ObjectMapper.class); testNeeds = new Need[] { - new Need("one", "Atlantis", 0, 100, Need.GoalType.MONETARY, false), - new Need("two", "Atlantis", 1, 100, Need.GoalType.MONETARY, false), - new Need("three", "Atlantis", 2, 100, Need.GoalType.MONETARY, false) + new Need("one", "", "Atlantis", 0, 100, Need.GoalType.MONETARY, false, ""), + new Need("two", "", "Atlantis", 1, 100, Need.GoalType.MONETARY, false, ""), + new Need("three", "", "Atlantis", 2, 100, Need.GoalType.MONETARY, false, "") }; // When the object mapper is supposed to read from the file // the mock object mapper will return the hero array above @@ -54,7 +54,7 @@ public class CupboardFileDAOTest { @Test public void createNeedTest() throws IOException { - Need newNeed = new Need("sea urchin hats", "Atlantis", 100, GoalType.PHYSICAL, false); + Need newNeed = new Need("sea urchin hats", "", "Atlantis", 100, GoalType.PHYSICAL, false, ""); Need actualNeed = cupboardFileDao.addNeed(newNeed); @@ -90,7 +90,7 @@ public class CupboardFileDAOTest { Need unupdatedNeed = needs[needs.length - 1]; assertNotNull(unupdatedNeed); - Need updatedNeed = new Need("sequin sea urchin hats", "Atlantis", 100, GoalType.PHYSICAL, false); + Need updatedNeed = new Need("sequin sea urchin hats", "", "Atlantis", 100, GoalType.PHYSICAL, false, ""); Need actualNeed = cupboardFileDao.updateNeed(updatedNeed); assertEquals(actualNeed, updatedNeed); @@ -103,7 +103,7 @@ public class CupboardFileDAOTest { Need unupdatedNeed = needs[needs.length - 1]; assertNotNull(unupdatedNeed); - Need updatedNeed = new Need("sequin sea urchin hats", "Atlantis", 5, 100, GoalType.PHYSICAL, false); + Need updatedNeed = new Need("sequin sea urchin hats", "", "Atlantis", 5, 100, GoalType.PHYSICAL, false, ""); Need actualNeed = cupboardFileDao.updateNeed(updatedNeed); assertNull(actualNeed); 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 f3cbc23..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 @@ -40,14 +40,16 @@ public class CupboardServiceTest { double maxGoal = 100; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + 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, location, maxGoal, type, urgent); + Need response = cupboardService.createNeed(name, location, image, maxGoal, type, urgent, description); // Analyze assertNotNull(response); @@ -62,7 +64,9 @@ public class CupboardServiceTest { double maxGoal = -100.0; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + 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); @@ -73,7 +77,7 @@ public class CupboardServiceTest { // Analyze assertThrows(IllegalArgumentException.class, () -> - cupboardService.createNeed(name, location, maxGoal, type, urgent)); + cupboardService.createNeed(name, location, image, maxGoal, type, urgent, description)); } @Test @@ -84,7 +88,9 @@ public class CupboardServiceTest { double maxGoal = 100.0; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + 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 @@ -96,7 +102,7 @@ public class CupboardServiceTest { // Analyze assertThrows(DuplicateKeyException.class, () -> - cupboardService.createNeed(name, location, maxGoal, type, urgent)); + cupboardService.createNeed(name, location, image, maxGoal, type, urgent, description)); } @Test @@ -107,7 +113,9 @@ public class CupboardServiceTest { double maxGoal = 100.0; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + 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 @@ -129,7 +137,9 @@ public class CupboardServiceTest { double maxGoal = 100.0; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + 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 @@ -150,7 +160,9 @@ public class CupboardServiceTest { double maxGoal = 100.0; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - var need = new Need(name, location, maxGoal, type, urgent); + 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(0)).thenReturn(need); @@ -170,8 +182,10 @@ public class CupboardServiceTest { double maxGoal = 100.0; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - Need need = new Need(name, location, maxGoal, type, urgent); - Need newNeed = new Need("Octopus", location, maxGoal, type, urgent); + 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); @@ -191,7 +205,9 @@ public class CupboardServiceTest { double maxGoal = 100.0; GoalType type = Need.GoalType.MONETARY; boolean urgent = false; - Need need = new Need(name, location, maxGoal, type, urgent); + 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(0)).thenReturn(true); -- cgit v1.2.3