diff options
Diffstat (limited to 'feature-lifecycle/src/test/java/org')
2 files changed, 317 insertions, 60 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 index 9b673dd5..1e9f1c66 100644 --- 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 @@ -69,12 +69,7 @@ public class ControllerSupport { ; } - 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()); - + ReleaseId coordinates = installArtifact(); Properties controllerProps = new Properties(); controllerProps.put(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME, name); @@ -87,6 +82,17 @@ public class ControllerSupport { } /** + * install artifact. + */ + public ReleaseId installArtifact() throws IOException { + return + 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()); + } + + /** * Destroy the echo controller. */ public void destroyController() { diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/server/restful/RestLifecycleManagerTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/server/restful/RestLifecycleManagerTest.java index 511fcc9b..2222399b 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/server/restful/RestLifecycleManagerTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/server/restful/RestLifecycleManagerTest.java @@ -18,43 +18,87 @@ package org.onap.policy.drools.server.restful; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Collections; import java.util.List; import java.util.Properties; import javax.ws.rs.client.Entity; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; +import org.apache.commons.lang3.StringUtils; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager; +import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories; import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.endpoints.http.client.HttpClient; import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; import org.onap.policy.common.endpoints.http.server.HttpServletServer; import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance; +import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; +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.common.utils.resources.ResourceUtils; +import org.onap.policy.drools.lifecycle.ControllerSupport; import org.onap.policy.drools.lifecycle.LifecycleFeature; +import org.onap.policy.drools.lifecycle.LifecycleFsm; import org.onap.policy.drools.persistence.SystemPersistenceConstants; +import org.onap.policy.drools.system.PolicyControllerConstants; +import org.onap.policy.drools.utils.logging.LoggerUtil; 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.ToscaServiceTemplate; /** * REST Lifecycle Manager Test. */ public class RestLifecycleManagerTest { + // Native Drools Policy + private static final String EXAMPLE_NATIVE_CONTROLLER_POLICY_NAME = "example.controller"; + private static final String EXAMPLE_NATIVE_CONTROLLER_POLICY_JSON = + "src/test/resources/tosca-policy-native-controller-example.json"; + + private static final String EXAMPLE_NATIVE_ARTIFACT_POLICY_NAME = "example.artifact"; + private static final String EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON = + "src/test/resources/tosca-policy-native-artifact-example.json"; + + private static final String OP_POLICY_NAME_VCPE = "operational.restart"; + private static final String VCPE_OPERATIONAL_DROOLS_POLICY_JSON = + "policies/vCPE.policy.operational.input.tosca.json"; + + private static StandardCoder coder = new StandardCoder(); + private static ControllerSupport controllerSupport = new ControllerSupport("lifecycle"); + + private LifecycleFsm fsm; + private HttpClient client; + /** * Set up. */ @Before public void setUp() throws Exception { + SystemPersistenceConstants.getManager().setConfigurationDir("target/test-classes"); + fsm = newFsmInstance(); + + LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO"); + LoggerUtil.setLevel("org.onap.policy.common.endpoints", "WARN"); + LoggerUtil.setLevel("org.onap.policy.drools", "INFO"); + HttpServletServerFactoryInstance.getServerFactory().destroy(); HttpClientFactoryInstance.getClientFactory().destroy(); - - SystemPersistenceConstants.getManager().setConfigurationDir("target/test-classes"); + PolicyControllerConstants.getFactory().destroy(); HttpClientFactoryInstance.getClientFactory().build( BusTopicParams.builder() @@ -70,8 +114,16 @@ public class RestLifecycleManagerTest { server.addServletClass("/*", RestLifecycleManager.class.getName()); server.waitedStart(5000L); - Assert.assertTrue(NetworkUtil.isTcpPortOpen("localhost", 8765, 5, 10000L)); + assertTrue(NetworkUtil.isTcpPortOpen("localhost", 8765, 5, 10000L)); + + controllerSupport.installArtifact(); + Properties noopTopicProperties = new Properties(); + noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS, "DCAE_TOPIC"); + noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, "APPC-CL"); + TopicEndpointManager.getManager().addTopics(noopTopicProperties); + + client = HttpClientFactoryInstance.getClientFactory().get("lifecycle"); } /** @@ -79,88 +131,287 @@ public class RestLifecycleManagerTest { */ @After public void tearDown() { - HttpServletServerFactoryInstance.getServerFactory().destroy(); + fsm.shutdown(); + + NoopTopicFactories.getSourceFactory().destroy(); + NoopTopicFactories.getSinkFactory().destroy(); + HttpClientFactoryInstance.getClientFactory().destroy(); + HttpServletServerFactoryInstance.getServerFactory().destroy(); + + PolicyControllerConstants.getFactory().destroy(); + SystemPersistenceConstants.getManager().setConfigurationDir(null); } @Test - public void testFsm() { + public void testMultiPolicyFlow() throws IOException, CoderException { + /* group assignments */ - HttpClient client = HttpClientFactoryInstance.getClientFactory().get("lifecycle"); - Response response; + group(); + subgroup(); - /* group */ + /* other resources */ - response = client.put("group/GG", Entity.json(""), Collections.emptyMap()); - assertEquals(Status.OK.getStatusCode(), response.getStatus()); - assertEquals("GG", HttpClient.getBody(response, String.class)); + properties(); + topics(); - response = HttpClientFactoryInstance.getClientFactory().get("lifecycle").get("group"); - assertEquals(Status.OK.getStatusCode(), response.getStatus()); - assertEquals(LifecycleFeature.fsm.getGroup(), HttpClient.getBody(response, String.class)); + /* status interval */ - /* subgroup */ + status(); - response = client.put("subgroup/YY", Entity.json(""), Collections.emptyMap()); - assertEquals(Status.OK.getStatusCode(), response.getStatus()); - assertEquals("YY", HttpClient.getBody(response, String.class)); + /* start up configuration */ - response = client.get("subgroup"); - assertEquals(Status.OK.getStatusCode(), response.getStatus()); - assertEquals(LifecycleFeature.fsm.getSubgroup(), HttpClient.getBody(response, String.class)); + resourceLists("policyTypes", 2); + get("policyTypes/onap.policies.native.drools.Artifact/1.0.0", Status.OK.getStatusCode()); + get("policyTypes/onap.policies.native.drools.Controller/1.0.0", Status.OK.getStatusCode()); + get("policyTypes/onap.policies.controlloop.Operational/1.0.0", Status.NOT_FOUND.getStatusCode()); + get("policyTypes/onap.policies.controlloop.operational.common.Drools/1.0.0", Status.NOT_FOUND.getStatusCode()); - /* properties */ + resourceLists("policies", 0); + get("policies/example.controller/1.0.0", Status.NOT_FOUND.getStatusCode()); + get("policies/example.artifact/1.0.0", Status.NOT_FOUND.getStatusCode()); - response = client.get("properties"); - assertEquals(Status.OK.getStatusCode(), response.getStatus()); - assertEquals(LifecycleFeature.fsm.getProperties(), HttpClient.getBody(response, Properties.class)); + /* start lifecycle */ - /* state (disallowed state change as has not been started) */ + assertTrue(fsm.start()); - response = client.put("state/PASSIVE", Entity.json(""), Collections.emptyMap()); - assertEquals(Status.OK.getStatusCode(), response.getStatus()); - assertEquals(Boolean.FALSE, HttpClient.getBody(response, Boolean.class)); + booleanPut("state/ACTIVE", "", Status.OK.getStatusCode(), Boolean.TRUE); + assertEquals(PdpState.ACTIVE, + HttpClient.getBody(get("state", Status.OK.getStatusCode()), PdpState.class)); - response = client.get("state"); - assertEquals(Status.OK.getStatusCode(), response.getStatus()); - assertEquals(PdpState.TERMINATED, HttpClient.getBody(response, PdpState.class)); + /* add native controller policy */ - /* topics */ + ToscaPolicy nativeControllerPolicy = + getPolicyFromFile(EXAMPLE_NATIVE_CONTROLLER_POLICY_JSON, EXAMPLE_NATIVE_CONTROLLER_POLICY_NAME); + booleanPost("policies", toString(nativeControllerPolicy), Status.OK.getStatusCode(), Boolean.TRUE); - assertEquals(Status.OK.getStatusCode(), client.get("topic/source").getStatus()); - assertEquals(Status.OK.getStatusCode(), client.get("topic/sink").getStatus()); + assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").isAlive()); + assertFalse(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isBrained()); + assertFalse(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isAlive()); - /* status interval */ + get("policyTypes/onap.policies.controlloop.Operational/1.0.0", Status.NOT_FOUND.getStatusCode()); + get("policyTypes/onap.policies.controlloop.operational.common.Drools/1.0.0", Status.NOT_FOUND.getStatusCode()); + + resourceLists("policies", 1); + get("policies/example.controller/1.0.0", Status.OK.getStatusCode()); + + /* add native artifact policy */ + + ToscaPolicy nativeArtifactPolicy = + getPolicyFromFile(EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON, EXAMPLE_NATIVE_ARTIFACT_POLICY_NAME); + booleanPost("policies", toString(nativeArtifactPolicy), Status.OK.getStatusCode(), Boolean.TRUE); + + assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").isAlive()); + assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isBrained()); + assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isAlive()); + + /* verify new supported operational policy types */ + + resourceLists("policyTypes", 5); + get("policyTypes/onap.policies.native.drools.Artifact/1.0.0", Status.OK.getStatusCode()); + get("policyTypes/onap.policies.native.drools.Controller/1.0.0", Status.OK.getStatusCode()); + get("policyTypes/onap.policies.controlloop.Operational/1.0.0", Status.OK.getStatusCode()); + get("policyTypes/onap.policies.controlloop.operational.common.Drools/1.0.0", Status.OK.getStatusCode()); + get("policyTypes/onap.policies.type1.type2/1.0.0", Status.OK.getStatusCode()); + + /* verify controller and artifact policies */ + + resourceLists("policies", 2); + get("policies/example.controller/1.0.0", Status.OK.getStatusCode()); + get("policies/example.artifact/1.0.0", Status.OK.getStatusCode()); + + /* add tosca compliant operational policy */ + + ToscaPolicy opPolicy = getExamplesPolicy(VCPE_OPERATIONAL_DROOLS_POLICY_JSON, OP_POLICY_NAME_VCPE); + opPolicy.getProperties().put("controllerName", "lifecycle"); + if (StringUtils.isBlank(opPolicy.getName())) { + opPolicy.setName(opPolicy.getMetadata().get("policy-id")); + } + + booleanPost("policies", toString(opPolicy), Status.OK.getStatusCode(), Boolean.TRUE); + assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").isAlive()); + assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isBrained()); + assertEquals(1, + PolicyControllerConstants + .getFactory().get("lifecycle").getDrools().facts("junits", ToscaPolicy.class) .size()); + + resourceLists("policies", 3); + get("policies/" + opPolicy.getName() + "/" + opPolicy.getVersion(), Status.OK.getStatusCode()); + get("policies/example.controller/1.0.0", Status.OK.getStatusCode()); + get("policies/example.artifact/1.0.0", Status.OK.getStatusCode()); + + booleanDelete("policies/" + opPolicy.getName() + "/" + opPolicy.getVersion(), + Status.OK.getStatusCode(), Boolean.TRUE); + assertEquals(0, + PolicyControllerConstants + .getFactory().get("lifecycle").getDrools().facts("junits", ToscaPolicy.class) .size()); - response = client.put("status/interval/1000", Entity.json(""), Collections.emptyMap()); + resourceLists("policies", 2); + get("policies/" + opPolicy.getName() + "/" + opPolicy.getVersion(), Status.NOT_FOUND.getStatusCode()); + get("policies/example.controller/1.0.0", Status.OK.getStatusCode()); + get("policies/example.artifact/1.0.0", Status.OK.getStatusCode()); + + /* individual deploy/undeploy operations */ + + resourceLists("policies/operations", 2); + + booleanPost("policies/operations/deployment", toString(opPolicy), Status.OK.getStatusCode(), Boolean.TRUE); + assertEquals(1, + PolicyControllerConstants + .getFactory().get("lifecycle").getDrools().facts("junits", ToscaPolicy.class) .size()); + + resourceLists("policies", 2); + get("policies/" + opPolicy.getName() + "/" + opPolicy.getVersion(), Status.NOT_FOUND.getStatusCode()); + get("policies/example.controller/1.0.0", Status.OK.getStatusCode()); + get("policies/example.artifact/1.0.0", Status.OK.getStatusCode()); + + booleanPost( + "policies/operations/undeployment", toString(opPolicy), Status.OK.getStatusCode(), Boolean.TRUE); + assertEquals(0, + PolicyControllerConstants + .getFactory().get("lifecycle").getDrools().facts("junits", ToscaPolicy.class) .size()); + + resourceLists("policies", 2); + get("policies/" + opPolicy.getName() + "/" + opPolicy.getVersion(), Status.NOT_FOUND.getStatusCode()); + get("policies/example.controller/1.0.0", Status.OK.getStatusCode()); + get("policies/example.artifact/1.0.0", Status.OK.getStatusCode()); + + /* delete native artifact policy */ + + booleanDelete("policies/example.artifact/1.0.0", Status.OK.getStatusCode(), Boolean.TRUE); + assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").isAlive()); + assertFalse(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isBrained()); + + resourceLists("policyTypes", 2); + get("policyTypes/onap.policies.native.drools.Artifact/1.0.0", Status.OK.getStatusCode()); + get("policyTypes/onap.policies.native.drools.Controller/1.0.0", Status.OK.getStatusCode()); + get("policyTypes/onap.policies.controlloop.Operational/1.0.0", Status.NOT_FOUND.getStatusCode()); + get("policyTypes/onap.policies.controlloop.operational.common.Drools/1.0.0", Status.NOT_FOUND.getStatusCode()); + get("policyTypes/onap.policies.type1.type2/1.0.0", Status.NOT_FOUND.getStatusCode()); + + resourceLists("policies", 1); + get("policies/" + opPolicy.getName() + "/" + opPolicy.getVersion(), Status.NOT_FOUND.getStatusCode()); + get("policies/example.artifact/1.0.0", Status.NOT_FOUND.getStatusCode()); + get("policies/example.controller/1.0.0", Status.OK.getStatusCode()); + + /* delete native controller policy */ + + booleanDelete("policies/example.controller/1.0.0", Status.OK.getStatusCode(), Boolean.TRUE); + + resourceLists("policyTypes", 2); + get("policyTypes/onap.policies.native.drools.Artifact/1.0.0", Status.OK.getStatusCode()); + get("policyTypes/onap.policies.native.drools.Controller/1.0.0", Status.OK.getStatusCode()); + get("policyTypes/onap.policies.controlloop.Operational/1.0.0", Status.NOT_FOUND.getStatusCode()); + get("policyTypes/onap.policies.controlloop.operational.common.Drools/1.0.0", Status.NOT_FOUND.getStatusCode()); + get("policyTypes/onap.policies.type1.type2/1.0.0", Status.NOT_FOUND.getStatusCode()); + + resourceLists("policies", 0); + get("policies/" + opPolicy.getName() + "/" + opPolicy.getVersion(), Status.NOT_FOUND.getStatusCode()); + get("policies/example.artifact/1.0.0", Status.NOT_FOUND.getStatusCode()); + get("policies/example.controller/1.0.0", Status.NOT_FOUND.getStatusCode()); + + assertThatIllegalArgumentException().isThrownBy(() -> PolicyControllerConstants.getFactory().get("lifecycle")); + } + + private Response get(String contextPath, int statusCode) { + Response response = client.get(contextPath); + assertEquals(statusCode, response.getStatus()); + return response; + } + + private void booleanResponse(Response response, int statusCode, Boolean bool) { + assertEquals(statusCode, response.getStatus()); + assertEquals(bool, HttpClient.getBody(response, Boolean.class)); + } + + private void booleanPut(String contextPath, String body, int statusCode, Boolean bool) { + Response response = client.put(contextPath, Entity.json(body), Collections.emptyMap()); + booleanResponse(response, statusCode, bool); + } + + private void booleanPost(String contextPath, String body, int statusCode, Boolean bool) { + Response response = client.post(contextPath, Entity.json(body), Collections.emptyMap()); + booleanResponse(response, statusCode, bool); + } + + private void booleanDelete(String contextPath, int statusCode, Boolean bool) { + Response response = client.delete(contextPath, Collections.emptyMap()); + booleanResponse(response, statusCode, bool); + } + + private void resourceLists(String resource, int size) { + Response response = client.get(resource); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + assertEquals(size, HttpClient.getBody(response, List.class).size()); + } + + private void status() { + Response response = client.put("status/interval/240", Entity.json(""), Collections.emptyMap()); assertEquals(Status.OK.getStatusCode(), response.getStatus()); - assertEquals(Long.valueOf(1000L), HttpClient.getBody(response, Long.class)); + assertEquals(Long.valueOf(240L), HttpClient.getBody(response, Long.class)); response = client.get("status/interval"); assertEquals(Status.OK.getStatusCode(), response.getStatus()); - assertEquals(Long.valueOf(1000L), HttpClient.getBody(response, Long.class)); + assertEquals(Long.valueOf(240L), HttpClient.getBody(response, Long.class)); + } - /* policy types */ + private void topics() { + assertEquals(Status.OK.getStatusCode(), client.get("topic/source").getStatus()); + assertEquals(Status.OK.getStatusCode(), client.get("topic/sink").getStatus()); + } - response = client.get("policyTypes"); + private void properties() { + Response response = client.get("properties"); assertEquals(Status.OK.getStatusCode(), response.getStatus()); - assertEquals(2, HttpClient.getBody(response, List.class).size()); + assertEquals(fsm.getProperties(), HttpClient.getBody(response, Properties.class)); + } - response = client.get("policyTypes/onap.policies.native.drools.Artifact/1.0.0"); + private void subgroup() { + Response response = client.put("subgroup/YY", Entity.json(""), Collections.emptyMap()); assertEquals(Status.OK.getStatusCode(), response.getStatus()); - assertNotNull(HttpClient.getBody(response, String.class)); + assertEquals("YY", HttpClient.getBody(response, String.class)); - response = client.get("policyTypes/onap.policies.native.drools.Controller/1.0.0"); + response = client.get("subgroup"); assertEquals(Status.OK.getStatusCode(), response.getStatus()); - assertNotNull(HttpClient.getBody(response, String.class)); + assertEquals("YY", HttpClient.getBody(response, String.class)); + } - /* policies */ + private void group() { + Response response = client.put("group/GG", Entity.json(""), Collections.emptyMap()); + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + assertEquals("GG", HttpClient.getBody(response, String.class)); - response = client.get("policies"); + response = HttpClientFactoryInstance.getClientFactory().get("lifecycle").get("group"); assertEquals(Status.OK.getStatusCode(), response.getStatus()); - assertEquals(0, HttpClient.getBody(response, List.class).size()); + assertEquals("GG", HttpClient.getBody(response, String.class)); + } + + private LifecycleFsm newFsmInstance() throws NoSuchFieldException, IllegalAccessException { + Field fsmField = LifecycleFeature.class.getDeclaredField("fsm"); + fsmField.setAccessible(true); + + Field modifiers = Field.class.getDeclaredField("modifiers"); + modifiers.setAccessible(true); + modifiers.setInt(fsmField, fsmField.getModifiers() & ~Modifier.FINAL ); + + LifecycleFsm fsm = new LifecycleFsm(); + fsmField.set(null, fsm); + return fsm; + } + + protected ToscaPolicy getPolicyFromFile(String filePath, String policyName) throws CoderException, IOException { + String policyJson = Files.readString(Paths.get(filePath)); + ToscaServiceTemplate serviceTemplate = coder.decode(policyJson, ToscaServiceTemplate.class); + return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName); + } + + protected String toString(ToscaPolicy policy) throws CoderException { + return coder.encode(policy); + } - response = client.get("policies/onap.policies.controlloop.Operational"); - assertEquals(Status.NOT_FOUND.getStatusCode(), response.getStatus()); + private ToscaPolicy getExamplesPolicy(String resourcePath, String policyName) throws CoderException { + String policyJson = ResourceUtils.getResourceAsString(resourcePath); + ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class); + return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName); } } |