Java Code Examples for org.kie.api.runtime.KieSession#signalEvent()

The following examples show how to use org.kie.api.runtime.KieSession#signalEvent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompensationTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void compensationOnlyAfterAssociatedActivityHasCompleted() throws Exception {
    KieSession ksession = createKnowledgeSession("compensation/BPMN2-Compensation-UserTaskBeforeAssociatedActivity.bpmn2");
    ksession.addEventListener(LOGGING_EVENT_LISTENER);
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", "0");
    ProcessInstance processInstance = ksession.startProcess("CompensateIntermediateThrowEvent", params);
    
    // should NOT cause compensation since compensated activity has not yet completed (or started)! 
    ksession.signalEvent("Compensation", "_3", processInstance.getId());
    
    // user task -> script task (associated with compensation) --> intermeidate throw compensation event
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItem().getId(), null);
    
    // compensation activity (assoc. with script task) signaled *after* to-compensate script task
    assertProcessInstanceCompleted(processInstance.getId(), ksession);
    assertProcessVarValue(processInstance, "x", "1");
}
 
Example 2
Source File: StandaloneBPMNProcessTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testEventBasedSplit4() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-EventBasedSplit4.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new SystemOutWorkItemHandler());
    // Yes
    ProcessInstance processInstance = ksession.startProcess("com.sample.test");
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    ksession = restoreSession(ksession, true);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new SystemOutWorkItemHandler());
    ksession.signalEvent("Message-YesMessage", "YesValue", processInstance.getId());
    assertProcessInstanceCompleted(processInstance.getId(), ksession);
    ksession = restoreSession(ksession, true);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new SystemOutWorkItemHandler());
    // No
    processInstance = ksession.startProcess("com.sample.test");
    ksession.signalEvent("Message-NoMessage", "NoValue", processInstance.getId());
    assertProcessInstanceCompleted(processInstance.getId(), ksession);
}
 
Example 3
Source File: StandaloneBPMNProcessTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testEventBasedSplitAfter() throws Exception {
    // signaling the other alternative after one has been selected should
    // have no effect
    KieBase kbase = createKnowledgeBase("BPMN2-EventBasedSplit.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new DoNothingWorkItemHandler());
    // Yes
    ProcessInstance processInstance = ksession.startProcess("com.sample.test");
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    ksession = restoreSession(ksession, true);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new DoNothingWorkItemHandler());
    ksession.signalEvent("Yes", "YesValue", processInstance.getId());
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    ksession = restoreSession(ksession, true);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new DoNothingWorkItemHandler());
    // No
    ksession.signalEvent("No", "NoValue", processInstance.getId());
}
 
Example 4
Source File: StandaloneBPMNProcessTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testEventBasedSplitBefore() throws Exception {
    // signaling before the split is reached should have no effect
    KieBase kbase = createKnowledgeBase("BPMN2-EventBasedSplit.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new DoNothingWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new DoNothingWorkItemHandler());
    // Yes
    ProcessInstance processInstance = ksession.startProcess("com.sample.test");
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    ksession = restoreSession(ksession, true);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new DoNothingWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new DoNothingWorkItemHandler());
    ksession.signalEvent("Yes", "YesValue", processInstance.getId());
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    // No
    processInstance = ksession.startProcess("com.sample.test");
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    ksession = restoreSession(ksession, true);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new DoNothingWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new DoNothingWorkItemHandler());
    ksession.signalEvent("No", "NoValue", processInstance.getId());
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_ACTIVE);
}
 
Example 5
Source File: StandaloneBPMNProcessTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testEventBasedSplit() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-EventBasedSplit.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new SystemOutWorkItemHandler());
    // Yes
    ProcessInstance processInstance = ksession.startProcess("com.sample.test");
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    ksession = restoreSession(ksession, true);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new SystemOutWorkItemHandler());
    ksession.signalEvent("Yes", "YesValue", processInstance.getId());
    assertProcessInstanceCompleted(processInstance.getId(), ksession);
    // No
    processInstance = ksession.startProcess("com.sample.test");
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    ksession = restoreSession(ksession, true);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new SystemOutWorkItemHandler());
    ksession.signalEvent("No", "NoValue", processInstance.getId());
    assertProcessInstanceCompleted(processInstance.getId(), ksession);
}
 
