diff options
author | jhh <jorge.hernandez-herrero@att.com> | 2019-04-08 11:13:14 -0500 |
---|---|---|
committer | jhh <jorge.hernandez-herrero@att.com> | 2019-04-08 19:46:52 -0500 |
commit | 3f90dba2636b06bcb90b8f1e158b30886af574d5 (patch) | |
tree | a4244aecdd658984afb46bb1d2720e363092eec5 /feature-lifecycle/src/test/java | |
parent | 420fb3114baf6dc468b3babf5b45304647fcb04d (diff) |
Initial support for deploy
This is a first pass, some functionality is
missing (undeploy).
Compile error fix from changes in policy/models repo.
Change-Id: If448492ab665c135bace99d4d684d403e2a6be03
Issue-ID: POLICY-1624
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Diffstat (limited to 'feature-lifecycle/src/test/java')
4 files changed, 246 insertions, 38 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); + } +} |