From 36e9c675db2fdf65ecb01a90c0d635664f1ccb19 Mon Sep 17 00:00:00 2001 From: nastiausenko Date: Sun, 21 Apr 2024 00:08:41 +0300 Subject: [PATCH] fix errors --- .../exception/GlobalExceptionHandler.java | 20 +++++++------------ .../auth/AuthControllerIntegrationTest.java | 5 +++-- .../link/LinkControllerIntegrationTest.java | 10 +++++----- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java b/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java index 9f96b90..a97e194 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java @@ -1,6 +1,7 @@ package com.linkurlshorter.urlshortener.exception; import com.linkurlshorter.urlshortener.auth.exception.EmailAlreadyTakenException; +import com.linkurlshorter.urlshortener.link.NoLinkFoundByIdException; import com.linkurlshorter.urlshortener.security.ForbiddenException; import com.linkurlshorter.urlshortener.user.NoSuchEmailFoundException; import com.linkurlshorter.urlshortener.user.NoUserFoundByEmailException; @@ -101,12 +102,6 @@ public ResponseEntity handleBadCredentialsException(BadCredentialsExcept * @param ex denied access error * @return {@link ResponseEntity} object with the corresponding status and error message */ - @ExceptionHandler(ForbiddenException.class) - public ResponseEntity handleForbiddenException(ForbiddenException ex) { - ErrorResponse errorResponse = buildErrorResponse(HttpStatus.FORBIDDEN, - "Forbidden!", ex.getMessage()); - return ResponseEntity.status(HttpStatus.FORBIDDEN).body(errorResponse); - } /** * Handles no resource (404) exceptions for different types of requests. @@ -152,21 +147,20 @@ private ErrorResponse buildErrorResponse(HttpStatus status, String message, Stri * Returns a response with a 403 status and the corresponding error message. * * @param ex forbidden exception - * @param request HttpServletRequest object representing the HTTP request * @return {@link ResponseEntity} object with the corresponding status and error message */ @ExceptionHandler(ForbiddenException.class) public ResponseEntity handleForbiddenException( - ForbiddenException ex, HttpServletRequest request) { - ErrorResponse errorResponse = buildErrorResponse(HttpStatus.FORBIDDEN, - ex.getMessage(), request.getRequestURI()); + ForbiddenException ex) { + ErrorResponse errorResponse = buildErrorResponse(HttpStatus.FORBIDDEN, "Operation forbidden!", + ex.getMessage()); return ResponseEntity.status(HttpStatus.FORBIDDEN).body(errorResponse); } @ExceptionHandler(NoLinkFoundByIdException.class) public ResponseEntity handleNoLinkFoundByIdException( - NoLinkFoundByIdException ex, HttpServletRequest request) { - ErrorResponse errorResponse = buildErrorResponse(HttpStatus.NOT_FOUND, - ex.getMessage(), request.getRequestURI()); + NoLinkFoundByIdException ex) { + ErrorResponse errorResponse = buildErrorResponse(HttpStatus.NOT_FOUND,"No link by provided id found", + ex.getMessage()); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } } diff --git a/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java b/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java index af288c9..3b714fe 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java @@ -77,7 +77,8 @@ void loginFailedWhenUserDoesNotExistTest() throws Exception { .content(objectMapper.writeValueAsString(authRequest))) .andExpect(status().is4xxClientError()) .andExpect(jsonPath("$.statusCode").value(401)) - .andExpect(jsonPath("$.message").value("Authentication failed!")) + .andExpect(jsonPath("$.message").value("Authentication failed!")); + } /** * Test case to verify login failure when password does not match. @@ -192,4 +193,4 @@ void registerFailedWhenInvalidEmailGivenTest(String email) throws Exception { .andExpect(jsonPath("$.statusCode").value(400)) .andExpect(jsonPath("$.message").value("Validation failed!")); } -} +} \ No newline at end of file diff --git a/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerIntegrationTest.java b/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerIntegrationTest.java index 9214ba5..545304b 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerIntegrationTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerIntegrationTest.java @@ -96,8 +96,8 @@ void createShortLinkFailsWhenUrlIsInvalid(String url) throws Exception { .content(objectMapper.writeValueAsString(createLinkRequest))) .andExpect(status().is4xxClientError()) .andExpect(jsonPath("$.statusCode").value(400)) - .andExpect(jsonPath("$.message").value("Not valid format url!")) - .andExpect(jsonPath("$.path").value("/api/V1/link/create")); + .andExpect(jsonPath("$.message").value("Validation failed!")) + .andExpect(jsonPath("$.exceptionMessage").value("Not valid format url!")); } @Test @@ -118,14 +118,14 @@ void deleteLinkFailsWhenIdIsInvalid() throws Exception { .andExpect(status().is4xxClientError()) .andExpect(jsonPath("$.statusCode").value(404)) .andExpect(jsonPath("$.message").value("No link by provided id found")) - .andExpect(jsonPath("$.path").value("/api/V1/link/delete")); + .andExpect(jsonPath("$.exceptionMessage").value("No link by provided id found")); } @Test void deleteLinkFailsWhenIdIsNull() throws Exception { mockMvc.perform(post(baseUrl + "delete" + "?id=" + null) .contentType(MediaType.APPLICATION_JSON) .header("Authorization", token)) - .andExpect(status().is4xxClientError()); + .andExpect(status().isInternalServerError()); } @Test void deleteLinkFailsWhenUserHasNoRightsForThisLink() throws Exception { @@ -144,6 +144,6 @@ void deleteLinkFailsWhenUserHasNoRightsForThisLink() throws Exception { .andExpect(status().is4xxClientError()) .andExpect(jsonPath("$.statusCode").value(403)) .andExpect(jsonPath("$.message").value("Operation forbidden!")) - .andExpect(jsonPath("$.path").value("/api/V1/link/delete")); + .andExpect(jsonPath("$.exceptionMessage").value("Operation forbidden!")); } }