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

Commit

Permalink
[SELC-5242] feat: add condition to sendMailForDelegation (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
giulia-tremolada authored Jul 8, 2024
1 parent bd592f5 commit 1b3d8fe
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import it.pagopa.selfcare.mscore.api.DelegationConnector;
import it.pagopa.selfcare.mscore.constant.CustomError;
import it.pagopa.selfcare.mscore.constant.DelegationState;
import it.pagopa.selfcare.mscore.constant.DelegationType;
import it.pagopa.selfcare.mscore.constant.Order;
import it.pagopa.selfcare.mscore.exception.MsCoreException;
import it.pagopa.selfcare.mscore.exception.ResourceConflictException;
Expand Down Expand Up @@ -45,10 +46,12 @@ public Delegation createDelegation(Delegation delegation) {

Delegation savedDelegation = checkIfExistsAndSaveDelegation(delegation);

try {
notificationService.sendMailForDelegation(delegation.getInstitutionFromName(), delegation.getProductId(), delegation.getTo());
} catch (Exception e) {
log.error(SEND_MAIL_FOR_DELEGATION_ERROR.getMessage() + ":", e.getMessage(), e);
if(delegation.getType().equals(DelegationType.PT)) {
try {
notificationService.sendMailForDelegation(delegation.getInstitutionFromName(), delegation.getProductId(), delegation.getTo());
} catch (Exception e) {
log.error(SEND_MAIL_FOR_DELEGATION_ERROR.getMessage() + ":", e.getMessage(), e);
}
}
return savedDelegation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import it.pagopa.selfcare.commons.base.utils.InstitutionType;
import it.pagopa.selfcare.mscore.api.DelegationConnector;
import it.pagopa.selfcare.mscore.constant.DelegationState;
import it.pagopa.selfcare.mscore.constant.DelegationType;
import it.pagopa.selfcare.mscore.constant.Order;
import it.pagopa.selfcare.mscore.exception.MsCoreException;
import it.pagopa.selfcare.mscore.exception.ResourceConflictException;
Expand Down Expand Up @@ -52,12 +53,14 @@ class DelegationServiceImplTest {
dummyDelegationProdIo.setTo("to");
dummyDelegationProdIo.setFrom("from");
dummyDelegationProdIo.setProductId("prod-io");
dummyDelegationProdIo.setType(DelegationType.PT);

dummyDelegationProdPagopa = new Delegation();
dummyDelegationProdPagopa.setId("id");
dummyDelegationProdPagopa.setProductId("prod-pagopa");
dummyDelegationProdPagopa.setTo("taxCodeTo");
dummyDelegationProdPagopa.setFrom("from");
dummyDelegationProdPagopa.setType(DelegationType.EA);

dummyDelegationTaxCode = new Delegation();
dummyDelegationTaxCode.setToTaxCode("taxCodeTo");
Expand Down Expand Up @@ -95,6 +98,35 @@ void testCreateDelegationWithProductProdIo() {
assertEquals(institutionFrom.getInstitutionType(), response.getInstitutionType());
}

/**
* Method under test: {@link DelegationServiceImpl#createDelegation(Delegation)}
*/
@Test
void testCreateDelegationEA() {
Institution institutionTo = new Institution();
institutionTo.setId("idTo");
institutionTo.setTaxCode("taxCodeTo");
institutionTo.setInstitutionType(InstitutionType.PA);
Institution institutionFrom = new Institution();
institutionTo.setId("idFrom");
institutionTo.setTaxCode("taxCodeFrom");
institutionTo.setInstitutionType(InstitutionType.PT);
when(delegationConnector.save(dummyDelegationProdPagopa)).thenAnswer(arg ->arg.getArguments()[0]);
when(institutionService.retrieveInstitutionById(dummyDelegationProdPagopa.getFrom())).thenReturn(institutionFrom);
when(institutionService.retrieveInstitutionById(dummyDelegationProdPagopa.getTo())).thenReturn(institutionTo);
doNothing().when(institutionService).updateInstitutionDelegation(any(),anyBoolean());
Delegation response = delegationServiceImpl.createDelegation(dummyDelegationProdPagopa);
verify(delegationConnector).save(dummyDelegationProdPagopa);
verifyNoInteractions(mailNotificationService);
assertNotNull(response);
assertNotNull(response.getId());
assertEquals(dummyDelegationProdPagopa.getId(), response.getId());
assertEquals(institutionTo.getTaxCode(), response.getToTaxCode());
assertEquals(institutionFrom.getTaxCode(), response.getFromTaxCode());
assertEquals(institutionTo.getInstitutionType(), response.getBrokerType());
assertEquals(institutionFrom.getInstitutionType(), response.getInstitutionType());
}

/**
* Method under test: {@link DelegationServiceImpl#createDelegation(Delegation)}
*/
Expand All @@ -106,7 +138,7 @@ void testCreateDelegationWithSendMailError() {
doThrow(new MsCoreException(SEND_MAIL_FOR_DELEGATION_ERROR.getMessage(), SEND_MAIL_FOR_DELEGATION_ERROR.getCode()))
.when(mailNotificationService)
.sendMailForDelegation(any(), any(), any());
assertDoesNotThrow(() -> delegationServiceImpl.createDelegation(dummyDelegationProdPagopa));
assertDoesNotThrow(() -> delegationServiceImpl.createDelegation(dummyDelegationProdIo));
verify(mailNotificationService).sendMailForDelegation(any(), any(), any());
}

Expand Down

0 comments on commit 1b3d8fe

Please sign in to comment.