use of org.kie.api.runtime.rule.ConsequenceException in project drools by kiegroup.
the class SecurityPolicyTest method testUntrustedMvelConsequence.
@Test
public void testUntrustedMvelConsequence() throws Exception {
String drl = "package org.foo.bar\n" + "rule R1 dialect \"mvel\" when\n" + "then\n" + " System.exit(0);" + "end\n";
try {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes()).setSourcePath("org/foo/bar/r1.drl"));
ks.newKieBuilder(kfs).buildAll();
ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
KieContainer kc = ks.newKieContainer(releaseId);
KieSession ksession = kc.newKieSession();
ksession.fireAllRules();
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (ShouldHavePrevented e) {
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (ConsequenceException e) {
// test succeeded. the policy in place prevented the rule from executing the System.exit().
}
}
use of org.kie.api.runtime.rule.ConsequenceException in project drools by kiegroup.
the class SecurityPolicyTest method testUntrustedJavaConsequence.
@Test
public void testUntrustedJavaConsequence() throws Exception {
String drl = "package org.foo.bar\n" + "rule R1 when\n" + "then\n" + " System.exit(0);" + "end\n";
try {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes()).setSourcePath("org/foo/bar/r1.drl"));
ks.newKieBuilder(kfs).buildAll();
ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
KieContainer kc = ks.newKieContainer(releaseId);
KieSession ksession = kc.newKieSession();
ksession.fireAllRules();
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (ShouldHavePrevented e) {
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (ConsequenceException e) {
// test succeeded. the policy in place prevented the rule from executing the System.exit().
}
}
use of org.kie.api.runtime.rule.ConsequenceException in project drools by kiegroup.
the class TruthMaintenanceTest method testUpdate.
@Test(timeout = 10000)
public void testUpdate() {
String droolsSource = "package org.drools.tms.test; \n" + " declare Feat1\n" + " context : String @key\n" + " value : double @key\n" + " missing : boolean = false\n" + " \n" + " end\n" + " \n" + " \n" + "rule \"InitAsMissing_Test_MLP_Feat1\"\n" + "when\n" + " not Feat1( context == null )\n" + "then\n" + " insertLogical( new Feat1( \"Test_MLP\", 0.0, true ) );\n" + "end\n" + " \n" + "rule \"miningFieldMissing_Test_MLP_Feat1\"\n" + "when\n" + " $x : Feat1( $m : missing == true, context == \"Test_MLP\" )\n" + "then\n" + " modify ( $x ) {\n" + " setValue( 3.95 ),\n" + " setMissing( false );\n" + " }\n" + "end\n" + " \n" + "rule \"OverrideInput_Feat1\"\n" + "when\n" + " $old: Feat1( value != 4.33 )\n" + "then\n" + " retract( $old );\n" + "end\n" + " \n" + " \n" + "rule \"Input_Feat1\"\n" + "when\n" + " not Feat1( context == null )\n" + "then\n" + " insert( new Feat1( null, 4.33 ) );\n" + "end";
KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieConf.setOption(EqualityBehaviorOption.IDENTITY);
KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
KieSession session = kbase.newKieSession();
List list = new ArrayList();
try {
session.fireAllRules();
fail("Currently we cannot handle updates for a belief set that is mixed stated and justified");
} catch (ConsequenceException e) {
assertEquals(IllegalStateException.class, e.getCause().getClass());
}
}
use of org.kie.api.runtime.rule.ConsequenceException in project drools by kiegroup.
the class SecurityPolicyTest method testUntrustedJavaConsequence.
@Test
public void testUntrustedJavaConsequence() {
String drl = "package org.foo.bar\n" + "rule R1 when\n" + "then\n" + " System.exit(0);" + "end\n";
try {
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession();
ksession.fireAllRules();
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (ShouldHavePrevented e) {
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (ConsequenceException e) {
// test succeeded. the policy in place prevented the rule from executing the System.exit().
}
}
use of org.kie.api.runtime.rule.ConsequenceException in project drools by kiegroup.
the class SecurityPolicyTest method testUntrustedMvelConsequence.
@Test
public void testUntrustedMvelConsequence() {
String drl = "package org.foo.bar\n" + "rule R1 dialect \"mvel\" when\n" + "then\n" + " System.exit(0);" + "end\n";
try {
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession();
ksession.fireAllRules();
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (ShouldHavePrevented e) {
Assert.fail("The security policy for the rule should have prevented this from executing...");
} catch (ConsequenceException e) {
// test succeeded. the policy in place prevented the rule from executing the System.exit().
}
}
Aggregations