Search in sources :

Example 6 with ProcessInstance

use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.

the class AdHocFragmentsIT method testStartUserTask.

@Test
void testStartUserTask() throws Exception {
    String taskName = "AdHoc User Task";
    Application app = generateCodeProcessesOnly("cases/AdHocFragments.bpmn");
    assertThat(app).isNotNull();
    Process<? extends Model> p = app.get(Processes.class).processById("TestCase.AdHocFragments");
    ProcessInstance<? extends Model> processInstance = p.createInstance(p.createModel());
    processInstance.start();
    Optional<WorkItem> workItem = processInstance.workItems().stream().filter(wi -> wi.getParameters().get("NodeName").equals(taskName)).findFirst();
    assertThat(workItem).isNotPresent();
    processInstance.send(Sig.of(taskName, p.createModel()));
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.status());
    workItem = processInstance.workItems().stream().filter(wi -> wi.getParameters().get("NodeName").equals(taskName)).findFirst();
    assertThat(workItem).isPresent();
    assertThat(workItem.get().getId()).isNotBlank();
    assertThat(workItem.get().getName()).isNotBlank();
}
Also used : ActionNode(org.jbpm.workflow.core.node.ActionNode) HumanTaskNode(org.jbpm.workflow.core.node.HumanTaskNode) Collection(java.util.Collection) AdHocFragment(org.kie.kogito.process.flexible.AdHocFragment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) Application(org.kie.kogito.Application) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) List(java.util.List) AbstractCodegenIT(org.kie.kogito.codegen.AbstractCodegenIT) ProcessInstance(org.kie.kogito.process.ProcessInstance) Model(org.kie.kogito.Model) MilestoneNode(org.jbpm.workflow.core.node.MilestoneNode) Map(java.util.Map) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Process(org.kie.kogito.process.Process) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Processes(org.kie.kogito.process.Processes) WorkItem(org.kie.kogito.process.WorkItem) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) Processes(org.kie.kogito.process.Processes) Application(org.kie.kogito.Application) WorkItem(org.kie.kogito.process.WorkItem) Test(org.junit.jupiter.api.Test)

Example 7 with ProcessInstance

use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.

the class PublishEventIT method testServiceTaskProcessWithError.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testServiceTaskProcessWithError() throws Exception {
    Application app = generateCodeProcessesOnly("servicetask/ServiceProcessDifferentOperations.bpmn2");
    assertThat(app).isNotNull();
    TestEventPublisher publisher = new TestEventPublisher();
    app.unitOfWorkManager().eventManager().setService("http://myhost");
    app.unitOfWorkManager().eventManager().addPublisher(publisher);
    UnitOfWork uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    Process<? extends Model> p = app.get(Processes.class).processById("ServiceProcessDifferentOperations");
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    m.fromMap(parameters);
    ProcessInstance processInstance = p.createInstance(m);
    processInstance.start();
    uow.end();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ERROR);
    List<DataEvent<?>> events = publisher.extract();
    assertThat(events).isNotNull().hasSize(1);
    ProcessInstanceEventBody body = assertProcessInstanceEvent(events.get(0), "ServiceProcessDifferentOperations", "Service Process", 5);
    assertThat(body.getNodeInstances()).hasSize(2).extractingResultOf("getNodeType").contains("StartNode", "WorkItemNode");
    assertThat(body.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
    // human task is active thus null for leave time
    assertThat(body.getNodeInstances()).extractingResultOf("getLeaveTime").containsNull();
    assertThat(body.getError()).isNotNull();
    assertThat(body.getError().getErrorMessage()).contains("java.lang.NullPointerException");
    assertThat(body.getError().getNodeDefinitionId()).isEqualTo("_38E04E27-3CCA-47F9-927B-E37DC4B8CE25");
    parameters.put("s", "john");
    m.fromMap(parameters);
    uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    processInstance.updateVariables(m);
    uow.end();
    events = publisher.extract();
    assertThat(events).isNotNull().hasSize(1);
    body = assertProcessInstanceEvent(events.get(0), "ServiceProcessDifferentOperations", "Service Process", 5);
    assertThat(body.getError()).isNotNull();
    assertThat(body.getError().getErrorMessage()).contains("java.lang.NullPointerException");
    assertThat(body.getError().getNodeDefinitionId()).isEqualTo("_38E04E27-3CCA-47F9-927B-E37DC4B8CE25");
    uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    if (processInstance.error().isPresent()) {
        ((ProcessError) processInstance.error().get()).retrigger();
    }
    uow.end();
    events = publisher.extract();
    assertThat(events).isNotNull().hasSize(1);
    body = assertProcessInstanceEvent(events.get(0), "ServiceProcessDifferentOperations", "Service Process", 2);
    assertThat(body.getError()).isNull();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    Model result = (Model) processInstance.variables();
    assertThat(result.toMap()).hasSize(1).containsKeys("s");
    assertThat(result.toMap().get("s")).isNotNull().isEqualTo("Goodbye Hello john!!");
}
Also used : UnitOfWork(org.kie.kogito.uow.UnitOfWork) HashMap(java.util.HashMap) ProcessInstanceEventBody(org.kie.kogito.services.event.impl.ProcessInstanceEventBody) UserTaskInstanceDataEvent(org.kie.kogito.services.event.UserTaskInstanceDataEvent) ProcessInstanceDataEvent(org.kie.kogito.services.event.ProcessInstanceDataEvent) DataEvent(org.kie.kogito.event.DataEvent) VariableInstanceDataEvent(org.kie.kogito.services.event.VariableInstanceDataEvent) Processes(org.kie.kogito.process.Processes) ProcessError(org.kie.kogito.process.ProcessError) Model(org.kie.kogito.Model) ProcessInstance(org.kie.kogito.process.ProcessInstance) Application(org.kie.kogito.Application) Test(org.junit.jupiter.api.Test)

