aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJorge Hernandez <jh1730@att.com>2018-01-31 12:31:05 -0600
committerJorge Hernandez <jh1730@att.com>2018-01-31 13:23:20 -0600
commitaa69a4db58140af6a605cadd3b8e6166f59d97ad (patch)
tree07b1c15b2f95421370e2f282485665cadb8ba241
parent9e2820c5820b3904e03cb7a4cb4292e864b1ca2b (diff)
junits+utils to generate/test rules applications
1. utilities to generate rules applications and installing in local maven repository programmatically. 2. using these utilities, use policy abstractions, ie. controllers, containers, .. to junit test it. Change-Id: I13c35e631a1120fad503feef2593d32a99e3358f Issue-ID: POLICY-583 Signed-off-by: Jorge Hernandez <jh1730@att.com>
-rw-r--r--.gitignore2
-rw-r--r--policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java112
-rw-r--r--policy-management/src/main/java/org/onap/policy/drools/controller/DroolsControllerFactory.java2
-rw-r--r--policy-management/src/test/java/org/onap/policy/drools/controller/DroolsControllerFactoryTest.java83
-rw-r--r--policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java106
-rw-r--r--policy-management/src/test/java/org/onap/policy/drools/protocol/coders/EventProtocolCoderTest.java100
-rw-r--r--policy-management/src/test/resources/echo.drl36
-rw-r--r--policy-management/src/test/resources/echo.kmodule26
-rw-r--r--policy-management/src/test/resources/echo.pom31
9 files changed, 497 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 30e60521..eb32cca6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,8 +8,10 @@
*.log
*.out
.metadata/
+.idea/
target/
*/logs/
*/sql/
*/testingLogs/
*/config/
+**/*.iml
diff --git a/policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java b/policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java
new file mode 100644
index 00000000..59ee5119
--- /dev/null
+++ b/policy-core/src/main/java/org/onap/policy/drools/util/KieUtils.java
@@ -0,0 +1,112 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.drools.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import org.drools.compiler.kie.builder.impl.InternalKieModule;
+import org.drools.compiler.kproject.models.KieModuleModelImpl;
+import org.kie.api.KieServices;
+import org.kie.api.builder.KieBuilder;
+import org.kie.api.builder.KieFileSystem;
+import org.kie.api.builder.Message;
+import org.kie.api.builder.ReleaseId;
+import org.kie.api.builder.model.KieModuleModel;
+import org.kie.scanner.MavenRepository;
+
+/**
+ * Kie related utilities
+ */
+public class KieUtils {
+
+ /**
+ * Installs a rules artifact in the local maven repository
+ *
+ * @param kmodule kmodule specification
+ * @param pom pom
+ * @param drlKJarPath path used in kjar drl to the actual drl
+ * @param drl rules in drl language
+ *
+ * @return releaseId result o a sucessful installation
+ * @throws IOException error accessing necessary resources
+ */
+ public static ReleaseId installArtifact(String kmodule, String pom, String drlKJarPath, String drl) throws IOException {
+ KieModuleModel kieModule = KieModuleModelImpl.fromXML(kmodule);
+
+ final KieFileSystem kieFileSystem = KieServices.Factory.get().newKieFileSystem();
+ kieFileSystem.writeKModuleXML(kieModule.toXML());
+ kieFileSystem.writePomXML(pom);
+ kieFileSystem.write(drlKJarPath, drl);
+
+ KieBuilder kieBuilder = kieBuild(kieFileSystem);
+
+ Path pomPath = Files.createTempFile("policy-core-", ".pom");
+ Files.write(pomPath, pom.getBytes(StandardCharsets.UTF_8));
+ File pomFile = pomPath.toFile();
+ pomFile.deleteOnExit();
+
+ ReleaseId releaseId = kieBuilder.getKieModule().getReleaseId();
+ MavenRepository.getMavenRepository().
+ installArtifact(releaseId,
+ (InternalKieModule) kieBuilder.getKieModule(),
+ pomFile);
+ return releaseId;
+ }
+
+ /**
+ * Installs a rules artifact in the local maven repository
+ *
+ * @param kmodule kmodule specification
+ * @param pom pom
+ * @param drlKJarPath path used in kjar drl to the actual drl
+ * @param drl rules in drl language
+ *
+ * @return releaseId result o a sucessful installation
+ * @throws IOException error accessing necessary resources
+ */
+ public static ReleaseId installArtifact(File kmodule, File pom, String drlKJarPath, File drl) throws IOException {
+ KieModuleModel kieModule = KieModuleModelImpl.fromXML(kmodule);
+
+ final KieFileSystem kieFileSystem = KieServices.Factory.get().newKieFileSystem();
+ kieFileSystem.writeKModuleXML(kieModule.toXML());
+ kieFileSystem.writePomXML(new String(Files.readAllBytes(pom.toPath())));
+ kieFileSystem.write(drlKJarPath, new String(Files.readAllBytes(drl.toPath())));
+
+ KieBuilder kieBuilder = kieBuild(kieFileSystem);
+
+ ReleaseId releaseId = kieBuilder.getKieModule().getReleaseId();
+ MavenRepository.getMavenRepository().
+ installArtifact(releaseId, (InternalKieModule) kieBuilder.getKieModule(), pom);
+ return releaseId;
+ }
+
+ private static KieBuilder kieBuild(KieFileSystem kieFileSystem) {
+ KieBuilder kieBuilder = KieServices.Factory.get().newKieBuilder(kieFileSystem);
+ List<Message> messages = kieBuilder.buildAll().getResults().getMessages();
+ if (messages != null && !messages.isEmpty())
+ throw new IllegalArgumentException(messages.toString());
+ return kieBuilder;
+ }
+}
diff --git a/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsControllerFactory.java b/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsControllerFactory.java
index 31b7cec7..0b9f9887 100644
--- a/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsControllerFactory.java
+++ b/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsControllerFactory.java
@@ -212,7 +212,7 @@ class IndexedDroolsControllerFactory implements DroolsControllerFactory {
List<TopicCoderFilterConfiguration>
topics2DecodedClasses2Filters = new ArrayList<>();
- if (topicEntities.isEmpty())
+ if (topicEntities == null || topicEntities.isEmpty())
return topics2DecodedClasses2Filters;
for (Topic topic: topicEntities) {
diff --git a/policy-management/src/test/java/org/onap/policy/drools/controller/DroolsControllerFactoryTest.java b/policy-management/src/test/java/org/onap/policy/drools/controller/DroolsControllerFactoryTest.java
new file mode 100644
index 00000000..3db2e7c7
--- /dev/null
+++ b/policy-management/src/test/java/org/onap/policy/drools/controller/DroolsControllerFactoryTest.java
@@ -0,0 +1,83 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * policy-management
+ * ================================================================================
+ * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.drools.controller;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.List;
+import java.util.Properties;
+import org.junit.Test;
+
+public class DroolsControllerFactoryTest {
+
+ @Test
+ public void buildNullController() {
+ Properties droolsProps = new Properties();
+ DroolsController droolsController =
+ DroolsController.factory.build(droolsProps, null, null);
+
+ assertNullController(droolsController);
+ }
+
+ @Test
+ public void getNullController() {
+ DroolsController controller =
+ DroolsController.factory.get(DroolsController.NO_GROUP_ID,
+ DroolsController.NO_ARTIFACT_ID, DroolsController.NO_VERSION);
+
+ assertNotNull(controller);
+ assertEquals(controller.getGroupId(), DroolsController.NO_GROUP_ID);
+ assertEquals(controller.getArtifactId(), DroolsController.NO_ARTIFACT_ID);
+ assertEquals(controller.getVersion(), DroolsController.NO_VERSION);
+ }
+
+ @Test
+ public void inventory() {
+ List<DroolsController> controllers = DroolsController.factory.inventory();
+ assertNotNull(controllers);
+ assertTrue(controllers.size() == 1);
+ assertNullController(controllers.get(0));
+ }
+
+ @Test
+ public void shutdown() {
+ DroolsControllerFactory droolsFactory = new IndexedDroolsControllerFactory();
+ droolsFactory.shutdown();
+ assertTrue(droolsFactory.inventory().isEmpty());
+ }
+
+ @Test
+ public void destroy() {
+ DroolsControllerFactory droolsFactory = new IndexedDroolsControllerFactory();
+ droolsFactory.destroy();
+ assertTrue(droolsFactory.inventory().isEmpty());
+ }
+
+ private void assertNullController(DroolsController droolsController) {
+ assertNotNull(droolsController);
+ assertEquals(droolsController.getGroupId(), DroolsController.NO_GROUP_ID);
+ assertEquals(droolsController.getArtifactId(), DroolsController.NO_ARTIFACT_ID);
+ assertEquals(droolsController.getVersion(), DroolsController.NO_VERSION);
+ }
+
+}
diff --git a/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java
new file mode 100644
index 00000000..a938bf20
--- /dev/null
+++ b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java
@@ -0,0 +1,106 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * policy-management
+ * ================================================================================
+ * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.drools.controller.internal;
+
+import java.io.IOException;
+import java.nio.file.Paths;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.kie.api.builder.ReleaseId;
+import org.onap.policy.drools.controller.DroolsController;
+import org.onap.policy.drools.util.KieUtils;
+
+public class MavenDroolsControllerTest {
+
+ private static final String JUNIT_ECHO_KSESSION = "echo";
+ private static final String JUNIT_ECHO_KMODULE_DRL_PATH = "src/test/resources/echo.drl";
+ private static final String JUNIT_ECHO_KMODULE_POM_PATH = "src/test/resources/echo.pom";
+ private static final String JUNIT_ECHO_KMODULE_PATH = "src/test/resources/echo.kmodule";
+ private static final String JUNIT_ECHO_KJAR_DRL_PATH =
+ "src/main/resources/kbEcho/org/onap/policy/drools/test/echo.drl";
+
+ private static volatile ReleaseId releaseId;
+
+ @BeforeClass
+ public static void setUp() throws IOException {
+ releaseId =
+ KieUtils.installArtifact(Paths.get(JUNIT_ECHO_KMODULE_PATH).toFile(),
+ Paths.get(JUNIT_ECHO_KMODULE_POM_PATH).toFile(),
+ JUNIT_ECHO_KJAR_DRL_PATH,
+ Paths.get(JUNIT_ECHO_KMODULE_DRL_PATH).toFile());
+ }
+
+ @Test
+ public void stop() throws IOException, InterruptedException {
+ createDroolsController(10000L).stop();
+ }
+
+ @Test
+ public void shutdown() throws IOException, InterruptedException {
+ createDroolsController(10000L).shutdown();
+ }
+
+ @Test
+ public void lock() throws IOException, InterruptedException {
+ DroolsController controller = createDroolsController(30000L);
+
+ controller.lock();
+ Assert.assertTrue(controller.isLocked());
+
+ controller.unlock();
+ Assert.assertFalse(controller.isLocked());
+
+ controller.halt();
+ Assert.assertFalse(controller.isAlive());
+ }
+
+ private DroolsController createDroolsController(long courtesyStartTimeMs) throws InterruptedException {
+ DroolsController controller = new MavenDroolsController(releaseId.getGroupId(),
+ releaseId.getArtifactId(), releaseId.getVersion(), null, null);
+
+ Assert.assertFalse(controller.isAlive());
+ Assert.assertTrue(controller.isBrained());
+
+ controller.start();
+
+ Assert.assertTrue(controller.isAlive());
+ Assert.assertTrue(controller.isBrained());
+
+ Assert.assertEquals(releaseId.getGroupId(), controller.getGroupId());
+ Assert.assertEquals(releaseId.getArtifactId(), controller.getArtifactId());
+ Assert.assertEquals(releaseId.getVersion(), controller.getVersion());
+
+ Assert.assertEquals(releaseId.getGroupId(), controller.getContainer().getGroupId());
+ Assert.assertEquals(releaseId.getArtifactId(), controller.getContainer().getArtifactId());
+ Assert.assertEquals(releaseId.getVersion(), controller.getContainer().getVersion());
+
+ /* courtesy timer to allow full initialization from local maven repository */
+ Thread.sleep(courtesyStartTimeMs);
+
+ Assert.assertTrue(controller.getSessionNames().size() == 1);
+ Assert.assertEquals(JUNIT_ECHO_KSESSION, controller.getSessionNames().get(0));
+ Assert.assertTrue(controller.getCanonicalSessionNames().size() == 1);
+ Assert.assertTrue(controller.getCanonicalSessionNames().get(0).contains(JUNIT_ECHO_KSESSION));
+
+ return controller;
+ }
+}
diff --git a/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/EventProtocolCoderTest.java b/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/EventProtocolCoderTest.java
new file mode 100644
index 00000000..a56bb5f2
--- /dev/null
+++ b/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/EventProtocolCoderTest.java
@@ -0,0 +1,100 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.drools.protocol.coders;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.Properties;
+
+
+import org.junit.Test;
+import org.onap.policy.drools.event.comm.TopicEndpoint;
+import org.onap.policy.drools.properties.PolicyProperties;
+import org.onap.policy.drools.protocol.configuration.DroolsConfiguration;
+
+/**
+ * Tests Coders
+ */
+public class EventProtocolCoderTest {
+
+ /**
+ * Coder Group
+ */
+ private static final String ENCODER_GROUP = "foo";
+
+ /**
+ * Coder Artifact
+ */
+ private static final String ENCODER_ARTIFACT = "bar";
+
+ /**
+ * Coder Version
+ */
+ private static final String ENCODER_VERSION = null;
+
+ /**
+ * noop topic
+ */
+ private static final String NOOP_TOPIC = "JUNIT";
+
+ /**
+ * Event Test Class
+ */
+ public static class EventTest {
+
+ private String field;
+
+ public EventTest(String field) {
+ super();
+ this.field = field;
+ }
+
+ public String getField() {
+ return this.field;
+ }
+
+ public void setField(String field) {
+ this.field = field;
+ }
+ }
+
+ @Test
+ public void test() {
+
+ final Properties noopSinkProperties = new Properties();
+ noopSinkProperties.put(PolicyProperties.PROPERTY_NOOP_SINK_TOPICS, NOOP_TOPIC);
+
+ TopicEndpoint.manager.addTopicSinks(noopSinkProperties);
+
+ EventProtocolCoder.manager.addEncoder(ENCODER_GROUP, ENCODER_ARTIFACT, NOOP_TOPIC,
+ DroolsConfiguration.class.getCanonicalName(), new JsonProtocolFilter(), null, null,
+ DroolsConfiguration.class.getName().hashCode());
+
+ final String json = EventProtocolCoder.manager.encode(NOOP_TOPIC,
+ new DroolsConfiguration(ENCODER_GROUP, ENCODER_ARTIFACT, ENCODER_VERSION));
+
+ assertTrue(json.contains(ENCODER_GROUP));
+ assertTrue(json.contains(ENCODER_ARTIFACT));
+
+ EventProtocolCoder.manager.removeEncoders(ENCODER_GROUP, ENCODER_ARTIFACT, NOOP_TOPIC);
+ }
+}
diff --git a/policy-management/src/test/resources/echo.drl b/policy-management/src/test/resources/echo.drl
new file mode 100644
index 00000000..664df639
--- /dev/null
+++ b/policy-management/src/test/resources/echo.drl
@@ -0,0 +1,36 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.drools.test;
+
+rule "INIT"
+lock-on-active
+when
+then
+ insert(new String("hello,I am up"));
+end
+
+rule "ECHO"
+when
+ $o : Object();
+then
+ System.out.println("ECHO: " + $o.toString());
+ retract($o);
+end
diff --git a/policy-management/src/test/resources/echo.kmodule b/policy-management/src/test/resources/echo.kmodule
new file mode 100644
index 00000000..437a91ca
--- /dev/null
+++ b/policy-management/src/test/resources/echo.kmodule
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ============LICENSE_START=======================================================
+ archetype-closed-loop-demo-rules
+ ================================================================================
+ Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ ================================================================================
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ ============LICENSE_END=========================================================
+ -->
+
+<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
+ <kbase name="kbEcho">
+ <ksession name="echo"/>
+ </kbase>
+</kmodule>
diff --git a/policy-management/src/test/resources/echo.pom b/policy-management/src/test/resources/echo.pom
new file mode 100644
index 00000000..eec12af1
--- /dev/null
+++ b/policy-management/src/test/resources/echo.pom
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ============LICENSE_START=======================================================
+ ONAP
+ ================================================================================
+ Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ ================================================================================
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ ============LICENSE_END=========================================================
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.onap.policy.drools.test</groupId>
+ <artifactId>echo</artifactId>
+ <version>1.2.0-SNAPSHOT</version>
+
+</project>