Search in sources :

Example 11 with UnitOfWork

use of org.kie.kogito.uow.UnitOfWork in project kogito-runtimes by kiegroup.

the class DefaultUnitOfWorkManagerTest method testUnitOfWorkStartOnFinishedUnit.

@Test
public void testUnitOfWorkStartOnFinishedUnit() {
    UnitOfWork unit = unitOfWorkManager.newUnitOfWork();
    assertThat(unit).isNotNull().isInstanceOf(ManagedUnitOfWork.class);
    unit.start();
    unit.abort();
    assertThrows(IllegalStateException.class, () -> unit.start(), "Cannot start already completed unit");
    assertThrows(IllegalStateException.class, () -> unit.end(), "Cannot end already completed unit");
    assertThrows(IllegalStateException.class, () -> unit.abort(), "Cannot abort already completed unit");
    assertThrows(IllegalStateException.class, () -> unit.intercept(null), "Cannot intercept on already completed unit");
}
Also used : UnitOfWork(org.kie.kogito.uow.UnitOfWork) Test(org.junit.jupiter.api.Test)

Example 12 with UnitOfWork

use of org.kie.kogito.uow.UnitOfWork in project kogito-runtimes by kiegroup.

the class UnitOfWorkExecutor method executeInUnitOfWork.

public static <T> T executeInUnitOfWork(UnitOfWorkManager uowManager, Supplier<T> supplier) {
    T result = null;
    UnitOfWork uow = uowManager.newUnitOfWork();
    try {
        uow.start();
        result = supplier.get();
        uow.end();
        return result;
    } catch (ProcessInstanceExecutionException e) {
        uow.end();
        throw e;
    } catch (Exception e) {
        uow.abort();
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new RuntimeException(e);
        }
    }
}
Also used : UnitOfWork(org.kie.kogito.uow.UnitOfWork) ProcessInstanceExecutionException(org.kie.kogito.process.ProcessInstanceExecutionException) ProcessInstanceExecutionException(org.kie.kogito.process.ProcessInstanceExecutionException)

Example 13 with UnitOfWork

use of org.kie.kogito.uow.UnitOfWork in project kogito-runtimes by kiegroup.

the class PublishEventIT method testBasicCallActivityTask.

