2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T 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 * Modifications copyright (c) 2018 Nokia
21 * ===================================================================
25 package org.onap.clamp.clds.it;
27 import java.io.IOException;
28 import java.util.List;
31 import javax.xml.transform.TransformerException;
33 import org.assertj.core.api.Assertions;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.onap.clamp.clds.client.req.policy.GuardPolicyAttributesConstructor;
38 import org.onap.clamp.clds.client.req.policy.OperationalPolicyAttributesConstructor;
39 import org.onap.clamp.clds.client.req.policy.PolicyClient;
40 import org.onap.clamp.clds.client.req.tca.TcaRequestFormatter;
41 import org.onap.clamp.clds.config.ClampProperties;
42 import org.onap.clamp.clds.model.CldsEvent;
43 import org.onap.clamp.clds.model.properties.ModelProperties;
44 import org.onap.clamp.clds.model.properties.Policy;
45 import org.onap.clamp.clds.model.properties.PolicyItem;
46 import org.onap.clamp.clds.model.properties.Tca;
47 import org.onap.clamp.clds.transform.XslTransformer;
48 import org.onap.clamp.clds.util.JacksonUtils;
49 import org.onap.clamp.clds.util.LoggingUtils;
50 import org.onap.clamp.clds.util.ResourceFileUtil;
51 import org.onap.policy.api.AttributeType;
52 import org.onap.policy.api.PolicyConfigType;
53 import org.onap.policy.api.PolicyType;
54 import org.onap.policy.controlloop.policy.builder.BuilderException;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.boot.test.context.SpringBootTest;
57 import org.springframework.test.context.junit4.SpringRunner;
60 * Test Policy API, this uses the emulator written in python that is started
61 * during the tests, It returns the payload sent in the policy queries so that
62 * it can be validated here.
64 @RunWith(SpringRunner.class)
66 public class PolicyClientItCase {
69 private ClampProperties refProp;
71 private PolicyClient policyClient;
73 XslTransformer cldsBpmnTransformer;
78 String modelBpmnPropJson;
82 public void setUp() throws IOException, TransformerException {
83 modelProp = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/model-properties.json");
84 modelName = "example-model06";
85 controlName = "ClosedLoop_FRWL_SIG_fad4dcae_e498_11e6_852e_0050568c4ccf";
86 modelBpmnPropJson = cldsBpmnTransformer.doXslTransformToString(
87 ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-template.xml"));
88 prop = new ModelProperties(modelName, controlName, CldsEvent.ACTION_SUBMIT, false, modelBpmnPropJson,
93 public void testSendGuardPolicy() throws TransformerException, IOException {
94 // Normally there is only one Guard
95 List<PolicyItem> policyItems = GuardPolicyAttributesConstructor
96 .getAllPolicyGuardsFromPolicyChain(prop.getType(Policy.class).getPolicyChains().get(0));
97 PolicyItem policyItem = policyItems.get(0);
98 prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
99 prop.setPolicyUniqueId(prop.getType(Policy.class).getPolicyChains().get(0).getPolicyId());
100 prop.setGuardUniqueId(policyItem.getId());
101 String response = policyClient.sendGuardPolicy(
102 GuardPolicyAttributesConstructor.formatAttributes(prop, policyItem), prop, LoggingUtils.getRequestId(),
104 Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
105 .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
106 Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Decision"),
107 Assertions.entry("policyName",
108 modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0_Guard_6TtHGPq"),
109 Assertions.entry("policyDescription", "from clds"), Assertions.entry("onapName", "PDPD"),
110 Assertions.entry("requestID", LoggingUtils.getRequestId()), Assertions.entry("ruleProvider", "GUARD_YAML"));
112 // Check Guard attributes
113 Assertions.assertThat((Map<String, Object>) mapNodes.get("attributes"))
114 .containsKey(AttributeType.MATCHING.name());
115 Assertions.assertThat(
116 (Map<String, Object>) ((Map<String, Object>) mapNodes.get("attributes")).get(AttributeType.MATCHING.name()))
117 .contains(Assertions.entry(GuardPolicyAttributesConstructor.ACTOR, "APPC"),
118 Assertions.entry(GuardPolicyAttributesConstructor.CLNAME, controlName + "_0"),
119 Assertions.entry(GuardPolicyAttributesConstructor.TIME_WINDOW, "10"));
123 public void testSendBrmsPolicy()
124 throws TransformerException, BuilderException, IllegalArgumentException, IOException {
125 Map<AttributeType, Map<String, String>> attributes = OperationalPolicyAttributesConstructor.formatAttributes(
126 refProp, prop, prop.getType(Policy.class).getId(), prop.getType(Policy.class).getPolicyChains().get(0));
127 String response = policyClient.sendBrmsPolicy(attributes, prop, LoggingUtils.getRequestId());
129 Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
130 .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
131 Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
132 Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0"),
133 Assertions.entry("policyConfigType", PolicyConfigType.BRMS_PARAM.name()),
134 Assertions.entry("requestID", LoggingUtils.getRequestId()));
136 // Check BRMS attributes present
137 Assertions.assertThat((Map<String, Object>) mapNodes.get("attributes"))
138 .containsKeys(AttributeType.MATCHING.name(), AttributeType.RULE.name());
143 public void testSendMicroServiceInJson()
144 throws TransformerException, BuilderException, IllegalArgumentException, IOException {
145 prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
146 String jsonToSend = "{\"test\":\"test\"}";
147 String response = policyClient.sendMicroServiceInJson(jsonToSend, prop, LoggingUtils.getRequestId());
149 Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
150 .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
151 Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
152 Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h"),
153 Assertions.entry("policyConfigType", PolicyConfigType.MicroService.name()),
154 Assertions.entry("requestID", LoggingUtils.getRequestId()),
155 Assertions.entry("configBodyType", PolicyType.JSON.name()), Assertions.entry("onapName", "DCAE"),
156 Assertions.entry("configBody", jsonToSend));
161 public void testSendBasePolicyInOther() throws IllegalArgumentException, IOException {
162 String body = "test";
163 String response = policyClient.sendBasePolicyInOther(body, "myPolicy", prop, LoggingUtils.getRequestId());
164 Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
165 .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
166 Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
167 Assertions.entry("policyName", "myPolicy"),
168 Assertions.entry("policyConfigType", PolicyConfigType.Base.name()),
169 Assertions.entry("requestID", LoggingUtils.getRequestId()),
170 Assertions.entry("configBodyType", PolicyType.OTHER.name()), Assertions.entry("onapName", "DCAE"),
171 Assertions.entry("configBody", body));
175 public void testSendMicroServiceInOther() throws IllegalArgumentException, IOException {
176 Tca tca = prop.getType(Tca.class);
177 String tcaJson = TcaRequestFormatter.createPolicyJson(refProp, prop);
178 String response = policyClient.sendMicroServiceInOther(tcaJson, prop);
180 Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
181 .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
182 Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
183 Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_TCA_1d13unw"),
184 Assertions.entry("policyConfigType", PolicyConfigType.MicroService.name()),
185 Assertions.entry("configBody", tcaJson), Assertions.entry("onapName", "DCAE"));
189 public void testDeleteMicrosService() throws IllegalArgumentException, IOException {
190 Tca tca = prop.getType(Tca.class);
191 prop.setCurrentModelElementId(tca.getId());
192 String[] responses = policyClient.deleteMicrosService(prop).split("\\}\\{");
194 // There are 2 responses appended to the result, one for PDP one for PAP !
195 Map<String, Object> mapNodesPdp = JacksonUtils.getObjectMapperInstance()
196 .convertValue(JacksonUtils.getObjectMapperInstance().readTree(responses[0] + "}"), Map.class);
197 Map<String, Object> mapNodesPap = JacksonUtils.getObjectMapperInstance()
198 .convertValue(JacksonUtils.getObjectMapperInstance().readTree("{" + responses[1]), Map.class);
200 Assertions.assertThat(mapNodesPdp).contains(
201 Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_TCA_1d13unw"),
202 Assertions.entry("policyType", PolicyConfigType.MicroService.name()),
203 Assertions.entry("policyComponent", "PDP"), Assertions.entry("deleteCondition", "ALL"));
205 Assertions.assertThat(mapNodesPap).contains(
206 Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_TCA_1d13unw"),
207 Assertions.entry("policyType", PolicyConfigType.MicroService.name()),
208 Assertions.entry("policyComponent", "PAP"), Assertions.entry("deleteCondition", "ALL"));
212 public void testDeleteGuard() throws IllegalArgumentException, IOException {
213 List<PolicyItem> policyItems = GuardPolicyAttributesConstructor
214 .getAllPolicyGuardsFromPolicyChain(prop.getType(Policy.class).getPolicyChains().get(0));
215 prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
216 prop.setPolicyUniqueId(prop.getType(Policy.class).getPolicyChains().get(0).getPolicyId());
217 prop.setGuardUniqueId(policyItems.get(0).getId());
218 String[] responses = policyClient.deleteGuard(prop).split("\\}\\{");
220 // There are 2 responses appended to the result, one for PDP one for PAP !
221 Map<String, Object> mapNodesPdp = JacksonUtils.getObjectMapperInstance()
222 .convertValue(JacksonUtils.getObjectMapperInstance().readTree(responses[0] + "}"), Map.class);
223 Map<String, Object> mapNodesPap = JacksonUtils.getObjectMapperInstance()
224 .convertValue(JacksonUtils.getObjectMapperInstance().readTree("{" + responses[1]), Map.class);
226 Assertions.assertThat(mapNodesPdp).contains(
227 Assertions.entry("policyName",
228 modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0_Guard_6TtHGPq"),
229 Assertions.entry("policyType", "Decision"), Assertions.entry("policyComponent", "PDP"),
230 Assertions.entry("deleteCondition", "ALL"));
231 Assertions.assertThat(mapNodesPap).contains(
232 Assertions.entry("policyName",
233 modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0_Guard_6TtHGPq"),
234 Assertions.entry("policyType", "Decision"), Assertions.entry("policyComponent", "PAP"),
235 Assertions.entry("deleteCondition", "ALL"));
239 public void testDeleteBrms() throws IllegalArgumentException, IOException {
240 prop.setPolicyUniqueId(prop.getType(Policy.class).getPolicyChains().get(0).getPolicyId());
241 prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
242 String[] responses = policyClient.deleteBrms(prop).split("\\}\\{");
244 // There are 2 responses appended to the result, one for PDP one for PAP !
245 Map<String, Object> mapNodesPdp = JacksonUtils.getObjectMapperInstance()
246 .convertValue(JacksonUtils.getObjectMapperInstance().readTree(responses[0] + "}"), Map.class);
247 Map<String, Object> mapNodesPap = JacksonUtils.getObjectMapperInstance()
248 .convertValue(JacksonUtils.getObjectMapperInstance().readTree("{" + responses[1]), Map.class);
250 Assertions.assertThat(mapNodesPdp).contains(
251 Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0"),
252 Assertions.entry("policyType", "BRMS_Param"), Assertions.entry("policyComponent", "PDP"),
253 Assertions.entry("deleteCondition", "ALL"));
254 Assertions.assertThat(mapNodesPap).contains(
255 Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0"),
256 Assertions.entry("policyType", "BRMS_Param"), Assertions.entry("policyComponent", "PAP"),
257 Assertions.entry("deleteCondition", "ALL"));