From 74ac675a44d3f7d80d35dca8b2b3dad64aa870bd Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Tue, 22 Oct 2024 16:41:47 +0200 Subject: [PATCH 1/3] Mapper fix --- .../citizen/dto/mapper/CitizenConsentObjectToDTOMapper.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/dto/mapper/CitizenConsentObjectToDTOMapper.java b/src/main/java/it/gov/pagopa/onboarding/citizen/dto/mapper/CitizenConsentObjectToDTOMapper.java index fa68c7c..022d91d 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/dto/mapper/CitizenConsentObjectToDTOMapper.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/dto/mapper/CitizenConsentObjectToDTOMapper.java @@ -10,6 +10,7 @@ public class CitizenConsentObjectToDTOMapper { public CitizenConsentDTO map(CitizenConsent citizenConsent){ return CitizenConsentDTO.builder() + .hashedFiscalCode(citizenConsent.getHashedFiscalCode()) .tppState(citizenConsent.getTppState()) .tppId(citizenConsent.getTppId()) .creationDate(citizenConsent.getCreationDate()) From 1f71071a9e0289fa5c6b7264d3ea3cf20834d393 Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Wed, 23 Oct 2024 11:10:18 +0200 Subject: [PATCH 2/3] Mapper fix --- .../citizen/service/CitizenServiceImpl.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 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 51962a8..35bd2b1 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 @@ -32,7 +32,6 @@ public CitizenServiceImpl(CitizenRepository citizenRepository, CitizenConsentObj this.mapperToObject = mapperToObject; this.exceptionMap = exceptionMap; } - @Override public Mono createCitizenConsent(CitizenConsentDTO citizenConsentDTO) { CitizenConsent citizenConsent = mapperToObject.map(citizenConsentDTO); @@ -41,10 +40,16 @@ public Mono createCitizenConsent(CitizenConsentDTO citizenCon citizenConsent.setCreationDate(LocalDateTime.now()); citizenConsent.setLastUpdateDate(LocalDateTime.now()); log.info("[EMD-CITIZEN][CREATE] Received consent: {}",inputSanify(citizenConsent.toString())); - return citizenRepository.save(citizenConsent) - .map(mapperToDTO::map) - .doOnSuccess(savedConsent -> log.info("[EMD][CREATE-CITIZEN-CONSENT] Created")); - + return citizenRepository.findById(hashedFiscalCode) + .flatMap(existingConsent -> { + log.info("[EMD][CREATE-CITIZEN-CONSENT] Citizen consent already exists"); + return Mono.just(mapperToDTO.map(existingConsent)); + }) + .switchIfEmpty( + citizenRepository.save(citizenConsent) + .doOnSuccess(savedConsent -> log.info("[EMD][CREATE-CITIZEN-CONSENT] Created new citizen consent")) + .map(mapperToDTO::map) + ); } @Override @@ -54,7 +59,7 @@ public Mono updateChannelState(String fiscalCode, String tppI ,hashedFiscalCode, inputSanify(tppId), tppState); return citizenRepository.findByHashedFiscalCodeAndTppId(hashedFiscalCode, tppId) .switchIfEmpty(Mono.error(exceptionMap.throwException - (ExceptionName.CITIZEN_NOT_ONBOARDED,"Citizen not founded during update state process"))) + (ExceptionName.CITIZEN_NOT_ONBOARDED,"Citizen consent not founded during update state process"))) .flatMap(citizenConsent -> { citizenConsent.setTppState(tppState); citizenConsent.setLastUpdateDate(LocalDateTime.now()); @@ -70,9 +75,9 @@ public Mono getConsentStatus(String fiscalCode, String tppId) log.info("[EMD-CITIZEN][GET-CONSENT-STATUS] Received hashedFiscalCode: {} and tppId: {}",hashedFiscalCode,inputSanify(tppId)); return citizenRepository.findByHashedFiscalCodeAndTppId(hashedFiscalCode, tppId) .switchIfEmpty(Mono.error(exceptionMap.throwException - (ExceptionName.CITIZEN_NOT_ONBOARDED,"Citizen not founded during get process "))) + (ExceptionName.CITIZEN_NOT_ONBOARDED,"Citizen consent not founded during get process "))) .map(mapperToDTO::map) - .doOnSuccess(consent -> log.info("[EMD-CITIZEN][GET-CONSENT-STATUS] Consent found:: {}",consent)); + .doOnSuccess(consent -> log.info("[EMD-CITIZEN][GET-CONSENT-STATUS] Consent consent found:: {}",consent)); } From 687c16ba11259272429f160c6e58f62ff6f92b36 Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Wed, 23 Oct 2024 11:33:07 +0200 Subject: [PATCH 3/3] Mapper fix --- .../citizen/service/CitizenServiceImpl.java | 2 +- .../citizen/service/CitizenServiceTest.java | 25 +++++++++++++++++-- 2 files changed, 24 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 35bd2b1..feb0baf 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 @@ -48,7 +48,7 @@ public Mono createCitizenConsent(CitizenConsentDTO citizenCon .switchIfEmpty( citizenRepository.save(citizenConsent) .doOnSuccess(savedConsent -> log.info("[EMD][CREATE-CITIZEN-CONSENT] Created new citizen consent")) - .map(mapperToDTO::map) + .flatMap(savedConsent -> Mono.just(mapperToDTO.map(savedConsent))) // Map the saved consent ); } 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 ad99409..5f3b54e 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 @@ -28,6 +28,7 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; @ExtendWith({SpringExtension.class, MockitoExtension.class}) @ContextConfiguration(classes = { @@ -61,6 +62,26 @@ void createCitizenConsent_Ok() { Mockito.when(citizenRepository.save(Mockito.any())) .thenReturn(Mono.just(CITIZEN_CONSENT)); + Mockito.when(citizenRepository.findById(anyString())) + .thenReturn(Mono.empty()); + + CitizenConsentDTO response = citizenService.createCitizenConsent(CITIZEN_CONSENT_DTO).block(); + assertNotNull(response); + + assertEquals(citizenConsentDTO, response); + } + + @Test + void createCitizenConsent_AlreadyExists() { + + CitizenConsentDTO citizenConsentDTO = dtoMapper.map(CITIZEN_CONSENT); + + Mockito.when(citizenRepository.save(Mockito.any())) + .thenReturn(Mono.empty()); + + Mockito.when(citizenRepository.findById(anyString())) + .thenReturn(Mono.just(CITIZEN_CONSENT)); + CitizenConsentDTO response = citizenService.createCitizenConsent(CITIZEN_CONSENT_DTO).block(); assertNotNull(response); @@ -103,7 +124,7 @@ void updateChannelState_Ko_CitizenNotOnboarded() { Executable executable = () -> citizenService.updateChannelState(FISCAL_CODE, TPP_ID, true).block(); ClientExceptionWithBody exception = assertThrows(ClientExceptionWithBody.class, executable); - assertEquals("Citizen not founded during update state process", exception.getMessage()); + assertEquals("Citizen consent not founded during update state process", exception.getMessage()); } @Test @@ -126,7 +147,7 @@ void getConsentStatus_Ko_CitizenNotOnboarded() { Executable executable = () -> citizenService.getConsentStatus(FISCAL_CODE, TPP_ID).block(); ClientExceptionWithBody exception = assertThrows(ClientExceptionWithBody.class, executable); - assertEquals("Citizen not founded during get process ", exception.getMessage()); + assertEquals("Citizen consent not founded during get process ", exception.getMessage()); } @Test