Example 6
Source File: IntermediateEventTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testIntermediateCatchEventSameSignalOnTwoKsessions() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-IntermediateCatchEventSignal.bpmn2");
    ksession = createKnowledgeSession(kbase);
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
    ProcessInstance processInstance = ksession.startProcess("IntermediateCatchEvent");

    KieBase kbase2 = createKnowledgeBase("BPMN2-IntermediateCatchEventSignal2.bpmn2");
    KieSession ksession2 = createKnowledgeSession(kbase2);
    ksession2.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
    ProcessInstance processInstance2 = ksession2.startProcess("IntermediateCatchEvent2");

    assertProcessInstanceActive(processInstance);
    assertProcessInstanceActive(processInstance2);
    ksession = restoreSession(ksession, true);
    ksession2 = restoreSession(ksession2, true);
    // now signal process instance
    ksession.signalEvent("MyMessage", "SomeValue");
    assertProcessInstanceFinished(processInstance, ksession);
    assertProcessInstanceActive(processInstance2);

    // now signal the other one
    ksession2.signalEvent("MyMessage", "SomeValue");
    assertProcessInstanceFinished(processInstance2, ksession2);
    ksession2.dispose();
}
 
Example 7
Source File: CompensationTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
@Disabled
public void compensationViaCancellation() throws Exception {
    KieSession ksession = createKnowledgeSession("compensation/BPMN2-Compensation-IntermediateThrowEvent.bpmn2");
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", "0");
    ProcessInstance processInstance = ksession.startProcess("CompensateIntermediateThrowEvent", params);

    ksession.signalEvent("Cancel", null, processInstance.getId());
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItem().getId(), null);

    // compensation activity (assoc. with script task) signaled *after* script task
    assertProcessInstanceCompleted(processInstance.getId(), ksession);
    assertProcessVarValue(processInstance, "x", "1");
}
 
Example 8
Source File: CompensationTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void compensationInSubSubProcesses() throws Exception {
    KieSession ksession = createKnowledgeSession("compensation/BPMN2-Compensation-InSubSubProcess.bpmn2");
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", "0");
    ProcessInstance processInstance = ksession.startProcess("CompensateSubSubSub", params);

    ksession.signalEvent("Compensation", "_C-2", processInstance.getId());
    
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItem().getId(), null);
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItem().getId(), null);
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItem().getId(), null);

    // compensation activity (assoc. with script task) signaled *after* script task
    assertProcessInstanceCompleted(processInstance.getId(), ksession);
    assertProcessVarValue(processInstance, "x", "2");
}
 
Example 9
Source File: SignalEventCommand.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );
    
    if (processInstanceId == null && correlationKey == null) {
        ksession.signalEvent(eventType, event);
    } else {
        ProcessInstance processInstance;
        if( correlationKey != null ) { 
            processInstance = ((CorrelationAwareProcessRuntime) ksession).getProcessInstance(correlationKey);
        } else { 
            processInstance = ksession.getProcessInstance(processInstanceId);
        }
        if (processInstance != null) {
            processInstance.signalEvent(eventType, event);
        }
    }
    return null;
}
 
Example 10
Source File: StandaloneBPMNProcessTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
@Disabled("Process does not complete.")
public void testAdHocSubProcess() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-AdHocSubProcess.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    ProcessInstance processInstance = ksession.startProcess("AdHocSubProcess");
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    WorkItem workItem = workItemHandler.getWorkItem();
    assertThat(workItem).isNull();
    ksession = restoreSession(ksession);
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    ksession.fireAllRules();
    
    ksession.signalEvent("Hello2", null, processInstance.getId());
    workItem = workItemHandler.getWorkItem();
    assertThat(workItem).isNotNull();
    ksession = restoreSession(ksession);
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    
    assertProcessInstanceCompleted(processInstance.getId(), ksession);
}
 
Example 11
Source File: CompensationTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public static void runCompensationEventSubProcessGeneralTest(KieSession ksession, RuleFlowProcess process, String processId, 
        String [] workItemNames, List<String> eventList, String compensationEvent) { 
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    for (String workItem : workItemNames) {
        ksession.getWorkItemManager().registerWorkItemHandler(workItem, workItemHandler);
    }
    ProcessInstance processInstance = ksession.startProcess(processId);

    // pre and sub process work item
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    
    // Call general compensation 

    ksession.signalEvent("Compensation", compensationEvent, processInstance.getId());
    assertEquals(1, eventList.size(), "Compensation should have fired once.");

    // post work item
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
 
Example 12
Source File: SLAComplianceTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testSLAonCatchEventNotViolated() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    final ProcessEventListener listener = new DefaultProcessEventListener(){

        @Override
        public void afterSLAViolated(SLAViolatedEvent event) {
            latch.countDown();
        }
        
    };
    KieBase kbase = createKnowledgeBase("BPMN2-IntermediateCatchEventSignalWithSLAOnEvent.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.addEventListener(listener);
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
    
    ProcessInstance processInstance = ksession.startProcess("IntermediateCatchEvent");
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);

    Collection<NodeInstance> active = ((WorkflowProcessInstance)processInstance).getNodeInstances();
    assertEquals(1, active.size());
    
    NodeInstance eventNode = active.iterator().next();
    
    ksession.signalEvent("MyMessage", null, processInstance.getId());
    
    assertProcessInstanceFinished(processInstance, ksession);        
    
    int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(ProcessInstance.SLA_NA, slaCompliance);

    slaCompliance = getSLAComplianceForNodeInstance(processInstance.getId(), (org.jbpm.workflow.instance.NodeInstance) eventNode, 1);
    assertEquals(ProcessInstance.SLA_MET, slaCompliance);
    

    boolean slaViolated = latch.await(3, TimeUnit.SECONDS);
    assertFalse(slaViolated, "SLA should not violated by timer");

    ksession.dispose();
}
 
