diff --git a/core/src/main/java/it/pagopa/selfcare/party/registry_proxy/core/InstitutionServiceImpl.java b/core/src/main/java/it/pagopa/selfcare/party/registry_proxy/core/InstitutionServiceImpl.java index b7936dbb..1b2fcde0 100644 --- a/core/src/main/java/it/pagopa/selfcare/party/registry_proxy/core/InstitutionServiceImpl.java +++ b/core/src/main/java/it/pagopa/selfcare/party/registry_proxy/core/InstitutionServiceImpl.java @@ -1,17 +1,21 @@ package it.pagopa.selfcare.party.registry_proxy.core; +import com.opencsv.bean.CsvToBean; +import com.opencsv.bean.CsvToBeanBuilder; import it.pagopa.selfcare.party.registry_proxy.connector.api.IndexSearchService; +import it.pagopa.selfcare.party.registry_proxy.connector.api.IndexWriterService; import it.pagopa.selfcare.party.registry_proxy.connector.exception.ResourceNotFoundException; -import it.pagopa.selfcare.party.registry_proxy.connector.model.Entity; -import it.pagopa.selfcare.party.registry_proxy.connector.model.Institution; +import it.pagopa.selfcare.party.registry_proxy.connector.model.*; import it.pagopa.selfcare.party.registry_proxy.connector.model.Institution.Field; -import it.pagopa.selfcare.party.registry_proxy.connector.model.Origin; -import it.pagopa.selfcare.party.registry_proxy.connector.model.QueryResult; +import it.pagopa.selfcare.party.registry_proxy.connector.rest.client.OpenDataRestClient; +import it.pagopa.selfcare.party.registry_proxy.connector.rest.model.IPAOpenDataInstitution; import it.pagopa.selfcare.party.registry_proxy.core.exception.TooManyResourceFoundException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; +import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -23,9 +27,15 @@ class InstitutionServiceImpl implements InstitutionService { private final IndexSearchService indexSearchService; + private final OpenDataRestClient openDataRestClient; + private final IndexWriterService institutionIndexWriterService; + + @Autowired - InstitutionServiceImpl(IndexSearchService indexSearchService) { + InstitutionServiceImpl(IndexSearchService indexSearchService, OpenDataRestClient openDataRestClient, IndexWriterService institutionIndexWriterService) { + this.openDataRestClient = openDataRestClient; + this.institutionIndexWriterService = institutionIndexWriterService; log.trace("Initializing {}", InstitutionServiceImpl.class.getSimpleName()); this.indexSearchService = indexSearchService; } @@ -85,4 +95,36 @@ public Institution findById(String id, Optional origin, List cat return institution; } } + + @Scheduled(cron = "0 0 2 * * *") + void updateInstitutionsIndex() { + log.trace("start update Institutions IPA index"); + List institutions = getInstitutions(); + if (!institutions.isEmpty()) { + institutionIndexWriterService.cleanIndex(Entity.INSTITUTION.toString()); + institutionIndexWriterService.adds(institutions); + } + log.trace("updated Institutions IPA index end"); + } + + private List getInstitutions() { + log.trace("getInstitutions start"); + List institutions; + final String csv = openDataRestClient.retrieveInstitutions(); + + try (Reader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(csv.getBytes())))) { + CsvToBean csvToBean = new CsvToBeanBuilder(reader) + .withType(IPAOpenDataInstitution.class) + .withIgnoreLeadingWhiteSpace(true) + .build(); + institutions = csvToBean.parse(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + log.trace("getInstitutions end"); + return institutions; + } + } + diff --git a/core/src/test/java/it/pagopa/selfcare/party/registry_proxy/core/InstitutionServiceImplTest.java b/core/src/test/java/it/pagopa/selfcare/party/registry_proxy/core/InstitutionServiceImplTest.java index fde3fd15..6809ab26 100644 --- a/core/src/test/java/it/pagopa/selfcare/party/registry_proxy/core/InstitutionServiceImplTest.java +++ b/core/src/test/java/it/pagopa/selfcare/party/registry_proxy/core/InstitutionServiceImplTest.java @@ -1,10 +1,13 @@ package it.pagopa.selfcare.party.registry_proxy.core; import it.pagopa.selfcare.party.registry_proxy.connector.api.IndexSearchService; +import it.pagopa.selfcare.party.registry_proxy.connector.api.IndexWriterService; import it.pagopa.selfcare.party.registry_proxy.connector.model.*; import it.pagopa.selfcare.party.registry_proxy.connector.model.Institution.Field; import it.pagopa.selfcare.party.registry_proxy.connector.exception.ResourceNotFoundException; +import it.pagopa.selfcare.party.registry_proxy.connector.rest.client.OpenDataRestClient; import it.pagopa.selfcare.party.registry_proxy.core.exception.TooManyResourceFoundException; +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; @@ -31,6 +34,12 @@ class InstitutionServiceImplTest { @InjectMocks private InstitutionServiceImpl institutionService; + @Mock + private OpenDataRestClient openDataRestClient; + + @Mock + private IndexWriterService institutionIndexWriterService; + @Test void search_emptySearchText() { @@ -300,4 +309,13 @@ void findById_ResourceNotFound5() { verifyNoMoreInteractions(indexSearchService); } + @Test + void updateInstitutionsIndex() { + final String response = "id,Codice_IPA,Denominazione_ente,Codice_fiscale_ente,Mail1,Data_aggiornamento\n" + + "id,Codice_IPA,Denominazione_ente,Codice_fiscale_ente,Mail1,2024-01-01"; + when(openDataRestClient.retrieveInstitutions()).thenReturn(response); + Executable executable = () -> institutionService.updateInstitutionsIndex(); + Assertions.assertDoesNotThrow(executable); + } + } \ No newline at end of file