Search in sources :

Example 6 with ProcessInstances

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

the class MongoDBProcessInstancesIT method testFindByIdReadMode.

void testFindByIdReadMode(MongoDBTransactionManager transactionManager) {
    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 MongoDBProcessInstancesFactory(mongoClient, transactionManager));
    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) Document(org.bson.Document) BpmnVariables(org.kie.kogito.process.bpmn2.BpmnVariables) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) WorkflowProcess(org.jbpm.workflow.core.WorkflowProcess) STATE_ACTIVE(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ACTIVE) MongoDBTransactionManager(org.kie.kogito.mongodb.transaction.MongoDBTransactionManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) ProcessInstances(org.kie.kogito.process.ProcessInstances) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) STATE_COMPLETED(org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_COMPLETED) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) StreamSupport(java.util.stream.StreamSupport) DroolsAction(org.jbpm.workflow.core.DroolsAction) UnitOfWorkStartEvent(org.kie.kogito.uow.events.UnitOfWorkStartEvent) 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) Collection(java.util.Collection) Testcontainers(org.testcontainers.junit.jupiter.Testcontainers) MongoClients(com.mongodb.client.MongoClients) SecurityPolicy(org.kie.kogito.auth.SecurityPolicy) Assertions.entry(org.assertj.core.api.Assertions.entry) KogitoProcessInstancesFactory(org.kie.kogito.persistence.KogitoProcessInstancesFactory) DOCUMENT_ID(org.kie.kogito.mongodb.utils.DocumentConstants.DOCUMENT_ID) Test(org.junit.jupiter.api.Test) DocumentConstants(org.kie.kogito.mongodb.utils.DocumentConstants) List(java.util.List) 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) KogitoMongoDBContainer(org.kie.kogito.testcontainers.KogitoMongoDBContainer) Collections(java.util.Collections) WorkItem(org.kie.kogito.process.WorkItem) UnitOfWorkEndEvent(org.kie.kogito.uow.events.UnitOfWorkEndEvent) 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)

Aggregations

ProcessInstance (org.kie.kogito.process.ProcessInstance)6 ProcessInstances (org.kie.kogito.process.ProcessInstances)6 Collections (java.util.Collections)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)4 Assertions.entry (org.assertj.core.api.Assertions.entry)4 ClassPathResource (org.drools.core.io.impl.ClassPathResource)4 Action (org.jbpm.process.instance.impl.Action)4 DroolsAction (org.jbpm.workflow.core.DroolsAction)4 WorkflowProcess (org.jbpm.workflow.core.WorkflowProcess)4 ActionNode (org.jbpm.workflow.core.node.ActionNode)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 Test (org.junit.jupiter.api.Test)4 Node (org.kie.api.definition.process.Node)4 SecurityPolicy (org.kie.kogito.auth.SecurityPolicy)4 STATE_ACTIVE (org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ACTIVE)4 STATE_COMPLETED (org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_COMPLETED)4 STATE_ERROR (org.kie.kogito.internal.process.runtime.KogitoProcessInstance.STATE_ERROR)4 KogitoProcessInstancesFactory (org.kie.kogito.persistence.KogitoProcessInstancesFactory)4 ProcessInstanceReadMode (org.kie.kogito.process.ProcessInstanceReadMode)4