Search in sources :

Example 16 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.getKieBaseAndBuildInstallModuleFromDrl(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 17 with Message

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

the class RuleTemplateTest method testGuidedRuleTemplate.

@Test
public void testGuidedRuleTemplate() throws Exception {
    final String resourceName = "cheese.template";
    final KieResources kieResources = KieServices.get().getResources();
    final Resource resource = kieResources.newClassPathResource(resourceName, RuleTemplateTest.class);
    resource.setResourceType(ResourceType.TEMPLATE);
    final KieBase kBase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resource);
    final KieSession kSession = kBase.newKieSession();
    final Cheese cheese = new Cheese();
    cheese.setPrice(90);
    final Customer petr = new Customer(0, "Peter");
    final Customer john = new Customer(1, "John");
    kSession.insert(cheese);
    kSession.insert(petr);
    kSession.insert(john);
    Assertions.assertThat(kSession.fireAllRules()).as("One rule should be fired").isEqualTo(1);
    final Collection messages = kSession.getObjects(object -> object instanceof Message);
    Assertions.assertThat(messages).hasSize(1);
    Assertions.assertThat(messages).hasOnlyOneElementSatisfying(message -> ((Message) message).getMessage().compareTo("Peter satisfied"));
}
Also used : Message(org.drools.testcoverage.common.model.Message) Customer(org.drools.testcoverage.common.model.Customer) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) KieResources(org.kie.api.io.KieResources) Test(org.junit.Test)

Example 18 with Message

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

the class OOPathCepTest method testTemporalOperatorStartsWithOOPath.

@Test
public void testTemporalOperatorStartsWithOOPath() {
    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 starts[ 1s ] ev1 )\n" + "then\n" + "  messages.add( $message );\n" + "end\n";
    final KieBase kieBase = KieBaseUtil.getKieBaseAndBuildInstallModuleFromDrl(MODULE_GROUP_ID, kieBaseTestConfiguration, drl);
    this.initKieSessionWithPseudoClock(kieBase);
    this.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Pong"), DEFAULT_DURATION_IN_SECS));
    this.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Ping"), DEFAULT_DURATION_IN_SECS));
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.messages).as("The first sequence of events should NOT make the rule fire").isEmpty();
    final Message pongMessage = this.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Pong"), DEFAULT_DURATION_IN_SECS - 1000));
    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) KieBase(org.kie.api.KieBase) MessageEvent(org.drools.testcoverage.common.model.MessageEvent) Test(org.junit.Test)

Example 19 with Message

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

the class OOPathCepTest method testTemporalOperatorIncludesWithOOPath.

@Test
public void testTemporalOperatorIncludesWithOOPath() {
    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 includes ev1 )\n" + "then\n" + "  messages.add( $message );\n" + "end\n";
    final KieBase kieBase = KieBaseUtil.getKieBaseAndBuildInstallModuleFromDrl(MODULE_GROUP_ID, kieBaseTestConfiguration, drl);
    final SessionPseudoClock clock = this.initKieSessionWithPseudoClock(kieBase);
    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("Pong"), DEFAULT_DURATION_IN_SECS - 1500));
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.messages).as("The first sequence of events should NOT make the rule fire").isEmpty();
    this.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Ping"), DEFAULT_DURATION_IN_SECS - 1500));
    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)

Example 20 with Message

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

the class OOPathCepTest method testTemporalOperatorDuringWithOOPath.

@Test
public void testTemporalOperatorDuringWithOOPath() {
    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 during ev1 )\n" + "then\n" + "  messages.add( $message );\n" + "end\n";
    final KieBase kieBase = KieBaseUtil.getKieBaseAndBuildInstallModuleFromDrl(MODULE_GROUP_ID, kieBaseTestConfiguration, drl);
    final SessionPseudoClock clock = this.initKieSessionWithPseudoClock(kieBase);
    this.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Ping"), DEFAULT_DURATION_IN_SECS));
    this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Pong"), DEFAULT_DURATION_IN_SECS), 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.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Pong"), DEFAULT_DURATION_IN_SECS - 1500));
    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)22 MessageEvent (org.drools.testcoverage.common.model.MessageEvent)18 Test (org.junit.Test)18 KieBase (org.kie.api.KieBase)17 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)14 KieSession (org.kie.api.runtime.KieSession)4 Resource (org.kie.api.io.Resource)2 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 KieSessionTest (org.drools.testcoverage.common.KieSessionTest)1 TrackingAgendaEventListener (org.drools.testcoverage.common.listener.TrackingAgendaEventListener)1 Cheese (org.drools.testcoverage.common.model.Cheese)1 Customer (org.drools.testcoverage.common.model.Customer)1 KieResources (org.kie.api.io.KieResources)1 EntryPoint (org.kie.api.runtime.rule.EntryPoint)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1