Search in sources :

Example 1 with KogitoProcessEventListener

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

the class IntermediateEventTest method testEventSubprocessMessageWithLocalVars.

@Test
@Timeout(10)
public void testEventSubprocessMessageWithLocalVars() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 1);
    kruntime = createKogitoProcessRuntime("subprocess/BPMN2-EventSubProcessWithLocalVariables.bpmn2");
    final Set<String> variablevalues = new HashSet<String>();
    KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() {

        @Override
        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            @SuppressWarnings("unchecked") Map<String, String> variable = (Map<String, String>) event.getNodeInstance().getVariable("richiesta");
            if (variable != null) {
                variablevalues.addAll(variable.keySet());
            }
        }
    };
    kruntime.getProcessEventManager().addEventListener(listener);
    kruntime.getProcessEventManager().addEventListener(countDownListener);
    KogitoProcessInstance processInstance = kruntime.startProcess("EventSPWithVars");
    assertProcessInstanceActive(processInstance);
    Map<String, String> data = new HashMap<>();
    kruntime.signalEvent("Message-MAIL", data, processInstance.getStringId());
    countDownListener.waitTillCompleted();
    processInstance = kruntime.getProcessInstance(processInstance.getStringId());
    assertThat(processInstance).isNull();
    assertThat(variablevalues.size()).isEqualTo(2);
    assertThat(variablevalues.contains("SCRIPT1")).isTrue();
    assertThat(variablevalues.contains("SCRIPT2")).isTrue();
}
Also used : NodeLeftCountDownProcessEventListener(org.jbpm.test.util.NodeLeftCountDownProcessEventListener) HashMap(java.util.HashMap) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) KogitoProcessEventListener(org.kie.kogito.internal.process.event.KogitoProcessEventListener) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 2 with KogitoProcessEventListener

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

the class ProcessFactoryTest method testBoundaryErrorEvent.

@Test
public void testBoundaryErrorEvent() throws Exception {
    final String boundaryErrorEvent = "BoundaryErrorEvent";
    final String errorCode = "java.lang.RuntimeException";
    final String processId = "myProcess";
    final RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess(processId);
    final String startNode = "Start";
    final String task = "Task";
    final String endOnError = "EndOnError";
    factory.name("My process").packageName("org.kie.kogito").startNode(1).name(startNode).metaData(UNIQUE_ID, startNode).done().workItemNode(2).name(task).workName(task).done().endNode(3).name("EndOnSuccess").done().boundaryEventNode(4).name(boundaryErrorEvent).attachedTo(2).metaData(ERROR_EVENT, errorCode).metaData(HAS_ERROR_EVENT, true).metaData(ERROR_STRUCTURE_REF, null).metaData("EventTpe", "error").metaData(UNIQUE_ID, boundaryErrorEvent).eventType("Error", errorCode).done().endNode(5).name(endOnError).metaData(UNIQUE_ID, endOnError).terminate(true).done().connection(1, 2).connection(2, 3).connection(4, 5);
    final RuleFlowProcess process = factory.validate().getProcess();
    Application application = mock(Application.class);
    when(application.get(Processes.class)).thenReturn(new BpmnProcesses().addProcess(new BpmnProcess(process)));
    final LightProcessRuntime processRuntime = LightProcessRuntime.of(application, Collections.singletonList(process), new LightProcessRuntimeServiceProvider());
    processRuntime.getKogitoWorkItemManager().registerWorkItemHandler(task, new ExceptionOnPurposeHandler());
    final List<String> completedNodes = new ArrayList<>();
    final KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() {

        @Override
        public void afterNodeLeft(ProcessNodeLeftEvent event) {
            completedNodes.add(event.getNodeInstance().getNodeName());
            super.afterNodeLeft(event);
        }
    };
    processRuntime.addEventListener(listener);
    ProcessInstance processInstance = processRuntime.startProcess(processId);
    assertThat(processInstance.getState()).isEqualTo(KogitoProcessInstance.STATE_COMPLETED);
    assertThat(completedNodes).contains(startNode, task, boundaryErrorEvent, endOnError);
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) BpmnProcess(org.kie.kogito.process.bpmn2.BpmnProcess) LightProcessRuntime(org.jbpm.process.instance.LightProcessRuntime) ArrayList(java.util.ArrayList) LightProcessRuntimeServiceProvider(org.jbpm.process.instance.LightProcessRuntimeServiceProvider) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) BpmnProcesses(org.kie.kogito.process.bpmn2.BpmnProcesses) RuleFlowProcessFactory(org.jbpm.ruleflow.core.RuleFlowProcessFactory) ExceptionOnPurposeHandler(org.jbpm.bpmn2.objects.ExceptionOnPurposeHandler) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) KogitoProcessEventListener(org.kie.kogito.internal.process.event.KogitoProcessEventListener) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ProcessNodeLeftEvent(org.kie.api.event.process.ProcessNodeLeftEvent) Application(org.kie.kogito.Application) Test(org.junit.jupiter.api.Test)

