Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: P4PU-193 added custom exception #30

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion helm/values-dev.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
microservice-chart:
image:
repository: arcditncorecommonacr.azurecr.io/pagopaarcbe
repository: arcditncorecommonacr.azurecr.io/arcbe
tag: latest
pullPolicy: Always

Expand Down
2 changes: 1 addition & 1 deletion helm/values-prod.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
microservice-chart:
image:
repository: arcpitncorecommonacr.azurecr.io/pagopaarcbe
repository: arcpitncorecommonacr.azurecr.io/arcbe
tag: latest
pullPolicy: Always

Expand Down
2 changes: 1 addition & 1 deletion helm/values-uat.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
microservice-chart:
image:
repository: arcuitncorecommonacr.azurecr.io/pagopaarcbe
repository: arcuitncorecommonacr.azurecr.io/arcbe
tag: latest
pullPolicy: Always

Expand Down
2 changes: 2 additions & 0 deletions openapi/pagopa-arc-be.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ components:
- generic_error
- transaction_not_found_error
- receipt_not_found_error
- invalid_amount
- invalid_date
- invalid_request
error_description:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public ResponseEntity<ErrorDTO> handleBizEventsReceiptNotFoundException(RuntimeE
return handleArcErrorException(ex, request, HttpStatus.NOT_FOUND, ErrorDTO.ErrorEnum.RECEIPT_NOT_FOUND_ERROR);
}

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

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

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

import lombok.Getter;

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

import lombok.Getter;

@Getter
public class BizEventsInvalidDateException extends RuntimeException{
public BizEventsInvalidDateException(String message){super(message);}
}
7 changes: 4 additions & 3 deletions src/main/java/it/gov/pagopa/arc/utils/Utilities.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package it.gov.pagopa.arc.utils;


import it.gov.pagopa.arc.exception.custom.BizEventsInvocationException;
import it.gov.pagopa.arc.exception.custom.BizEventsInvalidAmountException;
import it.gov.pagopa.arc.exception.custom.BizEventsInvalidDateException;

import java.text.NumberFormat;
import java.text.ParseException;
Expand All @@ -26,7 +27,7 @@ private static Double euroStringToDouble(String euroString){
Number parse = nf.parse(euroString);
return parse.doubleValue();
}catch (ParseException e){
throw new BizEventsInvocationException("Invalid amount format");
throw new BizEventsInvalidAmountException("Invalid amount format");
}
}

Expand All @@ -36,7 +37,7 @@ public static ZonedDateTime dateStringToZonedDateTime(String dateString){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX");
return ZonedDateTime.parse(dateString, formatter);
}catch (DateTimeParseException e){
throw new BizEventsInvocationException("Invalid date format");
throw new BizEventsInvalidDateException("Invalid date format");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package it.gov.pagopa.arc.exception;

import ch.qos.logback.classic.LoggerContext;
import it.gov.pagopa.arc.exception.custom.BizEventsInvocationException;
import it.gov.pagopa.arc.exception.custom.BizEventsReceiptNotFoundException;
import it.gov.pagopa.arc.exception.custom.BizEventsTransactionNotFoundException;
import it.gov.pagopa.arc.exception.custom.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -86,6 +84,38 @@ void givenRequestWhenBizEventsServiceReturnErrorThenHandleBizEventsInvocationExc
Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage().contains("A class it.gov.pagopa.arc.exception.custom.BizEventsInvocationException occurred handling request GET /test: HttpStatus 500 - Error"));
}

@Test
void givenRequestWhenBizEventsServiceReturnInvalidAmountThenHandleBizEventsInvalidAmountException() throws Exception {
doThrow(new BizEventsInvalidAmountException("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_amount"))
.andExpect(MockMvcResultMatchers.jsonPath("$.error_description").value("Error"));

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

@Test
void givenRequestWhenBizEventsServiceReturnInvalidDateThenHandleBizEventsInvalidDateException() throws Exception {
doThrow(new BizEventsInvalidDateException("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_date"))
.andExpect(MockMvcResultMatchers.jsonPath("$.error_description").value("Error"));

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

@Test
void givenRequestWhenBizEventsServiceReturnNotFoundErrorThenHandleBizEventsNotFoundExceptionTransactionError() throws Exception {
doThrow(new BizEventsTransactionNotFoundException("Error")).when(testControllerSpy).testEndpointTransactionDetails();
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/it/gov/pagopa/arc/utils/UtilitiesTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.gov.pagopa.arc.utils;

import it.gov.pagopa.arc.exception.custom.BizEventsInvocationException;
import it.gov.pagopa.arc.exception.custom.BizEventsInvalidAmountException;
import it.gov.pagopa.arc.exception.custom.BizEventsInvalidDateException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -40,7 +41,7 @@ void givenWrongEuroStringWhenCallEuroToCentsThenReturnException() {
String euroString = "";
//when
//then
BizEventsInvocationException exception = assertThrows(BizEventsInvocationException.class,
BizEventsInvalidAmountException exception = assertThrows(BizEventsInvalidAmountException.class,
() -> Utilities.euroToCents(euroString));
Assertions.assertEquals("Invalid amount format",exception.getMessage());

Expand All @@ -64,7 +65,7 @@ void givenWrongDateStringWhenCallDateStringToZonedDateTimeThenReturnException(){
String wrongDateString = "";
//when
//then
BizEventsInvocationException exception = assertThrows(BizEventsInvocationException.class,
BizEventsInvalidDateException exception = assertThrows(BizEventsInvalidDateException.class,
() -> Utilities.dateStringToZonedDateTime(wrongDateString));
Assertions.assertEquals("Invalid date format",exception.getMessage());
}
Expand Down