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();
}
}
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();
}
}
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("");
}
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("");
}
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();
}
}
Aggregations