Example 3 with KogitoProcessEventListener

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

the class SLAComplianceTest method testSLAonProcessViolatedWithExpression.

@Test
public void testSLAonProcessViolatedWithExpression() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    final KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() {

        @Override
        public void afterSLAViolated(SLAViolatedEvent event) {
            latch.countDown();
        }
    };
    kruntime = createKogitoProcessRuntime("BPMN2-UserTaskWithSLAExpr.bpmn2");
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    kruntime.getProcessEventManager().addEventListener(listener);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("s", "3s");
    KogitoProcessInstance processInstance = kruntime.startProcess("UserTask", parameters);
    assertEquals(KogitoProcessInstance.STATE_ACTIVE, processInstance.getState());
    KogitoWorkItem workItem = workItemHandler.getWorkItem();
    assertNotNull(workItem);
    assertEquals("john", workItem.getParameter("ActorId"));
    boolean slaViolated = latch.await(10, TimeUnit.SECONDS);
    assertTrue(slaViolated, "SLA was not violated while it is expected");
    processInstance = kruntime.getProcessInstance(processInstance.getStringId());
    assertEquals(KogitoProcessInstance.STATE_ACTIVE, processInstance.getState());
    int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(KogitoProcessInstance.SLA_VIOLATED, slaCompliance);
    kruntime.getKogitoWorkItemManager().completeWorkItem(workItem.getStringId(), null);
    assertProcessInstanceFinished(processInstance, kruntime);
    slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(KogitoProcessInstance.SLA_VIOLATED, slaCompliance);
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) HashMap(java.util.HashMap) SLAViolatedEvent(org.kie.api.event.process.SLAViolatedEvent) KogitoWorkItem(org.kie.kogito.internal.process.runtime.KogitoWorkItem) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) CountDownLatch(java.util.concurrent.CountDownLatch) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) KogitoProcessEventListener(org.kie.kogito.internal.process.event.KogitoProcessEventListener) Test(org.junit.jupiter.api.Test)

Example 4 with KogitoProcessEventListener

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

the class SLAComplianceTest method testSLAonUserTaskViolatedExternalTracking.

@Test
public void testSLAonUserTaskViolatedExternalTracking() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    final KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() {

        @Override
        public void afterSLAViolated(SLAViolatedEvent event) {
            latch.countDown();
        }
    };
    kruntime = createKogitoProcessRuntime("BPMN2-UserTaskWithSLAOnTask.bpmn2");
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    kruntime.getProcessEventManager().addEventListener(listener);
    kruntime.getKieRuntime().getEnvironment().set("SLATimerMode", "false");
    KogitoProcessInstance processInstance = kruntime.startProcess("UserTask");
    assertEquals(KogitoProcessInstance.STATE_ACTIVE, processInstance.getState());
    KogitoWorkItem workItem = workItemHandler.getWorkItem();
    assertNotNull(workItem);
    assertEquals("john", workItem.getParameter("ActorId"));
    boolean slaViolated = latch.await(5, TimeUnit.SECONDS);
    assertFalse(slaViolated, "SLA should not violated by timer");
    processInstance = kruntime.getProcessInstance(processInstance.getStringId());
    assertEquals(KogitoProcessInstance.STATE_ACTIVE, processInstance.getState());
    Collection<KogitoNodeInstance> active = ((KogitoWorkflowProcessInstance) processInstance).getKogitoNodeInstances();
    assertEquals(1, active.size());
    KogitoNodeInstance userTaskNode = active.iterator().next();
    // simulate external tracking of sla
    kruntime.signalEvent("slaViolation:" + userTaskNode.getStringId(), null, processInstance.getStringId());
    slaViolated = latch.await(10, TimeUnit.SECONDS);
    assertTrue(slaViolated, "SLA was not violated while it is expected");
    kruntime.getKogitoWorkItemManager().completeWorkItem(workItem.getStringId(), null);
    assertProcessInstanceFinished(processInstance, kruntime);
    int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(KogitoProcessInstance.SLA_NA, slaCompliance);
    slaCompliance = getSLAComplianceForNodeInstance(processInstance.getStringId(), (org.jbpm.workflow.instance.NodeInstance) userTaskNode, 0);
    assertEquals(KogitoProcessInstance.SLA_VIOLATED, slaCompliance);
    slaCompliance = getSLAComplianceForNodeInstance(processInstance.getStringId(), (org.jbpm.workflow.instance.NodeInstance) userTaskNode, 1);
    assertEquals(KogitoProcessInstance.SLA_VIOLATED, slaCompliance);
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) SLAViolatedEvent(org.kie.api.event.process.SLAViolatedEvent) KogitoWorkItem(org.kie.kogito.internal.process.runtime.KogitoWorkItem) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) CountDownLatch(java.util.concurrent.CountDownLatch) KogitoWorkflowProcessInstance(org.kie.kogito.internal.process.runtime.KogitoWorkflowProcessInstance) KogitoNodeInstance(org.kie.kogito.internal.process.runtime.KogitoNodeInstance) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) KogitoProcessEventListener(org.kie.kogito.internal.process.event.KogitoProcessEventListener) KogitoNodeInstance(org.kie.kogito.internal.process.runtime.KogitoNodeInstance) Test(org.junit.jupiter.api.Test)

