diff options
| author | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-07 15:43:17 -0500 | 
|---|---|---|
| committer | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-07 15:43:17 -0500 | 
| commit | 94869200a0d2f80f71fcd0efd4f82c0138b5440d (patch) | |
| tree | 52eff6463b3cec7c3d6bda18e186f9272f528a2c /ufund-api | |
| parent | fa1f1140b1e13d495c8e06e80928efb333917d31 (diff) | |
| parent | caaf278d1fa69fef69c210edb337fa54102d2737 (diff) | |
| download | JellySolutions-94869200a0d2f80f71fcd0efd4f82c0138b5440d.tar.gz JellySolutions-94869200a0d2f80f71fcd0efd4f82c0138b5440d.tar.bz2 JellySolutions-94869200a0d2f80f71fcd0efd4f82c0138b5440d.zip | |
Merge branch 'api-auth' of https://github.com/RIT-SWEN-261-02/team-project-2245-swen-261-02-2b into api-auth
Diffstat (limited to 'ufund-api')
12 files changed, 233 insertions, 83 deletions
| diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/DuplicateKeyException.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/DuplicateKeyException.java new file mode 100644 index 0000000..69ce6c0 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/DuplicateKeyException.java @@ -0,0 +1,7 @@ +package com.ufund.api.ufundapi; + +public class DuplicateKeyException extends Exception { +    public DuplicateKeyException(String message) { +        super(message); +    } +} 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 dfcb8a3..7773028 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 @@ -1,6 +1,7 @@  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; @@ -20,7 +21,7 @@ import org.springframework.web.bind.annotation.RestController;  import com.ufund.api.ufundapi.model.Need;  import com.ufund.api.ufundapi.model.Need.GoalType;  import com.ufund.api.ufundapi.service.CupboardService; -import com.ufund.api.ufundapi.service.CupboardService.DuplicateKeyException; +import com.ufund.api.ufundapi.DuplicateKeyException;  @RestController  @RequestMapping("cupboard") @@ -50,7 +51,7 @@ public class CupboardController {      public ResponseEntity<Need> createNeed(@RequestBody Map<String, String> params) {          String name = params.get("name");          int maxGoal = Integer.parseInt(params.get("maxGoal")); -        Need.GoalType goalType = GoalType.valueOf(params.get("maxGoal")); +        Need.GoalType goalType = GoalType.valueOf(params.get("goalType"));          try {              Need need = cupboardService.createNeed(name, maxGoal, goalType); @@ -141,15 +142,17 @@ public class CupboardController {       * @param need The need to update       * @return OK response and the need if it was successful, or INTERNAL_SERVER_ERROR if there was an issue       */ -    @PutMapping("") -    public ResponseEntity<Need> updateNeed(@RequestBody Need need) { +    @PutMapping("/{id}") +    public ResponseEntity<Need> updateNeed(@RequestBody Need need, @PathVariable int id) {          try { -            Need updatedNeed = cupboardService.updateNeed(need); +            Need updatedNeed = cupboardService.updateNeed(need, id);              if (updatedNeed != null) {                  return new ResponseEntity<>(need, HttpStatus.OK);              } else {                  return new ResponseEntity<>(HttpStatus.NOT_FOUND);              } +        } catch (InvalidParameterException ex) { +            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);          } catch (IOException e) {              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 02526af..0bb3fcf 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,10 +1,12 @@  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; +import com.ufund.api.ufundapi.DuplicateKeyException;  import org.springframework.http.HttpStatus;  import org.springframework.http.ResponseEntity;  import org.springframework.web.bind.annotation.*; @@ -20,12 +22,6 @@ public class UserController {      private final UserService userService;      private final AuthService authService;     -    /** -     * Creates a UserController -     *  -     * @param userService -     * @param authService -     */      public UserController(UserService userService, AuthService authService) {          this.userService = userService;          this.authService = authService; @@ -49,7 +45,8 @@ public class UserController {              } else {                  return new ResponseEntity<>(HttpStatus.CONFLICT);              } - +        } catch (DuplicateKeyException ex) { +            return new ResponseEntity<>(HttpStatus.CONFLICT);          } catch (IOException ex) {              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);          } @@ -93,7 +90,6 @@ public class UserController {       */      @PutMapping("/{username}")      public ResponseEntity<User> updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) { -          try {              authService.authenticate(username, key);              user = userService.updateUser(user, username); @@ -102,7 +98,8 @@ public class UserController {              } else {                  return new ResponseEntity<>(HttpStatus.NOT_FOUND);              } - +        } catch (InvalidParameterException ex) { +            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);          } catch (IOException e) {              return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);          } catch (IllegalAccessException e) { 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 4494939..1fc1e92 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 @@ -59,15 +59,15 @@ public class UserAuthFIleDAO implements UserAuthDAO {      public void addUserAuth(UserAuth userAuth) throws IOException {          synchronized (userAuthMap) {              userAuthMap.put(userAuth.getKey(), userAuth); +            save();          } -        save();      }      @Override      public void removeUserAuth(String key) throws IOException {          synchronized (userAuthMap) {              userAuthMap.remove(key); +            save();          } -        save();      }  } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java index 6558ce2..29d46cf 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java @@ -49,15 +49,14 @@ public interface UserDAO {      /**       * Updates and saves a {@linkplain User user}       *  -     * @param newUser {@link User user} object to be updated and saved -     * @param name {@link String name} name of object to be updated +     * @param user {@link User user} object to be updated and saved       *        * @return updated {@link User user} if successful, null if       *         {@link User user} could not be found       *        * @throws IOException if underlying storage cannot be accessed       */ -    User updateUser(User newUser, String name) throws IOException; +    User updateUser(User user) throws IOException;      /**       * Deletes a {@linkplain User user} with the given id 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 9b206c8..f17f8f2 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 @@ -58,13 +58,6 @@ public class UserFileDAO implements UserDAO {          }      } -    /** -     * Return the user with the String name name or null otherwise -     *  -     * @param username Name of desired user -     *  -     * @return Desired user, null otherwise -     */      @Override      public User getUser(String username) {          synchronized (users) { @@ -72,14 +65,6 @@ public class UserFileDAO implements UserDAO {          }      } -    /** -     * Create a User user -     *  -     * @param user User to create -     *  -     * @return Desired created user -     * @throws IOException If there was an IO issue saving the file -     */      @Override      public User addUser(User user) throws IOException {          synchronized (users) { @@ -92,36 +77,19 @@ public class UserFileDAO implements UserDAO {          }      } -    /** -     * Update a user that matches the supplied name -     *  -     * @param name    The name of the user -     * @param newUser New user data -     *  -     * @return Desired user, null otherwise -     * @throws IOException If there was an IO issue saving the file -     */      @Override -    public User updateUser(User newUser, String name) throws IOException { +    public User updateUser(User user) throws IOException {          synchronized (users) { -            if (users.containsKey(name)) { -                users.put(name, newUser); +            if (users.containsKey(user.getUsername())) { +                users.put(user.getUsername(), user);                  save(); -                return newUser; +                return user;              } else {                  return null;              }          }      } -    /** -     * Delete a user matching the name -     *  -     * @param username The name of the user -     *  -     * @return True if deleted, false otherwise -     * @throws IOException If there was an IO issue saving the file -     */      @Override      public boolean deleteUser(String username) throws IOException {          synchronized (users) { 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 7e54cfb..591d891 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,10 @@  package com.ufund.api.ufundapi.service; -import java.io.IOException; - -import org.springframework.stereotype.Component; -  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 { @@ -24,9 +23,8 @@ public class AuthService {       * @param username The username 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  -          */ -         public void authenticate(String username, String key) throws IllegalAccessException, IOException { +     */ +    public void authenticate(String username, String key) throws IllegalAccessException, IOException {          var userAuth = userAuthDAO.getUserAuth(key);          if (userAuth == null || !userAuth.getUsername().equals(username)) {              throw new IllegalAccessException("Unauthorized"); 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 6052e4f..c8609ab 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 @@ -6,22 +6,27 @@ import java.util.Arrays;  import com.ufund.api.ufundapi.model.Need;  import com.ufund.api.ufundapi.persistence.CupboardDAO;  import org.springframework.stereotype.Component; +import com.ufund.api.ufundapi.DuplicateKeyException;  @Component  public class CupboardService {      private final CupboardDAO cupboardDAO; -    public class DuplicateKeyException extends Exception { -        public DuplicateKeyException(String message) { -            super(message); -        } -    } -      public CupboardService(CupboardDAO cupboardDAO) {          this.cupboardDAO = cupboardDAO;      } +    /** +     * Creates a new Need +     * +     * @param name The name of the need to create +     * @param maxGoal The max goal of the new need +     * @param goalType The goal type 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, int maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException {          Need need = new Need(name, goalType, maxGoal); @@ -39,6 +44,12 @@ public class CupboardService {      } +    /** +     * Get all the needs in the cupboard +     * +     * @return An array containing all needs +     * @throws IOException Thrown if there was any issue saving the data +     */      public Need[] getNeeds() throws IOException {          return cupboardDAO.getNeeds();      } @@ -48,7 +59,7 @@ public class CupboardService {       *       * @param search The search substring       * @return The requested array -     * @throws IOException  +     * @throws IOException Thrown if there was any issue saving the data       */      public Need[] searchNeeds(String search) throws IOException {          return Arrays.stream(cupboardDAO.getNeeds()) @@ -67,13 +78,17 @@ public class CupboardService {      }      /** -     * Modify a need +     * Updates a need       * -     * @param need -     * @return +     * @param id The ID of the need to update +     * @param need The need object to set (note: the ID is ignored) +     * @return The updated need object       * @throws IOException Thrown if there was an issue saving the changes       */ -    public Need updateNeed(Need need) throws IOException { +    public Need updateNeed(Need need, int id) throws IOException { +        if (need.getId() != id) { +            throw new IllegalArgumentException("ID in URL and body must match"); +        }          return cupboardDAO.updateNeed(need);      } 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 6af3897..935ee72 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 com.ufund.api.ufundapi.DuplicateKeyException;  import com.ufund.api.ufundapi.model.User;  import com.ufund.api.ufundapi.persistence.UserDAO;  import org.springframework.stereotype.Component; @@ -11,29 +12,60 @@ public class UserService {      private final UserDAO userDAO; -    /** -     * Create a user controller to receive REST signals -     * -     * @param userDao The Data Access Object -     */      public UserService(UserDAO userDao) {          this.userDAO = userDao;      } -    public User createUser(String username, String password) throws IOException { +    /** +     * Creates a new user +     * +     * @param username The username of the user +     * @param password The password of the user +     * @return The created user object +     * @throws IOException Thrown on any problem saving the file +     */ +    public User createUser(String username, String password) throws IOException, DuplicateKeyException { +        if (userDAO.getUser(username) != null) { +            throw new DuplicateKeyException("A user with this name already exists"); +        }          User user = User.create(username, password);          return userDAO.addUser(user);      } -    public User getUser(String username) throws IOException, IllegalAccessException { +    /** +     * Gets a user with the given username +     * +     * @param username The username of the user +     * @return The user object with that username +     * @throws IOException If there was any problem saving the file +     */ +    public User getUser(String username) throws IOException {          return userDAO.getUser(username);      } -    public User updateUser(User user, String name) throws IllegalAccessException, IOException { -        return userDAO.updateUser(user, name); +    /** +     * Updates a user +     * +     * @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 +     */ +    public User updateUser(User user, String username) throws IOException { +        if (!user.getUsername().equals(username)) { +            throw new IllegalArgumentException("ID in URL and body must match"); +        } +        return userDAO.updateUser(user);      } -    public Boolean deleteUser(String username) throws IllegalAccessException, IOException { +    /** +     * Deletes a user +     * +     * @param username The username of the user to delete +     * @return True if the user was deleted +     * @throws IOException Thrown if there was any issue saving the data +     */ +    public boolean deleteUser(String username) throws IOException {          return userDAO.deleteUser(username);      } 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 a78c45c..c7a5584 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 @@ -49,6 +49,26 @@ public class CupboardControllerTest {      }      @Test +    public void createNeedBadMaxGoal() throws IOException { +        var need = new Need("Name", 1, -100, Need.GoalType.MONETARY); +        when(mockCupboardDAO.createNeed(need)).thenReturn(need); + +        var res = cupboardController.createNeed(need); + +        assertEquals(HttpStatus.BAD_REQUEST, res.getStatusCode()); +    } + +    @Test +    public void createNeedIOException() throws IOException { +        var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); +        when(mockCupboardDAO.createNeed(need)).thenThrow(new IOException()); + +        var res = cupboardController.createNeed(need); + +        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); +    } + +    @Test      public void getNeeds() {          var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);          when(mockCupboardService.getNeeds()).thenReturn(new Need[]{need}); @@ -60,6 +80,16 @@ public class CupboardControllerTest {      }      @Test +    public void getNeedsIOException() { +        var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); +        when(mockCupboardDAO.getNeeds()).thenThrow(new IOException()); + +        var res = cupboardController.getNeeds(); + +        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); +    } + +    @Test      public void getNeedsEmpty() {          when(mockCupboardService.getNeeds()).thenReturn(new Need[]{}); @@ -81,6 +111,16 @@ public class CupboardControllerTest {      }      @Test +    public void searchNeedsIOException() { +        var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); +        when(mockCupboardDAO.findNeeds("Na")).thenThrow(new IOException()); + +        var res = cupboardController.searchNeeds("Na"); + +        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); +    } + +    @Test      public void searchNeedsEmpty() {          when(mockCupboardService.findNeeds("Na")).thenReturn(new Need[]{}); @@ -102,6 +142,16 @@ public class CupboardControllerTest {      }      @Test +    public void getNeedIOException() { +        var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); +        when(mockCupboardDAO.getNeed(need.getId())).thenThrow(new IOException()); + +        var res = cupboardController.getNeed(need.getId()); + +        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); +    } + +    @Test      public void getNeedFail() {          var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);          when(mockCupboardService.getNeed(need.getId())).thenReturn(null); @@ -124,6 +174,16 @@ public class CupboardControllerTest {      }      @Test +    public void updateNeedsIOException() throws IOException { +        var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); +        when(mockCupboardDAO.updateNeed(need)).thenThrow(new IOException()); + +        var res = cupboardController.updateNeed(need); + +        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); +    } + +    @Test      public void deleteNeed() throws IOException {          var need = new Need("Name", 1, 100, Need.GoalType.MONETARY);          when(mockCupboardService.getNeed(1)).thenReturn(need); @@ -143,4 +203,15 @@ public class CupboardControllerTest {          assertEquals(HttpStatus.NOT_FOUND, res.getStatusCode());      } + +    @Test +    public void deleteNeedIOException() throws IOException { +        var need = new Need("Name", 1, 100, Need.GoalType.MONETARY); +        when(mockCupboardDAO.getNeed(1)).thenReturn(need); +        when(mockCupboardDAO.deleteNeed(1)).thenThrow(new IOException()); + +        var res = cupboardController.deleteNeed(1); + +        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, res.getStatusCode()); +    }  } diff --git a/ufund-api/src/test/java/com/ufund/api/ufundapi/model/NeedTest.java b/ufund-api/src/test/java/com/ufund/api/ufundapi/model/NeedTest.java index 50b5cf2..ffcd808 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 @@ -59,6 +59,7 @@ public class NeedTest {          assertEquals(need.getCurrent(), current);      } +      @Test      public void testFilterAttributes() { @@ -75,4 +76,35 @@ public class NeedTest {          assertEquals(need.getFilterAttributes(), filterAttributes);      } +    @Test +    public void testSetMaxGoal() { + +        String name = "Jellyfish"; +        int id = 0; +        double maxGoal = 100.00; +        GoalType type = GoalType.MONETARY; +        Need need = new Need(name, id, maxGoal, type); + +        double newGoal = 200.00; +        need.setMaxGoal(newGoal); + + +        assertEquals(newGoal, need.getMaxGoal()); +    } + +    @Test +    public void testSetName() { + +        String name = "Jellyfish"; +        int id = 0; +        double maxGoal = 100.00; +        GoalType type = GoalType.MONETARY; +        Need need = new Need(name, id, maxGoal, type); + +        String newName = "TESTINGFUN"; +        need.setName(newName); + +        assertEquals(newName, need.getName()); +    } +  } 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 6f35df0..22f6ffb 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 @@ -46,4 +46,32 @@ public class UserTest {      } +    @Test +    public void testRemoveBasketNeed() { + +        String expectedName = "Bob"; + +        User user = new User(expectedName); +        Need need = new Need("Test", 0, 100, Need.GoalType.MONETARY); +        Need need2 = new Need("Test2", 0, 100, Need.GoalType.MONETARY); + +        user.addToBasket(need); +        user.removeBasketNeed(need); +        user.addToBasket(need2); + +        assertEquals(need2, user.getBasketNeeds()[0]); + +    } + +    @Test +    public void testVerifyPassword() { + +        String expectedName = "Bob"; + +        User user = new User(expectedName); + +        assertEquals(false, user.verifyPassword(expectedName)); + +    } +  } | 