@Test
public void testBasicCallActivityTask() throws Exception {
    Application app = generateCodeProcessesOnly("subprocess/CallActivity.bpmn2", "subprocess/CallActivitySubProcess.bpmn2");
    assertThat(app).isNotNull();
    Process<? extends Model> p = app.get(Processes.class).processById("ParentProcess");
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("x", "a");
    parameters.put("y", "b");
    m.fromMap(parameters);
    TestEventPublisher publisher = new TestEventPublisher();
    app.unitOfWorkManager().eventManager().setService("http://myhost");
    app.unitOfWorkManager().eventManager().addPublisher(publisher);
    UnitOfWork uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    uow.end();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    Model result = (Model) processInstance.variables();
    assertThat(result.toMap()).hasSize(2).containsKeys("x", "y");
    assertThat(result.toMap().get("y")).isNotNull().isEqualTo("new value");
    assertThat(result.toMap().get("x")).isNotNull().isEqualTo("a");
    List<DataEvent<?>> events = publisher.extract().stream().filter(ProcessInstanceDataEvent.class::isInstance).collect(Collectors.toList());
    assertThat(events).isNotNull().hasSize(2);
    DataEvent<?> parent = null;
    DataEvent<?> child = null;
    for (DataEvent<?> e : events) {
        ProcessInstanceDataEvent processDataEvent = (ProcessInstanceDataEvent) e;
        if (processDataEvent.getKogitoProcessId().equals("ParentProcess")) {
            parent = e;
            assertThat(processDataEvent.getKogitoProcessinstanceId()).isNotNull();
            assertThat(processDataEvent.getKogitoParentProcessinstanceId()).isNull();
            assertThat(processDataEvent.getKogitoRootProcessinstanceId()).isNull();
            assertThat(processDataEvent.getKogitoRootProcessId()).isNull();
            assertThat(processDataEvent.getKogitoProcessId()).isEqualTo("ParentProcess");
            assertThat(processDataEvent.getKogitoProcessinstanceState()).isEqualTo("2");
        } else {
            child = e;
            assertThat(processDataEvent.getKogitoProcessinstanceId()).isNotNull();
            assertThat(processDataEvent.getKogitoParentProcessinstanceId()).isNotNull();
            assertThat(processDataEvent.getKogitoRootProcessinstanceId()).isNotNull();
            assertThat(processDataEvent.getKogitoProcessId()).isEqualTo("SubProcess");
            assertThat(processDataEvent.getKogitoRootProcessId()).isEqualTo("ParentProcess");
            assertThat(processDataEvent.getKogitoProcessinstanceState()).isEqualTo("2");
        }
    }
    ProcessInstanceEventBody parentBody = assertProcessInstanceEvent(parent, "ParentProcess", "Parent Process", 2);
    assertThat(parentBody.getNodeInstances()).hasSize(3).extractingResultOf("getNodeType").contains("StartNode", "SubProcessNode", "EndNode");
    assertThat(parentBody.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
    assertThat(parentBody.getNodeInstances()).extractingResultOf("getLeaveTime").allMatch(v -> v != null);
    ProcessInstanceEventBody childBody = assertProcessInstanceEventWithParentId(child, "SubProcess", "Sub Process", 2);
    assertThat(childBody.getNodeInstances()).hasSize(3).extractingResultOf("getNodeType").contains("StartNode", "ActionNode", "EndNode");
    assertThat(childBody.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
    assertThat(childBody.getNodeInstances()).extractingResultOf("getLeaveTime").allMatch(v -> v != null);
}
Also used : UnitOfWork(org.kie.kogito.uow.UnitOfWork) HashMap(java.util.HashMap) ProcessInstanceEventBody(org.kie.kogito.event.process.ProcessInstanceEventBody) UserTaskInstanceDataEvent(org.kie.kogito.event.process.UserTaskInstanceDataEvent) VariableInstanceDataEvent(org.kie.kogito.event.process.VariableInstanceDataEvent) ProcessInstanceDataEvent(org.kie.kogito.event.process.ProcessInstanceDataEvent) DataEvent(org.kie.kogito.event.DataEvent) Processes(org.kie.kogito.process.Processes) Model(org.kie.kogito.Model) ProcessInstanceDataEvent(org.kie.kogito.event.process.ProcessInstanceDataEvent) Application(org.kie.kogito.Application) Test(org.junit.jupiter.api.Test)

Example 14 with UnitOfWork

use of org.kie.kogito.uow.UnitOfWork in project kogito-runtimes by kiegroup.

the class PublishEventIT method testBusinessRuleProcessStartToEndWithVariableTracked.

@Test
public void testBusinessRuleProcessStartToEndWithVariableTracked() throws Exception {
    Map<TYPE, List<String>> resourcesTypeMap = new HashMap<>();
    resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList("ruletask/BusinessRuleTaskVariableTags.bpmn2"));
    resourcesTypeMap.put(TYPE.RULES, Collections.singletonList("ruletask/BusinessRuleTask.drl"));
    Application app = generateCode(resourcesTypeMap);
    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("BusinessRuleTask");
    Model m = p.createModel();
    m.fromMap(Collections.singletonMap("person", new Person("john", 25)));
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    Model result = (Model) processInstance.variables();
    assertThat(result.toMap()).hasSize(1).containsKey("person");
    assertThat(result.toMap().get("person")).isNotNull().hasFieldOrPropertyWithValue("adult", true);
    uow.end();
    List<DataEvent<?>> events = publisher.extract();
    assertThat(events).isNotNull().hasSize(3);
    DataEvent<?> event = events.get(0);
    assertThat(event).isInstanceOf(ProcessInstanceDataEvent.class);
    ProcessInstanceDataEvent processDataEvent = (ProcessInstanceDataEvent) event;
    assertThat(processDataEvent.getKogitoProcessinstanceId()).isNotNull();
    assertThat(processDataEvent.getKogitoParentProcessinstanceId()).isNull();
    assertThat(processDataEvent.getKogitoRootProcessinstanceId()).isNull();
    assertThat(processDataEvent.getKogitoProcessId()).isEqualTo("BusinessRuleTask");
    assertThat(processDataEvent.getKogitoProcessinstanceState()).isEqualTo("2");
    assertThat(processDataEvent.getSource().toString()).isEqualTo("http://myhost/BusinessRuleTask");
    ProcessInstanceEventBody body = assertProcessInstanceEvent(events.get(0), "BusinessRuleTask", "Default Process", 2);
    assertThat(body.getNodeInstances()).hasSize(3).extractingResultOf("getNodeType").contains("StartNode", "RuleSetNode", "EndNode");
    assertThat(body.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
    assertThat(body.getNodeInstances()).extractingResultOf("getLeaveTime").allMatch(v -> v != null);
    assertThat(body.getVariables()).hasSize(1).containsKey("person");
    assertThat(body.getVariables().get("person")).isNotNull().hasFieldOrPropertyWithValue("adult", true);
    event = events.get(1);
    assertThat(event).isInstanceOf(VariableInstanceDataEvent.class);
    VariableInstanceDataEvent variableDataEvent = (VariableInstanceDataEvent) event;
    assertThat(variableDataEvent.getKogitoProcessinstanceId()).isNotNull();
    assertThat(variableDataEvent.getKogitoRootProcessId()).isNull();
    assertThat(variableDataEvent.getKogitoRootProcessinstanceId()).isNull();
    assertThat(variableDataEvent.getKogitoProcessId()).isEqualTo("BusinessRuleTask");
    // first is event created based on process start so no node associated
    VariableInstanceEventBody variableEventBody = variableDataEvent.getData();
    assertThat(variableEventBody).isNotNull();
    assertThat(variableEventBody.getChangeDate()).isNotNull();
    assertThat(variableEventBody.getProcessInstanceId()).isEqualTo(variableDataEvent.getKogitoProcessinstanceId());
    assertThat(variableEventBody.getProcessId()).isEqualTo("BusinessRuleTask");
    assertThat(variableEventBody.getRootProcessId()).isNull();
    assertThat(variableEventBody.getRootProcessInstanceId()).isNull();
    assertThat(variableEventBody.getVariableName()).isEqualTo("person");
    assertThat(variableEventBody.getVariableValue()).isNotNull();
    assertThat(variableEventBody.getVariablePreviousValue()).isNull();
    assertThat(variableEventBody.getChangedByNodeId()).isNull();
    assertThat(variableEventBody.getChangedByNodeName()).isNull();
    assertThat(variableEventBody.getChangedByNodeType()).isNull();
    assertThat(variableEventBody.getChangedByUser()).isNull();
    event = events.get(2);
    assertThat(event).isInstanceOf(VariableInstanceDataEvent.class);
    variableDataEvent = (VariableInstanceDataEvent) event;
    assertThat(variableDataEvent.getKogitoProcessinstanceId()).isNotNull();
    assertThat(variableDataEvent.getKogitoRootProcessId()).isNull();
    assertThat(variableDataEvent.getKogitoRootProcessinstanceId()).isNull();
    assertThat(variableDataEvent.getKogitoProcessId()).isEqualTo("BusinessRuleTask");
    // next is event created based on business rule task so node associated
    variableEventBody = variableDataEvent.getData();
    assertThat(variableEventBody).isNotNull();
    assertThat(variableEventBody.getChangeDate()).isNotNull();
    assertThat(variableEventBody.getProcessInstanceId()).isEqualTo(variableDataEvent.getKogitoProcessinstanceId());
    assertThat(variableEventBody.getProcessId()).isEqualTo("BusinessRuleTask");
    assertThat(variableEventBody.getRootProcessId()).isNull();
    assertThat(variableEventBody.getRootProcessInstanceId()).isNull();
    assertThat(variableEventBody.getVariableName()).isEqualTo("person");
    assertThat(variableEventBody.getVariableValue()).isNotNull();
    assertThat(variableEventBody.getVariablePreviousValue()).isNotNull();
    assertThat(variableEventBody.getChangedByNodeId()).isEqualTo("BusinessRuleTask_2");
    assertThat(variableEventBody.getChangedByNodeName()).isEqualTo("Business Rule Task");
    assertThat(variableEventBody.getChangedByNodeType()).isEqualTo("RuleSetNode");
    assertThat(variableEventBody.getChangedByUser()).isNull();
}
Also used : UnitOfWork(org.kie.kogito.uow.UnitOfWork) HashMap(java.util.HashMap) ProcessInstanceEventBody(org.kie.kogito.event.process.ProcessInstanceEventBody) UserTaskInstanceDataEvent(org.kie.kogito.event.process.UserTaskInstanceDataEvent) VariableInstanceDataEvent(org.kie.kogito.event.process.VariableInstanceDataEvent) ProcessInstanceDataEvent(org.kie.kogito.event.process.ProcessInstanceDataEvent) DataEvent(org.kie.kogito.event.DataEvent) Processes(org.kie.kogito.process.Processes) VariableInstanceDataEvent(org.kie.kogito.event.process.VariableInstanceDataEvent) Model(org.kie.kogito.Model) ArrayList(java.util.ArrayList) List(java.util.List) ProcessInstanceDataEvent(org.kie.kogito.event.process.ProcessInstanceDataEvent) Application(org.kie.kogito.Application) Person(org.kie.kogito.codegen.data.Person) VariableInstanceEventBody(org.kie.kogito.event.process.VariableInstanceEventBody) Test(org.junit.jupiter.api.Test)

Example 15 with UnitOfWork

use of org.kie.kogito.uow.UnitOfWork in project kogito-runtimes by kiegroup.

the class PublishEventIT method testBasicUserTaskProcess.

@Test
public void testBasicUserTaskProcess() throws Exception {
    Application app = generateCodeProcessesOnly("usertask/UserTasksProcess.bpmn2");
    assertThat(app).isNotNull();
    Process<? extends Model> p = app.get(Processes.class).processById("UserTasksProcess");
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    m.fromMap(parameters);
    TestEventPublisher publisher = new TestEventPublisher();
    app.unitOfWorkManager().eventManager().setService("http://myhost");
    app.unitOfWorkManager().eventManager().addPublisher(publisher);
    UnitOfWork uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    uow.end();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    List<DataEvent<?>> events = publisher.extract();
    assertThat(events).isNotNull().hasSize(2);
    ProcessInstanceEventBody body = assertProcessInstanceEvent(events.get(0), "UserTasksProcess", "UserTasksProcess", 1);
    assertThat(body.getNodeInstances()).hasSize(2).extractingResultOf("getNodeType").contains("StartNode", "HumanTaskNode");
    assertThat(body.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
    // human task is active thus null for leave time
    assertThat(body.getNodeInstances()).extractingResultOf("getLeaveTime").containsNull();
    assertUserTaskInstanceEvent(events.get(1), "FirstTask", null, "1", "Ready", "UserTasksProcess", "First Task");
    List<WorkItem> workItems = processInstance.workItems(SecurityPolicy.of(new StaticIdentityProvider("john")));
    assertEquals(1, workItems.size());
    assertEquals("FirstTask", workItems.get(0).getName());
    uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    processInstance.completeWorkItem(workItems.get(0).getId(), null, SecurityPolicy.of(new StaticIdentityProvider("john")));
    uow.end();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    events = publisher.extract();
    assertThat(events).isNotNull().hasSize(3);
    body = assertProcessInstanceEvent(events.get(0), "UserTasksProcess", "UserTasksProcess", 1);
    assertThat(body.getNodeInstances()).hasSize(2).extractingResultOf("getNodeType").contains("HumanTaskNode", "HumanTaskNode");
    assertThat(body.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
    // human task is active thus null for leave time
    assertThat(body.getNodeInstances()).extractingResultOf("getLeaveTime").containsNull();
    assertUserTaskInstanceEvent(events.get(1), "SecondTask", null, "1", "Ready", "UserTasksProcess", "Second Task");
    assertUserTaskInstanceEvent(events.get(2), "FirstTask", null, "1", "Completed", "UserTasksProcess", "First Task");
    workItems = processInstance.workItems(SecurityPolicy.of(new StaticIdentityProvider("john")));
    assertEquals(1, workItems.size());
    assertEquals("SecondTask", workItems.get(0).getName());
    uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    processInstance.completeWorkItem(workItems.get(0).getId(), null, SecurityPolicy.of(new StaticIdentityProvider("john")));
    uow.end();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    events = publisher.extract();
    assertThat(events).isNotNull().hasSize(2);
    body = assertProcessInstanceEvent(events.get(0), "UserTasksProcess", "UserTasksProcess", 2);
    assertThat(body.getNodeInstances()).hasSize(2).extractingResultOf("getNodeType").contains("HumanTaskNode", "EndNode");
    assertThat(body.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
    assertThat(body.getNodeInstances()).extractingResultOf("getLeaveTime").allMatch(v -> v != null);
    assertUserTaskInstanceEvent(events.get(1), "SecondTask", null, "1", "Completed", "UserTasksProcess", "Second Task");
}
Also used : StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) UnitOfWork(org.kie.kogito.uow.UnitOfWork) HashMap(java.util.HashMap) ProcessInstanceEventBody(org.kie.kogito.event.process.ProcessInstanceEventBody) UserTaskInstanceDataEvent(org.kie.kogito.event.process.UserTaskInstanceDataEvent) VariableInstanceDataEvent(org.kie.kogito.event.process.VariableInstanceDataEvent) ProcessInstanceDataEvent(org.kie.kogito.event.process.ProcessInstanceDataEvent) DataEvent(org.kie.kogito.event.DataEvent) Processes(org.kie.kogito.process.Processes) WorkItem(org.kie.kogito.process.WorkItem) Model(org.kie.kogito.Model) Application(org.kie.kogito.Application) Test(org.junit.jupiter.api.Test)

Aggregations

UnitOfWork (org.kie.kogito.uow.UnitOfWork)22 Test (org.junit.jupiter.api.Test)20 HashMap (java.util.HashMap)14 Application (org.kie.kogito.Application)14 Model (org.kie.kogito.Model)14 Processes (org.kie.kogito.process.Processes)14 ProcessInstanceDataEvent (org.kie.kogito.event.process.ProcessInstanceDataEvent)11 ProcessInstanceEventBody (org.kie.kogito.event.process.ProcessInstanceEventBody)10 DataEvent (org.kie.kogito.event.DataEvent)9 UserTaskInstanceDataEvent (org.kie.kogito.event.process.UserTaskInstanceDataEvent)9 VariableInstanceDataEvent (org.kie.kogito.event.process.VariableInstanceDataEvent)9 List (java.util.List)8 ArrayList (java.util.ArrayList)7 Person (org.kie.kogito.codegen.data.Person)6 WorkItem (org.kie.kogito.process.WorkItem)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 StaticIdentityProvider (org.kie.kogito.services.identity.StaticIdentityProvider)3 ProcessStartedEvent (org.kie.api.event.process.ProcessStartedEvent)2 VariableInstanceEventBody (org.kie.kogito.event.process.VariableInstanceEventBody)2 DefaultKogitoProcessEventListener (org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener)2