From 5eef7755f799d0f4fffad88dd05f18459c206253 Mon Sep 17 00:00:00 2001 From: benal01 Date: Fri, 21 Mar 2025 10:18:32 -0400 Subject: gitignore --- ufund-api/.gitignore | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'ufund-api') diff --git a/ufund-api/.gitignore b/ufund-api/.gitignore index 052bd9d..24d3397 100644 --- a/ufund-api/.gitignore +++ b/ufund-api/.gitignore @@ -32,5 +32,7 @@ build/ .vscode/ ### application specific ### -# /src/main/resources/application.properties -# /data/cupboard.json \ No newline at end of file +ufund-api/src/main/resources/application.properties +ufund-api/data/cupboard.json +ufund-api/data/users.json +ufund-api/data/userAuths.json \ No newline at end of file -- cgit v1.2.3 From 2b67bd14828c8c0bffe461a66542a2dba6c19f93 Mon Sep 17 00:00:00 2001 From: benal01 Date: Sat, 22 Mar 2025 11:21:30 -0400 Subject: API creation bug fix- the max goal would not be casted properly --- .../java/com/ufund/api/ufundapi/controller/CupboardController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index 36ae341..55cf88d 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -50,7 +50,8 @@ public class CupboardController { public ResponseEntity createNeed(@RequestBody Map params) { System.out.println(params); String name = (String) params.get("name"); - double maxGoal = (double) params.get("maxGoal"); + System.out.println("attemtping cast maxgoual"); + double maxGoal = ((Number) params.get("maxGoal")).doubleValue(); Need.GoalType goalType = GoalType.valueOf((String) params.get("type")); try { -- cgit v1.2.3 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/controller/CupboardController.java | 29 ++++++++ .../api/ufundapi/controller/UserController.java | 7 +- .../java/com/ufund/api/ufundapi/model/Need.java | 8 ++- .../java/com/ufund/api/ufundapi/model/User.java | 17 +++++ .../com/ufund/api/ufundapi/model/UserAuth.java | 7 +- .../api/ufundapi/persistence/CupboardFileDAO.java | 11 +-- .../api/ufundapi/persistence/UserAuthFIleDAO.java | 16 +++-- .../ufund/api/ufundapi/service/AuthService.java | 26 ++++--- .../api/ufundapi/service/CupboardService.java | 22 +++++- .../ufund/api/ufundapi/service/UserService.java | 6 +- .../ufundapi/persistence/CupboardFileDAOTest.java | 79 ++++++++++++++-------- .../api/ufundapi/persistence/UserFileDAOTest.java | 18 +++++ .../api/ufundapi/service/CupboardServiceTest.java | 4 +- 13 files changed, 190 insertions(+), 60 deletions(-) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index 36ae341..664b53b 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -161,6 +162,34 @@ public class CupboardController { } } + /** + * Checks out a need by checkoutAmount + * + * @param data JSON object with paramters needID and amount + * @param key Key used to authenticate user + * @return OK if successful, other statuses if failure + * @throws IllegalAccessException + */ + @PutMapping("/checkout") + public ResponseEntity checkoutNeeds(@RequestBody Map data, @RequestHeader("jelly-api-key") String key) throws IllegalAccessException { + int needID = data.get("needID"); + int checkoutAmount = data.get("amount"); + LOG.log(Level.INFO, "Checking out need with ID: " + needID + " by " + checkoutAmount); + try { + cupboardService.checkoutNeed(needID, checkoutAmount, key); + return new ResponseEntity<>(HttpStatus.OK); + } catch (IllegalArgumentException ex) { + ex.printStackTrace(); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } catch (IllegalAccessException ex) { + ex.printStackTrace(); + return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + } catch (IOException ex) { + ex.printStackTrace(); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + /** * Deletes a single need from the cupboard using the Need's id * 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 dfaad3a..b0dbd1d 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 @@ -99,12 +99,13 @@ public class UserController { * @param key The authentication key of the user * @return OK response and the user if it was successful, or * INTERNAL_SERVER_ERROR if there was an issue + * @throws IllegalAccessException */ @PutMapping("/{username}") - public ResponseEntity updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) { - LOG.log(Level.INFO,"PUT: " + user + " " + username + " " + key.toString()); + public ResponseEntity updateUser(@RequestHeader("jelly-api-key") String key, @RequestBody User user, @PathVariable String username) throws IllegalAccessException { + LOG.log(Level.INFO,"PUT: " + user + " " + username + " " + key); try { - //authService.authenticate(username, key); + authService.authenticate(username, key); user = userService.updateUser(user, username); if (user != null) { return new ResponseEntity<>(user, HttpStatus.OK); diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java index c0e9214..22e86e3 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java @@ -17,14 +17,14 @@ public class Need { @JsonProperty("current") private double current; /** - * Create a new need + * Create a new need, used by the controller * * @param name The name of the need * @param id The unique ID of the need * @param maxGoal The maximum goal for this need * @param type The type of need (monetary, physical) */ - public Need(@JsonProperty("name") String name, @JsonProperty("id") int id, @JsonProperty("maxGoal") double maxGoal, GoalType type) { + public Need(@JsonProperty("name") String name, @JsonProperty("id") int id, @JsonProperty("maxGoal") double maxGoal, @JsonProperty("type") GoalType type) { this.id = id; this.name = name; this.maxGoal = maxGoal; @@ -86,6 +86,10 @@ public class Need { this.current = current; } + public void incrementCurrent(double incrementAmount) { + this.current += incrementAmount; + } + public void setFilterAttributes(String[] filterAttributes) { this.filterAttributes = filterAttributes; } 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 6de1a8a..2871916 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 @@ -43,10 +43,21 @@ public class User { return username; } + /** + * Verifies if the provided password's hash is the same as the user's actual hash + * + * @param password The password to check if valid + * @return True or false depending on if it's equal + */ public boolean verifyPassword(String password) { return password.hashCode() == passwordHash; } + /** + * Adds a need's ID to a user's basket + * + * @param need The need to add + */ public void addToBasket(Need need) { basket.add(need.getId()); } @@ -59,6 +70,11 @@ public class User { return basket.remove(needID); } + /** + * Returns a user without a password hash for security purposes + * + * @return new User with empty password hash + */ public User withoutPasswordHash() { return new User(this.username, 0, this.basket, this.type); } @@ -71,6 +87,7 @@ public class User { this.passwordHash = other.passwordHash; } + @Override public String toString() { return this.username + "; basket: " + this.basket + "; type:" + this.type + "; hash: " + this.passwordHash; } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/UserAuth.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/UserAuth.java index 1c11a28..78dccec 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/UserAuth.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/UserAuth.java @@ -1,10 +1,10 @@ package com.ufund.api.ufundapi.model; -import com.fasterxml.jackson.annotation.JsonProperty; - import java.time.LocalDateTime; import java.util.UUID; +import com.fasterxml.jackson.annotation.JsonProperty; + public class UserAuth { @JsonProperty("key") String key; @JsonProperty("username") String username; @@ -12,12 +12,13 @@ public class UserAuth { public UserAuth(@JsonProperty("key") String key, @JsonProperty("username") String username, @JsonProperty("expiration") LocalDateTime expiration) { this.key = key; - this.expiration = expiration; this.username = username; + this.expiration = expiration; } /** * Generate a new user authentication profile + * * @param username the username the key will belong to * @return The new user authentication profile */ 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 521acae..4d11554 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 @@ -1,15 +1,16 @@ package com.ufund.api.ufundapi.persistence; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.ufund.api.ufundapi.model.Need; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - import java.io.File; import java.io.IOException; import java.util.Map; import java.util.TreeMap; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.ufund.api.ufundapi.model.Need; + @Component public class CupboardFileDAO implements CupboardDAO { diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java index 1fc1e92..9023b42 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java @@ -1,15 +1,17 @@ package com.ufund.api.ufundapi.persistence; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.ufund.api.ufundapi.model.UserAuth; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - import java.io.File; import java.io.IOException; +import java.time.LocalDateTime; import java.util.HashMap; import java.util.Map; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.ufund.api.ufundapi.model.UserAuth; + @Component public class UserAuthFIleDAO implements UserAuthDAO { @@ -35,7 +37,9 @@ public class UserAuthFIleDAO implements UserAuthDAO { UserAuth[] userAuthKeysArray = objectMapper.readValue(new File(filename), UserAuth[].class); for (UserAuth userAuth : userAuthKeysArray) { - userAuthMap.put(userAuth.getKey(), userAuth); + if (userAuth.getExpiration().compareTo(LocalDateTime.now()) > -1) { // Someone else double check the logic is correct. Checks if auth is valid and adds if so + userAuthMap.put(userAuth.getKey(), userAuth); + } } } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java index 87a16a6..71b8f41 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java @@ -1,11 +1,12 @@ package com.ufund.api.ufundapi.service; +import java.io.IOException; + +import org.springframework.stereotype.Component; + import com.ufund.api.ufundapi.model.User; import com.ufund.api.ufundapi.model.UserAuth; import com.ufund.api.ufundapi.persistence.UserAuthDAO; -import org.springframework.stereotype.Component; - -import java.io.IOException; @Component public class AuthService { @@ -30,12 +31,19 @@ public class AuthService { if (userAuth == null) { throw new IllegalAccessException("Unauthenticated"); } -// -// var username = userAuth.getUsername(); -// var userType = userService.getUser(username).getType(); -// if (!username.equals(targetUsername) && userType != User.UserType.MANAGER) { -// throw new IllegalAccessException("Unauthorized"); -// } + + var username = userAuth.getUsername(); + var userType = userService.getUser(username).getType(); + if (!username.equals(targetUsername) && userType != User.UserType.MANAGER) { + throw new IllegalAccessException("Unauthorized"); + } + } + + public void authenticate(String key) throws IOException, IllegalAccessException { + var userAuth = userAuthDAO.getUserAuth(key); + if (userAuth == null) { + throw new IllegalAccessException("Unauthenticated"); + } } /** diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 2398745..8713882 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -3,6 +3,7 @@ package com.ufund.api.ufundapi.service; import java.io.IOException; import java.util.Arrays; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import com.ufund.api.ufundapi.DuplicateKeyException; @@ -13,8 +14,10 @@ import com.ufund.api.ufundapi.persistence.CupboardDAO; public class CupboardService { private final CupboardDAO cupboardDAO; + final AuthService authService; - public CupboardService(CupboardDAO cupboardDAO) { + public CupboardService(@Lazy AuthService authService, CupboardDAO cupboardDAO) { + this.authService = authService; this.cupboardDAO = cupboardDAO; } @@ -96,6 +99,23 @@ public class CupboardService { return cupboardDAO.updateNeed(need); } + /** + * Checks out a need with the desired amount + * + * @param id The ID of the need to update + * @param checkoutAmount The amount to update the need by + * @throws IOException + * @throws IllegalAccessException + */ + public void checkoutNeed(int id, double checkoutAmount, String key) throws IOException, IllegalAccessException { + if (checkoutAmount <= 0) { + throw new IllegalArgumentException("Amount must be greather than 0"); + } + authService.authenticate(key); + Need need = cupboardDAO.getNeed(id); + need.incrementCurrent(checkoutAmount); + } + /** * Delete a need from the cupboard * 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 caf9f4c..aaa2f06 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,6 +2,7 @@ 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; @@ -12,7 +13,7 @@ import com.ufund.api.ufundapi.persistence.UserDAO; public class UserService { private final UserDAO userDAO; - private final CupboardService cupboardService; + final CupboardService cupboardService; public UserService(UserDAO userDao, CupboardService cupboardService) { this.userDAO = userDao; @@ -44,6 +45,9 @@ public class UserService { */ public User getUser(String username) throws IOException { User user = userDAO.getUser(username); + if (user == null) { + return null; + } for (int needId : user.getNeeds()) { if (cupboardService.getNeed(needId) == null) { user.removeBasketNeed(needId); 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 c15aa3daab0cf9a640945d4e634d1327fb55d2db Mon Sep 17 00:00:00 2001 From: sowgro Date: Tue, 25 Mar 2025 00:03:45 -0400 Subject: Greatly improve logging and other backend clean up --- .../api/ufundapi/controller/AuthController.java | 12 ++++- .../ufundapi/controller/CupboardController.java | 57 +++++++++++++--------- .../api/ufundapi/controller/UserController.java | 38 +++++++++------ .../java/com/ufund/api/ufundapi/model/Need.java | 2 +- .../api/ufundapi/persistence/CupboardFileDAO.java | 13 +---- .../api/ufundapi/persistence/UserFileDAO.java | 9 ++-- .../ufund/api/ufundapi/service/AuthService.java | 8 +-- .../api/ufundapi/service/CupboardService.java | 6 +-- 8 files changed, 81 insertions(+), 64 deletions(-) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java index b46d4ee..6ba6160 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java @@ -2,6 +2,8 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -17,6 +19,7 @@ import com.ufund.api.ufundapi.service.AuthService; @RestController @RequestMapping("auth") public class AuthController { + private static final Logger LOG = Logger.getLogger(AuthController.class.getName()); private final AuthService authService; public AuthController(AuthService authService) { @@ -32,14 +35,17 @@ public class AuthController { */ @PostMapping("") public ResponseEntity login(@RequestBody Map params) { + LOG.log(Level.INFO, "POST /auth body: {0}", params); String username = params.get("username"); String password = params.get("password"); try { String key = authService.login(username, password); return new ResponseEntity<>(key, HttpStatus.OK); - } catch (IllegalAccessException e) { + } catch (IllegalAccessException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -52,10 +58,12 @@ public class AuthController { */ @DeleteMapping("") public ResponseEntity logout(@RequestHeader("jelly-api-key") String key) { + LOG.log(Level.INFO, "DELETE /auth key: {0}", key); try { authService.logout(key); return new ResponseEntity<>(HttpStatus.OK); - } catch (IOException e) { + } catch (IOException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index 664b53b..8db8901 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -23,6 +23,8 @@ import com.ufund.api.ufundapi.model.Need; import com.ufund.api.ufundapi.model.Need.GoalType; import com.ufund.api.ufundapi.service.CupboardService; +import static java.util.List.of; + @RestController @RequestMapping("cupboard") public class CupboardController { @@ -49,7 +51,8 @@ public class CupboardController { */ @PostMapping("") public ResponseEntity createNeed(@RequestBody Map params) { - System.out.println(params); + LOG.log(Level.INFO, "POST /cupboard body: {0}", params); + String name = (String) params.get("name"); double maxGoal = (double) params.get("maxGoal"); Need.GoalType goalType = GoalType.valueOf((String) params.get("type")); @@ -58,10 +61,13 @@ public class CupboardController { Need need = cupboardService.createNeed(name, maxGoal, goalType); return new ResponseEntity<>(need, HttpStatus.OK); } catch (DuplicateKeyException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.CONFLICT); } catch (IllegalArgumentException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -76,7 +82,7 @@ public class CupboardController { */ @GetMapping("") public ResponseEntity getNeeds() { - LOG.info("GET /needs"); + LOG.info("GET /cupboard"); try { Need[] needs = cupboardService.getNeeds(); @@ -88,19 +94,21 @@ public class CupboardController { } /** - * Responds to the GET request for all {@linkplain Need need} whose name contains - * the text in name - * - * @param name The name parameter which contains the text used to find the {@link Need need} - * - * @return ResponseEntity with array of {@link Need need} objects (may be empty) and - * HTTP status of OK
- * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise - *

