From bfb00fd5e0075e82f1a42c31849478550b22b739 Mon Sep 17 00:00:00 2001 From: Abhiram Gundala <164050036+Abhitocode@users.noreply.github.com> Date: Wed, 29 May 2024 09:57:31 -0400 Subject: [PATCH] [incubator-kie-issues-1131] Process test migration from v7 legacy runtime to code generation (#3519) --- .../java/org/jbpm/bpmn2/ActivityTest.java | 137 +++++++++++------- 1 file changed, 85 insertions(+), 52 deletions(-) diff --git a/jbpm/jbpm-tests/src/test/java/org/jbpm/bpmn2/ActivityTest.java b/jbpm/jbpm-tests/src/test/java/org/jbpm/bpmn2/ActivityTest.java index c396cf8abbd..4beb906a828 100755 --- a/jbpm/jbpm-tests/src/test/java/org/jbpm/bpmn2/ActivityTest.java +++ b/jbpm/jbpm-tests/src/test/java/org/jbpm/bpmn2/ActivityTest.java @@ -26,6 +26,8 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; import org.drools.compiler.rule.builder.PackageBuildContext; import org.jbpm.bpmn2.activity.ScriptTaskModel; @@ -56,8 +58,21 @@ import org.jbpm.bpmn2.objects.HelloService; import org.jbpm.bpmn2.objects.Person; import org.jbpm.bpmn2.objects.TestWorkItemHandler; +import org.jbpm.bpmn2.subprocess.CallActivityModel; +import org.jbpm.bpmn2.subprocess.CallActivityProcess; +import org.jbpm.bpmn2.subprocess.CallActivitySubProcessProcess; +import org.jbpm.bpmn2.subprocess.InputMappingUsingValueModel; +import org.jbpm.bpmn2.subprocess.InputMappingUsingValueProcess; +import org.jbpm.bpmn2.subprocess.SingleTaskWithVarDefModel; +import org.jbpm.bpmn2.subprocess.SingleTaskWithVarDefProcess; import org.jbpm.bpmn2.subprocess.SubProcessWithEntryExitScriptsModel; import org.jbpm.bpmn2.subprocess.SubProcessWithEntryExitScriptsProcess; +import org.jbpm.bpmn2.subprocess.SubProcessWithTerminateEndEventModel; +import org.jbpm.bpmn2.subprocess.SubProcessWithTerminateEndEventProcess; +import org.jbpm.bpmn2.subprocess.SubProcessWithTerminateEndEventProcessScopeModel; +import org.jbpm.bpmn2.subprocess.SubProcessWithTerminateEndEventProcessScopeProcess; +import org.jbpm.bpmn2.task.ReceiveTaskModel; +import org.jbpm.bpmn2.task.ReceiveTaskProcess; import org.jbpm.bpmn2.task.SendTaskModel; import org.jbpm.bpmn2.task.SendTaskProcess; import org.jbpm.bpmn2.test.RequirePersistence; @@ -85,6 +100,8 @@ import org.jbpm.workflow.instance.WorkflowRuntimeException; import org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl; import org.jbpm.workflow.instance.node.DynamicUtils; +import org.jbpm.workflow.instance.node.EndNodeInstance; +import org.jbpm.workflow.instance.node.StartNodeInstance; import org.jbpm.workflow.instance.node.WorkItemNodeInstance; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -94,6 +111,7 @@ import org.kie.api.definition.process.NodeContainer; import org.kie.api.definition.process.Process; import org.kie.api.definition.process.WorkflowElementIdentifier; +import org.kie.api.event.process.ProcessNodeEvent; import org.kie.api.event.process.ProcessNodeTriggeredEvent; import org.kie.api.event.process.ProcessStartedEvent; import org.kie.api.event.process.ProcessVariableChangedEvent; @@ -453,25 +471,26 @@ public void testUserTaskVerifyParameters() throws Exception { } @Test - public void testCallActivityWithContantsAssignment() throws Exception { - kruntime = createKogitoProcessRuntime( - "org/jbpm/bpmn2/subprocess/BPMN2-SingleTaskWithVarDef.bpmn2", - "org/jbpm/bpmn2/subprocess/BPMN2-InputMappingUsingValue.bpmn2"); - - TestWorkItemHandler handler = new TestWorkItemHandler(); - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("CustomTask", handler); - Map params = new HashMap<>(); - KogitoProcessInstance processInstance = kruntime.startProcess("InputMappingUsingValue", params); + public void testCallActivityWithContantsAssignment() { + Application app = ProcessTestHelper.newApplication(); + TestWorkItemHandler workItemHandler = new TestWorkItemHandler(); + ProcessTestHelper.registerHandler(app, "CustomTask", workItemHandler); + org.kie.kogito.process.Process singleTaskWithVarDefModelProcess = SingleTaskWithVarDefProcess.newProcess(app); + SingleTaskWithVarDefModel singleTaskWithVarDefModel = singleTaskWithVarDefModelProcess.createModel(); + ProcessInstance singleTaskWithVarDefModelProcessInstance = singleTaskWithVarDefModelProcess.createInstance(singleTaskWithVarDefModel); + org.kie.kogito.process.Process processDefinition = InputMappingUsingValueProcess.newProcess(app); + InputMappingUsingValueModel model = processDefinition.createModel(); + org.kie.kogito.process.ProcessInstance instance = processDefinition.createInstance(model); + instance.start(); - org.kie.kogito.internal.process.runtime.KogitoWorkItem workItem = handler.getWorkItem(); + assertThat(instance).extracting(ProcessInstance::status).isEqualTo(ProcessInstance.STATE_ACTIVE); + KogitoWorkItem workItem = workItemHandler.getWorkItem(); assertThat(workItem).isNotNull(); - Object value = workItem.getParameter("TaskName"); assertThat(value).isNotNull().isEqualTo("test string"); - kruntime.getKogitoWorkItemManager().completeWorkItem(workItem.getStringId(), null); - - assertProcessInstanceCompleted(processInstance); + singleTaskWithVarDefModelProcessInstance.completeWorkItem(workItem.getStringId(), Collections.emptyMap()); + assertThat(instance).extracting(ProcessInstance::status).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_COMPLETED); } @Test @@ -499,16 +518,18 @@ public void testSubProcessWithEntryExitScripts() throws Exception { } @Test - public void testCallActivity() throws Exception { - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/subprocess/BPMN2-CallActivity.bpmn2", - "org/jbpm/bpmn2/subprocess/BPMN2-CallActivitySubProcess.bpmn2"); + public void testCallActivity() { + Application app = ProcessTestHelper.newApplication(); + org.kie.kogito.process.Process processDefinition = CallActivityProcess.newProcess(app); + CallActivityModel model = processDefinition.createModel(); + model.setX("oldValue"); + ProcessInstance instance = processDefinition.createInstance(model); - Map params = new HashMap<>(); - params.put("x", "oldValue"); - KogitoProcessInstance processInstance = kruntime.startProcess( - "CallActivity", params); - assertProcessInstanceCompleted(processInstance); - assertThat(((KogitoWorkflowProcessInstance) processInstance).getVariable("y")).isEqualTo("new value"); + CallActivitySubProcessProcess.newProcess(app); + + instance.start(); + assertThat(instance).extracting(ProcessInstance::status).isEqualTo(ProcessInstance.STATE_COMPLETED); + assertThat(instance.variables().getY()).isEqualTo("new value"); } @Test @@ -650,36 +671,46 @@ public void testMultiinstanceSubProcessWrongStartEvent() throws Exception { } @Test - public void testSubProcessWithTerminateEndEvent() throws Exception { - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/subprocess/BPMN2-SubProcessWithTerminateEndEvent.bpmn2"); - final List list = new ArrayList<>(); - kruntime.getProcessEventManager().addEventListener(new DefaultKogitoProcessEventListener() { + public void testSubProcessWithTerminateEndEvent() { + Application app = ProcessTestHelper.newApplication(); + EventTrackerProcessListener listener = new EventTrackerProcessListener(); + ProcessTestHelper.registerProcessEventListener(app, listener); + org.kie.kogito.process.Process processDefinition = SubProcessWithTerminateEndEventProcess.newProcess(app); + SubProcessWithTerminateEndEventModel model = processDefinition.createModel(); + + org.kie.kogito.process.ProcessInstance instance = processDefinition.createInstance(model); + instance.start(); + Set processNodeEvents = listener.tracked().stream() + .map(ProcessNodeEvent::getNodeInstance) + .collect(Collectors.toSet()); + assertThat(instance).extracting(ProcessInstance::status).isEqualTo(ProcessInstance.STATE_COMPLETED); + assertThat(processNodeEvents).hasSize(7); - @Override - public void afterNodeTriggered(ProcessNodeTriggeredEvent event) { - list.add(event.getNodeInstance().getNodeName()); - } - }); - KogitoProcessInstance processInstance = kruntime.startProcess("SubProcessWithTerminateEndEvent"); - assertProcessInstanceCompleted(processInstance); - assertThat(list).hasSize(7); } @Test - public void testSubProcessWithTerminateEndEventProcessScope() - throws Exception { - kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/subprocess/BPMN2-SubProcessWithTerminateEndEventProcessScope.bpmn2"); - final List list = new ArrayList<>(); - kruntime.getProcessEventManager().addEventListener(new DefaultKogitoProcessEventListener() { + public void testSubProcessWithTerminateEndEventProcessScope() { + + Application app = ProcessTestHelper.newApplication(); + final List nodeList = new ArrayList<>(); + EventTrackerProcessListener listener = new EventTrackerProcessListener() { @Override public void afterNodeTriggered(ProcessNodeTriggeredEvent event) { - list.add(event.getNodeInstance().getNodeName()); + NodeInstance nodeInstance = event.getNodeInstance(); + if (!(nodeInstance instanceof EndNodeInstance || nodeInstance instanceof StartNodeInstance)) { + nodeList.add(nodeInstance.getNodeName()); + } } - }); - KogitoProcessInstance processInstance = kruntime.startProcess("SubProcessWithTerminateEndEventProcessScope"); - assertProcessInstanceCompleted(processInstance); - assertThat(list).hasSize(5); + }; + ProcessTestHelper.registerProcessEventListener(app, listener); + org.kie.kogito.process.Process processDefinition = SubProcessWithTerminateEndEventProcessScopeProcess.newProcess(app); + SubProcessWithTerminateEndEventProcessScopeModel model = processDefinition.createModel(); + + org.kie.kogito.process.ProcessInstance instance = processDefinition.createInstance(model); + instance.start(); + assertThat(instance).extracting(ProcessInstance::status).isEqualTo(ProcessInstance.STATE_COMPLETED); + assertThat(nodeList).hasSize(3); } @Test @@ -883,17 +914,19 @@ public void testSendTask() { @Test public void testReceiveTask() throws Exception { + Application app = ProcessTestHelper.newApplication(); kruntime = createKogitoProcessRuntime("org/jbpm/bpmn2/task/BPMN2-ReceiveTask.bpmn2"); ReceiveTaskHandler receiveTaskHandler = new ReceiveTaskHandler(kruntime); - - kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Receive Task", - receiveTaskHandler); - KogitoWorkflowProcessInstance processInstance = (KogitoWorkflowProcessInstance) kruntime - .startProcess("ReceiveTask"); - assertProcessInstanceActive(processInstance); + ProcessTestHelper.registerHandler(app, "Receive Task", receiveTaskHandler); + org.kie.kogito.process.Process processDefinition = ReceiveTaskProcess.newProcess(app); + ReceiveTaskModel model = processDefinition.createModel(); + org.kie.kogito.process.ProcessInstance instance = processDefinition.createInstance(model); + instance.start(); + assertThat(instance).extracting(ProcessInstance::status).isEqualTo(ProcessInstance.STATE_ACTIVE); receiveTaskHandler.setKnowledgeRuntime(kruntime); receiveTaskHandler.messageReceived("HelloMessage", "Hello john!"); - assertProcessInstanceFinished(processInstance, kruntime); + ProcessTestHelper.completeWorkItem(instance, "john", Collections.emptyMap()); + assertThat(instance.status()).isEqualTo(org.kie.kogito.process.ProcessInstance.STATE_COMPLETED); } @Test