From 32302b1047e08c55d7c8b7b2e55e874116b8af9f Mon Sep 17 00:00:00 2001 From: Giulia Tremolada Date: Wed, 3 Jul 2024 16:09:49 +0200 Subject: [PATCH 1/3] add product pagoPa condition in createDelegation --- .../connector/api/MsCoreConnector.java | 2 ++ .../connector/rest/CoreConnectorImpl.java | 15 +++++++++ .../connector/rest/CoreConnectorImplTest.java | 28 ++++++++++++++++- .../dashboard/core/DelegationServiceImpl.java | 27 ++++++++++++++++ .../core/DelegationServiceImplTest.java | 31 +++++++++++++++++++ .../expectations/DelegationRequestPagoPa.json | 9 ++++++ 6 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 core/src/test/resources/expectations/DelegationRequestPagoPa.json diff --git a/connector-api/src/main/java/it/pagopa/selfcare/dashboard/connector/api/MsCoreConnector.java b/connector-api/src/main/java/it/pagopa/selfcare/dashboard/connector/api/MsCoreConnector.java index de6c9baae..7e2dbb185 100644 --- a/connector-api/src/main/java/it/pagopa/selfcare/dashboard/connector/api/MsCoreConnector.java +++ b/connector-api/src/main/java/it/pagopa/selfcare/dashboard/connector/api/MsCoreConnector.java @@ -16,6 +16,8 @@ public interface MsCoreConnector { Institution getInstitution(String institutionId); + List getInstitutionsFromTaxCode(String taxCode, String subunitCode, String origin, String originId); + Institution updateInstitutionDescription(String institutionId, UpdateInstitutionResource updatePnPGInstitutionResource); DelegationId createDelegation(DelegationRequest delegation); diff --git a/connector/rest/src/main/java/it/pagopa/selfcare/dashboard/connector/rest/CoreConnectorImpl.java b/connector/rest/src/main/java/it/pagopa/selfcare/dashboard/connector/rest/CoreConnectorImpl.java index d58469db5..493eabb50 100644 --- a/connector/rest/src/main/java/it/pagopa/selfcare/dashboard/connector/rest/CoreConnectorImpl.java +++ b/connector/rest/src/main/java/it/pagopa/selfcare/dashboard/connector/rest/CoreConnectorImpl.java @@ -51,6 +51,8 @@ class CoreConnectorImpl implements MsCoreConnector { private final DelegationRestClientMapper delegationMapper; static final String REQUIRED_INSTITUTION_ID_MESSAGE = "An Institution id is required"; + static final String REQUIRED_TAX_CODE_MESSAGE = "A taxCode is required"; + static final String NO_INSTITUTION_FOUND = "No institution found with given taxCode"; static final String REQUIRED_PRODUCT_ID_MESSAGE = "A Product id is required"; static final String REQUIRED_INSTITUTION_TYPE_MESSAGE = "An Institution type is required"; static final String REQUIRED_UPDATE_RESOURCE_MESSAGE = "An Institution description is required"; @@ -80,6 +82,19 @@ public Institution updateInstitutionDescription(String institutionId, UpdateInst return institutionMapper.toInstitution(institution); } + @Override + @Retry(name = "retryTimeout") + public List getInstitutionsFromTaxCode(String taxCode, String subunitCode, String origin, String originId) { + log.trace("getInstitution start"); + log.debug("getInstitution institutionId = {}", taxCode); + Assert.hasText(taxCode, REQUIRED_TAX_CODE_MESSAGE); + InstitutionsResponse institutions = coreInstitutionApiRestClient._getInstitutionsUsingGET(taxCode, subunitCode, origin, originId).getBody(); + log.debug("getInstitution result = {}", institutions); + log.trace("getInstitution end"); + Assert.notNull(institutions, NO_INSTITUTION_FOUND); + return institutions.getInstitutions().stream().map(institutionMapper::toInstitution).toList(); + } + @Override public DelegationId createDelegation(DelegationRequest delegation) { log.trace("createDelegation start"); diff --git a/connector/rest/src/test/java/it/pagopa/selfcare/dashboard/connector/rest/CoreConnectorImplTest.java b/connector/rest/src/test/java/it/pagopa/selfcare/dashboard/connector/rest/CoreConnectorImplTest.java index 9cb38aaaa..cc178bc20 100644 --- a/connector/rest/src/test/java/it/pagopa/selfcare/dashboard/connector/rest/CoreConnectorImplTest.java +++ b/connector/rest/src/test/java/it/pagopa/selfcare/dashboard/connector/rest/CoreConnectorImplTest.java @@ -9,7 +9,10 @@ import it.pagopa.selfcare.dashboard.connector.model.delegation.DelegationId; import it.pagopa.selfcare.dashboard.connector.model.delegation.DelegationWithPagination; import it.pagopa.selfcare.dashboard.connector.model.delegation.GetDelegationParameters; -import it.pagopa.selfcare.dashboard.connector.model.institution.*; +import it.pagopa.selfcare.dashboard.connector.model.institution.GeographicTaxonomy; +import it.pagopa.selfcare.dashboard.connector.model.institution.GeographicTaxonomyList; +import it.pagopa.selfcare.dashboard.connector.model.institution.Institution; +import it.pagopa.selfcare.dashboard.connector.model.institution.UpdateInstitutionResource; import it.pagopa.selfcare.dashboard.connector.model.product.PartyProduct; import it.pagopa.selfcare.dashboard.connector.model.user.UserInfo; import it.pagopa.selfcare.dashboard.connector.rest.client.CoreDelegationApiRestClient; @@ -134,6 +137,29 @@ void getInstitution() throws IOException { ._retrieveInstitutionByIdUsingGET(institutionId); } + @Test + void getInstitutionsFromTaxCode() throws IOException { + // given + String institutionId = "institutionId"; + ClassPathResource resource = new ClassPathResource("stubs/InstitutionResponse.json"); + byte[] resourceStream = Files.readAllBytes(resource.getFile().toPath()); + InstitutionResponse institutionMock = objectMapper.readValue(resourceStream, new TypeReference<>() { + }); + + InstitutionsResponse institutionsResponse = new InstitutionsResponse(); + institutionsResponse.setInstitutions(List.of(institutionMock)); + + when(coreInstitutionApiRestClient._getInstitutionsUsingGET(institutionMock.getTaxCode(), null, null, null)) + .thenReturn(ResponseEntity.of(Optional.of(institutionsResponse))); + // when + List institutions = msCoreConnector.getInstitutionsFromTaxCode(institutionMock.getTaxCode(), null, null, null); + // then + + assertEquals(institutions.get(0), institutionMapperSpy.toInstitution(institutionMock)); + verify(coreInstitutionApiRestClient, times(1)) + ._getInstitutionsUsingGET(institutionMock.getTaxCode(), null, null, null); + } + @Test void updateInstitutionDescription() throws IOException { // given diff --git a/core/src/main/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImpl.java b/core/src/main/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImpl.java index 4e2dc4e1b..d698710e8 100644 --- a/core/src/main/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImpl.java +++ b/core/src/main/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImpl.java @@ -1,18 +1,24 @@ package it.pagopa.selfcare.dashboard.core; import it.pagopa.selfcare.commons.base.logging.LogUtils; +import it.pagopa.selfcare.commons.base.utils.InstitutionType; import it.pagopa.selfcare.dashboard.connector.api.MsCoreConnector; +import it.pagopa.selfcare.dashboard.connector.exception.ResourceNotFoundException; import it.pagopa.selfcare.dashboard.connector.model.delegation.*; +import it.pagopa.selfcare.dashboard.connector.model.institution.Institution; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; +import static it.pagopa.selfcare.onboarding.common.ProductId.PROD_PAGOPA; + @Slf4j @Service class DelegationServiceImpl implements DelegationService { + private static final String INSTITUTION_TAX_CODE_NOT_FOUND = "Cannot find Institution using taxCode %s"; private final MsCoreConnector msCoreConnector; @@ -25,12 +31,33 @@ public DelegationServiceImpl(MsCoreConnector msCoreConnector) { public DelegationId createDelegation(DelegationRequest delegation) { log.trace("createDelegation start"); log.debug(LogUtils.CONFIDENTIAL_MARKER, "createDelegation request = {}", delegation); + + /* + In case of prod-pagopa product, in the attribute "to" of the delegation object a taxCode is inserted. + So we have to retrieve the institutionId from the taxCode and set it in the "to" attribute. + */ + if(PROD_PAGOPA.getValue().equals(delegation.getProductId())) { + setToInstitutionId(delegation); + } + DelegationId result = msCoreConnector.createDelegation(delegation); log.debug("createDelegation result = {}", result); log.trace("createDelegation end"); return result; } + private void setToInstitutionId(DelegationRequest delegation) { + List institutions = msCoreConnector.getInstitutionsFromTaxCode(delegation.getTo(), null, null, null); + Institution partner = institutions.stream() + .filter(institution -> institution.getInstitutionType() == InstitutionType.PT) + .findFirst() + .orElse(institutions.stream().findFirst() + .orElseThrow(() -> new ResourceNotFoundException( + String.format(INSTITUTION_TAX_CODE_NOT_FOUND, delegation.getTo())) + )); + delegation.setTo(partner.getId()); + } + @Override public List getDelegations(GetDelegationParameters delegationParameters) { log.trace("getDelegations start"); diff --git a/core/src/test/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImplTest.java b/core/src/test/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImplTest.java index 80da07b2a..252baf0e2 100644 --- a/core/src/test/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImplTest.java +++ b/core/src/test/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImplTest.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import it.pagopa.selfcare.dashboard.connector.api.MsCoreConnector; import it.pagopa.selfcare.dashboard.connector.model.delegation.*; +import it.pagopa.selfcare.dashboard.connector.model.institution.Institution; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -50,6 +51,36 @@ void testCreateDelegation() throws IOException { .createDelegation(delegation); } + + @Test + void testCreateDelegationPagoPa() throws IOException { + + ClassPathResource pathResource = new ClassPathResource("expectations/DelegationRequestPagoPa.json"); + byte[] resourceStream = Files.readAllBytes(pathResource.getFile().toPath()); + DelegationRequest delegation = objectMapper.readValue(resourceStream, new TypeReference<>() { + }); + + DelegationRequest delegationTaxCode = objectMapper.readValue(resourceStream, new TypeReference<>() { + }); + + ClassPathResource pathResourceInstitution = new ClassPathResource("expectations/Institution.json"); + byte[] resourceStreamInstitution = Files.readAllBytes(pathResourceInstitution.getFile().toPath()); + Institution institution = objectMapper.readValue(resourceStreamInstitution, new TypeReference<>() { + }); + + DelegationId delegationId = new DelegationId(); + delegationId.setId("id"); + when(msCoreConnector.getInstitutionsFromTaxCode(delegationTaxCode.getTo(), null, null, null)).thenReturn(List.of(institution)); + when(msCoreConnector.createDelegation(delegation)).thenReturn(delegationId); + + DelegationId response = delegationServiceImpl.createDelegation(delegation); + Assertions.assertEquals(delegation.getId(), response.getId()); + Mockito.verify(msCoreConnector, Mockito.times(1)) + .getInstitutionsFromTaxCode(delegationTaxCode.getTo(), null, null, null); + Mockito.verify(msCoreConnector, Mockito.times(1)) + .createDelegation(delegation); + } + @Test void getDelegations() throws IOException { diff --git a/core/src/test/resources/expectations/DelegationRequestPagoPa.json b/core/src/test/resources/expectations/DelegationRequestPagoPa.json new file mode 100644 index 000000000..dbd36dc02 --- /dev/null +++ b/core/src/test/resources/expectations/DelegationRequestPagoPa.json @@ -0,0 +1,9 @@ +{ + "id": "id", + "from": "john.doe@example.com", + "to": "jane.smith@example.com", + "productId": "prod-pagopa", + "institutionFromName": "Institution A", + "institutionToName": "Institution B", + "type": "AOO" +} From 44889ab0dcc6ddb0b8964642481f57331b10b17f Mon Sep 17 00:00:00 2001 From: Giulia Tremolada Date: Thu, 4 Jul 2024 09:31:16 +0200 Subject: [PATCH 2/3] add unit test --- .../dashboard/core/DelegationServiceImplTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/src/test/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImplTest.java b/core/src/test/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImplTest.java index 252baf0e2..f8ff16358 100644 --- a/core/src/test/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImplTest.java +++ b/core/src/test/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImplTest.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import it.pagopa.selfcare.dashboard.connector.api.MsCoreConnector; +import it.pagopa.selfcare.dashboard.connector.exception.ResourceNotFoundException; import it.pagopa.selfcare.dashboard.connector.model.delegation.*; import it.pagopa.selfcare.dashboard.connector.model.institution.Institution; import org.junit.jupiter.api.Assertions; @@ -18,6 +19,8 @@ import java.nio.file.Files; import java.util.List; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) @@ -81,6 +84,14 @@ void testCreateDelegationPagoPa() throws IOException { .createDelegation(delegation); } + @Test + void testCreateDelegationWithResourceNotFoundException() { + DelegationRequest delegationPagoPa = new DelegationRequest(); + delegationPagoPa.setProductId("prod-pagopa"); + when(msCoreConnector.getInstitutionsFromTaxCode(any(), any(), any(), any())).thenReturn(List.of()); + assertThrows(ResourceNotFoundException.class, () -> delegationServiceImpl.createDelegation(delegationPagoPa)); + } + @Test void getDelegations() throws IOException { From b6732ec039d6737a0e1af56251a81273826cee46 Mon Sep 17 00:00:00 2001 From: Giulia Tremolada Date: Thu, 4 Jul 2024 09:31:54 +0200 Subject: [PATCH 3/3] add unit tests --- .../selfcare/dashboard/core/DelegationServiceImplTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/test/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImplTest.java b/core/src/test/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImplTest.java index f8ff16358..fca9a1dcb 100644 --- a/core/src/test/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImplTest.java +++ b/core/src/test/java/it/pagopa/selfcare/dashboard/core/DelegationServiceImplTest.java @@ -19,6 +19,7 @@ import java.nio.file.Files; import java.util.List; +import static it.pagopa.selfcare.onboarding.common.ProductId.PROD_PAGOPA; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @@ -87,7 +88,7 @@ void testCreateDelegationPagoPa() throws IOException { @Test void testCreateDelegationWithResourceNotFoundException() { DelegationRequest delegationPagoPa = new DelegationRequest(); - delegationPagoPa.setProductId("prod-pagopa"); + delegationPagoPa.setProductId(PROD_PAGOPA.getValue()); when(msCoreConnector.getInstitutionsFromTaxCode(any(), any(), any(), any())).thenReturn(List.of()); assertThrows(ResourceNotFoundException.class, () -> delegationServiceImpl.createDelegation(delegationPagoPa)); }