Skip to content

Commit

Permalink
[PAGOPA-2206] fix: replaced NAV with IUV in idempotency key generation (
Browse files Browse the repository at this point in the history
  • Loading branch information
aomegax authored Sep 30, 2024
2 parents ab634e6 + e793373 commit f66d4c8
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 36 deletions.
4 changes: 2 additions & 2 deletions helm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apiVersion: v2
name: pagopa-wisp-converter
description: A service that permits to handle nodoInviaRPT and nodoInviaCarrelloRPT request from WISP, interfacing them with GPD system
type: application
version: 0.211.0
appVersion: 0.3.7
version: 0.214.0
appVersion: 0.3.7-3-PAGOPA-2206
dependencies:
- name: microservice-chart
version: 3.0.0
Expand Down
2 changes: 1 addition & 1 deletion helm/values-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: ""
image:
repository: ghcr.io/pagopa/pagopa-wisp-converter
tag: "0.3.7"
tag: "0.3.7-3-PAGOPA-2206"
pullPolicy: Always
livenessProbe:
httpGet:
Expand Down
2 changes: 1 addition & 1 deletion helm/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: ""
image:
repository: ghcr.io/pagopa/pagopa-wisp-converter
tag: "0.3.7"
tag: "0.3.7-3-PAGOPA-2206"
pullPolicy: Always
livenessProbe:
httpGet:
Expand Down
2 changes: 1 addition & 1 deletion helm/values-uat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: ""
image:
repository: ghcr.io/pagopa/pagopa-wisp-converter
tag: "0.3.7"
tag: "0.3.7-3-PAGOPA-2206"
pullPolicy: Always
livenessProbe:
httpGet:
Expand Down
2 changes: 1 addition & 1 deletion openapi/openapi.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion openapi/openapi_redirect.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"openapi": "3.0.1",
"info": {
"title": "WISP-Converter-redirect",
"version": "0.3.7"
"version": "0.3.7-3-PAGOPA-2206"
},
"servers": [
{
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<groupId>it.gov.pagopa</groupId>
<artifactId>wisp-converter</artifactId>
<version>0.3.7</version>
<version>0.3.7-3-PAGOPA-2206</version>
<name>pagoPA WISP Converter</name>
<description>A service that permits to handle nodoInviaRPT and nodoInviaCarrelloRPT request from WISP, converting them in NMU payments.</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class IdempotencyService {
@Value("${wisp-converter.idempotency.lock-validity-in-minutes}")
private Integer lockValidityInMinutes;

public static String generateIdempotencyKeyId(String sessionId, String noticeNumber, String domainId) {
return String.format("%s_%s_%s", sessionId, noticeNumber, domainId);
public static String generateIdempotencyKeyId(String sessionId, String iuv, String domainId) {
return String.format("%s_%s_%s", sessionId, iuv, domainId);
}

public boolean isIdempotencyKeyProcessable(String idempotencyKey, ReceiptTypeEnum receiptType) {
Expand All @@ -47,7 +47,7 @@ Check if receipt type (set in idempotency key) is equals to the one defined in t
}

// check the processability of the idempotency key
isProcessable = isActiveLockExpired(idempotencyKeyEntity) || IdempotencyStatusEnum.FAILED.equals(idempotencyKeyEntity.getStatus());
isProcessable = !IdempotencyStatusEnum.SUCCESS.equals(idempotencyKeyEntity.getStatus()) && isActiveLockExpired(idempotencyKeyEntity) || IdempotencyStatusEnum.FAILED.equals(idempotencyKeyEntity.getStatus());
}
return isProcessable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import it.gov.pagopa.wispconverter.repository.model.enumz.ClientEnum;
import it.gov.pagopa.wispconverter.repository.model.enumz.InternalStepStatus;
import it.gov.pagopa.wispconverter.repository.model.enumz.OutcomeEnum;
import it.gov.pagopa.wispconverter.repository.model.enumz.ReceiptStatusEnum;
import it.gov.pagopa.wispconverter.service.model.re.ReEventDto;
import it.gov.pagopa.wispconverter.util.Constants;
import it.gov.pagopa.wispconverter.util.JaxbElementUtil;
Expand Down Expand Up @@ -38,12 +39,14 @@ public class PaaInviaRTSenderService {

private final ReService reService;

private final RtReceiptCosmosService rtReceiptCosmosService;

private final JaxbElementUtil jaxbElementUtil;

@Value("${wisp-converter.rt-send.avoid-scheduling-on-states}")
private Set<String> avoidSchedulingOnStates;

public void sendToCreditorInstitution(URI uri, InetSocketAddress proxyAddress, List<Pair<String, String>> headers, String payload) {
public void sendToCreditorInstitution(URI uri, InetSocketAddress proxyAddress, List<Pair<String, String>> headers, String payload, String domainId, String iuv, String ccp) {

try {

Expand All @@ -64,6 +67,8 @@ public void sendToCreditorInstitution(URI uri, InetSocketAddress proxyAddress, L
bodySpec.header(header.getFirst(), header.getSecond());
}

rtReceiptCosmosService.updateReceiptStatus(domainId, iuv, ccp, ReceiptStatusEnum.SENDING);

// Save an RE event in order to track the communication with creditor institution
generateREForRequestToCreditorInstitution(uri.toString(), headers, payload);

Expand All @@ -83,7 +88,9 @@ public void sendToCreditorInstitution(URI uri, InetSocketAddress proxyAddress, L

// set the correct response regarding the creditor institution response
if (avoidReScheduling) {

generateREForAlreadySentRtToCreditorInstitution();

} else if (Constants.KO.equals(esitoPaaInviaRT.getEsito()) || !Constants.OK.equals(esitoPaaInviaRT.getEsito())) {
FaultBean fault = esitoPaaInviaRT.getFault();
String faultCode = "ND";
Expand Down Expand Up @@ -113,8 +120,12 @@ public void sendToCreditorInstitution(URI uri, InetSocketAddress proxyAddress, L
generateREForResponseFromCreditorInstitution(uri.toString(), statusCode, error.getResponseHeaders(), responseBody, OutcomeEnum.RECEIVED_FAILURE, otherInfo);
}


throw new AppException(AppErrorCodeMessageEnum.RECEIPT_GENERATION_GENERIC_ERROR, e.getMessage());
}


rtReceiptCosmosService.updateReceiptStatus(domainId, iuv, ccp, ReceiptStatusEnum.SENT);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ private boolean sendReceiptToCreditorInstitution(SessionDataDTO sessionData, RPT
StationDto station, boolean mustSendNegativeRT) {

boolean isSuccessful = false;
String domainId = rpt.getRpt().getDomain().getDomainId();

/*
From station identifier (the common one defined, not the payment reference), retrieve the data
Expand All @@ -366,7 +367,7 @@ From station identifier (the common one defined, not the payment reference), ret
InetSocketAddress proxyAddress = CommonUtility.constructProxyAddress(uri, station, apimPath);

// idempotency key creation to check if the rt has already been sent
String idempotencyKey = IdempotencyService.generateIdempotencyKeyId(sessionData.getCommonFields().getSessionId(), noticeNumber, rpt.getRpt().getDomain().getDomainId());
String idempotencyKey = IdempotencyService.generateIdempotencyKeyId(sessionData.getCommonFields().getSessionId(), iuv, domainId);

// send to creditor institution only if another receipt wasn't already sent
ReceiptTypeEnum receiptType = mustSendNegativeRT ? ReceiptTypeEnum.KO : ReceiptTypeEnum.OK;
Expand All @@ -381,21 +382,18 @@ From station identifier (the common one defined, not the payment reference), ret
// finally, send the receipt to the creditor institution
IdempotencyStatusEnum idempotencyStatus;
try {
rtReceiptCosmosService.updateReceiptStatus(rpt, ReceiptStatusEnum.SENDING);

// send the receipt to the creditor institution via the URL set in the station configuration
paaInviaRTSenderService.sendToCreditorInstitution(uri, proxyAddress, headers, rawPayload);
String ccp = rpt.getCcp();
paaInviaRTSenderService.sendToCreditorInstitution(uri, proxyAddress, headers, rawPayload, domainId, iuv, ccp);

// generate a new event in RE for store the successful sending of the receipt
generateREForSentRT(rpt, iuv, noticeNumber);
idempotencyStatus = IdempotencyStatusEnum.SUCCESS;
isSuccessful = true;

rtReceiptCosmosService.updateReceiptStatus(rpt, ReceiptStatusEnum.SENT);

} catch (Exception e) {

rtReceiptCosmosService.updateReceiptStatus(rpt, ReceiptStatusEnum.SCHEDULED);

// generate a new event in RE for store the unsuccessful sending of the receipt
String message = e.getMessage();
if (e instanceof AppException appException) {
Expand Down Expand Up @@ -603,11 +601,13 @@ public void scheduleRTSend(SessionDataDTO sessionData, URI uri, InetSocketAddres

// generate a new event in RE for store the successful scheduling of the RT send
generateREForSuccessfulSchedulingSentRT(rpt, rpt.getIuv(), noticeNumber);
rtReceiptCosmosService.updateReceiptStatus(rpt, ReceiptStatusEnum.SCHEDULED);

} catch (Exception e) {

// generate a new event in RE for store the unsuccessful scheduling of the RT send
generateREForFailedSchedulingSentRT(rpt, rpt.getIuv(), noticeNumber, e);
rtReceiptCosmosService.updateReceiptStatus(rpt, ReceiptStatusEnum.NOT_SENT);
}
}

Expand Down Expand Up @@ -729,9 +729,10 @@ public void sendRTKoFromSessionId(String sessionId, InternalStepStatus internalS
for (RPTContentDTO rpt : sessionDataDTO.getRpts().values()) {

String domainId = rpt.getRpt().getDomain().getDomainId();
String iuv = rpt.getIuv();

// idempotency key creation to check if the rt has already been sent
String idempotencyKey = IdempotencyService.generateIdempotencyKeyId(sessionDataDTO.getCommonFields().getSessionId(), null, domainId);
String idempotencyKey = IdempotencyService.generateIdempotencyKeyId(sessionDataDTO.getCommonFields().getSessionId(), iuv, domainId);
idempotencyService.lockIdempotencyKey(idempotencyKey, ReceiptTypeEnum.KO);

String rtRawPayload = generateKoRtFromSessionData(
Expand All @@ -755,7 +756,8 @@ public void sendRTKoFromSessionId(String sessionId, InternalStepStatus internalS
try {

// send the receipt to the creditor institution via the URL set in the station configuration
paaInviaRTSenderService.sendToCreditorInstitution(uri, proxyAddress, headers, rtRawPayload);
String ccp = rpt.getCcp();
paaInviaRTSenderService.sendToCreditorInstitution(uri, proxyAddress, headers, rtRawPayload, domainId, iuv, ccp);

// generate a new event in RE for store the successful sending of the receipt
generateREForSentRT(rpt, rpt.getIuv(), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,25 +176,26 @@ private boolean resendRTToCreditorInstitution(String receiptId, RTRequestEntity

String rawPayload = new String(unzippedPayload);

rtReceiptCosmosService.updateReceiptStatus(ci, iuv, ccp, ReceiptStatusEnum.SENDING);
paaInviaRTSenderService.sendToCreditorInstitution(URI.create(receipt.getUrl()), proxyAddress, extractHeaders(receipt.getHeaders()), rawPayload);
paaInviaRTSenderService.sendToCreditorInstitution(URI.create(receipt.getUrl()), proxyAddress, extractHeaders(receipt.getHeaders()), rawPayload, ci, iuv, ccp);
rtRetryComosService.deleteRTRequestEntity(receipt);
log.debug("Sent receipt [{}]", receiptId);

// generate a new event in RE for store the successful re-sending of the receipt
generateREForSentRT();
isSend = true;
rtReceiptCosmosService.updateReceiptStatus(ci, iuv, ccp, ReceiptStatusEnum.SENT);

} catch (AppException e) {

// generate a new event in RE for store the unsuccessful re-sending of the receipt
generateREForNotSentRT(e);

// Rescheduled due to errors caused by faulty communication with creditor institution
reScheduleReceiptSend(receipt, receiptId, compositedIdForReceipt);

rtReceiptCosmosService.updateReceiptStatus(ci, iuv, ccp, ReceiptStatusEnum.SCHEDULED);
} catch (IOException e) {

rtReceiptCosmosService.updateReceiptStatus(ci, iuv, ccp, ReceiptStatusEnum.NOT_SENT);

throw new AppException(AppErrorCodeMessageEnum.PARSING_INVALID_ZIPPED_PAYLOAD);
}
return isSend;
Expand Down Expand Up @@ -231,11 +232,13 @@ private void reScheduleReceiptSend(RTRequestEntity receipt, String receiptId, St

// generate a new event in RE for store the successful scheduling of the RT send
generateREForSuccessfulReschedulingSentRT();
rtReceiptCosmosService.updateReceiptStatus(ci, iuv, ccp, ReceiptStatusEnum.SCHEDULED);

} catch (Exception e) {

// generate a new event in RE for store the unsuccessful scheduling of the RT send
generateREForFailedReschedulingSentRT(e);
rtReceiptCosmosService.updateReceiptStatus(ci, iuv, ccp, ReceiptStatusEnum.NOT_SENT);
}
} else {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private RTRequestEntity getStoredReceipt(int retries, String rawtype, String url
.receiptType(type)
.url(url)
.headers(List.of("soapaction:paaInviaRT"))
.idempotencyKey("uuid_nav_idDominio")
.idempotencyKey("uuid_iuv_idDominio")
.build();
}

Expand Down Expand Up @@ -218,7 +218,7 @@ void sendToPa_onError_reschedulable(String receiptType) {
when(ccs.getConfigData()).thenReturn(TestUtils.configData("mystation"));

PaaInviaRTSenderService paaInviaRTSenderService = mock(PaaInviaRTSenderService.class);
doThrow(new AppException(AppErrorCodeMessageEnum.PARSING_GENERIC_ERROR)).when(paaInviaRTSenderService).sendToCreditorInstitution(any(), any(), any(), any());
doThrow(new AppException(AppErrorCodeMessageEnum.PARSING_GENERIC_ERROR)).when(paaInviaRTSenderService).sendToCreditorInstitution(any(), any(), any(), any(), any(), any(), any());

ReflectionTestUtils.setField(rtConsumer, "rtRetryComosService", rtRetryComosService);
ReflectionTestUtils.setField(rtConsumer, "rtReceiptCosmosService", rtReceiptCosmosService);
Expand Down Expand Up @@ -276,7 +276,7 @@ void sendToPa_onError_notReschedulable(String receiptType) {
when(ccs.getConfigData()).thenReturn(TestUtils.configData("mystation"));

PaaInviaRTSenderService paaInviaRTSenderService = mock(PaaInviaRTSenderService.class);
doThrow(new AppException(AppErrorCodeMessageEnum.PARSING_GENERIC_ERROR)).when(paaInviaRTSenderService).sendToCreditorInstitution(any(), any(), any(), any());
doThrow(new AppException(AppErrorCodeMessageEnum.PARSING_GENERIC_ERROR)).when(paaInviaRTSenderService).sendToCreditorInstitution(any(), any(), any(), any(), any(), any(), any());

ReflectionTestUtils.setField(rtConsumer, "rtRetryComosService", rtRetryComosService);
ReflectionTestUtils.setField(rtConsumer, "paaInviaRTSenderService", paaInviaRTSenderService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void sendOkReceipt_notSent() throws Exception {

// mocking error response from creditor institution
doThrow(new AppException(AppErrorCodeMessageEnum.RECEIPT_GENERATION_ERROR_RESPONSE_FROM_CREDITOR_INSTITUTION, "PAA_ERRORE_RESPONSE", "PAA_ERRORE_RESPONSE", "Errore PA"))
.when(paaInviaRTSenderService).sendToCreditorInstitution(any(), any(), any(), anyString());
.when(paaInviaRTSenderService).sendToCreditorInstitution(any(), any(), any(), anyString(), anyString(), anyString(), anyString());

mvc.perform(MockMvcRequestBuilders.post("/receipt/ok")
.accept(MediaType.APPLICATION_JSON)
Expand All @@ -231,7 +231,7 @@ void sendOkReceipt_notSent() throws Exception {
assertNotNull(result.getResponse());
});

verify(paaInviaRTSenderService, times(1)).sendToCreditorInstitution(any(), any(), any(), anyString());
verify(paaInviaRTSenderService, times(1)).sendToCreditorInstitution(any(), any(), any(), anyString(), anyString(), anyString(), anyString());
}

@ParameterizedTest
Expand Down Expand Up @@ -427,7 +427,7 @@ void sendKoReceipt_notSent() throws Exception {
.paymentToken("token01")
.build();
doThrow(new AppException(AppErrorCodeMessageEnum.RECEIPT_GENERATION_ERROR_RESPONSE_FROM_CREDITOR_INSTITUTION, "PAA_ERRORE_RESPONSE", "PAA_ERRORE_RESPONSE", "Errore PA"))
.when(paaInviaRTSenderService).sendToCreditorInstitution(any(), any(), any(), anyString());
.when(paaInviaRTSenderService).sendToCreditorInstitution(any(), any(), any(), anyString(), anyString(), anyString(), anyString());

mvc.perform(MockMvcRequestBuilders.post("/receipt/ko")
.accept(MediaType.APPLICATION_JSON)
Expand All @@ -440,6 +440,6 @@ void sendKoReceipt_notSent() throws Exception {
assertNotNull(result.getResponse());
});

verify(paaInviaRTSenderService, times(1)).sendToCreditorInstitution(any(), any(), any(), anyString());
verify(paaInviaRTSenderService, times(1)).sendToCreditorInstitution(any(), any(), any(), anyString(), anyString(), anyString(), anyString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void esitoOK() {
RestClient.Builder builder = mock(RestClient.Builder.class);
ReService reService = mock(ReService.class);
JaxbElementUtil jaxbElementUtil = mock(JaxbElementUtil.class);
RtReceiptCosmosService rtReceiptCosmosService = mock(RtReceiptCosmosService.class);

RestClient client = mock(RestClient.class);
when(builder.build()).thenReturn(client);
Expand All @@ -44,9 +45,9 @@ void esitoOK() {
when(responseSpec.toEntity(String.class))
.thenReturn(ResponseEntity.ok().body(paaInviaRTRisposta));

PaaInviaRTSenderService p = new PaaInviaRTSenderService(builder, reService, jaxbElementUtil);
PaaInviaRTSenderService p = new PaaInviaRTSenderService(builder, reService, rtReceiptCosmosService, jaxbElementUtil);
org.springframework.test.util.ReflectionTestUtils.setField(p, "jaxbElementUtil", new JaxbElementUtil());
p.sendToCreditorInstitution(URI.create("http://pagopa.mock.dev/"), null, List.of(Pair.of("soapaction", "paaInviaRT")), "");
p.sendToCreditorInstitution(URI.create("http://pagopa.mock.dev/"), null, List.of(Pair.of("soapaction", "paaInviaRT")), "", "", "", "");
assertTrue(true);
}

Expand All @@ -55,6 +56,7 @@ void esitoKO() {
RestClient.Builder builder = mock(RestClient.Builder.class);
ReService reService = mock(ReService.class);
JaxbElementUtil jaxbElementUtil = mock(JaxbElementUtil.class);
RtReceiptCosmosService rtReceiptCosmosService = mock(RtReceiptCosmosService.class);
RestClient client = mock(RestClient.class);
when(builder.build()).thenReturn(client);
RestClient.RequestBodyUriSpec requestBodyUriSpec = mock(RestClient.RequestBodyUriSpec.class);
Expand All @@ -75,9 +77,9 @@ void esitoKO() {
when(responseSpec.toEntity(PaaInviaRTRisposta.class))
.thenReturn(ResponseEntity.ok().body(paaInviaRTRisposta));

PaaInviaRTSenderService p = new PaaInviaRTSenderService(builder, reService, jaxbElementUtil);
PaaInviaRTSenderService p = new PaaInviaRTSenderService(builder, reService, rtReceiptCosmosService, jaxbElementUtil);
try {
p.sendToCreditorInstitution(URI.create("http://pagopa.mock.dev/"), null, List.of(Pair.of("soapaction", "paaInviaRT")), "");
p.sendToCreditorInstitution(URI.create("http://pagopa.mock.dev/"), null, List.of(Pair.of("soapaction", "paaInviaRT")), "", "", "", "");
fail();
} catch (AppException e) {
assertTrue(true);
Expand Down

0 comments on commit f66d4c8

Please sign in to comment.