diff options
author | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-30 20:48:22 -0400 |
---|---|---|
committer | Gunther6070 <haydenhartman10@yahoo.com> | 2025-03-30 20:48:22 -0400 |
commit | 197be103d02db808b0e6bf8a1d1369e3d7928c03 (patch) | |
tree | ec9233147ecce35defea8cd85290f9232201c6c0 /ufund-api | |
parent | b29f29eca643648381bfb62a4b90ad29e17f48a7 (diff) | |
download | JellySolutions-197be103d02db808b0e6bf8a1d1369e3d7928c03.tar.gz JellySolutions-197be103d02db808b0e6bf8a1d1369e3d7928c03.tar.bz2 JellySolutions-197be103d02db808b0e6bf8a1d1369e3d7928c03.zip |
Modified controllers to return error text when catching errors. Also added additional error checking to CupboardService for physical needs.
Diffstat (limited to 'ufund-api')
5 files changed, 64 insertions, 64 deletions
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<Need> createNeed(@RequestBody Map<String, Object> params, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity<Object> createNeed(@RequestBody Map<String, Object> 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<Need[]> getNeeds() { + public ResponseEntity<Object> 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 { * <p> */ @GetMapping("/") - public ResponseEntity<Need[]> searchNeeds(@RequestParam String name) { + public ResponseEntity<Object> 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<br> */ @GetMapping("/{id}") - public ResponseEntity<Need> getNeed(@PathVariable int id) { + public ResponseEntity<Object> 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<Need> updateNeed(@RequestBody Need need, @PathVariable int id, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity<Object> 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<Need> deleteNeed(@PathVariable int id, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity<Object> 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<User> createUser(@RequestBody Map<String, String> params) { + public ResponseEntity<Object> createUser(@RequestBody Map<String, String> 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<User> getUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity<Object> 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<User> updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity<Object> 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<Boolean> deleteUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity<Object> 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<User> response = userController.getUser(username, key); + ResponseEntity<Object> 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<User> response = userController.getUser(username, key); + ResponseEntity<Object> 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<User> response = userController.getUser(username, key); + ResponseEntity<Object> 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<User> response = userController.getUser(username, key); + ResponseEntity<Object> response = userController.getUser(username, key); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); @@ -119,7 +119,7 @@ public class UserControllerTest { // Invoke - ResponseEntity<User> response = userController.createUser(userMap); + ResponseEntity<Object> response = userController.createUser(userMap); // Analyze assertEquals(HttpStatus.CREATED, response.getStatusCode()); @@ -138,7 +138,7 @@ public class UserControllerTest { // Invoke - ResponseEntity<User> response = userController.createUser(userMap); + ResponseEntity<Object> 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<User> response = userController.createUser(userMap); + ResponseEntity<Object> response = userController.createUser(userMap); // Analyze assertEquals(HttpStatus.CONFLICT, response.getStatusCode()); @@ -172,7 +172,7 @@ public class UserControllerTest { // Invoke - ResponseEntity<User> response = userController.createUser(userMap); + ResponseEntity<Object> 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<User> response = userController.updateUser(user, username, key); + ResponseEntity<Object> 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<User> response = userController.updateUser(user, username, key); + ResponseEntity<Object> 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<User> response = userController.updateUser(user, username, key); + ResponseEntity<Object> response = userController.updateUser(user, username, key); // Analyze assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); @@ -241,7 +241,7 @@ public class UserControllerTest { // Invoke - ResponseEntity<User> response = userController.updateUser(user, username, key); + ResponseEntity<Object> 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<Boolean> response = userController.deleteUser(username, key); + ResponseEntity<Object> 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<Boolean> response = userController.deleteUser(username, key); + ResponseEntity<Object> 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<Boolean> response = userController.deleteUser(username, key); + ResponseEntity<Object> 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<Boolean> response = userController.deleteUser(username, key); + ResponseEntity<Object> response = userController.deleteUser(username, key); // Analyze assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); |