Search in sources :

Example 21 with ReleaseId

use of org.kie.api.builder.ReleaseId in project drools by kiegroup.

the class SecurityPolicyTest method testCustomAccumulate.

@Test
public void testCustomAccumulate() throws Exception {
    String drl = "package org.foo.bar\n" + "rule testRule\n" + "    when\n" + "        Number() from accumulate(Object(), " + "               init(System.exit(-1);), " + "               action(System.exit(-1);), " + "               reverse(System.exit(-1);), " + "               result(0))\n" + "    then\n" + "end";
    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 (Exception 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) PropertyAccessException(org.mvel2.PropertyAccessException) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 22 with ReleaseId

use of org.kie.api.builder.ReleaseId in project drools by kiegroup.

the class DslExpansionTest method testDSLExpansion_MessageImplNPE.

@Test
public void testDSLExpansion_MessageImplNPE() throws Exception {
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId = ks.newReleaseId("org.kie", "dsl-test", "1.0-SNAPSHOT");
    final KieModuleModel kproj = ks.newKieModuleModel();
    final KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(kproj.toXML()).writePomXML(generatePomXml(releaseId)).write("src/main/resources/KBase1/test-dsl.dsl", createDSL()).write("src/main/resources/KBase1/test-rule.dslr", createDRL());
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    final List<Message> messages = kieBuilder.buildAll().getResults().getMessages();
    if (!messages.isEmpty()) {
        for (final Message m : messages) {
            System.out.println(m.getText());
        }
    }
    assertTrue(messages.isEmpty());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 23 with ReleaseId

use of org.kie.api.builder.ReleaseId in project drools by kiegroup.

the class WireChannelTest method testWireChannel.

@Test
public void testWireChannel() throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "listener-test", "1.0-SNAPSHOT");
    build(ks, releaseId);
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieSession ksession = kieContainer.newKieSession();
    ksession.fireAllRules();
    assertEquals(1, channelMessages.size());
    assertEquals("Test Message", channelMessages.get(0));
}
Also used : KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 24 with ReleaseId

use of org.kie.api.builder.ReleaseId in project drools by kiegroup.

the class KieModuleRepoTest method newerVersionDeployOverwritesTest.

// 2. simultaneous deploy requests
// - multitenant UI
// - duplicate REST requests
@Test(timeout = 5000)
public void newerVersionDeployOverwritesTest() throws Exception {
    // setup
    final ReleaseIdImpl releaseId = new ReleaseIdImpl("org", "deployTwiceAfterUpdateDependency", "1.0-SNAPSHOT");
    final InternalKieModule originalOldKieModule = new InternalKieModuleStub();
    final ReleaseId dependentReleaseid = new ReleaseIdImpl("org", "deployTwiceAfterUpdate", "1.0-SNAPSHOT");
    final KieContainerImpl kieContainer = createMockKieContainer(dependentReleaseid, kieModuleRepo);
    // 1. deploy
    kieModuleRepo.store(originalOldKieModule);
    // 2. another project is dependent on this project
    kieContainer.updateDependencyToVersion(releaseId, releaseId);
    final InternalKieModule newKieModule = mock(InternalKieModule.class);
    when(newKieModule.getReleaseId()).thenReturn(releaseId);
    when(newKieModule.getCreationTimestamp()).thenReturn(10l);
    final CyclicBarrier storeOperationBarrier = new CyclicBarrier(2);
    final CyclicBarrier storeSerializationBarrier = new CyclicBarrier(2);
    final CyclicBarrier threadsFinishedBarrier = new CyclicBarrier(3);
    final Runnable deployRunnable = () -> {
        waitFor(storeOperationBarrier);
        // Second thread waits with store until the first one finishes with it.
        if (Thread.currentThread().getName().equals("two")) {
            waitFor(storeSerializationBarrier);
        }
        kieModuleRepo.store(newKieModule);
        if (Thread.currentThread().getName().equals("one")) {
            waitFor(storeSerializationBarrier);
        }
        waitFor(threadsFinishedBarrier);
    };
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    // 2. remove and redploy
    final Thread deployThread = new Thread(deployRunnable);
    deployThread.setName("one");
    executor.submit(deployThread);
    final Thread secondDeployThread = new Thread(deployRunnable);
    secondDeployThread.setName("two");
    executor.submit(secondDeployThread);
    try {
        waitFor(threadsFinishedBarrier);
    } finally {
        executor.shutdownNow();
    }
    // never gets this far, but this is a good check
    final KieModule oldKieModule = kieModuleRepo.oldKieModules.get(releaseId);
    final long oldKieModuleTimeStamp = ((InternalKieModule) oldKieModule).getCreationTimestamp();
    final long originalKieModuleTimestamp = originalOldKieModule.getCreationTimestamp();
    assertEquals("The old kie module in the repo is not the originally deployed module!", originalKieModuleTimestamp, oldKieModuleTimeStamp);
}
Also used : ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) ExecutorService(java.util.concurrent.ExecutorService) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 25 with ReleaseId

use of org.kie.api.builder.ReleaseId in project drools by kiegroup.

the class ReleaseIdTest method testDecendingSort.

@Test
public void testDecendingSort() {
    List<ReleaseId> list = newUnsortedList();
    list.sort(new ReleaseIdComparator(DESCENDING));
    assertSame(gav7, list.get(0));
    assertSame(gav6, list.get(1));
    assertSame(gav5, list.get(2));
    assertSame(gav4, list.get(3));
    assertSame(gav3, list.get(4));
    assertSame(gav2, list.get(5));
    assertSame(gav1, list.get(6));
    assertSame(gav0, list.get(7));
}
Also used : ReleaseId(org.kie.api.builder.ReleaseId) ReleaseIdComparator(org.kie.api.builder.ReleaseIdComparator) Test(org.junit.Test)

Aggregations

ReleaseId (org.kie.api.builder.ReleaseId)565 KieServices (org.kie.api.KieServices)449 Test (org.junit.Test)398 KieContainer (org.kie.api.runtime.KieContainer)313 KieSession (org.kie.api.runtime.KieSession)234 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)159 KieFileSystem (org.kie.api.builder.KieFileSystem)132 ArrayList (java.util.ArrayList)120 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)100 KieModule (org.kie.api.builder.KieModule)94 KieBuilder (org.kie.api.builder.KieBuilder)86 KieModuleModel (org.kie.api.builder.model.KieModuleModel)85 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)76 File (java.io.File)75 Resource (org.kie.api.io.Resource)53 KieMavenRepository (org.kie.scanner.KieMavenRepository)53 FileOutputStream (java.io.FileOutputStream)51 KieBase (org.kie.api.KieBase)47 Before (org.junit.Before)39 HashMap (java.util.HashMap)31