diff options
author | Jorge Hernandez <jh1730@att.com> | 2018-01-31 12:31:05 -0600 |
---|---|---|
committer | Jorge Hernandez <jh1730@att.com> | 2018-01-31 13:23:20 -0600 |
commit | aa69a4db58140af6a605cadd3b8e6166f59d97ad (patch) | |
tree | 07b1c15b2f95421370e2f282485665cadb8ba241 /policy-management | |
parent | 9e2820c5820b3904e03cb7a4cb4292e864b1ca2b (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>
Diffstat (limited to 'policy-management')
7 files changed, 383 insertions, 1 deletions
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> |