Search in sources :

Example 1 with TrackingAgendaEventListener

use of org.jbpm.test.listener.TrackingAgendaEventListener in project jbpm by kiegroup.

the class FireUntilHaltTest method testFireUntilHaltWithProcess.

@Test(timeout = 30000)
public void testFireUntilHaltWithProcess() throws Exception {
    Map<String, ResourceType> res = new HashMap<String, ResourceType>();
    res.put(PROCESS, ResourceType.BPMN2);
    res.put(PROCESS_DRL, ResourceType.DRL);
    final KieSession ksession = createKSession(res);
    ksession.getEnvironment().set("org.jbpm.rule.task.waitstate", true);
    TrackingAgendaEventListener listener = new TrackingAgendaEventListener();
    ksession.addEventListener(listener);
    // thread for firing until halt
    ExecutorService thread = Executors.newSingleThreadExecutor();
    thread.submit(new Runnable() {

        @Override
        public void run() {
            ksession.fireUntilHalt();
        }
    });
    int wantedPersonsNum = 3;
    int unwantedPersonsNum = 2;
    Person p;
    // insert 3 wanted persons
    for (int i = 0; i < wantedPersonsNum; i++) {
        p = new Person("wanted person");
        p.setId(i);
        ksession.insert(p);
    }
    // insert 2 unwanted persons
    for (int i = 0; i < unwantedPersonsNum; i++) {
        p = new Person("unwanted person");
        p.setId(i);
        ksession.insert(p);
    }
    // wait for rule to fire
    Thread.sleep(100);
    // 8 persons should be acknowledged - person detector rule fired
    Assertions.assertThat(listener.ruleFiredCount("person detector")).isEqualTo(wantedPersonsNum + unwantedPersonsNum);
    // we start defined process
    ksession.startProcess(PROCESS_ID);
    Thread.sleep(100);
    Assertions.assertThat(listener.ruleFiredCount("initial actions")).isEqualTo(1);
    Assertions.assertThat(listener.ruleFiredCount("wanted person detector")).isEqualTo(wantedPersonsNum);
    Assertions.assertThat(listener.ruleFiredCount("change unwanted person to wanted")).isEqualTo(unwantedPersonsNum);
    // 5 + 2 changed + 1 added
    Assertions.assertThat(listener.ruleFiredCount("person detector")).isEqualTo(wantedPersonsNum * unwantedPersonsNum);
    Assertions.assertThat(listener.ruleFiredCount("closing actions")).isEqualTo(1);
    ksession.halt();
}
Also used : HashMap(java.util.HashMap) TrackingAgendaEventListener(org.jbpm.test.listener.TrackingAgendaEventListener) ExecutorService(java.util.concurrent.ExecutorService) ResourceType(org.kie.api.io.ResourceType) KieSession(org.kie.api.runtime.KieSession) Person(org.jbpm.test.domain.Person) Test(org.junit.Test)

Example 2 with TrackingAgendaEventListener

use of org.jbpm.test.listener.TrackingAgendaEventListener in project jbpm by kiegroup.

the class RuleTest method testNoOnEntryEvent.

