-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SELC-4840] feat: added ivass client and ivass connector in rest conn…
…ectors (#221)
- Loading branch information
Showing
9 changed files
with
308 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
.../main/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/IvassConnectorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package it.pagopa.selfcare.party.registry_proxy.connector.rest; | ||
|
||
import it.pagopa.selfcare.party.registry_proxy.connector.api.IvassDataConnector; | ||
import it.pagopa.selfcare.party.registry_proxy.connector.model.InsuranceCompany; | ||
import it.pagopa.selfcare.party.registry_proxy.connector.rest.client.IvassRestClient; | ||
import it.pagopa.selfcare.party.registry_proxy.connector.rest.utils.IvassUtils; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@Service | ||
@PropertySource("classpath:config/ivass-rest-config.properties") | ||
@ConditionalOnProperty( | ||
value = "ivass.file.connector.type", | ||
havingValue = "rest") | ||
public class IvassConnectorImpl implements IvassDataConnector { | ||
private final IvassRestClient ivassRestClient; | ||
private final List<String> registryTypesAdmitted; | ||
private final List<String> workTypesAdmitted; | ||
private final IvassUtils ivassUtils; | ||
|
||
public IvassConnectorImpl( | ||
IvassRestClient ivassRestClient, | ||
@Value("#{'${ivass.registryTypes.admitted}'.split(',')}") List<String> registryTypes, | ||
@Value("#{'${ivass.workTypes.admitted}'.split(',')}") List<String> registryWorkTypes, | ||
IvassUtils ivassUtils | ||
) { | ||
this.ivassRestClient = ivassRestClient; | ||
this.registryTypesAdmitted = registryTypes; | ||
this.workTypesAdmitted = registryWorkTypes; | ||
this.ivassUtils = ivassUtils; | ||
} | ||
|
||
@Override | ||
public List<InsuranceCompany> getInsurances() { | ||
byte[] zip = ivassRestClient.getInsurancesZip(); | ||
byte[] csv = ivassUtils.extractFirstEntryByteArrayFromZip(zip); | ||
csv = ivassUtils.manageUTF8BOM(csv); | ||
List<InsuranceCompany> companies = ivassUtils.readCsv(csv); | ||
return filterCompanies(companies); | ||
} | ||
|
||
private List<InsuranceCompany> filterCompanies(List<InsuranceCompany> companies) { | ||
return companies | ||
.stream() | ||
.filter(company -> StringUtils.hasText(company.getDigitalAddress()) | ||
&& workTypesAdmitted.contains(company.getWorkType()) | ||
&& registryTypesAdmitted | ||
.stream() | ||
.anyMatch(StringUtils.trimAllWhitespace(company.getRegisterType() | ||
.split("-")[0])::equals)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...n/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/client/IvassRestClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package it.pagopa.selfcare.party.registry_proxy.connector.rest.client; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Slf4j | ||
@Component | ||
@PropertySource("classpath:config/ivass-rest-config.properties") | ||
public class IvassRestClient { | ||
private final RestTemplate ivassRestTemplate; | ||
private final String ivassBasePath; | ||
private final String getInsurancesPath; | ||
|
||
public IvassRestClient( | ||
@Value("${ivass.rest.endpoint}") String ivassBasePath, | ||
@Value("${ivass.rest.getInsurances.path}") String getInsurancesPath, | ||
RestTemplate ivassRestTemplate) { | ||
this.ivassRestTemplate = ivassRestTemplate; | ||
this.ivassBasePath = ivassBasePath; | ||
this.getInsurancesPath = getInsurancesPath; | ||
} | ||
|
||
public byte[] getInsurancesZip() { | ||
log.info("getInsurances start"); | ||
String apiPath = this.ivassBasePath + getInsurancesPath; | ||
return ivassRestTemplate.getForObject(apiPath, byte[].class); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...ava/it/pagopa/selfcare/party/registry_proxy/connector/rest/config/RestTemplateConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package it.pagopa.selfcare.party.registry_proxy.connector.rest.config; | ||
|
||
import it.pagopa.selfcare.party.registry_proxy.connector.rest.decoder.RestTemplateErrorHandler; | ||
import it.pagopa.selfcare.party.registry_proxy.connector.rest.interceptor.IvassInterceptor; | ||
import org.apache.http.client.CookieStore; | ||
import org.apache.http.impl.client.BasicCookieStore; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.apache.http.impl.client.LaxRedirectStrategy; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.List; | ||
|
||
@Configuration | ||
public class RestTemplateConfig { | ||
private final IvassInterceptor ivassInterceptor; | ||
private final RestTemplateErrorHandler restTemplateErrorHandler; | ||
|
||
public RestTemplateConfig(IvassInterceptor ivassInterceptor, RestTemplateErrorHandler restTemplateErrorHandler) { | ||
this.ivassInterceptor = ivassInterceptor; | ||
this.restTemplateErrorHandler = restTemplateErrorHandler; | ||
} | ||
|
||
@Bean | ||
public RestTemplate ivassRestTemplate() { | ||
CookieStore cookieStore = new BasicCookieStore(); | ||
|
||
CloseableHttpClient httpClient = HttpClientBuilder.create() | ||
.setDefaultCookieStore(cookieStore) | ||
.setRedirectStrategy(new LaxRedirectStrategy()) | ||
.build(); | ||
|
||
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); | ||
|
||
RestTemplate restTemplate = new RestTemplate(factory); | ||
restTemplate.setInterceptors(List.of(ivassInterceptor)); | ||
restTemplate.setErrorHandler(restTemplateErrorHandler); | ||
return restTemplate; | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
connector/rest/src/main/resources/config/ivass-rest-config.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ivass.rest.endpoint=${IVASS_BASE_URL:http://localhost:8085} | ||
ivass.rest.getInsurances.path=/RIGAInquiry-public/getAreaDownloadExport.do?referenceDate=&product=VFLUSSO_IMPRESE&language=IT&exportType=CSV&isCompressed=S | ||
ivass.registryTypes.admitted=${IVASS_REGISTRY_TYPES:ElencoI,ElencoII,SezioneI,SezioneII} | ||
ivass.workTypes.admitted=${IVASS_WORK_TYPES:VITA,PICCOLO CUMULO,MISTA} |
80 changes: 80 additions & 0 deletions
80
...t/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/IvassConnectorImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package it.pagopa.selfcare.party.registry_proxy.connector.rest; | ||
|
||
import it.pagopa.selfcare.party.registry_proxy.connector.model.InsuranceCompany; | ||
import it.pagopa.selfcare.party.registry_proxy.connector.rest.client.IvassRestClient; | ||
import it.pagopa.selfcare.party.registry_proxy.connector.rest.model.IvassDataTemplate; | ||
import it.pagopa.selfcare.party.registry_proxy.connector.rest.utils.IvassUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.*; | ||
|
||
class IvassConnectorImplTest { | ||
private IvassRestClient ivassRestClient; | ||
private IvassUtils ivassUtils; | ||
private IvassConnectorImpl ivassConnector; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.ivassRestClient = mock(IvassRestClient.class); | ||
this.ivassUtils = mock(IvassUtils.class); | ||
List<String> registryTypes = Arrays.asList("ElencoI","ElencoII","SezioneI","SezioneII"); | ||
List<String> workTypes = Arrays.asList("VITA","PICCOLO CUMULO","MISTA"); | ||
ivassConnector = new IvassConnectorImpl(ivassRestClient, registryTypes, workTypes, ivassUtils); | ||
} | ||
|
||
@Test | ||
void getInsurances_shouldReturnFilteredCompanies() { | ||
byte[] zip = new byte[]{0, 1, 2, 3, 4}; | ||
byte[] csv = new byte[]{5, 6, 7, 8, 9}; | ||
|
||
IvassDataTemplate company1 = new IvassDataTemplate(); | ||
company1.setDigitalAddress("digitalAddress1"); | ||
company1.setWorkType("VITA"); | ||
company1.setRegisterType("ElencoI - test"); | ||
company1.setTaxCode("taxCode1"); | ||
|
||
IvassDataTemplate company2 = new IvassDataTemplate(); | ||
company2.setDigitalAddress("digitalAddress2"); | ||
company2.setWorkType("VITA"); | ||
company2.setRegisterType("ElencoII - test"); | ||
company2.setTaxCode("taxCode2"); | ||
|
||
|
||
List<InsuranceCompany> companies = Arrays.asList(company1, company2); | ||
|
||
when(ivassRestClient.getInsurancesZip()).thenReturn(zip); | ||
when(ivassUtils.extractFirstEntryByteArrayFromZip(zip)).thenReturn(csv); | ||
when(ivassUtils.readCsv(csv)).thenReturn(companies); | ||
when(ivassUtils.manageUTF8BOM(csv)).thenReturn(csv); | ||
|
||
List<InsuranceCompany> result = ivassConnector.getInsurances(); | ||
|
||
assertEquals(companies.size(), result.size()); | ||
verify(ivassRestClient, times(1)).getInsurancesZip(); | ||
verify(ivassUtils, times(1)).extractFirstEntryByteArrayFromZip(zip); | ||
} | ||
|
||
@Test | ||
void getInsurances_shouldReturnEmptyList_whenNoCompaniesMatchFilter() { | ||
byte[] zip = new byte[]{0, 1, 2, 3, 4}; | ||
byte[] csv = new byte[]{5, 6, 7, 8, 9}; | ||
List<InsuranceCompany> companies = Arrays.asList(new IvassDataTemplate(), new IvassDataTemplate()); | ||
|
||
when(ivassRestClient.getInsurancesZip()).thenReturn(zip); | ||
when(ivassUtils.extractFirstEntryByteArrayFromZip(zip)).thenReturn(csv); | ||
when(ivassUtils.readCsv(csv)).thenReturn(companies); | ||
|
||
ivassConnector = spy(ivassConnector); | ||
|
||
List<InsuranceCompany> result = ivassConnector.getInsurances(); | ||
|
||
assertEquals(0, result.size()); | ||
verify(ivassRestClient, times(1)).getInsurancesZip(); | ||
verify(ivassUtils, times(1)).extractFirstEntryByteArrayFromZip(zip); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...va/it/pagopa/selfcare/party/registry_proxy/connector/rest/client/IvassRestClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package it.pagopa.selfcare.party.registry_proxy.connector.rest.client; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.*; | ||
|
||
class IvassRestClientTest { | ||
|
||
@Mock | ||
private RestTemplate restTemplate; | ||
|
||
@InjectMocks | ||
private IvassRestClient ivassRestClient; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
ivassRestClient = new IvassRestClient("http://example.com", "getInsurancesPath", restTemplate); | ||
} | ||
|
||
@Test | ||
void getInsurancesZip_shouldReturnByteArray_whenSuccessful() { | ||
byte[] expectedResponse = new byte[]{1, 2, 3}; | ||
when(restTemplate.getForObject(anyString(), eq(byte[].class))).thenReturn(expectedResponse); | ||
|
||
byte[] result = ivassRestClient.getInsurancesZip(); | ||
|
||
assertArrayEquals(expectedResponse, result); | ||
verify(restTemplate, times(1)).getForObject(anyString(), eq(byte[].class)); | ||
} | ||
|
||
@Test | ||
void getInsurancesZip_shouldThrowException_whenRestTemplateThrowsException() { | ||
when(restTemplate.getForObject(anyString(), eq(byte[].class))).thenThrow(new RuntimeException("Test exception")); | ||
|
||
assertThrows(RuntimeException.class, () -> ivassRestClient.getInsurancesZip()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters