-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
workflow java dsl: added support for error end nodes to complete proc…
…essing with error
- Loading branch information
1 parent
7da1ae6
commit 4f4f344
Showing
4 changed files
with
219 additions
and
7 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
codegen/src/test/java/io/automatiko/engine/codegen/tests/ErrorEndAsCodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
|
||
package io.automatiko.engine.codegen.tests; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.automatiko.engine.api.Application; | ||
import io.automatiko.engine.api.Model; | ||
import io.automatiko.engine.api.definition.process.WorkflowProcess; | ||
import io.automatiko.engine.api.workflow.Process; | ||
import io.automatiko.engine.api.workflow.ProcessInstance; | ||
import io.automatiko.engine.codegen.AbstractCodegenTest; | ||
import io.automatiko.engine.codegen.LambdaParser; | ||
import io.automatiko.engine.codegen.process.ProcessNodeLocator; | ||
import io.automatiko.engine.workflow.AbstractProcess; | ||
import io.automatiko.engine.workflow.builder.BuilderContext; | ||
import io.automatiko.engine.workflow.builder.WorkflowBuilder; | ||
import io.automatiko.engine.workflow.process.core.node.FaultNode; | ||
|
||
public class ErrorEndAsCodeTest extends AbstractCodegenTest { | ||
|
||
@BeforeAll | ||
public static void prepare() { | ||
LambdaParser.parseLambdas( | ||
"src/test/java/" + ErrorEndAsCodeTest.class.getCanonicalName().replace(".", "/") + ".java", | ||
md -> true); | ||
} | ||
|
||
@AfterAll | ||
public static void clear() { | ||
|
||
BuilderContext.clear(); | ||
} | ||
|
||
@Test | ||
public void testBasicErrorEndEvent() throws Exception { | ||
|
||
WorkflowBuilder builder = WorkflowBuilder.newWorkflow("samples", "test workflow with error end event") | ||
.dataObject("a", Integer.class) | ||
.dataObject("b", String.class); | ||
|
||
builder.start("start here").then().log("Log", "Running {} {}", "a", "b") | ||
.then().endWithError("error").error("error", "409").expressionAsInput(String.class, () -> "error"); | ||
Application app = generateCode(List.of(builder.get())); | ||
assertThat(app).isNotNull(); | ||
|
||
Process<? extends Model> p = app.processes().processById("samples"); | ||
|
||
Collection<FaultNode> errors = ProcessNodeLocator.findFaultNodes(((AbstractProcess<?>) p).process(), | ||
((WorkflowProcess) ((AbstractProcess<?>) p).process()).getNodes()[0]); | ||
assertThat(errors).hasSize(1); | ||
|
||
Model m = p.createModel(); | ||
m.fromMap(Map.of("a", 1, "b", "test")); | ||
ProcessInstance<?> processInstance = p.createInstance(m); | ||
processInstance.start(); | ||
|
||
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED); | ||
assertThat(processInstance.abortCode()).isEqualTo("409"); | ||
assertThat(processInstance.abortData()).isEqualTo("error"); | ||
|
||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
...low-core/src/main/java/io/automatiko/engine/workflow/builder/EndWithErrorNodeBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package io.automatiko.engine.workflow.builder; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
import io.automatiko.engine.workflow.base.core.context.variable.Variable; | ||
import io.automatiko.engine.workflow.process.core.Node; | ||
import io.automatiko.engine.workflow.process.core.node.FaultNode; | ||
import io.automatiko.engine.workflow.process.executable.core.Metadata; | ||
|
||
/** | ||
* Builder responsible for building an end and terminate node | ||
*/ | ||
public class EndWithErrorNodeBuilder extends AbstractNodeBuilder { | ||
|
||
private FaultNode node; | ||
|
||
public EndWithErrorNodeBuilder(String name, boolean terminate, WorkflowBuilder workflowBuilder) { | ||
super(workflowBuilder); | ||
this.node = new FaultNode(); | ||
|
||
this.node.setId(ids.incrementAndGet()); | ||
this.node.setName(name); | ||
this.node.setMetaData("UniqueId", generateUiqueId(this.node)); | ||
this.node.setTerminateParent(true); | ||
|
||
this.node.setMetaData("functionFlowContinue", "true"); | ||
|
||
this.node.setMetaData(Metadata.DATA_INPUTS, new HashMap<>()); | ||
|
||
workflowBuilder.container().addNode(node); | ||
|
||
contect(); | ||
} | ||
|
||
public EndWithErrorNodeBuilder errorCode(String errorCode) { | ||
|
||
this.node.setErrorName(this.node.getName()); | ||
this.node.setFaultName(errorCode); | ||
return this; | ||
} | ||
|
||
public EndWithErrorNodeBuilder error(String errorName, String errorCode) { | ||
|
||
this.node.setErrorName(errorName); | ||
this.node.setFaultName(errorCode); | ||
return this; | ||
} | ||
|
||
public EndWithErrorNodeBuilder fromDataObject(String name) { | ||
Variable var = this.workflowBuilder.get().getVariableScope().findVariable(name); | ||
|
||
if (var == null) { | ||
throw new IllegalArgumentException("Cannot find data object with '" + name + "' name"); | ||
} | ||
addToDataInputs(name, var.getType().getStringType()); | ||
this.node.setFaultVariable(name); | ||
|
||
return this; | ||
} | ||
|
||
public <T> EndWithErrorNodeBuilder expressionAsInput(Class<T> type, Supplier<T> expression) { | ||
|
||
this.node.setFaultVariable( | ||
"#{" + BuilderContext.get(Thread.currentThread().getStackTrace()[2].getMethodName()).replace("\"", "\\\"") | ||
+ "}"); | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Completes given workflow path and returns the builder | ||
* | ||
* @return the builder | ||
*/ | ||
public WorkflowBuilder done() { | ||
return workflowBuilder; | ||
} | ||
|
||
@Override | ||
protected Node getNode() { | ||
return this.node; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private void addToDataInputs(String name, String type) { | ||
((Map<String, String>) this.node.getMetaData(Metadata.DATA_INPUTS)).put(name, type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters