Search in sources :

Example 1 with Message

use of org.drools.testcoverage.common.model.Message 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 2 with Message

use of org.drools.testcoverage.common.model.Message in project drools by kiegroup.

the class GlobalOnLHSTest method testNPEOnMutableGlobal.

@Test
public void testNPEOnMutableGlobal() throws Exception {
    KieSession ksession = session.getStateful();
    List<String> context = new ArrayList<String>();
    ksession.setGlobal("context", context);
    ksession.setGlobal("LOGGER", LOGGER);
    FactHandle b = ksession.insert(new Message("b"));
    ksession.delete(b);
    int fired = ksession.fireAllRules(1);
    Assertions.assertThat(fired).isEqualTo(0);
    ksession.dispose();
}
Also used : Message(org.drools.testcoverage.common.model.Message) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) KieSessionTest(org.drools.testcoverage.common.KieSessionTest) Test(org.junit.Test)

Example 3 with Message

use of org.drools.testcoverage.common.model.Message in project drools by kiegroup.

the class InaccurateComparisonTest method testStringCoercionComparison.

@Test
public void testStringCoercionComparison() {
    final String rule = "package " + TestConstants.PACKAGE_REGRESSION + "\n" + " import " + TestConstants.PACKAGE_TESTCOVERAGE_MODEL + ".Message;\n" + " rule \"string coercion\" \n" + " when\n" + "     m : Message( message < \"90201304122000000000000017\" )\n" + " then \n" + " end";
    final KieBase kieBase = KieBaseUtil.getKieBaseFromKieModuleFromDrl(TestConstants.PACKAGE_REGRESSION, kieBaseTestConfiguration, rule);
    KieSession ksession = kieBase.newKieSession();
    ksession.insert(new Message("90201304122000000000000015"));
    Assertions.assertThat(ksession.fireAllRules()).isEqualTo(1);
}
Also used : Message(org.drools.testcoverage.common.model.Message) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 4 with Message

use of org.drools.testcoverage.common.model.Message in project drools by kiegroup.

the class OOPathCepTest method populateAndVerifyLengthWindowCase.

private void populateAndVerifyLengthWindowCase(final KieBase kieBase) {
    this.initKieSession(kieBase);
    final MessageEvent pingEvent = new MessageEvent(MessageEvent.Type.sent, new Message("Ping"));
    this.kieSession.insert(pingEvent);
    final MessageEvent ping2Event = new MessageEvent(MessageEvent.Type.received, new Message("Ping"));
    this.kieSession.insert(ping2Event);
    final MessageEvent ping3Event = new MessageEvent(MessageEvent.Type.received, new Message("Ping"));
    this.kieSession.insert(ping3Event);
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.events).as("The rule should have fired for 2 events").contains(ping2Event, ping3Event);
    this.events.clear();
    final MessageEvent pongEvent = new MessageEvent(MessageEvent.Type.sent, new Message("Pong"));
    this.kieSession.insert(pongEvent);
    final MessageEvent ping4Event = new MessageEvent(MessageEvent.Type.received, new Message("Ping"));
    this.kieSession.insert(ping4Event);
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.events).as("The rule should have fired for ping event only").contains(ping4Event);
}
Also used : Message(org.drools.testcoverage.common.model.Message) MessageEvent(org.drools.testcoverage.common.model.MessageEvent)

Example 5 with Message

use of org.drools.testcoverage.common.model.Message in project drools by kiegroup.

the class OOPathCepTest method testTemporalOperatorOverlapsWithOOPath.

@Test
public void testTemporalOperatorOverlapsWithOOPath() {
    final String drl = "import org.drools.testcoverage.common.model.Message;\n" + "import org.drools.testcoverage.common.model.MessageEvent;\n" + "global java.util.List events\n" + "global java.util.List messages\n" + "\n" + "declare org.drools.testcoverage.common.model.MessageEvent\n" + "  @role( event )\n" + "  @duration( duration )\n" + "end\n" + "rule R when\n" + "  ev1: MessageEvent( /msg[ message == 'Ping' ] )\n" + "  ev2: MessageEvent( $message: /msg[ message == 'Pong' ], this overlaps[ 1s ] ev1 )\n" + "then\n" + "  messages.add( $message );\n" + "end\n";
    final KieBase kieBase = KieBaseUtil.getKieBaseFromKieModuleFromDrl(MODULE_GROUP_ID, kieBaseTestConfiguration, drl);
    final SessionPseudoClock clock = this.initKieSessionWithPseudoClock(kieBase);
    this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Pong"), 1), clock, 2);
    this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Ping"), 1), clock, 1);
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.messages).as("The first sequence of events should NOT make the rule fire").isEmpty();
    final Message pongMessage = this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Pong"), DEFAULT_DURATION_IN_SECS), clock, 1);
    this.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Ping"), DEFAULT_DURATION_IN_SECS));
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.messages).as("The last event should make the rule fire").containsExactlyInAnyOrder(pongMessage);
}
Also used : Message(org.drools.testcoverage.common.model.Message) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) MessageEvent(org.drools.testcoverage.common.model.MessageEvent) Test(org.junit.Test)

Aggregations

Message (org.drools.testcoverage.common.model.Message)39 Test (org.junit.Test)32 KieBase (org.kie.api.KieBase)21 KieSession (org.kie.api.runtime.KieSession)20 MessageEvent (org.drools.testcoverage.common.model.MessageEvent)18 KieServices (org.kie.api.KieServices)14 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)14 KieContainer (org.kie.api.runtime.KieContainer)13 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)13 ReleaseId (org.kie.api.builder.ReleaseId)11 ArrayList (java.util.ArrayList)4 KieFileSystem (org.kie.api.builder.KieFileSystem)4 KieBuilder (org.kie.api.builder.KieBuilder)3 Resource (org.kie.api.io.Resource)3 IncrementalResults (org.kie.internal.builder.IncrementalResults)3 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)3 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Cheese (org.drools.testcoverage.common.model.Cheese)2 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)2