diff --git a/src/main/java/com/linkurlshorter/urlshortener/exception/ErrorResponse.java b/src/main/java/com/linkurlshorter/urlshortener/exception/ErrorResponse.java index fec4362..d61c002 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/exception/ErrorResponse.java +++ b/src/main/java/com/linkurlshorter/urlshortener/exception/ErrorResponse.java @@ -11,5 +11,5 @@ public record ErrorResponse( LocalDateTime dateTime, int statusCode, String message, - String exceptionMessage + String path ) {} diff --git a/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java b/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java index a97e194..b7404fe 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java @@ -1,16 +1,14 @@ 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.link.*; import com.linkurlshorter.urlshortener.user.NoSuchEmailFoundException; import com.linkurlshorter.urlshortener.user.NoUserFoundByEmailException; import com.linkurlshorter.urlshortener.user.NoUserFoundByIdException; import com.linkurlshorter.urlshortener.user.NullEmailException; -import org.apache.coyote.BadRequestException; +import jakarta.servlet.http.HttpServletRequest; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.core.AuthenticationException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; @@ -30,13 +28,16 @@ public class GlobalExceptionHandler { * Handles method argument validation errors and invalid request errors (400). * Returns a response with status 400 and the corresponding error message. * - * @param exception method argument validation error + * @param ex method argument validation error * @return {@link ResponseEntity} object with the appropriate status and error message */ - @ExceptionHandler({MethodArgumentNotValidException.class, BadRequestException.class}) - public ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException exception) { - ErrorResponse errorResponse = buildErrorResponse(HttpStatus.BAD_REQUEST, "Validation failed!", - Objects.requireNonNull(exception.getFieldError()).getDefaultMessage()); + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity handleMethodArgumentNotValid( + MethodArgumentNotValidException ex, HttpServletRequest request) { + ErrorResponse errorResponse = buildErrorResponse( + HttpStatus.BAD_REQUEST, + Objects.requireNonNull(ex.getFieldError()).getDefaultMessage(), + request.getRequestURI()); return ResponseEntity.badRequest().body(errorResponse); } @@ -47,11 +48,11 @@ public ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotVali * @param ex null email error * @return {@link ResponseEntity} object with the corresponding status and error message */ - //TODO: write tests for this exception @ExceptionHandler(NullEmailException.class) - public ResponseEntity handleNullEmailException(NullEmailException ex) { + public ResponseEntity handleNullEmailException( + NullEmailException ex, HttpServletRequest request) { ErrorResponse errorResponse = buildErrorResponse(HttpStatus.BAD_REQUEST, - "Email provided is null, so request can not be processed!", ex.getMessage()); + ex.getMessage(), request.getRequestURI()); return ResponseEntity.badRequest().body(errorResponse); } @@ -62,9 +63,10 @@ public ResponseEntity handleNullEmailException(NullEmailException ex) { * @return {@link ResponseEntity} containing the error response for authentication failure */ @ExceptionHandler(AuthenticationException.class) - public ResponseEntity handleAuthenticationException(AuthenticationException ex) { + public ResponseEntity handleAuthenticationException( + AuthenticationException ex, HttpServletRequest request) { ErrorResponse errorResponse = buildErrorResponse(HttpStatus.UNAUTHORIZED, - "Authentication failed!", ex.getMessage()); + ex.getMessage(), request.getRequestURI()); return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorResponse); } @@ -75,34 +77,13 @@ public ResponseEntity handleAuthenticationException(AuthenticationExcept * @return {@link ResponseEntity} containing the error response for email already taken */ @ExceptionHandler(EmailAlreadyTakenException.class) - public ResponseEntity handleEmailAlreadyTakenException(EmailAlreadyTakenException ex) { + public ResponseEntity handleEmailAlreadyTakenException( + EmailAlreadyTakenException ex, HttpServletRequest request) { ErrorResponse errorResponse = buildErrorResponse(HttpStatus.BAD_REQUEST, - "Email already taken!", ex.getMessage()); + ex.getMessage(), request.getRequestURI()); return ResponseEntity.badRequest().body(errorResponse); } - /** - * Handles bad credentials (401) errors. - * Returns a response with a 401 status and the corresponding error message. - * - * @param ex bad credentials error - * @return {@link ResponseEntity} object with the corresponding status and error message - */ - @ExceptionHandler(BadCredentialsException.class) - public ResponseEntity handleBadCredentialsException(BadCredentialsException ex) { - ErrorResponse errorResponse = buildErrorResponse(HttpStatus.UNAUTHORIZED, - "Bad Credentials!", ex.getMessage()); - return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorResponse); - } - - /** - * Handles denied access (403) errors. - * Returns a response with a 403 status and the corresponding error message. - * - * @param ex denied access error - * @return {@link ResponseEntity} object with the corresponding status and error message - */ - /** * Handles no resource (404) exceptions for different types of requests. * Returns a response with a 404 status and the corresponding error message. @@ -110,58 +91,71 @@ public ResponseEntity handleBadCredentialsException(BadCredentialsExcept * @param ex missing resource exception * @return {@link ResponseEntity} object with the corresponding status and error message */ - @ExceptionHandler({NoSuchEmailFoundException.class, NoUserFoundByEmailException.class, NoUserFoundByIdException.class}) - public ResponseEntity handleNotFoundExceptions(Exception ex) { - ErrorResponse errorResponse = buildErrorResponse(HttpStatus.NOT_FOUND, "Email Not Found!", - ex.getMessage()); + @ExceptionHandler({NoSuchEmailFoundException.class, + NoUserFoundByEmailException.class, NoUserFoundByIdException.class}) + public ResponseEntity handleNotFoundExceptions( + RuntimeException ex, HttpServletRequest request) { + ErrorResponse errorResponse = buildErrorResponse(HttpStatus.NOT_FOUND, + ex.getMessage(), request.getRequestURI()); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } - /** - * Handles general exceptions (500). - * Returns a response with status 500 and the corresponding error message. - * - * @param ex general exception - * @return {@link ResponseEntity} object with the appropriate status and error message - */ - @ExceptionHandler({Exception.class}) - public ResponseEntity handleInternalServerError(Exception ex) { - ErrorResponse errorResponse = buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, - "Internal Server Error!", ex.getMessage()); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse); - } - /** * Creates an error response object. * - * @param status status of the error - * @param message error message - * @param exceptionMessage exception message + * @param status status of the error + * @param message error message + * @param requestURI request URL * @return an {@link ErrorResponse} object with the appropriate data */ - private ErrorResponse buildErrorResponse(HttpStatus status, String message, String exceptionMessage) { - return new ErrorResponse(LocalDateTime.now(), status.value(), message, exceptionMessage); + private ErrorResponse buildErrorResponse(HttpStatus status, String message, String requestURI) { + return new ErrorResponse(LocalDateTime.now(), status.value(), message, requestURI); } /** * Handles Forbidden (403) exceptions for different types of requests. * 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) { - ErrorResponse errorResponse = buildErrorResponse(HttpStatus.FORBIDDEN, "Operation forbidden!", - ex.getMessage()); + ForbiddenException ex, HttpServletRequest request) { + ErrorResponse errorResponse = buildErrorResponse(HttpStatus.FORBIDDEN, + ex.getMessage(), request.getRequestURI()); return ResponseEntity.status(HttpStatus.FORBIDDEN).body(errorResponse); } @ExceptionHandler(NoLinkFoundByIdException.class) public ResponseEntity handleNoLinkFoundByIdException( - NoLinkFoundByIdException ex) { - ErrorResponse errorResponse = buildErrorResponse(HttpStatus.NOT_FOUND,"No link by provided id found", - ex.getMessage()); + NoLinkFoundByIdException ex, HttpServletRequest request) { + ErrorResponse errorResponse = buildErrorResponse(HttpStatus.NOT_FOUND, + ex.getMessage(), request.getRequestURI()); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } + + @ExceptionHandler(DeletedLinkException.class) + public ResponseEntity handleDeletedLinkException( + DeletedLinkException ex, HttpServletRequest request) { + ErrorResponse errorResponse = buildErrorResponse(HttpStatus.BAD_REQUEST, + ex.getMessage(), request.getRequestURI()); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); + } + + @ExceptionHandler(LinkStatusException.class) + public ResponseEntity handleLinkStatusException( + LinkStatusException ex, HttpServletRequest request) { + ErrorResponse errorResponse = buildErrorResponse(HttpStatus.BAD_REQUEST, + ex.getMessage(), request.getRequestURI()); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); + } + + @ExceptionHandler(InternalServerLinkException.class) + public ResponseEntity handleInternalServerLinkException( + InternalServerLinkException ex, HttpServletRequest request) { + ErrorResponse errorResponse = buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, + ex.getMessage(), request.getRequestURI()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse); + } } diff --git a/src/main/java/com/linkurlshorter/urlshortener/security/ForbiddenException.java b/src/main/java/com/linkurlshorter/urlshortener/link/ForbiddenException.java similarity index 73% rename from src/main/java/com/linkurlshorter/urlshortener/security/ForbiddenException.java rename to src/main/java/com/linkurlshorter/urlshortener/link/ForbiddenException.java index 6ca6d31..86924fb 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/security/ForbiddenException.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/ForbiddenException.java @@ -1,4 +1,4 @@ -package com.linkurlshorter.urlshortener.security; +package com.linkurlshorter.urlshortener.link; public class ForbiddenException extends RuntimeException { diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/LinkController.java b/src/main/java/com/linkurlshorter/urlshortener/link/LinkController.java index 821bb7e..fdc3180 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/LinkController.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/LinkController.java @@ -1,6 +1,5 @@ package com.linkurlshorter.urlshortener.link; -import com.linkurlshorter.urlshortener.security.ForbiddenException; import com.linkurlshorter.urlshortener.user.User; import com.linkurlshorter.urlshortener.user.UserService; import io.swagger.v3.oas.annotations.Operation; diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/LinkService.java b/src/main/java/com/linkurlshorter/urlshortener/link/LinkService.java index 7226caa..e9e0f90 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/LinkService.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/LinkService.java @@ -1,5 +1,6 @@ package com.linkurlshorter.urlshortener.link; +import com.linkurlshorter.urlshortener.user.User; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -41,10 +42,8 @@ public Link update(Link link) { if (Objects.isNull(link)) { throw new NullLinkPropertyException(); } -//TODO:test for delete - if (findByShortLink(link.getShortLink()).getStatus() == LinkStatus.DELETED) { - throw new DeletedLinkException(); - } + + throwIfDeleted(link); return linkRepository.save(link); } @@ -134,7 +133,6 @@ public void deleteById(UUID id) { * @throws NoLinkFoundByShortLinkException If no link was found by the short link. * @throws NullLinkPropertyException If the found link does not have the ACTIVE status. */ - //TODO: tests for this method public Link findByExistUniqueLink(String shortLink) { Link existingLink = linkRepository.findByShortLink(shortLink).orElseThrow(NoLinkFoundByShortLinkException::new); if (existingLink.getStatus() == LinkStatus.ACTIVE) { @@ -143,4 +141,16 @@ public Link findByExistUniqueLink(String shortLink) { throw new NullLinkPropertyException(); } } + + /** + * Throws a DeletedLinkException if the link has been marked as deleted. + * + * @param link The link entity to check. + * @throws DeletedLinkException If the link has been marked as deleted. + */ + private void throwIfDeleted(Link link) { + if (findByShortLink(link.getShortLink()).getStatus() == LinkStatus.DELETED) { + throw new DeletedLinkException(); + } + } } diff --git a/src/main/java/com/linkurlshorter/urlshortener/user/UserController.java b/src/main/java/com/linkurlshorter/urlshortener/user/UserController.java index 8f6b522..16b1ca0 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/user/UserController.java +++ b/src/main/java/com/linkurlshorter/urlshortener/user/UserController.java @@ -78,7 +78,6 @@ public ResponseEntity changePassword(@RequestBody @Valid public ResponseEntity changeEmail(@RequestBody @Valid ChangeUserEmailRequest emailRequest) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String newEmail = emailRequest.getNewEmail(); - //TODO: test for this exception if (userService.existsByEmail(newEmail)) { throw new EmailAlreadyTakenException(newEmail); } diff --git a/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java b/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java index 3b714fe..af236b1 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java @@ -22,7 +22,6 @@ import org.testcontainers.junit.jupiter.Testcontainers; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /** @@ -59,9 +58,9 @@ void loginSuccessfulTest() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.post(baseUrl + "login") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(authRequest))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.message").value("User logged in successfully!")) - .andExpect(jsonPath("$.jwtToken").exists()); + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("User logged in successfully!")) + .andExpect(MockMvcResultMatchers.jsonPath("$.jwtToken").exists()); } /** @@ -72,12 +71,12 @@ void loginSuccessfulTest() throws Exception { @Test void loginFailedWhenUserDoesNotExistTest() throws Exception { authRequest = new AuthRequest("userNotFound@example.com", "Pass1234"); - this.mockMvc.perform(post(baseUrl + "login") + this.mockMvc.perform(MockMvcRequestBuilders.post(baseUrl + "login") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(authRequest))) - .andExpect(status().is4xxClientError()) - .andExpect(jsonPath("$.statusCode").value(401)) - .andExpect(jsonPath("$.message").value("Authentication failed!")); + .andExpect(MockMvcResultMatchers.status().is4xxClientError()) + .andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(401)) + .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("No user by provided email found")); } /** @@ -88,12 +87,12 @@ void loginFailedWhenUserDoesNotExistTest() throws Exception { @Test void loginFailedWhenPasswordDoesNotMatchTest() throws Exception { authRequest = new AuthRequest("user1@example.com", "Pass12345"); - this.mockMvc.perform(post(baseUrl + "login") + this.mockMvc.perform(MockMvcRequestBuilders.post(baseUrl + "login") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(authRequest))) .andExpect(MockMvcResultMatchers.status().is4xxClientError()) - .andExpect(jsonPath("$.statusCode").value(401)) - .andExpect(jsonPath("$.message").value("Bad Credentials!")); + .andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(401)) + .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Bad credentials")); } /** @@ -110,8 +109,10 @@ void loginFailedWhenInvalidPasswordGivenTest(String password) throws Exception { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(authRequest))) .andExpect(status().is4xxClientError()) - .andExpect(jsonPath("$.statusCode").value(400)) - .andExpect(jsonPath("$.message").value("Validation failed!")); + .andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(400)) + .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Password " + + "must be at least 8 characters long and contain at least one digit, one uppercase letter, " + + "and one lowercase letter. No spaces are allowed.")); } /** @@ -133,8 +134,8 @@ void loginFailedWhenInvalidEmailGivenTest(String email) throws Exception { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(authRequest))) .andExpect(status().is4xxClientError()) - .andExpect(jsonPath("$.statusCode").value(400)) - .andExpect(jsonPath("$.message").value("Validation failed!")); + .andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(400)) + .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Email address entered incorrectly!")); } /** @@ -167,8 +168,10 @@ void registerFailedWhenInvalidPasswordGivenTest(String password) throws Exceptio .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(authRequest))) .andExpect(status().is4xxClientError()) - .andExpect(jsonPath("$.statusCode").value(400)) - .andExpect(jsonPath("$.message").value("Validation failed!")); + .andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(400)) + .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Password " + + "must be at least 8 characters long and contain at least one digit, one uppercase letter, " + + "and one lowercase letter. No spaces are allowed.")); } /** @@ -190,7 +193,7 @@ void registerFailedWhenInvalidEmailGivenTest(String email) throws Exception { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(authRequest))) .andExpect(status().is4xxClientError()) - .andExpect(jsonPath("$.statusCode").value(400)) - .andExpect(jsonPath("$.message").value("Validation failed!")); + .andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(400)) + .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Email address entered incorrectly!")); } -} \ No newline at end of file +} diff --git a/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerTest.java b/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerTest.java index a8347bb..86baf9c 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerTest.java @@ -1,7 +1,6 @@ package com.linkurlshorter.urlshortener.auth; import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.dockerjava.api.exception.UnauthorizedException; import com.linkurlshorter.urlshortener.auth.dto.AuthRequest; import com.linkurlshorter.urlshortener.auth.exception.EmailAlreadyTakenException; import com.linkurlshorter.urlshortener.TestConfig; @@ -13,8 +12,6 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; -import org.springframework.security.authentication.BadCredentialsException; -import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; diff --git a/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerIntegrationTest.java b/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerIntegrationTest.java index 545304b..9214ba5 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("Validation failed!")) - .andExpect(jsonPath("$.exceptionMessage").value("Not valid format url!")); + .andExpect(jsonPath("$.message").value("Not valid format url!")) + .andExpect(jsonPath("$.path").value("/api/V1/link/create")); } @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("$.exceptionMessage").value("No link by provided id found")); + .andExpect(jsonPath("$.path").value("/api/V1/link/delete")); } @Test void deleteLinkFailsWhenIdIsNull() throws Exception { mockMvc.perform(post(baseUrl + "delete" + "?id=" + null) .contentType(MediaType.APPLICATION_JSON) .header("Authorization", token)) - .andExpect(status().isInternalServerError()); + .andExpect(status().is4xxClientError()); } @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("$.exceptionMessage").value("Operation forbidden!")); + .andExpect(jsonPath("$.path").value("/api/V1/link/delete")); } } diff --git a/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerTest.java b/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerTest.java index f136364..9e5e750 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/link/LinkControllerTest.java @@ -225,8 +225,9 @@ void editDeletedLinkContentTest() throws Exception { .param("id", String.valueOf(link.getId())) .content(objectMapper.writeValueAsString(request))); - resultActions.andExpect(status().isInternalServerError()) - .andExpect(jsonPath("$.exceptionMessage").value("Link status is invalid for the operation")); + resultActions.andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.message").value("Link status is invalid for the operation")) + .andExpect(jsonPath("$.path").value("/api/V1/link/edit/content")); } /** @@ -287,9 +288,10 @@ void refreshDeletedLinkTest() throws Exception { .contentType(MediaType.APPLICATION_JSON) .param("id", String.valueOf(link.getId()))); - resultActions.andExpect(status().isInternalServerError()) - .andExpect(jsonPath("$.exceptionMessage").value("The link has already been deleted, " + - "no operations are allowed")); + resultActions.andExpect(status().isBadRequest()) + .andExpect(jsonPath("$.message").value("The link has already been deleted, " + + "no operations are allowed")) + .andExpect(jsonPath("$.path").value("/api/V1/link/edit/refresh")); } /** diff --git a/src/test/java/com/linkurlshorter/urlshortener/link/LinkServiceTest.java b/src/test/java/com/linkurlshorter/urlshortener/link/LinkServiceTest.java index 774ba9e..6f33ba6 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/link/LinkServiceTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/link/LinkServiceTest.java @@ -184,7 +184,7 @@ void findByNullShortLinkTest() { */ @Test void findByShortLinkNotFoundTest() { - assertThatThrownBy(() -> linkService.deleteByShortLink("http://link/short")) + assertThatThrownBy(() -> linkService.deleteByShortLink("https://link/short")) .isInstanceOf(NoLinkFoundByShortLinkException.class); } diff --git a/src/test/java/com/linkurlshorter/urlshortener/user/UserControllerIntegrationTest.java b/src/test/java/com/linkurlshorter/urlshortener/user/UserControllerIntegrationTest.java index 6f71aa8..67d68ae 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/user/UserControllerIntegrationTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/user/UserControllerIntegrationTest.java @@ -99,7 +99,9 @@ void changePasswordFailedWhenInvalidPasswordGivenTest(String password) throws Ex .content(objectMapper.writeValueAsString(request))) .andExpect(status().is4xxClientError()) .andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(400)) - .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Validation failed!")); + .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Password " + + "must be at least 8 characters long and contain at least one digit, one uppercase letter, " + + "and one lowercase letter. No spaces are allowed.")); } /** @@ -140,6 +142,6 @@ void changeEmailFailedWhenInvalidEmailGivenTest(String email) throws Exception { .andExpect(status().is4xxClientError()) .andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(400)) .andExpect(MockMvcResultMatchers.jsonPath("$.message") - .value("Validation failed!")); + .value("Email address entered incorrectly!")); } }