aboutsummaryrefslogtreecommitdiffstats
path: root/policy-core/src/test
diff options
context:
space:
mode:
authorStraubs, Ralph (rs8887) <rs8887@att.com>2019-11-11 11:28:47 -0600
committerStraubs, Ralph (rs8887) <rs8887@att.com>2019-11-14 03:07:25 -0600
commit1320df00310f70b3cf6695466eb2d76a2d3dc49b (patch)
treec7d4f55b594db9a2bbec98a5ba1ecb207467770f /policy-core/src/test
parent2b0f80062f0609f483eea287bdf503be45f12472 (diff)
Add 'DroolsRunnable' class
This provides a simple way to run arbitrary Java code within the Drools thread. This change also includes a general way to specify Drools rules that are automatically added to every Drools session. Change-Id: I5ddcca4c807dc552fbcbd4a19dce311a4d358279 Issue-ID: POLICY-1948 Signed-off-by: Straubs, Ralph (rs8887) <rs8887@att.com>
Diffstat (limited to 'policy-core/src/test')
-rw-r--r--policy-core/src/test/java/org/onap/policy/drools/core/DroolsContainerTest.java12
-rw-r--r--policy-core/src/test/java/org/onap/policy/drools/util/KieUtilsTest.java62
2 files changed, 73 insertions, 1 deletions
diff --git a/policy-core/src/test/java/org/onap/policy/drools/core/DroolsContainerTest.java b/policy-core/src/test/java/org/onap/policy/drools/core/DroolsContainerTest.java
index d168ca8b..2f45f01e 100644
--- a/policy-core/src/test/java/org/onap/policy/drools/core/DroolsContainerTest.java
+++ b/policy-core/src/test/java/org/onap/policy/drools/core/DroolsContainerTest.java
@@ -165,6 +165,18 @@ public class DroolsContainerTest {
container.insert("session1", result);
assertEquals(48, result.poll(TIMEOUT_SEC, TimeUnit.SECONDS).intValue());
+
+ // verify that default KiePackages have been added by testing
+ // that 'DroolsRunnable' is functioning
+
+ final LinkedBlockingQueue<String> lbq = new LinkedBlockingQueue<>();
+ container.insert("session1", new DroolsRunnable() {
+ @Override
+ public void run() {
+ lbq.add("DroolsRunnable String");
+ }
+ });
+ assertEquals("DroolsRunnable String", lbq.poll(TIMEOUT_SEC, TimeUnit.SECONDS));
} finally {
container.shutdown();
assertFalse(container.isAlive());
diff --git a/policy-core/src/test/java/org/onap/policy/drools/util/KieUtilsTest.java b/policy-core/src/test/java/org/onap/policy/drools/util/KieUtilsTest.java
index 83f62b32..89ab9d0a 100644
--- a/policy-core/src/test/java/org/onap/policy/drools/util/KieUtilsTest.java
+++ b/policy-core/src/test/java/org/onap/policy/drools/util/KieUtilsTest.java
@@ -22,12 +22,15 @@ package org.onap.policy.drools.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import java.io.IOException;
+import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.Enumeration;
import java.util.stream.Collectors;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -119,4 +122,61 @@ public class KieUtilsTest {
public void getFacts() {
assertEquals(0, KieUtils.getFacts(session).size());
}
-} \ No newline at end of file
+
+ @Test
+ public void resourceToPackagesTests() {
+ // Some minimal logging -- it would be nice to verify the 'KieUtils' logger messages
+ StringBuffer log;
+
+ // test IOException from ClassLoader
+ log = new StringBuffer();
+ assertNull(KieUtils.resourceToPackages(new BogusClassLoader(log), "BogusClassLoader"));
+ assertEquals("IOException(BogusClassLoader)", log.toString());
+
+ // test 'null' return when no resources are found
+ assertNull(KieUtils.resourceToPackages(ClassLoader.getSystemClassLoader(), "no/such/url"));
+
+ // test IOException in 'IOUtils.toByteArray()' -> 'InputStream.read()'
+ log = new StringBuffer();
+ assertNull(KieUtils.resourceToPackages(new BogusClassLoader(log), "BogusUrl"));
+ assertEquals("", log.toString());
+
+ // don't know how to test 'KieBuilder' errors at this point
+
+ // success legs are tested in 'DroolsContainerTest'
+ }
+
+ static class BogusClassLoader extends ClassLoader {
+ StringBuffer log;
+
+ BogusClassLoader(StringBuffer log) {
+ this.log = log;
+ }
+
+ @Override
+ public Enumeration<URL> getResources(String name) throws IOException {
+ if ("BogusUrl".equals(name)) {
+ return new Enumeration<URL>() {
+ @Override
+ public boolean hasMoreElements() {
+ return true;
+ }
+
+ @Override
+ public URL nextElement() {
+ try {
+ // when the following URL is used, an IOException will occur
+ return new URL("http://127.0.0.1:1");
+ } catch (IOException e) {
+ // this should never happen, as the URL above is syntactically valid
+ return null;
+ }
+ }
+ };
+ } else {
+ log.append("IOException(BogusClassLoader)");
+ throw new IOException("BogusClassLoader");
+ }
+ }
+ }
+}