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 com.google.gson.reflect.TypeToken;
28 import java.io.IOException;
29 import java.lang.reflect.Type;
30 import java.util.List;
33 import javax.xml.transform.TransformerException;
35 import org.assertj.core.api.Assertions;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.onap.clamp.clds.client.req.policy.GuardPolicyAttributesConstructor;
40 import org.onap.clamp.clds.client.req.policy.OperationalPolicyAttributesConstructor;
41 import org.onap.clamp.clds.client.req.policy.PolicyClient;
42 import org.onap.clamp.clds.client.req.tca.TcaRequestFormatter;
43 import org.onap.clamp.clds.config.ClampProperties;
44 import org.onap.clamp.clds.dao.CldsDao;
45 import org.onap.clamp.clds.model.CldsEvent;
46 import org.onap.clamp.clds.model.CldsToscaModel;
47 import org.onap.clamp.clds.model.properties.ModelProperties;
48 import org.onap.clamp.clds.model.properties.Policy;
49 import org.onap.clamp.clds.model.properties.PolicyItem;
50 import org.onap.clamp.clds.model.properties.Tca;
51 import org.onap.clamp.clds.transform.XslTransformer;
52 import org.onap.clamp.clds.util.JsonUtils;
53 import org.onap.clamp.clds.util.LoggingUtils;
54 import org.onap.clamp.clds.util.ResourceFileUtil;
55 import org.onap.policy.api.AttributeType;
56 import org.onap.policy.api.PolicyConfigType;
57 import org.onap.policy.api.PolicyType;
58 import org.onap.policy.controlloop.policy.builder.BuilderException;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.boot.test.context.SpringBootTest;
61 import org.springframework.test.context.junit4.SpringRunner;
64 * Test Policy API, this uses the emulator written in python that is started
65 * during the tests, It returns the payload sent in the policy queries so that
66 * it can be validated here.
68 @RunWith(SpringRunner.class)
70 public class PolicyClientItCase {
72 private static final Type MAP_OF_STRING_TO_OBJECT_TYPE = new TypeToken<Map<String, Object>>() {}.getType();
75 private CldsDao cldsDao;
77 private ClampProperties refProp;
79 private PolicyClient policyClient;
81 XslTransformer cldsBpmnTransformer;
86 String modelBpmnPropJson;
91 * @throws IOException thrown if resources not found
92 * @throws TransformerException thrown if invalid xml given to transformation
95 public void setUp() throws IOException, TransformerException {
96 modelProp = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/model-properties.json");
97 modelName = "example-model06";
98 controlName = "ClosedLoop_FRWL_SIG_fad4dcae_e498_11e6_852e_0050568c4ccf";
99 modelBpmnPropJson = cldsBpmnTransformer.doXslTransformToString(
100 ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-template.xml"));
101 prop = new ModelProperties(modelName, controlName, CldsEvent.ACTION_SUBMIT, false, modelBpmnPropJson,
106 public void testSendGuardPolicy() {
107 // Normally there is only one Guard
108 List<PolicyItem> policyItems = GuardPolicyAttributesConstructor
109 .getAllPolicyGuardsFromPolicyChain(prop.getType(Policy.class).getPolicyChains().get(0));
110 PolicyItem policyItem = policyItems.get(0);
111 prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
112 prop.setPolicyUniqueId(prop.getType(Policy.class).getPolicyChains().get(0).getPolicyId());
113 prop.setGuardUniqueId(policyItem.getId());
114 String response = policyClient.sendGuardPolicy(
115 GuardPolicyAttributesConstructor.formatAttributes(prop, policyItem), prop, LoggingUtils.getRequestId(),
117 Map<String, Object> mapNodes = JsonUtils.GSON.fromJson(response, MAP_OF_STRING_TO_OBJECT_TYPE);
118 Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Decision"),
119 Assertions.entry("policyName",
120 modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0_Guard_6TtHGPq"),
121 Assertions.entry("policyDescription", "from clds"), Assertions.entry("onapName", "PDPD"),
122 Assertions.entry("requestID", LoggingUtils.getRequestId()), Assertions.entry("ruleProvider", "GUARD_YAML"));
124 // Check Guard attributes
125 Assertions.assertThat((Map<String, Object>) mapNodes.get("attributes"))
126 .containsKey(AttributeType.MATCHING.name());
127 Assertions.assertThat(
128 (Map<String, Object>) ((Map<String, Object>) mapNodes.get("attributes")).get(AttributeType.MATCHING.name()))
129 .contains(Assertions.entry(GuardPolicyAttributesConstructor.ACTOR, "APPC"),
130 Assertions.entry(GuardPolicyAttributesConstructor.CLNAME, controlName + "_0"),
131 Assertions.entry(GuardPolicyAttributesConstructor.TIME_WINDOW, "10"));
135 public void testSendBrmsPolicy() throws BuilderException, IllegalArgumentException, IOException {
136 Map<AttributeType, Map<String, String>> attributes = OperationalPolicyAttributesConstructor.formatAttributes(
137 refProp, prop, prop.getType(Policy.class).getId(), prop.getType(Policy.class).getPolicyChains().get(0));
138 String response = policyClient.sendBrmsPolicy(attributes, prop, LoggingUtils.getRequestId());
140 Map<String, Object> mapNodes = JsonUtils.GSON.fromJson(response, MAP_OF_STRING_TO_OBJECT_TYPE);
142 Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
143 Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0"),
144 Assertions.entry("policyConfigType", PolicyConfigType.BRMS_PARAM.name()),
145 Assertions.entry("requestID", LoggingUtils.getRequestId()));
147 // Check BRMS attributes present
148 Assertions.assertThat((Map<String, Object>) mapNodes.get("attributes"))
149 .containsKeys(AttributeType.MATCHING.name(), AttributeType.RULE.name());
154 public void testSendMicroServiceInJson() throws IllegalArgumentException {
155 prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
156 String jsonToSend = "{\"test\":\"test\"}";
157 String response = policyClient.sendMicroServiceInJson(jsonToSend, prop, LoggingUtils.getRequestId());
159 Map<String, Object> mapNodes = JsonUtils.GSON.fromJson(response, MAP_OF_STRING_TO_OBJECT_TYPE);
161 Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
162 Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h"),
163 Assertions.entry("policyConfigType", PolicyConfigType.MicroService.name()),
164 Assertions.entry("requestID", LoggingUtils.getRequestId()),
165 Assertions.entry("configBodyType", PolicyType.JSON.name()), Assertions.entry("onapName", "DCAE"),
166 Assertions.entry("configBody", jsonToSend));
171 public void testSendBasePolicyInOther() throws IllegalArgumentException, IOException {
172 String body = "test";
173 String response = policyClient.sendBasePolicyInOther(body, "myPolicy", prop, LoggingUtils.getRequestId());
174 Map<String, Object> mapNodes = JsonUtils.GSON.fromJson(response, MAP_OF_STRING_TO_OBJECT_TYPE);
176 Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
177 Assertions.entry("policyName", "myPolicy"),
178 Assertions.entry("policyConfigType", PolicyConfigType.Base.name()),
179 Assertions.entry("requestID", LoggingUtils.getRequestId()),
180 Assertions.entry("configBodyType", PolicyType.OTHER.name()), Assertions.entry("onapName", "DCAE"),
181 Assertions.entry("configBody", body));
185 public void testSendMicroServiceInOther() throws IllegalArgumentException, IOException {
186 Tca tca = prop.getType(Tca.class);
187 String tcaJson = TcaRequestFormatter.createPolicyJson(refProp, prop);
188 String response = policyClient.sendMicroServiceInOther(tcaJson, prop);
190 Map<String, Object> mapNodes = JsonUtils.GSON.fromJson(response, MAP_OF_STRING_TO_OBJECT_TYPE);
192 Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
193 Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_TCA_1d13unw"),
194 Assertions.entry("policyConfigType", PolicyConfigType.MicroService.name()),
195 Assertions.entry("configBody", tcaJson), Assertions.entry("onapName", "DCAE"));
199 public void testDeleteMicrosService() throws IllegalArgumentException, IOException {
200 Tca tca = prop.getType(Tca.class);
201 prop.setCurrentModelElementId(tca.getId());
202 String[] responses = policyClient.deleteMicrosService(prop).split("\\}\\{");
204 // There are 2 responses appended to the result, one for PDP one for PAP !
205 Map<String, Object> mapNodesPdp = JsonUtils.GSON.fromJson(responses[0] + "}",
206 MAP_OF_STRING_TO_OBJECT_TYPE);
207 Map<String, Object> mapNodesPap = JsonUtils.GSON.fromJson("{" + responses[1],
208 MAP_OF_STRING_TO_OBJECT_TYPE);
210 Assertions.assertThat(mapNodesPdp).contains(
211 Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_TCA_1d13unw"),
212 Assertions.entry("policyType", PolicyConfigType.MicroService.name()),
213 Assertions.entry("policyComponent", "PDP"), Assertions.entry("deleteCondition", "ALL"));
215 Assertions.assertThat(mapNodesPap).contains(
216 Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_TCA_1d13unw"),
217 Assertions.entry("policyType", PolicyConfigType.MicroService.name()),
218 Assertions.entry("policyComponent", "PAP"), Assertions.entry("deleteCondition", "ALL"));
222 public void testDeleteGuard() throws IllegalArgumentException, IOException {
223 List<PolicyItem> policyItems = GuardPolicyAttributesConstructor
224 .getAllPolicyGuardsFromPolicyChain(prop.getType(Policy.class).getPolicyChains().get(0));
225 prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
226 prop.setPolicyUniqueId(prop.getType(Policy.class).getPolicyChains().get(0).getPolicyId());
227 prop.setGuardUniqueId(policyItems.get(0).getId());
228 String[] responses = policyClient.deleteGuard(prop).split("\\}\\{");
230 Map<String, Object> mapNodesPdp = JsonUtils.GSON.fromJson(responses[0] + "}",
231 MAP_OF_STRING_TO_OBJECT_TYPE);
232 Map<String, Object> mapNodesPap = JsonUtils.GSON.fromJson("{" + responses[1],
233 MAP_OF_STRING_TO_OBJECT_TYPE);
235 Assertions.assertThat(mapNodesPdp).contains(
236 Assertions.entry("policyName",
237 modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0_Guard_6TtHGPq"),
238 Assertions.entry("policyType", "Decision"), Assertions.entry("policyComponent", "PDP"),
239 Assertions.entry("deleteCondition", "ALL"));
240 Assertions.assertThat(mapNodesPap).contains(
241 Assertions.entry("policyName",
242 modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0_Guard_6TtHGPq"),
243 Assertions.entry("policyType", "Decision"), Assertions.entry("policyComponent", "PAP"),
244 Assertions.entry("deleteCondition", "ALL"));
248 public void testDeleteBrms() throws IllegalArgumentException, IOException {
249 prop.setPolicyUniqueId(prop.getType(Policy.class).getPolicyChains().get(0).getPolicyId());
250 prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
251 String[] responses = policyClient.deleteBrms(prop).split("\\}\\{");
253 Map<String, Object> mapNodesPdp = JsonUtils.GSON.fromJson(responses[0] + "}",
254 MAP_OF_STRING_TO_OBJECT_TYPE);
255 Map<String, Object> mapNodesPap = JsonUtils.GSON.fromJson("{" + responses[1],
256 MAP_OF_STRING_TO_OBJECT_TYPE);
258 Assertions.assertThat(mapNodesPdp).contains(
259 Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0"),
260 Assertions.entry("policyType", "BRMS_Param"), Assertions.entry("policyComponent", "PDP"),
261 Assertions.entry("deleteCondition", "ALL"));
262 Assertions.assertThat(mapNodesPap).contains(
263 Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0"),
264 Assertions.entry("policyType", "BRMS_Param"), Assertions.entry("policyComponent", "PAP"),
265 Assertions.entry("deleteCondition", "ALL"));
269 public void testImportToscaModel() throws IOException {
270 String toscaModelYaml = ResourceFileUtil.getResourceAsString("tosca/tca-policy-test.yaml");
271 CldsToscaModel cldsToscaModel = new CldsToscaModel();
272 cldsToscaModel.setToscaModelName("tca-policy-test");
273 cldsToscaModel.setToscaModelYaml(toscaModelYaml);
274 cldsToscaModel.setUserId("admin");
275 cldsToscaModel.setPolicyType("tca");
276 cldsToscaModel = cldsToscaModel.save(cldsDao, refProp, policyClient, "test");
277 String tosca = policyClient.importToscaModel(cldsToscaModel);
279 Assertions.assertThat(tosca).contains(
280 "{\"serviceName\":\"tca-policy-test\",\"description\":\"tca-policy-test\","
281 + "\"requestID\":null,\"filePath\":\"/tmp/tosca-models/tca-policy-test.yml\",");
282 Assertions.assertThat(tosca).contains(toscaModelYaml);