From aa69a4db58140af6a605cadd3b8e6166f59d97ad Mon Sep 17 00:00:00 2001 From: Jorge Hernandez Date: Wed, 31 Jan 2018 12:31:05 -0600 Subject: 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 --- .../controller/DroolsControllerFactoryTest.java | 83 ++++++++++++++++ .../internal/MavenDroolsControllerTest.java | 106 +++++++++++++++++++++ .../protocol/coders/EventProtocolCoderTest.java | 100 +++++++++++++++++++ 3 files changed, 289 insertions(+) create mode 100644 policy-management/src/test/java/org/onap/policy/drools/controller/DroolsControllerFactoryTest.java create mode 100644 policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java create mode 100644 policy-management/src/test/java/org/onap/policy/drools/protocol/coders/EventProtocolCoderTest.java (limited to 'policy-management/src/test/java') 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 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); + } +} -- cgit 1.2.3-korg