Search in sources :

Example 1 with DefaultKogitoProcessEventListener

use of org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener 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 DefaultKogitoProcessEventListener

use of org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener in project kogito-runtimes by kiegroup.

the class UserTaskIT method testBasicUserTaskProcessClaimAndCompletePhasesWithIdentity.

@Test
public void testBasicUserTaskProcessClaimAndCompletePhasesWithIdentity() throws Exception {
    Application app = generateCodeProcessesOnly("usertask/UserTasksProcess.bpmn2");
    assertThat(app).isNotNull();
    final List<String> workItemTransitionEvents = new ArrayList<>();
    app.config().get(ProcessConfig.class).processEventListeners().listeners().add(new DefaultKogitoProcessEventListener() {

        @Override
        public void beforeWorkItemTransition(ProcessWorkItemTransitionEvent event) {
            workItemTransitionEvents.add("BEFORE:: " + event);
        }

        @Override
        public void afterWorkItemTransition(ProcessWorkItemTransitionEvent event) {
            workItemTransitionEvents.add("AFTER:: " + event);
        }
    });
    Process<? extends Model> p = app.get(Processes.class).processById("UserTasksProcess");
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    m.fromMap(parameters);
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    List<WorkItem> workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    WorkItem wi = workItems.get(0);
    assertEquals("FirstTask", wi.getName());
    assertEquals(Active.ID, wi.getPhase());
    assertEquals(Active.STATUS, wi.getPhaseStatus());
    assertEquals(0, wi.getResults().size());
    processInstance.transitionWorkItem(workItems.get(0).getId(), new HumanTaskTransition(Claim.ID, Collections.singletonMap("test", "value"), securityPolicy));
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    wi = workItems.get(0);
    assertEquals("FirstTask", wi.getName());
    assertEquals(Claim.ID, wi.getPhase());
    assertEquals(Claim.STATUS, wi.getPhaseStatus());
    assertEquals(2, wi.getResults().size());
    assertEquals("value", wi.getResults().get("test"));
    assertEquals("john", wi.getResults().get("ActorId"));
    processInstance.transitionWorkItem(workItems.get(0).getId(), new HumanTaskTransition(Complete.ID, null, securityPolicy));
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    wi = workItems.get(0);
    assertEquals("SecondTask", wi.getName());
    assertEquals(Active.ID, wi.getPhase());
    assertEquals(Active.STATUS, wi.getPhaseStatus());
    assertEquals(0, wi.getResults().size());
    processInstance.abort();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED);
    assertThat(workItemTransitionEvents).hasSize(10);
}
Also used : ProcessConfig(org.kie.kogito.process.ProcessConfig) HashMap(java.util.HashMap) ProcessWorkItemTransitionEvent(org.kie.kogito.internal.process.event.ProcessWorkItemTransitionEvent) ArrayList(java.util.ArrayList) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) Processes(org.kie.kogito.process.Processes) WorkItem(org.kie.kogito.process.WorkItem) HumanTaskTransition(org.jbpm.process.instance.impl.humantask.HumanTaskTransition) Model(org.kie.kogito.Model) Application(org.kie.kogito.Application) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultKogitoProcessEventListener

use of org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener in project kogito-runtimes by kiegroup.

the class UserTaskIT method testBasicUserTaskProcess.

@Test
public void testBasicUserTaskProcess() throws Exception {
    Application app = generateCodeProcessesOnly("usertask/UserTasksProcess.bpmn2");
    assertThat(app).isNotNull();
    final List<String> workItemTransitionEvents = new ArrayList<>();
    app.config().get(ProcessConfig.class).processEventListeners().listeners().add(new DefaultKogitoProcessEventListener() {

        @Override
        public void beforeWorkItemTransition(ProcessWorkItemTransitionEvent event) {
            workItemTransitionEvents.add("BEFORE:: " + event);
        }

        @Override
        public void afterWorkItemTransition(ProcessWorkItemTransitionEvent event) {
            workItemTransitionEvents.add("AFTER:: " + event);
        }
    });
    Process<? extends Model> p = app.get(Processes.class).processById("UserTasksProcess");
    Model m = p.createModel();
    Map<String, Object> parameters = new HashMap<>();
    m.fromMap(parameters);
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(KogitoProcessInstance.STATE_ACTIVE);
    List<WorkItem> workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    assertEquals("FirstTask", workItems.get(0).getName());
    processInstance.completeWorkItem(workItems.get(0).getId(), null, securityPolicy);
    assertThat(processInstance.status()).isEqualTo(KogitoProcessInstance.STATE_ACTIVE);
    workItems = processInstance.workItems(securityPolicy);
    assertEquals(1, workItems.size());
    assertEquals("SecondTask", workItems.get(0).getName());
    processInstance.completeWorkItem(workItems.get(0).getId(), null, securityPolicy);
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    assertThat(workItemTransitionEvents).hasSize(8);
}
Also used : ProcessConfig(org.kie.kogito.process.ProcessConfig) HashMap(java.util.HashMap) ProcessWorkItemTransitionEvent(org.kie.kogito.internal.process.event.ProcessWorkItemTransitionEvent) ArrayList(java.util.ArrayList) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) 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 4 with DefaultKogitoProcessEventListener

use of org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener in project kogito-runtimes by kiegroup.

the class ActivityTest method testMinimalProcessMetaData.

