Search in sources :

Example 1 with OrderListener

use of org.drools.testcoverage.common.listener.OrderListener in project drools by kiegroup.

the class DecisionTableTest method testLockOnActive.

@Test
public void testLockOnActive() {
    final Resource decisionTable = ResourceUtil.getDecisionTableResourceFromClasspath("agenda-group.csv", getClass(), DecisionTableInputType.CSV);
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, decisionTable);
    final KieSession ksession = kbase.newKieSession();
    final OrderListener listener = new OrderListener();
    ksession.addEventListener(listener);
    ksession.insert("lockOnActive");
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).isEqualTo(3);
    final String[] expected = { "a", "a2", "a3" };
    for (int i = 0; i < listener.size(); i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected[i]);
    }
}
Also used : KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) OrderListener(org.drools.testcoverage.common.listener.OrderListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 2 with OrderListener

use of org.drools.testcoverage.common.listener.OrderListener in project drools by kiegroup.

the class DeclarativeAgendaTest method testActivationBlock.

// test activation block together with agenda group
// BZ 999360
@Test(timeout = 10000L)
public void testActivationBlock() {
    final KieBase kbase = buildKieBase("declarative-agenda-block.drl");
    final KieSession ksession = kbase.newKieSession();
    OrderListener listener = new OrderListener();
    ksession.addEventListener(listener);
    // first run
    ksession.insert("startAgenda");
    ksession.insert("fireRules");
    final FactHandle fireBlockerRule = ksession.insert("fireBlockerRule");
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).isEqualTo(6);
    final String[] expected = { "startAgenda", "catering1", "sales1", "salesBlocker", "catering2", "salesBlocker" };
    for (int i = 0; i < listener.size(); i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected[i]);
    }
    // second run
    ksession.delete(fireBlockerRule);
    ksession.removeEventListener(listener);
    listener = new OrderListener();
    ksession.addEventListener(listener);
    ksession.fireAllRules();
    // BZ 1038076
    Assertions.assertThat(listener.size()).isEqualTo(1);
    final String[] expected2 = { "sales2" };
    for (int i = 0; i < listener.size(); i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected2[i]);
    }
    ksession.dispose();
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) OrderListener(org.drools.testcoverage.common.listener.OrderListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 3 with OrderListener

use of org.drools.testcoverage.common.listener.OrderListener in project drools by kiegroup.

the class DecisionTableTest method testAdvancedTable.

/**
 * test for advanced rule settings (no-loop, saliences, ...), file
 * sample_advanced_dt.xls
 *
 * covers also bugfix for Bug724257 (agenda group not added from dtable to
 * .drl)
 */
@Test
public void testAdvancedTable() {
    final Resource decisionTable = ResourceUtil.getDecisionTableResourceFromClasspath("advanced_dt.xls", getClass(), DecisionTableInputType.XLS);
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, decisionTable);
    KieSession session = kbase.newKieSession();
    final OrderListener listener = new OrderListener();
    session.addEventListener(listener);
    final Subject lili = new Subject("Lili");
    lili.setAge(100);
    final Sample sample = new Sample();
    session.insert(lili);
    session.insert(sample);
    session.fireAllRules();
    // just 4 rules should fire
    Assertions.assertThat(listener.size()).isEqualTo(4);
    // rules have to be fired in expected order
    final String[] expected = new String[] { "HelloWorld_11", "namedRule", "b1", "another rule" };
    for (int i = 0; i < 4; i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected[i]);
    }
    session.dispose();
}
Also used : KieBase(org.kie.api.KieBase) Sample(org.drools.testcoverage.common.model.Sample) Resource(org.kie.api.io.Resource) OrderListener(org.drools.testcoverage.common.listener.OrderListener) KieSession(org.kie.api.runtime.KieSession) Subject(org.drools.testcoverage.common.model.Subject) Test(org.junit.Test)

Example 4 with OrderListener

use of org.drools.testcoverage.common.listener.OrderListener in project drools by kiegroup.

the class DecisionTableTest method testAutoFocus.

/**
 * Agenda group rule with auto focus can fire a give focus to agenda group
 * without focus set on whole agenda group.
 */
@Test
public void testAutoFocus() {
    final Resource decisionTable = ResourceUtil.getDecisionTableResourceFromClasspath("agenda-group.csv", getClass(), DecisionTableInputType.CSV);
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, decisionTable);
    final KieSession ksession = kbase.newKieSession();
    final OrderListener listener = new OrderListener();
    ksession.addEventListener(listener);
    // first test - we try to fire rule in agenda group which has auto focus
    // disable, we won't succeed
    final FactHandle withoutAutoFocus = ksession.insert("withoutAutoFocus");
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).isEqualTo(0);
    // second test - we try to fire rule in agenda group with auto focus
    // enabled
    // it fires and it's defined consequence causes to fire second rule
    // which has no auto focus
    ksession.insert("autoFocus");
    ksession.delete(withoutAutoFocus);
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).isEqualTo(2);
    final String[] expected = { "b2", "b1" };
    for (int i = 0; i < listener.size(); i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected[i]);
    }
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) OrderListener(org.drools.testcoverage.common.listener.OrderListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 5 with OrderListener

use of org.drools.testcoverage.common.listener.OrderListener in project drools by kiegroup.

the class DecisionTableTest method testSequential.

/**
 * Test sequential turned on, it overrides all user defined saliences.
 */
@Test
public void testSequential() {
    final Resource decisionTable = ResourceUtil.getDecisionTableResourceFromClasspath("sequential.csv", getClass(), DecisionTableInputType.CSV);
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, decisionTable);
    final KieSession ksession = kbase.newKieSession();
    final OrderListener listener = new OrderListener();
    ksession.addEventListener(listener);
    ksession.insert("something");
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).as("Wrong number of rules fired").isEqualTo(3);
    final String[] expected = { "Rule1", "Rule2", "Rule3" };
    for (int i = 0; i < 3; i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected[i]);
    }
}
Also used : KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) OrderListener(org.drools.testcoverage.common.listener.OrderListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

OrderListener (org.drools.testcoverage.common.listener.OrderListener)9 Test (org.junit.Test)9 KieBase (org.kie.api.KieBase)9 KieSession (org.kie.api.runtime.KieSession)9 Resource (org.kie.api.io.Resource)4 FactHandle (org.kie.api.runtime.rule.FactHandle)4 Sample (org.drools.testcoverage.common.model.Sample)1 Subject (org.drools.testcoverage.common.model.Subject)1