diff --git a/src/main/java/it/gov/pagopa/payhub/auth/exception/AuthExceptionHandler.java b/src/main/java/it/gov/pagopa/payhub/auth/exception/AuthExceptionHandler.java index 072257b0..bc7ca817 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/exception/AuthExceptionHandler.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/exception/AuthExceptionHandler.java @@ -8,12 +8,12 @@ import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; +import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; -import static it.gov.pagopa.payhub.model.generated.AuthErrorDTO.CodeEnum.INVALID_TOKEN; -import static it.gov.pagopa.payhub.model.generated.AuthErrorDTO.CodeEnum.TOKEN_EXPIRED_DATE; +import static it.gov.pagopa.payhub.model.generated.AuthErrorDTO.CodeEnum.*; @RestControllerAdvice @Slf4j @@ -36,6 +36,19 @@ public AuthErrorDTO handleTokenExpiredException(TokenExpiredException ex, HttpSe return new AuthErrorDTO(TOKEN_EXPIRED_DATE, message); } + @ExceptionHandler(MissingServletRequestParameterException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public AuthErrorDTO handleMissingServletRequestParameterException( + MissingServletRequestParameterException ex, HttpServletRequest request) { + + String message = ex.getMessage(); + + log.info("A MissingServletRequestParameterException occurred handling request {}: HttpStatus 400 - {}", + AuthExceptionHandler.getRequestDetails(request), message); + log.debug("Something went wrong handling request", ex); + return new AuthErrorDTO(INVALID_REQUEST, message); + } + private static String logAndReturnUnauthorizedExceptionMessage(RuntimeException ex, HttpServletRequest request) { String message = ex.getMessage(); log.info("A {} occurred handling request {}: HttpStatus 401 - {}", diff --git a/src/main/java/it/gov/pagopa/payhub/auth/exception/ValidationExceptionHandler.java b/src/main/java/it/gov/pagopa/payhub/auth/exception/ValidationExceptionHandler.java deleted file mode 100644 index 2fb10a42..00000000 --- a/src/main/java/it/gov/pagopa/payhub/auth/exception/ValidationExceptionHandler.java +++ /dev/null @@ -1,43 +0,0 @@ -package it.gov.pagopa.payhub.auth.exception; - -import it.gov.pagopa.payhub.model.generated.AuthErrorDTO; -import jakarta.servlet.http.HttpServletRequest; -import lombok.extern.slf4j.Slf4j; -import org.springframework.core.Ordered; -import org.springframework.core.annotation.Order; -import org.springframework.http.HttpStatus; -import org.springframework.lang.Nullable; -import org.springframework.web.bind.MissingServletRequestParameterException; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestControllerAdvice; - -import java.util.Optional; - -import static it.gov.pagopa.payhub.model.generated.AuthErrorDTO.CodeEnum.INVALID_REQUEST; - -@RestControllerAdvice -@Slf4j -@Order(Ordered.HIGHEST_PRECEDENCE) -public class ValidationExceptionHandler { - - private final AuthErrorDTO templateValidationErrorDTO; - - public ValidationExceptionHandler(@Nullable AuthErrorDTO templateValidationErrorDTO) { - this.templateValidationErrorDTO = Optional.ofNullable(templateValidationErrorDTO) - .orElse(new AuthErrorDTO(INVALID_REQUEST, "Invalid request")); - } - - @ExceptionHandler(MissingServletRequestParameterException.class) - @ResponseStatus(HttpStatus.BAD_REQUEST) - public AuthErrorDTO handleMissingServletRequestParameterException( - MissingServletRequestParameterException ex, HttpServletRequest request) { - - String message = ex.getMessage(); - - log.info("A MissingServletRequestParameterException occurred handling request {}: HttpStatus 400 - {}", - AuthExceptionHandler.getRequestDetails(request), message); - log.debug("Something went wrong handling request", ex); - return new AuthErrorDTO(templateValidationErrorDTO.getCode(), message); - } -} diff --git a/src/test/java/it/gov/pagopa/payhub/auth/controller/AuthControllerTest.java b/src/test/java/it/gov/pagopa/payhub/auth/controller/AuthControllerTest.java index 4bb683da..37b3e18a 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/controller/AuthControllerTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/controller/AuthControllerTest.java @@ -1,7 +1,7 @@ package it.gov.pagopa.payhub.auth.controller; import com.fasterxml.jackson.databind.ObjectMapper; -import it.gov.pagopa.payhub.auth.exception.ValidationExceptionHandler; +import it.gov.pagopa.payhub.auth.exception.AuthExceptionHandler; import it.gov.pagopa.payhub.auth.service.AuthService; import it.gov.pagopa.payhub.model.generated.AuthErrorDTO; import org.junit.jupiter.api.Assertions; @@ -19,7 +19,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @WebMvcTest(AuthControllerImpl.class) -@Import({ValidationExceptionHandler.class}) +@Import({AuthExceptionHandler.class}) class AuthControllerTest { @Autowired private MockMvc mockMvc; diff --git a/src/test/java/it/gov/pagopa/payhub/auth/exception/AuthExceptionHandlerTest.java b/src/test/java/it/gov/pagopa/payhub/auth/exception/AuthExceptionHandlerTest.java index 9c610bee..7845b647 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/exception/AuthExceptionHandlerTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/exception/AuthExceptionHandlerTest.java @@ -17,6 +17,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import static org.mockito.Mockito.doThrow; @@ -28,6 +29,7 @@ AuthExceptionHandler.class}) class AuthExceptionHandlerTest { + public static final String DATA = "data"; @Autowired private MockMvc mockMvc; @@ -39,16 +41,17 @@ class AuthExceptionHandlerTest { static class TestController { @GetMapping("/test") - String testEndpoint() { + String testEndpoint(@RequestParam(DATA) String data) { return "OK"; } } @Test void handleInvalidTokenException() throws Exception { - doThrow(new InvalidTokenException("Error")).when(testControllerSpy).testEndpoint(); + doThrow(new InvalidTokenException("Error")).when(testControllerSpy).testEndpoint(DATA); mockMvc.perform(MockMvcRequestBuilders.get("/test") + .param(DATA, DATA) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isUnauthorized()) @@ -59,9 +62,10 @@ void handleInvalidTokenException() throws Exception { @Test void handleTokenExpiredException() throws Exception { - doThrow(new TokenExpiredException("Error")).when(testControllerSpy).testEndpoint(); + doThrow(new TokenExpiredException("Error")).when(testControllerSpy).testEndpoint(DATA); mockMvc.perform(MockMvcRequestBuilders.get("/test") + .param(DATA, DATA) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isUnauthorized()) @@ -69,4 +73,16 @@ void handleTokenExpiredException() throws Exception { .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Error")); } + + @Test + void handleMissingServletRequestParameterException() throws Exception { + + mockMvc.perform(MockMvcRequestBuilders.get("/test") + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isBadRequest()) + .andExpect(MockMvcResultMatchers.jsonPath("$.code").value("AUTH_INVALID_REQUEST")) + .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Required request parameter 'data' for method parameter type String is not present")); + + } } \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/payhub/auth/exception/ValidationExceptionHandlerTest.java b/src/test/java/it/gov/pagopa/payhub/auth/exception/ValidationExceptionHandlerTest.java deleted file mode 100644 index f20cb6a5..00000000 --- a/src/test/java/it/gov/pagopa/payhub/auth/exception/ValidationExceptionHandlerTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package it.gov.pagopa.payhub.auth.exception; - -import com.fasterxml.jackson.databind.ObjectMapper; -import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.boot.test.mock.mockito.SpyBean; -import org.springframework.http.MediaType; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import org.springframework.test.web.servlet.result.MockMvcResultMatchers; -import org.springframework.web.bind.annotation.*; - -@ExtendWith({SpringExtension.class, MockitoExtension.class}) -@WebMvcTest(value = {ValidationExceptionHandlerTest.TestController.class}, excludeAutoConfiguration = SecurityAutoConfiguration.class) -@ContextConfiguration(classes = { - ValidationExceptionHandlerTest.TestController.class, - ValidationExceptionHandler.class}) -class ValidationExceptionHandlerTest { - - @Autowired - private MockMvc mockMvc; - - @Autowired - ObjectMapper objectMapper; - - @SpyBean - private TestController testControllerSpy; - - @RestController - @Slf4j - static class TestController { - - @GetMapping("/test") - String testEndpoint(@RequestParam("data") String data) { - return "OK"; - } - } - - @Test - void handleMissingServletRequestParameterException() throws Exception { - - mockMvc.perform(MockMvcRequestBuilders.get("/test") - .contentType(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON)) - .andExpect(MockMvcResultMatchers.status().isBadRequest()) - .andExpect(MockMvcResultMatchers.jsonPath("$.code").value("AUTH_INVALID_REQUEST")) - .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Required request parameter 'data' for method parameter type String is not present")); - - } -} \ No newline at end of file