Example 8 with ProcessInstance

use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.

the class CacheProcessInstancesIT method testFindByIdReadMode.

@Test
void testFindByIdReadMode() {
    BpmnProcess process = BpmnProcess.from(new ClassPathResource("BPMN2-UserTask-Script.bpmn2")).get(0);
    // workaround as BpmnProcess does not compile the scripts but just reads the xml
    for (Node node : ((WorkflowProcess) process.get()).getNodes()) {
        if (node instanceof ActionNode) {
            DroolsAction a = ((ActionNode) node).getAction();
            a.setMetaData("Action", (Action) kcontext -> {
                System.out.println("The variable value is " + kcontext.getVariable("s") + " about to call toString on it");
                kcontext.getVariable("s").toString();
            });
        }
    }
    process.setProcessInstancesFactory(new CacheProcessInstancesFactory(cacheManager));
    process.configure();
    ProcessInstance<BpmnVariables> mutablePi = process.createInstance(BpmnVariables.create(Collections.singletonMap("var", "value")));
    mutablePi.start();
    assertThat(mutablePi.status()).isEqualTo(STATE_ERROR);
    assertThat(mutablePi.error()).hasValueSatisfying(error -> {
        assertThat(error.errorMessage()).contains("java.lang.NullPointerException");
        assertThat(error.failedNodeId()).isEqualTo("ScriptTask_1");
    });
    assertThat(mutablePi.variables().toMap()).containsExactly(entry("var", "value"));
    ProcessInstances<BpmnVariables> instances = process.instances();
    assertThat(instances.size()).isOne();
    ProcessInstance<BpmnVariables> pi = instances.findById(mutablePi.id(), ProcessInstanceReadMode.READ_ONLY).get();
    assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> pi.abort());
    ProcessInstance<BpmnVariables> readOnlyPi = instances.findById(mutablePi.id(), ProcessInstanceReadMode.READ_ONLY).get();
    assertThat(readOnlyPi.status()).isEqualTo(STATE_ERROR);
    assertThat(readOnlyPi.error()).hasValueSatisfying(error -> {
        assertThat(error.errorMessage()).contains("java.lang.NullPointerException");
        assertThat(error.failedNodeId()).isEqualTo("ScriptTask_1");
    });
    assertThat(readOnlyPi.variables().toMap()).containsExactly(entry("var", "value"));
    assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> readOnlyPi.abort());
    instances.findById(mutablePi.id()).get().abort();
    assertThat(instances.size()).isZero();
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) BeforeEach(org.junit.jupiter.api.BeforeEach) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) STATE_ACTIVE(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ACTIVE) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ProcessInstances(org.kie.kogito.process.ProcessInstances) RemoteCacheManager(org.infinispan.client.hotrod.RemoteCacheManager) STATE_COMPLETED(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_COMPLETED) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) DroolsAction(org.jbpm.workflow.core.DroolsAction) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Container(org.testcontainers.junit.jupiter.Container) ActionNode(org.jbpm.workflow.core.node.ActionNode) BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) Testcontainers(org.testcontainers.junit.jupiter.Testcontainers) ConfigurationBuilder(org.infinispan.client.hotrod.configuration.ConfigurationBuilder) SecurityPolicy(org.kie.kogito.auth.SecurityPolicy) Assertions.entry(org.assertj.core.api.Assertions.entry) KogitoProcessInstancesFactory(org.kie.kogito.persistence.KogitoProcessInstancesFactory) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) KogitoInfinispanContainer(org.kie.kogito.testcontainers.KogitoInfinispanContainer) ProcessInstance(org.kie.kogito.process.ProcessInstance) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Node(org.kie.api.definition.process.Node) ProcessInstanceReadMode(org.kie.kogito.process.ProcessInstanceReadMode) STATE_ERROR(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ERROR) Action(org.jbpm.process.instance.impl.Action) Collections(java.util.Collections) WorkItem(org.kie.kogito.process.WorkItem) BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) ActionNode(org.jbpm.workflow.core.node.ActionNode) Node(org.kie.api.definition.process.Node) ActionNode(org.jbpm.workflow.core.node.ActionNode) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) ClassPathResource(org.drools.core.io.impl.ClassPathResource) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables) Test(org.junit.jupiter.api.Test)

