Search in sources :

Example 11 with KieContainer

use of org.kie.api.runtime.KieContainer in project drools by kiegroup.

the class KieModuleMavenTest method testKieModuleFromMavenWithDependencies.

@Test
public void testKieModuleFromMavenWithDependencies() throws Exception {
    final KieServices ks = new KieServicesImpl() {

        @Override
        public KieRepository getRepository() {
            // override repository to not store the artifact on deploy to trigger load from maven repo
            return new KieRepositoryImpl();
        }
    };
    ReleaseId dependency = ks.newReleaseId("org.drools", "drools-core", "5.5.0.Final");
    ReleaseId releaseId = ks.newReleaseId("org.kie", "maven-test", "1.0-SNAPSHOT");
    InternalKieModule kJar1 = createKieJar(ks, releaseId, true, "rule1", "rule2");
    String pomText = getPom(releaseId, dependency);
    File pomFile = new File(System.getProperty("java.io.tmpdir"), MavenRepository.toFileName(releaseId, null) + ".pom");
    try {
        FileOutputStream fos = new FileOutputStream(pomFile);
        fos.write(pomText.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    KieMavenRepository.getKieMavenRepository().installArtifact(releaseId, kJar1, pomFile);
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieBaseModel kbaseModel = ((KieContainerImpl) kieContainer).getKieProject().getDefaultKieBaseModel();
    assertNotNull("Default kbase was not found", kbaseModel);
    String kbaseName = kbaseModel.getName();
    assertEquals("KBase1", kbaseName);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepositoryImpl(org.drools.compiler.kie.builder.impl.KieRepositoryImpl) FileOutputStream(java.io.FileOutputStream) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) KieServicesImpl(org.drools.compiler.kie.builder.impl.KieServicesImpl) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 12 with KieContainer

use of org.kie.api.runtime.KieContainer in project drools by kiegroup.

the class KieRepositoryScannerTest method testKScannerWithRange_withListener.

@Test
public void testKScannerWithRange_withListener() throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieMavenRepository repository = getKieMavenRepository();
    String artifactId = "scanner-range-test-withlistener";
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", artifactId, "1.0.1");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", artifactId, "1.0.2");
    // Because KieScanner also check the local repo, ensure the artifact are not deployed on the local maven repo
    repository.removeLocalArtifact(releaseId1);
    repository.removeLocalArtifact(releaseId2);
    ReleaseId releaseRange = ks.newReleaseId("org.kie", artifactId, "[1.0.0,)");
    InternalKieModule kJar1 = createKieJar(ks, releaseId1, "rule1", "rule2");
    KieContainer kieContainer = ks.newKieContainer(releaseRange);
    List<KieScannerEvent> events = new ArrayList<>();
    InternalKieScanner scanner = (KieRepositoryScannerImpl) ks.newKieScanner(kieContainer);
    KieScannerEventListener listener = new KieScannerEventListener() {

        public void onKieScannerStatusChangeEvent(KieScannerStatusChangeEvent statusChange) {
            LOG.info("KieScanner event: {}", statusChange);
            events.add(statusChange);
        }

        public void onKieScannerUpdateResultsEvent(KieScannerUpdateResultsEvent updtaeResults) {
            LOG.info("KieScanner event: {}", updtaeResults);
            events.add(updtaeResults);
        }
    };
    scanner.addListener(listener);
    scanner.scanNow();
    // Because 1.0.2 does not exist, will perform a scan but will NOT update.
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.SCANNING)));
    assertThat(events, CoreMatchers.not(CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.UPDATING))));
    events.clear();
    repository.installArtifact(releaseId1, kJar1, createKPom(fileManager, releaseId1));
    KieSession ksession = kieContainer.newKieSession("KSession1");
    checkKSession(ksession, "rule1", "rule2");
    // create a new kjar
    InternalKieModule kJar2 = createKieJar(ks, releaseId2, "rule2", "rule3");
    // deploy it on maven
    repository.installArtifact(releaseId2, kJar2, createKPom(fileManager, releaseId2));
    // since I am not calling start() on the scanner it means it won't have automatic scheduled scanning
    assertEquals(releaseId1, scanner.getCurrentReleaseId());
    assertEquals(InternalKieScanner.Status.STOPPED, scanner.getStatus());
    // scan the maven repo to get the new kjar version and deploy it on the kcontainer
    scanner.scanNow();
    assertEquals(releaseId2, scanner.getCurrentReleaseId());
    assertEquals(InternalKieScanner.Status.STOPPED, scanner.getStatus());
    // create a ksesion and check it works as expected
    KieSession ksession2 = kieContainer.newKieSession("KSession1");
    checkKSession(ksession2, "rule2", "rule3");
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.SCANNING)));
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.UPDATING)));
    assertTrue(events.get(2) instanceof KieScannerUpdateResultsEventImpl);
    assertFalse(((KieScannerUpdateResultsEventImpl) events.get(2)).getResults().hasMessages(Message.Level.ERROR));
    events.clear();
    ks.getRepository().removeKieModule(releaseId1);
    ks.getRepository().removeKieModule(releaseId2);
    scanner.removeListener(listener);
    assertEquals(0, scanner.getListeners().size());
    repository.removeLocalArtifact(releaseId1);
    repository.removeLocalArtifact(releaseId2);
}
Also used : KieScannerEventListener(org.kie.api.event.kiescanner.KieScannerEventListener) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieScannerUpdateResultsEventImpl(org.kie.scanner.event.KieScannerUpdateResultsEventImpl) InternalKieScanner(org.drools.compiler.kie.builder.impl.InternalKieScanner) KieScannerStatusChangeEventImpl(org.kie.scanner.event.KieScannerStatusChangeEventImpl) KieMavenRepository.getKieMavenRepository(org.kie.scanner.KieMavenRepository.getKieMavenRepository) KieScannerEvent(org.kie.api.event.kiescanner.KieScannerEvent) KieScannerStatusChangeEvent(org.kie.api.event.kiescanner.KieScannerStatusChangeEvent) KieSession(org.kie.api.runtime.KieSession) KieScannerUpdateResultsEvent(org.kie.api.event.kiescanner.KieScannerUpdateResultsEvent) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 13 with KieContainer

