Java Code Examples for org.kie.api.io.Resource#setTargetPath()

The following examples show how to use org.kie.api.io.Resource#setTargetPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SerializationSecurityPolicyTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerialization() throws IOException, ClassNotFoundException {
    final String rule =
            " rule R " +
            " when " +
            " then " +
            "     System.out.println(\"consequence!\"); " +
            " end";

    final KieServices kieServices = KieServices.get();
    final Resource drlResource = kieServices.getResources().newByteArrayResource(
            rule.getBytes(StandardCharsets.UTF_8.name()), StandardCharsets.UTF_8.name());
    drlResource.setResourceType(ResourceType.DRL);
    drlResource.setTargetPath("test.drl");
    final KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
    kieFileSystem.write(drlResource);
    final KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
    kieBuilder.buildAll();

    final KieBase kieBase = kieServices.newKieContainer(kieBuilder.getKieModule().getReleaseId()).getKieBase();

    final Collection<KiePackage> kpkgs = kieBase.getKiePackages();
    final Collection<KiePackage> newCollection = new ArrayList<>();
    for (KiePackage kpkg : kpkgs) {
        kpkg = SerializationHelper.serializeObject(kpkg);
        newCollection.add(kpkg);
    }
    ((InternalKnowledgeBase) kieBase).addPackages(newCollection);

    final KieSession kieSession = kieBase.newKieSession();
    assertThat(kieSession.fireAllRules()).isEqualTo(1);
}
 
Example 2
Source File: KieContainerTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDefaultKieSessionModelEmptyKmodule() {
    KieServices kieServices = KieServices.Factory.get();
    String drl = "package org.drools.test\n" +
            "rule R1 when\n" +
            "   $m : Object()\n" +
            "then\n" +
            "end\n";
    Resource resource = kieServices.getResources().newReaderResource( new StringReader( drl), "UTF-8" );
    resource.setTargetPath("org/drools/test/rules.drl");
    String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" +
            "</kmodule>";

    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-testGetDefaultKieSessionModelEmptyKmodule", "1.0.0");
    createAndDeployJar(kieServices, kmodule, releaseId, resource);

    KieContainer kieContainer = kieServices.newKieContainer(releaseId);

    KieSessionModel sessionModel = kieContainer.getKieSessionModel(null);
    assertNotNull(sessionModel);
}
 
Example 3
Source File: KieContainerTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testMemoryFileSystemFolderUniqueness() {
    KieServices kieServices = KieServices.Factory.get();
    String drl = "package org.drools.test\n" +
                 "rule R1 when\n" +
                 "   $m : Object()\n" +
                 "then\n" +
                 "end\n";
    Resource resource = kieServices.getResources().newReaderResource( new StringReader( drl), "UTF-8" );
    resource.setTargetPath("org/drools/test/rules.drl");
    String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                     "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" +
                     "  <kbase name=\"testKbase\" packages=\"org.drools.test\">\n" +
                     "    <ksession name=\"testKsession\"/>\n" +
                     "  </kbase>\n" +
                     "</kmodule>";

    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-delete", "1.0.0");
    createAndDeployJar(kieServices, kmodule, releaseId, resource);

    KieContainer kieContainer = kieServices.newKieContainer(releaseId);

    KieModule kieModule = ((InternalKieContainer) kieContainer).getMainKieModule();
    MemoryFileSystem memoryFileSystem = ((MemoryKieModule) kieModule).getMemoryFileSystem();
    Folder rootFolder = memoryFileSystem.getFolder("");
    Object[] members = rootFolder.getMembers().toArray();
    assertEquals(2, members.length);
    Folder firstFolder = (Folder) members[0];
    Folder secondFolder = (Folder) members[1];
    assertEquals(firstFolder.getParent(), secondFolder.getParent());
}
 
Example 4
Source File: KieContainerTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetDefaultKieSessionModel() {
    KieServices kieServices = KieServices.Factory.get();
    String drl = "package org.drools.test\n" +
            "rule R1 when\n" +
            "   $m : Object()\n" +
            "then\n" +
            "end\n";
    Resource resource = kieServices.getResources().newReaderResource( new StringReader( drl), "UTF-8" );
    resource.setTargetPath("org/drools/test/rules.drl");
    String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" +
            "  <kbase name=\"testKbase\" packages=\"org.drools.test\">\n" +
            "    <ksession name=\"testKsession\" default=\"true\"/>\n" +
            "  </kbase>\n" +
            "</kmodule>";

    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-testGetDefaultKieSessionModel", "1.0.0");
    createAndDeployJar(kieServices, kmodule, releaseId, resource);

    KieContainer kieContainer = kieServices.newKieContainer(releaseId);

    KieSessionModel sessionModel = kieContainer.getKieSessionModel(null);
    assertNotNull(sessionModel);
    assertEquals("testKsession", sessionModel.getName());
}
 
