Remove dead code
[clamp.git] / src / test / java / org / onap / clamp / clds / client / req / policy / OperationalPolicyAttributesConstructorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 Nokia Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.client.req.policy;
25
26 import com.google.common.collect.ImmutableMap;
27 import com.google.gson.JsonElement;
28 import java.io.IOException;
29 import java.net.URLDecoder;
30 import java.util.Map;
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;
48
49 public class OperationalPolicyAttributesConstructorTest {
50
51     private static final String CONTROL_NAME = "ClosedLoop-d4629aee-970f-11e8-86c9-02552dda865e";
52     private ModelProperties modelProperties;
53     private PolicyChain policyChain;
54
55     /**
56      * @throws Exception thrown if resource not found.
57      */
58     @Before
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,
63             "{}", modelProp);
64         policyChain = readPolicyChainFromResources();
65     }
66
67     @Test
68     public void shouldFormatRequestAttributes() throws IOException, BuilderException {
69         // given
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());
73
74         // when
75         Map<AttributeType, Map<String, String>> requestAttributes = OperationalPolicyAttributesConstructor
76             .formatAttributes(mockClampProperties, modelProperties, "789875c1-e788-432f-9a76-eac8ed889734",
77                 policyChain);
78         // then
79         Assertions.assertThat(requestAttributes).containsKeys(AttributeType.MATCHING, AttributeType.RULE);
80         Assertions.assertThat(requestAttributes.get(AttributeType.MATCHING))
81             .contains(Assertions.entry(OperationalPolicyAttributesConstructor.CONTROLLER, "amsterdam"));
82
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"));
94     }
95
96     @Test
97     public void shouldFormatRequestAttributesWithProperControlLoopYaml() throws IOException, BuilderException {
98         // given
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());
103
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);
107
108         // when
109         Map<AttributeType, Map<String, String>> requestAttributes = OperationalPolicyAttributesConstructor
110             .formatAttributes(mockClampProperties, modelProperties, "789875c1-e788-432f-9a76-eac8ed889734",
111                 policyChain);
112
113         // then
114         Assertions.assertThat(requestAttributes).containsKeys(AttributeType.MATCHING, AttributeType.RULE);
115         Assertions.assertThat(requestAttributes.get(AttributeType.MATCHING))
116             .contains(Assertions.entry("controller", "amsterdam"));
117
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"));
124
125         String controlLoopYaml = URLDecoder
126             .decode(ruleParameters.get(OperationalPolicyAttributesConstructor.CONTROL_LOOP_YAML), "UTF-8");
127         ControlLoopPolicy controlLoopPolicy = new Yaml().load(controlLoopYaml);
128
129         Assertions.assertThat(controlLoopPolicy.getControlLoop().getControlLoopName()).isEqualTo(CONTROL_NAME);
130         Assertions.assertThat(controlLoopPolicy.getPolicies()).usingElementComparatorIgnoringFields("id")
131             .containsExactly(expectedPolicy);
132     }
133
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));
138         return props;
139     }
140
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);
146     }
147 }