use of org.kie.api.runtime.KieContainer in project drools by kiegroup.

the class KieRepositoryScannerTest method testKScannerWithRange_withListener_errors.

@Test
public void testKScannerWithRange_withListener_errors() throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieMavenRepository repository = getKieMavenRepository();
    String artifactId = "scanner-range-test-withlistener-errors";
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", artifactId, "1.0.1");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", artifactId, "1.0.2");
    // Because KieScanner also check the local repo, ensure the artifact are not deployed on the local maven repo
    repository.removeLocalArtifact(releaseId1);
    repository.removeLocalArtifact(releaseId2);
    ReleaseId releaseRange = ks.newReleaseId("org.kie", artifactId, "[1.0.0,)");
    InternalKieModule kJar1 = createKieJar(ks, releaseId1, "rule1", "rule2");
    KieContainer kieContainer = ks.newKieContainer(releaseRange);
    List<KieScannerEvent> events = new ArrayList<>();
    InternalKieScanner scanner = (KieRepositoryScannerImpl) ks.newKieScanner(kieContainer);
    KieScannerEventListener listener = new KieScannerEventListener() {

        public void onKieScannerStatusChangeEvent(KieScannerStatusChangeEvent statusChange) {
            LOG.info("KieScanner event: {}", statusChange);
            events.add(statusChange);
        }

        public void onKieScannerUpdateResultsEvent(KieScannerUpdateResultsEvent updtaeResults) {
            LOG.info("KieScanner event: {}", updtaeResults);
            events.add(updtaeResults);
        }
    };
    scanner.addListener(listener);
    scanner.scanNow();
    // Because 1.0.2 does not exist, will perform a scan but will NOT update.
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.SCANNING)));
    assertThat(events, CoreMatchers.not(CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.UPDATING))));
    events.clear();
    repository.installArtifact(releaseId1, kJar1, createKPom(fileManager, releaseId1));
    KieSession ksession = kieContainer.newKieSession("KSession1");
    checkKSession(ksession, "rule1", "rule2");
    // Create a wrong KJAR for releaseId2
    KieFileSystem kfs = ks.newKieFileSystem();
    String pomContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n" + "  <modelVersion>4.0.0</modelVersion>\n" + "\n" + "  <groupId>" + releaseId2.getGroupId() + "</groupId>\n" + "  <artifactId>" + releaseId2.getArtifactId() + "</artifactId>\n" + "  <version>" + releaseId2.getVersion() + "</version></project>\n";
    kfs.writePomXML(pomContent);
    kfs.write("rulebroken" + ".drl", "rule X when asd asd then end");
    kfs.write("META-INF/kmodule.xml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <!-- This is a minimal kmodule.xml. Ref Drools documentation, see http://docs.jboss.org/drools/release/6.1.0.Final/drools-docs/html_single/index.html#d0e1112 -->\n" + "</kmodule>\n");
    File tempJar = File.createTempFile("org.kie" + artifactId + "1.0.2", "tmp");
    LOG.info("Writing to temp file: {}", tempJar);
    // please notice that writeAsJar would return a new file with .jar postfixed
    tempJar = ((KieFileSystemImpl) kfs).asMemoryFileSystem().writeAsJar(tempJar.getParentFile(), tempJar.getName());
    File tempPom = File.createTempFile("org.kie" + artifactId + "1.0.2", "tmp.xml");
    LOG.info("Writing to temp file: {}", tempPom);
    FileUtils.writeStringToFile(tempPom, pomContent, "UTF-8");
    repository.installArtifact(releaseId2, tempJar, tempPom);
    assertEquals(releaseId1, scanner.getCurrentReleaseId());
    assertEquals(InternalKieScanner.Status.STOPPED, scanner.getStatus());
    // scan the maven repo to get the new kjar version and deploy it on the kcontainer
    scanner.scanNow();
    // Keeping at previous release
    assertEquals(releaseId1, scanner.getCurrentReleaseId());
    assertEquals(InternalKieScanner.Status.STOPPED, scanner.getStatus());
    // there should be no update performed.
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.SCANNING)));
    assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.UPDATING)));
    assertTrue(events.get(2) instanceof KieScannerUpdateResultsEventImpl);
    assertTrue(((KieScannerUpdateResultsEventImpl) events.get(2)).getResults().hasMessages(Message.Level.ERROR));
    events.clear();
    ks.getRepository().removeKieModule(releaseId1);
    ks.getRepository().removeKieModule(releaseId2);
    scanner.removeListener(listener);
    assertEquals(0, scanner.getListeners().size());
    repository.removeLocalArtifact(releaseId1);
    repository.removeLocalArtifact(releaseId2);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) KieScannerEventListener(org.kie.api.event.kiescanner.KieScannerEventListener) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieScannerUpdateResultsEventImpl(org.kie.scanner.event.KieScannerUpdateResultsEventImpl) InternalKieScanner(org.drools.compiler.kie.builder.impl.InternalKieScanner) KieScannerStatusChangeEventImpl(org.kie.scanner.event.KieScannerStatusChangeEventImpl) KieMavenRepository.getKieMavenRepository(org.kie.scanner.KieMavenRepository.getKieMavenRepository) KieScannerEvent(org.kie.api.event.kiescanner.KieScannerEvent) KieScannerStatusChangeEvent(org.kie.api.event.kiescanner.KieScannerStatusChangeEvent) KieSession(org.kie.api.runtime.KieSession) File(java.io.File) KieScannerUpdateResultsEvent(org.kie.api.event.kiescanner.KieScannerUpdateResultsEvent) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 14 with KieContainer

