Skip to content

Commit

Permalink
fix: [P4PU-753] added custom exception (#176)
Browse files Browse the repository at this point in the history
* P4PU-753 added custom exception

* P4PU-753 added tests
  • Loading branch information
Giuseppe-LaManna authored Dec 9, 2024
1 parent 75fb0e0 commit 3a32ea6
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import feign.FeignException;
import it.gov.pagopa.arc.connector.gpd.dto.GPDPaymentNoticeDetailsDTO;
import it.gov.pagopa.arc.exception.custom.GPDInvalidRequestException;
import it.gov.pagopa.arc.exception.custom.GPDInvocationException;
import it.gov.pagopa.arc.exception.custom.GPDPaymentNoticeDetailsNotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
Expand All @@ -25,12 +28,11 @@ public GPDPaymentNoticeDetailsDTO getPaymentNoticeDetails(String userId, String
return gpdRestClient.paymentNoticeDetails(apiKey, organizationFiscalCode, iupd);
}catch (FeignException e){
if(e.status() == HttpStatus.NOT_FOUND.value()){
// The RuntimeException will be replaced by the custom exception in its specific task
throw new RuntimeException("An error occurred handling request from GPD to retrieve payment notice with organizationFiscalCode [%s] and iupd [%s] for the current user with userId [%s]".formatted(organizationFiscalCode, iupd, userId));
throw new GPDPaymentNoticeDetailsNotFoundException("An error occurred handling request from GPD to retrieve payment notice with organizationFiscalCode [%s] and iupd [%s] for the current user with userId [%s]".formatted(organizationFiscalCode, iupd, userId));
} else if(e.status() == HttpStatus.BAD_REQUEST.value()) {
throw new RuntimeException("One or more inputs provided during the request from GPD are invalid");
throw new GPDInvalidRequestException("One or more inputs provided during the request from GPD are invalid");
}else{
throw new RuntimeException("An error occurred handling request from GPD service");
throw new GPDInvocationException("An error occurred handling request from GPD service");
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/it/gov/pagopa/arc/exception/ArcExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ public ResponseEntity<ErrorDTO> handleGenericRuntimeException(RuntimeException e
return handleArcErrorException(ex, request, HttpStatus.INTERNAL_SERVER_ERROR, ErrorDTO.ErrorEnum.GENERIC_ERROR);
}

@ExceptionHandler(GPDInvalidRequestException.class)
public ResponseEntity<ErrorDTO> handleGPDInvalidRequestException(RuntimeException ex, HttpServletRequest request){
return handleArcErrorException(ex, request, HttpStatus.BAD_REQUEST, ErrorDTO.ErrorEnum.INVALID_REQUEST);
}

@ExceptionHandler(GPDPaymentNoticeDetailsNotFoundException.class)
public ResponseEntity<ErrorDTO> handleGPDNotFoundException(RuntimeException ex, HttpServletRequest request){
return handleArcErrorException(ex, request, HttpStatus.NOT_FOUND, ErrorDTO.ErrorEnum.PAYMENT_NOTICE_NOT_FOUND_ERROR);
}

@ExceptionHandler(GPDInvocationException.class)
public ResponseEntity<ErrorDTO> handleGPDInvocationException(RuntimeException ex, HttpServletRequest request){
return handleArcErrorException(ex, request, HttpStatus.INTERNAL_SERVER_ERROR, ErrorDTO.ErrorEnum.GENERIC_ERROR);
}

private static ResponseEntity<ErrorDTO> handleArcErrorException(RuntimeException ex, HttpServletRequest request, HttpStatus httpStatus, ErrorDTO.ErrorEnum errorEnum) {
String message = ex.getMessage();
log.error("A {} occurred handling request {}: HttpStatus {} - {}",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.gov.pagopa.arc.exception.custom;

import lombok.Getter;

@Getter
public class GPDInvalidRequestException extends RuntimeException{
public GPDInvalidRequestException(String message){
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package it.gov.pagopa.arc.exception.custom;

import lombok.Getter;

@Getter
public class GPDInvocationException extends RuntimeException{

public GPDInvocationException(String message){
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.gov.pagopa.arc.exception.custom;

import lombok.Getter;

@Getter
public class GPDPaymentNoticeDetailsNotFoundException extends RuntimeException{
public GPDPaymentNoticeDetailsNotFoundException(String message){
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import it.gov.pagopa.arc.config.FeignConfig;
import it.gov.pagopa.arc.config.WireMockConfig;
import it.gov.pagopa.arc.connector.gpd.dto.GPDPaymentNoticeDetailsDTO;
import it.gov.pagopa.arc.exception.custom.GPDInvalidRequestException;
import it.gov.pagopa.arc.exception.custom.GPDInvocationException;
import it.gov.pagopa.arc.exception.custom.GPDPaymentNoticeDetailsNotFoundException;
import it.gov.pagopa.arc.fakers.connector.gpd.GPDPaymentNoticeDetailsDTOFaker;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -70,21 +73,21 @@ void givenPDWithInstallmentsPOWhenGetPaymentNoticeDetailsThenReturnGPDPaymentNot

@Test
void givenNotFoundIUPDWhenGetPaymentNoticeDetailsThenThrowException() {
RuntimeException ex = assertThrows(RuntimeException.class,
GPDPaymentNoticeDetailsNotFoundException ex = assertThrows(GPDPaymentNoticeDetailsNotFoundException.class,
() -> gpdConnector.getPaymentNoticeDetails("USER_ID", "DUMMY_ORGANIZATION_FISCAL_CODE", "IUPD_NOT_FOUND_0"));
assertEquals("An error occurred handling request from GPD to retrieve payment notice with organizationFiscalCode [DUMMY_ORGANIZATION_FISCAL_CODE] and iupd [IUPD_NOT_FOUND_0] for the current user with userId [USER_ID]", ex.getMessage());
}

@Test
void givenWrongIUPDWhenGetPaymentNoticeDetailsThenThrowException() {
RuntimeException ex = assertThrows(RuntimeException.class,
GPDInvalidRequestException ex = assertThrows(GPDInvalidRequestException.class,
() -> gpdConnector.getPaymentNoticeDetails("USER_ID", "DUMMY_ORGANIZATION_FISCAL_CODE", "IUPD_BAD_REQUEST_0"));
assertEquals("One or more inputs provided during the request from GPD are invalid", ex.getMessage());
}

@Test
void givenErrorIUPDWhenGetPaymentNoticeDetailsThenThrowException() {
RuntimeException ex = assertThrows(RuntimeException.class,
GPDInvocationException ex = assertThrows(GPDInvocationException.class,
() -> gpdConnector.getPaymentNoticeDetails("USER_ID", "DUMMY_ORGANIZATION_FISCAL_CODE", "IUPD_ERROR_0"));
assertEquals("An error occurred handling request from GPD service", ex.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ArcExceptionHandlerTest {

@SpyBean
private TestController testControllerSpy;
private static final String EVENT_ID = "EVENT_ID";

private MemoryAppender memoryAppender;

@RestController
Expand All @@ -51,10 +51,6 @@ void testEndpoint() {
//Needed for testing notice API
}

@GetMapping("/test/{Id}")
void testEndpointDetails() {
//Needed for testing notice API
}
}

@BeforeEach
Expand Down Expand Up @@ -178,9 +174,9 @@ void givenInvalidEmailWhenExtractNameFromEmailAssistanceTokenThenHandleZendeskAs

@Test
void givenRequestWhenBizEventsServiceReturnNotFoundErrorThenHandleBizEventsPaidNoticeNotFoundExceptionError() throws Exception {
doThrow(new BizEventsPaidNoticeNotFoundException("Error")).when(testControllerSpy).testEndpointDetails();
doThrow(new BizEventsPaidNoticeNotFoundException("Error")).when(testControllerSpy).testEndpoint();

mockMvc.perform(MockMvcRequestBuilders.get("/test/{eventId}", EVENT_ID)
mockMvc.perform(MockMvcRequestBuilders.get("/test")
.param(DATA, DATA)
.header(HEADER,HEADER)
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -189,7 +185,7 @@ void givenRequestWhenBizEventsServiceReturnNotFoundErrorThenHandleBizEventsPaidN
.andExpect(MockMvcResultMatchers.jsonPath("$.error").value("notice_not_found_error"))
.andExpect(MockMvcResultMatchers.jsonPath("$.error_description").value("Error"));

Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage().contains("A class it.gov.pagopa.arc.exception.custom.BizEventsPaidNoticeNotFoundException occurred handling request GET /test/EVENT_ID: HttpStatus 404 - Error"));
Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage().contains("A class it.gov.pagopa.arc.exception.custom.BizEventsPaidNoticeNotFoundException occurred handling request GET /test: HttpStatus 404 - Error"));
}

@Test
Expand All @@ -203,4 +199,68 @@ void givenGenericErrorThenThrowException() throws Exception {
.andExpect(MockMvcResultMatchers.jsonPath("$.error").value("generic_error"));
}

@Test
void givenRequestWhenBizEventsServiceReturnNotFoundErrorThenHandleBizEventsNotFoundExceptionPdfError() throws Exception {
doThrow(new BizEventsReceiptNotFoundException("Error")).when(testControllerSpy).testEndpoint();

mockMvc.perform(MockMvcRequestBuilders.get("/test")
.param(DATA, DATA)
.header(HEADER,HEADER)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isNotFound())
.andExpect(MockMvcResultMatchers.jsonPath("$.error").value("receipt_not_found_error"))
.andExpect(MockMvcResultMatchers.jsonPath("$.error_description").value("Error"));

Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage().contains("A class it.gov.pagopa.arc.exception.custom.BizEventsReceiptNotFoundException occurred handling request GET /test: HttpStatus 404 - Error"));
}

@Test
void givenRequestWhenGPDReturnBadRequestErrorThenHandleGPDInvalidRequestException() throws Exception {
doThrow(new GPDInvalidRequestException("Error")).when(testControllerSpy).testEndpoint();

mockMvc.perform(MockMvcRequestBuilders.get("/test")
.param(DATA, DATA)
.header(HEADER,HEADER)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isBadRequest())
.andExpect(MockMvcResultMatchers.jsonPath("$.error").value("invalid_request"))
.andExpect(MockMvcResultMatchers.jsonPath("$.error_description").value("Error"));

Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage().contains("A class it.gov.pagopa.arc.exception.custom.GPDInvalidRequestException occurred handling request GET /test: HttpStatus 400 - Error"));
}

@Test
void givenRequestWhenGPDReturnNotFoundErrorThenHandleGPDPaymentNoticeDetailsNotFoundException() throws Exception {
doThrow(new GPDPaymentNoticeDetailsNotFoundException("Error")).when(testControllerSpy).testEndpoint();

mockMvc.perform(MockMvcRequestBuilders.get("/test")
.param(DATA, DATA)
.header(HEADER,HEADER)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isNotFound())
.andExpect(MockMvcResultMatchers.jsonPath("$.error").value("payment_notice_not_found_error"))
.andExpect(MockMvcResultMatchers.jsonPath("$.error_description").value("Error"));

Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage().contains("A class it.gov.pagopa.arc.exception.custom.GPDPaymentNoticeDetailsNotFoundException occurred handling request GET /test: HttpStatus 404 - Error"));
}

@Test
void givenRequestWhenGPDReturnErrorThenHandleGPDInvocationException() throws Exception {
doThrow(new GPDInvocationException("Error")).when(testControllerSpy).testEndpoint();

mockMvc.perform(MockMvcRequestBuilders.get("/test")
.param(DATA, DATA)
.header(HEADER,HEADER)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isInternalServerError())
.andExpect(MockMvcResultMatchers.jsonPath("$.error").value("generic_error"))
.andExpect(MockMvcResultMatchers.jsonPath("$.error_description").value("Error"));

Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage().contains("A class it.gov.pagopa.arc.exception.custom.GPDInvocationException occurred handling request GET /test: HttpStatus 500 - Error"));
}

}

0 comments on commit 3a32ea6

Please sign in to comment.