Skip to content

Commit

Permalink
Tpp Connector Error Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitolo-Andrea committed Nov 13, 2024
1 parent 0188a17 commit 961d4d1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,17 @@ public Mono<CitizenConsentDTO> updateTppState(String fiscalCode, String tppId, b
Utils.createSHA256(fiscalCode), inputSanify(tppId), tppState);

return tppConnector.get(tppId)
.switchIfEmpty(Mono.error(exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, ExceptionMessage.TPP_NOT_FOUND)))
.flatMap(tppResponse -> {
if (!validationService.isTppValid(tppResponse)) {
return Mono.error(exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, ExceptionMessage.TPP_NOT_FOUND));
}

return citizenRepository.findByFiscalCodeAndTppId(fiscalCode, tppId)
.switchIfEmpty(Mono.error(exceptionMap.throwException
(ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during update state process")))
.flatMap(citizenConsent -> {
ConsentDetails consentDetails = citizenConsent.getConsents().get(tppId);
if (consentDetails != null) {
consentDetails.setTppState(tppState);
} else {
return Mono.error(exceptionMap.throwException
(ExceptionName.CITIZEN_NOT_ONBOARDED, "ConsentDetails is null for this tppId"));
}
return citizenRepository.save(citizenConsent);
})
.map(mapperToDTO::map)
.doOnSuccess(savedConsent -> log.info("[EMD][CITIZEN][UPDATE-CHANNEL-STATE] Updated state"));
});
.onErrorMap(error ->exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, ExceptionMessage.TPP_NOT_FOUND))
.flatMap(tppResponse -> citizenRepository.findByFiscalCodeAndTppId(fiscalCode, tppId)
.switchIfEmpty(Mono.error(exceptionMap.throwException
(ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during update state process")))
.flatMap(citizenConsent -> {
ConsentDetails consentDetails = citizenConsent.getConsents().get(tppId);
consentDetails.setTppState(tppState);
return citizenRepository.save(citizenConsent);
})
.map(mapperToDTO::map)
.doOnSuccess(savedConsent -> log.info("[EMD][CITIZEN][UPDATE-CHANNEL-STATE] Updated state")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package it.gov.pagopa.onboarding.citizen.validation;

import it.gov.pagopa.onboarding.citizen.dto.CitizenConsentDTO;
import it.gov.pagopa.onboarding.citizen.dto.TppDTO;
import it.gov.pagopa.onboarding.citizen.model.CitizenConsent;
import reactor.core.publisher.Mono;

Expand All @@ -11,5 +10,4 @@ public interface CitizenConsentValidationService {

Mono<CitizenConsentDTO> validateTppAndSaveConsent(String fiscalCode, String tppId, CitizenConsent citizenConsent);

boolean isTppValid(TppDTO tppResponse);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import it.gov.pagopa.common.utils.Utils;
import it.gov.pagopa.onboarding.citizen.configuration.ExceptionMap;
import it.gov.pagopa.onboarding.citizen.connector.tpp.TppConnectorImpl;
import it.gov.pagopa.onboarding.citizen.constants.CitizenConstants;
import it.gov.pagopa.onboarding.citizen.constants.CitizenConstants.ExceptionName;
import it.gov.pagopa.onboarding.citizen.dto.CitizenConsentDTO;
import it.gov.pagopa.onboarding.citizen.dto.TppDTO;
import it.gov.pagopa.onboarding.citizen.dto.mapper.CitizenConsentObjectToDTOMapper;
import it.gov.pagopa.onboarding.citizen.model.CitizenConsent;
import it.gov.pagopa.onboarding.citizen.repository.CitizenRepository;
Expand Down Expand Up @@ -47,36 +47,30 @@ public Mono<CitizenConsentDTO> handleExistingConsent(CitizenConsent existingCons
@Override
public Mono<CitizenConsentDTO> validateTppAndSaveConsent(String fiscalCode, String tppId, CitizenConsent citizenConsent) {
return tppConnector.get(tppId)
.onErrorMap(error -> exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, CitizenConstants.ExceptionMessage.TPP_NOT_FOUND))
.flatMap(tppResponse -> {
if (isTppValid(tppResponse)) {
if (Boolean.TRUE.equals(citizenConsent.getConsents().get(tppId).getTppState())) {
return citizenRepository.save(citizenConsent)
.doOnSuccess(savedConsent -> {
log.info("[EMD][CREATE-CITIZEN-CONSENT] Created new citizen consent for fiscal code: {}", Utils.createSHA256(fiscalCode));
log.info("[EMD][CREATE-CITIZEN-CONSENT] Created new citizen consent for fiscal code: {}", Utils.createSHA256(fiscalCode));
bloomFilterService.add(fiscalCode);
})
.flatMap(savedConsent -> Mono.just(mapperToDTO.map(savedConsent)));
.map(mapperToDTO::map);
} else {
return Mono.error(exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, "TPP does not exist or is not active"));
return Mono.error(exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, "TPP is not active or is invalid"));
}
});
}

@Override
public boolean isTppValid(TppDTO tppResponse) {
return tppResponse != null && Boolean.TRUE.equals(tppResponse.getState());
}

private Mono<CitizenConsentDTO> validateTppAndUpdateConsent(CitizenConsent existingConsent, String tppId, CitizenConsent citizenConsent) {
return tppConnector.get(tppId)
.onErrorMap(error -> exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, CitizenConstants.ExceptionMessage.TPP_NOT_FOUND))
.flatMap(tppResponse -> {
if (isTppValid(tppResponse)) {
existingConsent.getConsents().put(tppId, citizenConsent.getConsents().get(tppId));
return citizenRepository.save(existingConsent)
.doOnSuccess(savedConsent -> log.info("[EMD][CREATE-CITIZEN-CONSENT] Updated citizen consent for TPP: {}", tppId))
.flatMap(savedConsent -> Mono.just(mapperToDTO.map(savedConsent)));
} else {
return Mono.error(exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, "TPP does not exist or is not active"));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ void updateChannelState_Ok() {
when(citizenRepository.save(any()))
.thenReturn(Mono.just(CITIZEN_CONSENT));

when(validationService.isTppValid(mockTppDTO)).thenReturn(true);

CitizenConsentDTO response = citizenService.updateTppState(FISCAL_CODE, TPP_ID, TPP_STATE).block();

Expand All @@ -191,7 +190,7 @@ void updateChannelState_Ko_CitizenNotOnboarded() {
when(citizenRepository.findByFiscalCodeAndTppId(FISCAL_CODE, TPP_ID))
.thenReturn(Mono.empty());

when(validationService.isTppValid(mockTppDTO)).thenReturn(true);


Executable executable = () -> citizenService.updateTppState(FISCAL_CODE, TPP_ID, true).block();
ClientExceptionWithBody exception = assertThrows(ClientExceptionWithBody.class, executable);
Expand All @@ -217,7 +216,7 @@ void updateChannelState_Ok_ConsentDetailsIsNull() {
when(citizenRepository.save(any()))
.thenReturn(Mono.just(citizenConsentWithConsentDetailNull));

when(validationService.isTppValid(mockTppDTO)).thenReturn(true);


Executable executable = () -> citizenService.updateTppState(FISCAL_CODE, TPP_ID, TPP_STATE).block();
ClientExceptionWithBody exception = assertThrows(ClientExceptionWithBody.class, executable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void validateTppAndSaveConsent_TppInvalid() {

StepVerifier.create(validationService.validateTppAndSaveConsent(fiscalCode, tppId, CITIZEN_CONSENT))
.expectErrorMatches(throwable -> throwable instanceof ClientExceptionWithBody &&
"TPP does not exist or is not active".equals(throwable.getMessage()) &&
"TPP is not active or is invalid".equals(throwable.getMessage()) &&
"TPP_NOT_FOUND".equals(((ClientExceptionWithBody) throwable).getCode()))
.verify();

Expand All @@ -126,21 +126,23 @@ void validateTppAndSaveConsent_TppInvalid() {
}

@Test
void isTppValid_TrueWhenActive() {
TppDTO activeTpp = TPP_DTO;
activeTpp.setState(true);
assertTrue(validationService.isTppValid(activeTpp));
}
void validateTppAndSaveConsent_TppNotFound() {
String fiscalCode = CITIZEN_CONSENT.getFiscalCode();
String tppId = "inactiveTppId";

@Test
void isTppValid_FalseWhenInactive() {
TppDTO inactiveTpp = TPP_DTO;
inactiveTpp.setState(false);
assertFalse(validationService.isTppValid(inactiveTpp));
}
TppDTO inactiveTppDTO = TPP_DTO;
inactiveTppDTO.setState(false);

@Test
void isTppValid_FalseWhenNull() {
assertFalse(validationService.isTppValid(null));
when(tppConnector.get(anyString())).thenReturn(Mono.just(inactiveTppDTO));

StepVerifier.create(validationService.validateTppAndSaveConsent(fiscalCode, tppId, CITIZEN_CONSENT))
.expectErrorMatches(throwable -> throwable instanceof ClientExceptionWithBody &&
"TPP does not exist or is not active".equals(throwable.getMessage()) &&
"TPP_NOT_FOUND".equals(((ClientExceptionWithBody) throwable).getCode()))
.verify();

verify(citizenRepository, never()).save(any());
verify(bloomFilterService, never()).add(fiscalCode);
}

}

0 comments on commit 961d4d1

Please sign in to comment.