From fe3d4bb7866a9340cf0103f6c27f2d93da8708ce Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Fri, 13 Dec 2024 10:43:31 +0100 Subject: [PATCH 01/13] Api fix --- .../citizen/constants/CitizenConstants.java | 1 + .../citizen/controller/CitizenController.java | 20 +- .../controller/CitizenControllerImpl.java | 13 +- .../citizen/service/CitizenService.java | 4 +- .../citizen/service/CitizenServiceImpl.java | 113 ++++++--- .../CitizenConsentValidationService.java | 13 - .../CitizenConsentValidationServiceImpl.java | 76 ------ .../controller/CitizenControllerTest.java | 21 +- .../citizen/service/CitizenServiceTest.java | 237 +++++++++--------- ...tizenConsentValidationServiceImplTest.java | 189 -------------- 10 files changed, 225 insertions(+), 462 deletions(-) delete mode 100644 src/main/java/it/gov/pagopa/onboarding/citizen/validation/CitizenConsentValidationService.java delete mode 100644 src/main/java/it/gov/pagopa/onboarding/citizen/validation/CitizenConsentValidationServiceImpl.java delete mode 100644 src/test/java/it/gov/pagopa/onboarding/citizen/validation/CitizenConsentValidationServiceImplTest.java diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/constants/CitizenConstants.java b/src/main/java/it/gov/pagopa/onboarding/citizen/constants/CitizenConstants.java index 647e90f..6a6a937 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/constants/CitizenConstants.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/constants/CitizenConstants.java @@ -32,6 +32,7 @@ public static final class ValidationRegex { public static final String FISCAL_CODE_STRUCTURE_REGEX = "(^([A-Za-z]{6}[0-9lmnpqrstuvLMNPQRSTUV]{2}[abcdehlmprstABCDEHLMPRST][0-9lmnpqrstuvLMNPQRSTUV]{2}[A-Za-z][0-9lmnpqrstuvLMNPQRSTUV]{3}[A-Za-z])$)|(^(\\d{11})$)"; + public static final String TPP_STRUCTURE_REGEX = "(^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}-\\d{13}$)"; private ValidationRegex() {} } diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/controller/CitizenController.java b/src/main/java/it/gov/pagopa/onboarding/citizen/controller/CitizenController.java index 397ba0f..e4f3067 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/controller/CitizenController.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/controller/CitizenController.java @@ -1,8 +1,7 @@ package it.gov.pagopa.onboarding.citizen.controller; import it.gov.pagopa.onboarding.citizen.dto.CitizenConsentDTO; -import it.gov.pagopa.onboarding.citizen.dto.CitizenConsentStateUpdateDTO; -import jakarta.validation.Valid; + import jakarta.validation.constraints.Pattern; import org.springframework.http.ResponseEntity; @@ -12,16 +11,19 @@ import java.util.List; import static it.gov.pagopa.onboarding.citizen.constants.CitizenConstants.ValidationRegex.FISCAL_CODE_STRUCTURE_REGEX; +import static it.gov.pagopa.onboarding.citizen.constants.CitizenConstants.ValidationRegex.TPP_STRUCTURE_REGEX; @RequestMapping("/emd/citizen") public interface CitizenController { - @PostMapping("") - Mono> saveCitizenConsent(@Valid @RequestBody CitizenConsentDTO citizenConsentDTO); + @PostMapping("/{fiscalCode}/{tppId}") + Mono> saveCitizenConsent(@PathVariable @Pattern(regexp = FISCAL_CODE_STRUCTURE_REGEX, message = "Invalid fiscal code format") String fiscalCode, + @PathVariable @Pattern(regexp = TPP_STRUCTURE_REGEX, message = "Invalid fiscal code format") String tppId); - @PutMapping("/stateUpdate") - Mono> stateUpdate(@Valid @RequestBody CitizenConsentStateUpdateDTO citizenConsentStateUpdateDTO); + @PutMapping("/{fiscalCode}/{tppId}") + Mono> stateSwitch(@PathVariable @Pattern(regexp = FISCAL_CODE_STRUCTURE_REGEX, message = "Invalid fiscal code format") String fiscalCode, + @PathVariable @Pattern(regexp = TPP_STRUCTURE_REGEX, message = "Invalid fiscal code format") String tppId); @GetMapping("/filter/{fiscalCode}") Mono> bloomFilterSearch(@PathVariable @Pattern(regexp = FISCAL_CODE_STRUCTURE_REGEX, message = "Invalid fiscal code format") String fiscalCode); @@ -37,8 +39,8 @@ public interface CitizenController { * @return the citizen consent status */ @GetMapping("/{fiscalCode}/{tppId}") - Mono> getCitizenConsentStatus(@PathVariable @Pattern(regexp = FISCAL_CODE_STRUCTURE_REGEX, message = "Invalid fiscal code format") String fiscalCode, @PathVariable String tppId); - + Mono> getCitizenConsentStatus(@PathVariable @Pattern(regexp = FISCAL_CODE_STRUCTURE_REGEX, message = "Invalid fiscal code format") String fiscalCode, + @PathVariable @Pattern(regexp = TPP_STRUCTURE_REGEX, message = "Invalid fiscal code format") String tppId); /** * Get consents for a specific citizen. @@ -54,6 +56,6 @@ public interface CitizenController { Mono> getCitizenConsentsListEnabled(@PathVariable @Pattern(regexp = FISCAL_CODE_STRUCTURE_REGEX, message = "Invalid fiscal code format") String fiscalCode); @GetMapping("/{tppId}") - Mono>> getCitizenEnabled(@PathVariable String tppId); + Mono>> getCitizenEnabled(@PathVariable @Pattern(regexp = TPP_STRUCTURE_REGEX, message = "Invalid fiscal code format") String fiscalCode); } diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/controller/CitizenControllerImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/controller/CitizenControllerImpl.java index ef78227..6c9eca2 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/controller/CitizenControllerImpl.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/controller/CitizenControllerImpl.java @@ -1,10 +1,8 @@ package it.gov.pagopa.onboarding.citizen.controller; import it.gov.pagopa.onboarding.citizen.dto.CitizenConsentDTO; -import it.gov.pagopa.onboarding.citizen.dto.CitizenConsentStateUpdateDTO; import it.gov.pagopa.onboarding.citizen.service.BloomFilterServiceImpl; import it.gov.pagopa.onboarding.citizen.service.CitizenServiceImpl; -import jakarta.validation.Valid; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RestController; @@ -25,17 +23,14 @@ public CitizenControllerImpl(BloomFilterServiceImpl bloomFilterService, CitizenS } @Override - public Mono> saveCitizenConsent(@Valid CitizenConsentDTO citizenConsentDTO) { - return citizenService.createCitizenConsent(citizenConsentDTO) + public Mono> saveCitizenConsent(String fiscalCode, String tppId) { + return citizenService.createCitizenConsent(fiscalCode, tppId) .map(ResponseEntity::ok); } @Override - public Mono> stateUpdate(@Valid CitizenConsentStateUpdateDTO citizenConsentStateUpdateDTO) { - return citizenService.updateTppState( - citizenConsentStateUpdateDTO.getFiscalCode(), - citizenConsentStateUpdateDTO.getTppId(), - citizenConsentStateUpdateDTO.getTppState()) + public Mono> stateSwitch(String fiscalCode, String tppId) { + return citizenService.switchState(fiscalCode, tppId) .map(ResponseEntity::ok); } diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenService.java b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenService.java index 5aabd00..af088dc 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenService.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenService.java @@ -7,8 +7,8 @@ public interface CitizenService { - Mono createCitizenConsent(CitizenConsentDTO citizenConsent); - Mono updateTppState(String fiscalCode, String tppId, boolean tppState); + Mono createCitizenConsent(String fiscalCode, String tppId); + Mono switchState(String fiscalCode, String tppId); Mono getCitizenConsentStatus(String fiscalCode, String tppId); Mono> getTppEnabledList(String fiscalCode); Mono getCitizenConsentsList(String fiscalCode); diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java index 83ddf29..e64044a 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java @@ -9,14 +9,13 @@ import it.gov.pagopa.onboarding.citizen.dto.mapper.CitizenConsentObjectToDTOMapper; import it.gov.pagopa.onboarding.citizen.model.CitizenConsent; import it.gov.pagopa.onboarding.citizen.model.ConsentDetails; -import it.gov.pagopa.onboarding.citizen.model.mapper.CitizenConsentDTOToObjectMapper; import it.gov.pagopa.onboarding.citizen.repository.CitizenRepository; -import it.gov.pagopa.onboarding.citizen.validation.CitizenConsentValidationServiceImpl; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import reactor.core.publisher.Mono; import java.time.LocalDateTime; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -29,57 +28,90 @@ public class CitizenServiceImpl implements CitizenService { private final CitizenRepository citizenRepository; private final CitizenConsentObjectToDTOMapper mapperToDTO; - private final CitizenConsentDTOToObjectMapper mapperToObject; private final ExceptionMap exceptionMap; private final TppConnectorImpl tppConnector; - private final CitizenConsentValidationServiceImpl validationService; + private final BloomFilterServiceImpl bloomFilterService; - public CitizenServiceImpl(CitizenRepository citizenRepository, CitizenConsentObjectToDTOMapper mapperToDTO, CitizenConsentDTOToObjectMapper mapperToObject, ExceptionMap exceptionMap, TppConnectorImpl tppConnector, CitizenConsentValidationServiceImpl validationService) { + public CitizenServiceImpl(CitizenRepository citizenRepository, + CitizenConsentObjectToDTOMapper mapperToDTO, + ExceptionMap exceptionMap, + TppConnectorImpl tppConnector, + BloomFilterServiceImpl bloomFilterService) { this.citizenRepository = citizenRepository; this.mapperToDTO = mapperToDTO; - this.mapperToObject = mapperToObject; this.exceptionMap = exceptionMap; this.tppConnector = tppConnector; - this.validationService = validationService; + this.bloomFilterService = bloomFilterService; } @Override - public Mono createCitizenConsent(CitizenConsentDTO citizenConsentDTO) { + public Mono createCitizenConsent(String fiscalCode, String tppId){ + log.info("[EMD][CITIZEN][UPDATE-CHANNEL-STATE] Received hashedFiscalCode: {} and tppId: {}", + Utils.createSHA256(fiscalCode), inputSanify(tppId)); - CitizenConsent citizenConsent = mapperToObject.map(citizenConsentDTO); - String fiscalCode = citizenConsent.getFiscalCode(); - - citizenConsent.getConsents().forEach((tppId, consentDetails) -> consentDetails.setTcDate(LocalDateTime.now())); - - log.info("[EMD-CITIZEN][CREATE] Received consent: {}", inputSanify(citizenConsent.toString())); - - String tppId = citizenConsent.getConsents().keySet().stream().findFirst().orElse(null); - if (tppId == null) { - return Mono.error(exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, ExceptionMessage.TPP_NOT_FOUND)); - } - - return citizenRepository.findByFiscalCode(fiscalCode) - .flatMap(existingConsent -> validationService.handleExistingConsent(existingConsent, tppId, citizenConsent)) - .switchIfEmpty(validationService.validateTppAndSaveConsent(fiscalCode, tppId, citizenConsent)); + return tppConnector.get(tppId) + .onErrorMap(error -> exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, ExceptionMessage.TPP_NOT_FOUND)) + .flatMap(tppResponse -> citizenRepository.findByFiscalCode(fiscalCode) + .flatMap(citizenConsent -> { + if (!citizenConsent.getConsents().containsKey(tppId)) { + citizenConsent.getConsents().put(tppId, ConsentDetails.builder() + .tppState(true) + .tcDate(LocalDateTime.now()) + .build()); + return citizenRepository.save(citizenConsent) + .map(mapperToDTO::map) + .doOnSuccess(savedConsent -> bloomFilterService.add(fiscalCode)); + } else { + Map consents = new HashMap<>(); + consents.put(tppId, citizenConsent.getConsents().get(tppId)); + citizenConsent.setConsents(consents); + return Mono.just(mapperToDTO.map(citizenConsent)); + } + }) + .switchIfEmpty(Mono.defer(() -> { + Map consents = new HashMap<>(); + consents.put(tppId, ConsentDetails.builder() + .tppState(true) + .tcDate(LocalDateTime.now()) + .build()); + CitizenConsent citizenConsentToSave = CitizenConsent.builder() + .fiscalCode(fiscalCode) + .consents(consents) + .build(); + return citizenRepository + .save(citizenConsentToSave) + .map(mapperToDTO::map) + .doOnSuccess(savedConsent -> bloomFilterService.add(fiscalCode)); + })) + ) + .doOnSuccess(savedConsent -> log.info("[EMD][CREATE-CITIZEN-CONSENT] Created new citizen consent for fiscal code: {}", Utils.createSHA256(fiscalCode))); } - @Override - public Mono updateTppState(String fiscalCode, String tppId, boolean tppState) { - log.info("[EMD][CITIZEN][UPDATE-CHANNEL-STATE] Received hashedFiscalCode: {} and tppId: {} with state: {}", - Utils.createSHA256(fiscalCode), inputSanify(tppId), tppState); + public Mono switchState(String fiscalCode, String tppId){ + log.info("[EMD][CITIZEN][UPDATE-CHANNEL-STATE] Received hashedFiscalCode: {} and tppId: {}", + Utils.createSHA256(fiscalCode), inputSanify(tppId)); - return tppConnector.get(tppId) - .onErrorMap(error ->exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, ExceptionMessage.TPP_NOT_FOUND)) - .flatMap(tppResponse -> citizenRepository.findByFiscalCodeAndTppId(fiscalCode, tppId) + return citizenRepository.findByFiscalCode(fiscalCode) .switchIfEmpty(Mono.error(exceptionMap.throwException - (ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during update state process"))) + (ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during update state process")) + ) .flatMap(citizenConsent -> { + if(!citizenConsent.getConsents().containsKey(tppId)) + return Mono.error(exceptionMap.throwException + (ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during update state process")); + ConsentDetails consentDetails = citizenConsent.getConsents().get(tppId); - consentDetails.setTppState(tppState); - return citizenRepository.save(citizenConsent); + consentDetails.setTppState(!consentDetails.getTppState()); + citizenRepository.save(citizenConsent); + + Map consents = new HashMap<>(); + consents.put(tppId,citizenConsent.getConsents().get(tppId)); + citizenConsent.setConsents(consents); + + return Mono.just(citizenConsent); }) .map(mapperToDTO::map) - .doOnSuccess(savedConsent -> log.info("[EMD][CITIZEN][UPDATE-CHANNEL-STATE] Updated state"))); + .doOnSuccess(savedConsent -> log.info("[EMD][CITIZEN][UPDATE-CHANNEL-STATE] Updated state")); } @Override @@ -87,9 +119,9 @@ public Mono getCitizenConsentStatus(String fiscalCode, String log.info("[EMD-CITIZEN][GET-CONSENT-STATUS] Received hashedFiscalCode: {} and tppId: {}", Utils.createSHA256(fiscalCode), inputSanify(tppId)); return citizenRepository.findByFiscalCodeAndTppId(fiscalCode, tppId) .switchIfEmpty(Mono.error(exceptionMap.throwException - (ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during get process "))) + (ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded"))) .map(mapperToDTO::map) - .doOnSuccess(consent -> log.info("[EMD-CITIZEN][GET-CONSENT-STATUS] Consent consent found:: {}", consent)); + .doOnSuccess(consent -> log.info("[EMD-CITIZEN][GET-CONSENT-STATUS] Consent consent found: {}", consent)); } @@ -104,7 +136,7 @@ public Mono> getTppEnabledList(String fiscalCode) { .map(Map.Entry::getKey) .toList()) .doOnSuccess(tppIdList -> { - if (tppIdList != null) { + if (tppIdList != null){ log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] Consents found: {}", (tppIdList.size())); } else { log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] No consents found."); @@ -116,6 +148,8 @@ public Mono> getTppEnabledList(String fiscalCode) { public Mono getCitizenConsentsList(String fiscalCode) { log.info("[EMD-CITIZEN][FIND-ALL-CITIZEN-CONSENTS] Received hashedFiscalCode: {}", (Utils.createSHA256(fiscalCode))); return citizenRepository.findByFiscalCode(fiscalCode) + .switchIfEmpty(Mono.error(exceptionMap.throwException + (ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during get process "))) .map(mapperToDTO::map) .doOnSuccess(consentList -> log.info("[EMD-CITIZEN][FIND-ALL-CITIZEN-CONSENTS] Consents found: {}", consentList)); } @@ -125,7 +159,8 @@ public Mono getCitizenConsentsListEnabled(String fiscalCode) log.info("[EMD-CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] Received hashedFiscalCode: {}", Utils.createSHA256(fiscalCode)); return citizenRepository.findByFiscalCode(fiscalCode) - .switchIfEmpty(Mono.empty()) + .switchIfEmpty(Mono.error(exceptionMap.throwException + (ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during get process "))) .map(citizenConsent -> { Map filteredConsents = citizenConsent.getConsents().entrySet().stream() .filter(tpp -> tpp.getValue().getTppState()) @@ -135,7 +170,7 @@ public Mono getCitizenConsentsListEnabled(String fiscalCode) return mapperToDTO.map(citizenConsent); }) .doOnSuccess(citizenConsent -> { - if (citizenConsent != null && citizenConsent.getConsents() != null) { + if (citizenConsent != null && !citizenConsent.getConsents().isEmpty()) { log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] Consents found: {}", citizenConsent.getConsents().size()); } else { log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] No consents found."); diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/validation/CitizenConsentValidationService.java b/src/main/java/it/gov/pagopa/onboarding/citizen/validation/CitizenConsentValidationService.java deleted file mode 100644 index 8cbabed..0000000 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/validation/CitizenConsentValidationService.java +++ /dev/null @@ -1,13 +0,0 @@ -package it.gov.pagopa.onboarding.citizen.validation; - -import it.gov.pagopa.onboarding.citizen.dto.CitizenConsentDTO; -import it.gov.pagopa.onboarding.citizen.model.CitizenConsent; -import reactor.core.publisher.Mono; - -public interface CitizenConsentValidationService { - - Mono handleExistingConsent(CitizenConsent existingConsent, String tppId, CitizenConsent citizenConsent); - - Mono validateTppAndSaveConsent(String fiscalCode, String tppId, CitizenConsent citizenConsent); - -} diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/validation/CitizenConsentValidationServiceImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/validation/CitizenConsentValidationServiceImpl.java deleted file mode 100644 index 85ffdfd..0000000 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/validation/CitizenConsentValidationServiceImpl.java +++ /dev/null @@ -1,76 +0,0 @@ -package it.gov.pagopa.onboarding.citizen.validation; - -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.mapper.CitizenConsentObjectToDTOMapper; -import it.gov.pagopa.onboarding.citizen.model.CitizenConsent; -import it.gov.pagopa.onboarding.citizen.repository.CitizenRepository; -import it.gov.pagopa.onboarding.citizen.service.BloomFilterServiceImpl; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Service; -import reactor.core.publisher.Mono; - -@Service -@Slf4j -public class CitizenConsentValidationServiceImpl implements CitizenConsentValidationService { - - private final CitizenRepository citizenRepository; - - private final BloomFilterServiceImpl bloomFilterService; - - private final TppConnectorImpl tppConnector; - private final CitizenConsentObjectToDTOMapper mapperToDTO; - private final ExceptionMap exceptionMap; - - public CitizenConsentValidationServiceImpl(CitizenRepository citizenRepository, BloomFilterServiceImpl bloomFilterService, TppConnectorImpl tppConnector, - CitizenConsentObjectToDTOMapper mapperToDTO, ExceptionMap exceptionMap) { - this.citizenRepository = citizenRepository; - this.bloomFilterService = bloomFilterService; - this.tppConnector = tppConnector; - this.mapperToDTO = mapperToDTO; - this.exceptionMap = exceptionMap; - } - - @Override - public Mono handleExistingConsent(CitizenConsent existingConsent, String tppId, CitizenConsent citizenConsent) { - if (existingConsent.getConsents().containsKey(tppId)) { - return Mono.just(mapperToDTO.map(citizenConsent)); - } else { - return validateTppAndUpdateConsent(existingConsent, tppId, citizenConsent); - } - } - - @Override - public Mono 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 (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)); - bloomFilterService.add(fiscalCode); - }) - .map(savedConsent -> mapperToDTO.map(citizenConsent)); - } else { - return Mono.error(exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, "TPP is not active or is invalid")); - } - }); - } - - - private Mono 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 -> { - 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)) - .map(savedConsent -> mapperToDTO.map(citizenConsent)); - }); - } -} diff --git a/src/test/java/it/gov/pagopa/onboarding/citizen/controller/CitizenControllerTest.java b/src/test/java/it/gov/pagopa/onboarding/citizen/controller/CitizenControllerTest.java index eb686dc..26b00c3 100644 --- a/src/test/java/it/gov/pagopa/onboarding/citizen/controller/CitizenControllerTest.java +++ b/src/test/java/it/gov/pagopa/onboarding/citizen/controller/CitizenControllerTest.java @@ -32,19 +32,18 @@ class CitizenControllerTest { private WebTestClient webClient; - private static final String FISCAL_CODE = "MLXHZZ43A70H203T"; - private static final String TPP_ID = "tppId"; + private static final String TPP_ID = "ae46399d-a3e4-a3d9-a2b8-a1c8fd5f5e40-1732202076421"; @Test void saveCitizenConsent_Ok() { CitizenConsentDTO citizenConsentDTO = CitizenConsentDTOFaker.mockInstance(true); - Mockito.when(citizenService.createCitizenConsent(citizenConsentDTO)).thenReturn(Mono.just(citizenConsentDTO)); + Mockito.when(citizenService.createCitizenConsent(FISCAL_CODE, TPP_ID)).thenReturn(Mono.just(citizenConsentDTO)); webClient.post() - .uri("/emd/citizen") + .uri("/emd/citizen/{fiscalCode}/{tppId}", FISCAL_CODE, TPP_ID) .contentType(MediaType.APPLICATION_JSON) .bodyValue(citizenConsentDTO) .exchange() @@ -63,14 +62,11 @@ void stateUpdate_Ok() { CitizenConsentDTO expectedResponseDTO = CitizenConsentDTOFaker.mockInstance(true); - Mockito.when(citizenService.updateTppState( - citizenConsentStateUpdateDTO.getFiscalCode(), - citizenConsentStateUpdateDTO.getTppId(), - citizenConsentStateUpdateDTO.getTppState())) + Mockito.when(citizenService.switchState(FISCAL_CODE, TPP_ID)) .thenReturn(Mono.just(expectedResponseDTO)); webClient.put() - .uri("/emd/citizen/stateUpdate") + .uri("/emd/citizen/{fiscalCode}/{tppId}", FISCAL_CODE, TPP_ID) .contentType(MediaType.APPLICATION_JSON) .bodyValue(citizenConsentStateUpdateDTO) .exchange() @@ -195,13 +191,12 @@ void getCitizenConsentsListEnabled_ShouldReturnCitizenConsent() { @Test void getCitizenEnabled_ShouldReturnListOfCitizens() { - String tppId = "TPP123"; - List mockConsents = List.of(CitizenConsentDTOFaker.mockInstance(true)); - Mockito.when(citizenService.getCitizenEnabled(tppId)) + List mockConsents = List.of(CitizenConsentDTOFaker.mockInstance(true)); + Mockito.when(citizenService.getCitizenEnabled(TPP_ID)) .thenReturn(Mono.just(mockConsents)); webClient.get() - .uri("/emd/citizen/{tppId}", tppId) + .uri("/emd/citizen/{tppId}", TPP_ID) .accept(MediaType.APPLICATION_JSON) .exchange() .expectStatus().isOk() diff --git a/src/test/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceTest.java b/src/test/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceTest.java index b917005..9c57c7b 100644 --- a/src/test/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceTest.java +++ b/src/test/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceTest.java @@ -6,22 +6,19 @@ 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.faker.CitizenConsentDTOFaker; import it.gov.pagopa.onboarding.citizen.faker.CitizenConsentFaker; import it.gov.pagopa.onboarding.citizen.faker.TppDTOFaker; import it.gov.pagopa.onboarding.citizen.model.CitizenConsent; import it.gov.pagopa.onboarding.citizen.model.ConsentDetails; import it.gov.pagopa.onboarding.citizen.model.mapper.CitizenConsentDTOToObjectMapper; import it.gov.pagopa.onboarding.citizen.repository.CitizenRepository; -import it.gov.pagopa.onboarding.citizen.validation.CitizenConsentValidationServiceImpl; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.api.function.Executable; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.http.HttpStatus; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; import reactor.core.publisher.Flux; @@ -41,7 +38,7 @@ @ExtendWith({SpringExtension.class, MockitoExtension.class}) @ContextConfiguration(classes = { CitizenServiceImpl.class, - CitizenConsentValidationServiceImpl.class, + BloomFilterServiceImpl.class, CitizenConsentObjectToDTOMapper.class, CitizenConsentDTOToObjectMapper.class, ExceptionMap.class @@ -52,7 +49,7 @@ class CitizenServiceTest { CitizenServiceImpl citizenService; @MockBean - CitizenConsentValidationServiceImpl validationService; + BloomFilterServiceImpl bloomFilterService; @MockBean CitizenRepository citizenRepository; @@ -65,9 +62,10 @@ class CitizenServiceTest { private static final String FISCAL_CODE = "fiscalCode"; private static final String TPP_ID = "tppId"; + private static final String TPP_ID_2 = "tppId2"; private static final boolean TPP_STATE = true; private static final CitizenConsent CITIZEN_CONSENT = CitizenConsentFaker.mockInstance(true); - private static final CitizenConsentDTO CITIZEN_CONSENT_DTO = CitizenConsentDTOFaker.mockInstance(true); + private static final CitizenConsent CITIZEN_CONSENT_2 = CitizenConsentFaker.mockInstance(true); private static final TppDTO TPP_DTO = TppDTOFaker.mockInstance(); @Test @@ -80,134 +78,117 @@ void createCitizenConsent_Ok() { when(tppConnector.get(anyString())).thenReturn(Mono.just(activeTppDTO)); when(citizenRepository.save(Mockito.any())).thenReturn(Mono.just(CITIZEN_CONSENT)); when(citizenRepository.findByFiscalCode(anyString())).thenReturn(Mono.empty()); - when(validationService.validateTppAndSaveConsent(anyString(), anyString(), any(CitizenConsent.class))) - .thenReturn(Mono.just(expectedConsentDTO)); - CitizenConsentDTO response = citizenService.createCitizenConsent(CITIZEN_CONSENT_DTO).block(); - assertNotNull(response); - assertEquals(expectedConsentDTO, response); + StepVerifier.create(citizenService.createCitizenConsent(FISCAL_CODE, TPP_ID)) + .assertNext(response -> { + assertNotNull(response); + assertEquals(expectedConsentDTO, response); + }) + .verifyComplete(); } @Test void createCitizenConsent_AlreadyExists() { CitizenConsentDTO expectedConsentDTO = dtoMapper.map(CITIZEN_CONSENT); - TppDTO activeTppDTO = TPP_DTO; - activeTppDTO.setState(true); - - when(tppConnector.get(anyString())).thenReturn(Mono.just(activeTppDTO)); + when(tppConnector.get(anyString())).thenReturn(Mono.just(TPP_DTO)); when(citizenRepository.save(Mockito.any())).thenReturn(Mono.just(CITIZEN_CONSENT)); when(citizenRepository.findByFiscalCode(anyString())).thenReturn(Mono.just(CITIZEN_CONSENT)); - when(citizenRepository.findByFiscalCodeAndTppId(anyString(), anyString())).thenReturn(Mono.just(CITIZEN_CONSENT)); - when(validationService.handleExistingConsent(any(), anyString(), any())) - .thenReturn(Mono.just(expectedConsentDTO)); - - when(validationService.validateTppAndSaveConsent(anyString(), anyString(), any(CitizenConsent.class))) - .thenReturn(Mono.just(expectedConsentDTO)); - - CitizenConsentDTO response = citizenService.createCitizenConsent(CITIZEN_CONSENT_DTO).block(); - - assertNotNull(response); - assertEquals(expectedConsentDTO, response); + StepVerifier.create(citizenService.createCitizenConsent(FISCAL_CODE, TPP_ID)) + .assertNext(response -> { + assertNotNull(response); + assertEquals(expectedConsentDTO, response); + }) + .verifyComplete(); } @Test - void createCitizenConsent_Ko_TppNotProvided() { - - CitizenConsentDTO incompleteConsentDTO = CitizenConsentDTOFaker.mockInstance(true); - incompleteConsentDTO.getConsents().clear(); + void createCitizenConsent_AlreadyOnboardedOnAnotherTpp() { + CITIZEN_CONSENT_2.getConsents().put(TPP_ID_2, ConsentDetails.builder() + .tppState(true) + .tcDate(LocalDateTime.now()) + .build()); + CitizenConsentDTO expectedConsentDTO = dtoMapper.map(CITIZEN_CONSENT_2); - when(tppConnector.get(anyString())).thenReturn(Mono.empty()); - when(citizenRepository.findByFiscalCodeAndTppId(anyString(), anyString())).thenReturn(Mono.empty()); + when(tppConnector.get(anyString())).thenReturn(Mono.just(TPP_DTO)); + when(citizenRepository.save(Mockito.any())).thenReturn(Mono.just(CITIZEN_CONSENT_2)); + when(citizenRepository.findByFiscalCode(anyString())).thenReturn(Mono.just(CITIZEN_CONSENT)); - when(validationService.validateTppAndSaveConsent(anyString(), anyString(), any(CitizenConsent.class))) - .thenReturn(Mono.error(new ClientExceptionWithBody(HttpStatus.BAD_REQUEST, "TPP_NOT_FOUND", "TPP does not exist or is not active"))); + StepVerifier.create(citizenService.createCitizenConsent(FISCAL_CODE, TPP_ID_2)) + .assertNext(response -> { + assertNotNull(response); + assertEquals(expectedConsentDTO, response); + }) + .verifyComplete(); - StepVerifier.create(citizenService.createCitizenConsent(incompleteConsentDTO)) - .expectErrorMatches(throwable -> throwable instanceof ClientExceptionWithBody && - "TPP does not exist or is not active".equals(throwable.getMessage()) && - "TPP_NOT_FOUND".equals(((ClientExceptionWithBody) throwable).getCode())) - .verify(); + CITIZEN_CONSENT.getConsents().remove(TPP_ID_2); } @Test - void createCitizenConsent_Ko_TppInactive() { + void createCitizenConsent_Ko_TppNotFound() { - TppDTO inactiveTppDTO = TPP_DTO; - inactiveTppDTO.setState(false); + when(tppConnector.get(anyString())).thenReturn(Mono.error(new RuntimeException("TPP not found"))); - when(tppConnector.get(anyString())).thenReturn(Mono.just(inactiveTppDTO)); - when(citizenRepository.findByFiscalCodeAndTppId(anyString(), anyString())).thenReturn(Mono.empty()); - when(citizenRepository.findByFiscalCode(anyString())).thenReturn(Mono.empty()); - - when(validationService.validateTppAndSaveConsent(anyString(), anyString(), any(CitizenConsent.class))) - .thenReturn(Mono.error(new ClientExceptionWithBody(HttpStatus.BAD_REQUEST, "TPP_NOT_FOUND", "TPP does not exist or is not active"))); - - CitizenConsentDTO citizenConsentDTO = dtoMapper.map(CITIZEN_CONSENT); - - StepVerifier.create(citizenService.createCitizenConsent(citizenConsentDTO)) + StepVerifier.create(citizenService.createCitizenConsent(FISCAL_CODE, TPP_ID)) .expectErrorMatches(throwable -> throwable instanceof ClientExceptionWithBody && "TPP does not exist or is not active".equals(throwable.getMessage()) && "TPP_NOT_FOUND".equals(((ClientExceptionWithBody) throwable).getCode())) .verify(); - Mockito.verify(citizenRepository, Mockito.never()).save(Mockito.any()); } @Test void updateChannelState_Ok() { - TppDTO mockTppDTO = TppDTOFaker.mockInstance(); - mockTppDTO.setState(true); - - when(tppConnector.get(anyString())) - .thenReturn(Mono.just(mockTppDTO)); - - when(citizenRepository.findByFiscalCodeAndTppId(FISCAL_CODE, TPP_ID)) + when(citizenRepository.findByFiscalCode(FISCAL_CODE)) .thenReturn(Mono.just(CITIZEN_CONSENT)); when(citizenRepository.save(any())) .thenReturn(Mono.just(CITIZEN_CONSENT)); + StepVerifier.create(citizenService.switchState(FISCAL_CODE, TPP_ID)) + .assertNext(response -> { + assertNotNull(response); + assertNotEquals(TPP_STATE, response.getConsents().get(TPP_ID).getTppState()); + }) + .verifyComplete(); + } - CitizenConsentDTO response = citizenService.updateTppState(FISCAL_CODE, TPP_ID, TPP_STATE).block(); + @Test + void updateChannelState_Ko_CitizenNotOnboardedOnTpp() { - assertNotNull(response); + when(citizenRepository.findByFiscalCode(FISCAL_CODE)) + .thenReturn(Mono.just(CITIZEN_CONSENT)); - assertEquals(TPP_STATE, response.getConsents().get(TPP_ID).getTppState()); + StepVerifier.create(citizenService.switchState(FISCAL_CODE, TPP_ID_2)) + .expectErrorMatches(throwable -> throwable instanceof ClientExceptionWithBody && + "Citizen consent not founded during update state process".equals(throwable.getMessage())) + .verify(); } @Test void updateChannelState_Ko_CitizenNotOnboarded() { - TppDTO mockTppDTO = TppDTOFaker.mockInstance(); - mockTppDTO.setState(true); - - when(tppConnector.get(anyString())) - .thenReturn(Mono.just(mockTppDTO)); - - when(citizenRepository.findByFiscalCodeAndTppId(FISCAL_CODE, TPP_ID)) + when(citizenRepository.findByFiscalCode(FISCAL_CODE)) .thenReturn(Mono.empty()); - - - Executable executable = () -> citizenService.updateTppState(FISCAL_CODE, TPP_ID, true).block(); - ClientExceptionWithBody exception = assertThrows(ClientExceptionWithBody.class, executable); - - assertEquals("Citizen consent not founded during update state process", exception.getMessage()); + StepVerifier.create(citizenService.switchState(FISCAL_CODE, TPP_ID)) + .expectErrorMatches(throwable -> throwable instanceof ClientExceptionWithBody && + "Citizen consent not founded during update state process".equals(throwable.getMessage())) + .verify(); } @Test void getConsentStatus_Ok() { - when(citizenRepository.findByFiscalCodeAndTppId(FISCAL_CODE, TPP_ID)) .thenReturn(Mono.just(CITIZEN_CONSENT)); - CitizenConsentDTO response = citizenService.getCitizenConsentStatus(FISCAL_CODE, TPP_ID).block(); - assertNotNull(response); + StepVerifier.create(citizenService.getCitizenConsentStatus(FISCAL_CODE, TPP_ID)) + .assertNext(Assertions::assertNotNull) + .verifyComplete(); } @Test @@ -216,10 +197,10 @@ void getConsentStatus_Ko_CitizenNotOnboarded() { when(citizenRepository.findByFiscalCodeAndTppId(FISCAL_CODE, TPP_ID)) .thenReturn(Mono.empty()); - Executable executable = () -> citizenService.getCitizenConsentStatus(FISCAL_CODE, TPP_ID).block(); - ClientExceptionWithBody exception = assertThrows(ClientExceptionWithBody.class, executable); - - assertEquals("Citizen consent not founded during get process ", exception.getMessage()); + StepVerifier.create(citizenService.getCitizenConsentStatus(FISCAL_CODE, TPP_ID)) + .expectErrorMatches(throwable -> throwable instanceof ClientExceptionWithBody && + "Citizen consent not founded".equals(throwable.getMessage())) + .verify(); } @Test @@ -244,10 +225,17 @@ void testGetTppEnabledList_Success() { when(citizenRepository.findByFiscalCode(FISCAL_CODE)).thenReturn(Mono.just(citizenConsent)); - Mono> result = citizenService.getTppEnabledList(FISCAL_CODE); + StepVerifier.create(citizenService.getTppEnabledList(FISCAL_CODE)) + .assertNext(result -> assertEquals(List.of("Tpp1"), result)) + .verifyComplete(); + } - StepVerifier.create(result) - .expectNext(List.of("Tpp1")) + @Test + void testGetTppEnabledList_Empty() { + when(citizenRepository.findByFiscalCode(FISCAL_CODE)).thenReturn(Mono.empty()); + + StepVerifier.create(citizenService.getTppEnabledList(FISCAL_CODE)) + .expectNextCount(0) .verifyComplete(); } @@ -257,11 +245,12 @@ void get_Ok() { when(citizenRepository.findByFiscalCode(FISCAL_CODE)) .thenReturn(Mono.just(CITIZEN_CONSENT)); - CitizenConsentDTO response = citizenService.getCitizenConsentsList(FISCAL_CODE).block(); - - assertNotNull(response); - - assertEquals(CITIZEN_CONSENT.getFiscalCode(), response.getFiscalCode()); + StepVerifier.create(citizenService.getCitizenConsentsList(FISCAL_CODE)) + .assertNext(response -> { + assertNotNull(response); + assertEquals(CITIZEN_CONSENT.getFiscalCode(), response.getFiscalCode()); + }) + .verifyComplete(); } @Test @@ -280,21 +269,41 @@ void getCitizenConsentsListEnabled_Ok() { when(citizenRepository.findByFiscalCode(FISCAL_CODE)).thenReturn(Mono.just(citizenConsent)); - CitizenConsentDTO response = citizenService.getCitizenConsentsListEnabled(FISCAL_CODE).block(); + StepVerifier.create(citizenService.getCitizenConsentsListEnabled(FISCAL_CODE)) + .assertNext(response -> { + assertNotNull(response); + assertEquals(1, response.getConsents().size()); + assertTrue(response.getConsents().containsKey("Tpp1")); + assertFalse(response.getConsents().containsKey("Tpp2")); + }) + .verifyComplete(); + } + @Test + void getCitizenConsentsListEnabled_Empty() { + CitizenConsent citizenConsent = CitizenConsent.builder() + .fiscalCode(FISCAL_CODE) + .consents(new HashMap<>()) + .build(); + + when(citizenRepository.findByFiscalCode(FISCAL_CODE)).thenReturn(Mono.just(citizenConsent)); - assertNotNull(response); - assertEquals(1, response.getConsents().size()); - assertTrue(response.getConsents().containsKey("Tpp1")); - assertFalse(response.getConsents().containsKey("Tpp2")); + StepVerifier.create(citizenService.getCitizenConsentsListEnabled(FISCAL_CODE)) + .assertNext(response -> { + assertNotNull(response); + assertEquals(0, response.getConsents().size()); + }) + .verifyComplete(); } + @Test - void getCitizenConsentsListEnabled_Empty() { + void getCitizenConsentsListEnabled_NotOnboarded() { when(citizenRepository.findByFiscalCode(FISCAL_CODE)).thenReturn(Mono.empty()); - CitizenConsentDTO response = citizenService.getCitizenConsentsListEnabled(FISCAL_CODE).block(); - - assertNull(response); + StepVerifier.create(citizenService.getCitizenConsentsListEnabled(FISCAL_CODE)) + .expectErrorMatches(throwable -> throwable instanceof ClientExceptionWithBody && + "Citizen consent not founded during get process ".equals(throwable.getMessage())) + .verify(); } @Test @@ -311,22 +320,26 @@ void getCitizenEnabled_Ok() { when(citizenRepository.findByTppIdEnabled(TPP_ID)).thenReturn(Flux.just(citizenConsent1, citizenConsent2)); - List response = citizenService.getCitizenEnabled(TPP_ID).block(); - - assertNotNull(response); - assertEquals(2, response.size()); - assertEquals("FiscalCode1", response.get(0).getFiscalCode()); - assertEquals("FiscalCode2", response.get(1).getFiscalCode()); + StepVerifier.create(citizenService.getCitizenEnabled(TPP_ID)) + .assertNext(response -> { + assertNotNull(response); + assertEquals(2, response.size()); + assertEquals("FiscalCode1", response.get(0).getFiscalCode()); + assertEquals("FiscalCode2", response.get(1).getFiscalCode()); + }) + .verifyComplete(); } @Test void getCitizenEnabled_Empty() { when(citizenRepository.findByTppIdEnabled(TPP_ID)).thenReturn(Flux.empty()); - List response = citizenService.getCitizenEnabled(TPP_ID).block(); - - assertNotNull(response); - assertTrue(response.isEmpty()); + StepVerifier.create(citizenService.getCitizenEnabled(TPP_ID)) + .assertNext(response -> { + assertNotNull(response); + assertTrue(response.isEmpty()); + }) + .verifyComplete(); } - } + diff --git a/src/test/java/it/gov/pagopa/onboarding/citizen/validation/CitizenConsentValidationServiceImplTest.java b/src/test/java/it/gov/pagopa/onboarding/citizen/validation/CitizenConsentValidationServiceImplTest.java deleted file mode 100644 index 1b16ab4..0000000 --- a/src/test/java/it/gov/pagopa/onboarding/citizen/validation/CitizenConsentValidationServiceImplTest.java +++ /dev/null @@ -1,189 +0,0 @@ -package it.gov.pagopa.onboarding.citizen.validation; - -import it.gov.pagopa.common.web.exception.ClientExceptionWithBody; -import it.gov.pagopa.onboarding.citizen.connector.tpp.TppConnectorImpl; -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.faker.CitizenConsentDTOFaker; -import it.gov.pagopa.onboarding.citizen.faker.CitizenConsentFaker; -import it.gov.pagopa.onboarding.citizen.faker.TppDTOFaker; -import it.gov.pagopa.onboarding.citizen.model.CitizenConsent; -import it.gov.pagopa.onboarding.citizen.model.ConsentDetails; -import it.gov.pagopa.onboarding.citizen.repository.CitizenRepository; -import it.gov.pagopa.onboarding.citizen.service.BloomFilterServiceImpl; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import reactor.core.publisher.Mono; -import reactor.test.StepVerifier; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.*; - -@SpringBootTest -class CitizenConsentValidationServiceImplTest { - - @MockBean - BloomFilterServiceImpl bloomFilterService; - - @MockBean - CitizenRepository citizenRepository; - - @MockBean - TppConnectorImpl tppConnector; - - @MockBean - CitizenConsentObjectToDTOMapper dtoMapper; - - @Autowired - CitizenConsentValidationServiceImpl validationService; - - private static final CitizenConsent CITIZEN_CONSENT = CitizenConsentFaker.mockInstance(true); - private static final CitizenConsentDTO CITIZEN_CONSENT_DTO = CitizenConsentDTOFaker.mockInstance(true); - private static final TppDTO TPP_DTO = TppDTOFaker.mockInstance(); - - @Test - void handleExistingConsent_ConsentAlreadyExists() { - - CitizenConsent existingConsent = CITIZEN_CONSENT; - String tppId = "existingTppId"; - existingConsent.getConsents().put(tppId, CITIZEN_CONSENT.getConsents().get(tppId)); - - when(dtoMapper.map(existingConsent)).thenReturn(CITIZEN_CONSENT_DTO); - - CitizenConsentDTO result = validationService.handleExistingConsent(existingConsent, tppId, CITIZEN_CONSENT).block(); - - assertNotNull(result); - assertEquals(CITIZEN_CONSENT_DTO, result); - } - - @Test - void handleExistingConsent_NewConsentForTpp() { - - CitizenConsent existingConsent = CITIZEN_CONSENT; - TppDTO activeTppDTO = TPP_DTO; - activeTppDTO.setState(true); - - when(tppConnector.get(anyString())).thenReturn(Mono.just(activeTppDTO)); - when(citizenRepository.save(existingConsent)).thenReturn(Mono.just(existingConsent)); - when(dtoMapper.map(existingConsent)).thenReturn(CITIZEN_CONSENT_DTO); - - CitizenConsentDTO result = validationService.handleExistingConsent(existingConsent, activeTppDTO.getTppId(), CITIZEN_CONSENT).block(); - - assertNotNull(result); - assertEquals(CITIZEN_CONSENT_DTO, result); - } - - @Test - void validateTppAndSaveConsent_TppValidAndActive() { - - CitizenConsent citizenConsentWithValidConsent = CITIZEN_CONSENT; - String fiscalCode = citizenConsentWithValidConsent.getFiscalCode(); - TppDTO activeTppDTO = TPP_DTO; - activeTppDTO.setState(true); - - ConsentDetails consentDetails = new ConsentDetails(); - consentDetails.setTppState(true); - - citizenConsentWithValidConsent.getConsents().put(activeTppDTO.getTppId(), consentDetails); - - when(tppConnector.get(anyString())).thenReturn(Mono.just(activeTppDTO)); - when(citizenRepository.save(CITIZEN_CONSENT)).thenReturn(Mono.just(CITIZEN_CONSENT)); - when(dtoMapper.map(CITIZEN_CONSENT)).thenReturn(CITIZEN_CONSENT_DTO); - - CitizenConsentDTO result = validationService.validateTppAndSaveConsent(fiscalCode, activeTppDTO.getTppId(), CITIZEN_CONSENT).block(); - - assertNotNull(result); - assertEquals(CITIZEN_CONSENT_DTO, result); - verify(bloomFilterService, times(1)).add(fiscalCode); - } - - @Test - void validateTppAndSaveConsent_TppInvalid() { - - CitizenConsent citizenConsentWithInvalidConsent = CITIZEN_CONSENT; - String fiscalCode = citizenConsentWithInvalidConsent.getFiscalCode(); - - TppDTO inactiveTppDTO = TPP_DTO; - inactiveTppDTO.setState(false); - - ConsentDetails consentDetails = new ConsentDetails(); - consentDetails.setTppState(false); - - citizenConsentWithInvalidConsent.getConsents().put(inactiveTppDTO.getTppId(), consentDetails); - - when(tppConnector.get(anyString())).thenReturn(Mono.just(inactiveTppDTO)); - - StepVerifier.create(validationService.validateTppAndSaveConsent(fiscalCode, inactiveTppDTO.getTppId(), CITIZEN_CONSENT)) - .expectErrorMatches(throwable -> throwable instanceof ClientExceptionWithBody && - "TPP is not active or is invalid".equals(throwable.getMessage()) && - "TPP_NOT_FOUND".equals(((ClientExceptionWithBody) throwable).getCode())) - .verify(); - - verify(citizenRepository, never()).save(any()); - verify(bloomFilterService, never()).add(fiscalCode); - } - - @Test - void validateTppAndSaveConsent_TppNotFound() { - - String fiscalCode = CITIZEN_CONSENT.getFiscalCode(); - String tppId = "nonExistentTppId"; - - when(tppConnector.get(tppId)).thenReturn(Mono.error(new RuntimeException("TPP not found"))); - - 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); - } - - @Test - void validateTppAndSaveConsent_TppInactive() { - - String fiscalCode = CITIZEN_CONSENT.getFiscalCode(); - TppDTO inactiveTppDTO = TPP_DTO; - inactiveTppDTO.setState(false); - - ConsentDetails consentDetails = new ConsentDetails(); - consentDetails.setTppState(false); - - CITIZEN_CONSENT.getConsents().put(inactiveTppDTO.getTppId(), consentDetails); - - when(tppConnector.get(anyString())).thenReturn(Mono.just(inactiveTppDTO)); - - StepVerifier.create(validationService.validateTppAndSaveConsent(fiscalCode, inactiveTppDTO.getTppId(), CITIZEN_CONSENT)) - .expectErrorMatches(throwable -> throwable instanceof ClientExceptionWithBody && - "TPP is not active or is invalid".equals(throwable.getMessage()) && - "TPP_NOT_FOUND".equals(((ClientExceptionWithBody) throwable).getCode())) - .verify(); - - verify(citizenRepository, never()).save(any()); - verify(bloomFilterService, never()).add(fiscalCode); - } - - @Test - void handleExistingConsent_TppNotFound() { - - String tppId = "nonExistentTppId"; - - when(tppConnector.get(anyString())).thenReturn(Mono.error(new RuntimeException("TPP not found"))); - - StepVerifier.create(validationService.handleExistingConsent(CITIZEN_CONSENT, 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(anyString()); - } -} From fbcc864cac1536928425540734cdc8dfefbb351c Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Fri, 13 Dec 2024 12:51:35 +0100 Subject: [PATCH 02/13] Api fix --- .../pagopa/onboarding/citizen/service/CitizenServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java index e64044a..6afe473 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java @@ -59,7 +59,7 @@ public Mono createCitizenConsent(String fiscalCode, String tp .tcDate(LocalDateTime.now()) .build()); return citizenRepository.save(citizenConsent) - .map(mapperToDTO::map) + .map(savedConsent -> mapperToDTO.map(citizenConsent)) .doOnSuccess(savedConsent -> bloomFilterService.add(fiscalCode)); } else { Map consents = new HashMap<>(); From 957473c70b3500fed360532521fa003ccc7b030c Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Fri, 13 Dec 2024 12:56:42 +0100 Subject: [PATCH 03/13] Api fix --- .../onboarding/citizen/service/CitizenServiceTest.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/test/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceTest.java b/src/test/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceTest.java index 9c57c7b..3407cd1 100644 --- a/src/test/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceTest.java +++ b/src/test/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceTest.java @@ -106,20 +106,16 @@ void createCitizenConsent_AlreadyExists() { @Test void createCitizenConsent_AlreadyOnboardedOnAnotherTpp() { - CITIZEN_CONSENT_2.getConsents().put(TPP_ID_2, ConsentDetails.builder() - .tppState(true) - .tcDate(LocalDateTime.now()) - .build()); - CitizenConsentDTO expectedConsentDTO = dtoMapper.map(CITIZEN_CONSENT_2); when(tppConnector.get(anyString())).thenReturn(Mono.just(TPP_DTO)); when(citizenRepository.save(Mockito.any())).thenReturn(Mono.just(CITIZEN_CONSENT_2)); when(citizenRepository.findByFiscalCode(anyString())).thenReturn(Mono.just(CITIZEN_CONSENT)); + StepVerifier.create(citizenService.createCitizenConsent(FISCAL_CODE, TPP_ID_2)) .assertNext(response -> { assertNotNull(response); - assertEquals(expectedConsentDTO, response); + assertTrue(response.getConsents().containsKey(TPP_ID_2)); }) .verifyComplete(); From 7929df194a982c04554b0b1fb4e344333e687b81 Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Fri, 13 Dec 2024 13:07:23 +0100 Subject: [PATCH 04/13] Api fix --- .../onboarding/citizen/service/CitizenServiceImpl.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java index 6afe473..f67d5e4 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java @@ -58,15 +58,15 @@ public Mono createCitizenConsent(String fiscalCode, String tp .tppState(true) .tcDate(LocalDateTime.now()) .build()); - return citizenRepository.save(citizenConsent) - .map(savedConsent -> mapperToDTO.map(citizenConsent)) + citizenRepository.save(citizenConsent) + .map(mapperToDTO::map) .doOnSuccess(savedConsent -> bloomFilterService.add(fiscalCode)); } else { Map consents = new HashMap<>(); consents.put(tppId, citizenConsent.getConsents().get(tppId)); citizenConsent.setConsents(consents); - return Mono.just(mapperToDTO.map(citizenConsent)); } + return Mono.just(mapperToDTO.map(citizenConsent)); }) .switchIfEmpty(Mono.defer(() -> { Map consents = new HashMap<>(); From a1b3c1cc0bf38c8f2f5930bc26525a96405ce80d Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Fri, 13 Dec 2024 13:08:05 +0100 Subject: [PATCH 05/13] Api fix --- .../onboarding/citizen/service/CitizenServiceImpl.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java index f67d5e4..0b77794 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java @@ -61,11 +61,10 @@ public Mono createCitizenConsent(String fiscalCode, String tp citizenRepository.save(citizenConsent) .map(mapperToDTO::map) .doOnSuccess(savedConsent -> bloomFilterService.add(fiscalCode)); - } else { - Map consents = new HashMap<>(); - consents.put(tppId, citizenConsent.getConsents().get(tppId)); - citizenConsent.setConsents(consents); } + Map consents = new HashMap<>(); + consents.put(tppId, citizenConsent.getConsents().get(tppId)); + citizenConsent.setConsents(consents); return Mono.just(mapperToDTO.map(citizenConsent)); }) .switchIfEmpty(Mono.defer(() -> { From 168287455b44c2e53b6baa9e9379601b5bd84b45 Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Fri, 13 Dec 2024 13:15:57 +0100 Subject: [PATCH 06/13] Api fix --- .../pagopa/onboarding/citizen/service/CitizenServiceImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java index 0b77794..f40c8a2 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java @@ -59,7 +59,6 @@ public Mono createCitizenConsent(String fiscalCode, String tp .tcDate(LocalDateTime.now()) .build()); citizenRepository.save(citizenConsent) - .map(mapperToDTO::map) .doOnSuccess(savedConsent -> bloomFilterService.add(fiscalCode)); } Map consents = new HashMap<>(); From 31dbfa8c4923e4d74650958ae2bb134a75b8f623 Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Fri, 13 Dec 2024 13:55:08 +0100 Subject: [PATCH 07/13] Api fix --- .../onboarding/citizen/service/CitizenServiceImpl.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java index f40c8a2..fae65ce 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java @@ -58,8 +58,7 @@ public Mono createCitizenConsent(String fiscalCode, String tp .tppState(true) .tcDate(LocalDateTime.now()) .build()); - citizenRepository.save(citizenConsent) - .doOnSuccess(savedConsent -> bloomFilterService.add(fiscalCode)); + citizenRepository.save(citizenConsent); } Map consents = new HashMap<>(); consents.put(tppId, citizenConsent.getConsents().get(tppId)); @@ -78,9 +77,9 @@ public Mono createCitizenConsent(String fiscalCode, String tp .build(); return citizenRepository .save(citizenConsentToSave) - .map(mapperToDTO::map) - .doOnSuccess(savedConsent -> bloomFilterService.add(fiscalCode)); + .map(mapperToDTO::map); })) + .doOnSuccess(savedConsent -> bloomFilterService.add(fiscalCode)) ) .doOnSuccess(savedConsent -> log.info("[EMD][CREATE-CITIZEN-CONSENT] Created new citizen consent for fiscal code: {}", Utils.createSHA256(fiscalCode))); } From 9692ff6417d977d35480b72ca3d1a87615a02150 Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Fri, 13 Dec 2024 14:02:02 +0100 Subject: [PATCH 08/13] Api fix --- .../citizen/service/CitizenServiceImpl.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java index fae65ce..74fb048 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java @@ -54,11 +54,14 @@ public Mono createCitizenConsent(String fiscalCode, String tp .flatMap(tppResponse -> citizenRepository.findByFiscalCode(fiscalCode) .flatMap(citizenConsent -> { if (!citizenConsent.getConsents().containsKey(tppId)) { - citizenConsent.getConsents().put(tppId, ConsentDetails.builder() + citizenConsent + .getConsents().put(tppId, ConsentDetails.builder() .tppState(true) .tcDate(LocalDateTime.now()) .build()); - citizenRepository.save(citizenConsent); + citizenRepository + .save(citizenConsent) + .doOnSuccess(savedConsent -> bloomFilterService.add(fiscalCode)); } Map consents = new HashMap<>(); consents.put(tppId, citizenConsent.getConsents().get(tppId)); @@ -77,9 +80,9 @@ public Mono createCitizenConsent(String fiscalCode, String tp .build(); return citizenRepository .save(citizenConsentToSave) - .map(mapperToDTO::map); + .map(mapperToDTO::map) + .doOnSuccess(savedConsent -> bloomFilterService.add(fiscalCode)); })) - .doOnSuccess(savedConsent -> bloomFilterService.add(fiscalCode)) ) .doOnSuccess(savedConsent -> log.info("[EMD][CREATE-CITIZEN-CONSENT] Created new citizen consent for fiscal code: {}", Utils.createSHA256(fiscalCode))); } From c38bf2a89b662e4ca8aff8004d8f780e0ee4f130 Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Fri, 13 Dec 2024 14:13:55 +0100 Subject: [PATCH 09/13] Api fix --- .../citizen/service/CitizenServiceImpl.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java index 74fb048..81cd496 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java @@ -59,9 +59,7 @@ public Mono createCitizenConsent(String fiscalCode, String tp .tppState(true) .tcDate(LocalDateTime.now()) .build()); - citizenRepository - .save(citizenConsent) - .doOnSuccess(savedConsent -> bloomFilterService.add(fiscalCode)); + citizenRepository.save(citizenConsent); } Map consents = new HashMap<>(); consents.put(tppId, citizenConsent.getConsents().get(tppId)); @@ -80,11 +78,13 @@ public Mono createCitizenConsent(String fiscalCode, String tp .build(); return citizenRepository .save(citizenConsentToSave) - .map(mapperToDTO::map) - .doOnSuccess(savedConsent -> bloomFilterService.add(fiscalCode)); + .map(mapperToDTO::map); })) ) - .doOnSuccess(savedConsent -> log.info("[EMD][CREATE-CITIZEN-CONSENT] Created new citizen consent for fiscal code: {}", Utils.createSHA256(fiscalCode))); + .doOnSuccess(savedConsent -> { + log.info("[EMD][CREATE-CITIZEN-CONSENT] Created new citizen consent for fiscal code: {}", Utils.createSHA256(fiscalCode)); + bloomFilterService.add(fiscalCode); + }); } @Override public Mono switchState(String fiscalCode, String tppId){ From 95bf092339a1530b83f71490f0699f9d156092a7 Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Fri, 13 Dec 2024 14:19:24 +0100 Subject: [PATCH 10/13] Api fix --- .../pagopa/onboarding/citizen/service/CitizenServiceImpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java index 81cd496..db58039 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java @@ -59,7 +59,8 @@ public Mono createCitizenConsent(String fiscalCode, String tp .tppState(true) .tcDate(LocalDateTime.now()) .build()); - citizenRepository.save(citizenConsent); + return citizenRepository.save(citizenConsent) + .map(savedConsent -> mapperToDTO.map(citizenConsent)); } Map consents = new HashMap<>(); consents.put(tppId, citizenConsent.getConsents().get(tppId)); From 349e324ba64f0aa3a0333f5773d70a980a8061ae Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Fri, 13 Dec 2024 14:25:09 +0100 Subject: [PATCH 11/13] Api fix --- .../pagopa/onboarding/citizen/service/CitizenServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java index db58039..ebeb66d 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java @@ -60,7 +60,7 @@ public Mono createCitizenConsent(String fiscalCode, String tp .tcDate(LocalDateTime.now()) .build()); return citizenRepository.save(citizenConsent) - .map(savedConsent -> mapperToDTO.map(citizenConsent)); + .flatMap(savedConsent -> Mono.just(mapperToDTO.map(citizenConsent))); } Map consents = new HashMap<>(); consents.put(tppId, citizenConsent.getConsents().get(tppId)); From 0d61d8aa911f9d01a3fd0783a4d7c98f9c29b845 Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Fri, 13 Dec 2024 14:34:59 +0100 Subject: [PATCH 12/13] Api fix --- .../onboarding/citizen/service/CitizenServiceImpl.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java index ebeb66d..04615fb 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java @@ -60,7 +60,12 @@ public Mono createCitizenConsent(String fiscalCode, String tp .tcDate(LocalDateTime.now()) .build()); return citizenRepository.save(citizenConsent) - .flatMap(savedConsent -> Mono.just(mapperToDTO.map(citizenConsent))); + .flatMap(savedConsent ->{ + Map consents = new HashMap<>(); + consents.put(tppId, citizenConsent.getConsents().get(tppId)); + citizenConsent.setConsents(consents); + return Mono.just(mapperToDTO.map(citizenConsent)); + }); } Map consents = new HashMap<>(); consents.put(tppId, citizenConsent.getConsents().get(tppId)); From 3f3fa0f5319fb2a3cad427b4cfc001f616fb3bd0 Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Fri, 13 Dec 2024 14:53:40 +0100 Subject: [PATCH 13/13] Api fix --- .../citizen/service/CitizenServiceImpl.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java index 04615fb..f67cda4 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java @@ -105,18 +105,16 @@ public Mono switchState(String fiscalCode, String tppId){ if(!citizenConsent.getConsents().containsKey(tppId)) return Mono.error(exceptionMap.throwException (ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during update state process")); - ConsentDetails consentDetails = citizenConsent.getConsents().get(tppId); consentDetails.setTppState(!consentDetails.getTppState()); - citizenRepository.save(citizenConsent); - - Map consents = new HashMap<>(); - consents.put(tppId,citizenConsent.getConsents().get(tppId)); - citizenConsent.setConsents(consents); - - return Mono.just(citizenConsent); + return citizenRepository.save(citizenConsent) + .flatMap(savedConsent -> { + Map consents = new HashMap<>(); + consents.put(tppId, citizenConsent.getConsents().get(tppId)); + citizenConsent.setConsents(consents); + return Mono.just(mapperToDTO.map(citizenConsent)); + }); }) - .map(mapperToDTO::map) .doOnSuccess(savedConsent -> log.info("[EMD][CITIZEN][UPDATE-CHANNEL-STATE] Updated state")); }