Fix for sonar issue
[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.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.google.common.collect.ImmutableMap;
29 import org.assertj.core.api.Assertions;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Matchers;
33 import org.mockito.Mockito;
34 import org.onap.clamp.clds.config.ClampProperties;
35 import org.onap.clamp.clds.model.properties.ModelProperties;
36 import org.onap.clamp.clds.model.properties.PolicyChain;
37 import org.onap.clamp.clds.util.ResourceFileUtil;
38 import org.onap.policy.api.AttributeType;
39 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
40 import org.onap.policy.controlloop.policy.Policy;
41 import org.onap.policy.controlloop.policy.Target;
42 import org.onap.policy.controlloop.policy.TargetType;
43 import org.onap.policy.controlloop.policy.builder.BuilderException;
44 import org.yaml.snakeyaml.Yaml;
45
46 import java.io.IOException;
47 import java.net.URLDecoder;
48 import java.util.Map;
49
50 public class OperationalPolicyAttributesConstructorTest {
51
52     private static final String CONTROL_NAME = "ClosedLoop-d4629aee-970f-11e8-86c9-02552dda865e";
53     private ModelProperties modelProperties;
54     private PolicyChain policyChain;
55
56     private OperationalPolicyYamlFormatter operationalPolicyYamlFormatter = new OperationalPolicyYamlFormatter();
57     private OperationalPolicyAttributesConstructor operationalPolicyAttributesConstructor =
58             new OperationalPolicyAttributesConstructor(operationalPolicyYamlFormatter);
59
60     @Before
61     public void setUp() throws Exception {
62         String modelProp = ResourceFileUtil
63                 .getResourceAsString("example/model-properties/policy/modelBpmnProperties.json");
64         modelProperties = new ModelProperties("CLAMPDemoVFW_v1_0_3af8daec-6f10-4027-a3540",
65                 CONTROL_NAME, "PUT", false, "{}", modelProp);
66         policyChain = readPolicyChainFromResources();
67     }
68
69
70     @Test
71     public void shouldFormatRequestAttributes() throws IOException, BuilderException {
72         // given
73         ClampProperties mockClampProperties = createMockClampProperties(ImmutableMap.<String, String>builder()
74                 .put("op.templateName", "ClosedLoopControlName")
75                 .put("op.notificationTopic", "POLICY-CL-MGT")
76                 .put("op.controller", "amsterdam")
77                 .put("op.recipeTopic", "APPC")
78                 .build());
79
80         //when
81         Map<AttributeType, Map<String, String>> requestAttributes
82                 = operationalPolicyAttributesConstructor.formatAttributes(mockClampProperties, modelProperties,
83                 "789875c1-e788-432f-9a76-eac8ed889734", policyChain);
84         //then
85         Assertions.assertThat(requestAttributes).containsKeys(AttributeType.MATCHING, AttributeType.RULE);
86         Assertions.assertThat(requestAttributes.get(AttributeType.MATCHING))
87                 .contains(Assertions.entry(OperationalPolicyAttributesConstructor.CONTROLLER, "amsterdam"));
88
89         Map<String, String> ruleParameters = requestAttributes.get(AttributeType.RULE);
90         Assertions.assertThat(ruleParameters).containsExactly(
91                 Assertions.entry(OperationalPolicyAttributesConstructor.MAX_RETRIES, "3"),
92                 Assertions.entry(OperationalPolicyAttributesConstructor.TEMPLATE_NAME, "ClosedLoopControlName"),
93                 Assertions.entry(OperationalPolicyAttributesConstructor.NOTIFICATION_TOPIC, "POLICY-CL-MGT"),
94                 Assertions.entry(OperationalPolicyAttributesConstructor.RECIPE_TOPIC, "APPC"),
95                 Assertions.entry(OperationalPolicyAttributesConstructor.RECIPE, "healthCheck"),
96                 Assertions.entry(OperationalPolicyAttributesConstructor.RESOURCE_ID,
97                         "cdb69724-57d5-4a22-b96c-4c345150fd0e"),
98                 Assertions.entry(OperationalPolicyAttributesConstructor.RETRY_TIME_LIMIT, "180"),
99                 Assertions.entry(OperationalPolicyAttributesConstructor.CLOSED_LOOP_CONTROL_NAME, CONTROL_NAME + "_1")
100         );
101     }
102
103     @Test
104     public void shouldFormatRequestAttributesWithProperControlLoopYaml() throws IOException, BuilderException {
105         //given
106         ClampProperties mockClampProperties = createMockClampProperties(ImmutableMap.<String, String>builder()
107                 .put("op.templateName", "ClosedLoopControlName")
108                 .put("op.operationTopic", "APPP-CL")
109                 .put("op.notificationTopic", "POLICY-CL-MGT")
110                 .put("op.controller", "amsterdam")
111                 .put("op.recipeTopic", "APPC")
112                 .build());
113
114         Policy expectedPolicy = new Policy("6f76ad0b-ea9d-4a92-8d7d-6a6367ce2c77", "healthCheck Policy",
115                 "healthCheck Policy - the trigger (no parent) policy - created by CLDS", "APPC",
116                 null, new Target(TargetType.VM, "cdb69724-57d5-4a22-b96c-4c345150fd0e"),
117                 "healthCheck", 3, 180);
118
119         //when
120         Map<AttributeType, Map<String, String>> requestAttributes = operationalPolicyAttributesConstructor
121                 .formatAttributes(mockClampProperties, modelProperties,
122                         "789875c1-e788-432f-9a76-eac8ed889734", policyChain);
123
124         //then
125         Assertions.assertThat(requestAttributes)
126                 .containsKeys(AttributeType.MATCHING, AttributeType.RULE);
127         Assertions.assertThat(requestAttributes
128                 .get(AttributeType.MATCHING))
129                 .contains(Assertions.entry("controller", "amsterdam"));
130
131         Map<String, String> ruleParameters = requestAttributes.get(AttributeType.RULE);
132         Assertions.assertThat(ruleParameters).contains(
133                 Assertions.entry(OperationalPolicyAttributesConstructor.OPERATION_TOPIC, "APPP-CL"),
134                 Assertions.entry(OperationalPolicyAttributesConstructor.TEMPLATE_NAME, "ClosedLoopControlName"),
135                 Assertions.entry(OperationalPolicyAttributesConstructor.NOTIFICATION_TOPIC, "POLICY-CL-MGT"),
136                 Assertions.entry(OperationalPolicyAttributesConstructor.CLOSED_LOOP_CONTROL_NAME, CONTROL_NAME + "_1")
137         );
138
139         String controlLoopYaml = URLDecoder.decode(
140                 ruleParameters.get(OperationalPolicyAttributesConstructor.CONTROL_LOOP_YAML), "UTF-8");
141         ControlLoopPolicy controlLoopPolicy = new Yaml().load(controlLoopYaml);
142
143         Assertions.assertThat(controlLoopPolicy.getControlLoop().getControlLoopName()).isEqualTo(CONTROL_NAME);
144         Assertions.assertThat(controlLoopPolicy.getPolicies())
145                 .usingElementComparatorIgnoringFields("id")
146                 .containsExactly(expectedPolicy);
147     }
148
149
150     private ClampProperties createMockClampProperties(ImmutableMap<String, String> propertiesMap) {
151         ClampProperties props = Mockito.mock(ClampProperties.class);
152         propertiesMap.forEach((property, value) ->
153                 Mockito.when(props.getStringValue(Matchers.matches(property), Matchers.any())).thenReturn(value)
154         );
155         return props;
156     }
157
158     private PolicyChain readPolicyChainFromResources() throws IOException {
159         String policyChainText = ResourceFileUtil
160                 .getResourceAsString("example/operational-policy/json-policy-chain.json");
161         JsonNode policyChainNode = new ObjectMapper().readTree(policyChainText);
162         return new PolicyChain(policyChainNode);
163     }
164 }