From a261b5d13c4c6e5d5b808377230ed0b4ef9409a4 Mon Sep 17 00:00:00 2001 From: nastiausenko Date: Sun, 21 Apr 2024 09:25:38 +0300 Subject: [PATCH 1/2] return to valid version --- .../exception/GlobalExceptionHandler.java | 27 +++++++++++++++++-- .../auth/AuthControllerIntegrationTest.java | 6 ++--- .../urlshortener/auth/AuthControllerTest.java | 24 ++++++++--------- .../urlshortener/link/LinkControllerTest.java | 12 +++++---- .../urlshortener/link/LinkServiceTest.java | 5 ---- 5 files changed, 47 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java b/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java index 8c9fe9d..b7404fe 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java @@ -1,8 +1,7 @@ package com.linkurlshorter.urlshortener.exception; import com.linkurlshorter.urlshortener.auth.exception.EmailAlreadyTakenException; -import com.linkurlshorter.urlshortener.link.ForbiddenException; -import com.linkurlshorter.urlshortener.link.NoLinkFoundByIdException; +import com.linkurlshorter.urlshortener.link.*; import com.linkurlshorter.urlshortener.user.NoSuchEmailFoundException; import com.linkurlshorter.urlshortener.user.NoUserFoundByEmailException; import com.linkurlshorter.urlshortener.user.NoUserFoundByIdException; @@ -134,5 +133,29 @@ public ResponseEntity handleNoLinkFoundByIdException( 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/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java b/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java index 8e5a855..af236b1 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerIntegrationTest.java @@ -70,13 +70,13 @@ void loginSuccessfulTest() throws Exception { */ @Test void loginFailedWhenUserDoesNotExistTest() throws Exception { - authRequest = new AuthRequest("user-not-found@example.com", "Pass1234"); + authRequest = new AuthRequest("userNotFound@example.com", "Pass1234"); this.mockMvc.perform(MockMvcRequestBuilders.post(baseUrl + "login") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(authRequest))) .andExpect(MockMvcResultMatchers.status().is4xxClientError()) - .andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(400)) - .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Email address entered incorrectly!")); + .andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(401)) + .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("No user by provided email found")); } /** diff --git a/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerTest.java b/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerTest.java index 41a771f..86baf9c 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/auth/AuthControllerTest.java @@ -5,7 +5,6 @@ import com.linkurlshorter.urlshortener.auth.exception.EmailAlreadyTakenException; import com.linkurlshorter.urlshortener.TestConfig; import com.linkurlshorter.urlshortener.security.SecurityConfig; -//import com.linkurlshorter.urlshortener.security.UnauthorizedException; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; @@ -13,6 +12,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; @@ -91,15 +91,15 @@ void loginSuccessfulTest() throws Exception { /** * Test case for the {@link AuthController#login(AuthRequest)} method when the user is not registered. */ -// @Test -// void loginFailedTest() throws Exception { -// AuthRequest request = new AuthRequest("test3@email.com", "Password1"); -// when(authService.loginUser(request)).thenThrow(UnauthorizedException.class); -// -// ResultActions resultActions = mockMvc.perform(post("/api/V1/auth/login") -// .contentType(MediaType.APPLICATION_JSON) -// .content(objectMapper.writeValueAsString(request))); -// -// resultActions.andExpect(status().isUnauthorized()); -// } + @Test + void loginFailedTest() throws Exception { + AuthRequest request = new AuthRequest("test3@email.com", "Password1"); + when(authService.loginUser(request)).thenThrow(UsernameNotFoundException.class); + + ResultActions resultActions = mockMvc.perform(post("/api/V1/auth/login") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(request))); + + resultActions.andExpect(status().isUnauthorized()); + } } 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 5fe16ea..774ba9e 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/link/LinkServiceTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/link/LinkServiceTest.java @@ -2,17 +2,12 @@ import com.linkurlshorter.urlshortener.user.User; import com.linkurlshorter.urlshortener.user.UserRole; -import jakarta.persistence.EntityManager; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -import org.springframework.boot.test.context.SpringBootTest; import java.time.LocalDateTime; import java.util.*; From b4209fe3054477dea2a65593b79836085d2f41bc Mon Sep 17 00:00:00 2001 From: nastiausenko Date: Sun, 21 Apr 2024 09:26:16 +0300 Subject: [PATCH 2/2] remove unnecessary constructors --- .../urlshortener/link/DeletedLinkException.java | 4 ---- .../linkurlshorter/urlshortener/link/ForbiddenException.java | 4 ---- .../urlshortener/link/InternalServerLinkException.java | 4 ---- .../linkurlshorter/urlshortener/link/LinkStatusException.java | 4 ---- .../urlshortener/link/NoLinkFoundByIdException.java | 4 ---- .../urlshortener/link/NoLinkFoundByShortLinkException.java | 4 ---- .../urlshortener/link/NullLinkPropertyException.java | 4 ---- .../urlshortener/user/NoSuchEmailFoundException.java | 4 ---- .../urlshortener/user/NoUserFoundByEmailException.java | 4 ---- .../urlshortener/user/NoUserFoundByIdException.java | 4 ---- .../linkurlshorter/urlshortener/user/NullEmailException.java | 4 ---- 11 files changed, 44 deletions(-) diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/DeletedLinkException.java b/src/main/java/com/linkurlshorter/urlshortener/link/DeletedLinkException.java index b241f0d..2e7b046 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/DeletedLinkException.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/DeletedLinkException.java @@ -11,8 +11,4 @@ public class DeletedLinkException extends RuntimeException { public DeletedLinkException() { super(DEFAULT_MSG); } - - public DeletedLinkException(String msg) { - super(msg); - } } diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/ForbiddenException.java b/src/main/java/com/linkurlshorter/urlshortener/link/ForbiddenException.java index 6cbd55b..86924fb 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/ForbiddenException.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/ForbiddenException.java @@ -5,8 +5,4 @@ public class ForbiddenException extends RuntimeException { public ForbiddenException(String message) { super(message); } - - public ForbiddenException(String message, Throwable cause) { - super(message, cause); - } } diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/InternalServerLinkException.java b/src/main/java/com/linkurlshorter/urlshortener/link/InternalServerLinkException.java index 87af4ed..9db6fd9 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/InternalServerLinkException.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/InternalServerLinkException.java @@ -12,8 +12,4 @@ public class InternalServerLinkException extends RuntimeException { public InternalServerLinkException() { super(DEFAULT_MSG); } - - public InternalServerLinkException(String msg) { - super(msg); - } } diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/LinkStatusException.java b/src/main/java/com/linkurlshorter/urlshortener/link/LinkStatusException.java index 9776cba..cc17f5a 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/LinkStatusException.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/LinkStatusException.java @@ -16,8 +16,4 @@ public class LinkStatusException extends RuntimeException { public LinkStatusException() { super(MSG); } - - public LinkStatusException(String message) { - super(message); - } } diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/NoLinkFoundByIdException.java b/src/main/java/com/linkurlshorter/urlshortener/link/NoLinkFoundByIdException.java index 11f05b0..c1040a8 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/NoLinkFoundByIdException.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/NoLinkFoundByIdException.java @@ -9,8 +9,4 @@ public class NoLinkFoundByIdException extends RuntimeException { public NoLinkFoundByIdException() { super(DEFAULT_MSG); } - - public NoLinkFoundByIdException(String msg) { - super(msg); - } } diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/NoLinkFoundByShortLinkException.java b/src/main/java/com/linkurlshorter/urlshortener/link/NoLinkFoundByShortLinkException.java index 262c486..ff7fcda 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/NoLinkFoundByShortLinkException.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/NoLinkFoundByShortLinkException.java @@ -9,8 +9,4 @@ public class NoLinkFoundByShortLinkException extends RuntimeException { public NoLinkFoundByShortLinkException() { super(DEFAULT_MSG); } - - public NoLinkFoundByShortLinkException(String msg) { - super(msg); - } } diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/NullLinkPropertyException.java b/src/main/java/com/linkurlshorter/urlshortener/link/NullLinkPropertyException.java index 78dae49..3619b7c 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/NullLinkPropertyException.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/NullLinkPropertyException.java @@ -15,8 +15,4 @@ public class NullLinkPropertyException extends RuntimeException { public NullLinkPropertyException() { super(DEFAULT_MSG); } - - public NullLinkPropertyException(String msg) { - super(msg); - } } diff --git a/src/main/java/com/linkurlshorter/urlshortener/user/NoSuchEmailFoundException.java b/src/main/java/com/linkurlshorter/urlshortener/user/NoSuchEmailFoundException.java index ee69ab0..564a890 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/user/NoSuchEmailFoundException.java +++ b/src/main/java/com/linkurlshorter/urlshortener/user/NoSuchEmailFoundException.java @@ -19,8 +19,4 @@ public class NoSuchEmailFoundException extends RuntimeException { public NoSuchEmailFoundException() { super(MSG); } - - public NoSuchEmailFoundException(String msg) { - super(msg); - } } diff --git a/src/main/java/com/linkurlshorter/urlshortener/user/NoUserFoundByEmailException.java b/src/main/java/com/linkurlshorter/urlshortener/user/NoUserFoundByEmailException.java index e9c8f75..d3a582e 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/user/NoUserFoundByEmailException.java +++ b/src/main/java/com/linkurlshorter/urlshortener/user/NoUserFoundByEmailException.java @@ -19,8 +19,4 @@ public class NoUserFoundByEmailException extends RuntimeException { public NoUserFoundByEmailException() { super(DEFAULT_MSG); } - - public NoUserFoundByEmailException(String msg) { - super(msg); - } } diff --git a/src/main/java/com/linkurlshorter/urlshortener/user/NoUserFoundByIdException.java b/src/main/java/com/linkurlshorter/urlshortener/user/NoUserFoundByIdException.java index 9751a97..518adb0 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/user/NoUserFoundByIdException.java +++ b/src/main/java/com/linkurlshorter/urlshortener/user/NoUserFoundByIdException.java @@ -19,8 +19,4 @@ public class NoUserFoundByIdException extends RuntimeException { public NoUserFoundByIdException() { super(DEFAULT_MSG); } - - public NoUserFoundByIdException(String msg) { - super(msg); - } } diff --git a/src/main/java/com/linkurlshorter/urlshortener/user/NullEmailException.java b/src/main/java/com/linkurlshorter/urlshortener/user/NullEmailException.java index 973ea22..bd8f2fd 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/user/NullEmailException.java +++ b/src/main/java/com/linkurlshorter/urlshortener/user/NullEmailException.java @@ -21,8 +21,4 @@ public class NullEmailException extends NullUserPropertyException { public NullEmailException() { super(MSG); } - - public NullEmailException(String msg) { - super(msg); - } } \ No newline at end of file