Search in sources :

Example 1 with UnitOfWork

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

the class BusinessRuleTaskIT method testBasicBusinessRuleTaskControlledByUnitOfWork.

@Test
public void testBasicBusinessRuleTaskControlledByUnitOfWork() throws Exception {
    Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
    resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList("ruletask/BusinessRuleTask.bpmn2"));
    resourcesTypeMap.put(TYPE.RULES, Collections.singletonList("ruletask/BusinessRuleTask.drl"));
    Application app = generateCode(resourcesTypeMap);
    assertThat(app).isNotNull();
    final List<String> startedProcesses = new ArrayList<>();
    // add custom event listener that collects data
    app.config().get(ProcessConfig.class).processEventListeners().listeners().add(new DefaultKogitoProcessEventListener() {

        @Override
        public void beforeProcessStarted(ProcessStartedEvent event) {
            startedProcesses.add(((KogitoProcessInstance) event.getProcessInstance()).getStringId());
        }
    });
    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);
    // since the unit of work has not been finished yet not listeners where invoked
    assertThat(startedProcesses).hasSize(0);
    uow.end();
    // after unit of work has been ended listeners are invoked
    assertThat(startedProcesses).hasSize(1);
}
Also used : UnitOfWork(org.kie.kogito.uow.UnitOfWork) ProcessConfig(org.kie.kogito.process.ProcessConfig) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) Processes(org.kie.kogito.process.Processes) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) Model(org.kie.kogito.Model) ArrayList(java.util.ArrayList) List(java.util.List) Application(org.kie.kogito.Application) Person(org.kie.kogito.codegen.data.Person) Test(org.junit.jupiter.api.Test)

Example 2 with UnitOfWork

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

the class PublishEventIT method testExclusiveGatewayStartToEnd.

@Test
public void testExclusiveGatewayStartToEnd() throws Exception {
    Application app = generateCodeProcessesOnly("gateway/ExclusiveSplit.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("ExclusiveSplit");
    Map<String, Object> params = new HashMap<>();
    params.put("x", "First");
    params.put("y", "None");
    Model m = p.createModel();
    m.fromMap(params);
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    Model result = (Model) processInstance.variables();
    assertThat(result.toMap()).hasSize(2).containsKeys("x", "y");
    uow.end();
    List<DataEvent<?>> events = publisher.extract();
    assertThat(events).isNotNull().hasSize(1);
    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("ExclusiveSplit");
    assertThat(processDataEvent.getKogitoProcessinstanceState()).isEqualTo("2");
    ProcessInstanceEventBody body = assertProcessInstanceEvent(events.get(0), "ExclusiveSplit", "Test", 2);
    assertThat(body.getNodeInstances()).hasSize(6).extractingResultOf("getNodeType").contains("StartNode", "ActionNode", "Split", "Join", "EndNode", "WorkItemNode");
    assertThat(body.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
    assertThat(body.getNodeInstances()).extractingResultOf("getLeaveTime").allMatch(v -> v != null);
}
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) Model(org.kie.kogito.Model) ProcessInstanceDataEvent(org.kie.kogito.services.event.ProcessInstanceDataEvent) Application(org.kie.kogito.Application) Test(org.junit.jupiter.api.Test)

Example 3 with UnitOfWork

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

the class PublishEventIT method testProcessWithMilestoneEvents.

@Test
public void testProcessWithMilestoneEvents() throws Exception {
    Application app = generateCodeProcessesOnly("cases/milestones/SimpleMilestone.bpmn");
    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("TestCase.SimpleMilestone");
    ProcessInstance<?> processInstance = p.createInstance(p.createModel());
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    uow.end();
    List<DataEvent<?>> events = publisher.extract();
    assertThat(events).isNotNull().hasSize(1);
    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("TestCase.SimpleMilestone");
    assertThat(processDataEvent.getKogitoProcessinstanceState()).isEqualTo("2");
    assertThat(processDataEvent.getSource().toString()).isEqualTo("http://myhost/SimpleMilestone");
    Set<MilestoneEventBody> milestones = ((ProcessInstanceDataEvent) event).getData().getMilestones();
    assertThat(milestones).hasSize(2).extracting(e -> e.getName(), e -> e.getStatus()).containsExactlyInAnyOrder(tuple("AutoStartMilestone", Status.COMPLETED.name()), tuple("SimpleMilestone", Status.COMPLETED.name()));
}
Also used : UnitOfWork(org.kie.kogito.uow.UnitOfWork) ZonedDateTime(java.time.ZonedDateTime) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UserTaskInstanceEventBody(org.kie.kogito.services.event.impl.UserTaskInstanceEventBody) HashMap(java.util.HashMap) Status(org.kie.kogito.process.flexible.ItemDescription.Status) Person(org.kie.kogito.codegen.data.Person) ArrayList(java.util.ArrayList) AbstractCodegenIT(org.kie.kogito.codegen.AbstractCodegenIT) VariableInstanceEventBody(org.kie.kogito.services.event.impl.VariableInstanceEventBody) Map(java.util.Map) UserTaskInstanceDataEvent(org.kie.kogito.services.event.UserTaskInstanceDataEvent) Process(org.kie.kogito.process.Process) ProcessInstanceDataEvent(org.kie.kogito.services.event.ProcessInstanceDataEvent) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Assertions.tuple(org.assertj.core.api.Assertions.tuple) StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) Collection(java.util.Collection) Set(java.util.Set) MilestoneEventBody(org.kie.kogito.services.event.impl.MilestoneEventBody) ProcessInstanceEventBody(org.kie.kogito.services.event.impl.ProcessInstanceEventBody) SecurityPolicy(org.kie.kogito.auth.SecurityPolicy) Application(org.kie.kogito.Application) Test(org.junit.jupiter.api.Test) List(java.util.List) ProcessInstance(org.kie.kogito.process.ProcessInstance) Model(org.kie.kogito.Model) EventPublisher(org.kie.kogito.event.EventPublisher) ProcessError(org.kie.kogito.process.ProcessError) DataEvent(org.kie.kogito.event.DataEvent) VariableInstanceDataEvent(org.kie.kogito.services.event.VariableInstanceDataEvent) Processes(org.kie.kogito.process.Processes) Collections(java.util.Collections) WorkItem(org.kie.kogito.process.WorkItem) UnitOfWork(org.kie.kogito.uow.UnitOfWork) 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) MilestoneEventBody(org.kie.kogito.services.event.impl.MilestoneEventBody) ProcessInstanceDataEvent(org.kie.kogito.services.event.ProcessInstanceDataEvent) Application(org.kie.kogito.Application) Test(org.junit.jupiter.api.Test)

