Skip to content

Commit

Permalink
onboarding function test
Browse files Browse the repository at this point in the history
  • Loading branch information
manuraf committed Nov 10, 2023
1 parent d905f96 commit bc188ef
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,7 @@ private String onboardingsOrchestratorPG(TaskOrchestrationContext ctx, String on
@FunctionName(BUILD_CONTRACT_ACTIVITY_NAME)
public String buildContract(@DurableActivityTrigger(name = "onboardingString") String onboardingString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, BUILD_CONTRACT_ACTIVITY_NAME, onboardingString));
try {
service.createContract(objectMapper.readValue(onboardingString, Onboarding.class));
} catch (JsonProcessingException e) {
throw new FunctionOrchestratedException(e);
}
service.createContract(readOnboardingValue(onboardingString));
return onboardingString;
}

Expand All @@ -144,11 +140,7 @@ public String buildContract(@DurableActivityTrigger(name = "onboardingString") S
@FunctionName(SAVE_TOKEN_WITH_CONTRACT_ACTIVITY_NAME)
public String saveToken(@DurableActivityTrigger(name = "onboardingString") String onboardingString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, SAVE_TOKEN_WITH_CONTRACT_ACTIVITY_NAME, onboardingString));
try {
service.saveTokenWithContract(objectMapper.readValue(onboardingString, Onboarding.class));
} catch (JsonProcessingException e) {
throw new FunctionOrchestratedException(e);
}
service.saveTokenWithContract(readOnboardingValue(onboardingString));
return onboardingString;
}

Expand All @@ -158,44 +150,28 @@ public String saveToken(@DurableActivityTrigger(name = "onboardingString") Strin
@FunctionName(SEND_MAIL_REGISTRATION_WITH_CONTRACT_ACTIVITY_NAME)
public String sendMailRegistrationWithContract(@DurableActivityTrigger(name = "onboardingString") String onboardingString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, SEND_MAIL_REGISTRATION_WITH_CONTRACT_ACTIVITY_NAME, onboardingString));
try {
service.sendMailRegistrationWithContract(objectMapper.readValue(onboardingString, Onboarding.class));
} catch (JsonProcessingException e) {
throw new FunctionOrchestratedException(e);
}
service.sendMailRegistrationWithContract(readOnboardingValue(onboardingString));
return onboardingString;
}

@FunctionName(SEND_MAIL_REGISTRATION_REQUEST_ACTIVITY_NAME)
public String sendMailRegistration(@DurableActivityTrigger(name = "onboardingString") String onboardingString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, SEND_MAIL_REGISTRATION_REQUEST_ACTIVITY_NAME, onboardingString));
try {
service.sendMailRegistration(objectMapper.readValue(onboardingString, Onboarding.class));
} catch (JsonProcessingException e) {
throw new FunctionOrchestratedException(e);
}
service.sendMailRegistration(readOnboardingValue(onboardingString));
return onboardingString;
}

@FunctionName(SEND_MAIL_REGISTRATION_APPROVE_ACTIVITY_NAME)
public String sendMailRegistrationApprove(@DurableActivityTrigger(name = "onboardingString") String onboardingString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, SEND_MAIL_REGISTRATION_APPROVE_ACTIVITY_NAME, onboardingString));
try {
service.sendMailRegistrationApprove(objectMapper.readValue(onboardingString, Onboarding.class));
} catch (JsonProcessingException e) {
throw new FunctionOrchestratedException(e);
}
service.sendMailRegistrationApprove(readOnboardingValue(onboardingString));
return onboardingString;
}

@FunctionName(SEND_MAIL_ONBOARDING_APPROVE_ACTIVITY_NAME)
public String sendMailOnboardingApprove(@DurableActivityTrigger(name = "onboardingString") String onboardingString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, SEND_MAIL_ONBOARDING_APPROVE_ACTIVITY_NAME, onboardingString));
try {
service.sendMailOnboardingApprove(objectMapper.readValue(onboardingString, Onboarding.class));
} catch (JsonProcessingException e) {
throw new FunctionOrchestratedException(e);
}
service.sendMailOnboardingApprove(readOnboardingValue(onboardingString));
return onboardingString;
}

Expand All @@ -207,4 +183,12 @@ public String sendMailConfirmation(@DurableActivityTrigger(name = "onboardingStr
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING,"SendMailConfirmation", onboardingString));
return onboardingString;
}

