Skip to content

Commit

Permalink
P4ADEV-320-refactoring-accordingly-handbook
Browse files Browse the repository at this point in the history
  • Loading branch information
LarissaASLeite committed May 23, 2024
1 parent 576ffdf commit 4599026
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 - {}",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +29,7 @@
AuthExceptionHandler.class})
class AuthExceptionHandlerTest {

public static final String DATA = "data";
@Autowired
private MockMvc mockMvc;

Expand All @@ -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())
Expand All @@ -59,14 +62,27 @@ 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())
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value("AUTH_TOKEN_EXPIRED_DATE"))
.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"));

}
}

This file was deleted.

0 comments on commit 4599026

Please sign in to comment.