use of org.kie.api.runtime.KieContainer in project drools by kiegroup.

the class KieRepositoryScannerTest method testScannerOnPomProject.

@Test
public void testScannerOnPomProject() throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "scanner-test", "1.0");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "scanner-test", "2.0");
    KieMavenRepository repository = getKieMavenRepository();
    repository.deployPomArtifact("org.kie", "scanner-master-test", "1.0", createMasterKPom("scanner-test"));
    resetFileManager();
    InternalKieModule kJar1 = createKieJarWithClass(ks, releaseId1, false, 2, 7);
    repository.installArtifact(releaseId1, kJar1, createKPom(fileManager, releaseId1));
    KieContainer kieContainer = ks.newKieContainer(ks.newReleaseId("org.kie", "scanner-master-test", "LATEST"));
    KieSession ksession = kieContainer.newKieSession("KSession1");
    checkKSession(ksession, 14);
    KieScanner scanner = ks.newKieScanner(kieContainer);
    InternalKieModule kJar2 = createKieJarWithClass(ks, releaseId2, false, 3, 5);
    repository.installArtifact(releaseId2, kJar2, createKPom(fileManager, releaseId1));
    scanner.scanNow();
    KieSession ksession2 = kieContainer.newKieSession("KSession1");
    checkKSession(ksession2, 15);
    ks.getRepository().removeKieModule(releaseId1);
    ks.getRepository().removeKieModule(releaseId2);
}
Also used : KieMavenRepository.getKieMavenRepository(org.kie.scanner.KieMavenRepository.getKieMavenRepository) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieScanner(org.drools.compiler.kie.builder.impl.InternalKieScanner) KieScanner(org.kie.api.builder.KieScanner) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 15 with KieContainer

