Asserting Flow with ProcessPath
bpmn-to-code-runtime ships ProcessPath, a path builder over the generated FlowNodes. You walk a route through the process, then hand its ids to the flow assertion of your engine's test library. Because every step is typed by the model, a path the model does not contain does not compile: regenerate after a model change and the build breaks at the edge that moved.
It works in any process test that has the runtime on its classpath. Kotlin uses ProcessPath with extension steps, Java the fluent PathWalk. C# gets the same walk in the package Miragon.BpmnToCode.Runtime (From C#), TypeScript in the package @miragon/bpmn-to-code-runtime (From TypeScript).
import io.miragon.bpmn.runtime.path.ProcessPath
import io.miragon.bpmn.runtime.path.enter
import io.miragon.bpmn.runtime.path.inside
import io.miragon.bpmn.runtime.path.onto
import io.miragon.bpmn.runtime.path.then
import com.example.process.BikeLeasingProcessApi.FlowNodes
val path = ProcessPath.from(FlowNodes.StartEventLeasingRequestReceived)
.then { it.serviceTaskValidateApplication }
.then { it.businessRuleTaskCheckCreditRating }
.then { it.gatewayIsSolvent }
.onto { it.subProcessConcludeContract }
.inside {
enter { it.startEventCustomerEligible }
.then { it.serviceTaskSendContract }
.then { it.gatewayAwaitSignature }
.then { it.eventContractSigned }
.then { it.endEventContractConcluded }
}
.then { it.gatewayFork }
.then { it.serviceTaskOrderBike }
.then { it.gatewayJoin }
.then { it.receiveTaskHandoverReported }
.then { it.timerWithdrawalPeriodElapsed }
.then { it.endEventLeasingActive }
assertThat(instance).hasPassedInOrder(*path.ids)Steps
| Step | Does | Records |
|---|---|---|
ProcessPath.from(node) | starts at any node of FlowNodes | the node |
then { it.x } | moves to a successor; it is the current node's Next | the target and its sequence flow |
thenMultipleTimes(n) { it.x } | the same, for a sequential multi-instance activity or a self-repeat | the target n times, the flow once |
onto { it.sub } | moves onto a subprocess | only the flow |
enter { it.start } | descends into the current subprocess; it is its Start | the inner node |
enter(FlowNodes.Sub) { it.start } | descends into a named subprocess from anywhere | the inner node |
inside { enter { … }.then { … } } | walks the interior of the current subprocess and returns to the subprocess node | what the block walked |
interruptedBy(FlowNodes.Host) { it.boundary } | leaves Host through one of its boundary events | the boundary event |
throwingCompensation(boundary) { it.handler } | records a compensation handler and stays on the current node | the handler |
jumpTo(node) | re-anchors to any node without a check | nothing |
Gateways are passed at runtime, so walk them with then. A subprocess node is a bracket around its interior, not a point in the ordered flow: onto does not record it. Assert it separately if you need it.
After inside, the walk stands on the subprocess, so the next then continues with what follows it. A token that leaves the subprocess early through a boundary event is written with interruptedBy instead:
val path = ProcessPath.from(FlowNodes.StartEventCustomerEligible)
.then { it.serviceTaskSendContract }
.interruptedBy(FlowNodes.SubProcessConcludeContract) { it.boundaryContractNotSigned }
.then { it.gatewayCollectRejections }jumpTo is the only unchecked step. It is marked @RiskyNavigation, so Kotlin code has to opt in with @OptIn(RiskyNavigation::class), which makes every use visible in review.
What a path gives you
| Property | Kotlin | Java |
|---|---|---|
| Element ids in walk order | ids | getIds() |
| The same without duplicates | distinctIds | getDistinctIds() |
| Ids of the sequence flows walked | flowIds | getFlowIds() |
| The nodes themselves | nodes | getNodes() |
The three id properties are Array<String> / String[], the type a string-vararg assertion takes: hasPassedInOrder(*path.ids) in Kotlin, hasPassedInOrder(ids) in Java.
flowIds holds a flow for each then and onto whose successor is unambiguous. Boundary events, compensation handlers and enter record none. When several flows lead to the same element, pick one from flows:
val path = ProcessPath.from(FlowNodes.BusinessRuleTaskCheckCreditRating)
.then { it.gatewayIsSolvent }
.then { it.subProcessConcludeContract }
assertThat(path.flowIds).containsExactly("flow_checkCreditRatingToIsSolvent", "flow_isSolventToConcludeContract")
// several flows to one element: pick by condition
.then { next -> next.taskApprove.flows.single { it.conditionExpression == "=customer.isVip" } }What is checked
The guarantee is structural: each step is an edge the model contains. It is not a simulation. A path can be valid in the model and still not be what the engine executes, since an exclusive gateway takes one branch and a parallel gateway all of them.
Parallel branches
An in-order assertion only makes sense within one sequential branch. Walk each parallel branch on its own and assert the unordered set with nodesOf:
val orderBranch = ProcessPath.from(FlowNodes.GatewayFork)
.then { it.serviceTaskOrderBike }
.then { it.gatewayJoin }
val insuranceBranch = ProcessPath.from(FlowNodes.GatewayFork)
.then { it.serviceTaskIssueInsurancePolicy }
.then { it.gatewayJoin }
val passed = nodesOf(orderBranch.nodes, insuranceBranch.nodes).map { it.id.value }
assertThat(instance).hasPassed(*passed.toTypedArray())Walking a compensated path
A compensation handler is not reached by a sequence flow. BPMN associates it with the compensation boundary event of the activity it compensates. Record the event that throws the compensation, then each handler it triggers: name the boundary event and pick the handler from its Next.
val path = ProcessPath.from(FlowNodes.StartEventApplicationWithdrawn)
.then { it.eventReverseApplication }
.throwingCompensation(FlowNodes.BoundaryCompensateContract) { it.serviceTaskCancelContract }
.then { it.serviceTaskSendCancellationConfirmation }
assertThat(instance).hasPassedInOrder(*path.ids)- In Kotlin the step only exists on a
CompensationThrowEvent, and only a compensation boundary event offers a handler. - The walk stays on the throwing event, because the token continues from there and not from the handler.
- The boundary event is left out by default. Zeebe reports a compensation boundary event as a passed element, Camunda 7 and Operaton do not. A path without it holds on all three as long as the assertion accepts further elements in between. To record it as well, pass
includeBoundaryEvent = true:throwingCompensation(boundary, includeBoundaryEvent = true) { … }. - A throw event without an
activityReftriggers every handler in its scope in no defined order. Chain severalthrowingCompensationsteps and assert the unordered set, or walk the handlers separately and unite them withnodesOf.
From Java
PathWalk offers the same steps as instance methods; each lambda receives the current node's Next:
String[] ids = PathWalk.from(FlowNodes.startEventLeasingRequestReceived())
.then(n -> n.serviceTaskValidateApplication())
.then(n -> n.boundaryApplicationInvalid())
.then(n -> n.gatewayCollectRejections())
.then(n -> n.serviceTaskSendRejection())
.end(n -> n.endEventApplicationRejected())
.getIds();
assertThat(instance).hasPassedInOrder(ids);Three differences from Kotlin:
- The last step is
end. An end event has no successors and cannot continue a chain.endreturns aTrailwithgetIds(),getDistinctIds(),getFlowIds()andgetNodes(). - Subprocess steps name the subprocess:
enter(FlowNodes.subProcessConcludeContract(), s -> s.startEventCustomerEligible())andinside(FlowNodes.subProcessConcludeContract(), s -> PathWalk.from(s.startEventCustomerEligible())…end(…)), where the block returns theTrailof the interior. throwingCompensationis not restricted to compensation throw events; the compiler cannot check that here. It also exists onTrail, for an end event that throws a compensation. To record the boundary event, passtrueas second argument.
String[] ids = PathWalk.from(FlowNodes.startEventApplicationWithdrawn())
.then(n -> n.eventReverseApplication())
.throwingCompensation(FlowNodes.boundaryCompensateContract(), n -> n.serviceTaskCancelContract())
.then(n -> n.serviceTaskSendCancellationConfirmation())
.end(n -> n.endEventApplicationCancelled())
.getIds();PathWalk.nodesOf(…) unites branches as in Kotlin.
From C#
Miragon.BpmnToCode.Runtime contains ProcessPath with the steps of PathWalk; each lambda receives the current node's Next:
using Miragon.BpmnToCode.Runtime;
using Api = MiraVelo.Process.BikeLeasingProcessApi;
string[] ids = ProcessPath.From(Api.FlowNodes.StartEventLeasingRequestReceived)
.Then(n => n.ServiceTaskValidateApplication)
.Then(n => n.BusinessRuleTaskCheckCreditRating)
.Then(n => n.GatewayIsSolvent)
.Onto(n => n.SubProcessConcludeContract)
.Inside(contract => contract
.Enter(start => start.StartEventCustomerEligible)
.Then(n => n.ServiceTaskSendContract)
.Then(n => n.GatewayAwaitSignature)
.Then(n => n.EventContractSigned)
.End(n => n.EndEventContractConcluded))
.Then(n => n.GatewayFork)
.Then(n => n.ServiceTaskOrderBike)
.Then(n => n.GatewayJoin)
.Then(n => n.ReceiveTaskHandoverReported)
.Then(n => n.TimerWithdrawalPeriodElapsed)
.End(n => n.EndEventLeasingActive)
.Ids;- The last step is
End, as in Java. It returns aPathEndthat can still record a compensation or leave through a boundary event withInterruptedBy. Ontoreturns a path standing on the subprocess:Enterdescends into it,Insidewalks its interior and continues after it.Enter(Api.FlowNodes.SubProcessConcludeContract, s => s.StartEventCustomerEligible)descends into a named subprocess from anywhere.ThrowingCompensation(Api.FlowNodes.BoundaryCompensateContract, n => n.ServiceTaskCancelContract, includeBoundaryEvent: true),ThenMultipleTimes(3, n => n.X)andProcessPath.NodesOf(a.Nodes, b.Nodes)work as in Kotlin.JumpTois marked[Experimental("BPMNTOCODE001")], so the compiler rejects it until you opt in with#pragma warning disable BPMNTOCODE001.Ids,DistinctIdsandFlowIdsarestring[],NodesandFlowsthe recorded elements and flows.- When several flows lead to the same element, step along one of them:
.Then(n => n.<Target>.Flows[1])records that flow.
From TypeScript
@miragon/bpmn-to-code-runtime contains ProcessPath with the steps of Kotlin; each lambda receives the current node's next:
import { BikeLeasingProcessApi } from "./process/BikeLeasingProcessApi.js";
import { ProcessPath } from "@miragon/bpmn-to-code-runtime";
const nodes = BikeLeasingProcessApi.FlowNodes;
const path = ProcessPath.from(nodes.StartEventLeasingRequestReceived)
.then((next) => next.serviceTaskValidateApplication)
.then((next) => next.businessRuleTaskCheckCreditRating)
.then((next) => next.gatewayIsSolvent)
.onto((next) => next.subProcessConcludeContract)
.inside((contract) => contract
.enter((start) => start.startEventCustomerEligible)
.then((next) => next.serviceTaskSendContract)
.then((next) => next.gatewayAwaitSignature)
.then((next) => next.eventContractSigned)
.then((next) => next.endEventContractConcluded))
.then((next) => next.gatewayFork)
.then((next) => next.serviceTaskOrderBike)
.then((next) => next.gatewayJoin)
.then((next) => next.receiveTaskHandoverReported)
.then((next) => next.timerWithdrawalPeriodElapsed)
.then((next) => next.endEventLeasingActive);
expect(passedElementIds).toEqual(path.ids);- The steps are those of Kotlin:
then,thenMultipleTimes(3, (next) => …),onto,enter,inside,interruptedBy(nodes.Host, (next) => next.boundary)andthrowingCompensation(nodes.Boundary, (next) => next.handler, { includeBoundaryEvent: true }). Entering a named subprocess from anywhere isenter(nodes.SubProcessConcludeContract, (start) => start.startEventCustomerEligible). - The compiler checks what Kotlin checks:
thenonly on an element with successors,enterandinsideonly on a subprocess,throwingCompensationonly on an event that throws a compensation, and only a compensation handler as its handler. - The unchecked step needs an explicit opt-in, as in Kotlin:
jumpTo(RiskyNavigation, nodes.GatewayFork), withRiskyNavigationimported from the package. ids,distinctIdsandflowIdsarestring[],nodesthe recorded elements;nodesOf(a.nodes, b.nodes)unites parallel branches.