mirror of https://github.com/dapr/java-sdk.git
Ensure Dapr workflows classes and interfaces have proper packages and visibility (#1176)
* Rename orchestrator and activity wrapper Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Move workflow activity to root package Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Move workflow context implementation to runtime Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Rename workflow activity context and add an interface Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Convert workflow abstract class to interface Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Move saga internals to runtime.saga package Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Tiny typo fix Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Adjusting the class names based on feedback Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Revert accidental JavaDocs commits Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Fixing some compilation errors Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> --------- Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> Co-authored-by: Artur Ciocanu <ciocanu@adobe.com>
This commit is contained in:
parent
69f24075f6
commit
380cda68f8
|
@ -1,7 +1,7 @@
|
||||||
package io.dapr.spring.boot.autoconfigure.client.workflows;
|
package io.dapr.spring.boot.autoconfigure.client.workflows;
|
||||||
|
|
||||||
import io.dapr.workflows.runtime.WorkflowActivity;
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
import io.dapr.workflows.runtime.WorkflowActivityContext;
|
import io.dapr.workflows.WorkflowActivityContext;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ import io.dapr.workflows.WorkflowStub;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
public class TestWorkflow extends Workflow {
|
public class TestWorkflow implements Workflow {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
package io.dapr.spring.workflows.config;
|
package io.dapr.spring.workflows.config;
|
||||||
|
|
||||||
import io.dapr.workflows.Workflow;
|
import io.dapr.workflows.Workflow;
|
||||||
import io.dapr.workflows.runtime.WorkflowActivity;
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
import io.dapr.workflows.runtime.WorkflowRuntime;
|
import io.dapr.workflows.runtime.WorkflowRuntime;
|
||||||
import io.dapr.workflows.runtime.WorkflowRuntimeBuilder;
|
import io.dapr.workflows.runtime.WorkflowRuntimeBuilder;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.ApplicationContextAware;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class DaprWorkflowExampleTest {
|
||||||
private static final String noTimeoutWorkflow = "DemoWorkflowNoTimeout";
|
private static final String noTimeoutWorkflow = "DemoWorkflowNoTimeout";
|
||||||
private static final String workflowDefaultId = "demo-workflow-123";
|
private static final String workflowDefaultId = "demo-workflow-123";
|
||||||
|
|
||||||
private class DemoWorkflow extends Workflow {
|
private class DemoWorkflow implements Workflow {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WorkflowStub create() {
|
public WorkflowStub create() {
|
||||||
|
|
|
@ -16,7 +16,7 @@ package io.dapr.examples.workflows.chain;
|
||||||
import io.dapr.workflows.Workflow;
|
import io.dapr.workflows.Workflow;
|
||||||
import io.dapr.workflows.WorkflowStub;
|
import io.dapr.workflows.WorkflowStub;
|
||||||
|
|
||||||
public class DemoChainWorkflow extends Workflow {
|
public class DemoChainWorkflow implements Workflow {
|
||||||
@Override
|
@Override
|
||||||
public WorkflowStub create() {
|
public WorkflowStub create() {
|
||||||
return ctx -> {
|
return ctx -> {
|
||||||
|
|
|
@ -13,8 +13,8 @@ limitations under the License.
|
||||||
|
|
||||||
package io.dapr.examples.workflows.chain;
|
package io.dapr.examples.workflows.chain;
|
||||||
|
|
||||||
import io.dapr.workflows.runtime.WorkflowActivity;
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
import io.dapr.workflows.runtime.WorkflowActivityContext;
|
import io.dapr.workflows.WorkflowActivityContext;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ package io.dapr.examples.workflows.childworkflow;
|
||||||
import io.dapr.workflows.Workflow;
|
import io.dapr.workflows.Workflow;
|
||||||
import io.dapr.workflows.WorkflowStub;
|
import io.dapr.workflows.WorkflowStub;
|
||||||
|
|
||||||
public class DemoChildWorkflow extends Workflow {
|
public class DemoChildWorkflow implements Workflow {
|
||||||
@Override
|
@Override
|
||||||
public WorkflowStub create() {
|
public WorkflowStub create() {
|
||||||
return ctx -> {
|
return ctx -> {
|
||||||
|
|
|
@ -16,7 +16,7 @@ package io.dapr.examples.workflows.childworkflow;
|
||||||
import io.dapr.workflows.Workflow;
|
import io.dapr.workflows.Workflow;
|
||||||
import io.dapr.workflows.WorkflowStub;
|
import io.dapr.workflows.WorkflowStub;
|
||||||
|
|
||||||
public class DemoWorkflow extends Workflow {
|
public class DemoWorkflow implements Workflow {
|
||||||
@Override
|
@Override
|
||||||
public WorkflowStub create() {
|
public WorkflowStub create() {
|
||||||
return ctx -> {
|
return ctx -> {
|
||||||
|
|
|
@ -13,8 +13,8 @@ limitations under the License.
|
||||||
|
|
||||||
package io.dapr.examples.workflows.childworkflow;
|
package io.dapr.examples.workflows.childworkflow;
|
||||||
|
|
||||||
import io.dapr.workflows.runtime.WorkflowActivity;
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
import io.dapr.workflows.runtime.WorkflowActivityContext;
|
import io.dapr.workflows.WorkflowActivityContext;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
|
@ -13,8 +13,8 @@ limitations under the License.
|
||||||
|
|
||||||
package io.dapr.examples.workflows.continueasnew;
|
package io.dapr.examples.workflows.continueasnew;
|
||||||
|
|
||||||
import io.dapr.workflows.runtime.WorkflowActivity;
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
import io.dapr.workflows.runtime.WorkflowActivityContext;
|
import io.dapr.workflows.WorkflowActivityContext;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ import io.dapr.workflows.WorkflowStub;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
|
||||||
public class DemoContinueAsNewWorkflow extends Workflow {
|
public class DemoContinueAsNewWorkflow implements Workflow {
|
||||||
/*
|
/*
|
||||||
Compared with a CRON schedule, this periodic workflow example will never overlap.
|
Compared with a CRON schedule, this periodic workflow example will never overlap.
|
||||||
For example, a CRON schedule that executes a cleanup every hour will execute it at 1:00, 2:00, 3:00 etc.
|
For example, a CRON schedule that executes a cleanup every hour will execute it at 1:00, 2:00, 3:00 etc.
|
||||||
|
|
|
@ -13,8 +13,8 @@ limitations under the License.
|
||||||
|
|
||||||
package io.dapr.examples.workflows.externalevent;
|
package io.dapr.examples.workflows.externalevent;
|
||||||
|
|
||||||
import io.dapr.workflows.runtime.WorkflowActivity;
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
import io.dapr.workflows.runtime.WorkflowActivityContext;
|
import io.dapr.workflows.WorkflowActivityContext;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ package io.dapr.examples.workflows.externalevent;
|
||||||
import io.dapr.workflows.Workflow;
|
import io.dapr.workflows.Workflow;
|
||||||
import io.dapr.workflows.WorkflowStub;
|
import io.dapr.workflows.WorkflowStub;
|
||||||
|
|
||||||
public class DemoExternalEventWorkflow extends Workflow {
|
public class DemoExternalEventWorkflow implements Workflow {
|
||||||
@Override
|
@Override
|
||||||
public WorkflowStub create() {
|
public WorkflowStub create() {
|
||||||
return ctx -> {
|
return ctx -> {
|
||||||
|
|
|
@ -13,8 +13,8 @@ limitations under the License.
|
||||||
|
|
||||||
package io.dapr.examples.workflows.externalevent;
|
package io.dapr.examples.workflows.externalevent;
|
||||||
|
|
||||||
import io.dapr.workflows.runtime.WorkflowActivity;
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
import io.dapr.workflows.runtime.WorkflowActivityContext;
|
import io.dapr.workflows.WorkflowActivityContext;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
|
@ -13,8 +13,8 @@ limitations under the License.
|
||||||
|
|
||||||
package io.dapr.examples.workflows.faninout;
|
package io.dapr.examples.workflows.faninout;
|
||||||
|
|
||||||
import io.dapr.workflows.runtime.WorkflowActivity;
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
import io.dapr.workflows.runtime.WorkflowActivityContext;
|
import io.dapr.workflows.WorkflowActivityContext;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ import io.dapr.workflows.WorkflowStub;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class DemoFanInOutWorkflow extends Workflow {
|
public class DemoFanInOutWorkflow implements Workflow {
|
||||||
@Override
|
@Override
|
||||||
public WorkflowStub create() {
|
public WorkflowStub create() {
|
||||||
return ctx -> {
|
return ctx -> {
|
||||||
|
|
|
@ -13,8 +13,8 @@ limitations under the License.
|
||||||
|
|
||||||
package io.dapr.it.testcontainers;
|
package io.dapr.it.testcontainers;
|
||||||
|
|
||||||
import io.dapr.workflows.runtime.WorkflowActivity;
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
import io.dapr.workflows.runtime.WorkflowActivityContext;
|
import io.dapr.workflows.WorkflowActivityContext;
|
||||||
|
|
||||||
public class FirstActivity implements WorkflowActivity {
|
public class FirstActivity implements WorkflowActivity {
|
||||||
|
|
||||||
|
|
|
@ -13,8 +13,8 @@ limitations under the License.
|
||||||
|
|
||||||
package io.dapr.it.testcontainers;
|
package io.dapr.it.testcontainers;
|
||||||
|
|
||||||
import io.dapr.workflows.runtime.WorkflowActivity;
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
import io.dapr.workflows.runtime.WorkflowActivityContext;
|
import io.dapr.workflows.WorkflowActivityContext;
|
||||||
|
|
||||||
public class SecondActivity implements WorkflowActivity {
|
public class SecondActivity implements WorkflowActivity {
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
|
||||||
public class TestWorkflow extends Workflow {
|
public class TestWorkflow implements Workflow {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WorkflowStub create() {
|
public WorkflowStub create() {
|
||||||
|
|
|
@ -21,16 +21,13 @@ import io.dapr.workflows.saga.SagaOption;
|
||||||
/**
|
/**
|
||||||
* Common interface for workflow implementations.
|
* Common interface for workflow implementations.
|
||||||
*/
|
*/
|
||||||
public abstract class Workflow {
|
public interface Workflow {
|
||||||
public Workflow() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the workflow logic.
|
* Executes the workflow logic.
|
||||||
*
|
*
|
||||||
* @return A WorkflowStub.
|
* @return A WorkflowStub.
|
||||||
*/
|
*/
|
||||||
public abstract WorkflowStub create();
|
WorkflowStub create();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the workflow logic.
|
* Executes the workflow logic.
|
||||||
|
@ -39,7 +36,7 @@ public abstract class Workflow {
|
||||||
* getting information about the current
|
* getting information about the current
|
||||||
* workflow instance.
|
* workflow instance.
|
||||||
*/
|
*/
|
||||||
public void run(WorkflowContext ctx) {
|
default void run(WorkflowContext ctx) {
|
||||||
WorkflowStub stub = this.create();
|
WorkflowStub stub = this.create();
|
||||||
|
|
||||||
if (!this.isSagaEnabled()) {
|
if (!this.isSagaEnabled()) {
|
||||||
|
@ -68,7 +65,7 @@ public abstract class Workflow {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSagaEnabled() {
|
default boolean isSagaEnabled() {
|
||||||
return this.getSagaOption() != null;
|
return this.getSagaOption() != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,7 +74,7 @@ public abstract class Workflow {
|
||||||
*
|
*
|
||||||
* @return saga configuration
|
* @return saga configuration
|
||||||
*/
|
*/
|
||||||
public SagaOption getSagaOption() {
|
default SagaOption getSagaOption() {
|
||||||
// by default, saga is disabled
|
// by default, saga is disabled
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package io.dapr.workflows.runtime;
|
package io.dapr.workflows;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Common interface for task activity implementations.
|
* Common interface for task activity implementations.
|
|
@ -0,0 +1,20 @@
|
||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
public interface WorkflowActivityContext {
|
||||||
|
String getName();
|
||||||
|
|
||||||
|
<T> T getInput(Class<T> targetType);
|
||||||
|
}
|
|
@ -14,11 +14,12 @@ limitations under the License.
|
||||||
package io.dapr.workflows.runtime;
|
package io.dapr.workflows.runtime;
|
||||||
|
|
||||||
import com.microsoft.durabletask.TaskActivityContext;
|
import com.microsoft.durabletask.TaskActivityContext;
|
||||||
|
import io.dapr.workflows.WorkflowActivityContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for Durable Task Framework {@link TaskActivityContext}.
|
* Wrapper for Durable Task Framework {@link TaskActivityContext}.
|
||||||
*/
|
*/
|
||||||
public class WorkflowActivityContext implements TaskActivityContext {
|
class DefaultWorkflowActivityContext implements WorkflowActivityContext {
|
||||||
private final TaskActivityContext innerContext;
|
private final TaskActivityContext innerContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,7 +28,7 @@ public class WorkflowActivityContext implements TaskActivityContext {
|
||||||
* @param context TaskActivityContext
|
* @param context TaskActivityContext
|
||||||
* @throws IllegalArgumentException if context is null
|
* @throws IllegalArgumentException if context is null
|
||||||
*/
|
*/
|
||||||
public WorkflowActivityContext(TaskActivityContext context) throws IllegalArgumentException {
|
public DefaultWorkflowActivityContext(TaskActivityContext context) throws IllegalArgumentException {
|
||||||
if (context == null) {
|
if (context == null) {
|
||||||
throw new IllegalArgumentException("Context cannot be null");
|
throw new IllegalArgumentException("Context cannot be null");
|
||||||
}
|
}
|
||||||
|
@ -39,6 +40,7 @@ public class WorkflowActivityContext implements TaskActivityContext {
|
||||||
*
|
*
|
||||||
* @return the name of the current activity
|
* @return the name of the current activity
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return this.innerContext.getName();
|
return this.innerContext.getName();
|
||||||
}
|
}
|
||||||
|
@ -50,6 +52,7 @@ public class WorkflowActivityContext implements TaskActivityContext {
|
||||||
* @param targetType targetType of the input
|
* @param targetType targetType of the input
|
||||||
* @return the input of the current activity
|
* @return the input of the current activity
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public <T> T getInput(Class<T> targetType) {
|
public <T> T getInput(Class<T> targetType) {
|
||||||
return this.innerContext.getInput(targetType);
|
return this.innerContext.getInput(targetType);
|
||||||
}
|
}
|
|
@ -11,14 +11,15 @@
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package io.dapr.workflows;
|
package io.dapr.workflows.runtime;
|
||||||
|
|
||||||
import com.microsoft.durabletask.CompositeTaskFailedException;
|
import com.microsoft.durabletask.CompositeTaskFailedException;
|
||||||
import com.microsoft.durabletask.Task;
|
import com.microsoft.durabletask.Task;
|
||||||
import com.microsoft.durabletask.TaskCanceledException;
|
import com.microsoft.durabletask.TaskCanceledException;
|
||||||
import com.microsoft.durabletask.TaskOptions;
|
import com.microsoft.durabletask.TaskOptions;
|
||||||
import com.microsoft.durabletask.TaskOrchestrationContext;
|
import com.microsoft.durabletask.TaskOrchestrationContext;
|
||||||
import io.dapr.workflows.saga.DaprSagaContextImpl;
|
import io.dapr.workflows.WorkflowContext;
|
||||||
|
import io.dapr.workflows.runtime.saga.DefaultSagaContext;
|
||||||
import io.dapr.workflows.saga.Saga;
|
import io.dapr.workflows.saga.Saga;
|
||||||
import io.dapr.workflows.saga.SagaContext;
|
import io.dapr.workflows.saga.SagaContext;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -32,7 +33,7 @@ import java.time.Instant;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class DaprWorkflowContextImpl implements WorkflowContext {
|
public class DefaultWorkflowContext implements WorkflowContext {
|
||||||
private final TaskOrchestrationContext innerContext;
|
private final TaskOrchestrationContext innerContext;
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
private final Saga saga;
|
private final Saga saga;
|
||||||
|
@ -43,7 +44,7 @@ public class DaprWorkflowContextImpl implements WorkflowContext {
|
||||||
* @param context TaskOrchestrationContext
|
* @param context TaskOrchestrationContext
|
||||||
* @throws IllegalArgumentException if context is null
|
* @throws IllegalArgumentException if context is null
|
||||||
*/
|
*/
|
||||||
public DaprWorkflowContextImpl(TaskOrchestrationContext context) throws IllegalArgumentException {
|
public DefaultWorkflowContext(TaskOrchestrationContext context) throws IllegalArgumentException {
|
||||||
this(context, LoggerFactory.getLogger(WorkflowContext.class));
|
this(context, LoggerFactory.getLogger(WorkflowContext.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,11 +55,11 @@ public class DaprWorkflowContextImpl implements WorkflowContext {
|
||||||
* @param logger Logger
|
* @param logger Logger
|
||||||
* @throws IllegalArgumentException if context or logger is null
|
* @throws IllegalArgumentException if context or logger is null
|
||||||
*/
|
*/
|
||||||
public DaprWorkflowContextImpl(TaskOrchestrationContext context, Logger logger) throws IllegalArgumentException {
|
public DefaultWorkflowContext(TaskOrchestrationContext context, Logger logger) throws IllegalArgumentException {
|
||||||
this(context, logger, null);
|
this(context, logger, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DaprWorkflowContextImpl(TaskOrchestrationContext context, Saga saga) throws IllegalArgumentException {
|
public DefaultWorkflowContext(TaskOrchestrationContext context, Saga saga) throws IllegalArgumentException {
|
||||||
this(context, LoggerFactory.getLogger(WorkflowContext.class), saga);
|
this(context, LoggerFactory.getLogger(WorkflowContext.class), saga);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +71,7 @@ public class DaprWorkflowContextImpl implements WorkflowContext {
|
||||||
* @param saga saga object, if null, saga is disabled
|
* @param saga saga object, if null, saga is disabled
|
||||||
* @throws IllegalArgumentException if context or logger is null
|
* @throws IllegalArgumentException if context or logger is null
|
||||||
*/
|
*/
|
||||||
public DaprWorkflowContextImpl(TaskOrchestrationContext context, Logger logger, Saga saga)
|
public DefaultWorkflowContext(TaskOrchestrationContext context, Logger logger, Saga saga)
|
||||||
throws IllegalArgumentException {
|
throws IllegalArgumentException {
|
||||||
if (context == null) {
|
if (context == null) {
|
||||||
throw new IllegalArgumentException("Context cannot be null");
|
throw new IllegalArgumentException("Context cannot be null");
|
||||||
|
@ -248,6 +249,6 @@ public class DaprWorkflowContextImpl implements WorkflowContext {
|
||||||
throw new UnsupportedOperationException("Saga is not enabled");
|
throw new UnsupportedOperationException("Saga is not enabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new DaprSagaContextImpl(this.saga, this);
|
return new DefaultSagaContext(this.saga, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -15,6 +15,7 @@ package io.dapr.workflows.runtime;
|
||||||
|
|
||||||
import com.microsoft.durabletask.TaskActivity;
|
import com.microsoft.durabletask.TaskActivity;
|
||||||
import com.microsoft.durabletask.TaskActivityFactory;
|
import com.microsoft.durabletask.TaskActivityFactory;
|
||||||
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
@ -22,16 +23,16 @@ import java.lang.reflect.InvocationTargetException;
|
||||||
/**
|
/**
|
||||||
* Wrapper for Durable Task Framework task activity factory.
|
* Wrapper for Durable Task Framework task activity factory.
|
||||||
*/
|
*/
|
||||||
public class ActivityWrapper<T extends WorkflowActivity> implements TaskActivityFactory {
|
public class WorkflowActivityWrapper<T extends WorkflowActivity> implements TaskActivityFactory {
|
||||||
private final Constructor<T> activityConstructor;
|
private final Constructor<T> activityConstructor;
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for ActivityWrapper.
|
* Constructor for WorkflowActivityWrapper.
|
||||||
*
|
*
|
||||||
* @param clazz Class of the activity to wrap.
|
* @param clazz Class of the activity to wrap.
|
||||||
*/
|
*/
|
||||||
public ActivityWrapper(Class<T> clazz) {
|
public WorkflowActivityWrapper(Class<T> clazz) {
|
||||||
this.name = clazz.getCanonicalName();
|
this.name = clazz.getCanonicalName();
|
||||||
try {
|
try {
|
||||||
this.activityConstructor = clazz.getDeclaredConstructor();
|
this.activityConstructor = clazz.getDeclaredConstructor();
|
||||||
|
@ -62,7 +63,7 @@ public class ActivityWrapper<T extends WorkflowActivity> implements TaskActivity
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
result = activity.run(new WorkflowActivityContext(ctx));
|
result = activity.run(new DefaultWorkflowActivityContext(ctx));
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
}
|
}
|
|
@ -17,6 +17,7 @@ import com.microsoft.durabletask.DurableTaskGrpcWorkerBuilder;
|
||||||
import io.dapr.config.Properties;
|
import io.dapr.config.Properties;
|
||||||
import io.dapr.utils.NetworkUtils;
|
import io.dapr.utils.NetworkUtils;
|
||||||
import io.dapr.workflows.Workflow;
|
import io.dapr.workflows.Workflow;
|
||||||
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
import io.dapr.workflows.internal.ApiTokenClientInterceptor;
|
import io.dapr.workflows.internal.ApiTokenClientInterceptor;
|
||||||
import io.grpc.ClientInterceptor;
|
import io.grpc.ClientInterceptor;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
|
@ -91,7 +92,7 @@ public class WorkflowRuntimeBuilder {
|
||||||
* @return the WorkflowRuntimeBuilder
|
* @return the WorkflowRuntimeBuilder
|
||||||
*/
|
*/
|
||||||
public <T extends Workflow> WorkflowRuntimeBuilder registerWorkflow(Class<T> clazz) {
|
public <T extends Workflow> WorkflowRuntimeBuilder registerWorkflow(Class<T> clazz) {
|
||||||
this.builder.addOrchestration(new OrchestratorWrapper<>(clazz));
|
this.builder.addOrchestration(new WorkflowWrapper<>(clazz));
|
||||||
this.workflowSet.add(clazz.getCanonicalName());
|
this.workflowSet.add(clazz.getCanonicalName());
|
||||||
this.workflows.add(clazz.getSimpleName());
|
this.workflows.add(clazz.getSimpleName());
|
||||||
|
|
||||||
|
@ -108,7 +109,7 @@ public class WorkflowRuntimeBuilder {
|
||||||
* @return the WorkflowRuntimeBuilder
|
* @return the WorkflowRuntimeBuilder
|
||||||
*/
|
*/
|
||||||
public <T extends WorkflowActivity> WorkflowRuntimeBuilder registerActivity(Class<T> clazz) {
|
public <T extends WorkflowActivity> WorkflowRuntimeBuilder registerActivity(Class<T> clazz) {
|
||||||
this.builder.addActivity(new ActivityWrapper<>(clazz));
|
this.builder.addActivity(new WorkflowActivityWrapper<>(clazz));
|
||||||
this.activitySet.add(clazz.getCanonicalName());
|
this.activitySet.add(clazz.getCanonicalName());
|
||||||
this.activities.add(clazz.getSimpleName());
|
this.activities.add(clazz.getSimpleName());
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,6 @@ package io.dapr.workflows.runtime;
|
||||||
|
|
||||||
import com.microsoft.durabletask.TaskOrchestration;
|
import com.microsoft.durabletask.TaskOrchestration;
|
||||||
import com.microsoft.durabletask.TaskOrchestrationFactory;
|
import com.microsoft.durabletask.TaskOrchestrationFactory;
|
||||||
import io.dapr.workflows.DaprWorkflowContextImpl;
|
|
||||||
import io.dapr.workflows.Workflow;
|
import io.dapr.workflows.Workflow;
|
||||||
import io.dapr.workflows.saga.Saga;
|
import io.dapr.workflows.saga.Saga;
|
||||||
|
|
||||||
|
@ -25,11 +24,11 @@ import java.lang.reflect.InvocationTargetException;
|
||||||
/**
|
/**
|
||||||
* Wrapper for Durable Task Framework orchestration factory.
|
* Wrapper for Durable Task Framework orchestration factory.
|
||||||
*/
|
*/
|
||||||
class OrchestratorWrapper<T extends Workflow> implements TaskOrchestrationFactory {
|
class WorkflowWrapper<T extends Workflow> implements TaskOrchestrationFactory {
|
||||||
private final Constructor<T> workflowConstructor;
|
private final Constructor<T> workflowConstructor;
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
public OrchestratorWrapper(Class<T> clazz) {
|
public WorkflowWrapper(Class<T> clazz) {
|
||||||
this.name = clazz.getCanonicalName();
|
this.name = clazz.getCanonicalName();
|
||||||
try {
|
try {
|
||||||
this.workflowConstructor = clazz.getDeclaredConstructor();
|
this.workflowConstructor = clazz.getDeclaredConstructor();
|
||||||
|
@ -59,9 +58,9 @@ class OrchestratorWrapper<T extends Workflow> implements TaskOrchestrationFactor
|
||||||
|
|
||||||
if (workflow.getSagaOption() != null) {
|
if (workflow.getSagaOption() != null) {
|
||||||
Saga saga = new Saga(workflow.getSagaOption());
|
Saga saga = new Saga(workflow.getSagaOption());
|
||||||
workflow.run(new DaprWorkflowContextImpl(ctx, saga));
|
workflow.run(new DefaultWorkflowContext(ctx, saga));
|
||||||
} else {
|
} else {
|
||||||
workflow.run(new DaprWorkflowContextImpl(ctx));
|
workflow.run(new DefaultWorkflowContext(ctx));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -11,14 +11,16 @@
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package io.dapr.workflows.saga;
|
package io.dapr.workflows.runtime.saga;
|
||||||
|
|
||||||
import io.dapr.workflows.WorkflowContext;
|
import io.dapr.workflows.WorkflowContext;
|
||||||
|
import io.dapr.workflows.saga.Saga;
|
||||||
|
import io.dapr.workflows.saga.SagaContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dapr Saga Context implementation.
|
* Dapr Saga Context implementation.
|
||||||
*/
|
*/
|
||||||
public class DaprSagaContextImpl implements SagaContext {
|
public class DefaultSagaContext implements SagaContext {
|
||||||
|
|
||||||
private final Saga saga;
|
private final Saga saga;
|
||||||
private final WorkflowContext workflowContext;
|
private final WorkflowContext workflowContext;
|
||||||
|
@ -30,7 +32,7 @@ public class DaprSagaContextImpl implements SagaContext {
|
||||||
* @param workflowContext Workflow context.
|
* @param workflowContext Workflow context.
|
||||||
* @throws IllegalArgumentException if saga or workflowContext is null.
|
* @throws IllegalArgumentException if saga or workflowContext is null.
|
||||||
*/
|
*/
|
||||||
public DaprSagaContextImpl(Saga saga, WorkflowContext workflowContext) {
|
public DefaultSagaContext(Saga saga, WorkflowContext workflowContext) {
|
||||||
if (saga == null) {
|
if (saga == null) {
|
||||||
throw new IllegalArgumentException("Saga should not be null");
|
throw new IllegalArgumentException("Saga should not be null");
|
||||||
}
|
}
|
|
@ -18,24 +18,24 @@ import com.microsoft.durabletask.TaskOptions;
|
||||||
/**
|
/**
|
||||||
* Information for a compensation activity.
|
* Information for a compensation activity.
|
||||||
*/
|
*/
|
||||||
class CompensatationInformation {
|
class CompensationInformation {
|
||||||
private final String compensatationActivityClassName;
|
private final String compensationActivityClassName;
|
||||||
private final Object compensatationActivityInput;
|
private final Object compensationActivityInput;
|
||||||
private final TaskOptions taskOptions;
|
private final TaskOptions taskOptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for a compensation information.
|
* Constructor for a compensation information.
|
||||||
*
|
*
|
||||||
* @param compensatationActivityClassName Class name of the activity to do
|
* @param compensationActivityClassName Class name of the activity to do
|
||||||
* compensatation.
|
* compensation.
|
||||||
* @param compensatationActivityInput Input of the activity to do
|
* @param compensationActivityInput Input of the activity to do
|
||||||
* compensatation.
|
* compensation.
|
||||||
* @param taskOptions task options to set retry strategy
|
* @param taskOptions task options to set retry strategy
|
||||||
*/
|
*/
|
||||||
public CompensatationInformation(String compensatationActivityClassName,
|
public CompensationInformation(String compensationActivityClassName,
|
||||||
Object compensatationActivityInput, TaskOptions taskOptions) {
|
Object compensationActivityInput, TaskOptions taskOptions) {
|
||||||
this.compensatationActivityClassName = compensatationActivityClassName;
|
this.compensationActivityClassName = compensationActivityClassName;
|
||||||
this.compensatationActivityInput = compensatationActivityInput;
|
this.compensationActivityInput = compensationActivityInput;
|
||||||
this.taskOptions = taskOptions;
|
this.taskOptions = taskOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,8 +44,8 @@ class CompensatationInformation {
|
||||||
*
|
*
|
||||||
* @return the class name of the activity.
|
* @return the class name of the activity.
|
||||||
*/
|
*/
|
||||||
public String getCompensatationActivityClassName() {
|
public String getCompensationActivityClassName() {
|
||||||
return compensatationActivityClassName;
|
return compensationActivityClassName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -53,8 +53,8 @@ class CompensatationInformation {
|
||||||
*
|
*
|
||||||
* @return the input of the activity.
|
* @return the input of the activity.
|
||||||
*/
|
*/
|
||||||
public Object getCompensatationActivityInput() {
|
public Object getCompensationActivityInput() {
|
||||||
return compensatationActivityInput;
|
return compensationActivityInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
|
@ -24,7 +24,7 @@ import java.util.List;
|
||||||
|
|
||||||
public final class Saga {
|
public final class Saga {
|
||||||
private final SagaOption option;
|
private final SagaOption option;
|
||||||
private final List<CompensatationInformation> compensationActivities = new ArrayList<>();
|
private final List<CompensationInformation> compensationActivities = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build up a Saga with its options.
|
* Build up a Saga with its options.
|
||||||
|
@ -59,7 +59,7 @@ public final class Saga {
|
||||||
if (activityClassName == null || activityClassName.isEmpty()) {
|
if (activityClassName == null || activityClassName.isEmpty()) {
|
||||||
throw new IllegalArgumentException("activityClassName is required and should not be null or empty.");
|
throw new IllegalArgumentException("activityClassName is required and should not be null or empty.");
|
||||||
}
|
}
|
||||||
this.compensationActivities.add(new CompensatationInformation(activityClassName, activityInput, taskOptions));
|
this.compensationActivities.add(new CompensationInformation(activityClassName, activityInput, taskOptions));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,7 +69,7 @@ public final class Saga {
|
||||||
*/
|
*/
|
||||||
public void compensate(WorkflowContext ctx) {
|
public void compensate(WorkflowContext ctx) {
|
||||||
// Check if parallel compensation is enabled
|
// Check if parallel compensation is enabled
|
||||||
// Specical case: when parallel compensation is enabled and there is only one
|
// Special case: when parallel compensation is enabled and there is only one
|
||||||
// compensation, we still
|
// compensation, we still
|
||||||
// compensate sequentially.
|
// compensate sequentially.
|
||||||
if (option.isParallelCompensation() && compensationActivities.size() > 1) {
|
if (option.isParallelCompensation() && compensationActivities.size() > 1) {
|
||||||
|
@ -81,7 +81,7 @@ public final class Saga {
|
||||||
|
|
||||||
private void compensateInParallel(WorkflowContext ctx) {
|
private void compensateInParallel(WorkflowContext ctx) {
|
||||||
List<Task<Void>> tasks = new ArrayList<>(compensationActivities.size());
|
List<Task<Void>> tasks = new ArrayList<>(compensationActivities.size());
|
||||||
for (CompensatationInformation compensationActivity : compensationActivities) {
|
for (CompensationInformation compensationActivity : compensationActivities) {
|
||||||
Task<Void> task = executeCompensateActivity(ctx, compensationActivity);
|
Task<Void> task = executeCompensateActivity(ctx, compensationActivity);
|
||||||
tasks.add(task);
|
tasks.add(task);
|
||||||
}
|
}
|
||||||
|
@ -96,7 +96,7 @@ public final class Saga {
|
||||||
private void compensateSequentially(WorkflowContext ctx) {
|
private void compensateSequentially(WorkflowContext ctx) {
|
||||||
SagaCompensationException sagaException = null;
|
SagaCompensationException sagaException = null;
|
||||||
for (int i = compensationActivities.size() - 1; i >= 0; i--) {
|
for (int i = compensationActivities.size() - 1; i >= 0; i--) {
|
||||||
String activityClassName = compensationActivities.get(i).getCompensatationActivityClassName();
|
String activityClassName = compensationActivities.get(i).getCompensationActivityClassName();
|
||||||
try {
|
try {
|
||||||
executeCompensateActivity(ctx, compensationActivities.get(i)).await();
|
executeCompensateActivity(ctx, compensationActivities.get(i)).await();
|
||||||
} catch (OrchestratorBlockedException | ContinueAsNewInterruption e) {
|
} catch (OrchestratorBlockedException | ContinueAsNewInterruption e) {
|
||||||
|
@ -104,8 +104,7 @@ public final class Saga {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (sagaException == null) {
|
if (sagaException == null) {
|
||||||
sagaException = new SagaCompensationException(
|
sagaException = new SagaCompensationException(
|
||||||
"Exception in saga compensatation: activity=" + activityClassName, e);
|
"Exception in saga compensation: activity=" + activityClassName, e);
|
||||||
;
|
|
||||||
} else {
|
} else {
|
||||||
sagaException.addSuppressed(e);
|
sagaException.addSuppressed(e);
|
||||||
}
|
}
|
||||||
|
@ -121,10 +120,10 @@ public final class Saga {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task<Void> executeCompensateActivity(WorkflowContext ctx, CompensatationInformation info)
|
private Task<Void> executeCompensateActivity(WorkflowContext ctx, CompensationInformation info)
|
||||||
throws SagaCompensationException {
|
throws SagaCompensationException {
|
||||||
String activityClassName = info.getCompensatationActivityClassName();
|
String activityClassName = info.getCompensationActivityClassName();
|
||||||
return ctx.callActivity(activityClassName, info.getCompensatationActivityInput(),
|
return ctx.callActivity(activityClassName, info.getCompensationActivityInput(),
|
||||||
info.getTaskOptions());
|
info.getTaskOptions());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,8 +92,8 @@ public final class SagaOption {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build Saga optiion.
|
* Build Saga option.
|
||||||
* @return Saga optiion
|
* @return Saga option
|
||||||
*/
|
*/
|
||||||
public SagaOption build() {
|
public SagaOption build() {
|
||||||
return new SagaOption(this.parallelCompensation, this.maxParallelThread, this.continueWithError);
|
return new SagaOption(this.parallelCompensation, this.maxParallelThread, this.continueWithError);
|
||||||
|
|
|
@ -20,6 +20,7 @@ import com.microsoft.durabletask.TaskCanceledException;
|
||||||
import com.microsoft.durabletask.TaskOptions;
|
import com.microsoft.durabletask.TaskOptions;
|
||||||
import com.microsoft.durabletask.TaskOrchestrationContext;
|
import com.microsoft.durabletask.TaskOrchestrationContext;
|
||||||
|
|
||||||
|
import io.dapr.workflows.runtime.DefaultWorkflowContext;
|
||||||
import io.dapr.workflows.saga.Saga;
|
import io.dapr.workflows.saga.Saga;
|
||||||
import io.dapr.workflows.saga.SagaContext;
|
import io.dapr.workflows.saga.SagaContext;
|
||||||
|
|
||||||
|
@ -43,15 +44,15 @@ import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
public class DaprWorkflowContextImplTest {
|
public class DefaultWorkflowContextTest {
|
||||||
private DaprWorkflowContextImpl context;
|
private DefaultWorkflowContext context;
|
||||||
private TaskOrchestrationContext mockInnerContext;
|
private TaskOrchestrationContext mockInnerContext;
|
||||||
private WorkflowContext testWorkflowContext;
|
private WorkflowContext testWorkflowContext;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
mockInnerContext = mock(TaskOrchestrationContext.class);
|
mockInnerContext = mock(TaskOrchestrationContext.class);
|
||||||
context = new DaprWorkflowContextImpl(mockInnerContext);
|
context = new DefaultWorkflowContext(mockInnerContext);
|
||||||
testWorkflowContext = new WorkflowContext() {
|
testWorkflowContext = new WorkflowContext() {
|
||||||
@Override
|
@Override
|
||||||
public Logger getLogger() {
|
public Logger getLogger() {
|
||||||
|
@ -190,13 +191,13 @@ public class DaprWorkflowContextImplTest {
|
||||||
@Test
|
@Test
|
||||||
public void DaprWorkflowContextWithEmptyInnerContext() {
|
public void DaprWorkflowContextWithEmptyInnerContext() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> {
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
context = new DaprWorkflowContextImpl(mockInnerContext, (Logger)null);
|
context = new DefaultWorkflowContext(mockInnerContext, (Logger)null);
|
||||||
}); }
|
}); }
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void DaprWorkflowContextWithEmptyLogger() {
|
public void DaprWorkflowContextWithEmptyLogger() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> {
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
context = new DaprWorkflowContextImpl(null, (Logger)null);
|
context = new DefaultWorkflowContext(null, (Logger)null);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,7 +217,7 @@ public class DaprWorkflowContextImplTest {
|
||||||
public void getLoggerReplayingTest() {
|
public void getLoggerReplayingTest() {
|
||||||
Logger mockLogger = mock(Logger.class);
|
Logger mockLogger = mock(Logger.class);
|
||||||
when(context.isReplaying()).thenReturn(true);
|
when(context.isReplaying()).thenReturn(true);
|
||||||
DaprWorkflowContextImpl testContext = new DaprWorkflowContextImpl(mockInnerContext, mockLogger);
|
DefaultWorkflowContext testContext = new DefaultWorkflowContext(mockInnerContext, mockLogger);
|
||||||
|
|
||||||
String expectedArg = "test print";
|
String expectedArg = "test print";
|
||||||
testContext.getLogger().info(expectedArg);
|
testContext.getLogger().info(expectedArg);
|
||||||
|
@ -228,7 +229,7 @@ public class DaprWorkflowContextImplTest {
|
||||||
public void getLoggerFirstTimeTest() {
|
public void getLoggerFirstTimeTest() {
|
||||||
Logger mockLogger = mock(Logger.class);
|
Logger mockLogger = mock(Logger.class);
|
||||||
when(context.isReplaying()).thenReturn(false);
|
when(context.isReplaying()).thenReturn(false);
|
||||||
DaprWorkflowContextImpl testContext = new DaprWorkflowContextImpl(mockInnerContext, mockLogger);
|
DefaultWorkflowContext testContext = new DefaultWorkflowContext(mockInnerContext, mockLogger);
|
||||||
|
|
||||||
String expectedArg = "test print";
|
String expectedArg = "test print";
|
||||||
testContext.getLogger().info(expectedArg);
|
testContext.getLogger().info(expectedArg);
|
||||||
|
@ -322,7 +323,7 @@ public class DaprWorkflowContextImplTest {
|
||||||
@Test
|
@Test
|
||||||
public void getSagaContextTest_sagaEnabled() {
|
public void getSagaContextTest_sagaEnabled() {
|
||||||
Saga saga = mock(Saga.class);
|
Saga saga = mock(Saga.class);
|
||||||
WorkflowContext context = new DaprWorkflowContextImpl(mockInnerContext, saga);
|
WorkflowContext context = new DefaultWorkflowContext(mockInnerContext, saga);
|
||||||
|
|
||||||
SagaContext sagaContext = context.getSagaContext();
|
SagaContext sagaContext = context.getSagaContext();
|
||||||
assertNotNull("SagaContext should not be null", sagaContext);
|
assertNotNull("SagaContext should not be null", sagaContext);
|
||||||
|
@ -330,7 +331,7 @@ public class DaprWorkflowContextImplTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getSagaContextTest_sagaDisabled() {
|
public void getSagaContextTest_sagaDisabled() {
|
||||||
WorkflowContext context = new DaprWorkflowContextImpl(mockInnerContext);
|
WorkflowContext context = new DefaultWorkflowContext(mockInnerContext);
|
||||||
assertThrows(UnsupportedOperationException.class, () -> {
|
assertThrows(UnsupportedOperationException.class, () -> {
|
||||||
context.getSagaContext();
|
context.getSagaContext();
|
||||||
});
|
});
|
|
@ -162,7 +162,7 @@ public class WorkflowTest {
|
||||||
verify(sagaContext, times(1)).compensate();
|
verify(sagaContext, times(1)).compensate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class WorkflowWithoutSaga extends Workflow {
|
public static class WorkflowWithoutSaga implements Workflow {
|
||||||
private final WorkflowStub stub;
|
private final WorkflowStub stub;
|
||||||
|
|
||||||
public WorkflowWithoutSaga(WorkflowStub stub) {
|
public WorkflowWithoutSaga(WorkflowStub stub) {
|
||||||
|
@ -175,7 +175,7 @@ public class WorkflowTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class WorkflowWithSaga extends Workflow {
|
public static class WorkflowWithSaga implements Workflow {
|
||||||
private final WorkflowStub stub;
|
private final WorkflowStub stub;
|
||||||
|
|
||||||
public WorkflowWithSaga(WorkflowStub stub) {
|
public WorkflowWithSaga(WorkflowStub stub) {
|
||||||
|
|
|
@ -44,7 +44,7 @@ public class DaprWorkflowClientTest {
|
||||||
private DurableTaskClient mockInnerClient;
|
private DurableTaskClient mockInnerClient;
|
||||||
private ManagedChannel mockGrpcChannel;
|
private ManagedChannel mockGrpcChannel;
|
||||||
|
|
||||||
public static class TestWorkflow extends Workflow {
|
public static class TestWorkflow implements Workflow {
|
||||||
@Override
|
@Override
|
||||||
public WorkflowStub create() {
|
public WorkflowStub create() {
|
||||||
return WorkflowContext::getInstanceId;
|
return WorkflowContext::getInstanceId;
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package io.dapr.workflows.runtime;
|
package io.dapr.workflows.runtime;
|
||||||
|
|
||||||
import com.microsoft.durabletask.TaskActivityContext;
|
import com.microsoft.durabletask.TaskActivityContext;
|
||||||
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
|
import io.dapr.workflows.WorkflowActivityContext;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -10,7 +12,7 @@ import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
|
||||||
public class ActivityWrapperTest {
|
public class WorkflowActivityWrapperTest {
|
||||||
public static class TestActivity implements WorkflowActivity {
|
public static class TestActivity implements WorkflowActivity {
|
||||||
@Override
|
@Override
|
||||||
public Object run(WorkflowActivityContext ctx) {
|
public Object run(WorkflowActivityContext ctx) {
|
||||||
|
@ -21,10 +23,10 @@ public class ActivityWrapperTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getName() throws NoSuchMethodException {
|
public void getName() throws NoSuchMethodException {
|
||||||
ActivityWrapper<ActivityWrapperTest.TestActivity> wrapper = new ActivityWrapper<>(
|
WorkflowActivityWrapper<TestActivity> wrapper = new WorkflowActivityWrapper<>(
|
||||||
ActivityWrapperTest.TestActivity.class);
|
WorkflowActivityWrapperTest.TestActivity.class);
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
"io.dapr.workflows.runtime.ActivityWrapperTest.TestActivity",
|
"io.dapr.workflows.runtime.WorkflowActivityWrapperTest.TestActivity",
|
||||||
wrapper.getName()
|
wrapper.getName()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -32,8 +34,8 @@ public class ActivityWrapperTest {
|
||||||
@Test
|
@Test
|
||||||
public void createWithClass() throws NoSuchMethodException {
|
public void createWithClass() throws NoSuchMethodException {
|
||||||
TaskActivityContext mockContext = mock(TaskActivityContext.class);
|
TaskActivityContext mockContext = mock(TaskActivityContext.class);
|
||||||
ActivityWrapper<ActivityWrapperTest.TestActivity> wrapper = new ActivityWrapper<>(
|
WorkflowActivityWrapper<TestActivity> wrapper = new WorkflowActivityWrapper<>(
|
||||||
ActivityWrapperTest.TestActivity.class);
|
WorkflowActivityWrapperTest.TestActivity.class);
|
||||||
when(mockContext.getInput(String.class)).thenReturn("Hello");
|
when(mockContext.getInput(String.class)).thenReturn("Hello");
|
||||||
when(mockContext.getName()).thenReturn("TestActivityContext");
|
when(mockContext.getName()).thenReturn("TestActivityContext");
|
||||||
Object result = wrapper.create().run(mockContext);
|
Object result = wrapper.create().run(mockContext);
|
|
@ -14,6 +14,8 @@ package io.dapr.workflows.runtime;
|
||||||
|
|
||||||
|
|
||||||
import io.dapr.workflows.Workflow;
|
import io.dapr.workflows.Workflow;
|
||||||
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
|
import io.dapr.workflows.WorkflowActivityContext;
|
||||||
import io.dapr.workflows.WorkflowStub;
|
import io.dapr.workflows.WorkflowStub;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
@ -25,7 +27,7 @@ import java.io.ByteArrayOutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
|
||||||
public class WorkflowRuntimeBuilderTest {
|
public class WorkflowRuntimeBuilderTest {
|
||||||
public static class TestWorkflow extends Workflow {
|
public static class TestWorkflow implements Workflow {
|
||||||
@Override
|
@Override
|
||||||
public WorkflowStub create() {
|
public WorkflowStub create() {
|
||||||
return ctx -> {
|
return ctx -> {
|
||||||
|
|
|
@ -16,27 +16,17 @@ package io.dapr.workflows.runtime;
|
||||||
|
|
||||||
import com.microsoft.durabletask.DurableTaskGrpcWorker;
|
import com.microsoft.durabletask.DurableTaskGrpcWorker;
|
||||||
import com.microsoft.durabletask.DurableTaskGrpcWorkerBuilder;
|
import com.microsoft.durabletask.DurableTaskGrpcWorkerBuilder;
|
||||||
import io.dapr.workflows.Workflow;
|
|
||||||
import io.dapr.workflows.WorkflowStub;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
|
||||||
public class WorkflowRuntimeTest {
|
public class WorkflowRuntimeTest {
|
||||||
public static class TestWorkflow extends Workflow {
|
|
||||||
@Override
|
|
||||||
public WorkflowStub create() {
|
|
||||||
return ctx -> { };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void startTest() {
|
public void startTest() {
|
||||||
DurableTaskGrpcWorker worker = new DurableTaskGrpcWorkerBuilder().build();
|
DurableTaskGrpcWorker worker = new DurableTaskGrpcWorkerBuilder().build();
|
||||||
try (WorkflowRuntime runtime = new WorkflowRuntime(worker)) {
|
try (WorkflowRuntime runtime = new WorkflowRuntime(worker)) {
|
||||||
assertDoesNotThrow(() -> {
|
assertDoesNotThrow(() -> runtime.start(false));
|
||||||
runtime.start(false);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,8 +26,8 @@ import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
public class OrchestratorWrapperTest {
|
public class WorkflowWrapperTest {
|
||||||
public static class TestWorkflow extends Workflow {
|
public static class TestWorkflow implements Workflow {
|
||||||
@Override
|
@Override
|
||||||
public WorkflowStub create() {
|
public WorkflowStub create() {
|
||||||
return WorkflowContext::getInstanceId;
|
return WorkflowContext::getInstanceId;
|
||||||
|
@ -36,9 +36,9 @@ public class OrchestratorWrapperTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getName() {
|
public void getName() {
|
||||||
OrchestratorWrapper<TestWorkflow> wrapper = new OrchestratorWrapper<>(TestWorkflow.class);
|
WorkflowWrapper<TestWorkflow> wrapper = new WorkflowWrapper<>(TestWorkflow.class);
|
||||||
Assertions.assertEquals(
|
Assertions.assertEquals(
|
||||||
"io.dapr.workflows.runtime.OrchestratorWrapperTest.TestWorkflow",
|
"io.dapr.workflows.runtime.WorkflowWrapperTest.TestWorkflow",
|
||||||
wrapper.getName()
|
wrapper.getName()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ public class OrchestratorWrapperTest {
|
||||||
@Test
|
@Test
|
||||||
public void createWithClass() {
|
public void createWithClass() {
|
||||||
TaskOrchestrationContext mockContext = mock(TaskOrchestrationContext.class);
|
TaskOrchestrationContext mockContext = mock(TaskOrchestrationContext.class);
|
||||||
OrchestratorWrapper<TestWorkflow> wrapper = new OrchestratorWrapper<>(TestWorkflow.class);
|
WorkflowWrapper<TestWorkflow> wrapper = new WorkflowWrapper<>(TestWorkflow.class);
|
||||||
when(mockContext.getInstanceId()).thenReturn("uuid");
|
when(mockContext.getInstanceId()).thenReturn("uuid");
|
||||||
wrapper.create().run(mockContext);
|
wrapper.create().run(mockContext);
|
||||||
verify(mockContext, times(1)).getInstanceId();
|
verify(mockContext, times(1)).getInstanceId();
|
|
@ -6,11 +6,12 @@ import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import io.dapr.workflows.runtime.saga.DefaultSagaContext;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import io.dapr.workflows.WorkflowContext;
|
import io.dapr.workflows.WorkflowContext;
|
||||||
|
|
||||||
public class DaprSagaContextImplTest {
|
public class DefaultSagaContextTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDaprSagaContextImpl_IllegalArgumentException() {
|
public void testDaprSagaContextImpl_IllegalArgumentException() {
|
||||||
|
@ -18,11 +19,11 @@ public class DaprSagaContextImplTest {
|
||||||
WorkflowContext workflowContext = mock(WorkflowContext.class);
|
WorkflowContext workflowContext = mock(WorkflowContext.class);
|
||||||
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> {
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
new DaprSagaContextImpl(saga, null);
|
new DefaultSagaContext(saga, null);
|
||||||
});
|
});
|
||||||
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> {
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
new DaprSagaContextImpl(null, workflowContext);
|
new DefaultSagaContext(null, workflowContext);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +31,7 @@ public class DaprSagaContextImplTest {
|
||||||
public void test_registerCompensation() {
|
public void test_registerCompensation() {
|
||||||
Saga saga = mock(Saga.class);
|
Saga saga = mock(Saga.class);
|
||||||
WorkflowContext workflowContext = mock(WorkflowContext.class);
|
WorkflowContext workflowContext = mock(WorkflowContext.class);
|
||||||
DaprSagaContextImpl ctx = new DaprSagaContextImpl(saga, workflowContext);
|
DefaultSagaContext ctx = new DefaultSagaContext(saga, workflowContext);
|
||||||
|
|
||||||
String activityClassName = "name1";
|
String activityClassName = "name1";
|
||||||
Object activityInput = new Object();
|
Object activityInput = new Object();
|
||||||
|
@ -44,7 +45,7 @@ public class DaprSagaContextImplTest {
|
||||||
public void test_compensate() {
|
public void test_compensate() {
|
||||||
Saga saga = mock(Saga.class);
|
Saga saga = mock(Saga.class);
|
||||||
WorkflowContext workflowContext = mock(WorkflowContext.class);
|
WorkflowContext workflowContext = mock(WorkflowContext.class);
|
||||||
DaprSagaContextImpl ctx = new DaprSagaContextImpl(saga, workflowContext);
|
DefaultSagaContext ctx = new DefaultSagaContext(saga, workflowContext);
|
||||||
|
|
||||||
doNothing().when(saga).compensate(workflowContext);
|
doNothing().when(saga).compensate(workflowContext);
|
||||||
|
|
|
@ -4,10 +4,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.microsoft.durabletask.TaskActivityContext;
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
|
import io.dapr.workflows.WorkflowActivityContext;
|
||||||
import io.dapr.workflows.runtime.WorkflowActivity;
|
|
||||||
import io.dapr.workflows.runtime.WorkflowActivityContext;
|
|
||||||
|
|
||||||
public class SagaIntegrationTest {
|
public class SagaIntegrationTest {
|
||||||
|
|
||||||
|
@ -125,7 +123,7 @@ public class SagaIntegrationTest {
|
||||||
try {
|
try {
|
||||||
Class<?> activityClass = Class.forName(activityClassName);
|
Class<?> activityClass = Class.forName(activityClassName);
|
||||||
WorkflowActivity activity = (WorkflowActivity) activityClass.getDeclaredConstructor().newInstance();
|
WorkflowActivity activity = (WorkflowActivity) activityClass.getDeclaredConstructor().newInstance();
|
||||||
WorkflowActivityContext ctx = new WorkflowActivityContext(new TaskActivityContext() {
|
WorkflowActivityContext ctx = new WorkflowActivityContext() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public java.lang.String getName() {
|
public java.lang.String getName() {
|
||||||
|
@ -136,7 +134,7 @@ public class SagaIntegrationTest {
|
||||||
public <T> T getInput(Class<T> targetType) {
|
public <T> T getInput(Class<T> targetType) {
|
||||||
return (T) input;
|
return (T) input;
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
randomFail();
|
randomFail();
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,7 @@ import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import io.dapr.workflows.WorkflowActivityContext;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.mockito.invocation.InvocationOnMock;
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
@ -41,8 +42,7 @@ import com.microsoft.durabletask.Task;
|
||||||
import com.microsoft.durabletask.TaskOptions;
|
import com.microsoft.durabletask.TaskOptions;
|
||||||
|
|
||||||
import io.dapr.workflows.WorkflowContext;
|
import io.dapr.workflows.WorkflowContext;
|
||||||
import io.dapr.workflows.runtime.WorkflowActivity;
|
import io.dapr.workflows.WorkflowActivity;
|
||||||
import io.dapr.workflows.runtime.WorkflowActivityContext;
|
|
||||||
|
|
||||||
public class SagaTest {
|
public class SagaTest {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue