Search in sources :

Example 81 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class KieBuilderTest method testJavaSourceFileAndDrlDeployWithClassFilter.

@Test
public void testJavaSourceFileAndDrlDeployWithClassFilter() {
    final String allowedJava = "package org.drools.compiler;\n" + "public class JavaSourceMessage { }\n";
    final String filteredJava = "package org.drools.compiler;\n" + "public class ClassCausingClassNotFoundException { non.existing.Type foo() { return null; } }\n";
    final String drl = "package org.drools.compiler;\n" + "import org.drools.compiler.JavaSourceMessage;" + "rule R1 when\n" + "   $m : JavaSourceMessage()\n" + "then\n" + "end\n";
    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"kbase1\">\n" + "    <ksession name=\"ksession1\" default=\"true\"/>\n" + "  </kbase>\n" + "</kmodule>";
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final Resource allowedJavaResource = ResourceFactory.newByteArrayResource(allowedJava.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/JavaSourceMessage.java");
    final Resource filteredJavaResource = ResourceFactory.newByteArrayResource(filteredJava.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/ClassCausingClassNotFoundException.java");
    final Resource drlResource = ResourceFactory.newByteArrayResource(drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    final Predicate<String> filter = fileName -> !fileName.endsWith("org/drools/compiler/ClassCausingClassNotFoundException.java");
    KieModule km = null;
    try {
        km = createAndDeployJar(ks, kmodule, filter, releaseId1, allowedJavaResource, filteredJavaResource, drlResource);
    } catch (final IllegalStateException ise) {
        if (ise.getMessage().contains("org/drools/compiler/ClassCausingClassNotFoundException.java")) {
            fail("Build failed because source file was not filtered out.");
        } else {
            throw ise;
        }
    }
    final KieContainer kieContainer = ks.newKieContainer(km.getReleaseId());
    try {
        final Class<?> messageClass = kieContainer.getClassLoader().loadClass("org.drools.compiler.JavaSourceMessage");
        assertNotNull(messageClass);
    } catch (final ClassNotFoundException e) {
        throw new IllegalStateException("Loading the java class failed.", e);
    }
}
Also used : InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KnowledgeBuilderFactory(org.kie.internal.builder.KnowledgeBuilderFactory) ResourceFactory(org.kie.internal.io.ResourceFactory) ResourceType(org.kie.api.io.ResourceType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) Assertions(org.assertj.core.api.Assertions) Assert.fail(org.junit.Assert.fail) KieBase(org.kie.api.KieBase) Channel(org.kie.api.runtime.Channel) KieSession(org.kie.api.runtime.KieSession) KieFileSystem(org.kie.api.builder.KieFileSystem) KieBuilder(org.kie.api.builder.KieBuilder) Predicate(java.util.function.Predicate) Assert.assertNotNull(org.junit.Assert.assertNotNull) CommonTestMethodBase(org.drools.compiler.CommonTestMethodBase) Results(org.kie.api.builder.Results) KieContainer(org.kie.api.runtime.KieContainer) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Resource(org.kie.api.io.Resource) List(java.util.List) KieModule(org.kie.api.builder.KieModule) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) KieModuleModel(org.kie.api.builder.model.KieModuleModel) Assert.assertEquals(org.junit.Assert.assertEquals) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 82 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class KieBuilderTest method testValidXsdTargetNamespace.

@Test
public void testValidXsdTargetNamespace() {
    final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"kbase1\">\n" + "    <ksession name=\"ksession1\" default=\"true\"/>\n" + "  </kbase>\n" + "</kmodule>";
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final Resource r1 = ResourceFactory.newByteArrayResource(drl1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, r1);
    ks.newKieContainer(km.getReleaseId());
}
Also used : Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 83 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class KieBuilderTest method testInvalidXsdTargetNamespace.

@Test
public void testInvalidXsdTargetNamespace() {
    final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://www.drools.org/xsd/doesNotExist\">\n" + "  <kbase name=\"kbase1\">\n" + "    <ksession name=\"ksession1\" default=\"true\"/>\n" + "  </kbase>\n" + "</kmodule>";
    final KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final Resource r1 = ResourceFactory.newByteArrayResource(drl1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    Assertions.assertThatThrownBy(() -> createAndDeployJar(ks, kmodule, releaseId1, r1)).isInstanceOf(IllegalStateException.class).hasMessageContaining("XSD validation failed");
}
Also used : Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) Test(org.junit.Test)

Example 84 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class KieBuilderTest method testOldXsdTargetNamespace.

@Test
public void testOldXsdTargetNamespace() {
    final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">\n" + "  <kbase name=\"kbase1\">\n" + "    <ksession name=\"ksession1\" default=\"true\"/>\n" + "  </kbase>\n" + "</kmodule>";
    final KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final Resource r1 = ResourceFactory.newByteArrayResource(drl1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, r1);
    ks.newKieContainer(km.getReleaseId());
}
Also used : Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 85 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class KieBuilderTest method testJavaSourceFileAndDrlDeploy.

@Test
public void testJavaSourceFileAndDrlDeploy() {
    final String java = "package org.drools.compiler;\n" + "public class JavaSourceMessage { }\n";
    final String drl = "package org.drools.compiler;\n" + "import org.drools.compiler.JavaSourceMessage;" + "rule R1 when\n" + "   $m : JavaSourceMessage()\n" + "then\n" + "end\n";
    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"kbase1\">\n" + "    <ksession name=\"ksession1\" default=\"true\"/>\n" + "  </kbase>\n" + "</kmodule>";
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final Resource javaResource = ResourceFactory.newByteArrayResource(java.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/JavaSourceMessage.java");
    final Resource drlResource = ResourceFactory.newByteArrayResource(drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, javaResource, drlResource);
    final KieContainer kieContainer = ks.newKieContainer(km.getReleaseId());
    try {
        final Class<?> messageClass = kieContainer.getClassLoader().loadClass("org.drools.compiler.JavaSourceMessage");
        assertNotNull(messageClass);
    } catch (final ClassNotFoundException e) {
        throw new IllegalStateException("Loading the java class failed.", e);
    }
}
Also used : Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

Resource (org.kie.api.io.Resource)162 Test (org.junit.Test)100 KieSession (org.kie.api.runtime.KieSession)50 KieServices (org.kie.api.KieServices)43 KieBase (org.kie.api.KieBase)42 ArrayList (java.util.ArrayList)30 KieContainer (org.kie.api.runtime.KieContainer)27 StringReader (java.io.StringReader)23 ReleaseId (org.kie.api.builder.ReleaseId)23 KieBuilder (org.kie.api.builder.KieBuilder)20 KieModule (org.kie.api.builder.KieModule)20 KieFileSystem (org.kie.api.builder.KieFileSystem)19 ClassPathResource (org.drools.core.io.impl.ClassPathResource)18 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)17 FactType (org.kie.api.definition.type.FactType)17 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)14 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)14 IOException (java.io.IOException)13 PMMLResource (org.drools.compiler.compiler.PMMLResource)11