diff options
Diffstat (limited to 'feature-lifecycle/src/test')
-rw-r--r-- | feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java | 112 | ||||
-rw-r--r-- | feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java | 42 | ||||
-rw-r--r-- | feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java | 67 | ||||
-rw-r--r-- | feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java | 63 | ||||
-rw-r--r-- | feature-lifecycle/src/test/resources/lifecycle.drl (renamed from feature-lifecycle/src/test/resources/echo.drl) | 23 | ||||
-rw-r--r-- | feature-lifecycle/src/test/resources/lifecycle.kmodule (renamed from feature-lifecycle/src/test/resources/echo.kmodule) | 4 | ||||
-rw-r--r-- | feature-lifecycle/src/test/resources/lifecycle.pom (renamed from feature-lifecycle/src/test/resources/echo.pom) | 2 | ||||
-rw-r--r-- | feature-lifecycle/src/test/resources/tosca-policy.json | 9 |
8 files changed, 271 insertions, 51 deletions
diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java new file mode 100644 index 00000000..1beee552 --- /dev/null +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java @@ -0,0 +1,112 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2019 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.lifecycle; + +import java.io.IOException; +import java.nio.file.Paths; +import java.util.List; +import java.util.Properties; +import java.util.stream.Collectors; +import lombok.Getter; +import lombok.NonNull; +import org.kie.api.builder.ReleaseId; +import org.onap.policy.drools.properties.DroolsProperties; +import org.onap.policy.drools.system.PolicyController; +import org.onap.policy.drools.util.KieUtils; + +/** + * Controller Test Support. + */ +public class ControllerSupport { + + protected static final String JUNIT_KMODULE_DRL_PATH = "src/test/resources/lifecycle.drl"; + protected static final String JUNIT_KMODULE_POM_PATH = "src/test/resources/lifecycle.pom"; + protected static final String JUNIT_KMODULE_PATH = "src/test/resources/lifecycle.kmodule"; + protected static final String JUNIT_KJAR_DRL_PATH = + "src/main/resources/kbLifecycle/org/onap/policy/drools/test/lifecycle.drl"; + + protected static final String POLICY_TYPE = "onap.policies.controlloop.Operational"; + protected static final String POLICY_TYPE_VERSION = "1.0.0"; + + protected static final String SESSION_NAME = "junits"; + + @Getter + private final String name; + + public ControllerSupport(@NonNull String name) { + this.name = name; + } + + /** + * Create controller. + */ + public PolicyController createController() throws IOException { + ReleaseId coordinates = + KieUtils.installArtifact(Paths.get(JUNIT_KMODULE_PATH).toFile(), + Paths.get(JUNIT_KMODULE_POM_PATH).toFile(), + JUNIT_KJAR_DRL_PATH, + Paths.get(JUNIT_KMODULE_DRL_PATH).toFile()); + + + Properties controllerProps = new Properties(); + controllerProps.put(DroolsProperties.PROPERTY_CONTROLLER_NAME, name); + controllerProps.put(DroolsProperties.PROPERTY_CONTROLLER_POLICY_TYPES, getPolicyType()); + controllerProps.put(DroolsProperties.RULES_GROUPID, coordinates.getGroupId()); + controllerProps.put(DroolsProperties.RULES_ARTIFACTID, coordinates.getArtifactId()); + controllerProps.put(DroolsProperties.RULES_VERSION, coordinates.getVersion()); + + return PolicyController.factory.build(name, controllerProps); + } + + /** + * Destroy the echo controller. + */ + public void destroyController() { + PolicyController.factory.destroy(name); + } + + /** + * Get controller. + */ + public PolicyController getController() { + return PolicyController.factory.get(name); + } + + /** + * Get Policy Type. + */ + public static String getPolicyType() { + return POLICY_TYPE + ":" + POLICY_TYPE_VERSION; + } + + /** + * Get facts. + */ + public <T> List<T> getFacts(Class<T> clazz) { + return PolicyController.factory.get(name) + .getDrools() + .facts(SESSION_NAME, clazz.getCanonicalName(), false) + .stream() + .filter(clazz::isInstance) + .map(clazz::cast) + .collect(Collectors.toList()); + } +} diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java index c4d47d83..32006425 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java @@ -27,43 +27,30 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; -import org.junit.AfterClass; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.network.NetworkUtil; -import org.onap.policy.drools.persistence.SystemPersistence; -import org.onap.policy.drools.utils.logging.LoggerUtil; import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.pdp.enums.PdpMessageType; import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; /** * Lifecycle State Active Test. */ -public class LifecycleStateActiveTest { - - private LifecycleFsm fsm; - - @BeforeClass - public static void setUp() { - SystemPersistence.manager.setConfigurationDir("src/test/resources"); - LoggerUtil.setLevel("org.onap.policy.common.endpoints", "WARN"); - } - - @AfterClass - public static void tearDown() { - SystemPersistence.manager.setConfigurationDir(null); - } +public class LifecycleStateActiveTest extends LifecycleStateRunningTest { /** * Start tests in the Active state. @@ -199,7 +186,7 @@ public class LifecycleStateActiveTest { } @Test - public void update() { + public void update() throws IOException, CoderException { PdpUpdate update = new PdpUpdate(); update.setName(NetworkUtil.getHostname()); update.setPdpGroup("Z"); @@ -210,6 +197,9 @@ public class LifecycleStateActiveTest { long interval = 2 * originalInterval; update.setPdpHeartbeatIntervalMs(interval * 1000L); + controllerSupport.getController().start(); + fsm.start(controllerSupport.getController()); + assertTrue(fsm.update(update)); assertEquals(PdpState.ACTIVE, fsm.state()); @@ -217,6 +207,20 @@ public class LifecycleStateActiveTest { assertEquals("Z", fsm.getGroup()); assertEquals("z", fsm.getSubgroup()); + String rawPolicy = + new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy.json"))); + ToscaPolicy toscaPolicy = new StandardCoder().decode(rawPolicy, ToscaPolicy.class); + update.setPolicies(Arrays.asList(toscaPolicy)); + + assertTrue(fsm.update(update)); + assertEquals(1, fsm.policyTypesMap.size()); + + List<ToscaPolicy> factPolicies = controllerSupport.getFacts(ToscaPolicy.class); + assertEquals(1, factPolicies.size()); + assertEquals(toscaPolicy, factPolicies.get(0)); + assertEquals(1, fsm.policiesMap.size()); + + controllerSupport.getController().stop(); fsm.shutdown(); } } diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java index 376eb3a7..100bcef5 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java @@ -27,45 +27,35 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Arrays; import java.util.Collections; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import org.awaitility.core.ConditionTimeoutException; -import org.junit.AfterClass; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.network.NetworkUtil; -import org.onap.policy.drools.persistence.SystemPersistence; -import org.onap.policy.drools.utils.logging.LoggerUtil; import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.pdp.enums.PdpHealthStatus; import org.onap.policy.models.pdp.enums.PdpMessageType; import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; /** * Lifecycle State Passive Tests. */ -public class LifecycleStatePassiveTest { - - private LifecycleFsm fsm; - - @BeforeClass - public static void setUp() { - SystemPersistence.manager.setConfigurationDir("src/test/resources"); - LoggerUtil.setLevel("org.onap.policy.common.endpoints", "WARN"); - } - - @AfterClass - public static void tearDown() { - SystemPersistence.manager.setConfigurationDir(null); - } +public class LifecycleStatePassiveTest extends LifecycleStateRunningTest { /** * Start tests in the Passive state. @@ -87,6 +77,21 @@ public class LifecycleStatePassiveTest { } @Test + public void controller() { + fsm.start(controllerSupport.getController()); + assertSame(controllerSupport.getController(), + fsm.getController(new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE, + ControllerSupport.POLICY_TYPE_VERSION))); + + fsm.stop(controllerSupport.getController()); + assertNull(fsm.getController( + new ToscaPolicyTypeIdentifier(ControllerSupport.POLICY_TYPE, + ControllerSupport.POLICY_TYPE_VERSION))); + + fsm.shutdown(); + } + + @Test public void start() { assertEquals(0, fsm.client.getSink().getRecentEvents().length); assertFalse(fsm.start()); @@ -163,7 +168,7 @@ public class LifecycleStatePassiveTest { } @Test - public void update() { + public void update() throws IOException, CoderException { PdpUpdate update = new PdpUpdate(); update.setName(NetworkUtil.getHostname()); update.setPdpGroup("Z"); @@ -181,6 +186,30 @@ public class LifecycleStatePassiveTest { assertEquals("z", fsm.getSubgroup()); assertBasicPassive(); + String rawPolicy = + new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy.json"))); + ToscaPolicy toscaPolicy = new StandardCoder().decode(rawPolicy, ToscaPolicy.class); + update.setPolicies(Arrays.asList(toscaPolicy)); + + assertFalse(fsm.update(update)); + + assertEquals(PdpState.PASSIVE, fsm.state()); + assertEquals(interval, fsm.getStatusTimerSeconds()); + assertEquals("Z", fsm.getGroup()); + assertEquals("z", fsm.getSubgroup()); + assertBasicPassive(); + + assertTrue(fsm.policyTypesMap.isEmpty()); + assertTrue(fsm.policiesMap.isEmpty()); + + fsm.start(controllerSupport.getController()); + assertEquals(1, fsm.policyTypesMap.size()); + assertTrue(fsm.policiesMap.isEmpty()); + + assertTrue(fsm.update(update)); + assertEquals(1, fsm.policyTypesMap.size()); + assertTrue(fsm.policiesMap.isEmpty()); + fsm.shutdown(); } diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java new file mode 100644 index 00000000..d7bb6d75 --- /dev/null +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateRunningTest.java @@ -0,0 +1,63 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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. + * + * SPDX-License-Identifier: Apache-2.0 + * =============LICENSE_END======================================================== + */ + +package org.onap.policy.drools.lifecycle; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.onap.policy.drools.persistence.SystemPersistence; +import org.onap.policy.drools.utils.logging.LoggerUtil; + +public abstract class LifecycleStateRunningTest { + + private static final String CONTROLLER_NAME = "lifecycle"; + protected static ControllerSupport controllerSupport = new ControllerSupport(CONTROLLER_NAME); + protected LifecycleFsm fsm; + + /** + * Set up. + */ + @BeforeClass + public static void setUp() throws IOException { + LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO"); + LoggerUtil.setLevel("org.onap.policy.common.endpoints", "WARN"); + LoggerUtil.setLevel("org.onap.policy.drools", "WARN"); + SystemPersistence.manager.setConfigurationDir("src/test/resources"); + controllerSupport.createController(); + } + + /** + * Tear Down. + */ + @AfterClass + public static void tearDown() { + controllerSupport.destroyController(); + try { + Files.deleteIfExists(Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), + CONTROLLER_NAME + "-controller.properties.bak")); + } catch (IOException e) { + ; + } + SystemPersistence.manager.setConfigurationDir(null); + } +} diff --git a/feature-lifecycle/src/test/resources/echo.drl b/feature-lifecycle/src/test/resources/lifecycle.drl index c044f2cb..597661cf 100644 --- a/feature-lifecycle/src/test/resources/echo.drl +++ b/feature-lifecycle/src/test/resources/lifecycle.drl @@ -20,17 +20,20 @@ package org.onap.policy.drools.test; -rule "INIT" -lock-on-active -when -then - insert("hello, I am up!"); -end +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; -rule "ECHO" +rule "INSERT.TOSCA.POLICY" when - $o : Object(); + $policy : ToscaPolicy(); then - System.out.println("ECHO: " + $o); - retract($o); + System.out.println(""); + System.out.println(""); + System.out.println("************************************************************************"); + System.out.println(drools.getRule().getName() + ":"); + System.out.println(""); + System.out.println("Tosca Policy Type: " + $policy.getType() + " " + $policy.getTypeVersion()); + System.out.println("Tosca Policy: " + $policy.getName() + " " + $policy.getVersion()); + System.out.println("************************************************************************"); + System.out.println(""); + System.out.println(""); end diff --git a/feature-lifecycle/src/test/resources/echo.kmodule b/feature-lifecycle/src/test/resources/lifecycle.kmodule index 1019bd3d..8bf1ed5a 100644 --- a/feature-lifecycle/src/test/resources/echo.kmodule +++ b/feature-lifecycle/src/test/resources/lifecycle.kmodule @@ -20,7 +20,7 @@ --> <kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule"> - <kbase name="controller-logs"> - <ksession name="test" /> + <kbase name="onap.policies.type1.type2"> + <ksession name="junits" /> </kbase> </kmodule> diff --git a/feature-lifecycle/src/test/resources/echo.pom b/feature-lifecycle/src/test/resources/lifecycle.pom index 7e654793..87eafc10 100644 --- a/feature-lifecycle/src/test/resources/echo.pom +++ b/feature-lifecycle/src/test/resources/lifecycle.pom @@ -23,7 +23,7 @@ <modelVersion>4.0.0</modelVersion> <groupId>org.onap.policy.drools.test</groupId> - <artifactId>echo</artifactId> + <artifactId>lifecycle</artifactId> <version>1.4.0-SNAPSHOT</version> </project> diff --git a/feature-lifecycle/src/test/resources/tosca-policy.json b/feature-lifecycle/src/test/resources/tosca-policy.json new file mode 100644 index 00000000..5258ca1c --- /dev/null +++ b/feature-lifecycle/src/test/resources/tosca-policy.json @@ -0,0 +1,9 @@ +{ + "type": "onap.policies.controlloop.Operational", + "typeVersion": "1.0.0", + "properties": { + "content": "controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20controlLoopName%3A%20ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e%0A%20%20trigger_policy%3A%20unique-policy-id-1-restart%0A%20%20timeout%3A%203600%0A%20%20abatement%3A%20true%0A%20%0Apolicies%3A%0A%20%20-%20id%3A%20unique-policy-id-1-restart%0A%20%20%20%20name%3A%20Restart%20the%20VM%0A%20%20%20%20description%3A%0A%20%20%20%20actor%3A%20APPC%0A%20%20%20%20recipe%3A%20Restart%0A%20%20%20%20target%3A%0A%20%20%20%20%20%20type%3A%20VM%0A%20%20%20%20retry%3A%203%0A%20%20%20%20timeout%3A%201200%0A%20%20%20%20success%3A%20final_success%0A%20%20%20%20failure%3A%20final_failure%0A%20%20%20%20failure_timeout%3A%20final_failure_timeout%0A%20%20%20%20failure_retries%3A%20final_failure_retries%0A%20%20%20%20failure_exception%3A%20final_failure_exception%0A%20%20%20%20failure_guard%3A%20final_failure_guard" + }, + "name": "operational.restart", + "version": "1.0.0" +} |