Skip to content

Commit

Permalink
SELC-4785 feat: added control to use the invoicing tax code for UOs t…
Browse files Browse the repository at this point in the history
…hat have it
  • Loading branch information
empassaro committed May 17, 2024
1 parent 7686910 commit 85e22a5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Institution {
private String address;
private String zipCode;
private String taxCode;
private String taxCodeInvoicing;
private String origin;
private Billing billing;
private InstitutionType institutionType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -65,6 +66,9 @@ public void sendInstitutionNotification(Notification notification, Acknowledgmen
if (checkAllowedNotification(notification)) {
log.debug(LogUtils.CONFIDENTIAL_MARKER, "send institution notification = {}", notification);
NotificationToSend notificationToSend = notificationMapper.createInstitutionNotification(notification);
if (StringUtils.hasText(notification.getInstitution().getTaxCodeInvoicing())) {
notificationToSend.getInstitution().setTaxCode(notification.getInstitution().getTaxCodeInvoicing());
}
setNotificationInstitutionLocationFields(notificationToSend);
setNotificationToSendInstitutionDescription(notificationToSend);
notificationToSend.setType(NotificationType.ADD_INSTITUTE);
Expand All @@ -77,23 +81,24 @@ public void sendInstitutionNotification(Notification notification, Acknowledgmen
}

private static void setNotificationToSendInstitutionDescription(NotificationToSend notificationToSend) {
if(notificationToSend.getInstitution().getRootParent() != null) {
if (notificationToSend.getInstitution().getRootParent() != null) {
notificationToSend.getInstitution().setDescription(
notificationToSend.getInstitution().getRootParent().getDescription()
+ " - " + notificationToSend.getInstitution().getDescription());
}
}

private boolean checkAllowedNotification(Notification notification){
private boolean checkAllowedNotification(Notification notification) {
return isProductAllowed(notification, allowedProducts)
&& isInstitutionTypeAllowed(notification, allowedInstitutionTypes)
&& isOriginAllowed(notification, allowedOrigins);
}

private boolean isProductAllowed(Notification notification, Optional<List<String>> allowedProducts) {
return (allowedProducts.isPresent() && allowedProducts.get().contains(notification.getProduct())) || isProdIoFast(notification.getProduct(), notification.getPricingPlan());
}

private boolean isProdIoFast(String productId, String pricingPlan){
private boolean isProdIoFast(String productId, String pricingPlan) {
return ProductId.PROD_IO.getValue().equals(productId) && PricingPlan.FA.name().equals(pricingPlan);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ void sendInstitutionNotificationEc() throws JsonProcessingException {
Institution institution = mockInstance(new Institution(), "setCity", "setRootParent");
institution.setSubUnitType("EC");
institution.setOrigin("IPA");
institution.setTaxCodeInvoicing(null);
institution.setInstitutionType(InstitutionType.PA);
final Billing billing = createBillingMock();
notification.setInstitution(institution);
Expand Down Expand Up @@ -157,6 +158,53 @@ void sendInstitutionNotificationEc() throws JsonProcessingException {
checkNotNullFields(captured.getInstitution(), "rootParent");
}

@Test
void sendInstitutionNotificationEcWithTaxCodeInvoicing() throws JsonProcessingException {
//given
final Notification notification = createNotificationMock();
Institution institution = mockInstance(new Institution(), "setCity", "setRootParent");
institution.setSubUnitType("UO");
institution.setOrigin("IPA");
institution.setTaxCodeInvoicing("taxCodeInvoicing");
institution.setInstitutionType(InstitutionType.PA);
final Billing billing = createBillingMock();
notification.setInstitution(institution);
notification.setBilling(billing);
notification.setProduct("prod-io-premium");
notification.setState("ACTIVE");
OrganizationUnit organizationUnit = mockInstance(new OrganizationUnit());
GeographicTaxonomies proxyTaxonomy = mockInstance(new GeographicTaxonomies());
proxyTaxonomy.setCountry("proxyContry");
proxyTaxonomy.setProvinceAbbreviation("proxyProvince");
proxyTaxonomy.setDescription("proxyCity - COMUNE");
proxyTaxonomy.setIstatCode(organizationUnit.getMunicipalIstatCode());
when(registryProxyConnector.getUoById(any())).thenReturn(organizationUnit);
when(registryProxyConnector.getExtById(any())).thenReturn(proxyTaxonomy);
when(kafkaTemplate.send(any(ProducerRecord.class)))
.thenReturn(mockFuture);

doAnswer(invocationOnMock -> {
ListenableFutureCallback callback = invocationOnMock.getArgument(0);
callback.onSuccess(mockSendResult);
return null;
}).when(mockFuture).addCallback(any(ListenableFutureCallback.class));

//when
Executable executable = () -> service.sendInstitutionNotification(notification, acknowledgment);
//then
assertDoesNotThrow(executable);
ArgumentCaptor<ProducerRecord<String, String>> producerRecordArgumentCaptor = ArgumentCaptor.forClass(ProducerRecord.class);
verify(kafkaTemplate, times(1)).send(producerRecordArgumentCaptor.capture());
verify(acknowledgment, times(1)).acknowledge();
verify(registryProxyConnector, times(1)).getUoById(notification.getInstitution().getSubUnitCode());
verify(registryProxyConnector, times(1)).getExtById(organizationUnit.getMunicipalIstatCode());
verifyNoMoreInteractions(registryProxyConnector);
NotificationToSend captured = mapper.readValue(producerRecordArgumentCaptor.getValue().value(), NotificationToSend.class);
checkNotNullFields(captured, "user");
checkNotNullFields(captured.getInstitution(), "rootParent");

}

@Test
void sendInstitutionNotificationUo() throws JsonProcessingException {
//given
Expand Down Expand Up @@ -244,7 +292,7 @@ void sendInstitutionNotificationAoo() throws JsonProcessingException {
verifyNoMoreInteractions(registryProxyConnector);
NotificationToSend captured = mapper.readValue(producerRecordArgumentCaptor.getValue().value(), NotificationToSend.class);
checkNotNullFields(captured, "user");
assertEquals("Sc-Contracts-Sap",producerRecordArgumentCaptor.getValue().topic());
assertEquals("Sc-Contracts-Sap", producerRecordArgumentCaptor.getValue().topic());
checkNotNullFields(captured.getInstitution());
}

Expand Down Expand Up @@ -276,7 +324,8 @@ void sendInstitutionNotification_NotFound() throws JsonProcessingException {
//then
assertDoesNotThrow(executable);
ArgumentCaptor<ProducerRecord<String, String>> producerRecordArgumentCaptor = ArgumentCaptor.forClass(ProducerRecord.class);
verify(kafkaTemplate, times(1)).send(producerRecordArgumentCaptor.capture());;
verify(kafkaTemplate, times(1)).send(producerRecordArgumentCaptor.capture());
;
verify(acknowledgment, times(1)).acknowledge();
verify(registryProxyConnector, times(1)).getAooById(institution.getSubUnitCode());
verifyNoMoreInteractions(registryProxyConnector);
Expand Down Expand Up @@ -404,7 +453,7 @@ void productNotAllowed() {
}

@Test
void isProdIoFast(){
void isProdIoFast() {
//given
final Notification notification = createNotificationMock();
notification.setPricingPlan("FA");
Expand All @@ -423,6 +472,7 @@ void isProdIoFast(){
assertDoesNotThrow(executable);
verifyNoInteractions(kafkaTemplate);
}

@Test
void allowedOriginsNotPresent() {
//given
Expand All @@ -446,7 +496,7 @@ void allowedOriginsNotPresent() {
}

@Test
void originNotAllowed(){
void originNotAllowed() {
//given
service = new SendSapNotification(kafkaTemplate, notificationMapperSpy, mapper, registryProxyConnector, externalApiConnector, Set.of(InstitutionType.PA, InstitutionType.GSP), List.of("prod-pn"), Set.of(Origin.IPA));
final Notification notification = new Notification();
Expand Down Expand Up @@ -503,19 +553,19 @@ void sendOldEvents() throws JsonProcessingException {
}


private static Notification createNotificationMock(){
private static Notification createNotificationMock() {
return mockInstance(new Notification());
}

private static Institution createInstitutionMock(){
private static Institution createInstitutionMock() {
return mockInstance(new Institution());
}

private static Billing createBillingMock(){
private static Billing createBillingMock() {
return mockInstance(new Billing());
}

private static OnboardedProduct createOnboardedProductMock(){
private static OnboardedProduct createOnboardedProductMock() {
return mockInstance(new OnboardedProduct());
}
}

0 comments on commit 85e22a5

Please sign in to comment.