Example 9 with ProcessInstance

use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.

the class MockCacheProcessInstancesTest method testBasicFlowWithError.

private void testBasicFlowWithError(Consumer<ProcessInstance<BpmnVariables>> op) {
    BpmnProcess process = BpmnProcess.from(new ClassPathResource("BPMN2-UserTask-Script.bpmn2")).get(0);
    // workaround as BpmnProcess does not compile the scripts but just reads the xml
    for (Node node : ((WorkflowProcess) process.get()).getNodes()) {
        if (node instanceof ActionNode) {
            DroolsAction a = ((ActionNode) node).getAction();
            a.setMetaData("Action", (Action) kcontext -> {
                System.out.println("The variable value is " + kcontext.getVariable("s") + " about to call toString on it");
                kcontext.getVariable("s").toString();
            });
        }
    }
    process.setProcessInstancesFactory(new CacheProcessInstancesFactory(cacheManager));
    process.configure();
    ProcessInstance<BpmnVariables> processInstance = process.createInstance(BpmnVariables.create());
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(STATE_ERROR);
    Optional<ProcessError> errorOp = processInstance.error();
    assertThat(errorOp).isPresent();
    assertThat(errorOp.get().failedNodeId()).isEqualTo("ScriptTask_1");
    assertThat(errorOp.get().errorMessage()).isNotNull().contains("java.lang.NullPointerException");
    op.accept(processInstance);
    assertThat(processInstance.error()).isNotPresent();
    WorkItem workItem = processInstance.workItems(SecurityPolicy.of(new StaticIdentityProvider("john"))).get(0);
    assertThat(workItem).isNotNull();
    assertThat(workItem.getParameters().get("ActorId")).isEqualTo("john");
    processInstance.completeWorkItem(workItem.getId(), null, SecurityPolicy.of(new StaticIdentityProvider("john")));
    assertThat(processInstance.status()).isEqualTo(STATE_COMPLETED);
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) MetadataValueImpl(org.infinispan.client.hotrod.impl.MetadataValueImpl) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) STATE_ACTIVE(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ACTIVE) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RemoteCache(org.infinispan.client.hotrod.RemoteCache) ProcessInstances(org.kie.kogito.process.ProcessInstances) RemoteCacheManager(org.infinispan.client.hotrod.RemoteCacheManager) STATE_COMPLETED(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_COMPLETED) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) DroolsAction(org.jbpm.workflow.core.DroolsAction) RemoteCacheManagerAdmin(org.infinispan.client.hotrod.RemoteCacheManagerAdmin) ActionNode(org.jbpm.workflow.core.node.ActionNode) BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SecurityPolicy(org.kie.kogito.auth.SecurityPolicy) Mockito.when(org.mockito.Mockito.when) Assertions.entry(org.assertj.core.api.Assertions.entry) KogitoProcessInstancesFactory(org.kie.kogito.persistence.KogitoProcessInstancesFactory) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) ProcessInstance(org.kie.kogito.process.ProcessInstance) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Node(org.kie.api.definition.process.Node) ProcessError(org.kie.kogito.process.ProcessError) ProcessInstanceReadMode(org.kie.kogito.process.ProcessInstanceReadMode) STATE_ERROR(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ERROR) Optional(java.util.Optional) Action(org.jbpm.process.instance.impl.Action) Collections(java.util.Collections) WorkItem(org.kie.kogito.process.WorkItem) ProcessInstanceNotFoundException(org.kie.kogito.process.ProcessInstanceNotFoundException) Mockito.mock(org.mockito.Mockito.mock) StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) ActionNode(org.jbpm.workflow.core.node.ActionNode) Node(org.kie.api.definition.process.Node) ActionNode(org.jbpm.workflow.core.node.ActionNode) WorkItem(org.kie.kogito.process.WorkItem) ClassPathResource(org.drools.core.io.impl.ClassPathResource) ProcessError(org.kie.kogito.process.ProcessError) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables)

