diff --git a/app/src/main/resources/config/application.yml b/app/src/main/resources/config/application.yml index da3b4949..95f82d54 100644 --- a/app/src/main/resources/config/application.yml +++ b/app/src/main/resources/config/application.yml @@ -56,17 +56,6 @@ resilience4j: circuit-breaker-aspect-order: 1 instances: geotaxCircuitbreaker: - wait-duration-in-open-state: 1m - permitted-number-of-calls-in-half-open-state: 3 - sliding-window-type: count-based - sliding-window-size: 3 - minimum-number-of-calls: 3 - slow-call-duration-threshold: 10s - slow-call-rate-threshold: 60 - failure-rate-threshold: 60 - ignore-exceptions: - - it.pagopa.selfcare.party.registry_proxy.connector.exception.ResourceNotFoundException - pdndInfoCamereCircuitbreaker: wait-duration-in-open-state: 1m permitted-number-of-calls-in-half-open-state: 3 sliding-window-type: count-based diff --git a/connector/rest/src/main/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/PDNDInfoCamereConnectorImpl.java b/connector/rest/src/main/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/PDNDInfoCamereConnectorImpl.java index 8f0f89a9..e4c2d834 100644 --- a/connector/rest/src/main/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/PDNDInfoCamereConnectorImpl.java +++ b/connector/rest/src/main/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/PDNDInfoCamereConnectorImpl.java @@ -1,9 +1,6 @@ package it.pagopa.selfcare.party.registry_proxy.connector.rest; -import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker; -import io.github.resilience4j.retry.annotation.Retry; import it.pagopa.selfcare.party.registry_proxy.connector.api.PDNDInfoCamereConnector; -import it.pagopa.selfcare.party.registry_proxy.connector.exception.ResourceNotFoundException; import it.pagopa.selfcare.party.registry_proxy.connector.model.nationalregistriespdnd.PDNDBusiness; import it.pagopa.selfcare.party.registry_proxy.connector.rest.client.PDNDInfoCamereRestClient; import it.pagopa.selfcare.party.registry_proxy.connector.rest.config.PDNDInfoCamereRestClientConfig; @@ -20,50 +17,42 @@ @Slf4j @Service public class PDNDInfoCamereConnectorImpl implements PDNDInfoCamereConnector { - public static final String ERROR_PDND_INFO_CAMERE_REST_CLIENT_MESSAGE = "Error pdnd-infocamere rest client, message: %s"; - - private final PDNDInfoCamereRestClient pdndInfoCamereRestClient; - private final PDNDBusinessMapper pdndBusinessMapper; - private final TokenProvider tokenProvider; - private final PDNDInfoCamereRestClientConfig pdndInfoCamereRestClientConfig; - - public PDNDInfoCamereConnectorImpl(PDNDInfoCamereRestClient pdndInfoCamereRestClient, PDNDBusinessMapper pdndBusinessMapper, TokenProvider tokenProvider, PDNDInfoCamereRestClientConfig pdndInfoCamereRestClientConfig) { - this.pdndInfoCamereRestClient = pdndInfoCamereRestClient; - this.pdndBusinessMapper = pdndBusinessMapper; - this.tokenProvider = tokenProvider; - this.pdndInfoCamereRestClientConfig = pdndInfoCamereRestClientConfig; - } - - @Override - @CircuitBreaker(name = "pdndInfoCamereCircuitbreaker", fallbackMethod = "fallbackRetrieveInstitutionByDescription") - @Retry(name = "retryServiceUnavailable") - public List retrieveInstitutionsPdndByDescription(String description) { - Assert.hasText(description, "Description is required"); - ClientCredentialsResponse tokenResponse = tokenProvider.getTokenPdnd(pdndInfoCamereRestClientConfig.getPdndSecretValue()); - String bearer = "Bearer " + tokenResponse.getAccessToken(); - List result = pdndInfoCamereRestClient.retrieveInstitutionsPdndByDescription(description, bearer); - return pdndBusinessMapper.toPDNDBusinesses(result); - } - - public List fallbackRetrieveInstitutionByDescription(RuntimeException e) { - log.error(String.format(ERROR_PDND_INFO_CAMERE_REST_CLIENT_MESSAGE, e.getMessage())); - return List.of(); - } - - @Override - @CircuitBreaker(name = "pdndInfoCamereCircuitbreaker", fallbackMethod = "fallbackRetrieveInstitutionByTaxCode") - @Retry(name = "retryServiceUnavailable") - public PDNDBusiness retrieveInstitutionPdndByTaxCode(String taxCode) { - Assert.hasText(taxCode, "TaxCode is required"); - ClientCredentialsResponse tokenResponse = tokenProvider.getTokenPdnd(pdndInfoCamereRestClientConfig.getPdndSecretValue()); - String bearer = "Bearer " + tokenResponse.getAccessToken(); - PDNDImpresa result = pdndInfoCamereRestClient.retrieveInstitutionPdndByTaxCode(taxCode, bearer).get(0); - return pdndBusinessMapper.toPDNDBusiness(result); - } - - public PDNDBusiness fallbackRetrieveInstitutionByTaxCode(RuntimeException e) { - log.error(String.format(ERROR_PDND_INFO_CAMERE_REST_CLIENT_MESSAGE, e.getMessage()), e); - throw new ResourceNotFoundException(""); - } + private final PDNDInfoCamereRestClient pdndInfoCamereRestClient; + private final PDNDBusinessMapper pdndBusinessMapper; + private final TokenProvider tokenProvider; + private final PDNDInfoCamereRestClientConfig pdndInfoCamereRestClientConfig; + + public PDNDInfoCamereConnectorImpl( + PDNDInfoCamereRestClient pdndInfoCamereRestClient, + PDNDBusinessMapper pdndBusinessMapper, + TokenProvider tokenProvider, + PDNDInfoCamereRestClientConfig pdndInfoCamereRestClientConfig) { + this.pdndInfoCamereRestClient = pdndInfoCamereRestClient; + this.pdndBusinessMapper = pdndBusinessMapper; + this.tokenProvider = tokenProvider; + this.pdndInfoCamereRestClientConfig = pdndInfoCamereRestClientConfig; + } + + @Override + public List retrieveInstitutionsPdndByDescription(String description) { + Assert.hasText(description, "Description is required"); + ClientCredentialsResponse tokenResponse = + tokenProvider.getTokenPdnd(pdndInfoCamereRestClientConfig.getPdndSecretValue()); + String bearer = "Bearer " + tokenResponse.getAccessToken(); + List result = + pdndInfoCamereRestClient.retrieveInstitutionsPdndByDescription(description, bearer); + return pdndBusinessMapper.toPDNDBusinesses(result); + } + + @Override + public PDNDBusiness retrieveInstitutionPdndByTaxCode(String taxCode) { + Assert.hasText(taxCode, "TaxCode is required"); + ClientCredentialsResponse tokenResponse = + tokenProvider.getTokenPdnd(pdndInfoCamereRestClientConfig.getPdndSecretValue()); + String bearer = "Bearer " + tokenResponse.getAccessToken(); + PDNDImpresa result = + pdndInfoCamereRestClient.retrieveInstitutionPdndByTaxCode(taxCode, bearer).get(0); + return pdndBusinessMapper.toPDNDBusiness(result); + } } diff --git a/connector/rest/src/main/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/model/PDNDImpresa.java b/connector/rest/src/main/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/model/PDNDImpresa.java index 2097f4df..e4b57e6b 100644 --- a/connector/rest/src/main/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/model/PDNDImpresa.java +++ b/connector/rest/src/main/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/model/PDNDImpresa.java @@ -1,44 +1,51 @@ package it.pagopa.selfcare.party.registry_proxy.connector.rest.model; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.Objects; import lombok.Data; @Data +@JsonIgnoreProperties(ignoreUnknown = true) public class PDNDImpresa { - @JsonProperty("ProgressivoImpresa") - private long progressivoImpresa; + @JsonProperty("ProgressivoImpresa") + private long progressivoImpresa; - @JsonProperty("CodiceFiscale") - private String businessTaxId; + @JsonProperty("CodiceFiscale") + private String businessTaxId; - @JsonProperty("Denominazione") - private String businessName; + @JsonProperty("Denominazione") + private String businessName; - @JsonProperty("NaturaGiuridica") - private String legalNature; + @JsonProperty("NaturaGiuridica") + private String legalNature; - @JsonProperty("DescNaturaGiuridica") - private String legalNatureDescription; + @JsonProperty("DescNaturaGiuridica") + private String legalNatureDescription; - @JsonProperty("Cciaa") - private String cciaa; + @JsonProperty("Cciaa") + private String cciaa; - @JsonProperty("NRea") - private String nRea; + @JsonProperty("NRea") + private String nRea; - @JsonProperty("StatoImpresa") - private String businessStatus; + @JsonProperty("StatoImpresa") + private String businessStatus; - @JsonProperty("IndirizzoSedeLegale") - private PDNDSedeImpresa businessAddress; + @JsonProperty("IndirizzoSedeLegale") + private PDNDSedeImpresa businessAddress; - @JsonProperty("PEC") - private String digitalAddress; - - - public String getAddress() { - return businessAddress.getToponimoSede() + " " + businessAddress.getViaSede() + " " + businessAddress.getNcivicoSede(); - } + @JsonProperty("PEC") + private String digitalAddress; + public String getAddress() { + if (Objects.nonNull(businessAddress)) { + return businessAddress.getToponimoSede() + + " " + + businessAddress.getViaSede() + + " " + + businessAddress.getNcivicoSede(); + } else return ""; + } } diff --git a/connector/rest/src/main/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/model/PDNDSedeImpresa.java b/connector/rest/src/main/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/model/PDNDSedeImpresa.java index d5be7930..7cb69027 100644 --- a/connector/rest/src/main/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/model/PDNDSedeImpresa.java +++ b/connector/rest/src/main/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/model/PDNDSedeImpresa.java @@ -5,21 +5,21 @@ @Data public class PDNDSedeImpresa { - @JsonProperty("ComuneSede") - private String city; + @JsonProperty("ComuneSede") + private String city; - @JsonProperty("ProvinciaSede") - private String county; + @JsonProperty("ProvinciaSede") + private String county; - @JsonProperty("CapSede") - private String zipCode; + @JsonProperty("CapSede") + private String zipCode; - @JsonProperty("ToponimoSede") - private String toponimoSede; + @JsonProperty("ToponimoSede") + private String toponimoSede; - @JsonProperty("ViaSede") - private String viaSede; + @JsonProperty("ViaSede") + private String viaSede; - @JsonProperty("NcivicoSede") - private String ncivicoSede; + @JsonProperty("NcivicoSede") + private String ncivicoSede; } diff --git a/connector/rest/src/test/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/PDNDInfoCamereConnectorImplTest.java b/connector/rest/src/test/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/PDNDInfoCamereConnectorImplTest.java index d6f71b42..99aa0316 100644 --- a/connector/rest/src/test/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/PDNDInfoCamereConnectorImplTest.java +++ b/connector/rest/src/test/java/it/pagopa/selfcare/party/registry_proxy/connector/rest/PDNDInfoCamereConnectorImplTest.java @@ -1,7 +1,5 @@ package it.pagopa.selfcare.party.registry_proxy.connector.rest; -import it.pagopa.selfcare.party.registry_proxy.connector.exception.ResourceNotFoundException; -import it.pagopa.selfcare.party.registry_proxy.connector.exception.ServiceUnavailableException; import it.pagopa.selfcare.party.registry_proxy.connector.model.nationalregistriespdnd.PDNDBusiness; import it.pagopa.selfcare.party.registry_proxy.connector.rest.client.PDNDInfoCamereRestClient; import it.pagopa.selfcare.party.registry_proxy.connector.rest.config.PDNDInfoCamereRestClientConfig; @@ -10,7 +8,6 @@ import it.pagopa.selfcare.party.registry_proxy.connector.rest.model.PDNDSedeImpresa; import it.pagopa.selfcare.party.registry_proxy.connector.rest.model.mapper.PDNDBusinessMapper; import it.pagopa.selfcare.party.registry_proxy.connector.rest.service.TokenProvider; -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; @@ -28,202 +25,186 @@ @ExtendWith(SpringExtension.class) class PDNDInfoCamereConnectorImplTest { - @InjectMocks - private PDNDInfoCamereConnectorImpl pdndInfoCamereConnector; - @Mock - private PDNDInfoCamereRestClient pdndInfoCamereRestClient; - @Mock - private TokenProvider tokenProvider; - @Mock - private PDNDBusinessMapper pdndBusinessMapper; - @Mock - private PDNDInfoCamereRestClientConfig pdndInfoCamereRestClientConfig; - - - @Test - void testRetrieveInstitutionsByDescription() { - - //given - String description = "description"; - List pdndBusinesses = new ArrayList<>(); - pdndBusinesses.add(dummyPDNDBusiness()); - List pdndImpresaList = new ArrayList<>(); - pdndImpresaList.add(dummyPDNDImpresa()); - - mockPdndSecretValue(); - mockPdndToken(); - when(pdndInfoCamereRestClient.retrieveInstitutionsPdndByDescription(anyString(), anyString())).thenReturn(pdndImpresaList); - when(pdndBusinessMapper.toPDNDBusinesses(pdndImpresaList)).thenReturn(pdndBusinesses); - - //when - pdndBusinesses = pdndInfoCamereConnector.retrieveInstitutionsPdndByDescription(description); - - //then - assertNotNull(pdndBusinesses); - assertNotNull(pdndBusinesses.getClass()); - assertEquals(1, pdndBusinesses.size()); - PDNDBusiness pdndBusiness = pdndBusinesses.iterator().next(); - assertEquals(dummyPDNDImpresa().getBusinessTaxId(), pdndBusiness.getBusinessTaxId()); - assertEquals(dummyPDNDImpresa().getBusinessName(), pdndBusiness.getBusinessName()); - assertEquals(dummyPDNDImpresa().getBusinessStatus(), pdndBusiness.getBusinessStatus()); - assertEquals(dummyPDNDImpresa().getLegalNature(), pdndBusiness.getLegalNature()); - assertEquals(dummyPDNDImpresa().getLegalNatureDescription(), pdndBusiness.getLegalNatureDescription()); - assertEquals(dummyPDNDImpresa().getAddress(), pdndBusiness.getAddress()); - assertEquals(dummyPDNDImpresa().getDigitalAddress(), pdndBusiness.getDigitalAddress()); - assertEquals(dummyPDNDImpresa().getNRea(), pdndBusiness.getNRea()); - assertEquals(dummyPDNDImpresa().getCciaa(), pdndBusiness.getCciaa()); - assertEquals(dummyPDNDImpresa().getBusinessAddress().getCity(), pdndBusiness.getCity()); - assertEquals(dummyPDNDImpresa().getBusinessAddress().getCounty(), pdndBusiness.getCounty()); - assertEquals(dummyPDNDImpresa().getBusinessAddress().getZipCode(), pdndBusiness.getZipCode()); - - verify(pdndInfoCamereRestClient, times(1)) - .retrieveInstitutionsPdndByDescription(anyString(), anyString()); - verifyNoMoreInteractions(pdndInfoCamereRestClient); - - } - @Test - void testRetrieveInstitutionsByDescription_nullDescription() { - - //given - String description = null; - List pdndImpresaList = new ArrayList<>(); - pdndImpresaList.add(dummyPDNDImpresa()); - - when(pdndInfoCamereRestClient.retrieveInstitutionsPdndByDescription(anyString(), anyString())).thenReturn(pdndImpresaList); - - //when - Executable executable = () -> pdndInfoCamereConnector.retrieveInstitutionsPdndByDescription(description); - - //then - IllegalArgumentException e = assertThrows(IllegalArgumentException.class, executable); - assertEquals("Description is required", e.getMessage()); - Mockito.verifyNoInteractions(pdndInfoCamereRestClient); - - } - - @Test - void fallbackGetExtByDescriptionTest(){ - List list = pdndInfoCamereConnector.fallbackRetrieveInstitutionByDescription(new ServiceUnavailableException()); - Assertions.assertTrue(list.isEmpty()); - } - - @Test - void testRetrieveInstitutionByTaxCode() { - - //given - String taxCode = "taxCode"; - PDNDBusiness pdndBusiness = dummyPDNDBusiness(); - List pdndImpresaList = new ArrayList<>(); - pdndImpresaList.add(dummyPDNDImpresa()); - - mockPdndToken(); - mockPdndSecretValue(); - when(pdndInfoCamereRestClient.retrieveInstitutionPdndByTaxCode(anyString(), anyString())).thenReturn(pdndImpresaList); - when(pdndBusinessMapper.toPDNDBusiness(dummyPDNDImpresa())).thenReturn(pdndBusiness); - - //when - pdndBusiness = pdndInfoCamereConnector.retrieveInstitutionPdndByTaxCode(taxCode); - - //then - assertNotNull(pdndBusiness); - assertNotNull(pdndBusiness.getClass()); - assertEquals(1, pdndImpresaList.size()); - PDNDImpresa pdndImpresa = pdndImpresaList.iterator().next(); - assertEquals(pdndImpresa.getBusinessTaxId(), pdndBusiness.getBusinessTaxId()); - assertEquals(pdndImpresa.getBusinessName(), pdndBusiness.getBusinessName()); - assertEquals(pdndImpresa.getBusinessStatus(), pdndBusiness.getBusinessStatus()); - assertEquals(pdndImpresa.getLegalNature(), pdndBusiness.getLegalNature()); - assertEquals(pdndImpresa.getLegalNatureDescription(), pdndBusiness.getLegalNatureDescription()); - assertEquals(pdndImpresa.getAddress(), pdndBusiness.getAddress()); - assertEquals(pdndImpresa.getDigitalAddress(), pdndBusiness.getDigitalAddress()); - assertEquals(pdndImpresa.getNRea(), pdndBusiness.getNRea()); - assertEquals(pdndImpresa.getCciaa(), pdndBusiness.getCciaa()); - assertEquals(pdndImpresa.getBusinessAddress().getCity(), pdndBusiness.getCity()); - assertEquals(pdndImpresa.getBusinessAddress().getCounty(), pdndBusiness.getCounty()); - assertEquals(pdndImpresa.getBusinessAddress().getZipCode(), pdndBusiness.getZipCode()); - - verify(pdndInfoCamereRestClient, times(1)) - .retrieveInstitutionPdndByTaxCode(anyString(), anyString()); - verifyNoMoreInteractions(pdndInfoCamereRestClient); - - } - - @Test - void testRetrieveInstitutionByTaxCode_nullTaxCode() { - - //given - String taxCode = null; - List pdndImpresaList = new ArrayList<>(); - pdndImpresaList.add(dummyPDNDImpresa()); - - when(pdndInfoCamereRestClient.retrieveInstitutionPdndByTaxCode(anyString(), anyString())).thenReturn(pdndImpresaList); - - //when - Executable executable = () -> pdndInfoCamereConnector.retrieveInstitutionPdndByTaxCode(taxCode); - - //then - IllegalArgumentException e = assertThrows(IllegalArgumentException.class, executable); - assertEquals("TaxCode is required", e.getMessage()); - Mockito.verifyNoInteractions(pdndInfoCamereRestClient); - - } - - @Test - void fallbackGetExtByTaxCodeTest(){ - Assertions.assertThrows(ResourceNotFoundException.class, () -> pdndInfoCamereConnector.fallbackRetrieveInstitutionByTaxCode(new ServiceUnavailableException())); - } - - private PDNDBusiness dummyPDNDBusiness(){ - PDNDBusiness pdndBusiness = new PDNDBusiness(); - pdndBusiness.setBusinessTaxId("12345678901"); - pdndBusiness.setBusinessName("Dummy Business Name"); - pdndBusiness.setBusinessStatus("Active"); - pdndBusiness.setCity("Milano"); - pdndBusiness.setCciaa("MI123456"); - pdndBusiness.setAddress("Via Montenapoleone 10"); - pdndBusiness.setDigitalAddress("dummy@example.com"); - pdndBusiness.setCounty("MI"); - pdndBusiness.setLegalNature("LLC"); - pdndBusiness.setLegalNatureDescription("Limited Liability Company"); - pdndBusiness.setNRea("MI67890"); - pdndBusiness.setZipCode("20100"); - return pdndBusiness; - } - - public PDNDImpresa dummyPDNDImpresa() { - PDNDImpresa pdndImpresa = new PDNDImpresa(); - pdndImpresa.setBusinessTaxId("12345678901"); - pdndImpresa.setBusinessName("Dummy Business Name"); - pdndImpresa.setLegalNature("LLC"); - pdndImpresa.setLegalNatureDescription("Limited Liability Company"); - pdndImpresa.setCciaa("MI123456"); - pdndImpresa.setNRea("MI67890"); - pdndImpresa.setBusinessStatus("Active"); - pdndImpresa.setBusinessAddress(dummyPDNDSedeImpresa()); - pdndImpresa.setDigitalAddress("dummy@example.com"); - return pdndImpresa; - } - - public PDNDSedeImpresa dummyPDNDSedeImpresa() { - PDNDSedeImpresa pdndSedeImpresa = new PDNDSedeImpresa(); - pdndSedeImpresa.setToponimoSede("Via"); - pdndSedeImpresa.setViaSede("Montenapoleone"); - pdndSedeImpresa.setNcivicoSede("10"); - pdndSedeImpresa.setZipCode("20100"); - pdndSedeImpresa.setCity("Milano"); - pdndSedeImpresa.setCounty("MI"); - return pdndSedeImpresa; - } - - private void mockPdndToken() { - ClientCredentialsResponse clientCredentialsResponse = new ClientCredentialsResponse(); - clientCredentialsResponse.setAccessToken("accessToken"); - when(tokenProvider.getTokenPdnd(any())).thenReturn(clientCredentialsResponse); - } - - private void mockPdndSecretValue() { - when(pdndInfoCamereRestClientConfig.getPdndSecretValue()).thenReturn(null); - } - + @InjectMocks private PDNDInfoCamereConnectorImpl pdndInfoCamereConnector; + @Mock private PDNDInfoCamereRestClient pdndInfoCamereRestClient; + @Mock private TokenProvider tokenProvider; + @Mock private PDNDBusinessMapper pdndBusinessMapper; + @Mock private PDNDInfoCamereRestClientConfig pdndInfoCamereRestClientConfig; + + @Test + void testRetrieveInstitutionsByDescription() { + + // given + String description = "description"; + List pdndBusinesses = new ArrayList<>(); + pdndBusinesses.add(dummyPDNDBusiness()); + List pdndImpresaList = new ArrayList<>(); + pdndImpresaList.add(dummyPDNDImpresa()); + + mockPdndSecretValue(); + mockPdndToken(); + when(pdndInfoCamereRestClient.retrieveInstitutionsPdndByDescription(anyString(), anyString())) + .thenReturn(pdndImpresaList); + when(pdndBusinessMapper.toPDNDBusinesses(pdndImpresaList)).thenReturn(pdndBusinesses); + + // when + pdndBusinesses = pdndInfoCamereConnector.retrieveInstitutionsPdndByDescription(description); + + // then + assertNotNull(pdndBusinesses); + assertNotNull(pdndBusinesses.getClass()); + assertEquals(1, pdndBusinesses.size()); + PDNDBusiness pdndBusiness = pdndBusinesses.iterator().next(); + assertEquals(dummyPDNDImpresa().getBusinessTaxId(), pdndBusiness.getBusinessTaxId()); + assertEquals(dummyPDNDImpresa().getBusinessName(), pdndBusiness.getBusinessName()); + assertEquals(dummyPDNDImpresa().getBusinessStatus(), pdndBusiness.getBusinessStatus()); + assertEquals(dummyPDNDImpresa().getLegalNature(), pdndBusiness.getLegalNature()); + assertEquals( + dummyPDNDImpresa().getLegalNatureDescription(), pdndBusiness.getLegalNatureDescription()); + assertEquals(dummyPDNDImpresa().getAddress(), pdndBusiness.getAddress()); + assertEquals(dummyPDNDImpresa().getDigitalAddress(), pdndBusiness.getDigitalAddress()); + assertEquals(dummyPDNDImpresa().getNRea(), pdndBusiness.getNRea()); + assertEquals(dummyPDNDImpresa().getCciaa(), pdndBusiness.getCciaa()); + assertEquals(dummyPDNDImpresa().getBusinessAddress().getCity(), pdndBusiness.getCity()); + assertEquals(dummyPDNDImpresa().getBusinessAddress().getCounty(), pdndBusiness.getCounty()); + assertEquals(dummyPDNDImpresa().getBusinessAddress().getZipCode(), pdndBusiness.getZipCode()); + + verify(pdndInfoCamereRestClient, times(1)) + .retrieveInstitutionsPdndByDescription(anyString(), anyString()); + verifyNoMoreInteractions(pdndInfoCamereRestClient); + } + + @Test + void testRetrieveInstitutionsByDescription_nullDescription() { + + // given + String description = null; + List pdndImpresaList = new ArrayList<>(); + pdndImpresaList.add(dummyPDNDImpresa()); + + when(pdndInfoCamereRestClient.retrieveInstitutionsPdndByDescription(anyString(), anyString())) + .thenReturn(pdndImpresaList); + + // when + Executable executable = + () -> pdndInfoCamereConnector.retrieveInstitutionsPdndByDescription(description); + + // then + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, executable); + assertEquals("Description is required", e.getMessage()); + Mockito.verifyNoInteractions(pdndInfoCamereRestClient); + } + + @Test + void testRetrieveInstitutionByTaxCode() { + + // given + String taxCode = "taxCode"; + PDNDBusiness pdndBusiness = dummyPDNDBusiness(); + List pdndImpresaList = new ArrayList<>(); + pdndImpresaList.add(dummyPDNDImpresa()); + + mockPdndToken(); + mockPdndSecretValue(); + when(pdndInfoCamereRestClient.retrieveInstitutionPdndByTaxCode(anyString(), anyString())) + .thenReturn(pdndImpresaList); + when(pdndBusinessMapper.toPDNDBusiness(dummyPDNDImpresa())).thenReturn(pdndBusiness); + + // when + pdndBusiness = pdndInfoCamereConnector.retrieveInstitutionPdndByTaxCode(taxCode); + + // then + assertNotNull(pdndBusiness); + assertNotNull(pdndBusiness.getClass()); + assertEquals(1, pdndImpresaList.size()); + PDNDImpresa pdndImpresa = pdndImpresaList.iterator().next(); + assertEquals(pdndImpresa.getBusinessTaxId(), pdndBusiness.getBusinessTaxId()); + assertEquals(pdndImpresa.getBusinessName(), pdndBusiness.getBusinessName()); + assertEquals(pdndImpresa.getBusinessStatus(), pdndBusiness.getBusinessStatus()); + assertEquals(pdndImpresa.getLegalNature(), pdndBusiness.getLegalNature()); + assertEquals(pdndImpresa.getLegalNatureDescription(), pdndBusiness.getLegalNatureDescription()); + assertEquals(pdndImpresa.getAddress(), pdndBusiness.getAddress()); + assertEquals(pdndImpresa.getDigitalAddress(), pdndBusiness.getDigitalAddress()); + assertEquals(pdndImpresa.getNRea(), pdndBusiness.getNRea()); + assertEquals(pdndImpresa.getCciaa(), pdndBusiness.getCciaa()); + assertEquals(pdndImpresa.getBusinessAddress().getCity(), pdndBusiness.getCity()); + assertEquals(pdndImpresa.getBusinessAddress().getCounty(), pdndBusiness.getCounty()); + assertEquals(pdndImpresa.getBusinessAddress().getZipCode(), pdndBusiness.getZipCode()); + + verify(pdndInfoCamereRestClient, times(1)) + .retrieveInstitutionPdndByTaxCode(anyString(), anyString()); + verifyNoMoreInteractions(pdndInfoCamereRestClient); + } + + @Test + void testRetrieveInstitutionByTaxCode_nullTaxCode() { + + // given + String taxCode = null; + List pdndImpresaList = new ArrayList<>(); + pdndImpresaList.add(dummyPDNDImpresa()); + + when(pdndInfoCamereRestClient.retrieveInstitutionPdndByTaxCode(anyString(), anyString())) + .thenReturn(pdndImpresaList); + + // when + Executable executable = () -> pdndInfoCamereConnector.retrieveInstitutionPdndByTaxCode(taxCode); + + // then + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, executable); + assertEquals("TaxCode is required", e.getMessage()); + Mockito.verifyNoInteractions(pdndInfoCamereRestClient); + } + + private PDNDBusiness dummyPDNDBusiness() { + PDNDBusiness pdndBusiness = new PDNDBusiness(); + pdndBusiness.setBusinessTaxId("12345678901"); + pdndBusiness.setBusinessName("Dummy Business Name"); + pdndBusiness.setBusinessStatus("Active"); + pdndBusiness.setCity("Milano"); + pdndBusiness.setCciaa("MI123456"); + pdndBusiness.setAddress("Via Montenapoleone 10"); + pdndBusiness.setDigitalAddress("dummy@example.com"); + pdndBusiness.setCounty("MI"); + pdndBusiness.setLegalNature("LLC"); + pdndBusiness.setLegalNatureDescription("Limited Liability Company"); + pdndBusiness.setNRea("MI67890"); + pdndBusiness.setZipCode("20100"); + return pdndBusiness; + } + + public PDNDImpresa dummyPDNDImpresa() { + PDNDImpresa pdndImpresa = new PDNDImpresa(); + pdndImpresa.setBusinessTaxId("12345678901"); + pdndImpresa.setBusinessName("Dummy Business Name"); + pdndImpresa.setLegalNature("LLC"); + pdndImpresa.setLegalNatureDescription("Limited Liability Company"); + pdndImpresa.setCciaa("MI123456"); + pdndImpresa.setNRea("MI67890"); + pdndImpresa.setBusinessStatus("Active"); + pdndImpresa.setBusinessAddress(dummyPDNDSedeImpresa()); + pdndImpresa.setDigitalAddress("dummy@example.com"); + return pdndImpresa; + } + + public PDNDSedeImpresa dummyPDNDSedeImpresa() { + PDNDSedeImpresa pdndSedeImpresa = new PDNDSedeImpresa(); + pdndSedeImpresa.setToponimoSede("Via"); + pdndSedeImpresa.setViaSede("Montenapoleone"); + pdndSedeImpresa.setNcivicoSede("10"); + pdndSedeImpresa.setZipCode("20100"); + pdndSedeImpresa.setCity("Milano"); + pdndSedeImpresa.setCounty("MI"); + return pdndSedeImpresa; + } + + private void mockPdndToken() { + ClientCredentialsResponse clientCredentialsResponse = new ClientCredentialsResponse(); + clientCredentialsResponse.setAccessToken("accessToken"); + when(tokenProvider.getTokenPdnd(any())).thenReturn(clientCredentialsResponse); + } + + private void mockPdndSecretValue() { + when(pdndInfoCamereRestClientConfig.getPdndSecretValue()).thenReturn(null); + } } -