- */ + * Responds to the GET request for all {@linkplain Need need} whose name contains + * the text in name + * + * @param name The name parameter which contains the text used to find the {@link Need need} + * + * @deprecated Searching should now be done client side in the future + * + * @return ResponseEntity with array of {@link Need need} objects (may be empty) and + * HTTP status of OK
+ * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise + *

+ */ @GetMapping("/") public ResponseEntity searchNeeds(@RequestParam String name) { - LOG.info("GET /need/?name="+name); + LOG.info("GET /cupboard/?name="+name); try { Need[] needs = cupboardService.searchNeeds(name); @@ -121,7 +129,7 @@ public class CupboardController { */ @GetMapping("/{id}") public ResponseEntity getNeed(@PathVariable int id) { - LOG.log(Level.INFO, "GET /need/{0}", id); + LOG.log(Level.INFO, "GET /cupboard/{0}", id); try { Need need = cupboardService.getNeed(id); @@ -145,7 +153,7 @@ public class CupboardController { */ @PutMapping("/{id}") public ResponseEntity updateNeed(@RequestBody Need need, @PathVariable int id) { - LOG.log(Level.INFO, "Updating need: " + need); + LOG.log(Level.INFO, "PUT /cupboard/{0} body: {1}", of(id, need)); try { Need updatedNeed = cupboardService.updateNeed(need, id); if (updatedNeed != null) { @@ -154,10 +162,10 @@ public class CupboardController { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } catch (IllegalArgumentException ex) { - ex.printStackTrace(); + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } catch (IOException ex) { - ex.printStackTrace(); + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -168,24 +176,23 @@ public class CupboardController { * @param data JSON object with paramters needID and amount * @param key Key used to authenticate user * @return OK if successful, other statuses if failure - * @throws IllegalAccessException */ @PutMapping("/checkout") - public ResponseEntity checkoutNeeds(@RequestBody Map data, @RequestHeader("jelly-api-key") String key) throws IllegalAccessException { + public ResponseEntity checkoutNeeds(@RequestBody Map data, @RequestHeader("jelly-api-key") String key) { int needID = data.get("needID"); int checkoutAmount = data.get("amount"); - LOG.log(Level.INFO, "Checking out need with ID: " + needID + " by " + checkoutAmount); + LOG.log(Level.INFO, "PUT /need/checkout body: {0}", data); try { cupboardService.checkoutNeed(needID, checkoutAmount, key); return new ResponseEntity<>(HttpStatus.OK); } catch (IllegalArgumentException ex) { - ex.printStackTrace(); + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } catch (IllegalAccessException ex) { - ex.printStackTrace(); + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); } catch (IOException ex) { - ex.printStackTrace(); + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -198,6 +205,7 @@ public class CupboardController { */ @DeleteMapping("/{id}") public ResponseEntity deleteNeed(@PathVariable int id) { + LOG.log(Level.INFO, "DELETE /cupboard/{0}", id); try { Need need = cupboardService.getNeed(id); if (cupboardService.deleteNeed(id)) { @@ -205,7 +213,8 @@ public class CupboardController { } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - } catch (IOException e) { + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } 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 b0dbd1d..cd340ef 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 @@ -23,6 +23,8 @@ import com.ufund.api.ufundapi.model.User; import com.ufund.api.ufundapi.service.AuthService; import com.ufund.api.ufundapi.service.UserService; +import static java.util.List.of; + @RestController @RequestMapping("users") public class UserController { @@ -43,6 +45,7 @@ public class UserController { */ @PostMapping("") public ResponseEntity createUser(@RequestBody Map params) { + LOG.log(Level.INFO, "POST /users body: {0}", params); String username = params.get("username"); String password = params.get("password"); @@ -54,8 +57,10 @@ public class UserController { return new ResponseEntity<>(HttpStatus.CONFLICT); } } catch (DuplicateKeyException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.CONFLICT); } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -72,7 +77,7 @@ public class UserController { */ @GetMapping("/{username}") public ResponseEntity getUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { - LOG.log(Level.INFO, "GET /user/{0}", username); + LOG.log(Level.INFO, "GET /user/{0} key: {1}", of(username, key)); try { authService.authenticate(username, key); @@ -83,9 +88,10 @@ public class UserController { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } catch (IllegalAccessException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); - } catch (IOException e) { - LOG.log(Level.SEVERE, e.getLocalizedMessage()); + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } @@ -99,11 +105,10 @@ public class UserController { * @param key The authentication key of the user * @return OK response and the user if it was successful, or * INTERNAL_SERVER_ERROR if there was an issue - * @throws IllegalAccessException */ @PutMapping("/{username}") - public ResponseEntity updateUser(@RequestHeader("jelly-api-key") String key, @RequestBody User user, @PathVariable String username) throws IllegalAccessException { - LOG.log(Level.INFO,"PUT: " + user + " " + username + " " + key); + public ResponseEntity updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) { + LOG.log(Level.INFO,"PUT /users/{0} body: {1} key: {2}", of(user, username, key)); try { authService.authenticate(username, key); user = userService.updateUser(user, username); @@ -113,13 +118,15 @@ public class UserController { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } catch (InvalidParameterException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } catch (IOException e) { + } catch (IllegalAccessException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); + return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); - } - // catch (IllegalAccessException e) { - // return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); - // } + } } /** @@ -132,6 +139,7 @@ public class UserController { */ @DeleteMapping("/{username}") public ResponseEntity deleteUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { + LOG.log(Level.INFO, "DELETE /users/{0} id: {1}", of(username, key)); try { authService.authenticate(username, key); @@ -140,10 +148,12 @@ public class UserController { } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - } catch (IOException e) { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); - } catch (IllegalAccessException e) { + } catch (IllegalAccessException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java index 22e86e3..786b104 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java @@ -38,7 +38,7 @@ public class Need { * @param maxGoal The maximum goal for this need * @param type The type of need (monetary, physical) */ - public Need(String name, GoalType type, double maxGoal) { + public Need(String name, GoalType type, double maxGoal) { // TODO why is this needed this.name = name; this.type = type; this.maxGoal = maxGoal; 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 4d11554..3115204 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 @@ -52,15 +52,6 @@ public class CupboardFileDAO implements CupboardDAO { nextId++; } - /** - * Return an array of the needs - * - * @return An array of all the needs - */ - private Need[] getNeedsArray() { - return needs.values().toArray(Need[]::new); - } - /** * Saves the needs to json * @@ -68,7 +59,7 @@ public class CupboardFileDAO implements CupboardDAO { * @throws IOException If there was an IO issue saving the file */ private boolean save() throws IOException { - Need[] needArray = getNeedsArray(); + Need[] needArray = needs.values().toArray(Need[]::new); objectMapper.writeValue(new File(filename), needArray); return true; @@ -77,7 +68,7 @@ public class CupboardFileDAO implements CupboardDAO { @Override public Need[] getNeeds() { synchronized (needs) { - return getNeedsArray(); + return needs.values().toArray(Need[]::new); } } 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 6e900aa..1b888cd 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 @@ -5,6 +5,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -82,16 +83,14 @@ 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.getNeeds() == null || user.getType() == null) { + if (user.getNeeds() == null || user.getType() == null) { // TODO clean this up -tyler User oldData = users.get(user.getUsername()); - User crutch = new User(oldData.getUsername(), 0, new ArrayList(), oldData.getType()); + User crutch = new User(oldData.getUsername(), 0, new ArrayList<>(), oldData.getType()); crutch.copyPassword(oldData); users.put(user.getUsername(), crutch); } else { var old = users.put(user.getUsername(), user); - user.copyPassword(old); + user.copyPassword(Objects.requireNonNull(old)); } save(); return user; diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java index 71b8f41..4e5ebce 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java @@ -29,20 +29,20 @@ public class AuthService { public void authenticate(String targetUsername, String key) throws IllegalAccessException, IOException { var userAuth = userAuthDAO.getUserAuth(key); if (userAuth == null) { - throw new IllegalAccessException("Unauthenticated"); + throw new IllegalAccessException("Invalid authentication key"); } var username = userAuth.getUsername(); var userType = userService.getUser(username).getType(); if (!username.equals(targetUsername) && userType != User.UserType.MANAGER) { - throw new IllegalAccessException("Unauthorized"); + throw new IllegalAccessException("Provided key does not grant access to perform the requested operation"); } } public void authenticate(String key) throws IOException, IllegalAccessException { var userAuth = userAuthDAO.getUserAuth(key); if (userAuth == null) { - throw new IllegalAccessException("Unauthenticated"); + throw new IllegalAccessException("Invalid authentication key"); } } @@ -58,7 +58,7 @@ public class AuthService { public String login(String username, String password) throws IllegalAccessException, IOException { var usr = userService.getUser(username); if (usr == null || !usr.verifyPassword(password)) { - throw new IllegalAccessException("Unauthorized"); + throw new IllegalAccessException("Incorrect username or password"); } var userAuth = UserAuth.generate(username); userAuthDAO.addUserAuth(userAuth); diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 8713882..91e3ba5 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -104,12 +104,12 @@ public class CupboardService { * * @param id The ID of the need to update * @param checkoutAmount The amount to update the need by - * @throws IOException - * @throws IllegalAccessException + * @throws IOException If there is an error reading the file + * @throws IllegalAccessException If the user has insufficient permission */ public void checkoutNeed(int id, double checkoutAmount, String key) throws IOException, IllegalAccessException { if (checkoutAmount <= 0) { - throw new IllegalArgumentException("Amount must be greather than 0"); + throw new IllegalArgumentException("Amount must be greater than 0"); } authService.authenticate(key); Need need = cupboardDAO.getNeed(id); -- 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 --- .../java/com/ufund/api/ufundapi/model/User.java | 27 +++++++++++++--------- .../api/ufundapi/persistence/UserFileDAO.java | 2 +- .../ufund/api/ufundapi/service/UserService.java | 6 ++--- .../com/ufund/api/ufundapi/model/UserTest.java | 4 ++-- 4 files changed, 22 insertions(+), 17 deletions(-) (limited to 'ufund-api') 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 2871916..d04d8b7 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 @@ -12,18 +12,23 @@ public class User { MANAGER } - @JsonProperty("username") private final String username; - @JsonProperty("passwordHash") private int passwordHash; - @JsonProperty("basket") private final List basket; - @JsonProperty("type") private final UserType type; + @JsonProperty("username") + private final String username; + @JsonProperty("passwordHash") + private int passwordHash; + @JsonProperty("basket") + private final List basket; + @JsonProperty("type") + private final UserType type; /** * Create a new user * - * @param username The name of the user - * @param basket A basket to copy from + * @param username The name of the user + * @param basket A basket to copy from */ - public User(@JsonProperty("username") String username, @JsonProperty("passwordHash") int passwordHash, @JsonProperty("basket") List basket, @JsonProperty("type") UserType userType) { + public User(@JsonProperty("username") String username, @JsonProperty("passwordHash") int passwordHash, + @JsonProperty("basket") List basket, @JsonProperty("type") UserType userType) { this.username = username; this.basket = basket; this.passwordHash = passwordHash; @@ -35,8 +40,7 @@ public class User { username, password.hashCode(), new ArrayList<>(), - UserType.HELPER - ); + UserType.HELPER); } public String getUsername() { @@ -44,7 +48,8 @@ public class User { } /** - * Verifies if the provided password's hash is the same as the user's actual hash + * Verifies if the provided password's hash is the same as the user's actual + * hash * * @param password The password to check if valid * @return True or false depending on if it's equal @@ -62,7 +67,7 @@ public class User { basket.add(need.getId()); } - public Integer[] getNeeds() { + public Integer[] getBasket() { return basket.toArray(Integer[]::new); } 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 6e900aa..16560e7 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 @@ -84,7 +84,7 @@ public class UserFileDAO implements UserDAO { if (users.containsKey(user.getUsername())) { // var old = users.put(user.getUsername(), user); // user.copyPassword(old); - if (user.getNeeds() == null || user.getType() == null) { + if (user.getBasket() == null || user.getType() == null) { User oldData = users.get(user.getUsername()); User crutch = new User(oldData.getUsername(), 0, new ArrayList(), oldData.getType()); crutch.copyPassword(oldData); 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 aaa2f06..51283fc 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 @@ -48,7 +48,7 @@ public class UserService { if (user == null) { return null; } - for (int needId : user.getNeeds()) { + for (int needId : user.getBasket()) { if (cupboardService.getNeed(needId) == null) { user.removeBasketNeed(needId); } @@ -59,7 +59,7 @@ public class UserService { /** * Updates a user * - * @param user The ID of the user to update + * @param user The ID of the user to update * @param username The user object to set (note: the ID is ignored) * @return The updated user object * @throws IOException Thrown if there was any issue saving the data @@ -81,5 +81,5 @@ public class UserService { public boolean deleteUser(String username) throws IOException { return userDAO.deleteUser(username); } - + } 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 a2f35f6c35b96e3103d8eb6c2bdefc7c081f72f2 Mon Sep 17 00:00:00 2001 From: sowgro Date: Tue, 25 Mar 2025 09:05:23 -0400 Subject: Tweak logging --- .../java/com/ufund/api/ufundapi/controller/AuthController.java | 4 ++-- .../com/ufund/api/ufundapi/controller/CupboardController.java | 6 +++--- .../java/com/ufund/api/ufundapi/controller/UserController.java | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java index 6ba6160..aa99a90 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java @@ -35,7 +35,7 @@ public class AuthController { */ @PostMapping("") public ResponseEntity login(@RequestBody Map params) { - LOG.log(Level.INFO, "POST /auth body: {0}", params); + LOG.log(Level.INFO, "POST /auth body={0}", params); String username = params.get("username"); String password = params.get("password"); try { @@ -58,7 +58,7 @@ public class AuthController { */ @DeleteMapping("") public ResponseEntity logout(@RequestHeader("jelly-api-key") String key) { - LOG.log(Level.INFO, "DELETE /auth key: {0}", key); + LOG.log(Level.INFO, "DELETE /auth key={0}", key); try { authService.logout(key); return new ResponseEntity<>(HttpStatus.OK); diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index 8db8901..e62d5ab 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -51,7 +51,7 @@ public class CupboardController { */ @PostMapping("") public ResponseEntity createNeed(@RequestBody Map params) { - LOG.log(Level.INFO, "POST /cupboard body: {0}", params); + LOG.log(Level.INFO, "POST /cupboard body={0}", params); String name = (String) params.get("name"); double maxGoal = (double) params.get("maxGoal"); @@ -153,7 +153,7 @@ public class CupboardController { */ @PutMapping("/{id}") public ResponseEntity updateNeed(@RequestBody Need need, @PathVariable int id) { - LOG.log(Level.INFO, "PUT /cupboard/{0} body: {1}", of(id, need)); + LOG.log(Level.INFO, "PUT /cupboard/{0} body={1}", of(id, need)); try { Need updatedNeed = cupboardService.updateNeed(need, id); if (updatedNeed != null) { @@ -181,7 +181,7 @@ public class CupboardController { public ResponseEntity checkoutNeeds(@RequestBody Map data, @RequestHeader("jelly-api-key") String key) { int needID = data.get("needID"); int checkoutAmount = data.get("amount"); - LOG.log(Level.INFO, "PUT /need/checkout body: {0}", data); + LOG.log(Level.INFO, "PUT /need/checkout body={0}", data); try { cupboardService.checkoutNeed(needID, checkoutAmount, key); return new ResponseEntity<>(HttpStatus.OK); 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 cd340ef..d2f3f28 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 @@ -45,7 +45,7 @@ public class UserController { */ @PostMapping("") public ResponseEntity createUser(@RequestBody Map params) { - LOG.log(Level.INFO, "POST /users body: {0}", params); + LOG.log(Level.INFO, "POST /users body={0}", params); String username = params.get("username"); String password = params.get("password"); @@ -77,7 +77,7 @@ public class UserController { */ @GetMapping("/{username}") public ResponseEntity getUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { - LOG.log(Level.INFO, "GET /user/{0} key: {1}", of(username, key)); + LOG.log(Level.INFO, "GET /user/{0} key={1}", of(username, key)); try { authService.authenticate(username, key); @@ -108,7 +108,7 @@ public class UserController { */ @PutMapping("/{username}") public ResponseEntity updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) { - LOG.log(Level.INFO,"PUT /users/{0} body: {1} key: {2}", of(user, username, key)); + LOG.log(Level.INFO,"PUT /users/{0} body={1} key={2}", of(username, user, key)); try { authService.authenticate(username, key); user = userService.updateUser(user, username); @@ -139,7 +139,7 @@ public class UserController { */ @DeleteMapping("/{username}") public ResponseEntity deleteUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { - LOG.log(Level.INFO, "DELETE /users/{0} id: {1}", of(username, key)); + LOG.log(Level.INFO, "DELETE /users/{0} id={1}", of(username, key)); try { authService.authenticate(username, key); -- 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 --- .../api/ufundapi/controller/UserController.java | 3 +-- .../java/com/ufund/api/ufundapi/model/User.java | 4 +-- .../api/ufundapi/persistence/CupboardFileDAO.java | 7 +++--- .../api/ufundapi/persistence/UserFileDAO.java | 11 +++----- .../ufund/api/ufundapi/service/UserService.java | 1 - .../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 +++++----- 12 files changed, 56 insertions(+), 69 deletions(-) (limited to 'ufund-api') 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 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 b0369f8b5e50eaec22c9178748f57dde6912d383 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Tue, 25 Mar 2025 18:07:45 -0400 Subject: Created signup component and implemented some functionality. Did not finish implementing color bar and error messages. --- .../main/java/com/ufund/api/ufundapi/controller/CupboardController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index e62d5ab..d2029ed 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -173,7 +173,7 @@ public class CupboardController { /** * Checks out a need by checkoutAmount * - * @param data JSON object with paramters needID and amount + * @param data JSON object with parameters needID and amount * @param key Key used to authenticate user * @return OK if successful, other statuses if failure */ -- 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 --- .../ufundapi/controller/CupboardController.java | 33 ++++++++++++++++------ .../api/ufundapi/controller/UserController.java | 12 ++++---- .../ufund/api/ufundapi/service/AuthService.java | 30 ++++++++++++++++++-- .../api/ufundapi/service/CupboardService.java | 3 +- .../controller/CupboardControllerTest.java | 29 +++++++++++-------- .../ufundapi/controller/UserControllerTest.java | 6 ++-- .../api/ufundapi/service/AuthServiceTest.java | 12 ++++---- 7 files changed, 89 insertions(+), 36 deletions(-) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index e62d5ab..55ee457 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -5,6 +5,7 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import com.ufund.api.ufundapi.service.AuthService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; @@ -23,21 +24,21 @@ import com.ufund.api.ufundapi.model.Need; import com.ufund.api.ufundapi.model.Need.GoalType; import com.ufund.api.ufundapi.service.CupboardService; -import static java.util.List.of; - @RestController @RequestMapping("cupboard") public class CupboardController { private static final Logger LOG = Logger.getLogger(CupboardController.class.getName()); private final CupboardService cupboardService; + private final AuthService authService; /** * Create a cupboard controller to receive REST signals * * @param cupboardService The Data Access Object */ - public CupboardController(CupboardService cupboardService) { + public CupboardController(CupboardService cupboardService, AuthService authService) { this.cupboardService = cupboardService; + this.authService = authService; } /** @@ -50,14 +51,15 @@ public class CupboardController { * INTERNAL_SERVER_ERROR otherwise */ @PostMapping("") - public ResponseEntity createNeed(@RequestBody Map params) { + public ResponseEntity createNeed(@RequestBody Map params, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO, "POST /cupboard body={0}", params); String name = (String) params.get("name"); - double maxGoal = (double) params.get("maxGoal"); + double maxGoal = ((Number) params.get("maxGoal")).doubleValue(); Need.GoalType goalType = GoalType.valueOf((String) params.get("type")); try { + authService.keyHasAccessToCupboard(key); Need need = cupboardService.createNeed(name, maxGoal, goalType); return new ResponseEntity<>(need, HttpStatus.OK); } catch (DuplicateKeyException ex) { @@ -66,6 +68,9 @@ public class CupboardController { } catch (IllegalArgumentException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } catch (IllegalAccessException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); + return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); @@ -152,9 +157,10 @@ public class CupboardController { * @return OK response and the need if it was successful, or INTERNAL_SERVER_ERROR if there was an issue */ @PutMapping("/{id}") - public ResponseEntity updateNeed(@RequestBody Need need, @PathVariable int id) { + public ResponseEntity updateNeed(@RequestBody Need need, @PathVariable int id, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO, "PUT /cupboard/{0} body={1}", of(id, need)); try { + authService.keyHasAccessToCupboard(key); Need updatedNeed = cupboardService.updateNeed(need, id); if (updatedNeed != null) { return new ResponseEntity<>(need, HttpStatus.OK); @@ -164,6 +170,9 @@ public class CupboardController { } catch (IllegalArgumentException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } catch (IllegalAccessException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); + return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); @@ -204,19 +213,27 @@ public class CupboardController { * @return OK if the need was deleted, NOT_FOUND if the need was not found, or INTERNAL_SERVER_ERROR if an error occurred */ @DeleteMapping("/{id}") - public ResponseEntity deleteNeed(@PathVariable int id) { + public ResponseEntity deleteNeed(@PathVariable int id, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO, "DELETE /cupboard/{0}", id); try { + authService.keyHasAccessToCupboard(key); Need need = cupboardService.getNeed(id); if (cupboardService.deleteNeed(id)) { return new ResponseEntity<>(need, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); - } + } + } catch (IllegalAccessException ex) { + LOG.log(Level.WARNING, ex.getLocalizedMessage()); + return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } + private Object[] of(Object ...params) { + return params; + } + } 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 c2d9e06..33d2e4f 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 @@ -22,8 +22,6 @@ import com.ufund.api.ufundapi.model.User; import com.ufund.api.ufundapi.service.AuthService; import com.ufund.api.ufundapi.service.UserService; -import static java.util.List.of; - @RestController @RequestMapping("users") public class UserController { @@ -79,7 +77,7 @@ public class UserController { LOG.log(Level.INFO, "GET /user/{0} key={1}", of(username, key)); try { - authService.authenticate(username, key); + authService.keyHasAccessToUser(username, key); User user = userService.getUser(username); if (user != null) { return new ResponseEntity<>(user.withoutPasswordHash(), HttpStatus.OK); @@ -109,7 +107,7 @@ public class UserController { public ResponseEntity updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO,"PUT /users/{0} body={1} key={2}", of(username, user, key)); try { - authService.authenticate(username, key); + authService.keyHasAccessToUser(username, key); user = userService.updateUser(user, username); if (user != null) { return new ResponseEntity<>(user, HttpStatus.OK); @@ -141,7 +139,7 @@ public class UserController { LOG.log(Level.INFO, "DELETE /users/{0} id={1}", of(username, key)); try { - authService.authenticate(username, key); + authService.keyHasAccessToUser(username, key); if (userService.deleteUser(username)) { return new ResponseEntity<>(HttpStatus.OK); } else { @@ -156,4 +154,8 @@ public class UserController { } } + private Object[] of(Object ...params) { + return params; + } + } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java index 4e5ebce..cdce80d 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java @@ -25,8 +25,9 @@ public class AuthService { * @param targetUsername The targetUsername of the user trying to be accessed. * @param key The api key obtained by the client from logging in. * @throws IllegalAccessException Thrown if access was denied to the user. + * @throws IOException Thrown on a file writing issue */ - public void authenticate(String targetUsername, String key) throws IllegalAccessException, IOException { + public void keyHasAccessToUser(String targetUsername, String key) throws IllegalAccessException, IOException { var userAuth = userAuthDAO.getUserAuth(key); if (userAuth == null) { throw new IllegalAccessException("Invalid authentication key"); @@ -39,11 +40,36 @@ public class AuthService { } } - public void authenticate(String key) throws IOException, IllegalAccessException { + /** + * Check if the provided key is valid + * @param key The api key obtained by the client from logging in. + * @throws IllegalAccessException Thrown if access was denied to the user. + * @throws IOException Thrown on a file writing issue + */ + public void keyIsValid(String key) throws IOException, IllegalAccessException { + var userAuth = userAuthDAO.getUserAuth(key); + if (userAuth == null) { + throw new IllegalAccessException("Invalid authentication key"); + } + } + + /** + * Check if the provided key has access to edit the cupboard + * @param key The api key obtained by the client from logging in. + * @throws IllegalAccessException Thrown if access was denied to the user. + * @throws IOException Thrown on a file writing issue + */ + public void keyHasAccessToCupboard(String key) throws IOException, IllegalAccessException { var userAuth = userAuthDAO.getUserAuth(key); if (userAuth == null) { throw new IllegalAccessException("Invalid authentication key"); } + + var username = userAuth.getUsername(); + var userType = userService.getUser(username).getType(); + if (userType != User.UserType.MANAGER) { + throw new IllegalAccessException("Provided key does not grant access to perform the requested operation"); + } } /** diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 91e3ba5..aaa8cb8 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -111,7 +111,7 @@ public class CupboardService { if (checkoutAmount <= 0) { throw new IllegalArgumentException("Amount must be greater than 0"); } - authService.authenticate(key); + authService.keyIsValid(key); Need need = cupboardDAO.getNeed(id); need.incrementCurrent(checkoutAmount); } @@ -124,6 +124,7 @@ public class CupboardService { * @throws IOException Thrown on any problem removing the need */ public boolean deleteNeed(int id) throws IOException { + return cupboardDAO.deleteNeed(id); } } 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 5dfb1327c4507ae1613debb5b485fd74edff33db Mon Sep 17 00:00:00 2001 From: sowgro Date: Wed, 26 Mar 2025 19:00:04 -0400 Subject: fix expiration logic and cleanup --- ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java | 2 +- .../main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java | 2 +- .../src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java index 786b104..22e86e3 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java @@ -38,7 +38,7 @@ public class Need { * @param maxGoal The maximum goal for this need * @param type The type of need (monetary, physical) */ - public Need(String name, GoalType type, double maxGoal) { // TODO why is this needed + public Need(String name, GoalType type, double maxGoal) { this.name = name; this.type = type; this.maxGoal = maxGoal; diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java index 9023b42..7bda3f9 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java @@ -37,7 +37,7 @@ public class UserAuthFIleDAO implements UserAuthDAO { UserAuth[] userAuthKeysArray = objectMapper.readValue(new File(filename), UserAuth[].class); for (UserAuth userAuth : userAuthKeysArray) { - if (userAuth.getExpiration().compareTo(LocalDateTime.now()) > -1) { // Someone else double check the logic is correct. Checks if auth is valid and adds if so + if (userAuth.getExpiration().isBefore(LocalDateTime.now())) { userAuthMap.put(userAuth.getKey(), userAuth); } } 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 0d9b9e4..4b09449 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 @@ -81,7 +81,8 @@ public class UserFileDAO implements UserDAO { public User updateUser(User user) throws IOException { synchronized (users) { if (users.containsKey(user.getUsername())) { - if (user.getBasket() == null || user.getType() == null) { // TODO clean this up + if (user.getBasket() == null || user.getType() == null) { + System.err.println("CRUTCH HAPPENED"); User oldData = users.get(user.getUsername()); User crutch = new User(oldData.getUsername(), 0, new ArrayList<>(), oldData.getType()); crutch.copyPassword(oldData); -- 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 --- .../main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java | 2 +- .../java/com/ufund/api/ufundapi/persistence/UserAuthFileDAOTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java index 7bda3f9..24a426b 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserAuthFIleDAO.java @@ -37,7 +37,7 @@ public class UserAuthFIleDAO implements UserAuthDAO { UserAuth[] userAuthKeysArray = objectMapper.readValue(new File(filename), UserAuth[].class); for (UserAuth userAuth : userAuthKeysArray) { - if (userAuth.getExpiration().isBefore(LocalDateTime.now())) { + if (userAuth.getExpiration().isAfter(LocalDateTime.now())) { userAuthMap.put(userAuth.getKey(), userAuth); } } 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 4f5e9e9ecda282a98af5d70bd6cf0540973c7314 Mon Sep 17 00:00:00 2001 From: sowgro Date: Thu, 27 Mar 2025 18:47:27 -0400 Subject: Remove crutch --- .../com/ufund/api/ufundapi/persistence/UserFileDAO.java | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) (limited to 'ufund-api') 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 4b09449..ec94da8 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 @@ -2,7 +2,6 @@ package com.ufund.api.ufundapi.persistence; import java.io.File; import java.io.IOException; -import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -81,16 +80,8 @@ public class UserFileDAO implements UserDAO { public User updateUser(User user) throws IOException { synchronized (users) { if (users.containsKey(user.getUsername())) { - if (user.getBasket() == null || user.getType() == null) { - System.err.println("CRUTCH HAPPENED"); - User oldData = users.get(user.getUsername()); - User crutch = new User(oldData.getUsername(), 0, new ArrayList<>(), oldData.getType()); - crutch.copyPassword(oldData); - users.put(user.getUsername(), crutch); - } else { - var old = users.put(user.getUsername(), user); - user.copyPassword(Objects.requireNonNull(old)); - } + var old = users.put(user.getUsername(), user); + user.copyPassword(Objects.requireNonNull(old)); save(); return user; } else { -- cgit v1.2.3 From e0a3f2c2c0fec40aa50c8889e343a1dbc7f9d7fb Mon Sep 17 00:00:00 2001 From: benal01 Date: Sat, 29 Mar 2025 15:01:12 -0400 Subject: API functionality for urgency and location --- .../ufundapi/controller/CupboardController.java | 5 ++-- .../java/com/ufund/api/ufundapi/model/Need.java | 32 ++++++++++++++++++++-- .../api/ufundapi/service/CupboardService.java | 4 +-- 3 files changed, 35 insertions(+), 6 deletions(-) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index 55cf88d..f79e445 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -50,12 +50,13 @@ public class CupboardController { public ResponseEntity createNeed(@RequestBody Map params) { System.out.println(params); String name = (String) params.get("name"); - System.out.println("attemtping cast maxgoual"); + String location = (String) params.get("location"); double maxGoal = ((Number) params.get("maxGoal")).doubleValue(); + boolean urgent = (Boolean) params.get("urgent"); Need.GoalType goalType = GoalType.valueOf((String) params.get("type")); try { - Need need = cupboardService.createNeed(name, maxGoal, goalType); + Need need = cupboardService.createNeed(name, location, maxGoal, goalType, urgent); return new ResponseEntity<>(need, HttpStatus.OK); } catch (DuplicateKeyException ex) { return new ResponseEntity<>(HttpStatus.CONFLICT); diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java index c0e9214..45f1f9a 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java @@ -10,38 +10,48 @@ public class Need { } @JsonProperty("name") private String name; + @JsonProperty("location") private String location; @JsonProperty("id") private int id; @JsonProperty("filterAttributes") private String[] filterAttributes; @JsonProperty("type") final private GoalType type; @JsonProperty("maxGoal") private double maxGoal; + @JsonProperty("urgent") private boolean urgent; @JsonProperty("current") private double current; /** * Create a new need * * @param name The name of the need + * @param location The physical location of the need * @param id The unique ID of the need * @param maxGoal The maximum goal for this need * @param type The type of need (monetary, physical) + * @param urgent The urgency of the need */ - public Need(@JsonProperty("name") String name, @JsonProperty("id") int id, @JsonProperty("maxGoal") double maxGoal, GoalType type) { + public Need(@JsonProperty("name") String name, @JsonProperty("location") String location, @JsonProperty("id") int id, @JsonProperty("maxGoal") double maxGoal, GoalType type, @JsonProperty("urgent") boolean urgent) { this.id = id; + this.location = location; this.name = name; this.maxGoal = maxGoal; this.type = type; + this.urgent = urgent; } /** * Create a new need * * @param name The name of the need + * @param location The location of the need * @param maxGoal The maximum goal for this need * @param type The type of need (monetary, physical) + * @param urgency The urgency of the need */ - public Need(String name, GoalType type, double maxGoal) { + public Need(String name, String location, GoalType type, double maxGoal, boolean urgent) { this.name = name; + this.location = location; this.type = type; this.maxGoal = maxGoal; + this.urgent = urgent; } /** @@ -51,17 +61,23 @@ public class Need { */ public Need(Need other) { this.name = other.name; + this.location = other.location; this.id = other.id; this.filterAttributes = other.filterAttributes; this.type = other.type; this.maxGoal = other.maxGoal; this.current = other.current; + this.urgent = other.urgent; } public String getName() { return name; } + public String getLocation() { + return location; + } + public int getId() { return id; } @@ -82,6 +98,10 @@ public class Need { return current; } + public boolean isUrgent() { + return urgent; + } + public void setCurrent(double current) { this.current = current; } @@ -98,7 +118,15 @@ public class Need { this.name = name; } + public void setLocation(String location) { + this.location = location; + } + public void setID(int id){ this.id = id; } + + public void setUrgent(boolean urgent) { + this.urgent = urgent; + } } \ No newline at end of file diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 2398745..d09afb4 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -28,7 +28,7 @@ public class CupboardService { * @throws IOException Thrown if there was any issue saving the data * @throws DuplicateKeyException If there already exists a need with the same name */ - public Need createNeed(String name, double maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException { + public Need createNeed(String name, String location, double maxGoal, Need.GoalType goalType, boolean urgent) throws IOException, DuplicateKeyException { if (maxGoal <= 0) { throw new IllegalArgumentException("Max Goal must be greater than zero"); @@ -40,7 +40,7 @@ public class CupboardService { } } - Need need = new Need(name, goalType, maxGoal); + Need need = new Need(name, location, goalType, maxGoal, urgent); return cupboardDAO.addNeed(need); } -- 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 --- .../ufundapi/controller/CupboardController.java | 2 +- .../controller/CupboardControllerTest.java | 137 ++++++++++++++++++++- 2 files changed, 135 insertions(+), 4 deletions(-) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index 55ee457..bbfd3f6 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -182,7 +182,7 @@ public class CupboardController { /** * Checks out a need by checkoutAmount * - * @param data JSON object with paramters needID and amount + * @param data JSON object with parameters needID and amount * @param key Key used to authenticate user * @return OK if successful, other statuses if failure */ 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 c44eb965652a96498bff057d6d282af42196473e Mon Sep 17 00:00:00 2001 From: benal01 Date: Sun, 30 Mar 2025 16:22:02 -0400 Subject: 23 new needs that adhere to the updated structure --- ufund-api/data/cupboard.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ufund-api') diff --git a/ufund-api/data/cupboard.json b/ufund-api/data/cupboard.json index abc2293..3d49031 100644 --- a/ufund-api/data/cupboard.json +++ b/ufund-api/data/cupboard.json @@ -1 +1 @@ -[{"name":"Jellyfish Hats","id":26,"maxGoal":10.0,"type":"MONETARY","filterAttributes":["#savethejellyfish","Clothing","Storefront"],"current":0.0},{"name":"Pollution Re-Filtering","id":27,"maxGoal":1.0E7,"type":"MONETARY","filterAttributes":["Cleanup","Donations","Third-Party"],"current":0.0},{"name":"Coral re-re-habilitation","id":28,"maxGoal":10000.0,"type":"MONETARY","filterAttributes":["Preservation","#savethecoral","#helloPhil"],"current":0.0}] \ No newline at end of file +[{"name":"Pollution Filters","location":"New York Harbor","id":5,"maxGoal":1000.0,"type":"PHYSICAL","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Diving Equipment","location":"Gulf of Mexico","id":6,"maxGoal":50.0,"type":"PHYSICAL","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Life Straw","location":"RIT","id":7,"maxGoal":1.0,"type":"PHYSICAL","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Dog Fish research","location":"RIT","id":8,"maxGoal":1000000.0,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Jellyfish Glue","location":"Pacific","id":9,"maxGoal":100000.0,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Fish Food","location":"","id":10,"maxGoal":1000.0,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Harpoons","location":"Atlantic City","id":11,"maxGoal":900.0,"type":"PHYSICAL","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Sea Urchin Hats","location":"Seaworld","id":12,"maxGoal":90.0,"type":"PHYSICAL","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Awareness Exhibit","location":"Seneca Park Zoo","id":13,"maxGoal":1.0E7,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0},{"name":"New Whale","location":"Seneca Park Zoo","id":14,"maxGoal":1.0,"type":"PHYSICAL","urgent":false,"filterAttributes":null,"current":0.0},{"name":"New Gosnell Algae Filter","location":"Gosnell College","id":15,"maxGoal":40.0,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Awareness Lunches","location":"Colleges and Highschools","id":16,"maxGoal":5000.0,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Fish Column for RIT Tours","location":"Wallace Library","id":17,"maxGoal":2.0E7,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Submarine Matinience","location":"New York Harbor","id":18,"maxGoal":1000000.0,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Volunteer Lunches ","location":"Lake Ontario","id":19,"maxGoal":150.0,"type":"PHYSICAL","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Volunteer Misc. Equipment","location":"Lake Ontario","id":20,"maxGoal":2500.0,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Invasive eel removal","location":"Pacific Seafloor","id":21,"maxGoal":1.0E8,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Fishing Liscense Enforcement","location":"Rochester Bridges","id":22,"maxGoal":10000.0,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Waste Runoff Management","location":"RIT Watershed","id":23,"maxGoal":98000.0,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Lobbying for anti-dynamite fishing legislation","location":"Washington DC","id":24,"maxGoal":50000.0,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Lobbying for better fishing practice legislation","location":"Washington DC","id":25,"maxGoal":65000.0,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Bioluminescence Tunnel","location":"Golisano College of Computing","id":26,"maxGoal":2.8E7,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Pollution awareness campain","location":"Middle and Highschools","id":27,"maxGoal":35000.0,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0}] \ No newline at end of file -- 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 --- .../ufundapi/controller/CupboardController.java | 2 - .../java/com/ufund/api/ufundapi/model/Need.java | 18 +--- .../api/ufundapi/service/CupboardService.java | 2 +- .../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 +++++++++++-------- 8 files changed, 167 insertions(+), 109 deletions(-) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index c62bff3..d426aee 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -106,8 +106,6 @@ public class CupboardController { * * @param name The name parameter which contains the text used to find the {@link Need need} * - * @deprecated Searching should now be done client side in the future - * * @return ResponseEntity with array of {@link Need need} objects (may be empty) and * HTTP status of OK
* ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java index 00cd38f..55a9441 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java @@ -46,7 +46,7 @@ public class Need { * @param type The type of need (monetary, physical) * @param urgent The urgency of the need */ - public Need(String name, String location, GoalType type, double maxGoal, boolean urgent) { + public Need(String name, String location, double maxGoal, GoalType type, boolean urgent) { this.name = name; this.location = location; this.type = type; @@ -74,10 +74,6 @@ public class Need { return name; } - public String getLocation() { - return location; - } - public int getId() { return id; } @@ -98,10 +94,6 @@ public class Need { return current; } - public boolean isUrgent() { - return urgent; - } - public void setCurrent(double current) { this.current = current; } @@ -122,15 +114,7 @@ public class Need { this.name = name; } - public void setLocation(String location) { - this.location = location; - } - public void setID(int id){ this.id = id; } - - public void setUrgent(boolean urgent) { - this.urgent = urgent; - } } \ No newline at end of file diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 15d1fad..4dcfcad 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -43,7 +43,7 @@ public class CupboardService { } } - Need need = new Need(name, location, goalType, maxGoal, urgent); + Need need = new Need(name, location, maxGoal, goalType, urgent); return cupboardDAO.addNeed(need); } 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') 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 6bfbf7fa3b5b14b04f99f2dd6c33d336f6f081f6 Mon Sep 17 00:00:00 2001 From: sowgro Date: Sun, 30 Mar 2025 20:31:32 -0400 Subject: Experimental sign up page design --- ufund-api/data/cupboard.json | 2 +- ufund-api/data/userAuths.json | 2 +- ufund-api/data/users.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'ufund-api') diff --git a/ufund-api/data/cupboard.json b/ufund-api/data/cupboard.json index abc2293..b0e71ca 100644 --- a/ufund-api/data/cupboard.json +++ b/ufund-api/data/cupboard.json @@ -1 +1 @@ -[{"name":"Jellyfish Hats","id":26,"maxGoal":10.0,"type":"MONETARY","filterAttributes":["#savethejellyfish","Clothing","Storefront"],"current":0.0},{"name":"Pollution Re-Filtering","id":27,"maxGoal":1.0E7,"type":"MONETARY","filterAttributes":["Cleanup","Donations","Third-Party"],"current":0.0},{"name":"Coral re-re-habilitation","id":28,"maxGoal":10000.0,"type":"MONETARY","filterAttributes":["Preservation","#savethecoral","#helloPhil"],"current":0.0}] \ No newline at end of file +[{"name":"Jellyfish Hats","location":null,"id":26,"maxGoal":10.0,"type":"MONETARY","urgent":false,"filterAttributes":["#savethejellyfish","Clothing","Storefront"],"current":0.0},{"name":"Coral re-re-habilitation","location":null,"id":28,"maxGoal":10000.0,"type":"MONETARY","urgent":false,"filterAttributes":["Preservation","#savethecoral","#helloPhil"],"current":0.0}] \ No newline at end of file diff --git a/ufund-api/data/userAuths.json b/ufund-api/data/userAuths.json index b703ae2..f86b1a4 100644 --- a/ufund-api/data/userAuths.json +++ b/ufund-api/data/userAuths.json @@ -1 +1 @@ -[{"key":"3fdd4e7e-bc59-4e3a-ba5c-177d0833022a","username":"sowgro","expiration":"2025-04-14T18:35:26.42935739"},{"key":"5d0182e2-247e-4b4e-b165-cd95710c2402","username":"phil","expiration":"2025-04-17T08:17:09.1886771"},{"key":"13d12a6d-6825-4c1d-8b22-ba960de140b8","username":"phil","expiration":"2025-04-14T17:20:58.531711142"},{"key":"960ef9a5-021d-49a1-a752-ad9dfd8a40f3","username":"phil","expiration":"2025-04-17T10:53:51.2661748"},{"key":"20529784-361d-4111-8b2e-13a23b24d6cc","username":"phil","expiration":"2025-04-17T09:07:11.4534752"},{"key":"342977bc-2095-4325-96d8-ea4cdaa241c6","username":"phil","expiration":"2025-04-17T08:47:07.5445119"},{"key":"e2c17a24-ad11-4e63-8b01-74baf3ae6b4a","username":"phil","expiration":"2025-04-17T10:14:03.2239052"},{"key":"f9f6bc24-7a69-4bfe-92c3-c1166d20f0db","username":"phil","expiration":"2025-04-17T10:50:51.9716943"},{"key":"1dc114a0-f5d7-410e-8664-2ca9af3393ac","username":"phil","expiration":"2025-04-17T09:06:49.5204299"},{"key":"ff1e80a8-344f-4578-9ead-a6d46c8ed1c2","username":"phil","expiration":"2025-04-17T10:51:00.0607854"},{"key":"eeea7b02-7265-4a26-96de-a8ad1860c533","username":"phil","expiration":"2025-03-31T23:04:47.455490668"},{"key":"e121c7c6-e534-4fde-8a78-4f175e9db9c8","username":"phil","expiration":"2025-04-14T17:23:23.218442063"},{"key":"4df8bb43-f597-49ca-863a-6e0da5280d79","username":"phil","expiration":"2025-04-14T01:13:53.799331844"},{"key":"05e8790e-67fa-45de-adfe-82c6f5fdd15b","username":"phil","expiration":"2025-04-17T08:18:05.2696558"},{"key":"4eafe9d9-1b05-4fbd-90f1-c7f856d338dd","username":"phil","expiration":"2025-04-17T09:46:48.0123639"},{"key":"1a9b7e5a-d19c-43ef-bb02-838b6fc695e0","username":"phil","expiration":"2025-04-17T09:37:48.4955941"},{"key":"718be1e2-cfc7-44a6-b3c6-965684d1d0a9","username":"adf","expiration":"2025-04-14T18:35:58.888847176"},{"key":"85319427-4603-4a16-af33-2e9525dda8c0","username":"phil","expiration":"2025-04-14T00:39:34.952183453"},{"key":"f14f187c-355f-444a-88bf-42202f82f947","username":"phil","expiration":"2025-04-17T09:07:05.5541299"},{"key":"004f9f22-2b7e-4448-9c37-7437de47f1e0","username":"phil","expiration":"2025-04-17T08:27:28.3862592"},{"key":"24e8cf17-ad76-45b1-bfb6-79a790035231","username":"admin","expiration":"2025-04-17T08:17:27.7488131"},{"key":"f1d6a110-4232-4ef3-b6ec-9a2962664158","username":"phil","expiration":"2025-04-14T17:23:40.834526839"},{"key":"e48872fa-b89f-494a-b681-11a809d32ff4","username":"phil","expiration":"2025-04-14T17:20:23.265745224"},{"key":"31fcbc15-9902-41d2-8d6f-5b0e40ebddd2","username":"phil","expiration":"2025-04-14T16:45:41.082560826"},{"key":"98c11c42-4e7c-4601-b591-a4af1a5163f9","username":"phil","expiration":"2025-04-17T08:54:04.5078852"},{"key":"27583604-609f-4dac-bb88-01c6035c4142","username":"phil","expiration":"2025-04-17T11:15:56.8479454"},{"key":"967de418-4f86-44ac-b364-eb3a1653aa7d","username":"phil","expiration":"2025-04-17T10:54:19.237888"},{"key":"f20b64c5-a7ed-48c0-a813-6d9802cf9109","username":"phil","expiration":"2025-04-17T08:41:02.6531587"},{"key":"10ea39c2-3869-47da-8630-87b21a88681d","username":"phil","expiration":"2025-04-17T08:45:33.1781528"},{"key":"92d0d6cb-ceb0-4f84-bab3-d959dfb5df9d","username":"phil","expiration":"2025-04-17T08:30:04.2567034"},{"key":"7a7d2e8b-4242-4c18-b7dd-d0565ab5c725","username":"phil","expiration":"2025-04-17T10:10:44.3380846"},{"key":"e14f8ee5-5780-4b9b-bf34-7a41c2bbfcb4","username":"phil","expiration":"2025-04-05T13:46:10.90733016"},{"key":"e1905fc6-ca86-43d7-b1e7-60d458c75a04","username":"phil","expiration":"2025-04-17T09:12:05.990995"},{"key":"a1417644-2b43-4a18-bf5a-26bf3b7ac1fc","username":"phil","expiration":"2025-04-17T10:23:26.122743"},{"key":"7e312d55-bf3e-4dc3-b44a-74d8be591287","username":"phil","expiration":"2025-04-17T10:56:38.9103348"},{"key":"af38add5-b100-4b96-9ffb-5afaccd59979","username":"adf","expiration":"2025-04-14T18:18:47.670506361"},{"key":"4ae3922b-7d5d-45c2-83eb-f8c77e3ce218","username":"phil","expiration":"2025-04-17T08:18:53.5158112"},{"key":"0eb494c8-2fa4-4ef4-915d-4b9fcb3c75ef","username":"phil","expiration":"2025-04-17T08:24:20.412242"},{"key":"0d81e2b4-d9f2-4bc0-b93f-084c086e3707","username":"phil","expiration":"2025-04-17T08:52:41.0723771"},{"key":"04900729-cdcf-4758-9c6e-4f70f03ddb86","username":"phil","expiration":"2025-04-17T11:15:05.0632779"},{"key":"0c8da4b1-bca8-42df-a5b6-65023eadab05","username":"phil","expiration":"2025-04-17T08:15:37.7698648"},{"key":"a3703b55-c7fd-4c5a-9b6a-160c1906aa1a","username":"phil","expiration":"2025-04-17T11:35:39.9101516"},{"key":"ba426671-db67-4d2f-a701-3aa0f284f497","username":"phil","expiration":"2025-04-17T08:54:43.230802"},{"key":"639ea3fa-854a-4052-b1cf-80ea1d3d5917","username":"phil","expiration":"2025-04-17T09:38:47.9716959"},{"key":"a1825159-2e62-48a3-beba-74a3daaca5b5","username":"phil","expiration":"2025-04-17T08:12:28.2166599"},{"key":"799ed420-e843-4777-8c1a-c6d061cca773","username":"phil","expiration":"2025-04-17T08:39:38.8493536"},{"key":"cdd4df80-8139-4479-86ec-953190796d7b","username":"phil","expiration":"2025-04-17T10:02:07.6745458"},{"key":"3d3fb646-9954-496b-98bf-73136c7792ea","username":"phil","expiration":"2025-04-17T10:04:11.7737565"},{"key":"da61796e-402a-4a80-88ae-7607a37989a4","username":"phil","expiration":"2025-04-14T17:07:10.618039573"},{"key":"3beac724-e9f9-4969-9634-60826ad9db43","username":"phil","expiration":"2025-04-17T11:20:36.8424562"},{"key":"0d6e6910-b5dc-45ae-ba8e-c28604939f83","username":"phil","expiration":"2025-04-17T11:19:21.9682553"},{"key":"b7b403a0-d81f-4f7d-bd28-ccf3e2ac706b","username":"phil","expiration":"2025-04-17T10:28:53.9964744"},{"key":"125a4847-3a1c-4834-961f-7f96e997f92e","username":"sowgro","expiration":"2025-04-14T18:35:38.922687686"},{"key":"ce4accc3-0fa8-40fc-a480-1290d12caaed","username":"phil","expiration":"2025-04-17T09:12:58.9504986"},{"key":"adc5ce2f-5a40-4ee5-b9a6-6154860e6861","username":"phil","expiration":"2025-04-17T08:28:48.4584821"},{"key":"2aeaab28-99c9-4b45-bdef-82096c70945e","username":"phil","expiration":"2025-04-14T17:19:48.552121268"},{"key":"7805de89-9b38-46dc-8f59-09c06ef9d2dd","username":"phil","expiration":"2025-04-17T08:52:44.6283659"},{"key":"ad6d92d4-c496-407c-823a-edaa386e67ed","username":"phil","expiration":"2025-04-14T17:07:36.032623002"},{"key":"fbdf7ac2-cf01-4dad-baec-ed9310a4eba7","username":"phil","expiration":"2025-04-17T08:38:12.8162926"},{"key":"7907ccb2-bf01-4962-a280-ebb6aa9c5b20","username":"phil","expiration":"2025-04-17T09:45:01.6777324"},{"key":"a69796dd-734c-4545-ac91-e5fe0387d0ad","username":"phil","expiration":"2025-04-17T08:29:32.671782"},{"key":"9f3e380d-fead-4d40-a1c0-278e857dd674","username":"phil","expiration":"2025-04-17T08:52:29.4148814"},{"key":"efc531fb-ab24-4d5a-a2f5-7f4ede74819f","username":"phil","expiration":"2025-04-13T19:41:51.017327545"},{"key":"88b539a9-3986-41b4-a6ed-a79672042ccf","username":"phil","expiration":"2025-04-17T08:23:45.5712888"},{"key":"68bc53af-e21a-4364-adf5-8f163f642235","username":"phil","expiration":"2025-04-17T08:24:43.3201656"},{"key":"a07ae51f-f80b-4001-95f1-48c11d4917a4","username":"phil","expiration":"2025-04-05T15:04:30.900359001"},{"key":"cc49c007-fd36-4828-b8fa-f5b85ad0676d","username":"phil","expiration":"2025-04-14T16:46:12.80566798"},{"key":"fb2d7dd5-783e-47d8-9a43-8b7693c5f070","username":"phil","expiration":"2025-04-17T11:13:05.9489267"},{"key":"49a0ad40-3223-4f62-94e8-0f9e96241a85","username":"phil","expiration":"2025-04-17T09:13:39.9633546"},{"key":"db53acf2-61d1-45ea-9d01-5c710b80bdaf","username":"phil","expiration":"2025-04-17T08:53:47.487545"},{"key":"d7cef571-0f76-49fe-941f-ecbeae69557a","username":"phil","expiration":"2025-04-05T15:14:00.363201102"},{"key":"3fc557b6-0306-4779-9b74-b7292a5cf1cc","username":"phil","expiration":"2025-04-14T16:06:08.564069822"},{"key":"77392d17-6e0c-45ec-857d-6595a55ddd97","username":"phil","expiration":"2025-04-14T16:06:48.335542315"},{"key":"58e4e2a2-3a36-4fd6-8bb1-ad0831664d01","username":"phil","expiration":"2025-04-12T23:17:42.638952959"},{"key":"6083ae1d-a761-4ed3-8c48-a429afa2c521","username":"phil","expiration":"2025-04-17T08:32:25.3500545"},{"key":"1c438301-e8f5-4c12-a40b-e20b8a282814","username":"keshey","expiration":"2025-04-17T10:26:04.7690496"},{"key":"fe2146e2-7982-4226-b215-96879939f485","username":"phil","expiration":"2025-04-17T08:34:59.3412483"},{"key":"6d2ea170-50aa-4c48-9247-9310a29ae753","username":"phil","expiration":"2025-04-17T11:29:10.0637357"},{"key":"f5f53053-ef5e-4850-93a0-3dc20646f78b","username":"sowgro","expiration":"2025-04-14T18:11:29.438554549"},{"key":"03424ad1-376c-47aa-8553-7f2ea8099d45","username":"phil","expiration":"2025-04-17T08:05:35.7264696"},{"key":"568e4738-70b5-4be7-bfa6-1367cd22ce3f","username":"admin","expiration":"2025-04-17T08:16:59.0542222"},{"key":"7a634e0a-628b-4b31-8950-dc33d4ee5d95","username":"phil","expiration":"2025-04-17T08:15:47.1663258"},{"key":"9c6a36b8-7f71-4b09-b26b-682f1f0be4cb","username":"phil","expiration":"2025-04-17T09:12:15.5425885"},{"key":"f6951471-2578-4a6a-b3e5-b7e97ed9207a","username":"phil","expiration":"2025-04-17T10:23:45.9450825"},{"key":"cb658550-aafc-4a45-89be-f7899e44d7ba","username":"phil","expiration":"2025-04-17T10:50:40.0249774"}] \ No newline at end of file +[{"key":"3fdd4e7e-bc59-4e3a-ba5c-177d0833022a","username":"sowgro","expiration":"2025-04-14T18:35:26.42935739"},{"key":"5d0182e2-247e-4b4e-b165-cd95710c2402","username":"phil","expiration":"2025-04-17T08:17:09.1886771"},{"key":"13d12a6d-6825-4c1d-8b22-ba960de140b8","username":"phil","expiration":"2025-04-14T17:20:58.531711142"},{"key":"960ef9a5-021d-49a1-a752-ad9dfd8a40f3","username":"phil","expiration":"2025-04-17T10:53:51.2661748"},{"key":"20529784-361d-4111-8b2e-13a23b24d6cc","username":"phil","expiration":"2025-04-17T09:07:11.4534752"},{"key":"342977bc-2095-4325-96d8-ea4cdaa241c6","username":"phil","expiration":"2025-04-17T08:47:07.5445119"},{"key":"e2c17a24-ad11-4e63-8b01-74baf3ae6b4a","username":"phil","expiration":"2025-04-17T10:14:03.2239052"},{"key":"f9f6bc24-7a69-4bfe-92c3-c1166d20f0db","username":"phil","expiration":"2025-04-17T10:50:51.9716943"},{"key":"1dc114a0-f5d7-410e-8664-2ca9af3393ac","username":"phil","expiration":"2025-04-17T09:06:49.5204299"},{"key":"ff1e80a8-344f-4578-9ead-a6d46c8ed1c2","username":"phil","expiration":"2025-04-17T10:51:00.0607854"},{"key":"eeea7b02-7265-4a26-96de-a8ad1860c533","username":"phil","expiration":"2025-03-31T23:04:47.455490668"},{"key":"e121c7c6-e534-4fde-8a78-4f175e9db9c8","username":"phil","expiration":"2025-04-14T17:23:23.218442063"},{"key":"4df8bb43-f597-49ca-863a-6e0da5280d79","username":"phil","expiration":"2025-04-14T01:13:53.799331844"},{"key":"05e8790e-67fa-45de-adfe-82c6f5fdd15b","username":"phil","expiration":"2025-04-17T08:18:05.2696558"},{"key":"4eafe9d9-1b05-4fbd-90f1-c7f856d338dd","username":"phil","expiration":"2025-04-17T09:46:48.0123639"},{"key":"1a9b7e5a-d19c-43ef-bb02-838b6fc695e0","username":"phil","expiration":"2025-04-17T09:37:48.4955941"},{"key":"718be1e2-cfc7-44a6-b3c6-965684d1d0a9","username":"adf","expiration":"2025-04-14T18:35:58.888847176"},{"key":"85319427-4603-4a16-af33-2e9525dda8c0","username":"phil","expiration":"2025-04-14T00:39:34.952183453"},{"key":"f14f187c-355f-444a-88bf-42202f82f947","username":"phil","expiration":"2025-04-17T09:07:05.5541299"},{"key":"004f9f22-2b7e-4448-9c37-7437de47f1e0","username":"phil","expiration":"2025-04-17T08:27:28.3862592"},{"key":"24e8cf17-ad76-45b1-bfb6-79a790035231","username":"admin","expiration":"2025-04-17T08:17:27.7488131"},{"key":"f1d6a110-4232-4ef3-b6ec-9a2962664158","username":"phil","expiration":"2025-04-14T17:23:40.834526839"},{"key":"e48872fa-b89f-494a-b681-11a809d32ff4","username":"phil","expiration":"2025-04-14T17:20:23.265745224"},{"key":"31fcbc15-9902-41d2-8d6f-5b0e40ebddd2","username":"phil","expiration":"2025-04-14T16:45:41.082560826"},{"key":"98c11c42-4e7c-4601-b591-a4af1a5163f9","username":"phil","expiration":"2025-04-17T08:54:04.5078852"},{"key":"27583604-609f-4dac-bb88-01c6035c4142","username":"phil","expiration":"2025-04-17T11:15:56.8479454"},{"key":"967de418-4f86-44ac-b364-eb3a1653aa7d","username":"phil","expiration":"2025-04-17T10:54:19.237888"},{"key":"f20b64c5-a7ed-48c0-a813-6d9802cf9109","username":"phil","expiration":"2025-04-17T08:41:02.6531587"},{"key":"10ea39c2-3869-47da-8630-87b21a88681d","username":"phil","expiration":"2025-04-17T08:45:33.1781528"},{"key":"92d0d6cb-ceb0-4f84-bab3-d959dfb5df9d","username":"phil","expiration":"2025-04-17T08:30:04.2567034"},{"key":"7a7d2e8b-4242-4c18-b7dd-d0565ab5c725","username":"phil","expiration":"2025-04-17T10:10:44.3380846"},{"key":"e14f8ee5-5780-4b9b-bf34-7a41c2bbfcb4","username":"phil","expiration":"2025-04-05T13:46:10.90733016"},{"key":"e1905fc6-ca86-43d7-b1e7-60d458c75a04","username":"phil","expiration":"2025-04-17T09:12:05.990995"},{"key":"a1417644-2b43-4a18-bf5a-26bf3b7ac1fc","username":"phil","expiration":"2025-04-17T10:23:26.122743"},{"key":"7e312d55-bf3e-4dc3-b44a-74d8be591287","username":"phil","expiration":"2025-04-17T10:56:38.9103348"},{"key":"af38add5-b100-4b96-9ffb-5afaccd59979","username":"adf","expiration":"2025-04-14T18:18:47.670506361"},{"key":"4ae3922b-7d5d-45c2-83eb-f8c77e3ce218","username":"phil","expiration":"2025-04-17T08:18:53.5158112"},{"key":"0eb494c8-2fa4-4ef4-915d-4b9fcb3c75ef","username":"phil","expiration":"2025-04-17T08:24:20.412242"},{"key":"0d81e2b4-d9f2-4bc0-b93f-084c086e3707","username":"phil","expiration":"2025-04-17T08:52:41.0723771"},{"key":"04900729-cdcf-4758-9c6e-4f70f03ddb86","username":"phil","expiration":"2025-04-17T11:15:05.0632779"},{"key":"0c8da4b1-bca8-42df-a5b6-65023eadab05","username":"phil","expiration":"2025-04-17T08:15:37.7698648"},{"key":"a3703b55-c7fd-4c5a-9b6a-160c1906aa1a","username":"phil","expiration":"2025-04-17T11:35:39.9101516"},{"key":"ba426671-db67-4d2f-a701-3aa0f284f497","username":"phil","expiration":"2025-04-17T08:54:43.230802"},{"key":"639ea3fa-854a-4052-b1cf-80ea1d3d5917","username":"phil","expiration":"2025-04-17T09:38:47.9716959"},{"key":"a1825159-2e62-48a3-beba-74a3daaca5b5","username":"phil","expiration":"2025-04-17T08:12:28.2166599"},{"key":"799ed420-e843-4777-8c1a-c6d061cca773","username":"phil","expiration":"2025-04-17T08:39:38.8493536"},{"key":"cdd4df80-8139-4479-86ec-953190796d7b","username":"phil","expiration":"2025-04-17T10:02:07.6745458"},{"key":"3d3fb646-9954-496b-98bf-73136c7792ea","username":"phil","expiration":"2025-04-17T10:04:11.7737565"},{"key":"da61796e-402a-4a80-88ae-7607a37989a4","username":"phil","expiration":"2025-04-14T17:07:10.618039573"},{"key":"3beac724-e9f9-4969-9634-60826ad9db43","username":"phil","expiration":"2025-04-17T11:20:36.8424562"},{"key":"0d6e6910-b5dc-45ae-ba8e-c28604939f83","username":"phil","expiration":"2025-04-17T11:19:21.9682553"},{"key":"b7b403a0-d81f-4f7d-bd28-ccf3e2ac706b","username":"phil","expiration":"2025-04-17T10:28:53.9964744"},{"key":"125a4847-3a1c-4834-961f-7f96e997f92e","username":"sowgro","expiration":"2025-04-14T18:35:38.922687686"},{"key":"ce4accc3-0fa8-40fc-a480-1290d12caaed","username":"phil","expiration":"2025-04-17T09:12:58.9504986"},{"key":"adc5ce2f-5a40-4ee5-b9a6-6154860e6861","username":"phil","expiration":"2025-04-17T08:28:48.4584821"},{"key":"50019b5d-25ad-40fe-bd21-19d4dbe57a92","username":"phil","expiration":"2025-04-29T20:23:12.415416825"},{"key":"2aeaab28-99c9-4b45-bdef-82096c70945e","username":"phil","expiration":"2025-04-14T17:19:48.552121268"},{"key":"7805de89-9b38-46dc-8f59-09c06ef9d2dd","username":"phil","expiration":"2025-04-17T08:52:44.6283659"},{"key":"ad6d92d4-c496-407c-823a-edaa386e67ed","username":"phil","expiration":"2025-04-14T17:07:36.032623002"},{"key":"fbdf7ac2-cf01-4dad-baec-ed9310a4eba7","username":"phil","expiration":"2025-04-17T08:38:12.8162926"},{"key":"7907ccb2-bf01-4962-a280-ebb6aa9c5b20","username":"phil","expiration":"2025-04-17T09:45:01.6777324"},{"key":"904f6de1-10ab-465d-abd8-be0612311251","username":"phil","expiration":"2025-04-29T20:24:45.666998397"},{"key":"a69796dd-734c-4545-ac91-e5fe0387d0ad","username":"phil","expiration":"2025-04-17T08:29:32.671782"},{"key":"9f3e380d-fead-4d40-a1c0-278e857dd674","username":"phil","expiration":"2025-04-17T08:52:29.4148814"},{"key":"efc531fb-ab24-4d5a-a2f5-7f4ede74819f","username":"phil","expiration":"2025-04-13T19:41:51.017327545"},{"key":"88b539a9-3986-41b4-a6ed-a79672042ccf","username":"phil","expiration":"2025-04-17T08:23:45.5712888"},{"key":"68bc53af-e21a-4364-adf5-8f163f642235","username":"phil","expiration":"2025-04-17T08:24:43.3201656"},{"key":"a07ae51f-f80b-4001-95f1-48c11d4917a4","username":"phil","expiration":"2025-04-05T15:04:30.900359001"},{"key":"cc49c007-fd36-4828-b8fa-f5b85ad0676d","username":"phil","expiration":"2025-04-14T16:46:12.80566798"},{"key":"fb2d7dd5-783e-47d8-9a43-8b7693c5f070","username":"phil","expiration":"2025-04-17T11:13:05.9489267"},{"key":"49a0ad40-3223-4f62-94e8-0f9e96241a85","username":"phil","expiration":"2025-04-17T09:13:39.9633546"},{"key":"db53acf2-61d1-45ea-9d01-5c710b80bdaf","username":"phil","expiration":"2025-04-17T08:53:47.487545"},{"key":"d7cef571-0f76-49fe-941f-ecbeae69557a","username":"phil","expiration":"2025-04-05T15:14:00.363201102"},{"key":"25d6a49b-c185-460c-adbe-a874419d20aa","username":"phil","expiration":"2025-04-29T20:22:19.629805123"},{"key":"3fc557b6-0306-4779-9b74-b7292a5cf1cc","username":"phil","expiration":"2025-04-14T16:06:08.564069822"},{"key":"77392d17-6e0c-45ec-857d-6595a55ddd97","username":"phil","expiration":"2025-04-14T16:06:48.335542315"},{"key":"58e4e2a2-3a36-4fd6-8bb1-ad0831664d01","username":"phil","expiration":"2025-04-12T23:17:42.638952959"},{"key":"6083ae1d-a761-4ed3-8c48-a429afa2c521","username":"phil","expiration":"2025-04-17T08:32:25.3500545"},{"key":"1c438301-e8f5-4c12-a40b-e20b8a282814","username":"keshey","expiration":"2025-04-17T10:26:04.7690496"},{"key":"fe2146e2-7982-4226-b215-96879939f485","username":"phil","expiration":"2025-04-17T08:34:59.3412483"},{"key":"6d2ea170-50aa-4c48-9247-9310a29ae753","username":"phil","expiration":"2025-04-17T11:29:10.0637357"},{"key":"f5f53053-ef5e-4850-93a0-3dc20646f78b","username":"sowgro","expiration":"2025-04-14T18:11:29.438554549"},{"key":"03424ad1-376c-47aa-8553-7f2ea8099d45","username":"phil","expiration":"2025-04-17T08:05:35.7264696"},{"key":"568e4738-70b5-4be7-bfa6-1367cd22ce3f","username":"admin","expiration":"2025-04-17T08:16:59.0542222"},{"key":"7a634e0a-628b-4b31-8950-dc33d4ee5d95","username":"phil","expiration":"2025-04-17T08:15:47.1663258"},{"key":"9c6a36b8-7f71-4b09-b26b-682f1f0be4cb","username":"phil","expiration":"2025-04-17T09:12:15.5425885"},{"key":"f6951471-2578-4a6a-b3e5-b7e97ed9207a","username":"phil","expiration":"2025-04-17T10:23:45.9450825"},{"key":"cb658550-aafc-4a45-89be-f7899e44d7ba","username":"phil","expiration":"2025-04-17T10:50:40.0249774"}] \ No newline at end of file diff --git a/ufund-api/data/users.json b/ufund-api/data/users.json index 5b2457f..9bddb7d 100644 --- a/ufund-api/data/users.json +++ b/ufund-api/data/users.json @@ -1 +1 @@ -[{"username":"keshey","passwordHash":-1134843357,"basket":[],"type":"HELPER"},{"username":"admin","passwordHash":92668751,"basket":[],"type":"MANAGER"},{"username":"phil","passwordHash":-1054080181,"basket":[22,26],"type":"HELPER"}] \ No newline at end of file +[{"username":"keshey","passwordHash":-1134843357,"basket":[],"type":"HELPER"},{"username":"admin","passwordHash":92668751,"basket":[],"type":"MANAGER"},{"username":"phil","passwordHash":-1054080181,"basket":[],"type":"HELPER"}] \ No newline at end of file -- 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. --- .../api/ufundapi/controller/AuthController.java | 6 +-- .../ufundapi/controller/CupboardController.java | 54 +++++++++++----------- .../api/ufundapi/controller/UserController.java | 26 +++++------ .../controller/CupboardControllerTest.java | 8 ++-- .../ufundapi/controller/UserControllerTest.java | 34 +++++++------- 5 files changed, 64 insertions(+), 64 deletions(-) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java index aa99a90..82b2c67 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java @@ -43,10 +43,10 @@ public class AuthController { return new ResponseEntity<>(key, HttpStatus.OK); } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -64,7 +64,7 @@ public class AuthController { return new ResponseEntity<>(HttpStatus.OK); } catch (IOException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index d426aee..cce016c 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -51,7 +51,7 @@ public class CupboardController { * INTERNAL_SERVER_ERROR otherwise */ @PostMapping("") - public ResponseEntity createNeed(@RequestBody Map params, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity createNeed(@RequestBody Map params, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO, "POST /cupboard body={0}", params); String name = (String) params.get("name"); @@ -66,16 +66,16 @@ public class CupboardController { return new ResponseEntity<>(need, HttpStatus.OK); } catch (DuplicateKeyException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.CONFLICT); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.CONFLICT); } catch (IllegalArgumentException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -88,15 +88,15 @@ public class CupboardController { * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise */ @GetMapping("") - public ResponseEntity getNeeds() { + public ResponseEntity getNeeds() { LOG.info("GET /cupboard"); try { Need[] needs = cupboardService.getNeeds(); return new ResponseEntity<>(needs, HttpStatus.OK); - } catch (IOException e) { - LOG.log(Level.SEVERE, e.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -112,15 +112,15 @@ public class CupboardController { *

*/ @GetMapping("/") - public ResponseEntity searchNeeds(@RequestParam String name) { + public ResponseEntity searchNeeds(@RequestParam String name) { LOG.info("GET /cupboard/?name="+name); try { Need[] needs = cupboardService.searchNeeds(name); return new ResponseEntity<>(needs, HttpStatus.OK); - } catch (IOException e) { - LOG.log(Level.SEVERE,e.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } catch (IOException ex) { + LOG.log(Level.SEVERE,ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -133,7 +133,7 @@ public class CupboardController { * ResponseEntity with HTTP status of NOT_FOUND if not found
*/ @GetMapping("/{id}") - public ResponseEntity getNeed(@PathVariable int id) { + public ResponseEntity getNeed(@PathVariable int id) { LOG.log(Level.INFO, "GET /cupboard/{0}", id); try { @@ -143,9 +143,9 @@ public class CupboardController { } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - } catch (IOException e) { - LOG.log(Level.SEVERE, e.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -157,7 +157,7 @@ public class CupboardController { * @return OK response and the need if it was successful, or INTERNAL_SERVER_ERROR if there was an issue */ @PutMapping("/{id}") - public ResponseEntity updateNeed(@RequestBody Need need, @PathVariable int id, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity updateNeed(@RequestBody Need need, @PathVariable int id, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO, "PUT /cupboard/{0} body={1}", of(id, need)); try { authService.keyHasAccessToCupboard(key); @@ -169,13 +169,13 @@ public class CupboardController { } } catch (IllegalArgumentException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -196,13 +196,13 @@ public class CupboardController { return new ResponseEntity<>(HttpStatus.OK); } catch (IllegalArgumentException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -213,7 +213,7 @@ public class CupboardController { * @return OK if the need was deleted, NOT_FOUND if the need was not found, or INTERNAL_SERVER_ERROR if an error occurred */ @DeleteMapping("/{id}") - public ResponseEntity deleteNeed(@PathVariable int id, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity deleteNeed(@PathVariable int id, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO, "DELETE /cupboard/{0}", id); try { authService.keyHasAccessToCupboard(key); @@ -225,10 +225,10 @@ public class CupboardController { } } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } 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 33d2e4f..a34e891 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 @@ -41,7 +41,7 @@ public class UserController { * otherwise */ @PostMapping("") - public ResponseEntity createUser(@RequestBody Map params) { + public ResponseEntity createUser(@RequestBody Map params) { LOG.log(Level.INFO, "POST /users body={0}", params); String username = params.get("username"); String password = params.get("password"); @@ -55,10 +55,10 @@ public class UserController { } } catch (DuplicateKeyException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.CONFLICT); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.CONFLICT); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -73,7 +73,7 @@ public class UserController { * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise */ @GetMapping("/{username}") - public ResponseEntity getUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity getUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO, "GET /user/{0} key={1}", of(username, key)); try { @@ -86,10 +86,10 @@ public class UserController { } } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -104,7 +104,7 @@ public class UserController { * INTERNAL_SERVER_ERROR if there was an issue */ @PutMapping("/{username}") - public ResponseEntity updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO,"PUT /users/{0} body={1} key={2}", of(username, user, key)); try { authService.keyHasAccessToUser(username, key); @@ -116,13 +116,13 @@ public class UserController { } } catch (IllegalArgumentException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -135,7 +135,7 @@ public class UserController { * INTERNAL_SERVER_ERROR if an error occurred */ @DeleteMapping("/{username}") - public ResponseEntity deleteUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity deleteUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO, "DELETE /users/{0} id={1}", of(username, key)); try { @@ -147,10 +147,10 @@ public class UserController { } } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } 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 eb09f05ca697a3a6d3587f9278e332056bfd6f66 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Sun, 30 Mar 2025 20:48:32 -0400 Subject: Modified controllers to return error text when catching errors. Also added additional error checking to CupboardService for physical needs. --- .../src/main/java/com/ufund/api/ufundapi/service/CupboardService.java | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 4dcfcad..a86fe28 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -35,6 +35,8 @@ public class CupboardService { if (maxGoal <= 0) { throw new IllegalArgumentException("Max Goal must be greater than zero"); + } else if (goalType.equals(Need.GoalType.PHYSICAL) && maxGoal % 1 != 0) { + throw new IllegalArgumentException("Cannot have non whole number value for physical goal"); } for (Need searchNeed : cupboardDAO.getNeeds()) { @@ -95,6 +97,8 @@ public class CupboardService { } if (need.getMaxGoal() <= 0) { throw new IllegalArgumentException("Goal must be greater than 0"); + } else if (need.getType().equals(Need.GoalType.PHYSICAL) && need.getMaxGoal() % 1 != 0) { + throw new IllegalArgumentException("Cannot have non whole number value for physical goal"); } return cupboardDAO.updateNeed(need); } -- cgit v1.2.3 From 227360990424abe323c44664b1d58d667b89a92f Mon Sep 17 00:00:00 2001 From: benal01 Date: Mon, 31 Mar 2025 10:10:41 -0400 Subject: backend support for description --- .../com/ufund/api/ufundapi/controller/CupboardController.java | 5 +++-- .../src/main/java/com/ufund/api/ufundapi/model/Need.java | 11 +++++++++-- .../java/com/ufund/api/ufundapi/service/CupboardService.java | 7 +++++-- 3 files changed, 17 insertions(+), 6 deletions(-) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index d426aee..ea86aa8 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -5,7 +5,6 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; -import com.ufund.api.ufundapi.service.AuthService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; @@ -22,6 +21,7 @@ import org.springframework.web.bind.annotation.RestController; import com.ufund.api.ufundapi.DuplicateKeyException; import com.ufund.api.ufundapi.model.Need; import com.ufund.api.ufundapi.model.Need.GoalType; +import com.ufund.api.ufundapi.service.AuthService; import com.ufund.api.ufundapi.service.CupboardService; @RestController @@ -58,11 +58,12 @@ public class CupboardController { String location = (String) params.get("location"); double maxGoal = ((Number) params.get("maxGoal")).doubleValue(); boolean urgent = (Boolean) params.get("urgent"); + String description = (String) params.get("description"); Need.GoalType goalType = GoalType.valueOf((String) params.get("type")); try { authService.keyHasAccessToCupboard(key); - Need need = cupboardService.createNeed(name, location, maxGoal, goalType, urgent); + Need need = cupboardService.createNeed(name, location, maxGoal, goalType, urgent, description); return new ResponseEntity<>(need, HttpStatus.OK); } catch (DuplicateKeyException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java index 55a9441..35e81b3 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java @@ -17,6 +17,7 @@ public class Need { @JsonProperty("maxGoal") private double maxGoal; @JsonProperty("urgent") private boolean urgent; @JsonProperty("current") private double current; + @JsonProperty("description") private String description; /** * Create a new need, used by the controller @@ -27,14 +28,16 @@ public class Need { * @param maxGoal The maximum goal for this need * @param type The type of need (monetary, physical) * @param urgent The urgency of the need + * @param description The description of the need */ - public Need(@JsonProperty("name") String name, @JsonProperty("location") String location, @JsonProperty("id") int id, @JsonProperty("maxGoal") double maxGoal, @JsonProperty("type") GoalType type, @JsonProperty("urgent") boolean urgent) { + public Need(@JsonProperty("name") String name, @JsonProperty("location") String location, @JsonProperty("id") int id, @JsonProperty("maxGoal") double maxGoal, @JsonProperty("type") GoalType type, @JsonProperty("urgent") boolean urgent, @JsonProperty("Description") String description) { this.id = id; this.location = location; this.name = name; this.maxGoal = maxGoal; this.type = type; this.urgent = urgent; + this.description = description; } /** @@ -45,13 +48,15 @@ public class Need { * @param maxGoal The maximum goal for this need * @param type The type of need (monetary, physical) * @param urgent The urgency of the need + * @param description The description of the need */ - public Need(String name, String location, double maxGoal, GoalType type, boolean urgent) { + public Need(String name, String location, double maxGoal, GoalType type, boolean urgent, String description) { this.name = name; this.location = location; this.type = type; this.maxGoal = maxGoal; this.urgent = urgent; + this.description = description; } /** @@ -68,6 +73,7 @@ public class Need { this.maxGoal = other.maxGoal; this.current = other.current; this.urgent = other.urgent; + this.description = other.description; } public String getName() { @@ -94,6 +100,7 @@ public class Need { return current; } + public void setCurrent(double current) { this.current = current; } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 4dcfcad..d216b01 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -25,13 +25,16 @@ public class CupboardService { * Creates a new Need * * @param name The name of the need to create + * @param location The location of the new need * @param maxGoal The max goal of the new need * @param goalType The goal type of the new need + * @param urgent The urgency of the new need + * @param description The description of the new need * @return The need that was created * @throws IOException Thrown if there was any issue saving the data * @throws DuplicateKeyException If there already exists a need with the same name */ - public Need createNeed(String name, String location, double maxGoal, Need.GoalType goalType, boolean urgent) throws IOException, DuplicateKeyException { + public Need createNeed(String name, String location, double maxGoal, Need.GoalType goalType, boolean urgent, String description) throws IOException, DuplicateKeyException { if (maxGoal <= 0) { throw new IllegalArgumentException("Max Goal must be greater than zero"); @@ -43,7 +46,7 @@ public class CupboardService { } } - Need need = new Need(name, location, maxGoal, goalType, urgent); + Need need = new Need(name, location, maxGoal, goalType, urgent, description); return cupboardDAO.addNeed(need); } -- cgit v1.2.3 From ab07239e9daa1d86f89fc53a8ad5beb6c580c517 Mon Sep 17 00:00:00 2001 From: benal01 Date: Mon, 31 Mar 2025 17:42:49 -0400 Subject: descriptions for current needs --- ufund-api/data/cupboard.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ufund-api') diff --git a/ufund-api/data/cupboard.json b/ufund-api/data/cupboard.json index 3d49031..0e3917f 100644 --- a/ufund-api/data/cupboard.json +++ b/ufund-api/data/cupboard.json @@ -1 +1 @@ -[{"name":"Pollution Filters","location":"New York Harbor","id":5,"maxGoal":1000.0,"type":"PHYSICAL","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Diving Equipment","location":"Gulf of Mexico","id":6,"maxGoal":50.0,"type":"PHYSICAL","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Life Straw","location":"RIT","id":7,"maxGoal":1.0,"type":"PHYSICAL","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Dog Fish research","location":"RIT","id":8,"maxGoal":1000000.0,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Jellyfish Glue","location":"Pacific","id":9,"maxGoal":100000.0,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Fish Food","location":"","id":10,"maxGoal":1000.0,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Harpoons","location":"Atlantic City","id":11,"maxGoal":900.0,"type":"PHYSICAL","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Sea Urchin Hats","location":"Seaworld","id":12,"maxGoal":90.0,"type":"PHYSICAL","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Awareness Exhibit","location":"Seneca Park Zoo","id":13,"maxGoal":1.0E7,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0},{"name":"New Whale","location":"Seneca Park Zoo","id":14,"maxGoal":1.0,"type":"PHYSICAL","urgent":false,"filterAttributes":null,"current":0.0},{"name":"New Gosnell Algae Filter","location":"Gosnell College","id":15,"maxGoal":40.0,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Awareness Lunches","location":"Colleges and Highschools","id":16,"maxGoal":5000.0,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Fish Column for RIT Tours","location":"Wallace Library","id":17,"maxGoal":2.0E7,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Submarine Matinience","location":"New York Harbor","id":18,"maxGoal":1000000.0,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Volunteer Lunches ","location":"Lake Ontario","id":19,"maxGoal":150.0,"type":"PHYSICAL","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Volunteer Misc. Equipment","location":"Lake Ontario","id":20,"maxGoal":2500.0,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Invasive eel removal","location":"Pacific Seafloor","id":21,"maxGoal":1.0E8,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Fishing Liscense Enforcement","location":"Rochester Bridges","id":22,"maxGoal":10000.0,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Waste Runoff Management","location":"RIT Watershed","id":23,"maxGoal":98000.0,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Lobbying for anti-dynamite fishing legislation","location":"Washington DC","id":24,"maxGoal":50000.0,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Lobbying for better fishing practice legislation","location":"Washington DC","id":25,"maxGoal":65000.0,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0},{"name":"Bioluminescence Tunnel","location":"Golisano College of Computing","id":26,"maxGoal":2.8E7,"type":"MONETARY","urgent":true,"filterAttributes":null,"current":0.0},{"name":"Pollution awareness campain","location":"Middle and Highschools","id":27,"maxGoal":35000.0,"type":"MONETARY","urgent":false,"filterAttributes":null,"current":0.0}] \ No newline at end of file +[{"name":"Pollution Filters","location":"New York Harbor","id":5,"maxGoal":1000.0,"type":"PHYSICAL","urgent":true,"filterAttributes":[],"current":0.0,"description":"One thousand heavy duty pollution filters need to be installed as soon as possible throughout the new york harbor."},{"name":"Diving Equipment","location":"Gulf of Mexico","id":6,"maxGoal":120000.0,"type":"MONETARY","urgent":false,"filterAttributes":[],"current":0.0,"description":"Our research team in mexico needs new deep sea rated equipment to continue their research on the ecosystem! Until the equipment funding is fully reached and gear installed, no more progress can be made."},{"name":"Dog Fish research","location":"RIT","id":8,"maxGoal":1000000.0,"type":"MONETARY","urgent":true,"filterAttributes":[],"current":0.0,"description":"Dog Fish are a very interesting species that many research facilities are not researching. The funding will go towards starting research papers on the species."},{"name":"Fish Food Promotion","location":"","id":10,"maxGoal":1000.0,"type":"MONETARY","urgent":true,"filterAttributes":[],"current":0.0,"description":"The money will be spent on a small advertising campaign at a local pet store, where free fish food will be given out to people willing to listen to our cause."},{"name":"Harpoons","location":"Atlantic City","id":11,"maxGoal":25.0,"type":"PHYSICAL","urgent":false,"filterAttributes":[],"current":0.0,"description":"Harpoons are needed for an upcoming whaling museum exhibit. We aren't picky, and need any harpoons that would be interesting for display. We are accepting any harpoons at least 30 years old, but the older the better."},{"name":"Sea Urchin Hats","location":"Seaworld","id":12,"maxGoal":90.0,"type":"PHYSICAL","urgent":true,"filterAttributes":[],"current":0.0,"description":"Seaworld's sea urchins will be wearing your hats as a part of a promo! Hats donated will be used as a part of an awareness exhibit at Seaworld."},{"name":"Awareness Exhibit","location":"Seneca Park Zoo","id":13,"maxGoal":1.0E7,"type":"MONETARY","urgent":false,"filterAttributes":[],"current":0.0,"description":"Getting the Jelly Solutions message out is our top priority. We would like to open a large exhibit featuring all the endangered species due to climate change, overfishing, and pollution. Sadly, this wouldn't be cheap as we want to really get the message across to visitors through different animals, scenes, info boards, and videos."},{"name":"New Whale","location":"Seneca Park Zoo","id":14,"maxGoal":1.0,"type":"PHYSICAL","urgent":false,"filterAttributes":[],"current":0.0,"description":"Seneca park zoo needs a new whale for the upcoming promotional exhibit! The zoo will provide a commemorative plaque for the donor."},{"name":"New Gosnell Algae Filter","location":"Gosnell College","id":15,"maxGoal":40.0,"type":"MONETARY","urgent":true,"filterAttributes":[],"current":0.0,"description":"The Gosnell College fish tank gets way to dirty way too quickly. $40 surely could replace the filter with a more effective one."},{"name":"Awareness Lunches","location":"Colleges and Highschools","id":16,"maxGoal":5000.0,"type":"MONETARY","urgent":false,"filterAttributes":[],"current":0.0,"description":"College and Highschool students would be great recruits for our volunteer events. The lunches would promote our cause and demonstrate ways that they could easily contribute to the movement."},{"name":"Fish Column for RIT Tours","location":"Wallace Library","id":17,"maxGoal":2.0E7,"type":"MONETARY","urgent":false,"filterAttributes":[],"current":0.0,"description":"RIT tours always run through Wallace Library into the SHED. What better way to encourage students to attend the college than a massive aquarium column? This would not only encourage students to join RIT, and could also raise awareness for our cause."},{"name":"Submarine Matinience","location":"New York Harbor","id":18,"maxGoal":1000000.0,"type":"MONETARY","urgent":true,"filterAttributes":[],"current":0.0,"description":"Submarines are necessary tools that many other needs cannot function without. These urgent repairs need to happen as soon as possible."},{"name":"Volunteering for Volunteer Lunches ","location":"Lake Ontario","id":19,"maxGoal":5.0,"type":"PHYSICAL","urgent":false,"filterAttributes":[],"current":0.0,"description":"We need staff to help organize our awareness lunches! The lunches will recruit interested people into volunteering for upcoming events and cleanups."},{"name":"Volunteer Misc. Equipment","location":"Lake Ontario","id":20,"maxGoal":2500.0,"type":"MONETARY","urgent":true,"filterAttributes":[],"current":0.0,"description":"$2500 is needed to cover the unforeseen costs of the upcoming beach cleanup. The money will be spent on nets and other tools for volunteers."},{"name":"Invasive eel removal","location":"Pacific Seafloor","id":21,"maxGoal":1.0E8,"type":"MONETARY","urgent":true,"filterAttributes":[],"current":0.0,"description":"The population of invasive eels are destroying ecosystems. We need to remove these eels and restore balance to these ecosystems as soon as possible."},{"name":"Fishing Liscense Enforcement","location":"Rochester Bridges","id":22,"maxGoal":10000.0,"type":"MONETARY","urgent":false,"filterAttributes":[],"current":0.0,"description":"Rochester citizens won't stop fishing off bridges no matter how many times we ask. The funding will go to increased enforcement of fishing licenses in delegated stocked areas."},{"name":"Waste Runoff Management","location":"RIT Watershed","id":23,"maxGoal":98000.0,"type":"MONETARY","urgent":true,"filterAttributes":[],"current":0.0,"description":"Waste runoff into the protected watershed near RIT is a consistent issue. We need funding to research how to best solve this problem."},{"name":"Lobbying for anti-dynamite fishing legislation","location":"Washington DC","id":24,"maxGoal":50000.0,"type":"MONETARY","urgent":false,"filterAttributes":[],"current":0.0,"description":"Dynamite fishing destroys ecosystems. Legislation needs to be passed as soon as possible! However, lobbyists aren't cheap. Any support to the fund would be greatly appreciated."},{"name":"Bioluminescence Tunnel","location":"Golisano College of Computing","id":26,"maxGoal":2.8E7,"type":"MONETARY","urgent":true,"filterAttributes":[],"current":0.0,"description":"Golisano needs a biolumenescence tunnel! The addition would be costly, but would make the building much more interesting to be in. People sometimes complain about the way the building feels, and this would be a great way to fix that."}] \ No newline at end of file -- cgit v1.2.3 From 371caf37fb71fd9adf89e52a5557a9359d680539 Mon Sep 17 00:00:00 2001 From: benal01 Date: Mon, 31 Mar 2025 17:47:09 -0400 Subject: backend image support --- .../com/ufund/api/ufundapi/controller/CupboardController.java | 3 ++- ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java | 9 +++++++-- .../java/com/ufund/api/ufundapi/service/CupboardService.java | 5 +++-- 3 files changed, 12 insertions(+), 5 deletions(-) (limited to 'ufund-api') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index ea86aa8..d448f6c 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -55,6 +55,7 @@ public class CupboardController { LOG.log(Level.INFO, "POST /cupboard body={0}", params); String name = (String) params.get("name"); + String image = (String) params.get("image"); String location = (String) params.get("location"); double maxGoal = ((Number) params.get("maxGoal")).doubleValue(); boolean urgent = (Boolean) params.get("urgent"); @@ -63,7 +64,7 @@ public class CupboardController { try { authService.keyHasAccessToCupboard(key); - Need need = cupboardService.createNeed(name, location, maxGoal, goalType, urgent, description); + Need need = cupboardService.createNeed(name, image, location, maxGoal, goalType, urgent, description); return new ResponseEntity<>(need, HttpStatus.OK); } catch (DuplicateKeyException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java index 35e81b3..9b6170b 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/model/Need.java @@ -10,6 +10,7 @@ public class Need { } @JsonProperty("name") private String name; + @JsonProperty("image") private String image; @JsonProperty("location") private String location; @JsonProperty("id") private int id; @JsonProperty("filterAttributes") private String[] filterAttributes; @@ -30,8 +31,9 @@ public class Need { * @param urgent The urgency of the need * @param description The description of the need */ - public Need(@JsonProperty("name") String name, @JsonProperty("location") String location, @JsonProperty("id") int id, @JsonProperty("maxGoal") double maxGoal, @JsonProperty("type") GoalType type, @JsonProperty("urgent") boolean urgent, @JsonProperty("Description") String description) { + public Need(@JsonProperty("name") String name, @JsonProperty("image") String image, @JsonProperty("location") String location, @JsonProperty("id") int id, @JsonProperty("maxGoal") double maxGoal, @JsonProperty("type") GoalType type, @JsonProperty("urgent") boolean urgent, @JsonProperty("Description") String description) { this.id = id; + this.image = image; this.location = location; this.name = name; this.maxGoal = maxGoal; @@ -44,14 +46,16 @@ public class Need { * Create a new need * * @param name The name of the need + * @param image The image representation of the need * @param location The location of the need * @param maxGoal The maximum goal for this need * @param type The type of need (monetary, physical) * @param urgent The urgency of the need * @param description The description of the need */ - public Need(String name, String location, double maxGoal, GoalType type, boolean urgent, String description) { + public Need(String name, String image, String location, double maxGoal, GoalType type, boolean urgent, String description) { this.name = name; + this.image = image; this.location = location; this.type = type; this.maxGoal = maxGoal; @@ -66,6 +70,7 @@ public class Need { */ public Need(Need other) { this.name = other.name; + this.image = other.image; this.location = other.location; this.id = other.id; this.filterAttributes = other.filterAttributes; diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index d216b01..0652696 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -25,6 +25,7 @@ public class CupboardService { * Creates a new Need * * @param name The name of the need to create + * @param image The image representation of the need to create * @param location The location of the new need * @param maxGoal The max goal of the new need * @param goalType The goal type of the new need @@ -34,7 +35,7 @@ public class CupboardService { * @throws IOException Thrown if there was any issue saving the data * @throws DuplicateKeyException If there already exists a need with the same name */ - public Need createNeed(String name, String location, double maxGoal, Need.GoalType goalType, boolean urgent, String description) throws IOException, DuplicateKeyException { + public Need createNeed(String name, String image, String location, double maxGoal, Need.GoalType goalType, boolean urgent, String description) throws IOException, DuplicateKeyException { if (maxGoal <= 0) { throw new IllegalArgumentException("Max Goal must be greater than zero"); @@ -46,7 +47,7 @@ public class CupboardService { } } - Need need = new Need(name, location, maxGoal, goalType, urgent, description); + Need need = new Need(name, image, location, maxGoal, goalType, urgent, description); return cupboardDAO.addNeed(need); } -- 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') 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