Skip to content

Commit

Permalink
[SELC-5224] feat: add product pagoPa condition in createDelegation (#462
Browse files Browse the repository at this point in the history
)
  • Loading branch information
giulia-tremolada authored Jul 4, 2024
1 parent 95764c4 commit b6fa11e
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public interface MsCoreConnector {

Institution getInstitution(String institutionId);

List<Institution> getInstitutionsFromTaxCode(String taxCode, String subunitCode, String origin, String originId);

Institution updateInstitutionDescription(String institutionId, UpdateInstitutionResource updatePnPGInstitutionResource);

DelegationId createDelegation(DelegationRequest delegation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -80,6 +82,19 @@ public Institution updateInstitutionDescription(String institutionId, UpdateInst
return institutionMapper.toInstitution(institution);
}

@Override
@Retry(name = "retryTimeout")
public List<Institution> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Institution> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;


Expand All @@ -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<Institution> 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<Delegation> getDelegations(GetDelegationParameters delegationParameters) {
log.trace("getDelegations start");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -17,6 +19,9 @@
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;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -50,6 +55,44 @@ 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 testCreateDelegationWithResourceNotFoundException() {
DelegationRequest delegationPagoPa = new DelegationRequest();
delegationPagoPa.setProductId(PROD_PAGOPA.getValue());
when(msCoreConnector.getInstitutionsFromTaxCode(any(), any(), any(), any())).thenReturn(List.of());
assertThrows(ResourceNotFoundException.class, () -> delegationServiceImpl.createDelegation(delegationPagoPa));
}

@Test
void getDelegations() throws IOException {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "id",
"from": "[email protected]",
"to": "[email protected]",
"productId": "prod-pagopa",
"institutionFromName": "Institution A",
"institutionToName": "Institution B",
"type": "AOO"
}

0 comments on commit b6fa11e

Please sign in to comment.