From 17ad2d200cb0c3fb5da9536658fde0ef52ec48ee Mon Sep 17 00:00:00 2001 From: pierpaolodidato89 <137791912+pierpaolodidato89@users.noreply.github.com> Date: Thu, 28 Nov 2024 13:27:38 +0100 Subject: [PATCH] [SELC-5642] feat: added new workflow executor for GPU --- .../functions/OnboardingFunctions.java | 6 +- .../functions/utils/ActivityName.java | 1 + .../workflow/WorkflowExecutorForApprove.java | 20 ++++- .../WorkflowExecutorForApproveGpu.java | 27 +++++++ .../functions/OnboardingFunctionsTest.java | 79 +++++++++++++++++-- 5 files changed, 122 insertions(+), 11 deletions(-) create mode 100644 apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/workflow/WorkflowExecutorForApproveGpu.java diff --git a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/functions/OnboardingFunctions.java b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/functions/OnboardingFunctions.java index b354904ed..ecb395889 100644 --- a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/functions/OnboardingFunctions.java +++ b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/functions/OnboardingFunctions.java @@ -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 -> @@ -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", @@ -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_ACTIVITY) public void buildAttachmentAndSaveToken( @DurableOrchestrationTrigger(name = "taskOrchestrationContext") TaskOrchestrationContext ctx, ExecutionContext functionContext) diff --git a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/functions/utils/ActivityName.java b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/functions/utils/ActivityName.java index bf8291a52..8b03f1358 100644 --- a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/functions/utils/ActivityName.java +++ b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/functions/utils/ActivityName.java @@ -32,6 +32,7 @@ public class ActivityName { public static final String EXISTS_DELEGATION_ACTIVITY = "ExistsDelegationActivity"; public static final String DELETE_MANAGERS_BY_IC_AND_ADE = "DeleteManagersByIcAndAde"; public static final String RETRIEVE_AGGREGATES_ACTIVITY = "RetrieveAggregates"; + public static final String BUILD_ATTACHMENTS_SAVE_TOKENS_ACTIVITY = "BuildAttachmentAndSaveTokens"; private ActivityName() {} } diff --git a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/workflow/WorkflowExecutorForApprove.java b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/workflow/WorkflowExecutorForApprove.java index 6c53420ac..db2093995 100644 --- a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/workflow/WorkflowExecutorForApprove.java +++ b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/workflow/WorkflowExecutorForApprove.java @@ -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 executeRequestState(TaskOrchestrationContext ctx, OnboardingWorkflow onboardingWorkflow) { @@ -42,4 +50,14 @@ public Optional 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; + } } diff --git a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/workflow/WorkflowExecutorForApproveGpu.java b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/workflow/WorkflowExecutorForApproveGpu.java new file mode 100644 index 000000000..fc6eccd20 --- /dev/null +++ b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/workflow/WorkflowExecutorForApproveGpu.java @@ -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 executeRequestState(TaskOrchestrationContext ctx, OnboardingWorkflow onboardingWorkflow) { + String onboardingString = getOnboardingString(super.objectMapper(), onboardingWorkflow.getOnboarding()); + ctx.callSubOrchestrator(BUILD_ATTACHMENTS_SAVE_TOKENS_ACTIVITY, onboardingString, String.class); + ctx.callActivity(SEND_MAIL_ONBOARDING_APPROVE_ACTIVITY, onboardingString, super.optionsRetry(), String.class).await(); + return Optional.of(OnboardingStatus.TOBEVALIDATED); + } + +} diff --git a/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/functions/OnboardingFunctionsTest.java b/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/functions/OnboardingFunctionsTest.java index c80f1806d..0e22cdb7b 100644 --- a/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/functions/OnboardingFunctionsTest.java +++ b/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/functions/OnboardingFunctionsTest.java @@ -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 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_ACTIVITY), 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 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 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"); @@ -1173,12 +1244,4 @@ private static Map createDummyContractTemplateInstitut return institutionTemplate; } - private static Map createDummyContractTemplateUser() { - Map institutionTemplate = new HashMap<>(); - ContractTemplate conctractTemplate = new ContractTemplate(); - conctractTemplate.setContractTemplatePath("example"); - conctractTemplate.setContractTemplateVersion("version"); - institutionTemplate.put("default", conctractTemplate); - return institutionTemplate; - } }