Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SELC-5642] feat: added new workflow executor for GPU #621

Merged
merged 12 commits into from
Nov 28, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ public void onboardingsOrchestrator(
workflowExecutor = new WorkflowExecutorForApprove(objectMapper, optionsRetry);
case FOR_APPROVE_PT ->
workflowExecutor = new WorkflowExecutorForApprovePt(objectMapper, optionsRetry);
case FOR_APPROVE_GPU ->
workflowExecutor = new WorkflowExecutorForApproveGpu(objectMapper, optionsRetry);
case CONFIRMATION ->
workflowExecutor = new WorkflowExecutorConfirmation(objectMapper, optionsRetry);
case CONFIRMATION_AGGREGATE ->
Expand Down Expand Up @@ -254,7 +256,7 @@ public void buildContract(
}

/** This HTTP-triggered function invokes an orchestration to build attachments and save tokens */
@FunctionName("BuildAttachmentsAndSaveTokens")
@FunctionName("TriggerBuildAttachmentsAndSaveTokens")
public HttpResponseMessage buildAttachmentsAndSaveTokens(
@HttpTrigger(
name = "req",
Expand Down Expand Up @@ -293,7 +295,7 @@ public HttpResponseMessage buildAttachmentsAndSaveTokens(
* for invoking the activity function "BuildAttachment" and "saveTokenAttachment" until there are
* no more attachment to process.
*/
@FunctionName("BuildAttachmentAndSaveToken")
@FunctionName(BUILD_ATTACHMENTS_SAVE_TOKENS)
public void buildAttachmentAndSaveToken(
@DurableOrchestrationTrigger(name = "taskOrchestrationContext") TaskOrchestrationContext ctx,
ExecutionContext functionContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class ActivityName {

public static final String SAVE_TOKEN_WITH_CONTRACT_ACTIVITY_NAME = "SaveTokenWithContract";
public static final String BUILD_ATTACHMENTS_SAVE_TOKENS = "BuildAttachmentAndSaveTokens";
public static final String SAVE_TOKEN_WITH_ATTACHMENT_ACTIVITY_NAME = "SaveTokenWithAttachment";
public static final String BUILD_CONTRACT_ACTIVITY_NAME = "BuildContract";
public static final String BUILD_ATTACHMENT_ACTIVITY_NAME = "BuildAttachment";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
import static it.pagopa.selfcare.onboarding.utils.Utils.getOnboardingString;
import static it.pagopa.selfcare.onboarding.utils.Utils.getOnboardingWorkflowString;

public record WorkflowExecutorForApprove(ObjectMapper objectMapper, TaskOptions optionsRetry) implements WorkflowExecutor {
public class WorkflowExecutorForApprove implements WorkflowExecutor {

private final ObjectMapper objectMapper;
private final TaskOptions optionsRetry;

public WorkflowExecutorForApprove(ObjectMapper objectMapper, TaskOptions optionsRetry) {
this.objectMapper = objectMapper;
this.optionsRetry = optionsRetry;
}

@Override
public Optional<OnboardingStatus> executeRequestState(TaskOrchestrationContext ctx, OnboardingWorkflow onboardingWorkflow) {
Expand All @@ -42,4 +50,14 @@ public Optional<OnboardingStatus> executePendingState(TaskOrchestrationContext c
public OnboardingWorkflow createOnboardingWorkflow(Onboarding onboarding) {
return new OnboardingWorkflowInstitution(onboarding, INSTITUTION.name());
}

@Override
public ObjectMapper objectMapper() {
return objectMapper;
}

@Override
public TaskOptions optionsRetry() {
return optionsRetry;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package it.pagopa.selfcare.onboarding.workflow;

import static it.pagopa.selfcare.onboarding.functions.utils.ActivityName.*;
import static it.pagopa.selfcare.onboarding.utils.Utils.getOnboardingString;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.durabletask.TaskOptions;
import com.microsoft.durabletask.TaskOrchestrationContext;
import it.pagopa.selfcare.onboarding.common.OnboardingStatus;
import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflow;
import java.util.Optional;

public class WorkflowExecutorForApproveGpu extends WorkflowExecutorForApprove {

public WorkflowExecutorForApproveGpu(ObjectMapper objectMapper, TaskOptions optionsRetry) {
super(objectMapper, optionsRetry);
}

@Override
public Optional<OnboardingStatus> executeRequestState(TaskOrchestrationContext ctx, OnboardingWorkflow onboardingWorkflow) {
String onboardingString = getOnboardingString(super.objectMapper(), onboardingWorkflow.getOnboarding());
ctx.callSubOrchestrator(BUILD_ATTACHMENTS_SAVE_TOKENS, onboardingString, String.class);
ctx.callActivity(SEND_MAIL_ONBOARDING_APPROVE_ACTIVITY, onboardingString, super.optionsRetry(), String.class).await();
return Optional.of(OnboardingStatus.TOBEVALIDATED);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,77 @@ void onboardingOrchestratorUsersEaPending() {
function.onboardingsOrchestrator(orchestrationContext, executionContext);
}

@Test
void onboardingsOrchestratorForApproveGpu() {
Onboarding onboarding = new Onboarding();
onboarding.setId("onboardingId");
onboarding.setStatus(OnboardingStatus.REQUEST);
onboarding.setWorkflowType(WorkflowType.FOR_APPROVE_GPU);

TaskOrchestrationContext orchestrationContext = mockTaskOrchestrationContext(onboarding);

function.onboardingsOrchestrator(orchestrationContext, executionContext);

ArgumentCaptor<String> captorActivity = ArgumentCaptor.forClass(String.class);
verify(orchestrationContext, times(1))
.callActivity(captorActivity.capture(), any(), any(), any());

assertEquals(SEND_MAIL_ONBOARDING_APPROVE_ACTIVITY, captorActivity.getAllValues().get(0));

Mockito.verify(orchestrationContext, times(1))
.callSubOrchestrator(eq(BUILD_ATTACHMENTS_SAVE_TOKENS), any(), any());

verify(service, times(1))
.updateOnboardingStatus(onboarding.getId(), OnboardingStatus.TOBEVALIDATED);
}

@Test
void onboardingsOrchestratorForApproveGpuWhenIsPending() {
Onboarding onboarding = new Onboarding();
onboarding.setId("onboardingId");
onboarding.setStatus(OnboardingStatus.PENDING);
onboarding.setInstitution(new Institution());
onboarding.setWorkflowType(WorkflowType.FOR_APPROVE_GPU);

TaskOrchestrationContext orchestrationContext = mockTaskOrchestrationContext(onboarding);

function.onboardingsOrchestrator(orchestrationContext, executionContext);

ArgumentCaptor<String> captorActivity = ArgumentCaptor.forClass(String.class);
verify(orchestrationContext, times(5))
.callActivity(captorActivity.capture(), any(), any(), any());
assertEquals(CREATE_INSTITUTION_ACTIVITY, captorActivity.getAllValues().get(0));
assertEquals(CREATE_ONBOARDING_ACTIVITY, captorActivity.getAllValues().get(1));
assertEquals(CREATE_USERS_ACTIVITY, captorActivity.getAllValues().get(2));
assertEquals(STORE_ONBOARDING_ACTIVATEDAT, captorActivity.getAllValues().get(3));
assertEquals(SEND_MAIL_COMPLETION_ACTIVITY, captorActivity.getAllValues().get(4));

verify(service, times(1))
.updateOnboardingStatus(onboarding.getId(), OnboardingStatus.COMPLETED);
}

@Test
void onboardingsOrchestratorForApproveGpuWhenToBeValidated() {
Onboarding onboarding = new Onboarding();
onboarding.setId("onboardingId");
onboarding.setStatus(OnboardingStatus.TOBEVALIDATED);
onboarding.setWorkflowType(WorkflowType.FOR_APPROVE_GPU);

TaskOrchestrationContext orchestrationContext = mockTaskOrchestrationContext(onboarding);

function.onboardingsOrchestrator(orchestrationContext, executionContext);

ArgumentCaptor<String> captorActivity = ArgumentCaptor.forClass(String.class);
verify(orchestrationContext, times(3))
.callActivity(captorActivity.capture(), any(), any(), any());
assertEquals(BUILD_CONTRACT_ACTIVITY_NAME, captorActivity.getAllValues().get(0));
assertEquals(SAVE_TOKEN_WITH_CONTRACT_ACTIVITY_NAME, captorActivity.getAllValues().get(1));
assertEquals(SEND_MAIL_REGISTRATION_FOR_CONTRACT_WHEN_APPROVE_ACTIVITY, captorActivity.getAllValues().get(2));

verify(service, times(1))
.updateOnboardingStatus(onboarding.getId(), OnboardingStatus.PENDING);
}

private Product createDummyProduct() {
Product product = new Product();
product.setTitle("Title");
Expand Down Expand Up @@ -1173,12 +1244,4 @@ private static Map<String, ContractTemplate> createDummyContractTemplateInstitut
return institutionTemplate;
}

private static Map<String, ContractTemplate> createDummyContractTemplateUser() {
Map<String, ContractTemplate> institutionTemplate = new HashMap<>();
ContractTemplate conctractTemplate = new ContractTemplate();
conctractTemplate.setContractTemplatePath("example");
conctractTemplate.setContractTemplateVersion("version");
institutionTemplate.put("default", conctractTemplate);
return institutionTemplate;
}
}