Skip to content

Commit

Permalink
chore: Adding test for mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-putzu committed Aug 28, 2024
1 parent 7dc911a commit 3b5376f
Showing 1 changed file with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package it.pagopa.selfcare.onboarding.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.azure.functions.ExecutionContext;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
Expand All @@ -21,6 +24,7 @@
import jakarta.inject.Inject;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.openapi.quarkus.core_json.api.InstitutionApi;
import org.openapi.quarkus.core_json.model.InstitutionResponse;

Expand All @@ -29,14 +33,15 @@
import java.util.Optional;
import java.util.logging.Logger;

import static it.pagopa.selfcare.onboarding.TestUtils.getMockedContext;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@QuarkusTest
public class NotificationEventServiceDefaultTest {

@Inject
NotificationEventServiceDefault messageServiceDefault;
NotificationEventServiceDefault notificationServiceDefault;

@InjectMock
ProductService productService;
Expand Down Expand Up @@ -70,7 +75,7 @@ void sendMessage() {
ExecutionContext context = mock(ExecutionContext.class);
doReturn(Logger.getGlobal()).when(context).getLogger();
doNothing().when(eventHubRestClient).sendMessage(anyString(), anyString());
messageServiceDefault.send(context, onboarding, QueueEvent.ADD);
notificationServiceDefault.send(context, onboarding, QueueEvent.ADD);
verify(eventHubRestClient, times(3))
.sendMessage(anyString(), anyString());
}
Expand All @@ -87,7 +92,7 @@ void sendMessageWithoutQueueEvent() {
doReturn(Logger.getGlobal()).when(context).getLogger();
doNothing().when(eventHubRestClient).sendMessage(anyString(), anyString());
when(queueEventExaminer.determineEventType(any())).thenReturn(QueueEvent.ADD);
messageServiceDefault.send(context, onboarding, null);
notificationServiceDefault.send(context, onboarding, null);
verify(eventHubRestClient, times(3))
.sendMessage(anyString(), anyString());
}
Expand All @@ -111,7 +116,7 @@ void sendMessageWithoutToken() {
ExecutionContext context = mock(ExecutionContext.class);
doReturn(Logger.getGlobal()).when(context).getLogger();
doNothing().when(eventHubRestClient).sendMessage(anyString(), anyString());
messageServiceDefault.send(context, onboarding, QueueEvent.ADD);
notificationServiceDefault.send(context, onboarding, QueueEvent.ADD);
verify(eventHubRestClient, times(3))
.sendMessage(anyString(), anyString());
}
Expand All @@ -127,7 +132,7 @@ void sendMessageDoesntSendNotificationIfFilterDoesntAllow() {
ExecutionContext context = mock(ExecutionContext.class);
doReturn(Logger.getGlobal()).when(context).getLogger();
doNothing().when(eventHubRestClient).sendMessage(anyString(), anyString());
messageServiceDefault.send(context, onboarding, QueueEvent.ADD);
notificationServiceDefault.send(context, onboarding, QueueEvent.ADD);
verifyNoInteractions(eventHubRestClient);
}

Expand All @@ -143,7 +148,7 @@ void sendMessageWithTestEnvProducts() {
ExecutionContext context = mock(ExecutionContext.class);
doReturn(Logger.getGlobal()).when(context).getLogger();
doNothing().when(eventHubRestClient).sendMessage(anyString(), anyString());
messageServiceDefault.send(context, onboarding, QueueEvent.ADD);
notificationServiceDefault.send(context, onboarding, QueueEvent.ADD);
verify(eventHubRestClient, times(9))
.sendMessage(anyString(), anyString());
}
Expand All @@ -160,7 +165,7 @@ void sendMessageWithError() {
.when(eventHubRestClient).sendMessage(anyString(), anyString());
ExecutionContext context = mock(ExecutionContext.class);
doReturn(Logger.getGlobal()).when(context).getLogger();
assertThrows(NotificationException.class, () -> messageServiceDefault.send(context, onboarding, QueueEvent.ADD));
assertThrows(NotificationException.class, () -> notificationServiceDefault.send(context, onboarding, QueueEvent.ADD));
verify(eventHubRestClient, times(1))
.sendMessage(anyString(), anyString());
}
Expand All @@ -173,7 +178,7 @@ void sendMessageNullConsumers() {
when(productService.getProduct(any())).thenReturn(test);
ExecutionContext context = mock(ExecutionContext.class);
doReturn(Logger.getGlobal()).when(context).getLogger();
messageServiceDefault.send(context, onboarding, QueueEvent.ADD);
notificationServiceDefault.send(context, onboarding, QueueEvent.ADD);
verifyNoInteractions(eventHubRestClient);
}

Expand All @@ -185,7 +190,7 @@ void sendMessageWontProceedsWhenOnboardingIsNotReferredToInstitution() {
ExecutionContext context = mock(ExecutionContext.class);
doReturn(Logger.getGlobal()).when(context).getLogger();

messageServiceDefault.send(context, onboarding, QueueEvent.ADD);
notificationServiceDefault.send(context, onboarding, QueueEvent.ADD);
verifyNoInteractions(productService);
verifyNoInteractions(tokenRepository);
verifyNoInteractions(institutionApi);
Expand Down Expand Up @@ -285,6 +290,33 @@ void notificationEventMapRootParentTest() {
assertEquals(properties.get("billing.TaxCodeInvoicing"), "456");
}

@Test
void sendNotificationsJsonError() throws JsonProcessingException {
final Onboarding onboarding = createOnboarding();
final Product product = createProduct();
when(productService.getProduct(any())).thenReturn(product);

when(tokenRepository.findByOnboardingId(any())).thenReturn(Optional.of(new Token()));
when(institutionApi.retrieveInstitutionByIdUsingGET(any())).thenReturn(new InstitutionResponse());

BaseNotificationBuilder notificationMapper = mock(BaseNotificationBuilder.class);
when(notificationBuilderFactory.create(any())).thenReturn(notificationMapper);

Object mockNotificationToSend = mock(NotificationToSend.class);
when(mockNotificationToSend.toString()).thenReturn(mockNotificationToSend.getClass().getName());

when(notificationMapper.buildNotificationToSend(any(), any(), any(), any())).thenReturn((NotificationToSend) mockNotificationToSend);
when(notificationMapper.shouldSendNotification(any(), any())).thenReturn(true);

ExecutionContext context = mock(ExecutionContext.class);
doReturn(Logger.getGlobal()).when(context).getLogger();
doNothing().when(eventHubRestClient).sendMessage(anyString(), anyString());
TelemetryClient telemetryClient = mock(TelemetryClient.class);
doNothing().when(telemetryClient).trackEvent(anyString(), any(), any());

assertThrows(RuntimeException.class, () -> notificationServiceDefault.send(context, onboarding, QueueEvent.ADD));
}

private Onboarding createOnboarding() {
Onboarding onboarding = new Onboarding();
onboarding.setWorkflowType(WorkflowType.CONTRACT_REGISTRATION);
Expand Down

0 comments on commit 3b5376f

Please sign in to comment.