From 4c3394ecff89d2f66de64529ee1bbf8958a5b045 Mon Sep 17 00:00:00 2001 From: Abhiram Gundala <164050036+Abhitocode@users.noreply.github.com> Date: Mon, 26 Aug 2024 15:16:30 -0400 Subject: [PATCH] [incubator-kie-issues-1131] v7 migration to code generation --- .../org/jbpm/bpmn2/IntermediateEventTest.java | 1534 ++++++++++------- 1 file changed, 869 insertions(+), 665 deletions(-) diff --git a/jbpm/jbpm-tests/src/test/java/org/jbpm/bpmn2/IntermediateEventTest.java b/jbpm/jbpm-tests/src/test/java/org/jbpm/bpmn2/IntermediateEventTest.java index 1d24ef1aa49..228e5c87b6b 100755 --- a/jbpm/jbpm-tests/src/test/java/org/jbpm/bpmn2/IntermediateEventTest.java +++ b/jbpm/jbpm-tests/src/test/java/org/jbpm/bpmn2/IntermediateEventTest.java @@ -20,6 +20,7 @@ import java.time.OffsetDateTime; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -41,29 +42,99 @@ import org.jbpm.bpmn2.event.BoundaryTimerCycleISOVariableProcess; import org.jbpm.bpmn2.handler.ReceiveTaskHandler; import org.jbpm.bpmn2.handler.SendTaskHandler; +import org.jbpm.bpmn2.intermediate.BoundaryEventWithNonEffectiveSignalModel; +import org.jbpm.bpmn2.intermediate.BoundaryEventWithNonEffectiveSignalProcess; +import org.jbpm.bpmn2.intermediate.BoundaryEventWithSignalsModel; +import org.jbpm.bpmn2.intermediate.BoundaryEventWithSignalsProcess; +import org.jbpm.bpmn2.intermediate.BoundaryMessageEventOnTaskModel; +import org.jbpm.bpmn2.intermediate.BoundaryMessageEventOnTaskProcess; +import org.jbpm.bpmn2.intermediate.EndThrowEventScopeModel; +import org.jbpm.bpmn2.intermediate.EndThrowEventScopeProcess; +import org.jbpm.bpmn2.intermediate.EventSubProcessWithLocalVariablesModel; +import org.jbpm.bpmn2.intermediate.EventSubProcessWithLocalVariablesProcess; import org.jbpm.bpmn2.intermediate.EventSubprocessErrorSignalEmbeddedModel; import org.jbpm.bpmn2.intermediate.EventSubprocessErrorSignalEmbeddedProcess; +import org.jbpm.bpmn2.intermediate.EventSubprocessSignalExpressionModel; +import org.jbpm.bpmn2.intermediate.EventSubprocessSignalExpressionProcess; +import org.jbpm.bpmn2.intermediate.EventSubprocessSignalInterruptingModel; +import org.jbpm.bpmn2.intermediate.EventSubprocessSignalInterruptingProcess; +import org.jbpm.bpmn2.intermediate.EventSubprocessSignalModel; +import org.jbpm.bpmn2.intermediate.EventSubprocessSignalNestedModel; +import org.jbpm.bpmn2.intermediate.EventSubprocessSignalNestedProcess; +import org.jbpm.bpmn2.intermediate.EventSubprocessSignalProcess; +import org.jbpm.bpmn2.intermediate.EventSubprocessSignalWithStateNodeModel; +import org.jbpm.bpmn2.intermediate.EventSubprocessSignalWithStateNodeProcess; +import org.jbpm.bpmn2.intermediate.ExclusiveEventBasedGatewayInSubprocessModel; +import org.jbpm.bpmn2.intermediate.ExclusiveEventBasedGatewayInSubprocessProcess; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventMessageModel; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventMessageProcess; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventMessageWithRefModel; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventMessageWithRefProcess; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventMessageWithTransformationModel; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventMessageWithTransformationProcess; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventSignal2Model; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventSignal2Process; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventSignalModel; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventSignalProcess; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventSignalWithRefModel; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventSignalWithRefProcess; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventSignalWithTransformationModel; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventSignalWithTransformationProcess; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventSignalWithVariableModel; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventSignalWithVariableProcess; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerCycle1Model; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerCycle1Process; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerCycle2Model; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerCycle2Process; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerCycle3Model; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerCycle3Process; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerCycleCronModel; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerCycleCronProcess; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerCycleISOModel; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerCycleISOProcess; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerCycleWithErrorModel; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerCycleWithErrorProcess; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerDateISOModel; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerDateISOProcess; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerDurationISOModel; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerDurationISOProcess; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerDurationModel; +import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerDurationProcess; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerDurationWithErrorModel; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerDurationWithErrorProcess; import org.jbpm.bpmn2.intermediate.IntermediateCatchEventTimerDurationWithErrorProcessInstance; import org.jbpm.bpmn2.intermediate.IntermediateCatchSignalBetweenUserTasksModel; import org.jbpm.bpmn2.intermediate.IntermediateCatchSignalBetweenUserTasksProcess; +import org.jbpm.bpmn2.intermediate.IntermediateLinkEventModel; +import org.jbpm.bpmn2.intermediate.IntermediateLinkEventProcess; +import org.jbpm.bpmn2.intermediate.IntermediateThrowEventExternalScopeModel; +import org.jbpm.bpmn2.intermediate.IntermediateThrowEventExternalScopeProcess; import org.jbpm.bpmn2.intermediate.IntermediateThrowEventMessageModel; import org.jbpm.bpmn2.intermediate.IntermediateThrowEventMessageProcess; import org.jbpm.bpmn2.intermediate.IntermediateThrowEventMessageWithTransformationModel; import org.jbpm.bpmn2.intermediate.IntermediateThrowEventMessageWithTransformationProcess; +import org.jbpm.bpmn2.intermediate.IntermediateThrowEventNoneModel; +import org.jbpm.bpmn2.intermediate.IntermediateThrowEventNoneProcess; +import org.jbpm.bpmn2.intermediate.IntermediateThrowEventScopeModel; +import org.jbpm.bpmn2.intermediate.IntermediateThrowEventScopeProcess; +import org.jbpm.bpmn2.intermediate.IntermediateThrowEventSignalWithDataModel; +import org.jbpm.bpmn2.intermediate.IntermediateThrowEventSignalWithDataProcess; +import org.jbpm.bpmn2.intermediate.IntermediateThrowEventSignalWithVariableModel; +import org.jbpm.bpmn2.intermediate.IntermediateThrowEventSignalWithVariableProcess; +import org.jbpm.bpmn2.intermediate.LinkEventCompositeProcessModel; +import org.jbpm.bpmn2.intermediate.LinkEventCompositeProcessProcess; +import org.jbpm.bpmn2.intermediate.MultiInstanceSubprocessWithBoundaryErrorModel; +import org.jbpm.bpmn2.intermediate.MultiInstanceSubprocessWithBoundaryErrorProcess; +import org.jbpm.bpmn2.intermediate.MultiInstanceSubprocessWithBoundarySignalModel; +import org.jbpm.bpmn2.intermediate.MultiInstanceSubprocessWithBoundarySignalNoInteruptingModel; +import org.jbpm.bpmn2.intermediate.MultiInstanceSubprocessWithBoundarySignalNoInteruptingProcess; +import org.jbpm.bpmn2.intermediate.MultiInstanceSubprocessWithBoundarySignalProcess; +import org.jbpm.bpmn2.intermediate.MultipleMessageSignalSubprocessModel; +import org.jbpm.bpmn2.intermediate.MultipleMessageSignalSubprocessProcess; +import org.jbpm.bpmn2.intermediate.SignalBoundaryOnSubProcessModel; +import org.jbpm.bpmn2.intermediate.SignalBoundaryOnSubProcessProcess; +import org.jbpm.bpmn2.intermediate.SubprocessWithSignalEndEventAndSignalBoundaryEventModel; +import org.jbpm.bpmn2.intermediate.SubprocessWithSignalEndEventAndSignalBoundaryEventProcess; import org.jbpm.bpmn2.loop.MultiInstanceLoopBoundaryTimerModel; import org.jbpm.bpmn2.loop.MultiInstanceLoopBoundaryTimerProcess; import org.jbpm.bpmn2.loop.MultiInstanceLoopCharacteristicsProcessSequentialModel; @@ -87,7 +158,30 @@ import org.jbpm.bpmn2.subprocess.EventSubprocessSignalWithTransformationModel; import org.jbpm.bpmn2.subprocess.EventSubprocessSignalWithTransformationProcess; import org.jbpm.bpmn2.test.RequirePersistence; +import org.jbpm.bpmn2.timer.IntermediateTimerEventMIModel; +import org.jbpm.bpmn2.timer.IntermediateTimerEventMIProcess; +import org.jbpm.bpmn2.timer.IntermediateTimerParallelGatewayModel; +import org.jbpm.bpmn2.timer.IntermediateTimerParallelGatewayProcess; +import org.jbpm.bpmn2.timer.TimerBoundaryEventCycle1Model; +import org.jbpm.bpmn2.timer.TimerBoundaryEventCycle1Process; +import org.jbpm.bpmn2.timer.TimerBoundaryEventCycle2Model; +import org.jbpm.bpmn2.timer.TimerBoundaryEventCycle2Process; +import org.jbpm.bpmn2.timer.TimerBoundaryEventCycleISOModel; +import org.jbpm.bpmn2.timer.TimerBoundaryEventCycleISOProcess; +import org.jbpm.bpmn2.timer.TimerBoundaryEventDateISOModel; +import org.jbpm.bpmn2.timer.TimerBoundaryEventDateISOProcess; +import org.jbpm.bpmn2.timer.TimerBoundaryEventDurationISOModel; +import org.jbpm.bpmn2.timer.TimerBoundaryEventDurationISOProcess; +import org.jbpm.bpmn2.timer.TimerBoundaryEventDurationModel; +import org.jbpm.bpmn2.timer.TimerBoundaryEventDurationProcess; +import org.jbpm.bpmn2.timer.TimerBoundaryEventInterruptingModel; +import org.jbpm.bpmn2.timer.TimerBoundaryEventInterruptingOnTaskCancelTimerModel; +import org.jbpm.bpmn2.timer.TimerBoundaryEventInterruptingOnTaskCancelTimerProcess; +import org.jbpm.bpmn2.timer.TimerBoundaryEventInterruptingOnTaskModel; +import org.jbpm.bpmn2.timer.TimerBoundaryEventInterruptingOnTaskProcess; +import org.jbpm.bpmn2.timer.TimerBoundaryEventInterruptingProcess; import org.jbpm.process.core.datatype.impl.type.StringDataType; +import org.jbpm.process.instance.LightWorkItemManager; import org.jbpm.process.instance.event.listeners.RuleAwareProcessEventListener; import org.jbpm.process.instance.impl.demo.DoNothingWorkItemHandler; import org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler; @@ -98,7 +192,6 @@ import org.jbpm.test.utils.ProcessTestHelper.CompletionKogitoEventListener; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.kie.api.command.ExecutableCommand; import org.kie.api.event.process.ProcessCompletedEvent; import org.kie.api.event.process.ProcessNodeLeftEvent; import org.kie.api.event.process.ProcessNodeTriggeredEvent; @@ -121,6 +214,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; +import static org.assertj.core.api.IterableAssert.assertThatIterable; import static org.jbpm.workflow.instance.node.TimerNodeInstance.TIMER_TRIGGERED_EVENT; public class IntermediateEventTest extends JbpmBpmn2TestCase { @@ -210,35 +304,31 @@ public void testSignalBoundaryEvent() throws Exception { } @Test - public void testSignalBoundaryNonEffectiveEvent() throws Exception { + public void testSignalBoundaryNonEffectiveEvent() { final String signal = "signalTest"; final AtomicBoolean eventAfterNodeLeftTriggered = new AtomicBoolean(false); - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-BoundaryEventWithNonEffectiveSignal.bpmn2"); - + Application app = ProcessTestHelper.newApplication(); TestWorkItemHandler handler = new TestWorkItemHandler(); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); - - kruntime.getProcessEventManager().addEventListener(new DefaultKogitoProcessEventListener() { + ProcessTestHelper.registerHandler(app, "Human Task", handler); + ProcessTestHelper.registerProcessEventListener(app, new DefaultKogitoProcessEventListener() { @Override public void afterNodeLeft(ProcessNodeLeftEvent event) { - // BoundaryEventNodeInstance if (signal.equals(event.getNodeInstance().getNodeName())) { eventAfterNodeLeftTriggered.set(true); } } }); - KogitoProcessInstance processInstance = kruntime.startProcess("BoundaryEventWithNonEffectiveSignal"); - - // outer human work - kruntime.getKogitoWorkItemManager().completeWorkItem(handler.getWorkItem().getStringId(), null); - - kruntime.signalEvent(signal, signal); - // inner human task - kruntime.getKogitoWorkItemManager().completeWorkItem(handler.getWorkItem().getStringId(), null); - - assertProcessInstanceFinished(processInstance, kruntime); + org.kie.kogito.process.Process processDefinition = + BoundaryEventWithNonEffectiveSignalProcess.newProcess(app); + ProcessInstance processInstance = + processDefinition.createInstance(processDefinition.createModel()); + processInstance.start(); + KogitoWorkItem workItem = handler.getWorkItem(); + assertThat(workItem).isNotNull(); + processInstance.completeWorkItem(workItem.getStringId(), null); + processInstance.send(Sig.of(signal, signal)); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); assertThat(eventAfterNodeLeftTriggered).isTrue(); } @@ -560,183 +650,230 @@ public void testEventBasedSplit5() throws Exception { } @Test - public void testEventBasedSplitWithSubprocess() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-ExclusiveEventBasedGatewayInSubprocess.bpmn2"); - + public void testEventBasedSplitWithSubprocess() { + Application app = ProcessTestHelper.newApplication(); + org.kie.kogito.process.Process processDefinition = + ExclusiveEventBasedGatewayInSubprocessProcess.newProcess(app); + ExclusiveEventBasedGatewayInSubprocessModel model = processDefinition.createModel(); + org.kie.kogito.process.ProcessInstance instance = + processDefinition.createInstance(model); // Stop - KogitoProcessInstance processInstance = kruntime.startProcess("ExclusiveEventBasedGatewayInSubprocess"); - assertProcessInstanceActive(processInstance); + instance.start(); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + instance.send(Sig.of("StopSignal", "")); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_COMPLETED); + // Continue and Stop + instance = processDefinition.createInstance(model); + instance.start(); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + instance.send(Sig.of("ContinueSignal", "")); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + instance.send(Sig.of("StopSignal", "")); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_COMPLETED); + } - kruntime.signalEvent("StopSignal", "", processInstance.getStringId()); - assertProcessInstanceFinished(processInstance, kruntime); + @Test + public void testEventSubprocessSignal() { + String[] nodes = { "start", "User Task 1", "end", "Sub Process 1", "start-sub", "sub-script", "end-sub" }; + Application app = ProcessTestHelper.newApplication(); - // Continue and Stop - processInstance = kruntime.startProcess("ExclusiveEventBasedGatewayInSubprocess"); - assertProcessInstanceActive(processInstance); + TestWorkItemHandler workItemHandler = new TestWorkItemHandler(); + ProcessTestHelper.registerHandler(app, "Human Task", workItemHandler); + List executedNodes = new ArrayList<>(); - kruntime.signalEvent("ContinueSignal", "", processInstance.getStringId()); + KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() { - assertProcessInstanceActive(processInstance); + @Override + public void afterNodeLeft(ProcessNodeLeftEvent event) { + if (event.getNodeInstance().getNodeName().equals("sub-script")) { + executedNodes.add(((KogitoNodeInstance) event.getNodeInstance()).getStringId()); + } + } + }; - kruntime.signalEvent("StopSignal", "", processInstance.getStringId()); - assertProcessInstanceFinished(processInstance, kruntime); - } + EventTrackerProcessListener eventTrackerProcessListener = new EventTrackerProcessListener(); - @Test - public void testEventSubprocessSignal() throws Exception { - String[] nodes = { "start", "User Task 1", "end", "Sub Process 1", "start-sub", "sub-script", "end-sub" }; - runTestEventSubprocessSignal("org/jbpm/bpmn2/intermediate/BPMN2-EventSubprocessSignal.bpmn2", - "EventSubprocessSignal", nodes); + ProcessTestHelper.registerProcessEventListener(app, listener); + ProcessTestHelper.registerProcessEventListener(app, eventTrackerProcessListener); + + org.kie.kogito.process.Process processDefinition = EventSubprocessSignalProcess.newProcess(app); + EventSubprocessSignalModel model = processDefinition.createModel(); + org.kie.kogito.process.ProcessInstance instance = processDefinition.createInstance(model); + + instance.start(); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + + Set> eventDescriptions = instance.events(); + assertThat(eventDescriptions).hasSize(2) // Adjusted to expect two events + .extracting(EventDescription::getEvent) + .contains("MySignal", "workItemCompleted"); + assertThat(eventDescriptions).extracting(EventDescription::getEventType) + .contains("signal", "workItem"); + assertThat(eventDescriptions).extracting(EventDescription::getProcessInstanceId) + .contains(instance.id()); + + for (int i = 0; i < 4; i++) { + instance.send(Sig.of("MySignal")); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + } + + KogitoWorkItem workItem = workItemHandler.getWorkItem(); + assertThat(workItem).isNotNull(); + instance.completeWorkItem(workItem.getStringId(), null); + + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_COMPLETED); + assertThatIterable(eventTrackerProcessListener.tracked().stream().map(a -> a.getNodeInstance().getNodeName()).toList()) + .contains(nodes); + assertThat(executedNodes).hasSize(4); } @Test - public void testEventSubprocessSignalNested() throws Exception { - String[] nodes = { "Start", "Sub Process", "Sub Start", "Sub Sub Process", "Sub Sub Start", "Sub Sub User Task", - "Sub Sub Sub Process", "start-sub", "sub-script", "end-sub", "Sub Sub End", "Sub End", "End" }; - runTestEventSubprocessSignal("org/jbpm/bpmn2/intermediate/BPMN2-EventSubprocessSignalNested.bpmn2", - "EventSubprocessSignalNested", nodes); - } + public void testEventSubprocessSignalNested() { + String[] nodes = { "Start", "Sub Process", "Sub Start", "Sub Sub Process", "Sub Sub Start", "Sub Sub User Task", "Sub Sub Sub Process", "start-sub", "sub-script", "end-sub", "Sub Sub End", + "Sub End", "End" }; + Application app = ProcessTestHelper.newApplication(); - public void runTestEventSubprocessSignal(String processFile, String[] completedNodes) throws Exception { - runTestEventSubprocessSignal(processFile, processFile, completedNodes); - } + TestWorkItemHandler workItemHandler = new TestWorkItemHandler(); + ProcessTestHelper.registerHandler(app, "Human Task", workItemHandler); + List executedNodes = new ArrayList<>(); - public void runTestEventSubprocessSignal(String processFile, String processId, String[] completedNodes) - throws Exception { - kruntime = createKogitoProcessRuntime(processFile); - final List executednodes = new ArrayList<>(); KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() { @Override public void afterNodeLeft(ProcessNodeLeftEvent event) { if (event.getNodeInstance().getNodeName().equals("sub-script")) { - executednodes.add(((KogitoNodeInstance) event.getNodeInstance()).getStringId()); + executedNodes.add(((KogitoNodeInstance) event.getNodeInstance()).getStringId()); } } - }; - kruntime.getProcessEventManager().addEventListener(listener); - TestWorkItemHandler workItemHandler = new TestWorkItemHandler(); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler); - KogitoProcessInstance processInstance = kruntime.startProcess(processId); - assertProcessInstanceActive(processInstance); - kruntime.getProcessEventManager().addEventListener(listener); + EventTrackerProcessListener eventTrackerProcessListener = new EventTrackerProcessListener(); - Set> eventDescriptions = processInstance.getEventDescriptions(); - assertThat(eventDescriptions).hasSize(2).extracting("event").contains("MySignal", "workItemCompleted"); - assertThat(eventDescriptions).extracting("eventType").contains("signal", "workItem"); - assertThat(eventDescriptions).extracting("processInstanceId").contains(processInstance.getStringId()); - kruntime.signalEvent("MySignal", null, processInstance.getStringId()); - assertProcessInstanceActive(processInstance); + ProcessTestHelper.registerProcessEventListener(app, listener); + ProcessTestHelper.registerProcessEventListener(app, eventTrackerProcessListener); - kruntime.signalEvent("MySignal", null); - assertProcessInstanceActive(processInstance); + org.kie.kogito.process.Process processDefinition = EventSubprocessSignalNestedProcess.newProcess(app); + EventSubprocessSignalNestedModel model = processDefinition.createModel(); + org.kie.kogito.process.ProcessInstance instance = processDefinition.createInstance(model); + + instance.start(); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + + Set> eventDescriptions = instance.events(); + assertThat(eventDescriptions).hasSize(2) // Adjusted to expect two events + .extracting(EventDescription::getEvent) + .contains("MySignal", "workItemCompleted"); + assertThat(eventDescriptions).extracting(EventDescription::getEventType) + .contains("signal", "workItem"); + assertThat(eventDescriptions).extracting(EventDescription::getProcessInstanceId) + .contains(instance.id()); + + for (int i = 0; i < 4; i++) { + instance.send(Sig.of("MySignal")); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + } - kruntime.signalEvent("MySignal", null); - assertProcessInstanceActive(processInstance); - kruntime.signalEvent("MySignal", null); - assertProcessInstanceActive(processInstance); KogitoWorkItem workItem = workItemHandler.getWorkItem(); assertThat(workItem).isNotNull(); + instance.completeWorkItem(workItem.getStringId(), null); - kruntime.getKogitoWorkItemManager().completeWorkItem(workItem.getStringId(), null); - assertProcessInstanceFinished(processInstance, kruntime); - assertNodeTriggered(processInstance.getStringId(), completedNodes); - assertThat(executednodes).hasSize(4); - + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_COMPLETED); + assertThatIterable(eventTrackerProcessListener.tracked().stream().map(a -> a.getNodeInstance().getNodeName()).toList()) + .contains(nodes); + assertThat(executedNodes).hasSize(4); } @Test - public void testEventSubprocessSignalWithStateNode() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-EventSubprocessSignalWithStateNode.bpmn2"); - final List executednodes = new ArrayList<>(); + public void testEventSubprocessSignalWithStateNode() { + String[] nodes = { "start", "User Task 1", "end", "Sub Process 1", "start-sub", "User Task 2", "end-sub" }; + Application app = ProcessTestHelper.newApplication(); + TestWorkItemHandler workItemHandler = new TestWorkItemHandler(); + ProcessTestHelper.registerHandler(app, "Human Task", workItemHandler); + List executedNodes = new ArrayList<>(); KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() { - @Override public void afterNodeLeft(ProcessNodeLeftEvent event) { if (event.getNodeInstance().getNodeName().equals("User Task 2")) { - executednodes.add(((KogitoNodeInstance) event.getNodeInstance()).getStringId()); + executedNodes.add(((KogitoNodeInstance) event.getNodeInstance()).getStringId()); } } - }; + EventTrackerProcessListener eventTrackerProcessListener = new EventTrackerProcessListener(); + ProcessTestHelper.registerProcessEventListener(app, listener); + ProcessTestHelper.registerProcessEventListener(app, eventTrackerProcessListener); - kruntime.getProcessEventManager().addEventListener(listener); - TestWorkItemHandler workItemHandler = new TestWorkItemHandler(); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler); - KogitoProcessInstance processInstance = kruntime.startProcess("EventSubprocessSignalWithStateNode"); - assertProcessInstanceActive(processInstance); - kruntime.getProcessEventManager().addEventListener(listener); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler); + org.kie.kogito.process.Process processDefinition = EventSubprocessSignalWithStateNodeProcess.newProcess(app); + EventSubprocessSignalWithStateNodeModel model = processDefinition.createModel(); + org.kie.kogito.process.ProcessInstance instance = processDefinition.createInstance(model); + instance.start(); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); KogitoWorkItem workItemTopProcess = workItemHandler.getWorkItem(); + instance.send(Sig.of("MySignal")); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); - kruntime.signalEvent("MySignal", null, processInstance.getStringId()); - assertProcessInstanceActive(processInstance); KogitoWorkItem workItem = workItemHandler.getWorkItem(); assertThat(workItem).isNotNull(); - kruntime.getKogitoWorkItemManager().completeWorkItem(workItem.getStringId(), null); + instance.completeWorkItem(workItem.getStringId(), null); + instance.send(Sig.of("MySignal")); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); - kruntime.signalEvent("MySignal", null); - assertProcessInstanceActive(processInstance); workItem = workItemHandler.getWorkItem(); assertThat(workItem).isNotNull(); - kruntime.getKogitoWorkItemManager().completeWorkItem(workItem.getStringId(), null); + instance.completeWorkItem(workItem.getStringId(), null); + instance.send(Sig.of("MySignal")); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); - kruntime.signalEvent("MySignal", null); - assertProcessInstanceActive(processInstance); workItem = workItemHandler.getWorkItem(); assertThat(workItem).isNotNull(); - kruntime.getKogitoWorkItemManager().completeWorkItem(workItem.getStringId(), null); + instance.completeWorkItem(workItem.getStringId(), null); + instance.send(Sig.of("MySignal")); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); - kruntime.signalEvent("MySignal", null); - assertProcessInstanceActive(processInstance); workItem = workItemHandler.getWorkItem(); assertThat(workItem).isNotNull(); - kruntime.getKogitoWorkItemManager().completeWorkItem(workItem.getStringId(), null); + instance.completeWorkItem(workItem.getStringId(), null); assertThat(workItemTopProcess).isNotNull(); - kruntime.getKogitoWorkItemManager().completeWorkItem(workItemTopProcess.getStringId(), null); - assertProcessInstanceFinished(processInstance, kruntime); - assertNodeTriggered(processInstance.getStringId(), "start", "User Task 1", "end", "Sub Process 1", "start-sub", - "User Task 2", "end-sub"); - assertThat(executednodes).hasSize(4); + instance.completeWorkItem(workItemTopProcess.getStringId(), null); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_COMPLETED); + assertThatIterable(eventTrackerProcessListener.tracked().stream().map(event -> event.getNodeInstance().getNodeName()).toList()) + .contains(nodes); + assertThat(executedNodes).hasSize(4); } @Test - public void testEventSubprocessSignalInterrupting() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-EventSubprocessSignalInterrupting.bpmn2"); - final List executednodes = new ArrayList<>(); + public void testEventSubprocessSignalInterrupting() { + Application app = ProcessTestHelper.newApplication(); + TestWorkItemHandler workItemHandler = new TestWorkItemHandler(); + ProcessTestHelper.registerHandler(app, "Human Task", workItemHandler); + List executedNodes = new ArrayList<>(); KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() { - @Override public void afterNodeLeft(ProcessNodeLeftEvent event) { if (event.getNodeInstance().getNodeName().equals("Script Task 1")) { - executednodes.add(((KogitoNodeInstance) event.getNodeInstance()).getStringId()); + executedNodes.add(((KogitoNodeInstance) event.getNodeInstance()).getStringId()); } } - }; - kruntime.getProcessEventManager().addEventListener(listener); + EventTrackerProcessListener tracker = new EventTrackerProcessListener(); + ProcessTestHelper.registerProcessEventListener(app, listener); + ProcessTestHelper.registerProcessEventListener(app, tracker); - TestWorkItemHandler workItemHandler = new TestWorkItemHandler(); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler); - KogitoProcessInstance processInstance = kruntime.startProcess("EventSubprocessSignalInterrupting"); - assertProcessInstanceActive(processInstance); - kruntime.getProcessEventManager().addEventListener(listener); + org.kie.kogito.process.Process processDefinition = EventSubprocessSignalInterruptingProcess.newProcess(app); + EventSubprocessSignalInterruptingModel model = processDefinition.createModel(); + org.kie.kogito.process.ProcessInstance instance = processDefinition.createInstance(model); + instance.start(); - kruntime.signalEvent("MySignal", null, processInstance.getStringId()); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); - assertProcessInstanceFinished(processInstance, kruntime); - assertNodeTriggered(processInstance.getStringId(), "start", "User Task 1", "Sub Process 1", "start-sub", - "Script Task 1", "end-sub"); - assertThat(executednodes).hasSize(1); + instance.send(Sig.of("MySignal")); + assertThat(instance.status()).isEqualTo(ProcessInstance.STATE_ABORTED); + assertThat(executedNodes).hasSize(1); + List trackedNodes = tracker.tracked().stream().map(e -> e.getNodeInstance().getNodeName()).toList(); + assertThatIterable(trackedNodes).contains("start", "User Task 1", "Sub Process 1", "start-sub", "Script Task 1", "end-sub"); } @Test @@ -919,36 +1056,31 @@ public void afterNodeLeft(ProcessNodeLeftEvent event) { } @Test - public void testEventSubprocessMessageWithLocalVars() throws Exception { + public void testEventSubprocessMessageWithLocalVars() { + Application app = ProcessTestHelper.newApplication(); NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 1); - - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-EventSubProcessWithLocalVariables.bpmn2"); - final Set variablevalues = new HashSet(); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + final Set variableValues = new HashSet<>(); KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() { - @Override public void afterNodeLeft(ProcessNodeLeftEvent event) { @SuppressWarnings("unchecked") Map variable = (Map) event.getNodeInstance().getVariable("richiesta"); if (variable != null) { - variablevalues.addAll(variable.keySet()); + variableValues.addAll(variable.keySet()); } } - }; - kruntime.getProcessEventManager().addEventListener(listener); - kruntime.getProcessEventManager().addEventListener(countDownListener); - KogitoProcessInstance processInstance = kruntime.startProcess("EventSubProcessWithLocalVariables"); - assertProcessInstanceActive(processInstance); - + ProcessTestHelper.registerProcessEventListener(app, listener); + org.kie.kogito.process.Process process = EventSubProcessWithLocalVariablesProcess.newProcess(app); + ProcessInstance processInstance = process.createInstance(process.createModel()); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); Map data = new HashMap<>(); - kruntime.signalEvent("Message-MAIL", data, processInstance.getStringId()); + processInstance.send(Sig.of("Message-MAIL", data)); countDownListener.waitTillCompleted(); - - processInstance = kruntime.getProcessInstance(processInstance.getStringId()); - assertThat(processInstance).isNull(); - assertThat(variablevalues).hasSize(2).contains("SCRIPT1", "SCRIPT2"); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED); + assertThat(variableValues).hasSize(2).contains("SCRIPT1", "SCRIPT2"); } @Test @@ -979,339 +1111,408 @@ public void produce(KogitoProcessInstance pi, String eventData) { } @Test - public void testMessageBoundaryEventOnTask() throws Exception { - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/intermediate/BPMN2-BoundaryMessageEventOnTask.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new TestWorkItemHandler()); + public void testMessageBoundaryEventOnTask() { + Application app = ProcessTestHelper.newApplication(); + List triggeredNodes = new ArrayList<>(); - KogitoProcessInstance processInstance = kruntime.startProcess("BoundaryMessageEventOnTask"); - kruntime.signalEvent("Message-HelloMessage", "message data"); - assertProcessInstanceFinished(processInstance, kruntime); - assertNodeTriggered(processInstance.getStringId(), "StartProcess", "User Task", "Boundary event", - "Condition met", "End2"); + KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() { + @Override + public void afterNodeLeft(ProcessNodeLeftEvent event) { + triggeredNodes.add(event.getNodeInstance().getNodeName()); + } + }; + ProcessTestHelper.registerProcessEventListener(app, listener); + + TestWorkItemHandler workItemHandler = new TestWorkItemHandler(); + ProcessTestHelper.registerHandler(app, "Human Task", workItemHandler); + + org.kie.kogito.process.Process process = BoundaryMessageEventOnTaskProcess.newProcess(app); + ProcessInstance processInstance = process.createInstance(process.createModel()); + processInstance.start(); + processInstance.send(Sig.of("Message-HelloMessage", "message data")); + + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); + assertThat(triggeredNodes).containsExactlyInAnyOrder("StartProcess", "User Task", "Boundary event", "Condition met", "End2"); } @Test - public void testMessageBoundaryEventOnTaskComplete() throws Exception { - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/intermediate/BPMN2-BoundaryMessageEventOnTask.bpmn2"); - TestWorkItemHandler handler = new TestWorkItemHandler(); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); + public void testMessageBoundaryEventOnTaskComplete() { + Application app = ProcessTestHelper.newApplication(); + EventTrackerProcessListener listener = new EventTrackerProcessListener(); + ProcessTestHelper.registerProcessEventListener(app, listener); + TestWorkItemHandler workItemHandler = new TestWorkItemHandler(); + ProcessTestHelper.registerHandler(app, "Human Task", workItemHandler); - KogitoProcessInstance processInstance = kruntime.startProcess("BoundaryMessageEventOnTask"); - kruntime.getKogitoWorkItemManager().completeWorkItem(handler.getWorkItem().getStringId(), null); - kruntime.signalEvent("Message-HelloMessage", "message data"); - kruntime.getKogitoWorkItemManager().completeWorkItem(handler.getWorkItem().getStringId(), null); - assertProcessInstanceFinished(processInstance, kruntime); - assertNodeTriggered(processInstance.getStringId(), "StartProcess", "User Task", "User Task2", "End1"); + org.kie.kogito.process.Process process = BoundaryMessageEventOnTaskProcess.newProcess(app); + ProcessInstance processInstance = process.createInstance(process.createModel()); + processInstance.start(); + + processInstance.completeWorkItem(workItemHandler.getWorkItem().getStringId(), null); + processInstance.send(Sig.of("Message-HelloMessage", "message data")); + processInstance.completeWorkItem(workItemHandler.getWorkItem().getStringId(), null); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); + List triggeredNodes = listener.tracked().stream().map(e -> e.getNodeInstance().getNodeName()).toList(); + assertThatIterable(triggeredNodes).contains("StartProcess", "User Task", "User Task2", "End1"); } @Test - public void testTimerBoundaryEventDuration() throws Exception { + public void testTimerBoundaryEventDuration() { + Application app = ProcessTestHelper.newApplication(); ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(); - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/timer/BPMN2-TimerBoundaryEventDuration.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("MyTask", new DoNothingWorkItemHandler()); - kruntime.getProcessEventManager().addEventListener(countDownListener); - KogitoProcessInstance processInstance = kruntime.startProcess("TimerBoundaryEventDuration"); - assertProcessInstanceActive(processInstance); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "MyTask", new DoNothingWorkItemHandler()); - Set> eventDescriptions = processInstance.getEventDescriptions(); - assertThat(eventDescriptions).hasSize(2).extracting("event").contains("workItemCompleted", - TIMER_TRIGGERED_EVENT); + org.kie.kogito.process.Process process = TimerBoundaryEventDurationProcess.newProcess(app); + ProcessInstance processInstance = process.createInstance(process.createModel()); + processInstance.start(); + + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + + Set> eventDescriptions = processInstance.events(); + assertThat(eventDescriptions).hasSize(2).extracting("event").contains("workItemCompleted", TIMER_TRIGGERED_EVENT); assertThat(eventDescriptions).extracting("eventType").contains("workItem", "timer"); - assertThat(eventDescriptions).extracting("processInstanceId").contains(processInstance.getStringId()); - assertThat(eventDescriptions).filteredOn("eventType", "timer").hasSize(1).extracting("properties", Map.class) + assertThat(eventDescriptions).extracting("processInstanceId").contains(processInstance.id()); + assertThat(eventDescriptions).filteredOn("eventType", "timer").hasSize(1) + .extracting("properties", Map.class) .anyMatch(m -> m.containsKey("TimerID") && m.containsKey("Period")); countDownListener.waitTillCompleted(); - - assertProcessInstanceFinished(processInstance, kruntime); - + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test - public void testTimerBoundaryEventDurationISO() throws Exception { + public void testTimerBoundaryEventDurationISO() { + Application app = ProcessTestHelper.newApplication(); ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(); - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/timer/BPMN2-TimerBoundaryEventDurationISO.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("MyTask", new DoNothingWorkItemHandler()); - kruntime.getProcessEventManager().addEventListener(countDownListener); - KogitoProcessInstance processInstance = kruntime.startProcess("TimerBoundaryEventDurationISO"); - assertProcessInstanceActive(processInstance); - countDownListener.waitTillCompleted(); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "MyTask", new DoNothingWorkItemHandler()); - assertProcessInstanceFinished(processInstance, kruntime); + org.kie.kogito.process.Process definition = TimerBoundaryEventDurationISOProcess.newProcess(app); + TimerBoundaryEventDurationISOModel model = definition.createModel(); + org.kie.kogito.process.ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + countDownListener.waitTillCompleted(); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_COMPLETED); } @Test - public void testTimerBoundaryEventDateISO() throws Exception { - ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(); + public void testTimerBoundaryEventDateISO() { + Application app = ProcessTestHelper.newApplication(); + ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(2); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "MyTask", new DoNothingWorkItemHandler()); + + org.kie.kogito.process.Process definition = TimerBoundaryEventDateISOProcess.newProcess(app); + TimerBoundaryEventDateISOModel model = definition.createModel(); - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/timer/BPMN2-TimerBoundaryEventDateISO.bpmn2"); - kruntime.getProcessEventManager().addEventListener(countDownListener); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("MyTask", new DoNothingWorkItemHandler()); - HashMap params = new HashMap<>(); OffsetDateTime plusTwoSeconds = OffsetDateTime.now().plusSeconds(2); - params.put("date", plusTwoSeconds.toString()); - KogitoProcessInstance processInstance = kruntime.startProcess("TimerBoundaryEventDateISO", params); - assertProcessInstanceActive(processInstance); - countDownListener.waitTillCompleted(); + model.setDate(plusTwoSeconds.toString()); - assertProcessInstanceFinished(processInstance, kruntime); + org.kie.kogito.process.ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + countDownListener.waitTillCompleted(); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_COMPLETED); } @Test public void testTimerBoundaryEventCycle1() throws Exception { - ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(); - - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/timer/BPMN2-TimerBoundaryEventCycle1.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("MyTask", new DoNothingWorkItemHandler()); - kruntime.getProcessEventManager().addEventListener(countDownListener); - KogitoProcessInstance processInstance = kruntime.startProcess("TimerBoundaryEventCycle1"); - assertProcessInstanceActive(processInstance); - countDownListener.waitTillCompleted(); + Application app = ProcessTestHelper.newApplication(); + NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("TimerEvent", 3); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "MyTask", new DoNothingWorkItemHandler()); - assertProcessInstanceFinished(processInstance, kruntime); + org.kie.kogito.process.Process definition = TimerBoundaryEventCycle1Process.newProcess(app); + TimerBoundaryEventCycle1Model model = definition.createModel(); + org.kie.kogito.process.ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + countDownListener.waitTillCompleted(); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_COMPLETED); } @Test public void testTimerBoundaryEventCycle2() throws Exception { - NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener( - "TimerEvent", 3); + Application app = ProcessTestHelper.newApplication(); + NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("TimerEvent", 3); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "MyTask", new DoNothingWorkItemHandler()); - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/timer/BPMN2-TimerBoundaryEventCycle2.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("MyTask", new DoNothingWorkItemHandler()); - kruntime.getProcessEventManager().addEventListener(countDownListener); - KogitoProcessInstance processInstance = kruntime.startProcess("TimerBoundaryEventCycle2"); - assertProcessInstanceActive(processInstance); - countDownListener.waitTillCompleted(); + org.kie.kogito.process.Process definition = TimerBoundaryEventCycle2Process.newProcess(app); + TimerBoundaryEventCycle2Model model = definition.createModel(); - assertProcessInstanceActive(processInstance); - kruntime.abortProcessInstance(processInstance.getStringId()); + org.kie.kogito.process.ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + countDownListener.waitTillCompleted(); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + + processInstance.abort(); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ABORTED); } @Test @RequirePersistence(false) - public void testTimerBoundaryEventCycleISO() throws Exception { - NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener( - "TimerEvent", 2); - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/timer/BPMN2-TimerBoundaryEventCycleISO.bpmn2"); - kruntime.getProcessEventManager().addEventListener(countDownListener); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("MyTask", new DoNothingWorkItemHandler()); - KogitoProcessInstance processInstance = kruntime.startProcess("TimerBoundaryEventCycleISO"); - assertProcessInstanceActive(processInstance); - countDownListener.waitTillCompleted(); - assertProcessInstanceActive(processInstance); - kruntime.abortProcessInstance(processInstance.getStringId()); - } + public void testTimerBoundaryEventCycleISO() { + Application app = ProcessTestHelper.newApplication(); + NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("TimerEvent", 2); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "MyTask", new DoNothingWorkItemHandler()); - @Test - public void testTimerBoundaryEventInterrupting() throws Exception { - ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(); + org.kie.kogito.process.Process definition = TimerBoundaryEventCycleISOProcess.newProcess(app); + TimerBoundaryEventCycleISOModel model = definition.createModel(); - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/timer/BPMN2-TimerBoundaryEventInterrupting.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("MyTask", new DoNothingWorkItemHandler()); - kruntime.getProcessEventManager().addEventListener(countDownListener); - KogitoProcessInstance processInstance = kruntime.startProcess("TimerBoundaryEventInterrupting"); - assertProcessInstanceActive(processInstance); + org.kie.kogito.process.ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); - countDownListener.waitTillCompleted(); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); - logger.debug("Firing timer"); + countDownListener.waitTillCompleted(); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); - assertProcessInstanceFinished(processInstance, kruntime); + processInstance.abort(); + } + @Test + public void testTimerBoundaryEventInterrupting() throws Exception { + Application app = ProcessTestHelper.newApplication(); + ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(2); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "MyTask", new DoNothingWorkItemHandler()); + org.kie.kogito.process.Process definition = TimerBoundaryEventInterruptingProcess.newProcess(app); + TimerBoundaryEventInterruptingModel model = definition.createModel(); + org.kie.kogito.process.ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + countDownListener.waitTillCompleted(); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_COMPLETED); } @Test public void testTimerBoundaryEventInterruptingOnTask() throws Exception { - ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(); - - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/timer/BPMN2-TimerBoundaryEventInterruptingOnTask.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new TestWorkItemHandler()); - kruntime.getProcessEventManager().addEventListener(countDownListener); - - KogitoProcessInstance processInstance = kruntime.startProcess("TimerBoundaryEventInterruptingOnTask"); - assertProcessInstanceActive(processInstance); + Application app = ProcessTestHelper.newApplication(); + ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(2); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "Human Task", new TestWorkItemHandler()); + org.kie.kogito.process.Process definition = TimerBoundaryEventInterruptingOnTaskProcess.newProcess(app); + TimerBoundaryEventInterruptingOnTaskModel model = definition.createModel(); + org.kie.kogito.process.ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); countDownListener.waitTillCompleted(); - - logger.debug("Firing timer"); - - assertProcessInstanceFinished(processInstance, kruntime); - + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_COMPLETED); } @Test - public void testTimerBoundaryEventInterruptingOnTaskCancelTimer() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/timer/BPMN2-TimerBoundaryEventInterruptingOnTaskCancelTimer.bpmn2"); + public void testTimerBoundaryEventInterruptingOnTaskCancelTimer() { + Application app = ProcessTestHelper.newApplication(); TestWorkItemHandler handler = new TestWorkItemHandler(); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); - KogitoProcessInstance processInstance = kruntime - .startProcess("TimerBoundaryEventInterruptingOnTaskCancelTimer"); - assertProcessInstanceActive(processInstance); + ProcessTestHelper.registerHandler(app, "Human Task", handler); + org.kie.kogito.process.Process definition = + TimerBoundaryEventInterruptingOnTaskCancelTimerProcess.newProcess(app); + TimerBoundaryEventInterruptingOnTaskCancelTimerModel model = definition.createModel(); + ProcessInstance processInstance = definition.createInstance(model); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); - kruntime.getKogitoWorkItemManager().completeWorkItem(handler.getWorkItem().getStringId(), null); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); KogitoWorkItem workItem = handler.getWorkItem(); if (workItem != null) { - kruntime.getKogitoWorkItemManager().completeWorkItem(workItem.getStringId(), null); + processInstance.completeWorkItem(workItem.getStringId(), null); } - - assertProcessInstanceFinished(processInstance, kruntime); - + workItem = handler.getWorkItem(); + if (workItem != null) { + processInstance.completeWorkItem(workItem.getStringId(), null); + } + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test - public void testIntermediateCatchEventSignal() throws Exception { - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/intermediate/BPMN2-IntermediateCatchEventSignal.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler()); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventSignal"); - assertProcessInstanceActive(processInstance); - - // now signal process instance - kruntime.signalEvent("MyMessage", "SomeValue", processInstance.getStringId()); - assertProcessInstanceFinished(processInstance, kruntime); - assertNodeTriggered(processInstance.getStringId(), "StartProcess", "UserTask", "EndProcess", "event"); - + public void testIntermediateCatchEventSignal() { + Application app = ProcessTestHelper.newApplication(); + List triggeredNodes = new ArrayList<>(); + KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() { + @Override + public void afterNodeLeft(ProcessNodeLeftEvent event) { + triggeredNodes.add(event.getNodeInstance().getNodeName()); + } + }; + ProcessTestHelper.registerProcessEventListener(app, listener); + ProcessTestHelper.registerHandler(app, "Human Task", new SystemOutWorkItemHandler()); + org.kie.kogito.process.Process process = IntermediateCatchEventSignalProcess.newProcess(app); + ProcessInstance processInstance = process.createInstance(process.createModel()); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + processInstance.send(Sig.of("MyMessage", "SomeValue")); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); + assertThat(triggeredNodes).containsExactlyInAnyOrder("StartProcess", "UserTask", "Event", "event", "EndProcess"); } @Test public void testIntermediateCatchEventMessage() throws Exception { - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/intermediate/BPMN2-IntermediateCatchEventMessage.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler()); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventMessage"); - assertProcessInstanceActive(processInstance); // now signal process instance - kruntime.signalEvent("Message-HelloMessage", "SomeValue", processInstance.getStringId()); - assertProcessInstanceFinished(processInstance, kruntime); + Application app = ProcessTestHelper.newApplication(); + ProcessTestHelper.registerHandler(app, "Human Task", new SystemOutWorkItemHandler()); + + org.kie.kogito.process.Process process = IntermediateCatchEventMessageProcess.newProcess(app); + ProcessInstance processInstance = process.createInstance(process.createModel()); + processInstance.start(); + + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + // Signal the process instance + processInstance.send(Sig.of("Message-HelloMessage", "SomeValue")); + + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test public void testIntermediateCatchEventMessageWithRef() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateCatchEventMessageWithRef.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler()); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventMessageWithRef"); - assertProcessInstanceActive(processInstance); - + Application app = ProcessTestHelper.newApplication(); + ProcessTestHelper.registerHandler(app, "Human Task", new SystemOutWorkItemHandler()); + org.kie.kogito.process.Process process = IntermediateCatchEventMessageWithRefProcess.newProcess(app); + ProcessInstance processInstance = process.createInstance(process.createModel()); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); // now signal process instance - kruntime.signalEvent("Message-HelloMessage", "SomeValue", processInstance.getStringId()); - assertProcessInstanceFinished(processInstance, kruntime); - + processInstance.send(Sig.of("Message-HelloMessage", "SomeValue")); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test public void testIntermediateCatchEventTimerDuration() throws Exception { - ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(); + Application app = ProcessTestHelper.newApplication(); + ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(2); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "Human Task", new DoNothingWorkItemHandler()); - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateCatchEventTimerDuration.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new DoNothingWorkItemHandler()); - kruntime.getProcessEventManager().addEventListener(countDownListener); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventTimerDuration"); - assertProcessInstanceActive(processInstance); + org.kie.kogito.process.Process definition = IntermediateCatchEventTimerDurationProcess.newProcess(app); + IntermediateCatchEventTimerDurationModel model = definition.createModel(); + ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); - // now wait for 1 second for timer to trigger + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + + // Wait for the timer to trigger countDownListener.waitTillCompleted(); - assertProcessInstanceFinished(processInstance, kruntime); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test public void testIntermediateCatchEventTimerDateISO() throws Exception { - ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(); + Application app = ProcessTestHelper.newApplication(); + ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(2); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "Human Task", new DoNothingWorkItemHandler()); - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateCatchEventTimerDateISO.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new DoNothingWorkItemHandler()); - kruntime.getProcessEventManager().addEventListener(countDownListener); + org.kie.kogito.process.Process definition = IntermediateCatchEventTimerDateISOProcess.newProcess(app); + IntermediateCatchEventTimerDateISOModel model = definition.createModel(); - HashMap params = new HashMap<>(); OffsetDateTime plusTwoSeconds = OffsetDateTime.now().plusSeconds(2); - params.put("date", plusTwoSeconds.toString()); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventTimerDateISO", params); - assertProcessInstanceActive(processInstance); - // now wait for 1 second for timer to trigger - countDownListener.waitTillCompleted(); + model.setDate(plusTwoSeconds.toString()); - assertProcessInstanceFinished(processInstance, kruntime); + ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); + + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + + // Wait for the timer to trigger + countDownListener.waitTillCompleted(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test public void testIntermediateCatchEventTimerDurationISO() throws Exception { - ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(); + Application app = ProcessTestHelper.newApplication(); + ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(2); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "Human Task", new DoNothingWorkItemHandler()); - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateCatchEventTimerDurationISO.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new DoNothingWorkItemHandler()); - kruntime.getProcessEventManager().addEventListener(countDownListener); + org.kie.kogito.process.Process definition = IntermediateCatchEventTimerDurationISOProcess.newProcess(app); + IntermediateCatchEventTimerDurationISOModel model = definition.createModel(); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventTimerDurationISO"); - assertProcessInstanceActive(processInstance); - // now wait for 1.5 second for timer to trigger + ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); + + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + + // Wait for the timer to trigger countDownListener.waitTillCompleted(); - assertProcessInstanceFinished(processInstance, kruntime); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test public void testIntermediateCatchEventTimerCycle1() throws Exception { - ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(); + Application app = ProcessTestHelper.newApplication(); + ProcessCompletedCountDownProcessEventListener countDownListener = new ProcessCompletedCountDownProcessEventListener(2); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "Human Task", new DoNothingWorkItemHandler()); - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateCatchEventTimerCycle1.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new DoNothingWorkItemHandler()); - kruntime.getProcessEventManager().addEventListener(countDownListener); + org.kie.kogito.process.Process definition = IntermediateCatchEventTimerCycle1Process.newProcess(app); + IntermediateCatchEventTimerCycle1Model model = definition.createModel(); + + ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventTimerCycle1"); - assertProcessInstanceActive(processInstance); - // now wait for 1 second for timer to trigger + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + + // Wait for the timer to trigger countDownListener.waitTillCompleted(); - assertProcessInstanceFinished(processInstance, kruntime); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test public void testIntermediateCatchEventTimerCycleISO() throws Exception { + Application app = ProcessTestHelper.newApplication(); NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 5); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "Human Task", new DoNothingWorkItemHandler()); - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateCatchEventTimerCycleISO.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new DoNothingWorkItemHandler()); - kruntime.getProcessEventManager().addEventListener(countDownListener); + org.kie.kogito.process.Process definition = IntermediateCatchEventTimerCycleISOProcess.newProcess(app); + IntermediateCatchEventTimerCycleISOModel model = definition.createModel(); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventTimerCycleISO"); - assertProcessInstanceActive(processInstance); + org.kie.kogito.process.ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); + + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); countDownListener.waitTillCompleted(); - assertProcessInstanceActive(processInstance); - kruntime.abortProcessInstance(processInstance.getStringId()); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + + processInstance.abort(); } @Test public void testIntermediateCatchEventTimerCycle2() throws Exception { + Application app = ProcessTestHelper.newApplication(); NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 3); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "Human Task", new DoNothingWorkItemHandler()); - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateCatchEventTimerCycle2.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new DoNothingWorkItemHandler()); - kruntime.getProcessEventManager().addEventListener(countDownListener); + org.kie.kogito.process.Process definition = IntermediateCatchEventTimerCycle2Process.newProcess(app); + IntermediateCatchEventTimerCycle2Model model = definition.createModel(); + + org.kie.kogito.process.ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); + + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventTimerCycle2"); - assertProcessInstanceActive(processInstance); - // now wait for 1 second for timer to trigger countDownListener.waitTillCompleted(); - assertProcessInstanceActive(processInstance); - kruntime.abortProcessInstance(processInstance.getStringId()); + assertThat(processInstance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_ACTIVE); + + processInstance.abort(); } @Test @@ -1320,13 +1521,11 @@ public void testIntermediateCatchEventCondition() throws Exception { "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateCatchEventCondition.bpmn2"); KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventCondition"); assertProcessInstanceActive(processInstance); - // now activate condition Person person = new Person(); person.setName("Jack"); kruntime.getKieSession().insert(person); assertProcessInstanceFinished(processInstance, kruntime); - } @Test @@ -1400,61 +1599,65 @@ public void testIntermediateCatchEventTimerCycleWithError() throws Exception { @Test @RequirePersistence - public void testIntermediateCatchEventTimerCycleWithErrorWithPersistence() throws Exception { + public void testIntermediateCatchEventTimerCycleWithErrorWithPersistence() { + Application app = ProcessTestHelper.newApplication(); NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 2); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + ProcessTestHelper.registerHandler(app, "Human Task", new DoNothingWorkItemHandler()); - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateCatchEventTimerCycleWithError.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new DoNothingWorkItemHandler()); - kruntime.getProcessEventManager().addEventListener(countDownListener); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventTimerCycleWithError"); - assertProcessInstanceActive(processInstance); + org.kie.kogito.process.Process definition = + IntermediateCatchEventTimerCycleWithErrorProcess.newProcess(app); - final String piId = processInstance.getStringId(); - kruntime.getKieSession().execute((ExecutableCommand) context -> { - KogitoWorkflowProcessInstance processInstance1 = (KogitoWorkflowProcessInstance) kruntime - .getProcessInstance(piId); - processInstance1.setVariable("x", 0); - return null; - }); + IntermediateCatchEventTimerCycleWithErrorModel model = definition.createModel(); + ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); - // now wait for 1 second for timer to trigger + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + model.setX(0); + processInstance.updateVariables(model); + + // Wait for the timer to trigger countDownListener.waitTillCompleted(); - assertProcessInstanceActive(processInstance); - Integer xValue = kruntime.getKieSession().execute((ExecutableCommand) context -> { - KogitoWorkflowProcessInstance processInstance2 = (KogitoWorkflowProcessInstance) kruntime - .getProcessInstance(piId); - return (Integer) processInstance2.getVariable("x"); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); - }); - assertThat(xValue).isEqualTo(2); - kruntime.abortProcessInstance(processInstance.getStringId()); - assertProcessInstanceFinished(processInstance, kruntime); + // Retrieve the updated model variables + model = processInstance.variables(); + Integer xValue = model.getX(); + assertThat(xValue).isGreaterThanOrEqualTo(1); + + processInstance.abort(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED); } @Test - public void testNoneIntermediateThrow() throws Exception { - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/intermediate/BPMN2-IntermediateThrowEventNone.bpmn2"); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateThrowEventNone"); - assertProcessInstanceCompleted(processInstance); - + public void testNoneIntermediateThrow() { + Application app = ProcessTestHelper.newApplication(); + org.kie.kogito.process.Process process = IntermediateThrowEventNoneProcess.newProcess(app); + IntermediateThrowEventNoneModel model = process.createModel(); + ProcessInstance processInstance = process.createInstance(model); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test - public void testLinkIntermediateEvent() throws Exception { - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/intermediate/BPMN2-IntermediateLinkEvent.bpmn2"); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateLinkEvent"); - assertProcessInstanceCompleted(processInstance); - + public void testLinkIntermediateEvent() { + Application app = ProcessTestHelper.newApplication(); + org.kie.kogito.process.Process process = IntermediateLinkEventProcess.newProcess(app); + IntermediateLinkEventModel model = process.createModel(); + ProcessInstance processInstance = process.createInstance(model); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test public void testLinkEventCompositeProcess() throws Exception { - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/intermediate/BPMN2-LinkEventCompositeProcess.bpmn2"); - KogitoProcessInstance processInstance = kruntime.startProcess("LinkEventCompositeProcess"); - assertProcessInstanceCompleted(processInstance); - + Application app = ProcessTestHelper.newApplication(); + org.kie.kogito.process.Process process = LinkEventCompositeProcessProcess.newProcess(app); + LinkEventCompositeProcessModel model = process.createModel(); + ProcessInstance processInstance = process.createInstance(model); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test @@ -1462,15 +1665,12 @@ public void testConditionalBoundaryEventOnTask() throws Exception { kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/intermediate/BPMN2-BoundaryConditionalEventOnTask.bpmn2"); kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new TestWorkItemHandler()); KogitoProcessInstance processInstance = kruntime.startProcess("BoundaryConditionalEventOnTask"); - Person person = new Person(); person.setName("john"); kruntime.getKieSession().insert(person); - assertProcessInstanceFinished(processInstance, kruntime); assertNodeTriggered(processInstance.getStringId(), "StartProcess", "User Task", "Boundary event", "Condition met", "End2"); - } @Test @@ -1527,20 +1727,20 @@ public void testConditionalBoundaryEventInterrupting() throws Exception { } @Test - public void testSignalBoundaryEventOnSubprocessTakingDifferentPaths() throws Exception { - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/intermediate/BPMN2-SignalBoundaryOnSubProcess.bpmn"); - - KogitoProcessInstance processInstance = kruntime.startProcess("SignalBoundaryOnSubProcess"); - assertProcessInstanceActive(processInstance); - - kruntime.signalEvent("continue", null, processInstance.getStringId()); - assertProcessInstanceFinished(processInstance, kruntime); - - processInstance = kruntime.startProcess("SignalBoundaryOnSubProcess"); - assertProcessInstanceActive(processInstance); + public void testSignalBoundaryEventOnSubprocessTakingDifferentPaths() { + Application app = ProcessTestHelper.newApplication(); + org.kie.kogito.process.Process definition = SignalBoundaryOnSubProcessProcess.newProcess(app); - kruntime.signalEvent("forward", null); - assertProcessInstanceFinished(processInstance, kruntime); + ProcessInstance processInstance = definition.createInstance(definition.createModel()); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + processInstance.send(Sig.of("continue", null)); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); + processInstance = definition.createInstance(definition.createModel()); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + processInstance.send(Sig.of("forward", null)); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test @@ -1586,119 +1786,99 @@ public void testIntermediateCatchEventNoIncommingConnection() throws Exception { } @Test - public void testSignalBoundaryEventOnMultiInstanceSubprocess() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-MultiInstanceSubprocessWithBoundarySignal.bpmn2"); - + public void testSignalBoundaryEventOnMultiInstanceSubprocess() { + Application app = ProcessTestHelper.newApplication(); TestWorkItemHandler handler = new TestWorkItemHandler(); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); - - Map params = new HashMap<>(); - List approvers = new ArrayList<>(); - approvers.add("john"); - approvers.add("john"); + ProcessTestHelper.registerHandler(app, "Human Task", handler); - params.put("approvers", approvers); + org.kie.kogito.process.Process definition = MultiInstanceSubprocessWithBoundarySignalProcess.newProcess(app); + MultiInstanceSubprocessWithBoundarySignalModel model = definition.createModel(); + model.setApprovers(Arrays.asList("john", "john")); + ProcessInstance processInstance = definition.createInstance(model); - KogitoProcessInstance processInstance = kruntime.startProcess("MultiInstanceSubprocessWithBoundarySignal", - params); - assertProcessInstanceActive(processInstance); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); List workItems = handler.getWorkItems(); assertThat(workItems).isNotNull().hasSize(2); - kruntime.signalEvent("Outside", null, processInstance.getStringId()); - assertProcessInstanceFinished(processInstance, kruntime); - + processInstance.send(Sig.of("Outside", null)); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test - public void testSignalBoundaryEventNoInteruptOnMultiInstanceSubprocess() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-MultiInstanceSubprocessWithBoundarySignalNoInterupting.bpmn2"); - + public void testSignalBoundaryEventNoInteruptOnMultiInstanceSubprocess() { + Application app = ProcessTestHelper.newApplication(); TestWorkItemHandler handler = new TestWorkItemHandler(); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); - - Map params = new HashMap<>(); - List approvers = new ArrayList<>(); - approvers.add("john"); - approvers.add("john"); + ProcessTestHelper.registerHandler(app, "Human Task", handler); - params.put("approvers", approvers); + org.kie.kogito.process.Process definition = + MultiInstanceSubprocessWithBoundarySignalNoInteruptingProcess.newProcess(app); + MultiInstanceSubprocessWithBoundarySignalNoInteruptingModel model = definition.createModel(); + model.setApprovers(Arrays.asList("john", "john")); - KogitoProcessInstance processInstance = kruntime - .startProcess("MultiInstanceSubprocessWithBoundarySignalNoInterupting", params); - assertProcessInstanceActive(processInstance); + ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); List workItems = handler.getWorkItems(); assertThat(workItems).isNotNull().hasSize(2); - kruntime.signalEvent("Outside", null, processInstance.getStringId()); - - assertProcessInstanceActive(processInstance.getStringId(), kruntime); + processInstance.send(Sig.of("Outside", null)); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); for (KogitoWorkItem wi : workItems) { - kruntime.getKogitoWorkItemManager().completeWorkItem(wi.getStringId(), null); + processInstance.completeWorkItem(wi.getStringId(), null); } - assertProcessInstanceFinished(processInstance, kruntime); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test - public void testErrorBoundaryEventOnMultiInstanceSubprocess() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-MultiInstanceSubprocessWithBoundaryError.bpmn2"); - + public void testErrorBoundaryEventOnMultiInstanceSubprocess() { + Application app = ProcessTestHelper.newApplication(); TestWorkItemHandler handler = new TestWorkItemHandler(); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); - - Map params = new HashMap<>(); - List approvers = new ArrayList<>(); - approvers.add("john"); - approvers.add("john"); - - params.put("approvers", approvers); - - KogitoProcessInstance processInstance = kruntime.startProcess("MultiInstanceSubprocessWithBoundaryError", - params); - assertProcessInstanceActive(processInstance); - Set> eventDescriptions = processInstance.getEventDescriptions(); - assertThat(eventDescriptions).hasSize(3).extracting("event").contains("workItemCompleted", "Inside", - "Error-_D83CFC28-3322-4ABC-A12D-83476B08C7E8-MyError"); - assertThat(eventDescriptions).extracting("eventType").contains("workItem", "signal"); - assertThat(eventDescriptions).extracting("processInstanceId").contains(processInstance.getStringId()); - assertThat(eventDescriptions).filteredOn("eventType", "signal").hasSize(2).extracting("properties", Map.class) + ProcessTestHelper.registerHandler(app, "Human Task", handler); + org.kie.kogito.process.Process definition = + MultiInstanceSubprocessWithBoundaryErrorProcess.newProcess(app); + MultiInstanceSubprocessWithBoundaryErrorModel model = definition.createModel(); + model.setApprovers(Arrays.asList("john", "john")); + ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + Set> eventDescriptions = processInstance.events(); + assertThat(eventDescriptions).hasSize(3) + .extracting("event") + .contains("workItemCompleted", "Inside", "Error-_D83CFC28-3322-4ABC-A12D-83476B08C7E8-MyError"); + assertThat(eventDescriptions).extracting("eventType") + .contains("workItem", "signal"); + assertThat(eventDescriptions).extracting("processInstanceId") + .contains(processInstance.id()); + assertThat(eventDescriptions).filteredOn("eventType", "signal").hasSize(2) + .extracting("properties", Map.class) .anyMatch(m -> m.containsKey("AttachedToID") && m.containsKey("AttachedToName")); - List workItems = handler.getWorkItems(); assertThat(workItems).isNotNull().hasSize(2); - - kruntime.signalEvent("Inside", null, processInstance.getStringId()); - assertProcessInstanceFinished(processInstance, kruntime); - + processInstance.send(Sig.of("Inside", null)); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test - public void testIntermediateCatchEventSignalAndBoundarySignalEvent() throws Exception { - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/intermediate/BPMN2-BoundaryEventWithSignals.bpmn2"); + public void testIntermediateCatchEventSignalAndBoundarySignalEvent() { + Application app = ProcessTestHelper.newApplication(); TestWorkItemHandler handler = new TestWorkItemHandler(); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); - KogitoProcessInstance processInstance = kruntime.startProcess("BoundaryEventWithSignals"); - assertProcessInstanceActive(processInstance); - - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); - // now signal process instance - kruntime.signalEvent("moveon", "", processInstance.getStringId()); - assertProcessInstanceActive(processInstance); - + ProcessTestHelper.registerHandler(app, "Human Task", handler); + org.kie.kogito.process.Process process = BoundaryEventWithSignalsProcess.newProcess(app); + BoundaryEventWithSignalsModel model = process.createModel(); + ProcessInstance processInstance = process.createInstance(model); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + processInstance.send(Sig.of("moveon", "")); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); KogitoWorkItem wi = handler.getWorkItem(); assertThat(wi).isNotNull(); - - // signal boundary event on user task - kruntime.signalEvent("moveon", "", processInstance.getStringId()); - - assertProcessInstanceFinished(processInstance, kruntime); + processInstance.send(Sig.of("moveon", "")); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test @@ -1909,38 +2089,46 @@ public void testEventSubprocessSignalWithTransformation() throws Exception { } @Test - public void testMultipleMessageSignalSubprocess() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-MultipleMessageSignalSubprocess.bpmn2"); - KogitoProcessInstance processInstance = kruntime.startProcess("MultipleMessageSignalSubprocess"); - logger.debug("Parent Process ID: " + processInstance.getStringId()); - - kruntime.signalEvent("Message-Message 1", "Test", processInstance.getStringId()); - assertProcessInstanceActive(processInstance.getStringId(), kruntime); - - kruntime.signalEvent("Message-Message 1", "Test", processInstance.getStringId()); - assertProcessInstanceCompleted(processInstance.getStringId(), kruntime); + public void testMultipleMessageSignalSubprocess() { + Application app = ProcessTestHelper.newApplication(); + org.kie.kogito.process.Process process = MultipleMessageSignalSubprocessProcess.newProcess(app); + MultipleMessageSignalSubprocessModel model = process.createModel(); + ProcessInstance processInstance = process.createInstance(model); + processInstance.start(); + logger.debug("Parent Process ID: " + processInstance.id()); + processInstance.send(Sig.of("Message-Message 1", "Test")); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + processInstance.send(Sig.of("Message-Message 1", "Test")); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test - public void testIntermediateCatchEventSignalWithRef() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateCatchEventSignalWithRef.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler()); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventSignalWithRef"); - assertProcessInstanceActive(processInstance); + public void testIntermediateCatchEventSignalWithRef() { + Application app = ProcessTestHelper.newApplication(); + ProcessTestHelper.registerHandler(app, "Human Task", new SystemOutWorkItemHandler()); + EventTrackerProcessListener listener = new EventTrackerProcessListener(); + ProcessTestHelper.registerProcessEventListener(app, listener); - // now signal process instance - kruntime.signalEvent("Signal1", "SomeValue", processInstance.getStringId()); - assertProcessInstanceFinished(processInstance, kruntime); - assertNodeTriggered(processInstance.getStringId(), "StartProcess", "UserTask", "EndProcess", "event"); + org.kie.kogito.process.Process process = IntermediateCatchEventSignalWithRefProcess.newProcess(app); + IntermediateCatchEventSignalWithRefModel model = process.createModel(); + + ProcessInstance processInstance = process.createInstance(model); + processInstance.start(); + + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + processInstance.send(Sig.of("Signal1", "SomeValue")); + + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); + + List nodes = listener.tracked().stream().map(e -> e.getNodeInstance().getNodeName()).toList(); + assertThatIterable(nodes).contains("StartProcess", "UserTask", "EndProcess", "event"); } @Test public void testMultiInstanceLoopBoundaryTimer() throws Exception { Application app = ProcessTestHelper.newApplication(); - NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 3); + NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 2); ProcessTestHelper.registerProcessEventListener(app, countDownListener); TestWorkItemHandler handler = new TestWorkItemHandler(); ProcessTestHelper.registerHandler(app, "Human Task", handler); @@ -2090,213 +2278,233 @@ public void testMultiInstanceLoopCharacteristicsTaskWithOutputCmpCondSequential( } @Test - public void testIntermediateTimerParallelGateway() throws Exception { - NodeLeftCountDownProcessEventListener countDownListener1 = new NodeLeftCountDownProcessEventListener("Timer1", - 1); - NodeLeftCountDownProcessEventListener countDownListener2 = new NodeLeftCountDownProcessEventListener("Timer2", - 1); - NodeLeftCountDownProcessEventListener countDownListener3 = new NodeLeftCountDownProcessEventListener("Timer3", - 1); + public void testIntermediateTimerParallelGateway() { + Application app = ProcessTestHelper.newApplication(); + NodeLeftCountDownProcessEventListener countDownListener1 = new NodeLeftCountDownProcessEventListener("Timer1", 1); + NodeLeftCountDownProcessEventListener countDownListener2 = new NodeLeftCountDownProcessEventListener("Timer2", 1); + NodeLeftCountDownProcessEventListener countDownListener3 = new NodeLeftCountDownProcessEventListener("Timer3", 1); ProcessCompletedCountDownProcessEventListener countDownProcessEventListener = new ProcessCompletedCountDownProcessEventListener(); - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/timer/BPMN2-IntermediateTimerParallelGateway.bpmn2"); - kruntime.getProcessEventManager().addEventListener(countDownListener1); - kruntime.getProcessEventManager().addEventListener(countDownListener2); - kruntime.getProcessEventManager().addEventListener(countDownListener3); - kruntime.getProcessEventManager().addEventListener(countDownProcessEventListener); + + ProcessTestHelper.registerProcessEventListener(app, countDownListener1); + ProcessTestHelper.registerProcessEventListener(app, countDownListener2); + ProcessTestHelper.registerProcessEventListener(app, countDownListener3); + ProcessTestHelper.registerProcessEventListener(app, countDownProcessEventListener); + TestWorkItemHandler handler = new TestWorkItemHandler(); + ProcessTestHelper.registerHandler(app, "Human Task", handler); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateTimerParallelGateway"); - assertProcessInstanceActive(processInstance); + org.kie.kogito.process.Process process = IntermediateTimerParallelGatewayProcess.newProcess(app); + IntermediateTimerParallelGatewayModel model = process.createModel(); + ProcessInstance processInstance = process.createInstance(model); + processInstance.start(); + + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); countDownListener1.waitTillCompleted(); countDownListener2.waitTillCompleted(); countDownListener3.waitTillCompleted(); countDownProcessEventListener.waitTillCompleted(); - assertProcessInstanceCompleted(processInstance.getStringId(), kruntime); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test - public void testIntermediateTimerEventMI() throws Exception { - NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener( - "After timer", 3); - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/timer/BPMN2-IntermediateTimerEventMI.bpmn2"); - kruntime.getProcessEventManager().addEventListener(countDownListener); + public void testIntermediateTimerEventMI() { + Application app = ProcessTestHelper.newApplication(); + NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("After timer", 3); + ProcessTestHelper.registerProcessEventListener(app, countDownListener); + TestWorkItemHandler handler = new TestWorkItemHandler(); + ProcessTestHelper.registerHandler(app, "Human Task", handler); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateTimerEventMI"); - assertProcessInstanceActive(processInstance); + org.kie.kogito.process.Process process = IntermediateTimerEventMIProcess.newProcess(app); + IntermediateTimerEventMIModel model = process.createModel(); + ProcessInstance processInstance = process.createInstance(model); + processInstance.start(); + + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); countDownListener.waitTillCompleted(); - assertProcessInstanceActive(processInstance.getStringId(), kruntime); - kruntime.abortProcessInstance(processInstance.getStringId()); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + + processInstance.abort(); - assertProcessInstanceAborted(processInstance.getStringId(), kruntime); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED); } @Test - public void testThrowIntermediateSignalWithScope() throws Exception { - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/intermediate/BPMN2-IntermediateThrowEventScope.bpmn2"); + public void testThrowIntermediateSignalWithScope() { + Application app = ProcessTestHelper.newApplication(); TestWorkItemHandler handler = new TestWorkItemHandler(); + ProcessTestHelper.registerHandler(app, "Human Task", handler); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); - Map params = new HashMap<>(); + org.kie.kogito.process.Process process = IntermediateThrowEventScopeProcess.newProcess(app); + IntermediateThrowEventScopeModel model1 = process.createModel(); + IntermediateThrowEventScopeModel model2 = process.createModel(); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateThrowEventScope", params); - KogitoProcessInstance processInstance2 = kruntime.startProcess("IntermediateThrowEventScope", params); + ProcessInstance processInstance1 = process.createInstance(model1); + ProcessInstance processInstance2 = process.createInstance(model2); - assertProcessInstanceActive(processInstance); - assertProcessInstanceActive(processInstance2); + processInstance1.start(); + processInstance2.start(); - assertNodeActive(processInstance.getStringId(), kruntime, "Complete work", "Wait"); - assertNodeActive(processInstance2.getStringId(), kruntime, "Complete work", "Wait"); + assertThat(processInstance1.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + assertThat(processInstance2.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); List items = handler.getWorkItems(); - - KogitoWorkItem wi = items.get(0); - + KogitoWorkItem wi1 = items.get(0); Map result = new HashMap<>(); result.put("_output", "sending event"); - kruntime.getKogitoWorkItemManager().completeWorkItem(wi.getStringId(), result); + processInstance1.completeWorkItem(wi1.getStringId(), result); - assertProcessInstanceCompleted(processInstance); - assertProcessInstanceActive(processInstance2); - assertNodeActive(processInstance2.getStringId(), kruntime, "Complete work", "Wait"); + assertThat(processInstance1.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); + assertThat(processInstance2.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); - wi = items.get(1); - kruntime.getKogitoWorkItemManager().completeWorkItem(wi.getStringId(), result); - assertProcessInstanceCompleted(processInstance2); + KogitoWorkItem wi2 = items.get(1); + processInstance2.completeWorkItem(wi2.getStringId(), result); + assertThat(processInstance2.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test - public void testThrowEndSignalWithScope() throws Exception { - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/intermediate/BPMN2-EndThrowEventScope.bpmn2"); + public void testThrowEndSignalWithScope() { + Application app = ProcessTestHelper.newApplication(); TestWorkItemHandler handler = new TestWorkItemHandler(); + ProcessTestHelper.registerHandler(app, "Human Task", handler); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); - Map params = new HashMap<>(); + org.kie.kogito.process.Process process = EndThrowEventScopeProcess.newProcess(app); + EndThrowEventScopeModel model1 = process.createModel(); + EndThrowEventScopeModel model2 = process.createModel(); - KogitoProcessInstance processInstance = kruntime.startProcess("EndThrowEventScope", params); - KogitoProcessInstance processInstance2 = kruntime.startProcess("EndThrowEventScope", params); + ProcessInstance processInstance1 = process.createInstance(model1); + ProcessInstance processInstance2 = process.createInstance(model2); - assertProcessInstanceActive(processInstance); - assertProcessInstanceActive(processInstance2); + processInstance1.start(); + processInstance2.start(); - assertNodeActive(processInstance.getStringId(), kruntime, "Complete work", "Wait"); - assertNodeActive(processInstance2.getStringId(), kruntime, "Complete work", "Wait"); + assertThat(processInstance1.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + assertThat(processInstance2.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); List items = handler.getWorkItems(); - - KogitoWorkItem wi = items.get(0); - + KogitoWorkItem wi1 = items.get(0); Map result = new HashMap<>(); result.put("_output", "sending event"); - kruntime.getKogitoWorkItemManager().completeWorkItem(wi.getStringId(), result); + processInstance1.completeWorkItem(wi1.getStringId(), result); - assertProcessInstanceCompleted(processInstance); - assertProcessInstanceActive(processInstance2); - assertNodeActive(processInstance2.getStringId(), kruntime, "Complete work", "Wait"); + assertThat(processInstance1.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); + assertThat(processInstance2.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); - wi = items.get(1); - kruntime.getKogitoWorkItemManager().completeWorkItem(wi.getStringId(), result); - assertProcessInstanceCompleted(processInstance2); + KogitoWorkItem wi2 = items.get(1); + processInstance2.completeWorkItem(wi2.getStringId(), result); + assertThat(processInstance2.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test - public void testThrowIntermediateSignalWithExternalScope() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateThrowEventExternalScope.bpmn2"); + public void testThrowIntermediateSignalWithExternalScope() { + Application app = ProcessTestHelper.newApplication(); TestWorkItemHandler handler = new TestWorkItemHandler(); + ProcessTestHelper.registerHandler(app, "Human Task", handler); KogitoWorkItemHandler externalHandler = new KogitoWorkItemHandler() { - @Override public void executeWorkItem(KogitoWorkItem workItem, KogitoWorkItemManager manager) { String signal = (String) workItem.getParameter("Signal"); - kruntime.signalEvent(signal, null); - + if (manager instanceof LightWorkItemManager) { + ((LightWorkItemManager) manager).signalEvent(signal, null); + } manager.completeWorkItem(workItem.getStringId(), null); - } @Override public void abortWorkItem(KogitoWorkItem workItem, KogitoWorkItemManager manager) { } }; + ProcessTestHelper.registerHandler(app, "External Send Task", externalHandler); + org.kie.kogito.process.Process process = IntermediateThrowEventExternalScopeProcess.newProcess(app); + IntermediateThrowEventExternalScopeModel model = process.createModel(); + ProcessInstance processInstance = process.createInstance(model); + processInstance.start(); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("External Send Task", externalHandler); - Map params = new HashMap<>(); - - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateThrowEventExternalScope", params); - - assertProcessInstanceActive(processInstance); - - assertNodeActive(processInstance.getStringId(), kruntime, "Complete work", "Wait"); - + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); List items = handler.getWorkItems(); assertThat(items).hasSize(1); KogitoWorkItem wi = items.get(0); - Map result = new HashMap<>(); result.put("_output", "sending event"); + processInstance.completeWorkItem(wi.getStringId(), result); - kruntime.getKogitoWorkItemManager().completeWorkItem(wi.getStringId(), result); - - assertProcessInstanceCompleted(processInstance); - + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test - public void testIntermediateCatchEventSignalWithVariable() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateCatchEventSignalWithVariable.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler()); + public void testIntermediateCatchEventSignalWithVariable() { + Application app = ProcessTestHelper.newApplication(); + List triggeredNodes = new ArrayList<>(); + + KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() { + @Override + public void afterNodeLeft(ProcessNodeLeftEvent event) { + triggeredNodes.add(event.getNodeInstance().getNodeName()); + } + }; + + ProcessTestHelper.registerProcessEventListener(app, listener); + ProcessTestHelper.registerHandler(app, "Human Task", new SystemOutWorkItemHandler()); String signalVar = "myVarSignal"; Map parameters = new HashMap<>(); parameters.put("signalName", signalVar); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventSignalWithVariable", - parameters); - assertProcessInstanceActive(processInstance); - // now signal process instance - kruntime.signalEvent(signalVar, "SomeValue", processInstance.getStringId()); - assertProcessInstanceFinished(processInstance, kruntime); - assertNodeTriggered(processInstance.getStringId(), "StartProcess", "UserTask", "EndProcess", "event"); + org.kie.kogito.process.Process process = IntermediateCatchEventSignalWithVariableProcess.newProcess(app); + IntermediateCatchEventSignalWithVariableModel model = process.createModel(); + model.fromMap(parameters); + ProcessInstance processInstance = process.createInstance(model); + processInstance.start(); + + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + + processInstance.send(Sig.of(signalVar, "SomeValue")); + + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); + assertThat(triggeredNodes).containsExactlyInAnyOrder("StartProcess", "UserTask", "Event", "event", "EndProcess"); } @Test public void testSignalIntermediateThrowWithVariable() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateThrowEventSignalWithVariable.bpmn2", - "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateCatchEventSignalWithVariable.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler()); - // create catch process instance + Application app = ProcessTestHelper.newApplication(); + List triggeredNodes = new ArrayList<>(); + KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() { + @Override + public void afterNodeLeft(ProcessNodeLeftEvent event) { + triggeredNodes.add(event.getNodeInstance().getNodeName()); + } + }; + ProcessTestHelper.registerProcessEventListener(app, listener); + ProcessTestHelper.registerHandler(app, "Human Task", new SystemOutWorkItemHandler()); String signalVar = "myVarSignal"; - Map parameters = new HashMap<>(); - parameters.put("signalName", signalVar); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEventSignalWithVariable", - parameters); - assertProcessInstanceActive(processInstance); - - Map params = new HashMap<>(); - params.put("x", "MyValue"); - params.put("signalName", signalVar); - KogitoProcessInstance processInstanceThrow = kruntime.startProcess("IntermediateThrowEventSignalWithVariable", - params); - assertThat(processInstanceThrow.getState()).isEqualTo(KogitoProcessInstance.STATE_COMPLETED); - - // catch process instance should now be completed - assertProcessInstanceFinished(processInstance, kruntime); - + Map catchParameters = new HashMap<>(); + catchParameters.put("signalName", signalVar); + org.kie.kogito.process.Process catchProcess = IntermediateCatchEventSignalWithVariableProcess.newProcess(app); + IntermediateCatchEventSignalWithVariableModel catchModel = catchProcess.createModel(); + catchModel.fromMap(catchParameters); + ProcessInstance catchProcessInstance = catchProcess.createInstance(catchModel); + catchProcessInstance.start(); + assertThat(catchProcessInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + Map throwParameters = new HashMap<>(); + throwParameters.put("x", "MyValue"); + throwParameters.put("signalName", signalVar); + org.kie.kogito.process.Process throwProcess = IntermediateThrowEventSignalWithVariableProcess.newProcess(app); + IntermediateThrowEventSignalWithVariableModel throwModel = throwProcess.createModel(); + throwModel.fromMap(throwParameters); + ProcessInstance throwProcessInstance = throwProcess.createInstance(throwModel); + throwProcessInstance.start(); + assertThat(throwProcessInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); + assertThat(catchProcessInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); + assertThat(triggeredNodes).contains("StartProcess", "UserTask", "Event", "event", "EndProcess"); } @Test @@ -2458,20 +2666,22 @@ public void testEventSubprocessWithEmbeddedSignals() throws Exception { } @Test - public void testEventSubprocessWithExpression() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-EventSubprocessSignalExpression.bpmn2"); + public void testEventSubprocessWithExpression() { + Application app = ProcessTestHelper.newApplication(); TestWorkItemHandler handler = new TestWorkItemHandler(); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); - Map params = new HashMap<>(); - params.put("x", "signalling"); - KogitoProcessInstance processInstance = kruntime.startProcess("EventSubprocessSignalExpression", params); + ProcessTestHelper.registerHandler(app, "Human Task", handler); - assertProcessInstanceActive(processInstance.getStringId(), kruntime); - assertProcessInstanceActive(processInstance); - kruntime.signalEvent("signalling", null, processInstance.getStringId()); + org.kie.kogito.process.Process process = EventSubprocessSignalExpressionProcess.newProcess(app); + EventSubprocessSignalExpressionModel model = process.createModel(); + model.setX("signalling"); + ProcessInstance processInstance = process.createInstance(model); + processInstance.start(); - assertProcessInstanceFinished(processInstance, kruntime); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + + processInstance.send(Sig.of("signalling")); + + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED); } @Test @@ -2483,63 +2693,57 @@ public void testConditionalProcessFactInsertedBefore() throws Exception { kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", handler); Person person0 = new Person("john"); kruntime.getKieSession().insert(person0); - Map params0 = new HashMap<>(); params0.put("name", "john"); KogitoProcessInstance pi0 = kruntime.startProcess("IntermediateCatchEventSignal", params0); kruntime.getKieSession().insert(pi0); - Person person = new Person("Jack"); kruntime.getKieSession().insert(person); - Map params = new HashMap<>(); params.put("name", "Poul"); KogitoProcessInstance pi = kruntime.startProcess("IntermediateCatchEventConditionPI", params); kruntime.getKieSession().insert(pi); pi = kruntime.getProcessInstance(pi.getStringId()); assertThat(pi).isNotNull(); - Person person2 = new Person("Poul"); kruntime.getKieSession().insert(person2); - pi = kruntime.getProcessInstance(pi.getStringId()); assertThat(pi).isNull(); - } @Test - public void testBoundarySignalEventOnSubprocessWithVariableResolution() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-SubprocessWithSignalEndEventAndSignalBoundaryEvent.bpmn2"); - kruntime.getProcessEventManager().addEventListener(LOGGING_EVENT_LISTENER); + public void testBoundarySignalEventOnSubprocessWithVariableResolution() { + Application app = ProcessTestHelper.newApplication(); + + org.kie.kogito.process.Process process = + SubprocessWithSignalEndEventAndSignalBoundaryEventProcess.newProcess(app); Map params = new HashMap<>(); params.put("document-ref", "signalling"); params.put("message", "hello"); - KogitoProcessInstance processInstance = kruntime - .startProcess("SubprocessWithSignalEndEventAndSignalBoundaryEvent", params); - - assertNodeTriggered(processInstance.getStringId(), "sysout from boundary", "end2"); - assertNotNodeTriggered(processInstance.getStringId(), "end1"); - - assertProcessInstanceFinished(processInstance, kruntime); - } - - @Test - public void testSignalEndWithData() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/intermediate/BPMN2-IntermediateThrowEventSignalWithData.bpmn2"); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler()); - Map params = new HashMap<>(); - KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateThrowEventSignalWithData", params); + SubprocessWithSignalEndEventAndSignalBoundaryEventModel model = process.createModel(); + model.fromMap(params); - assertProcessInstanceActive(processInstance); + ProcessInstance processInstance = process.createInstance(model); + processInstance.start(); - kruntime.signalEvent("mysignal", null, processInstance.getStringId()); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); - assertProcessInstanceCompleted(processInstance); + } + @Test + public void testSignalEndWithData() { + Application app = ProcessTestHelper.newApplication(); + ProcessTestHelper.registerHandler(app, "Human Task", new SystemOutWorkItemHandler()); + org.kie.kogito.process.Process definition = + IntermediateThrowEventSignalWithDataProcess.newProcess(app); + IntermediateThrowEventSignalWithDataModel model = definition.createModel(); + ProcessInstance processInstance = definition.createInstance(model); + processInstance.start(); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE); + processInstance.send(Sig.of("mysignal", null)); + assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED); } @Test