From 0636411a4915a462d2cf6698ecb9c68c478f6ef5 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Thu, 16 Aug 2018 16:49:24 +0200 Subject: Fix for sonar issue Fix for some sonars. Increase code coverage. Issue-ID: CLAMP-211 Change-Id: If1b5169ff832fc94886b178226570dc0559fe3c2 Signed-off-by: Gabriel --- ...OperationalPolicyAttributesConstructorTest.java | 164 +++++++++++++++++++++ .../req/policy/OperationalPolicyReqTest.java | 69 --------- .../policy/OperationalPolicyYamlFormatterTest.java | 71 +++++++++ .../clamp/clds/it/OperationPolicyReqItCase.java | 8 +- .../org/onap/clamp/clds/it/PolicyClientItCase.java | 12 +- .../operational-policy/json-policy-chain.json | 63 ++++++++ 6 files changed, 312 insertions(+), 75 deletions(-) create mode 100644 src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyAttributesConstructorTest.java delete mode 100644 src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReqTest.java create mode 100644 src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyYamlFormatterTest.java create mode 100644 src/test/resources/example/operational-policy/json-policy-chain.json (limited to 'src/test') diff --git a/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyAttributesConstructorTest.java b/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyAttributesConstructorTest.java new file mode 100644 index 000000000..c257ec3fd --- /dev/null +++ b/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyAttributesConstructorTest.java @@ -0,0 +1,164 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2018 Nokia 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.clamp.clds.client.req.policy; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableMap; +import org.assertj.core.api.Assertions; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.onap.clamp.clds.config.ClampProperties; +import org.onap.clamp.clds.model.properties.ModelProperties; +import org.onap.clamp.clds.model.properties.PolicyChain; +import org.onap.clamp.clds.util.ResourceFileUtil; +import org.onap.policy.api.AttributeType; +import org.onap.policy.controlloop.policy.ControlLoopPolicy; +import org.onap.policy.controlloop.policy.Policy; +import org.onap.policy.controlloop.policy.Target; +import org.onap.policy.controlloop.policy.TargetType; +import org.onap.policy.controlloop.policy.builder.BuilderException; +import org.yaml.snakeyaml.Yaml; + +import java.io.IOException; +import java.net.URLDecoder; +import java.util.Map; + +public class OperationalPolicyAttributesConstructorTest { + + private static final String CONTROL_NAME = "ClosedLoop-d4629aee-970f-11e8-86c9-02552dda865e"; + private ModelProperties modelProperties; + private PolicyChain policyChain; + + private OperationalPolicyYamlFormatter operationalPolicyYamlFormatter = new OperationalPolicyYamlFormatter(); + private OperationalPolicyAttributesConstructor operationalPolicyAttributesConstructor = + new OperationalPolicyAttributesConstructor(operationalPolicyYamlFormatter); + + @Before + public void setUp() throws Exception { + String modelProp = ResourceFileUtil + .getResourceAsString("example/model-properties/policy/modelBpmnProperties.json"); + modelProperties = new ModelProperties("CLAMPDemoVFW_v1_0_3af8daec-6f10-4027-a3540", + CONTROL_NAME, "PUT", false, "{}", modelProp); + policyChain = readPolicyChainFromResources(); + } + + + @Test + public void shouldFormatRequestAttributes() throws IOException, BuilderException { + // given + ClampProperties mockClampProperties = createMockClampProperties(ImmutableMap.builder() + .put("op.templateName", "ClosedLoopControlName") + .put("op.notificationTopic", "POLICY-CL-MGT") + .put("op.controller", "amsterdam") + .put("op.recipeTopic", "APPC") + .build()); + + //when + Map> requestAttributes + = operationalPolicyAttributesConstructor.formatAttributes(mockClampProperties, modelProperties, + "789875c1-e788-432f-9a76-eac8ed889734", policyChain); + //then + Assertions.assertThat(requestAttributes).containsKeys(AttributeType.MATCHING, AttributeType.RULE); + Assertions.assertThat(requestAttributes.get(AttributeType.MATCHING)) + .contains(Assertions.entry(OperationalPolicyAttributesConstructor.CONTROLLER, "amsterdam")); + + Map ruleParameters = requestAttributes.get(AttributeType.RULE); + Assertions.assertThat(ruleParameters).containsExactly( + Assertions.entry(OperationalPolicyAttributesConstructor.MAX_RETRIES, "3"), + Assertions.entry(OperationalPolicyAttributesConstructor.TEMPLATE_NAME, "ClosedLoopControlName"), + Assertions.entry(OperationalPolicyAttributesConstructor.NOTIFICATION_TOPIC, "POLICY-CL-MGT"), + Assertions.entry(OperationalPolicyAttributesConstructor.RECIPE_TOPIC, "APPC"), + Assertions.entry(OperationalPolicyAttributesConstructor.RECIPE, "healthCheck"), + Assertions.entry(OperationalPolicyAttributesConstructor.RESOURCE_ID, + "cdb69724-57d5-4a22-b96c-4c345150fd0e"), + Assertions.entry(OperationalPolicyAttributesConstructor.RETRY_TIME_LIMIT, "180"), + Assertions.entry(OperationalPolicyAttributesConstructor.CLOSED_LOOP_CONTROL_NAME, CONTROL_NAME + "_1") + ); + } + + @Test + public void shouldFormatRequestAttributesWithProperControlLoopYaml() throws IOException, BuilderException { + //given + ClampProperties mockClampProperties = createMockClampProperties(ImmutableMap.builder() + .put("op.templateName", "ClosedLoopControlName") + .put("op.operationTopic", "APPP-CL") + .put("op.notificationTopic", "POLICY-CL-MGT") + .put("op.controller", "amsterdam") + .put("op.recipeTopic", "APPC") + .build()); + + Policy expectedPolicy = new Policy("6f76ad0b-ea9d-4a92-8d7d-6a6367ce2c77", "healthCheck Policy", + "healthCheck Policy - the trigger (no parent) policy - created by CLDS", "APPC", + null, new Target(TargetType.VM, "cdb69724-57d5-4a22-b96c-4c345150fd0e"), + "healthCheck", 3, 180); + + //when + Map> requestAttributes = operationalPolicyAttributesConstructor + .formatAttributes(mockClampProperties, modelProperties, + "789875c1-e788-432f-9a76-eac8ed889734", policyChain); + + //then + Assertions.assertThat(requestAttributes) + .containsKeys(AttributeType.MATCHING, AttributeType.RULE); + Assertions.assertThat(requestAttributes + .get(AttributeType.MATCHING)) + .contains(Assertions.entry("controller", "amsterdam")); + + Map ruleParameters = requestAttributes.get(AttributeType.RULE); + Assertions.assertThat(ruleParameters).contains( + Assertions.entry(OperationalPolicyAttributesConstructor.OPERATION_TOPIC, "APPP-CL"), + Assertions.entry(OperationalPolicyAttributesConstructor.TEMPLATE_NAME, "ClosedLoopControlName"), + Assertions.entry(OperationalPolicyAttributesConstructor.NOTIFICATION_TOPIC, "POLICY-CL-MGT"), + Assertions.entry(OperationalPolicyAttributesConstructor.CLOSED_LOOP_CONTROL_NAME, CONTROL_NAME + "_1") + ); + + String controlLoopYaml = URLDecoder.decode( + ruleParameters.get(OperationalPolicyAttributesConstructor.CONTROL_LOOP_YAML), "UTF-8"); + ControlLoopPolicy controlLoopPolicy = new Yaml().load(controlLoopYaml); + + Assertions.assertThat(controlLoopPolicy.getControlLoop().getControlLoopName()).isEqualTo(CONTROL_NAME); + Assertions.assertThat(controlLoopPolicy.getPolicies()) + .usingElementComparatorIgnoringFields("id") + .containsExactly(expectedPolicy); + } + + + private ClampProperties createMockClampProperties(ImmutableMap propertiesMap) { + ClampProperties props = Mockito.mock(ClampProperties.class); + propertiesMap.forEach((property, value) -> + Mockito.when(props.getStringValue(Matchers.matches(property), Matchers.any())).thenReturn(value) + ); + return props; + } + + private PolicyChain readPolicyChainFromResources() throws IOException { + String policyChainText = ResourceFileUtil + .getResourceAsString("example/operational-policy/json-policy-chain.json"); + JsonNode policyChainNode = new ObjectMapper().readTree(policyChainText); + return new PolicyChain(policyChainNode); + } +} diff --git a/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReqTest.java b/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReqTest.java deleted file mode 100644 index d5e3069b2..000000000 --- a/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReqTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP CLAMP - * ================================================================================ - * Copyright (C) 2017 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.clamp.clds.client.req.policy; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.lang.reflect.InvocationTargetException; -import java.util.Arrays; -import java.util.List; -import org.junit.Test; -import org.onap.policy.controlloop.policy.PolicyResult; -import org.onap.policy.sdc.Resource; -import org.onap.policy.sdc.ResourceType; - -public class OperationalPolicyReqTest { - - @Test - public void shouldConvertGivenStringsToResourceObjects() - throws NoSuchMethodException, SecurityException, IllegalAccessException, - IllegalArgumentException, InvocationTargetException { - - //given - List stringList = Arrays.asList("test1", "test2", "test3", "test4"); - - //when - Resource[] resources = OperationalPolicyReq.convertToResource(stringList, ResourceType.VF); - - //then - assertThat(resources).extracting(Resource::getResourceName) - .containsExactly("test1", "test2", "test3", "test4"); - } - - @Test - public void shouldConvertGivenStringsToPolicyResults() - throws NoSuchMethodException, SecurityException, IllegalAccessException, - IllegalArgumentException, InvocationTargetException { - //given - List stringList = Arrays.asList("FAILURE", "SUCCESS", "FAILURE_GUARD", "FAILURE_TIMEOUT"); - - //when - PolicyResult[] policyResults = OperationalPolicyReq.convertToPolicyResult(stringList); - - //then - assertThat(policyResults) - .containsExactly(PolicyResult.FAILURE, PolicyResult.SUCCESS, - PolicyResult.FAILURE_GUARD, PolicyResult.FAILURE_TIMEOUT); - } -} diff --git a/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyYamlFormatterTest.java b/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyYamlFormatterTest.java new file mode 100644 index 000000000..046d7394d --- /dev/null +++ b/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyYamlFormatterTest.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2017 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============================================ + * Modifications copyright (c) 2018 Nokia + * =================================================================== + * + */ + +package org.onap.clamp.clds.client.req.policy; + +import java.util.Arrays; +import java.util.List; + +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.sdc.Resource; +import org.onap.policy.sdc.ResourceType; + +public class OperationalPolicyYamlFormatterTest { + + private OperationalPolicyYamlFormatter policyYamlFormatter = new OperationalPolicyYamlFormatter(); + + @Test + public void shouldConvertGivenStringsToResourceObjects() + throws SecurityException, + IllegalArgumentException { + + //given + List stringList = Arrays.asList("test1", "test2", "test3", "test4"); + + //when + Resource[] resources = policyYamlFormatter.convertToResources(stringList, ResourceType.VF); + + //then + Assertions.assertThat(resources).extracting(Resource::getResourceName) + .containsExactly("test1", "test2", "test3", "test4"); + } + + @Test + public void shouldConvertGivenStringsToPolicyResults() + throws SecurityException, + IllegalArgumentException { + //given + List stringList = Arrays.asList("FAILURE", "SUCCESS", "FAILURE_GUARD", "FAILURE_TIMEOUT"); + + //when + PolicyResult[] policyResults = policyYamlFormatter.convertToPolicyResults(stringList); + + //then + Assertions.assertThat(policyResults) + .containsExactly(PolicyResult.FAILURE, PolicyResult.SUCCESS, + PolicyResult.FAILURE_GUARD, PolicyResult.FAILURE_TIMEOUT); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/clamp/clds/it/OperationPolicyReqItCase.java b/src/test/java/org/onap/clamp/clds/it/OperationPolicyReqItCase.java index 69dad53ec..ad58ea1e4 100644 --- a/src/test/java/org/onap/clamp/clds/it/OperationPolicyReqItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/OperationPolicyReqItCase.java @@ -17,6 +17,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END============================================ + * Modifications copyright (c) 2018 Nokia * =================================================================== * */ @@ -35,7 +36,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; -import org.onap.clamp.clds.client.req.policy.OperationalPolicyReq; +import org.onap.clamp.clds.client.req.policy.OperationalPolicyAttributesConstructor; import org.onap.clamp.clds.config.ClampProperties; import org.onap.clamp.clds.model.CldsEvent; import org.onap.clamp.clds.model.properties.ModelProperties; @@ -55,6 +56,9 @@ public class OperationPolicyReqItCase { @Autowired private ClampProperties refProp; + @Autowired + private OperationalPolicyAttributesConstructor operationalPolicyAttributesConstructor; + @Test public void formatAttributesTest() throws IOException, BuilderException { String modelBpmnProp = ResourceFileUtil @@ -65,7 +69,7 @@ public class OperationPolicyReqItCase { List>> attributes = new ArrayList<>(); if (modelProperties.getType(Policy.class).isFound()) { for (PolicyChain policyChain : modelProperties.getType(Policy.class).getPolicyChains()) { - attributes.add(OperationalPolicyReq.formatAttributes(refProp, modelProperties, + attributes.add(operationalPolicyAttributesConstructor.formatAttributes(refProp, modelProperties, modelProperties.getType(Policy.class).getId(), policyChain)); } } diff --git a/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java b/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java index c6fc09dbb..2400d4ab6 100644 --- a/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java @@ -17,6 +17,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END============================================ + * Modifications copyright (c) 2018 Nokia * =================================================================== * */ @@ -35,7 +36,7 @@ import java.util.concurrent.TimeUnit; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.onap.clamp.clds.client.req.policy.OperationalPolicyReq; +import org.onap.clamp.clds.client.req.policy.OperationalPolicyAttributesConstructor; import org.onap.clamp.clds.client.req.policy.PolicyClient; import org.onap.clamp.clds.client.req.tca.TcaRequestFormatter; import org.onap.clamp.clds.config.ClampProperties; @@ -64,7 +65,10 @@ public class PolicyClientItCase { @Autowired private ClampProperties refProp; @Autowired - protected PolicyClient policyClient; + private PolicyClient policyClient; + @Autowired + private OperationalPolicyAttributesConstructor operationalPolicyAttributesConstructor; + String modelProp; String modelBpmnProp; String modelName; @@ -87,8 +91,8 @@ public class PolicyClientItCase { if (policy.isFound()) { for (PolicyChain policyChain : policy.getPolicyChains()) { String operationalPolicyRequestUuid = UUID.randomUUID().toString(); - Map> attributes = OperationalPolicyReq.formatAttributes(refProp, - prop, policy.getId(), policyChain); + Map> attributes = operationalPolicyAttributesConstructor + .formatAttributes(refProp, prop, policy.getId(), policyChain); policyClient.sendBrmsPolicy(attributes, prop, operationalPolicyRequestUuid); } } diff --git a/src/test/resources/example/operational-policy/json-policy-chain.json b/src/test/resources/example/operational-policy/json-policy-chain.json new file mode 100644 index 000000000..037b4d656 --- /dev/null +++ b/src/test/resources/example/operational-policy/json-policy-chain.json @@ -0,0 +1,63 @@ +[ + { + "name": "pname", + "value": "healthCheck Policy" + }, + { + "name": "pid", + "value": "1" + }, + { + "name": "timeout", + "value": "180" + }, + { + "policyConfigurations": [ + [ + { + "name": "recipe", + "value": [ + "healthCheck" + ] + }, + { + "name": "maxRetries", + "value": [ + "3" + ] + }, + { + "name": "retryTimeLimit", + "value": [ + "180" + ] + }, + { + "name": "_id", + "value": [ + "789875c1-e788-432f-9a76-eac8ed889734" + ] + }, + { + "name": "parentPolicy", + "value": [ + "" + ] + }, + { + "name": "actor", + "value": [ + "APPC" + ] + }, + { + "name": "targetResourceId", + "value": [ + "cdb69724-57d5-4a22-b96c-4c345150fd0e" + ] + } + + ] + ] + } +] \ No newline at end of file -- cgit 1.2.3-korg