Additional code for Tosca
[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
30 import java.io.IOException;
31 import java.net.URLDecoder;
32 import java.util.Map;
33
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;
50
51 public class OperationalPolicyAttributesConstructorTest {
52
53     private static final String CONTROL_NAME = "ClosedLoop-d4629aee-970f-11e8-86c9-02552dda865e";
54     private ModelProperties modelProperties;
55     private PolicyChain policyChain;
56
57     @Before
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,
62             "{}", modelProp);
63         policyChain = readPolicyChainFromResources();
64     }
65
66     @Test
67     public void shouldFormatRequestAttributes() throws IOException, BuilderException {
68         // given
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());
72
73         // when
74         Map<AttributeType, Map<String, String>> requestAttributes = OperationalPolicyAttributesConstructor
75             .formatAttributes(mockClampProperties, modelProperties, "789875c1-e788-432f-9a76-eac8ed889734",
76                 policyChain);
77         // then
78         Assertions.assertThat(requestAttributes).containsKeys(AttributeType.MATCHING, AttributeType.RULE);
79         Assertions.assertThat(requestAttributes.get(AttributeType.MATCHING))
80             .contains(Assertions.entry(OperationalPolicyAttributesConstructor.CONTROLLER, "amsterdam"));
81
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"));
93     }
94
95     @Test
96     public void shouldFormatRequestAttributesWithProperControlLoopYaml() throws IOException, BuilderException {
97         // given
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());
102
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);
106
107         // when
108         Map<AttributeType, Map<String, String>> requestAttributes = OperationalPolicyAttributesConstructor
109             .formatAttributes(mockClampProperties, modelProperties, "789875c1-e788-432f-9a76-eac8ed889734",
110                 policyChain);
111
112         // then
113         Assertions.assertThat(requestAttributes).containsKeys(AttributeType.MATCHING, AttributeType.RULE);
114         Assertions.assertThat(requestAttributes.get(AttributeType.MATCHING))
115             .contains(Assertions.entry("controller", "amsterdam"));
116
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"));
123
124         String controlLoopYaml = URLDecoder
125             .decode(ruleParameters.get(OperationalPolicyAttributesConstructor.CONTROL_LOOP_YAML), "UTF-8");
126         ControlLoopPolicy controlLoopPolicy = new Yaml().load(controlLoopYaml);
127
128         Assertions.assertThat(controlLoopPolicy.getControlLoop().getControlLoopName()).isEqualTo(CONTROL_NAME);
129         Assertions.assertThat(controlLoopPolicy.getPolicies()).usingElementComparatorIgnoringFields("id")
130             .containsExactly(expectedPolicy);
131     }
132
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));
137         return props;
138     }
139
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);
145     }
146 }