private Onboarding readOnboardingValue(String onboardingString) {
try {
return objectMapper.readValue(onboardingString, Onboarding.class);
} catch (JsonProcessingException e) {
throw new FunctionOrchestratedException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ quarkus.mailer.start-tls=${MAIL_SERVER_SMTP_TLS_ENABLE:REQUIRED}


## Jacoco
quarkus.jacoco.includes=it/pagopa/selfcare/onboarding/controller/*,it/pagopa/selfcare/onboarding/service/**,it/pagopa/selfcare/onboarding/repository/**
quarkus.jacoco.includes=it/pagopa/selfcare/onboarding/*,it/pagopa/selfcare/onboarding/service/**,it/pagopa/selfcare/onboarding/repository/**
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.durabletask.DurableTaskClient;
import com.microsoft.durabletask.Task;
import com.microsoft.durabletask.TaskOrchestrationContext;
import com.microsoft.durabletask.azurefunctions.DurableClientContext;
import io.quarkus.test.InjectMock;
import io.quarkus.test.Mock;
import io.quarkus.test.junit.QuarkusTest;
import it.pagopa.selfcare.onboarding.OnboardingFunctions;
import it.pagopa.selfcare.onboarding.entity.Onboarding;
import it.pagopa.selfcare.onboarding.exception.ResourceNotFoundException;
import it.pagopa.selfcare.onboarding.service.OnboardingService;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

Expand All @@ -18,11 +28,9 @@
import java.util.logging.Logger;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

import static org.mockito.Mockito.*;


/**
Expand All @@ -34,18 +42,24 @@ public class OnboardingFunctionsTest {
@Inject
OnboardingFunctions function;

@InjectMock
OnboardingService service;

final String onboardinString = "{\"onboardingId\":\"onboardingId\"}";

/**
* Unit test for HttpTriggerJava method.
*/
//@Test
@Test
public void testHttpTriggerJava() throws Exception {
// Setup
@SuppressWarnings("unchecked")
final HttpRequestMessage<Optional<String>> req = mock(HttpRequestMessage.class);
final HttpResponseMessage res = mock(HttpResponseMessage.class);

final Map<String, String> queryParams = new HashMap<>();
queryParams.put("onboardingId", "Azure");
final String onboardingId = "onboardingId";
queryParams.put("onboardingId", onboardingId);
doReturn(queryParams).when(req).getQueryParameters();

final Optional<String> queryBody = Optional.empty();
Expand All @@ -65,14 +79,106 @@ public HttpResponseMessage.Builder answer(InvocationOnMock invocation) {
final DurableClientContext durableContext = mock(DurableClientContext.class);
final DurableTaskClient client = mock(DurableTaskClient.class);
final String scheduleNewOrchestrationInstance = "scheduleNewOrchestrationInstance";
doReturn(scheduleNewOrchestrationInstance).when(client).scheduleNewOrchestrationInstance(any());
doReturn(client).when(durableContext).getClient();
doReturn(res).when(durableContext).createCheckStatusResponse(req, scheduleNewOrchestrationInstance);
doReturn(scheduleNewOrchestrationInstance).when(client).scheduleNewOrchestrationInstance("Onboardings",onboardingId);
doReturn(res).when(durableContext).createCheckStatusResponse(any(), any());

// Invoke
final HttpResponseMessage ret = function.startOrchestration(req, durableContext, context);
function.startOrchestration(req, durableContext, context);

// Verify
assertEquals(ret.getStatus(), ret.getStatus());
ArgumentCaptor<String> captorInstanceId = ArgumentCaptor.forClass(String.class);
Mockito.verify(durableContext, times(1))
.createCheckStatusResponse(any(), captorInstanceId.capture());
assertEquals(scheduleNewOrchestrationInstance, captorInstanceId.getValue());
}

@Test
void onboardingsOrchestrator_thorwExceptionIfOnboardingNotPresent() {
final String onboardingId = "onboardingId";
TaskOrchestrationContext orchestrationContext = mock(TaskOrchestrationContext.class);
when(orchestrationContext.getInput(String.class)).thenReturn(onboardingId);
when(service.getOnboarding(onboardingId)).thenReturn(Optional.empty());
assertThrows(ResourceNotFoundException.class, () -> function.onboardingsOrchestrator(orchestrationContext));
}

@Test
void onboardingsOrchestratorDefault() {
Onboarding onboarding = new Onboarding();
onboarding.setOnboardingId("onboardingId");

TaskOrchestrationContext orchestrationContext = mock(TaskOrchestrationContext.class);
when(orchestrationContext.getInput(String.class)).thenReturn(onboarding.getOnboardingId());
when(service.getOnboarding(onboarding.getOnboardingId())).thenReturn(Optional.of(onboarding));

Task task = mock(Task.class);
when(orchestrationContext.callActivity(any(),any(),any(),any())).thenReturn(task);
when(task.await()).thenReturn("example");

function.onboardingsOrchestrator(orchestrationContext);

Mockito.verify(orchestrationContext, times(4))
.callActivity(any(), any(), any(),any());

}

@Test
void buildContract() {
ExecutionContext executionContext = mock(ExecutionContext.class);
when(executionContext.getLogger()).thenReturn(Logger.getGlobal());
doNothing().when(service).createContract(any());

function.buildContract(onboardinString, executionContext);

Mockito.verify(service, times(1))
.createContract(any());
}

@Test
void saveToken() {
ExecutionContext executionContext = mock(ExecutionContext.class);
when(executionContext.getLogger()).thenReturn(Logger.getGlobal());
doNothing().when(service).saveTokenWithContract(any());

function.saveToken(onboardinString, executionContext);

Mockito.verify(service, times(1))
.saveTokenWithContract(any());
}

@Test
void sendMailRegistrationWithContract() {
ExecutionContext executionContext = mock(ExecutionContext.class);
when(executionContext.getLogger()).thenReturn(Logger.getGlobal());
doNothing().when(service).sendMailRegistrationWithContract(any());

function.sendMailRegistrationWithContract(onboardinString, executionContext);

Mockito.verify(service, times(1))
.sendMailRegistrationWithContract(any());
}

@Test
void sendMailRegistration() {
ExecutionContext executionContext = mock(ExecutionContext.class);
when(executionContext.getLogger()).thenReturn(Logger.getGlobal());
doNothing().when(service).sendMailRegistration(any());

function.sendMailRegistration(onboardinString, executionContext);

Mockito.verify(service, times(1))
.sendMailRegistration(any());
}

@Test
void sendMailRegistrationApprove() {
ExecutionContext executionContext = mock(ExecutionContext.class);
when(executionContext.getLogger()).thenReturn(Logger.getGlobal());
doNothing().when(service).sendMailRegistrationApprove(any());

function.sendMailRegistrationApprove(onboardinString, executionContext);

Mockito.verify(service, times(1))
.sendMailRegistrationApprove(any());
}
}

0 comments on commit bc188ef

Please sign in to comment.