use of org.kie.api.runtime.KieContainer in project drools by kiegroup.

the class KieRepositoryScannerTest method testKScannerWithTransitiveInclusion.

@Test
@Ignore("avoid use external dependency")
public void testKScannerWithTransitiveInclusion() throws Exception {
    String pom = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n" + "  <modelVersion>4.0.0</modelVersion>\n" + "\n" + "  <groupId>org.kie</groupId>\n" + "  <artifactId>test-with-inc</artifactId>\n" + "  <version>1.0-SNAPSHOT</version>\n" + "  \n" + "        <dependencies>\n" + "            <dependency>\n" + "                <groupId>io.swagger</groupId>\n" + "                <artifactId>swagger-jaxrs</artifactId>\n" + "                <version>1.5.0</version>\n" + "            </dependency>\n" + "        </dependencies>\n" + "</project>";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseIdWithExc = ks.newReleaseId("org.kie", "test-with-inc", "1.0-SNAPSHOT");
    InternalKieModule kJar = createKieJarWithPom(ks, releaseIdWithExc, false, "rule1", pom);
    KieContainer kieContainer = ks.newKieContainer(releaseIdWithExc);
    try {
        Class.forName("com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider", true, kieContainer.getClassLoader());
    } catch (ClassNotFoundException e) {
        fail("the transitive dependency class should be present");
    }
}
Also used : KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

KieContainer (org.kie.api.runtime.KieContainer)357 KieServices (org.kie.api.KieServices)271 Test (org.junit.Test)246 KieSession (org.kie.api.runtime.KieSession)191 ReleaseId (org.kie.api.builder.ReleaseId)185 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)108 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)90 KieFileSystem (org.kie.api.builder.KieFileSystem)81 KieModule (org.kie.api.builder.KieModule)75 KieBuilder (org.kie.api.builder.KieBuilder)56 KieModuleModel (org.kie.api.builder.model.KieModuleModel)54 KieBase (org.kie.api.KieBase)49 ArrayList (java.util.ArrayList)44 Results (org.kie.api.builder.Results)40 Resource (org.kie.api.io.Resource)29 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)29 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)27 Message (org.drools.compiler.Message)26 KieBaseModel (org.kie.api.builder.model.KieBaseModel)26 InternalKieContainer (org.drools.core.impl.InternalKieContainer)25