Example 4 with UnitOfWork

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

the class PublishEventIT method testBasicUserTaskProcessAbort.

@Test
public void testBasicUserTaskProcessAbort() 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.abort();
    uow.end();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED);
    events = publisher.extract();
    assertThat(events).isNotNull().hasSize(2);
    body = assertProcessInstanceEvent(events.get(0), "UserTasksProcess", "UserTasksProcess", ProcessInstance.STATE_ABORTED);
    assertThat(body.getNodeInstances()).hasSize(1).extractingResultOf("getNodeType").contains("HumanTaskNode");
    assertThat(body.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
    assertThat(body.getNodeInstances()).extractingResultOf("getLeaveTime").allMatch(v -> v != null);
    assertUserTaskInstanceEvent(events.get(1), "FirstTask", null, "1", "Aborted", "UserTasksProcess", "First Task");
}
Also used : StaticIdentityProvider(org.kie.kogito.services.identity.StaticIdentityProvider) 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) WorkItem(org.kie.kogito.process.WorkItem) Model(org.kie.kogito.Model) Application(org.kie.kogito.Application) Test(org.junit.jupiter.api.Test)

Example 5 with UnitOfWork

use of org.kie.kogito.uow.UnitOfWork 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)

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 DataEvent (org.kie.kogito.event.DataEvent)11 ProcessInstanceDataEvent (org.kie.kogito.services.event.ProcessInstanceDataEvent)11 UserTaskInstanceDataEvent (org.kie.kogito.services.event.UserTaskInstanceDataEvent)11 VariableInstanceDataEvent (org.kie.kogito.services.event.VariableInstanceDataEvent)11 ProcessInstanceEventBody (org.kie.kogito.services.event.impl.ProcessInstanceEventBody)10 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 DefaultKogitoProcessEventListener (org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener)2 KogitoProcessInstance (org.kie.kogito.internal.process.runtime.KogitoProcessInstance)2