Search in sources :

Example 1 with ConsequenceException

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().
    }
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) ConsequenceException(org.kie.api.runtime.rule.ConsequenceException) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 2 with ConsequenceException

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().
    }
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) ConsequenceException(org.kie.api.runtime.rule.ConsequenceException) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 3 with ConsequenceException

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());
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) ConsequenceException(org.kie.api.runtime.rule.ConsequenceException) Test(org.junit.Test)

Example 4 with ConsequenceException

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().
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ConsequenceException(org.kie.api.runtime.rule.ConsequenceException) Test(org.junit.Test)

Example 5 with ConsequenceException

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().
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ConsequenceException(org.kie.api.runtime.rule.ConsequenceException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 KieSession (org.kie.api.runtime.KieSession)5 ConsequenceException (org.kie.api.runtime.rule.ConsequenceException)5 KieBase (org.kie.api.KieBase)3 KieServices (org.kie.api.KieServices)2 KieFileSystem (org.kie.api.builder.KieFileSystem)2 ReleaseId (org.kie.api.builder.ReleaseId)2 KieContainer (org.kie.api.runtime.KieContainer)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)1