Example 10 with ProcessInstance

use of org.kie.kogito.process.ProcessInstance in project kogito-runtimes by kiegroup.

the class ActivityGenerationModelTest method testAsyncExecution.

@Test
public void testAsyncExecution() throws Exception {
    BpmnProcess process = BpmnProcess.from(new ClassPathResource("async/AsyncProcess.bpmn")).get(0);
    ProcessMetaData metaData = ProcessToExecModelGenerator.INSTANCE.generate((WorkflowProcess) process.get());
    String content = metaData.getGeneratedClassModel().toString();
    assertThat(content).isNotNull();
    log(content);
    Map<String, String> classData = Collections.singletonMap("com.example.AsyncProcessProcess", content);
    CountDownLatch latch = new CountDownLatch(1);
    String mainThread = Thread.currentThread().getName();
    AtomicReference<String> workItemThread = new AtomicReference<>();
    KogitoWorkItemHandler workItemHandler = new KogitoWorkItemHandler() {

        @Override
        public void executeWorkItem(KogitoWorkItem workItem, KogitoWorkItemManager manager) {
            workItemThread.set(Thread.currentThread().getName());
            manager.completeWorkItem(workItem.getStringId(), Collections.singletonMap("response", "hello " + workItem.getParameter("name")));
            latch.countDown();
        }

        @Override
        public void abortWorkItem(KogitoWorkItem workItem, KogitoWorkItemManager manager) {
            latch.countDown();
        }
    };
    Map<String, BpmnProcess> processes = createProcesses(classData, Collections.singletonMap("org.jbpm.bpmn2.objects.HelloService_hello_3_Handler", workItemHandler));
    ProcessInstance i = UnitOfWorkExecutor.executeInUnitOfWork(process.getApplication().unitOfWorkManager(), () -> {
        ProcessInstance<BpmnVariables> processInstance = processes.get("AsyncProcess").createInstance(BpmnVariables.create(Collections.singletonMap("name", "Tiago")));
        processInstance.start();
        assertEquals(STATE_ACTIVE, processInstance.status());
        return processInstance;
    });
    // since the tasks as async, possibly executed in different threads.
    latch.await(5, TimeUnit.SECONDS);
    assertEquals(STATE_COMPLETED, i.status());
    BpmnVariables variables = (BpmnVariables) i.variables();
    assertEquals(variables.get("greeting"), "hello Tiago");
    assertNotEquals(mainThread, workItemThread.get());
}
Also used : BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) KogitoWorkItemManager(org.kie.kogito.internal.process.runtime.KogitoWorkItemManager) KogitoWorkItemHandler(org.kie.kogito.internal.process.runtime.KogitoWorkItemHandler) KogitoWorkItem(org.kie.kogito.internal.process.runtime.KogitoWorkItem) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ClassPathResource(org.drools.core.io.impl.ClassPathResource) ProcessMetaData(org.jbpm.compiler.canonical.ProcessMetaData) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) ProcessInstance(org.kie.kogito.process.ProcessInstance) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables) Test(org.junit.jupiter.api.Test)

Aggregations

ProcessInstance (org.kie.kogito.process.ProcessInstance)19 Test (org.junit.jupiter.api.Test)15 Application (org.kie.kogito.Application)10 Processes (org.kie.kogito.process.Processes)10 List (java.util.List)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 WorkItem (org.kie.kogito.process.WorkItem)8 HashMap (java.util.HashMap)7 ProcessInstances (org.kie.kogito.process.ProcessInstances)7 BpmnVariables (org.kie.kogito.process.bpmn2.BpmnVariables)7 Collections (java.util.Collections)6 ClassPathResource (org.drools.core.io.impl.ClassPathResource)6 ActionNode (org.jbpm.workflow.core.node.ActionNode)6 Model (org.kie.kogito.Model)6 SecurityPolicy (org.kie.kogito.auth.SecurityPolicy)6 KogitoProcessInstance (org.kie.kogito.internal.process.runtime.KogitoProcessInstance)6 Process (org.kie.kogito.process.Process)6 BpmnProcess (org.kie.kogito.process.bpmn2.BpmnProcess)6 StaticIdentityProvider (org.kie.kogito.services.identity.StaticIdentityProvider)6 ArrayList (java.util.ArrayList)5