Example 13
Source File: CompensationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public static void runCompensationEventSubProcessSpecificTest(KieSession ksession, RuleFlowProcess process, String processId, 
        String [] workItemNames, List<String> eventList, String compensationEvent) { 
    // run process
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    for (String workItem : workItemNames) {
        ksession.getWorkItemManager().registerWorkItemHandler(workItem, workItemHandler);
    }
    ProcessInstance processInstance = ksession.startProcess(processId);

    // call compensation on the uncompleted work 1 (which should not fire)
    ksession.signalEvent("Compensation", compensationEvent, processInstance.getId());
    assertEquals(0, eventList.size(), "Compensation should not have fired yet.");

    // pre work item
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);

    // sub-process is active, but not complete
    ksession.signalEvent("Compensation", compensationEvent, processInstance.getId());
    assertEquals(0, eventList.size(), "Compensation should not have fired yet.");

    // sub process work item
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    
    // sub-process has completed 
    ksession.signalEvent("Compensation", compensationEvent, processInstance.getId());
    assertEquals(1, eventList.size(), "Compensation should have fired once.");

    // post work item
    ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}
 
Example 14
Source File: StandaloneBPMNProcessTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testIntermediateCatchEventMessage() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-IntermediateCatchEventMessage.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
    ProcessInstance processInstance = ksession.startProcess("IntermediateCatchEvent");
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    ksession = restoreSession(ksession, true);
    // now signal process instance
    ksession.signalEvent("Message-HelloMessage", "SomeValue", processInstance.getId());
    assertProcessInstanceCompleted(processInstance.getId(), ksession);
}
 
Example 15
Source File: StandaloneBPMNProcessTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
@Timeout(10)
public void testEventBasedSplit2() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("timer", 2);
    KieBase kbase = createKnowledgeBase("BPMN2-EventBasedSplit2.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.addEventListener(countDownListener);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new SystemOutWorkItemHandler());
    // Yes
    ProcessInstance processInstance = ksession.startProcess("com.sample.test");
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    ksession = restoreSession(ksession, true);
    ksession.addEventListener(countDownListener);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new SystemOutWorkItemHandler());
    ksession.signalEvent("Yes", "YesValue", processInstance.getId());
    assertProcessInstanceCompleted(processInstance.getId(), ksession);
    
    ksession = restoreSession(ksession, true);
    ksession.addEventListener(countDownListener);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new SystemOutWorkItemHandler());
    ksession = restoreSession(ksession, true);
    ksession.addEventListener(countDownListener);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new SystemOutWorkItemHandler());
    // Timer
    processInstance = ksession.startProcess("com.sample.test");
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    
    countDownListener.waitTillCompleted();
    ksession = restoreSession(ksession, true);
    ksession.addEventListener(countDownListener);
    ksession.getWorkItemManager().registerWorkItemHandler("Email1", new SystemOutWorkItemHandler());
    ksession.getWorkItemManager().registerWorkItemHandler("Email2", new SystemOutWorkItemHandler());
    assertProcessInstanceCompleted(processInstance.getId(), ksession);
}
 
Example 16
Source File: StandaloneBPMNProcessTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testSignalStart() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-SignalStart.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    final List<String> list = new ArrayList<>();
    ksession.addEventListener(new DefaultProcessEventListener() {
        public void afterProcessStarted(ProcessStartedEvent event) {
            list.add(event.getProcessInstance().getId());
        }
    });
    ksession.signalEvent("MySignal", "NewValue");
    assertThat(list.size()).isEqualTo(1);
}
 
Example 17
Source File: StandaloneBPMNProcessTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testIntermediateCatchEventSignal() throws Exception {
    KieBase kbase = createKnowledgeBase("BPMN2-IntermediateCatchEventSignal.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
    ProcessInstance processInstance = ksession.startProcess("IntermediateCatchEvent");
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_ACTIVE);
    ksession = restoreSession(ksession, true);
    // now signal process instance
    ksession.signalEvent("MyMessage", "SomeValue", processInstance.getId());
    assertProcessInstanceCompleted(processInstance.getId(), ksession);
}
 
