Skip to content

Commit

Permalink
[SELC - 1547] feat: added scheduler for Institutions IPA index (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
gianmarcoplutino authored Oct 1, 2024
1 parent 41bd2e4 commit 5653fe0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,9 +27,15 @@
class InstitutionServiceImpl implements InstitutionService {

private final IndexSearchService<Institution> indexSearchService;
private final OpenDataRestClient openDataRestClient;
private final IndexWriterService<Institution> institutionIndexWriterService;



@Autowired
InstitutionServiceImpl(IndexSearchService<Institution> indexSearchService) {
InstitutionServiceImpl(IndexSearchService<Institution> indexSearchService, OpenDataRestClient openDataRestClient, IndexWriterService<Institution> institutionIndexWriterService) {
this.openDataRestClient = openDataRestClient;
this.institutionIndexWriterService = institutionIndexWriterService;
log.trace("Initializing {}", InstitutionServiceImpl.class.getSimpleName());
this.indexSearchService = indexSearchService;
}
Expand Down Expand Up @@ -85,4 +95,36 @@ public Institution findById(String id, Optional<Origin> origin, List<String> cat
return institution;
}
}

@Scheduled(cron = "0 0 2 * * *")
void updateInstitutionsIndex() {
log.trace("start update Institutions IPA index");
List<Institution> institutions = getInstitutions();
if (!institutions.isEmpty()) {
institutionIndexWriterService.cleanIndex(Entity.INSTITUTION.toString());
institutionIndexWriterService.adds(institutions);
}
log.trace("updated Institutions IPA index end");
}

private List<Institution> getInstitutions() {
log.trace("getInstitutions start");
List<Institution> institutions;
final String csv = openDataRestClient.retrieveInstitutions();

try (Reader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(csv.getBytes())))) {
CsvToBean<Institution> csvToBean = new CsvToBeanBuilder<Institution>(reader)
.withType(IPAOpenDataInstitution.class)
.withIgnoreLeadingWhiteSpace(true)
.build();
institutions = csvToBean.parse();
} catch (IOException e) {
throw new RuntimeException(e);
}

log.trace("getInstitutions end");
return institutions;
}

}

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -31,6 +34,12 @@ class InstitutionServiceImplTest {
@InjectMocks
private InstitutionServiceImpl institutionService;

@Mock
private OpenDataRestClient openDataRestClient;

@Mock
private IndexWriterService<Institution> institutionIndexWriterService;


@Test
void search_emptySearchText() {
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 5653fe0

Please sign in to comment.