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.google.common.collect.ImmutableMap;
27 import com.google.gson.JsonElement;
28 import java.io.IOException;
29 import java.net.URLDecoder;
31 import org.assertj.core.api.Assertions;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Matchers;
35 import org.mockito.Mockito;
36 import org.onap.clamp.clds.config.ClampProperties;
37 import org.onap.clamp.clds.model.properties.ModelProperties;
38 import org.onap.clamp.clds.model.properties.PolicyChain;
39 import org.onap.clamp.clds.util.JsonUtils;
40 import org.onap.clamp.clds.util.ResourceFileUtil;
41 import org.onap.policy.api.AttributeType;
42 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
43 import org.onap.policy.controlloop.policy.Policy;
44 import org.onap.policy.controlloop.policy.Target;
45 import org.onap.policy.controlloop.policy.TargetType;
46 import org.onap.policy.controlloop.policy.builder.BuilderException;
47 import org.yaml.snakeyaml.Yaml;
49 public class OperationalPolicyAttributesConstructorTest {
51 private static final String CONTROL_NAME = "ClosedLoop-d4629aee-970f-11e8-86c9-02552dda865e";
52 private ModelProperties modelProperties;
53 private PolicyChain policyChain;
56 * @throws Exception thrown if resource not found.
59 public void setUp() throws Exception {
60 String modelProp = ResourceFileUtil
61 .getResourceAsString("example/model-properties/policy/modelBpmnProperties.json");
62 modelProperties = new ModelProperties("CLAMPDemoVFW_v1_0_3af8daec-6f10-4027-a3540", CONTROL_NAME, "PUT", false,
64 policyChain = readPolicyChainFromResources();
68 public void shouldFormatRequestAttributes() throws IOException, BuilderException {
70 ClampProperties mockClampProperties = createMockClampProperties(ImmutableMap.<String, String>builder()
71 .put("op.templateName", "ClosedLoopControlName").put("op.notificationTopic", "POLICY-CL-MGT")
72 .put("op.controller", "amsterdam").put("op.recipeTopic", "APPC").build());
75 Map<AttributeType, Map<String, String>> requestAttributes = OperationalPolicyAttributesConstructor
76 .formatAttributes(mockClampProperties, modelProperties, "789875c1-e788-432f-9a76-eac8ed889734",
79 Assertions.assertThat(requestAttributes).containsKeys(AttributeType.MATCHING, AttributeType.RULE);
80 Assertions.assertThat(requestAttributes.get(AttributeType.MATCHING))
81 .contains(Assertions.entry(OperationalPolicyAttributesConstructor.CONTROLLER, "amsterdam"));
83 Map<String, String> ruleParameters = requestAttributes.get(AttributeType.RULE);
84 Assertions.assertThat(ruleParameters).containsExactly(
85 Assertions.entry(OperationalPolicyAttributesConstructor.MAX_RETRIES, "3"),
86 Assertions.entry(OperationalPolicyAttributesConstructor.TEMPLATE_NAME, "ClosedLoopControlName"),
87 Assertions.entry(OperationalPolicyAttributesConstructor.NOTIFICATION_TOPIC, "POLICY-CL-MGT"),
88 Assertions.entry(OperationalPolicyAttributesConstructor.RECIPE_TOPIC, "APPC"),
89 Assertions.entry(OperationalPolicyAttributesConstructor.RECIPE, "healthCheck"),
90 Assertions.entry(OperationalPolicyAttributesConstructor.RESOURCE_ID,
91 "cdb69724-57d5-4a22-b96c-4c345150fd0e"),
92 Assertions.entry(OperationalPolicyAttributesConstructor.RETRY_TIME_LIMIT, "180"),
93 Assertions.entry(OperationalPolicyAttributesConstructor.CLOSED_LOOP_CONTROL_NAME, CONTROL_NAME + "_1"));
97 public void shouldFormatRequestAttributesWithProperControlLoopYaml() throws IOException, BuilderException {
99 ClampProperties mockClampProperties = createMockClampProperties(
100 ImmutableMap.<String, String>builder().put("op.templateName", "ClosedLoopControlName")
101 .put("op.operationTopic", "APPP-CL").put("op.notificationTopic", "POLICY-CL-MGT")
102 .put("op.controller", "amsterdam").put("op.recipeTopic", "APPC").build());
104 Policy expectedPolicy = new Policy("6f76ad0b-ea9d-4a92-8d7d-6a6367ce2c77", "healthCheck Policy",
105 "healthCheck Policy - the trigger (no parent) policy - created by CLDS", "APPC", null,
106 new Target(TargetType.VM, "cdb69724-57d5-4a22-b96c-4c345150fd0e"), "healthCheck", 3, 180);
109 Map<AttributeType, Map<String, String>> requestAttributes = OperationalPolicyAttributesConstructor
110 .formatAttributes(mockClampProperties, modelProperties, "789875c1-e788-432f-9a76-eac8ed889734",
114 Assertions.assertThat(requestAttributes).containsKeys(AttributeType.MATCHING, AttributeType.RULE);
115 Assertions.assertThat(requestAttributes.get(AttributeType.MATCHING))
116 .contains(Assertions.entry("controller", "amsterdam"));
118 Map<String, String> ruleParameters = requestAttributes.get(AttributeType.RULE);
119 Assertions.assertThat(ruleParameters).contains(
120 Assertions.entry(OperationalPolicyAttributesConstructor.OPERATION_TOPIC, "APPP-CL"),
121 Assertions.entry(OperationalPolicyAttributesConstructor.TEMPLATE_NAME, "ClosedLoopControlName"),
122 Assertions.entry(OperationalPolicyAttributesConstructor.NOTIFICATION_TOPIC, "POLICY-CL-MGT"),
123 Assertions.entry(OperationalPolicyAttributesConstructor.CLOSED_LOOP_CONTROL_NAME, CONTROL_NAME + "_1"));
125 String controlLoopYaml = URLDecoder
126 .decode(ruleParameters.get(OperationalPolicyAttributesConstructor.CONTROL_LOOP_YAML), "UTF-8");
127 ControlLoopPolicy controlLoopPolicy = new Yaml().load(controlLoopYaml);
129 Assertions.assertThat(controlLoopPolicy.getControlLoop().getControlLoopName()).isEqualTo(CONTROL_NAME);
130 Assertions.assertThat(controlLoopPolicy.getPolicies()).usingElementComparatorIgnoringFields("id")
131 .containsExactly(expectedPolicy);
134 private ClampProperties createMockClampProperties(ImmutableMap<String, String> propertiesMap) {
135 ClampProperties props = Mockito.mock(ClampProperties.class);
136 propertiesMap.forEach((property, value) -> Mockito
137 .when(props.getStringValue(Matchers.matches(property), Matchers.any())).thenReturn(value));
141 private PolicyChain readPolicyChainFromResources() throws IOException {
142 String policyChainText = ResourceFileUtil
143 .getResourceAsString("example/operational-policy/json-policy-chain.json");
144 JsonElement policyChainNode = JsonUtils.GSON.fromJson(policyChainText, JsonElement.class);
145 return new PolicyChain(policyChainNode);