Search in sources :

Example 1 with TrackingAgendaEventListener

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

the class DecisionTableTest method testActivationGroup.

@Test
public void testActivationGroup() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, agendaGroupDecisionTable);
    final KieSession ksession = kbase.newKieSession();
    final TrackingAgendaEventListener listener = new TrackingAgendaEventListener();
    ksession.addEventListener(listener);
    // only one rule from activation group may fire
    ksession.insert("activationGroup");
    ksession.fireAllRules();
    Assertions.assertThat(listener.isRuleFired("c1")).isFalse();
    Assertions.assertThat(listener.isRuleFired("c2")).isTrue();
    Assertions.assertThat(listener.isRuleFired("c3")).isFalse();
    ksession.dispose();
}
Also used : TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 2 with TrackingAgendaEventListener

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

the class KieSessionTest method createKieSession.

@Before
public void createKieSession() {
    Resource[] resources = createResources();
    if (resources == null) {
        session = KieSessionUtil.getKieSessionFromKieBaseModel(TestConstants.PACKAGE_REGRESSION, kieBaseTestConfiguration, kieSessionTestConfiguration);
    } else {
        session = KieSessionUtil.getKieSessionFromKieBaseModel(TestConstants.PACKAGE_REGRESSION, kieBaseTestConfiguration, kieSessionTestConfiguration, resources);
    }
    firedRules = new TrackingAgendaEventListener();
    session.addEventListener(firedRules);
}
Also used : TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) Resource(org.kie.api.io.Resource) Before(org.junit.Before)

Example 3 with TrackingAgendaEventListener

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

the class FusionAfterBeforeTest method testAfterBeforeOperators.

@Test
public void testAfterBeforeOperators() {
    final Resource drlResource = KieServices.Factory.get().getResources().newClassPathResource("fusionAfterBeforeTest.drl", getClass());
    final KieBase kieBase = KieBaseUtil.getKieBaseFromKieModuleFromResources(TestConstants.PACKAGE_REGRESSION, kieBaseTestConfiguration, drlResource);
    final KieSessionConfiguration ksconf = RuleBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.PSEUDO);
    final KieSession ksession = kieBase.newKieSession(ksconf, null);
    final TrackingAgendaEventListener listener = new TrackingAgendaEventListener();
    ksession.addEventListener(listener);
    final EntryPoint stream = ksession.getEntryPoint("EventStream");
    SessionPseudoClock clock = ksession.getSessionClock();
    try {
        for (int i = 0; i < 3; i++) {
            final MessageEvent tc = new MessageEvent(MessageEvent.Type.received, new Message());
            stream.insert(tc);
            ksession.fireAllRules();
            clock.advanceTime(8, TimeUnit.SECONDS);
        }
        ksession.fireAllRules();
    } finally {
        ksession.dispose();
    }
    Assertions.assertThat(listener.isRuleFired("AfterMessageEvent")).as("Rule 'AfterMessageEvent' was no fired!").isTrue();
    Assertions.assertThat(listener.isRuleFired("BeforeMessageEvent")).as("Rule 'BeforeMessageEvent' was no fired!").isTrue();
    // each rules should be fired 2 times
    int firedCount = 2;
    int actuallyFired = listener.ruleFiredCount("AfterMessageEvent");
    Assertions.assertThat(firedCount).as("Rule 'AfterMessageEvent' should be fired 2 times, but was fired " + firedCount + " time(s)!").isEqualTo(actuallyFired);
    firedCount = listener.ruleFiredCount("BeforeMessageEvent");
    Assertions.assertThat(firedCount).as("Rule 'BeforeMessageEvent' should be fired 2 times, but was fired " + firedCount + " time(s)!").isEqualTo(actuallyFired);
}
Also used : Message(org.drools.testcoverage.common.model.Message) TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) MessageEvent(org.drools.testcoverage.common.model.MessageEvent) Resource(org.kie.api.io.Resource) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 4 with TrackingAgendaEventListener

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

the class ActivationGroupTest method testActivationGroupRecursivelyWithDefaultSalience.

@Test
public void testActivationGroupRecursivelyWithDefaultSalience() {
    TrackingAgendaEventListener listener = prepareKSession("defaultSalienceWithRecursion");
    Assertions.assertThat(listener.rulesCount()).isEqualTo(2);
}
Also used : TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) Test(org.junit.Test)

Example 5 with TrackingAgendaEventListener

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

the class UnwantedStringConversionTest method testStringToDecimalConversion.

@Test
public void testStringToDecimalConversion() {
    final Resource drlResource = KieServices.Factory.get().getResources().newClassPathResource("unwantedStringConversionTest.drl", getClass());
    final KieBase kieBase = KieBaseUtil.getKieBaseFromKieModuleFromResources(TestConstants.PACKAGE_REGRESSION, kieBaseTestConfiguration, drlResource);
    final KieSession ksession = kieBase.newKieSession();
    final TrackingAgendaEventListener listener = new TrackingAgendaEventListener();
    ksession.addEventListener(listener);
    final Message message = new Message();
    final SomeEntity someEntity = new SomeEntity("1.5");
    message.setSomeEntity(someEntity);
    message.setMessage("1.5");
    ksession.insert(message);
    ksession.fireAllRules();
    Assertions.assertThat(listener.isRuleFired("R1")).isFalse();
    Assertions.assertThat(listener.isRuleFired("R2")).isFalse();
    Assertions.assertThat(listener.rulesCount()).isEqualTo(0);
}
Also used : TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

TrackingAgendaEventListener (org.drools.testcoverage.common.listener.TrackingAgendaEventListener)13 Test (org.junit.Test)11 KieSession (org.kie.api.runtime.KieSession)8 KieBase (org.kie.api.KieBase)7 Resource (org.kie.api.io.Resource)4 Person (org.drools.testcoverage.common.model.Person)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 ArrayList (java.util.ArrayList)2 Before (org.junit.Before)2 FactType (org.kie.api.definition.type.FactType)2 Collection (java.util.Collection)1 Stream (java.util.stream.Stream)1 Assertions (org.assertj.core.api.Assertions)1 Address (org.drools.testcoverage.common.model.Address)1 Message (org.drools.testcoverage.common.model.Message)1 MessageEvent (org.drools.testcoverage.common.model.MessageEvent)1 Sample (org.drools.testcoverage.common.model.Sample)1 Subject (org.drools.testcoverage.common.model.Subject)1 KieBaseTestConfiguration (org.drools.testcoverage.common.util.KieBaseTestConfiguration)1 KieBaseUtil (org.drools.testcoverage.common.util.KieBaseUtil)1