X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fclient%2Freq%2Fpolicy%2FOperationalPolicyAttributesConstructorTest.java;h=a6a209a1e5e126f2ca41e8ebea46df5c4b6c1c57;hb=d1ff5b9dcfce8c3a69b80832ad2cbe4dbaf1d9cc;hp=c257ec3fd33abd7da26f64dbb3f3c65e61daa842;hpb=6651e17eab83a2231e1502f8083689f0ddc4751b;p=clamp.git 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 index c257ec3f..a6a209a1 100644 --- 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 @@ -26,6 +26,11 @@ 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 java.io.IOException; +import java.net.URLDecoder; +import java.util.Map; + import org.assertj.core.api.Assertions; import org.junit.Before; import org.junit.Test; @@ -43,121 +48,98 @@ 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); + .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 + .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")); + .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") - ); + 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()); + // 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")); + "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"); + 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); + 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) - ); + 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"); + .getResourceAsString("example/operational-policy/json-policy-chain.json"); JsonNode policyChainNode = new ObjectMapper().readTree(policyChainText); return new PolicyChain(policyChainNode); }