Example 18
Source File: SLAComplianceTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSLAonCatchEventViolated() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    final ProcessEventListener listener = new DefaultProcessEventListener(){

        @Override
        public void afterSLAViolated(SLAViolatedEvent event) {
            latch.countDown();
        }
        
    };
    KieBase kbase = createKnowledgeBase("BPMN2-IntermediateCatchEventSignalWithSLAOnEvent.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.addEventListener(listener);
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
    
    ProcessInstance processInstance = ksession.startProcess("IntermediateCatchEvent");
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);

    boolean slaViolated = latch.await(5, TimeUnit.SECONDS);
    assertTrue(slaViolated, "SLA should be violated by timer");

    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);

    Collection<NodeInstance> active = ((WorkflowProcessInstance)processInstance).getNodeInstances();
    assertEquals(1, active.size());
    
    NodeInstance eventNode = active.iterator().next();
    
    ksession.signalEvent("MyMessage", null, processInstance.getId());
    
    assertProcessInstanceFinished(processInstance, ksession);        
    
    int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(ProcessInstance.SLA_NA, slaCompliance);

    slaCompliance = getSLAComplianceForNodeInstance(processInstance.getId(), (org.jbpm.workflow.instance.NodeInstance) eventNode, 0);
    assertEquals(ProcessInstance.SLA_VIOLATED, slaCompliance);

    slaCompliance = getSLAComplianceForNodeInstance(processInstance.getId(), (org.jbpm.workflow.instance.NodeInstance) eventNode, 1);
    assertEquals(ProcessInstance.SLA_VIOLATED, slaCompliance);
    
    ksession.dispose();
}
 
Example 19
Source File: SLAComplianceTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSLAonUserTaskViolatedExternalTracking() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    final ProcessEventListener listener = new DefaultProcessEventListener(){

        @Override
        public void afterSLAViolated(SLAViolatedEvent event) {
            latch.countDown();
        }
        
    };
    KieBase kbase = createKnowledgeBase("BPMN2-UserTaskWithSLAOnTask.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    ksession.addEventListener(listener);
    ksession.getEnvironment().set("SLATimerMode", "false");
    
    ProcessInstance processInstance = ksession.startProcess("UserTask");
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);

    WorkItem 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 = ksession.getProcessInstance(processInstance.getId());
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);

    Collection<NodeInstance> active = ((WorkflowProcessInstance)processInstance).getNodeInstances();
    assertEquals(1, active.size());
    
    NodeInstance userTaskNode = active.iterator().next();
    
    // simulate external tracking of sla
    ksession.signalEvent("slaViolation:" + userTaskNode.getId(), null, processInstance.getId());
    
    slaViolated = latch.await(10, TimeUnit.SECONDS);
    assertTrue(slaViolated, "SLA was not violated while it is expected");

    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    assertProcessInstanceFinished(processInstance, ksession);        
    
    int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(ProcessInstance.SLA_NA, slaCompliance);

    slaCompliance = getSLAComplianceForNodeInstance(processInstance.getId(), (org.jbpm.workflow.instance.NodeInstance) userTaskNode, 0);
    assertEquals(ProcessInstance.SLA_VIOLATED, slaCompliance);

    slaCompliance = getSLAComplianceForNodeInstance(processInstance.getId(), (org.jbpm.workflow.instance.NodeInstance) userTaskNode, 1);
    assertEquals(ProcessInstance.SLA_VIOLATED, slaCompliance);
    
    ksession.dispose();
}
 
Example 20
Source File: SLAComplianceTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSLAonProcessViolatedExternalTracking() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    final ProcessEventListener listener = new DefaultProcessEventListener(){

        @Override
        public void afterSLAViolated(SLAViolatedEvent event) {
            latch.countDown();
        }
        
    };
    KieBase kbase = createKnowledgeBase("BPMN2-UserTaskWithSLA.bpmn2");
    KieSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
    ksession.addEventListener(listener);
    ksession.getEnvironment().set("SLATimerMode", "false");
    
    ProcessInstance processInstance = ksession.startProcess("UserTask");
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);

    WorkItem 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");

    // simulate external tracking of sla
    ksession.signalEvent("slaViolation", null, processInstance.getId());
    
    slaViolated = latch.await(10, TimeUnit.SECONDS);
    assertTrue(slaViolated, "SLA was not violated while it is expected");

    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);

    int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(ProcessInstance.SLA_VIOLATED, slaCompliance);
    
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    assertProcessInstanceFinished(processInstance, ksession);        
    
    slaCompliance = getSLAComplianceForProcessInstance(processInstance);
    assertEquals(ProcessInstance.SLA_VIOLATED, slaCompliance);
    
    ksession.dispose();
}