Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

[SELC-5223] feat: remove product pagoPa condition in createDelegation #522

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.pagopa.selfcare.mscore.core;

import it.pagopa.selfcare.commons.base.utils.InstitutionType;
import it.pagopa.selfcare.mscore.api.DelegationConnector;
import it.pagopa.selfcare.mscore.constant.CustomError;
import it.pagopa.selfcare.mscore.constant.DelegationState;
Expand Down Expand Up @@ -30,7 +29,6 @@ public class DelegationServiceImpl implements DelegationService {
private final DelegationConnector delegationConnector;
private final MailNotificationService notificationService;
private final InstitutionService institutionService;
private static final String PROD_PAGOPA = "prod-pagopa";

public DelegationServiceImpl(DelegationConnector delegationConnector,
MailNotificationService notificationService,
Expand All @@ -42,17 +40,11 @@ public DelegationServiceImpl(DelegationConnector delegationConnector,

@Override
public Delegation createDelegation(Delegation 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.equals(delegation.getProductId())) {
setPartnerByInstitutionTaxCode(delegation);
} else {
setTaxCodesByInstitutionIds(delegation);
}

setTaxCodesByInstitutionIds(delegation);

Delegation savedDelegation = checkIfExistsAndSaveDelegation(delegation);

try {
notificationService.sendMailForDelegation(delegation.getInstitutionFromName(), delegation.getProductId(), delegation.getTo());
} catch (Exception e) {
Expand Down Expand Up @@ -84,33 +76,6 @@ private Delegation checkIfExistsAndSaveDelegation(Delegation delegation) {
return savedDelegation;
}

private void setPartnerByInstitutionTaxCode(Delegation delegation) {
/*
In case the api returns more institutions we always try to take the PT,
otherwise it is okay to take the first one
It is caused by this issue https://pagopa.atlassian.net/wiki/spaces/SCP/pages/1058832442/RFC+-+Gestione+di+pi+institutionType+su+una+Institution
*/
List<Institution> institutionsTo = institutionService.getInstitutions(delegation.getTo(), null);
Institution partner = institutionsTo.stream()
.filter(institution -> institution.getInstitutionType() == InstitutionType.PT)
.findFirst()
.orElse(institutionsTo.stream().findFirst()
.orElseThrow(() -> new ResourceNotFoundException(
String.format(INSTITUTION_TAX_CODE_NOT_FOUND.getMessage(), delegation.getTo()),
INSTITUTION_TAX_CODE_NOT_FOUND.getCode())
));
delegation.setToTaxCode(partner.getTaxCode());
delegation.setBrokerType(partner.getInstitutionType());
delegation.setTo(partner.getId());

/*
Retrieve delegator's institution to set taxCodeFrom
*/
Institution institutionFrom = institutionService.retrieveInstitutionById(delegation.getFrom());
delegation.setFromTaxCode(institutionFrom.getTaxCode());
delegation.setInstitutionType(institutionFrom.getInstitutionType());
}

private void setTaxCodesByInstitutionIds(Delegation delegation){
/*
Retrieve both delegator's and partner's institutions to set taxCodeFrom and taxCodeTo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,65 +95,13 @@ void testCreateDelegationWithProductProdIo() {
assertEquals(institutionFrom.getInstitutionType(), response.getInstitutionType());
}

/**
* Method under test: {@link DelegationServiceImpl#createDelegation(Delegation)}
*/
@Test
void testCreateDelegationForPTWithProductPagopa() {
Institution institutionTo = new Institution();
institutionTo.setId("id");
institutionTo.setInstitutionType(InstitutionType.PT);
institutionTo.setTaxCode("taxCodeTo");
Institution institutionFrom = new Institution();
institutionFrom.setId("id");
institutionFrom.setTaxCode("taxCodeFrom");
when(delegationConnector.save(dummyDelegationProdPagopa)).thenAnswer(arg ->arg.getArguments()[0]);
doNothing().when(mailNotificationService).sendMailForDelegation(any(), any(), any());
when(institutionService.getInstitutions(dummyDelegationProdPagopa.getTo(), null)).thenReturn(List.of(institutionTo));
doNothing().when(institutionService).updateInstitutionDelegation(any(),anyBoolean());
when(institutionService.retrieveInstitutionById(dummyDelegationProdPagopa.getFrom())).thenReturn(institutionFrom);
Delegation response = delegationServiceImpl.createDelegation(dummyDelegationProdPagopa);
verify(institutionService).getInstitutions(any(), any());
verify(delegationConnector).save(any());
assertNotNull(response);
assertNotNull(response.getId());
assertEquals(dummyDelegationProdPagopa.getId(), response.getId());
}

/**
* Method under test: {@link DelegationServiceImpl#createDelegation(Delegation)}
*/
@Test
void testCreateDelegationForEcWithProductPagopa() {
Institution institutionTo = new Institution();
institutionTo.setId("to");
institutionTo.setInstitutionType(InstitutionType.PT);
institutionTo.setTaxCode("taxCodeTo");
Institution institutionFrom = new Institution();
institutionFrom.setId("from");
institutionFrom.setTaxCode("taxCodeFrom");
when(delegationConnector.findAndActivate(institutionFrom.getId(), institutionTo.getId(), dummyDelegationProdPagopa.getProductId())).thenReturn(dummyDelegationProdPagopa);
doNothing().when(mailNotificationService).sendMailForDelegation(any(), any(), any());
when(institutionService.getInstitutions(dummyDelegationProdPagopa.getTo(), null)).thenReturn(List.of(institutionTo));
doNothing().when(institutionService).updateInstitutionDelegation(any(),anyBoolean());
when(delegationServiceImpl.checkIfExistsWithStatus(dummyDelegationProdPagopa, DelegationState.DELETED)).thenReturn(true);
when(institutionService.retrieveInstitutionById(dummyDelegationProdPagopa.getFrom())).thenReturn(institutionFrom);
Delegation response = delegationServiceImpl.createDelegation(dummyDelegationProdPagopa);
verify(delegationConnector).findAndActivate(institutionFrom.getId(), institutionTo.getId(), dummyDelegationProdPagopa.getProductId());
verify(institutionService).getInstitutions(any(), any());
assertNotNull(response);
assertNotNull(response.getId());
assertEquals(dummyDelegationProdPagopa.getId(), response.getId());
}

/**
* Method under test: {@link DelegationServiceImpl#createDelegation(Delegation)}
*/
@Test
void testCreateDelegationWithSendMailError() {
Institution institution = new Institution();
institution.setId("id");
when(institutionService.getInstitutions(any(), any())).thenReturn(List.of(institution));
when(institutionService.retrieveInstitutionById(any())).thenReturn(institution);
doThrow(new MsCoreException(SEND_MAIL_FOR_DELEGATION_ERROR.getMessage(), SEND_MAIL_FOR_DELEGATION_ERROR.getCode()))
.when(mailNotificationService)
Expand All @@ -175,15 +123,6 @@ void testCreateDelegationWithError() {
verify(delegationConnector).save(any());
}

/**
* Method under test: {@link DelegationServiceImpl#createDelegation(Delegation)}
*/
@Test
void testCreateDelegationWithResourceNotFoundException() {
when(institutionService.getInstitutions(any(), any())).thenReturn(List.of());
assertThrows(ResourceNotFoundException.class, () -> delegationServiceImpl.createDelegation(dummyDelegationProdPagopa));
}

/**
* Method under test: {@link DelegationServiceImpl#createDelegation(Delegation)}
*/
Expand Down
Loading