2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.client.req.policy;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.google.common.collect.ImmutableMap;
30 import java.io.IOException;
31 import java.net.URLDecoder;
34 import org.assertj.core.api.Assertions;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Matchers;
38 import org.mockito.Mockito;
39 import org.onap.clamp.clds.config.ClampProperties;
40 import org.onap.clamp.clds.model.properties.ModelProperties;
41 import org.onap.clamp.clds.model.properties.PolicyChain;
42 import org.onap.clamp.clds.util.ResourceFileUtil;
43 import org.onap.policy.api.AttributeType;
44 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
45 import org.onap.policy.controlloop.policy.Policy;
46 import org.onap.policy.controlloop.policy.Target;
47 import org.onap.policy.controlloop.policy.TargetType;
48 import org.onap.policy.controlloop.policy.builder.BuilderException;
49 import org.yaml.snakeyaml.Yaml;
51 public class OperationalPolicyAttributesConstructorTest {
53 private static final String CONTROL_NAME = "ClosedLoop-d4629aee-970f-11e8-86c9-02552dda865e";
54 private ModelProperties modelProperties;
55 private PolicyChain policyChain;
58 public void setUp() throws Exception {
59 String modelProp = ResourceFileUtil
60 .getResourceAsString("example/model-properties/policy/modelBpmnProperties.json");
61 modelProperties = new ModelProperties("CLAMPDemoVFW_v1_0_3af8daec-6f10-4027-a3540", CONTROL_NAME, "PUT", false,
63 policyChain = readPolicyChainFromResources();
67 public void shouldFormatRequestAttributes() throws IOException, BuilderException {
69 ClampProperties mockClampProperties = createMockClampProperties(ImmutableMap.<String, String>builder()
70 .put("op.templateName", "ClosedLoopControlName").put("op.notificationTopic", "POLICY-CL-MGT")
71 .put("op.controller", "amsterdam").put("op.recipeTopic", "APPC").build());
74 Map<AttributeType, Map<String, String>> requestAttributes = OperationalPolicyAttributesConstructor
75 .formatAttributes(mockClampProperties, modelProperties, "789875c1-e788-432f-9a76-eac8ed889734",
78 Assertions.assertThat(requestAttributes).containsKeys(AttributeType.MATCHING, AttributeType.RULE);
79 Assertions.assertThat(requestAttributes.get(AttributeType.MATCHING))
80 .contains(Assertions.entry(OperationalPolicyAttributesConstructor.CONTROLLER, "amsterdam"));
82 Map<String, String> ruleParameters = requestAttributes.get(AttributeType.RULE);
83 Assertions.assertThat(ruleParameters).containsExactly(
84 Assertions.entry(OperationalPolicyAttributesConstructor.MAX_RETRIES, "3"),
85 Assertions.entry(OperationalPolicyAttributesConstructor.TEMPLATE_NAME, "ClosedLoopControlName"),
86 Assertions.entry(OperationalPolicyAttributesConstructor.NOTIFICATION_TOPIC, "POLICY-CL-MGT"),
87 Assertions.entry(OperationalPolicyAttributesConstructor.RECIPE_TOPIC, "APPC"),
88 Assertions.entry(OperationalPolicyAttributesConstructor.RECIPE, "healthCheck"),
89 Assertions.entry(OperationalPolicyAttributesConstructor.RESOURCE_ID,
90 "cdb69724-57d5-4a22-b96c-4c345150fd0e"),
91 Assertions.entry(OperationalPolicyAttributesConstructor.RETRY_TIME_LIMIT, "180"),
92 Assertions.entry(OperationalPolicyAttributesConstructor.CLOSED_LOOP_CONTROL_NAME, CONTROL_NAME + "_1"));
96 public void shouldFormatRequestAttributesWithProperControlLoopYaml() throws IOException, BuilderException {
98 ClampProperties mockClampProperties = createMockClampProperties(
99 ImmutableMap.<String, String>builder().put("op.templateName", "ClosedLoopControlName")
100 .put("op.operationTopic", "APPP-CL").put("op.notificationTopic", "POLICY-CL-MGT")
101 .put("op.controller", "amsterdam").put("op.recipeTopic", "APPC").build());
103 Policy expectedPolicy = new Policy("6f76ad0b-ea9d-4a92-8d7d-6a6367ce2c77", "healthCheck Policy",
104 "healthCheck Policy - the trigger (no parent) policy - created by CLDS", "APPC", null,
105 new Target(TargetType.VM, "cdb69724-57d5-4a22-b96c-4c345150fd0e"), "healthCheck", 3, 180);
108 Map<AttributeType, Map<String, String>> requestAttributes = OperationalPolicyAttributesConstructor
109 .formatAttributes(mockClampProperties, modelProperties, "789875c1-e788-432f-9a76-eac8ed889734",
113 Assertions.assertThat(requestAttributes).containsKeys(AttributeType.MATCHING, AttributeType.RULE);
114 Assertions.assertThat(requestAttributes.get(AttributeType.MATCHING))
115 .contains(Assertions.entry("controller", "amsterdam"));
117 Map<String, String> ruleParameters = requestAttributes.get(AttributeType.RULE);
118 Assertions.assertThat(ruleParameters).contains(
119 Assertions.entry(OperationalPolicyAttributesConstructor.OPERATION_TOPIC, "APPP-CL"),
120 Assertions.entry(OperationalPolicyAttributesConstructor.TEMPLATE_NAME, "ClosedLoopControlName"),
121 Assertions.entry(OperationalPolicyAttributesConstructor.NOTIFICATION_TOPIC, "POLICY-CL-MGT"),
122 Assertions.entry(OperationalPolicyAttributesConstructor.CLOSED_LOOP_CONTROL_NAME, CONTROL_NAME + "_1"));
124 String controlLoopYaml = URLDecoder
125 .decode(ruleParameters.get(OperationalPolicyAttributesConstructor.CONTROL_LOOP_YAML), "UTF-8");
126 ControlLoopPolicy controlLoopPolicy = new Yaml().load(controlLoopYaml);
128 Assertions.assertThat(controlLoopPolicy.getControlLoop().getControlLoopName()).isEqualTo(CONTROL_NAME);
129 Assertions.assertThat(controlLoopPolicy.getPolicies()).usingElementComparatorIgnoringFields("id")
130 .containsExactly(expectedPolicy);
133 private ClampProperties createMockClampProperties(ImmutableMap<String, String> propertiesMap) {
134 ClampProperties props = Mockito.mock(ClampProperties.class);
135 propertiesMap.forEach((property, value) -> Mockito
136 .when(props.getStringValue(Matchers.matches(property), Matchers.any())).thenReturn(value));
140 private PolicyChain readPolicyChainFromResources() throws IOException {
141 String policyChainText = ResourceFileUtil
142 .getResourceAsString("example/operational-policy/json-policy-chain.json");
143 JsonNode policyChainNode = new ObjectMapper().readTree(policyChainText);
144 return new PolicyChain(policyChainNode);