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

Commit

Permalink
fix: modify CreateDelegationFromTaxCode and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
giulia-tremolada committed Apr 15, 2024
1 parent 1ddf81f commit c1f427c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Delegation createDelegation(Delegation delegation) {
try {
if(checkIfExistsWithStatus(delegation, DelegationState.DELETED)){
savedDelegation = delegationConnector.findAndActivate(delegation.getFrom(), delegation.getTo(), delegation.getProductId());
} else{
} else {
delegation.setCreatedAt(OffsetDateTime.now());
delegation.setUpdatedAt(OffsetDateTime.now());
delegation.setStatus(DelegationState.ACTIVE);
Expand Down Expand Up @@ -128,13 +128,16 @@ public Delegation createDelegationFromInstitutionsTaxCode(Delegation delegation)
CustomError.CREATE_DELEGATION_CONFLICT.getCode());
}

Delegation savedDelegation;
try {
if(!checkIfExistsWithStatus(delegation, DelegationState.DELETED)){
if(checkIfExistsWithStatus(delegation, DelegationState.DELETED)){
savedDelegation = delegationConnector.findAndActivate(delegation.getFrom(),delegation.getTo(),delegation.getProductId());
} else {
delegation.setCreatedAt(OffsetDateTime.now());
delegation.setUpdatedAt(OffsetDateTime.now());
delegation.setStatus(DelegationState.ACTIVE);
savedDelegation = delegationConnector.save(delegation);
}
delegation.setUpdatedAt(OffsetDateTime.now());
delegation.setStatus(DelegationState.ACTIVE);
Delegation savedDelegation = delegationConnector.save(delegation);
institutionService.updateInstitutionDelegation(delegation.getTo(), true);
return savedDelegation;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,14 @@ void testCreateDelegationFromTaxCode() {
void testCreateDelegationFromTaxCodeWithSubunitCode() {
Institution institution = new Institution();
institution.setId("id");
when(delegationConnector.save(any())).thenReturn(dummyDelegationProdPagopa);
when(delegationConnector.findAndActivate(anyString(), anyString(), anyString())).thenReturn(dummyDelegationProdPagopa);
when(institutionService.getInstitutions(dummyDelegationProdPagopa.getTo(), dummyDelegationProdPagopa.getToSubunitCode())).thenReturn(List.of(institution));
when(institutionService.getInstitutions(dummyDelegationProdPagopa.getFrom(), dummyDelegationProdPagopa.getFromSubunitCode())).thenReturn(List.of(institution));
doNothing().when(institutionService).updateInstitutionDelegation(any(),anyBoolean());
when(delegationConnector.checkIfExistsWithStatus(dummyDelegationProdPagopa, DelegationState.ACTIVE)).thenReturn(false);
when(delegationConnector.checkIfExistsWithStatus(dummyDelegationProdPagopa, DelegationState.DELETED)).thenReturn(true);
Delegation response = delegationServiceImpl.createDelegationFromInstitutionsTaxCode(dummyDelegationProdPagopa);
verify(delegationConnector).save(any());
verify(delegationConnector).findAndActivate(anyString(), anyString(), anyString());
assertNotNull(response);
assertNotNull(response.getId());
assertEquals(dummyDelegationProdPagopa.getId(), response.getId());
Expand Down

0 comments on commit c1f427c

Please sign in to comment.