From 6aa3c8dab03522d20a249ce51a5519388ede3321 Mon Sep 17 00:00:00 2001 From: kaibocai <89094811+kaibocai@users.noreply.github.com> Date: Mon, 30 Oct 2023 21:03:44 -0500 Subject: [PATCH 1/4] add NewWorkflowOption Signed-off-by: kaibocai <89094811+kaibocai@users.noreply.github.com> --- .../workflows/client/DaprWorkflowClient.java | 17 ++- .../workflows/client/NewWorkflowOption.java | 100 ++++++++++++++++++ 2 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java diff --git a/sdk-workflows/src/main/java/io/dapr/workflows/client/DaprWorkflowClient.java b/sdk-workflows/src/main/java/io/dapr/workflows/client/DaprWorkflowClient.java index 2915b2c71f..24656e768a 100644 --- a/sdk-workflows/src/main/java/io/dapr/workflows/client/DaprWorkflowClient.java +++ b/sdk-workflows/src/main/java/io/dapr/workflows/client/DaprWorkflowClient.java @@ -13,10 +13,7 @@ package io.dapr.workflows.client; -import com.microsoft.durabletask.DurableTaskClient; -import com.microsoft.durabletask.DurableTaskGrpcClientBuilder; -import com.microsoft.durabletask.OrchestrationMetadata; -import com.microsoft.durabletask.PurgeResult; +import com.microsoft.durabletask.*; import io.dapr.client.Headers; import io.dapr.config.Properties; import io.dapr.utils.NetworkUtils; @@ -119,6 +116,18 @@ public String scheduleNewWorkflow(Class clazz, Object in return this.innerClient.scheduleNewOrchestrationInstance(clazz.getCanonicalName(), input, instanceId); } + /** + * Schedules a new workflow with a specified set of options for execution. + * + * @param any Workflow type + * @param clazz Class extending Workflow to start an instance of. + * @param options the options for the new workflow, including input, instance ID, etc. + * @return the instanceId parameter value. + */ + public String scheduleNewWorkflow(Class clazz, NewWorkflowOption options) { + return this.innerClient.scheduleNewOrchestrationInstance(clazz.getCanonicalName(), options.getNewOrchestrationInstanceOptions()); + } + /** * Terminates the workflow associated with the provided instance id. * diff --git a/sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java b/sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java new file mode 100644 index 0000000000..d68e2af425 --- /dev/null +++ b/sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java @@ -0,0 +1,100 @@ +package io.dapr.workflows.client; + +import com.microsoft.durabletask.NewOrchestrationInstanceOptions; +import java.time.Instant; + +/** + * Options for starting a new instance of a workflow. + */ +public class NewWorkflowOption { + private final NewOrchestrationInstanceOptions newOrchestrationInstanceOptions = new NewOrchestrationInstanceOptions(); + + /** + * Sets the version of the workflow to start. + * + * @param version the user-defined version of workflow + * @return this {@link NewWorkflowOption} object + */ + public NewWorkflowOption setVersion(String version) { + this.newOrchestrationInstanceOptions.setVersion(version); + return this; + } + + /** + * Sets the instance ID of the workflow to start. + *

+ * If no instance ID is configured, the workflow will be created with a randomly generated instance ID. + * + * @param instanceId the ID of the new workflow + * @return this {@link NewWorkflowOption} object + */ + public NewWorkflowOption setInstanceId(String instanceId) { + this.newOrchestrationInstanceOptions.setInstanceId(instanceId); + return this; + } + + /** + * Sets the input of the workflow to start. + * + * @param input the input of the new workflow + * @return this {@link NewWorkflowOption} object + */ + public NewWorkflowOption setInput(Object input) { + this.newOrchestrationInstanceOptions.setInput(input); + return this; + } + + /** + * Sets the start time of the new workflow. + *

+ * By default, new workflow instances start executing immediately. This method can be used + * to start them at a specific time in the future. + * + * @param startTime the start time of the new workflow + * @return this {@link NewWorkflowOption} object + */ + public NewWorkflowOption setStartTime(Instant startTime) { + this.newOrchestrationInstanceOptions.setStartTime(startTime); + return this; + } + + /** + * Gets the user-specified version of the new workflow. + * + * @return the user-specified version of the new workflow. + */ + public String getVersion() { + return this.newOrchestrationInstanceOptions.getVersion(); + } + + /** + * Gets the instance ID of the new workflow. + * + * @return the instance ID of the new workflow. + */ + public String getInstanceId() { + return this.newOrchestrationInstanceOptions.getInstanceId(); + } + + /** + * Gets the input of the new workflow. + * + * @return the input of the new workflow. + */ + public Object getInput() { + return this.newOrchestrationInstanceOptions.getInput(); + } + + /** + * Gets the configured start time of the new workflow instance. + * + * @return the configured start time of the new workflow instance. + */ + public Instant getStartTime() { + return this.newOrchestrationInstanceOptions.getStartTime(); + } + + public NewOrchestrationInstanceOptions getNewOrchestrationInstanceOptions() { + return newOrchestrationInstanceOptions; + } +} From 45d30d24c2cb0ee424d90d60c4935961e94f8ac1 Mon Sep 17 00:00:00 2001 From: kaibocai <89094811+kaibocai@users.noreply.github.com> Date: Mon, 30 Oct 2023 21:43:50 -0500 Subject: [PATCH 2/4] fix style Signed-off-by: kaibocai <89094811+kaibocai@users.noreply.github.com> --- .../workflows/client/DaprWorkflowClient.java | 8 +- .../workflows/client/NewWorkflowOption.java | 176 ++++++++++-------- 2 files changed, 101 insertions(+), 83 deletions(-) diff --git a/sdk-workflows/src/main/java/io/dapr/workflows/client/DaprWorkflowClient.java b/sdk-workflows/src/main/java/io/dapr/workflows/client/DaprWorkflowClient.java index 24656e768a..42f12f3282 100644 --- a/sdk-workflows/src/main/java/io/dapr/workflows/client/DaprWorkflowClient.java +++ b/sdk-workflows/src/main/java/io/dapr/workflows/client/DaprWorkflowClient.java @@ -13,7 +13,10 @@ package io.dapr.workflows.client; -import com.microsoft.durabletask.*; +import com.microsoft.durabletask.DurableTaskClient; +import com.microsoft.durabletask.DurableTaskGrpcClientBuilder; +import com.microsoft.durabletask.OrchestrationMetadata; +import com.microsoft.durabletask.PurgeResult; import io.dapr.client.Headers; import io.dapr.config.Properties; import io.dapr.utils.NetworkUtils; @@ -125,7 +128,8 @@ public String scheduleNewWorkflow(Class clazz, Object in * @return the instanceId parameter value. */ public String scheduleNewWorkflow(Class clazz, NewWorkflowOption options) { - return this.innerClient.scheduleNewOrchestrationInstance(clazz.getCanonicalName(), options.getNewOrchestrationInstanceOptions()); + return this.innerClient.scheduleNewOrchestrationInstance(clazz.getCanonicalName(), + options.getNewOrchestrationInstanceOptions()); } /** diff --git a/sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java b/sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java index d68e2af425..d802c8f2c3 100644 --- a/sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java +++ b/sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java @@ -1,100 +1,114 @@ +/* + * Copyright 2023 The Dapr Authors + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and +limitations under the License. +*/ + package io.dapr.workflows.client; import com.microsoft.durabletask.NewOrchestrationInstanceOptions; + import java.time.Instant; /** * Options for starting a new instance of a workflow. */ public class NewWorkflowOption { - private final NewOrchestrationInstanceOptions newOrchestrationInstanceOptions = new NewOrchestrationInstanceOptions(); + private final NewOrchestrationInstanceOptions newOrchestrationInstanceOptions = new NewOrchestrationInstanceOptions(); - /** - * Sets the version of the workflow to start. - * - * @param version the user-defined version of workflow - * @return this {@link NewWorkflowOption} object - */ - public NewWorkflowOption setVersion(String version) { - this.newOrchestrationInstanceOptions.setVersion(version); - return this; - } + /** + * Sets the version of the workflow to start. + * + * @param version the user-defined version of workflow + * @return this {@link NewWorkflowOption} object + */ + public NewWorkflowOption setVersion(String version) { + this.newOrchestrationInstanceOptions.setVersion(version); + return this; + } - /** - * Sets the instance ID of the workflow to start. - *

- * If no instance ID is configured, the workflow will be created with a randomly generated instance ID. - * - * @param instanceId the ID of the new workflow - * @return this {@link NewWorkflowOption} object - */ - public NewWorkflowOption setInstanceId(String instanceId) { - this.newOrchestrationInstanceOptions.setInstanceId(instanceId); - return this; - } + /** + * Sets the instance ID of the workflow to start. + * + *

If no instance ID is configured, the workflow will be created with a randomly generated instance ID. + * + * @param instanceId the ID of the new workflow + * @return this {@link NewWorkflowOption} object + */ + public NewWorkflowOption setInstanceId(String instanceId) { + this.newOrchestrationInstanceOptions.setInstanceId(instanceId); + return this; + } - /** - * Sets the input of the workflow to start. - * - * @param input the input of the new workflow - * @return this {@link NewWorkflowOption} object - */ - public NewWorkflowOption setInput(Object input) { - this.newOrchestrationInstanceOptions.setInput(input); - return this; - } + /** + * Sets the input of the workflow to start. + * + * @param input the input of the new workflow + * @return this {@link NewWorkflowOption} object + */ + public NewWorkflowOption setInput(Object input) { + this.newOrchestrationInstanceOptions.setInput(input); + return this; + } - /** - * Sets the start time of the new workflow. - *

- * By default, new workflow instances start executing immediately. This method can be used - * to start them at a specific time in the future. - * - * @param startTime the start time of the new workflow - * @return this {@link NewWorkflowOption} object - */ - public NewWorkflowOption setStartTime(Instant startTime) { - this.newOrchestrationInstanceOptions.setStartTime(startTime); - return this; - } + /** + * Sets the start time of the new workflow. + * + *

By default, new workflow instances start executing immediately. This method can be used + * to start them at a specific time in the future. + * + * @param startTime the start time of the new workflow + * @return this {@link NewWorkflowOption} object + */ + public NewWorkflowOption setStartTime(Instant startTime) { + this.newOrchestrationInstanceOptions.setStartTime(startTime); + return this; + } - /** - * Gets the user-specified version of the new workflow. - * - * @return the user-specified version of the new workflow. - */ - public String getVersion() { - return this.newOrchestrationInstanceOptions.getVersion(); - } + /** + * Gets the user-specified version of the new workflow. + * + * @return the user-specified version of the new workflow. + */ + public String getVersion() { + return this.newOrchestrationInstanceOptions.getVersion(); + } - /** - * Gets the instance ID of the new workflow. - * - * @return the instance ID of the new workflow. - */ - public String getInstanceId() { - return this.newOrchestrationInstanceOptions.getInstanceId(); - } + /** + * Gets the instance ID of the new workflow. + * + * @return the instance ID of the new workflow. + */ + public String getInstanceId() { + return this.newOrchestrationInstanceOptions.getInstanceId(); + } - /** - * Gets the input of the new workflow. - * - * @return the input of the new workflow. - */ - public Object getInput() { - return this.newOrchestrationInstanceOptions.getInput(); - } + /** + * Gets the input of the new workflow. + * + * @return the input of the new workflow. + */ + public Object getInput() { + return this.newOrchestrationInstanceOptions.getInput(); + } - /** - * Gets the configured start time of the new workflow instance. - * - * @return the configured start time of the new workflow instance. - */ - public Instant getStartTime() { - return this.newOrchestrationInstanceOptions.getStartTime(); - } + /** + * Gets the configured start time of the new workflow instance. + * + * @return the configured start time of the new workflow instance. + */ + public Instant getStartTime() { + return this.newOrchestrationInstanceOptions.getStartTime(); + } - public NewOrchestrationInstanceOptions getNewOrchestrationInstanceOptions() { - return newOrchestrationInstanceOptions; - } + public NewOrchestrationInstanceOptions getNewOrchestrationInstanceOptions() { + return newOrchestrationInstanceOptions; + } } From e37225d60ce629a4a558c2d21ee078ea8b36472c Mon Sep 17 00:00:00 2001 From: kaibocai <89094811+kaibocai@users.noreply.github.com> Date: Thu, 2 Nov 2023 10:01:21 -0500 Subject: [PATCH 3/4] add unit test Signed-off-by: kaibocai <89094811+kaibocai@users.noreply.github.com> --- .../workflows/client/DaprWorkflowClientTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sdk-workflows/src/test/java/io/dapr/workflows/client/DaprWorkflowClientTest.java b/sdk-workflows/src/test/java/io/dapr/workflows/client/DaprWorkflowClientTest.java index 50b69f93b0..45c5c8c9ec 100644 --- a/sdk-workflows/src/test/java/io/dapr/workflows/client/DaprWorkflowClientTest.java +++ b/sdk-workflows/src/test/java/io/dapr/workflows/client/DaprWorkflowClientTest.java @@ -26,6 +26,7 @@ import java.lang.reflect.Constructor; import java.time.Duration; +import java.time.Instant; import java.util.Arrays; import java.util.concurrent.TimeoutException; @@ -106,6 +107,19 @@ public void scheduleNewWorkflowWithArgsNameInputInstance() { .scheduleNewOrchestrationInstance(expectedName, expectedInput, expectedInstanceId); } + @Test + public void scheduleNewWorkflowWithNewWorkflowOption() { + String expectedName = TestWorkflow.class.getCanonicalName(); + Object expectedInput = new Object(); + NewWorkflowOption newWorkflowOption = new NewWorkflowOption(); + newWorkflowOption.setInput(expectedInput).setStartTime(Instant.now()); + + client.scheduleNewWorkflow(TestWorkflow.class, newWorkflowOption); + + verify(mockInnerClient, times(1)) + .scheduleNewOrchestrationInstance(expectedName, newWorkflowOption.getNewOrchestrationInstanceOptions()); + } + @Test public void terminateWorkflow() { String expectedArgument = "TestWorkflowInstanceId"; From c764f47329f45c00dbadcf875a7a0e9285919222 Mon Sep 17 00:00:00 2001 From: kaibocai <89094811+kaibocai@users.noreply.github.com> Date: Fri, 3 Nov 2023 16:02:05 -0500 Subject: [PATCH 4/4] add more unit tests for improving coverage Signed-off-by: kaibocai <89094811+kaibocai@users.noreply.github.com> --- .../client/NewWorkflowOptionTest.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 sdk-workflows/src/test/java/io/dapr/workflows/client/NewWorkflowOptionTest.java diff --git a/sdk-workflows/src/test/java/io/dapr/workflows/client/NewWorkflowOptionTest.java b/sdk-workflows/src/test/java/io/dapr/workflows/client/NewWorkflowOptionTest.java new file mode 100644 index 0000000000..78feb84f26 --- /dev/null +++ b/sdk-workflows/src/test/java/io/dapr/workflows/client/NewWorkflowOptionTest.java @@ -0,0 +1,28 @@ +package io.dapr.workflows.client; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.time.Instant; + +public class NewWorkflowOptionTest { + + @Test + void testNewWorkflowOption() { + NewWorkflowOption workflowOption = new NewWorkflowOption(); + String version = "v1"; + String instanceId = "123"; + Object input = new Object(); + Instant startTime = Instant.now(); + + workflowOption.setVersion(version) + .setInstanceId(instanceId) + .setInput(input) + .setStartTime(startTime); + + Assertions.assertEquals(version, workflowOption.getVersion()); + Assertions.assertEquals(instanceId, workflowOption.getInstanceId()); + Assertions.assertEquals(input, workflowOption.getInput()); + Assertions.assertEquals(startTime, workflowOption.getStartTime()); + } +}