Example 5
Source File: StartEventTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
@Timeout(10)
public void testTimerStartDateISO() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("StartProcess", 1);
    byte[] content = IoUtils.readBytesFromInputStream(this.getClass().getResourceAsStream("/BPMN2-TimerStartDate.bpmn2"));
    String processContent = new String(content, "UTF-8");

    OffsetDateTime plusTwoSeconds = OffsetDateTime.now().plusSeconds(2);

    processContent = processContent.replaceFirst("#\\{date\\}", plusTwoSeconds.toString());
    Resource resource = ResourceFactory.newReaderResource(new StringReader(processContent));
    resource.setSourcePath("/BPMN2-TimerStartDate.bpmn2");
    resource.setTargetPath("/BPMN2-TimerStartDate.bpmn2");
    KieBase kbase = createKnowledgeBaseFromResources(resource);

    ksession = createKnowledgeSession(kbase);
    ksession.addEventListener(countDownListener);
    final List<String> list = new ArrayList<>();
    ksession.addEventListener(new DefaultProcessEventListener() {
        public void beforeProcessStarted(ProcessStartedEvent event) {
            list.add(event.getProcessInstance().getId());
        }
    });
    assertThat(list.size()).isEqualTo(0);
    countDownListener.waitTillCompleted();
    assertThat(list.size()).isEqualTo(1);

}
 
Example 6
Source File: StandaloneBPMNProcessTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testXXEProcessVulnerability() throws Exception {
	Resource processResource = ResourceFactory.newClassPathResource("xxe-protection/BPMN2-XXE-Process.bpmn2");
	
	File dtdFile = new File("src/test/resources/xxe-protection/external.dtd");
    assertThat(dtdFile).exists();
	
	String dtdContent = IoUtils.readFileAsString(dtdFile);
	dtdContent = dtdContent.replaceAll("@@PATH@@", dtdFile.getParentFile().getAbsolutePath());
	
	IoUtils.write(dtdFile, dtdContent.getBytes("UTF-8"));
	
	byte[] data = IoUtils.readBytesFromInputStream(processResource.getInputStream());
	String processAsString = new String(data, "UTF-8");
	// replace place holders with actual paths
	File testFiles = new File("src/test/resources/xxe-protection");

    assertThat(testFiles).exists();
	
	String path = testFiles.getAbsolutePath();
	processAsString = processAsString.replaceAll("@@PATH@@", path);
	
	Resource resource = ResourceFactory.newReaderResource(new StringReader(processAsString));
	resource.setSourcePath(processResource.getSourcePath());
	resource.setTargetPath(processResource.getTargetPath());
	
    KieBase kbase = createKnowledgeBaseFromResources(resource);
    KieSession ksession = createKnowledgeSession(kbase);
    ProcessInstance processInstance = ksession.startProcess("async-examples.bp1");
    
    String var1 = getProcessVarValue(processInstance, "testScript1");
    String var2 = getProcessVarValue(processInstance, "testScript2");

    assertThat(var1).isNull();
    assertThat(var2).isNull();
    assertThat(processInstance.getState()).isEqualTo(ProcessInstance.STATE_COMPLETED);
}
 
Example 7
Source File: KieContainerTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testClassLoaderGetResources() throws IOException, URISyntaxException {
    KieServices kieServices = KieServices.Factory.get();
    String drl1 = "package org.drools.testdrl;\n" +
                 "rule R1 when\n" +
                 "   $m : Object()\n" +
                 "then\n" +
                 "end\n";
    Resource resource1 = kieServices.getResources().newReaderResource(new StringReader(drl1), "UTF-8");
    resource1.setTargetPath("org/drools/testdrl/rules1.drl");

    String drl2 = "package org.drools.testdrl;\n" +
                 "rule R2 when\n" +
                 "   $m : Object()\n" +
                 "then\n" +
                 "end\n";
    Resource resource2 = kieServices.getResources().newReaderResource(new StringReader(drl2), "UTF-8");
    resource2.setTargetPath("org/drools/testdrl/rules2.drl");

    String java3 = "package org.drools.testjava;\n" +
                 "public class Message {}";
    Resource resource3 = kieServices.getResources().newReaderResource(new StringReader(java3), "UTF-8");
    resource3.setTargetPath("org/drools/testjava/Message.java");
    resource3.setResourceType(ResourceType.JAVA);

    String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                     "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" +
                     "  <kbase name=\"testKbase\" packages=\"org.drools.testdrl\">\n" +
                     "    <ksession name=\"testKsession\"/>\n" +
                     "  </kbase>\n" +
                     "</kmodule>";

    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-delete", "1.0.0");
    createAndDeployJar(kieServices, kmodule, releaseId, resource1, resource2, resource3);

    KieContainer kieContainer = kieServices.newKieContainer(releaseId);

    ClassLoader classLoader = kieContainer.getClassLoader();
    assertEnumerationSize(1, classLoader.getResources("org/drools/testjava")); // no trailing "/"

    assertEnumerationSize(1, classLoader.getResources("org/drools/testdrl/")); // trailing "/" to test both variants
    // make sure the package resource correctly lists all its child resources (files in this case)
    URL url = classLoader.getResources("org/drools/testdrl").nextElement();
    
    List<String> lines = read(url.openStream());
    assertThat(lines).contains("rules1.drl", "rules1.drl.properties", "rules2.drl", "rules2.drl.properties");

    assertUrlEnumerationContainsMatch("^mfs\\:/$", classLoader.getResources(""));
}