@Test
@BZ("852095")
public void testNoOnEntryEvent() {
    Map<String, ResourceType> res = new HashMap<String, ResourceType>();
    res.put(ON_ENTRY_EVENT, ResourceType.BPMN2);
    res.put(ON_ENTRY_EVENT_DRL, ResourceType.DRL);
    KieSession ksession = createKSession(res);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    TrackingAgendaEventListener agendaEvents = new TrackingAgendaEventListener();
    TrackingProcessEventListener processEvents = new TrackingProcessEventListener();
    TrackingRuleRuntimeEventListener ruleEvents = new TrackingRuleRuntimeEventListener();
    ksession.addEventListener(agendaEvents);
    ksession.addEventListener(processEvents);
    ksession.addEventListener(ruleEvents);
    commands.add(getCommands().newStartProcess(ON_ENTRY_EVENT_ID));
    commands.add(getCommands().newFireAllRules());
    ksession.execute(getCommands().newBatchExecution(commands, null));
    Assertions.assertThat(processEvents.wasProcessStarted(ON_ENTRY_EVENT_ID)).isTrue();
    Assertions.assertThat(processEvents.wasNodeTriggered("Rule")).isTrue();
    Assertions.assertThat(ruleEvents.wasInserted("OnEntry")).isTrue();
    Assertions.assertThat(agendaEvents.isRuleFired("dummyRule")).isTrue();
    Assertions.assertThat(ruleEvents.wasInserted("OnExit")).isTrue();
    Assertions.assertThat(processEvents.wasNodeLeft("Rule")).isTrue();
    Assertions.assertThat(processEvents.wasProcessCompleted(ON_ENTRY_EVENT_ID)).isTrue();
}
Also used : HashMap(java.util.HashMap) Command(org.kie.api.command.Command) TrackingAgendaEventListener(org.jbpm.test.listener.TrackingAgendaEventListener) ArrayList(java.util.ArrayList) TrackingProcessEventListener(org.jbpm.test.listener.TrackingProcessEventListener) TrackingRuleRuntimeEventListener(org.jbpm.test.listener.TrackingRuleRuntimeEventListener) ResourceType(org.kie.api.io.ResourceType) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test) BZ(qa.tools.ikeeper.annotation.BZ)

Example 3 with TrackingAgendaEventListener

use of org.jbpm.test.listener.TrackingAgendaEventListener in project jbpm by kiegroup.

the class TransactionsTest method testRuleflowGroup.

@Test(timeout = 60000)
public void testRuleflowGroup() throws Exception {
    TrackingProcessEventListener process = new TrackingProcessEventListener(false);
    TrackingAgendaEventListener agenda = new TrackingAgendaEventListener();
    ksession.addEventListener(process);
    ksession.addEventListener(agenda);
    long processId = startProcess(ksession);
    UserTransaction ut = getUserTransaction();
    try {
        ut.begin();
        ksession.signalEvent("start", "rfg", processId);
        Assertions.assertThat(process.wasNodeLeft("rfg")).isTrue();
    } finally {
        ut.rollback();
    }
    Thread.sleep(600);
    process.clear();
    agenda.clear();
    ksession = restoreKSession(resources);
    ksession.addEventListener(process);
    ksession.addEventListener(agenda);
    ksession.fireAllRules();
    Assertions.assertThat(agenda.isRuleFired("dummyRule")).isFalse();
    agenda.clear();
    process.clear();
    String ruleFlowGroupNodeName = "rfg";
    ut = getUserTransaction();
    try {
        ut.begin();
        ksession.signalEvent("start", "rfg", processId);
        assertTrue("Node '" + ruleFlowGroupNodeName + "' was not left on time!", process.waitForNodeToBeLeft(ruleFlowGroupNodeName, 1000));
        ut.commit();
    } catch (Exception ex) {
        ut.rollback();
        throw ex;
    }
    Assertions.assertThat(process.wasNodeLeft(ruleFlowGroupNodeName)).isTrue();
    ksession.signalEvent("finish", null, processId);
    ksession.fireAllRules();
    assertTrue("Process did not complete on time!", process.waitForProcessToComplete(1000));
    Assertions.assertThat(agenda.isRuleFired("dummyRule")).isTrue();
    assertProcessInstanceCompleted(processId);
}
Also used : UserTransaction(javax.transaction.UserTransaction) TrackingAgendaEventListener(org.jbpm.test.listener.TrackingAgendaEventListener) TrackingProcessEventListener(org.jbpm.test.listener.TrackingProcessEventListener) Test(org.junit.Test)

Aggregations

TrackingAgendaEventListener (org.jbpm.test.listener.TrackingAgendaEventListener)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 TrackingProcessEventListener (org.jbpm.test.listener.TrackingProcessEventListener)2 ResourceType (org.kie.api.io.ResourceType)2 KieSession (org.kie.api.runtime.KieSession)2 ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1 UserTransaction (javax.transaction.UserTransaction)1 Person (org.jbpm.test.domain.Person)1 TrackingRuleRuntimeEventListener (org.jbpm.test.listener.TrackingRuleRuntimeEventListener)1 Command (org.kie.api.command.Command)1 BZ (qa.tools.ikeeper.annotation.BZ)1