Example 5 with KogitoProcessEventListener

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

the class SLAComplianceTest method testSLAonCatchEventNotViolated.

@Test
public void testSLAonCatchEventNotViolated() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    final KogitoProcessEventListener listener = new DefaultKogitoProcessEventListener() {

        @Override
        public void afterSLAViolated(SLAViolatedEvent event) {
            latch.countDown();
        }
    };
    kruntime = createKogitoProcessRuntime("BPMN2-IntermediateCatchEventSignalWithSLAOnEvent.bpmn2");
    kruntime.getProcessEventManager().addEventListener(listener);
    kruntime.getKogitoWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
    KogitoProcessInstance processInstance = kruntime.startProcess("IntermediateCatchEvent");
    assertEquals(KogitoProcessInstance.STATE_ACTIVE, processInstance.getState());
    Collection<KogitoNodeInstance> active = ((KogitoWorkflowProcessInstance) processInstance).getKogitoNodeInstances();
    assertEquals(1, active.size());
    KogitoNodeInstance eventNode = active.iterator().next();
    kruntime.signalEvent("MyMessage", null, processInstance.getStringId());
    assertProcessInstanceFinished(processInstance, kruntime);
    int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(KogitoProcessInstance.SLA_NA, slaCompliance);
    slaCompliance = getSLAComplianceForNodeInstance(processInstance.getStringId(), (org.jbpm.workflow.instance.NodeInstance) eventNode, 1);
    assertEquals(KogitoProcessInstance.SLA_MET, slaCompliance);
    boolean slaViolated = latch.await(3, TimeUnit.SECONDS);
    assertFalse(slaViolated, "SLA should not violated by timer");
}
Also used : SLAViolatedEvent(org.kie.api.event.process.SLAViolatedEvent) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) CountDownLatch(java.util.concurrent.CountDownLatch) KogitoWorkflowProcessInstance(org.kie.kogito.internal.process.runtime.KogitoWorkflowProcessInstance) KogitoNodeInstance(org.kie.kogito.internal.process.runtime.KogitoNodeInstance) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) KogitoProcessEventListener(org.kie.kogito.internal.process.event.KogitoProcessEventListener) KogitoNodeInstance(org.kie.kogito.internal.process.runtime.KogitoNodeInstance) Test(org.junit.jupiter.api.Test)

Aggregations

KogitoProcessEventListener (org.kie.kogito.internal.process.event.KogitoProcessEventListener)23 Test (org.junit.jupiter.api.Test)21 KogitoProcessInstance (org.kie.kogito.internal.process.runtime.KogitoProcessInstance)21 DefaultKogitoProcessEventListener (org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener)20 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)14 KogitoWorkItem (org.kie.kogito.internal.process.runtime.KogitoWorkItem)14 ProcessNodeLeftEvent (org.kie.api.event.process.ProcessNodeLeftEvent)13 ArrayList (java.util.ArrayList)11 SLAViolatedEvent (org.kie.api.event.process.SLAViolatedEvent)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 KogitoNodeInstance (org.kie.kogito.internal.process.runtime.KogitoNodeInstance)5 KogitoWorkflowProcessInstance (org.kie.kogito.internal.process.runtime.KogitoWorkflowProcessInstance)4 InternalKogitoWorkItem (org.kie.kogito.process.workitems.InternalKogitoWorkItem)4 SignalEvent (org.kie.api.event.process.SignalEvent)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)2 Application (org.kie.kogito.Application)2 EventDescription (org.kie.kogito.process.EventDescription)2 ProcessConfig (org.kie.kogito.process.ProcessConfig)2