@Test
public void testMinimalProcessMetaData() throws Exception {
    kruntime = createKogitoProcessRuntime("BPMN2-MinimalProcessMetaData.bpmn2");
    final List<String> list1 = new ArrayList<>();
    final List<String> list2 = new ArrayList<>();
    final List<String> list3 = new ArrayList<>();
    final List<String> list4 = new ArrayList<>();
    kruntime.getProcessEventManager().addEventListener(new DefaultKogitoProcessEventListener() {

        @Override
        public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
            logger.debug("before node");
            Map<String, Object> metaData = event.getNodeInstance().getNode().getMetaData();
            for (Map.Entry<String, Object> entry : metaData.entrySet()) {
                logger.debug(entry.getKey() + " " + entry.getValue());
            }
            String customTag = (String) metaData.get("customTag");
            if (customTag != null) {
                list1.add(customTag);
            }
            String customTag2 = (String) metaData.get("customTag2");
            if (customTag2 != null) {
                list2.add(customTag2);
            }
        }

        @Override
        public void afterVariableChanged(ProcessVariableChangedEvent event) {
            logger.debug("after variable");
            VariableScope variableScope = (VariableScope) ((org.jbpm.process.core.impl.ProcessImpl) event.getProcessInstance().getProcess()).resolveContext(VariableScope.VARIABLE_SCOPE, event.getVariableId());
            if (variableScope == null) {
                return;
            }
            Map<String, Object> metaData = variableScope.findVariable(event.getVariableId()).getMetaData();
            for (Map.Entry<String, Object> entry : metaData.entrySet()) {
                logger.debug(entry.getKey() + " " + entry.getValue());
            }
            String customTag = (String) metaData.get("customTagVar");
            if (customTag != null) {
                list3.add(customTag);
            }
        }

        @Override
        public void afterProcessStarted(ProcessStartedEvent event) {
            logger.debug("after process");
            Map<String, Object> metaData = event.getProcessInstance().getProcess().getMetaData();
            for (Map.Entry<String, Object> entry : metaData.entrySet()) {
                logger.debug(entry.getKey() + " " + entry.getValue());
            }
            String customTag = (String) metaData.get("customTagProcess");
            if (customTag != null) {
                list4.add(customTag);
            }
        }
    });
    Map<String, Object> params = new HashMap<>();
    params.put("x", "krisv");
    KogitoProcessInstance processInstance = kruntime.startProcess("Minimal", params);
    assertProcessInstanceCompleted(processInstance);
    assertEquals(3, list1.size());
    assertEquals(2, list2.size());
    assertEquals(1, list3.size());
    assertEquals(1, list4.size());
}
Also used : ProcessVariableChangedEvent(org.kie.api.event.process.ProcessVariableChangedEvent) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) ProcessNodeTriggeredEvent(org.kie.api.event.process.ProcessNodeTriggeredEvent) Map(java.util.Map) HashMap(java.util.HashMap) VariableScope(org.jbpm.process.core.context.variable.VariableScope) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultKogitoProcessEventListener

use of org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener in project kogito-runtimes by kiegroup.

the class ActivityTest method testCallActivityWithTransformation.

@Test
@Disabled("Transformer has been disabled")
public void testCallActivityWithTransformation() throws Exception {
    kruntime = createKogitoProcessRuntime("BPMN2-CallActivityWithTransformation.bpmn2", "BPMN2-CallActivitySubProcess.bpmn2");
    final List<KogitoProcessInstance> instances = new ArrayList<>();
    kruntime.getProcessEventManager().addEventListener(new DefaultKogitoProcessEventListener() {

        @Override
        public void beforeProcessStarted(ProcessStartedEvent event) {
            instances.add((KogitoProcessInstance) event.getProcessInstance());
        }
    });
    Map<String, Object> params = new HashMap<>();
    params.put("x", "oldValue");
    KogitoProcessInstance processInstance = kruntime.startProcess("ParentProcess", params);
    assertProcessInstanceCompleted(processInstance);
    assertEquals(2, instances.size());
    // assert variables of parent process, first in start (input transformation, then on end output transformation)
    assertEquals("oldValue", ((KogitoWorkflowProcessInstance) instances.get(0)).getVariable("x"));
    assertEquals("NEW VALUE", ((KogitoWorkflowProcessInstance) instances.get(0)).getVariable("y"));
    // assert variables of subprocess, first in start (input transformation, then on end output transformation)
    assertEquals("OLDVALUE", ((KogitoWorkflowProcessInstance) instances.get(1)).getVariable("subX"));
    assertEquals("new value", ((KogitoWorkflowProcessInstance) instances.get(1)).getVariable("subY"));
}
Also used : HashMap(java.util.HashMap) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

DefaultKogitoProcessEventListener (org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener)68 Test (org.junit.jupiter.api.Test)65 KogitoProcessInstance (org.kie.kogito.internal.process.runtime.KogitoProcessInstance)65 ArrayList (java.util.ArrayList)47 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)32 ProcessStartedEvent (org.kie.api.event.process.ProcessStartedEvent)31 KogitoWorkItem (org.kie.kogito.internal.process.runtime.KogitoWorkItem)28 HashMap (java.util.HashMap)21 KogitoProcessEventListener (org.kie.kogito.internal.process.event.KogitoProcessEventListener)20 ProcessNodeLeftEvent (org.kie.api.event.process.ProcessNodeLeftEvent)14 ProcessNodeTriggeredEvent (org.kie.api.event.process.ProcessNodeTriggeredEvent)14 NodeLeftCountDownProcessEventListener (org.jbpm.test.util.NodeLeftCountDownProcessEventListener)12 Timeout (org.junit.jupiter.api.Timeout)11 CountDownLatch (java.util.concurrent.CountDownLatch)8 SLAViolatedEvent (org.kie.api.event.process.SLAViolatedEvent)8 InternalKogitoWorkItem (org.kie.kogito.process.workitems.InternalKogitoWorkItem)7 Map (java.util.Map)6 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)6 Disabled (org.junit.jupiter.api.Disabled)6 KogitoWorkflowProcessInstance (org.kie.kogito.internal.process.runtime.KogitoWorkflowProcessInstance)5