Search in sources :

Example 76 with Cheese

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

the class ConsequenceTest method testConsequenceException.

@Test
public void testConsequenceException() {
    final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "rule \"Throw Consequence Exception\"\n" + "    when\n" + "        cheese : Cheese( )\n" + "    then\n" + "        throw new Exception( \"this should throw an exception\" );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("consequence-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final Cheese brie = new Cheese("brie", 12);
        ksession.insert(brie);
        try {
            ksession.fireAllRules();
            fail("Should throw an Exception from the Consequence");
        } catch (final org.kie.api.runtime.rule.ConsequenceException e) {
            assertEquals("Throw Consequence Exception", e.getMatch().getRule().getName());
            assertEquals("this should throw an exception", e.getCause().getMessage());
        }
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) Test(org.junit.Test)

Example 77 with Cheese

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

the class DRLTest method testDuplicateVariableBinding.

@Test
public void testDuplicateVariableBinding() {
    final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.Map results;\n" + "\n" + "rule \"Duplicate Variable testing\"\n" + " when\n" + "   // there should be no problem since each variable \n" + "   // is in a different logical branch\n" + "   Cheese( $type : type == \"stilton\", $price : price ) or\n" + "   Cheese( $type : type == \"brie\", $price : price )\n" + " then\n" + "   results.put( $type, new Integer( $price ) );\n" + "end\n" + "\n" + "rule \"Duplicate Variable testing 2\"\n" + " when\n" + "   // there should be no problem since each variable \n" + "   // is in a different logical branch\n" + "   $cheese : Cheese( type == \"stilton\", $price : price ) or\n" + "   $cheese : Cheese( type == \"brie\", $price : price )\n" + " then\n" + "   results.put( $cheese, new Integer( $price ) );\n" + "end\n" + "\n" + "rule \"Duplicate Variable testing 3\"\n" + " when\n" + "   // there should be no problem since each variable \n" + "   // is in a different logical branch\n" + "   Cheese( $type : type == \"stilton\", $price : price ) or\n" + "   ( Cheese( $type : type == \"brie\", $price : price ) and Person( name == \"bob\", likes == $type ) )\n" + " then\n" + "   results.put( \"test3\"+$type, new Integer( $price ) );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("drl-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final Map result = new HashMap();
        ksession.setGlobal("results", result);
        final Cheese stilton = new Cheese("stilton", 20);
        final Cheese brie = new Cheese("brie", 10);
        ksession.insert(stilton);
        ksession.insert(brie);
        ksession.fireAllRules();
        assertEquals(5, result.size());
        assertEquals(stilton.getPrice(), ((Integer) result.get(stilton.getType())).intValue());
        assertEquals(brie.getPrice(), ((Integer) result.get(brie.getType())).intValue());
        assertEquals(stilton.getPrice(), ((Integer) result.get(stilton)).intValue());
        assertEquals(brie.getPrice(), ((Integer) result.get(brie)).intValue());
        assertEquals(stilton.getPrice(), ((Integer) result.get("test3" + stilton.getType())).intValue());
        final Person bob = new Person("bob");
        bob.setLikes(brie.getType());
        ksession.insert(bob);
        ksession.fireAllRules();
        assertEquals(6, result.size());
        assertEquals(brie.getPrice(), ((Integer) result.get("test3" + brie.getType())).intValue());
    } finally {
        ksession.dispose();
    }
}
Also used : HashMap(java.util.HashMap) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) Person(org.drools.testcoverage.common.model.Person) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 78 with Cheese

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

the class DRLTest method testWithInvalidRule.

@Test
public void testWithInvalidRule() {
    final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "//error 1) missing a person import\n" + "\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule \"not rule test\"\n" + "    when\n" + "        // error 2) incorrect field\n" + "        $person : Person( $likes:likeypooh )\n" + "        not Cheese( type == $likes )\n" + "    then\n" + "        list.add( $person );\n" + "end";
    final KieBuilder kieBuilder = KieUtil.getKieBuilderFromDrls(kieBaseTestConfiguration, false, drl);
    Assertions.assertThat(kieBuilder.getResults().getMessages()).isNotEmpty();
    Assertions.assertThat(kieBuilder.getResults().getMessages()).extracting(Message::getText).doesNotContain("");
}
Also used : Cheese(org.drools.testcoverage.common.model.Cheese) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 79 with Cheese

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

the class DRLTest method testDeclarationNonExistingField.

@Test
public void testDeclarationNonExistingField() {
    final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "rule \"test declaration of non existing field\"\n" + "when\n" + "    Person( $likes : likes, $nef : nonExistingField )\n" + "    Cheese( type == $likes ) \n" + "then\n" + "end";
    final KieBuilder kieBuilder = KieUtil.getKieBuilderFromDrls(kieBaseTestConfiguration, false, drl);
    Assertions.assertThat(kieBuilder.getResults().getMessages()).isNotEmpty();
    Assertions.assertThat(kieBuilder.getResults().getMessages()).extracting(Message::getText).doesNotContain("");
}
Also used : Cheese(org.drools.testcoverage.common.model.Cheese) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 80 with Cheese

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

the class GlobalTest method testReturnValueAndGlobal.

@Test
public void testReturnValueAndGlobal() {
    final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "\n" + "global java.lang.String cheeseType;\n" + "global java.util.List   matchingList;\n" + "global java.util.List   nonMatchingList;\n" + "\n" + "\n" + "rule \"Match type\"\n" + "    when\n" + "        $cheese : Cheese( type == (cheeseType) )\n" + "    then\n" + "        matchingList.add( $cheese );\n" + "end\n" + "\n" + "rule \"Non matching type\"\n" + "    when\n" + "        $cheese : Cheese( type != (cheeseType) )\n" + "    then\n" + "        nonMatchingList.add( $cheese );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("global-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final List matchlist = new ArrayList();
        ksession.setGlobal("matchingList", matchlist);
        final List nonmatchlist = new ArrayList();
        ksession.setGlobal("nonMatchingList", nonmatchlist);
        ksession.setGlobal("cheeseType", "stilton");
        final Cheese stilton1 = new Cheese("stilton", 5);
        final Cheese stilton2 = new Cheese("stilton", 7);
        final Cheese brie = new Cheese("brie", 4);
        ksession.insert(stilton1);
        ksession.insert(stilton2);
        ksession.insert(brie);
        ksession.fireAllRules();
        assertEquals(2, matchlist.size());
        assertEquals(1, nonmatchlist.size());
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Cheese(org.drools.testcoverage.common.model.Cheese) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

Cheese (org.drools.testcoverage.common.model.Cheese)97 KieBase (org.kie.api.KieBase)92 KieSession (org.kie.api.runtime.KieSession)88 Test (org.junit.Test)85 ArrayList (java.util.ArrayList)65 Person (org.drools.testcoverage.common.model.Person)47 List (java.util.List)33 FactHandle (org.kie.api.runtime.rule.FactHandle)22 Cheesery (org.drools.testcoverage.common.model.Cheesery)11 Arrays.asList (java.util.Arrays.asList)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 BigDecimal (java.math.BigDecimal)3 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)3 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)3 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)2