Add tests
[clamp.git] / src / test / java / org / onap / clamp / clds / it / PolicyClientItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T 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  * Modifications copyright (c) 2018 Nokia
21  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.clds.it;
26
27 import java.io.IOException;
28 import java.util.List;
29 import java.util.Map;
30
31 import javax.xml.transform.TransformerException;
32
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.dao.CldsDao;
43 import org.onap.clamp.clds.model.CldsEvent;
44 import org.onap.clamp.clds.model.CldsToscaModel;
45 import org.onap.clamp.clds.model.properties.ModelProperties;
46 import org.onap.clamp.clds.model.properties.Policy;
47 import org.onap.clamp.clds.model.properties.PolicyItem;
48 import org.onap.clamp.clds.model.properties.Tca;
49 import org.onap.clamp.clds.transform.XslTransformer;
50 import org.onap.clamp.clds.util.JacksonUtils;
51 import org.onap.clamp.clds.util.LoggingUtils;
52 import org.onap.clamp.clds.util.ResourceFileUtil;
53 import org.onap.policy.api.AttributeType;
54 import org.onap.policy.api.PolicyConfigType;
55 import org.onap.policy.api.PolicyType;
56 import org.onap.policy.controlloop.policy.builder.BuilderException;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.boot.test.context.SpringBootTest;
59 import org.springframework.test.context.junit4.SpringRunner;
60
61 /**
62  * Test Policy API, this uses the emulator written in python that is started
63  * during the tests, It returns the payload sent in the policy queries so that
64  * it can be validated here.
65  */
66 @RunWith(SpringRunner.class)
67 @SpringBootTest
68 public class PolicyClientItCase {
69
70     @Autowired
71     private CldsDao cldsDao;
72     @Autowired
73     private ClampProperties refProp;
74     @Autowired
75     private PolicyClient policyClient;
76     @Autowired
77     XslTransformer cldsBpmnTransformer;
78
79     String modelProp;
80     String modelName;
81     String controlName;
82     String modelBpmnPropJson;
83     ModelProperties prop;
84
85     @Before
86     public void setUp() throws IOException, TransformerException {
87         modelProp = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/model-properties.json");
88         modelName = "example-model06";
89         controlName = "ClosedLoop_FRWL_SIG_fad4dcae_e498_11e6_852e_0050568c4ccf";
90         modelBpmnPropJson = cldsBpmnTransformer.doXslTransformToString(
91             ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-template.xml"));
92         prop = new ModelProperties(modelName, controlName, CldsEvent.ACTION_SUBMIT, false, modelBpmnPropJson,
93             modelProp);
94     }
95
96     @Test
97     public void testSendGuardPolicy() throws TransformerException, IOException {
98         // Normally there is only one Guard
99         List<PolicyItem> policyItems = GuardPolicyAttributesConstructor
100             .getAllPolicyGuardsFromPolicyChain(prop.getType(Policy.class).getPolicyChains().get(0));
101         PolicyItem policyItem = policyItems.get(0);
102         prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
103         prop.setPolicyUniqueId(prop.getType(Policy.class).getPolicyChains().get(0).getPolicyId());
104         prop.setGuardUniqueId(policyItem.getId());
105         String response = policyClient.sendGuardPolicy(
106             GuardPolicyAttributesConstructor.formatAttributes(prop, policyItem), prop, LoggingUtils.getRequestId(),
107             policyItem);
108         Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
109             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
110         Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Decision"),
111             Assertions.entry("policyName",
112                 modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0_Guard_6TtHGPq"),
113             Assertions.entry("policyDescription", "from clds"), Assertions.entry("onapName", "PDPD"),
114             Assertions.entry("requestID", LoggingUtils.getRequestId()), Assertions.entry("ruleProvider", "GUARD_YAML"));
115
116         // Check Guard attributes
117         Assertions.assertThat((Map<String, Object>) mapNodes.get("attributes"))
118             .containsKey(AttributeType.MATCHING.name());
119         Assertions.assertThat(
120             (Map<String, Object>) ((Map<String, Object>) mapNodes.get("attributes")).get(AttributeType.MATCHING.name()))
121             .contains(Assertions.entry(GuardPolicyAttributesConstructor.ACTOR, "APPC"),
122                 Assertions.entry(GuardPolicyAttributesConstructor.CLNAME, controlName + "_0"),
123                 Assertions.entry(GuardPolicyAttributesConstructor.TIME_WINDOW, "10"));
124     }
125
126     @Test
127     public void testSendBrmsPolicy()
128         throws TransformerException, BuilderException, IllegalArgumentException, IOException {
129         Map<AttributeType, Map<String, String>> attributes = OperationalPolicyAttributesConstructor.formatAttributes(
130             refProp, prop, prop.getType(Policy.class).getId(), prop.getType(Policy.class).getPolicyChains().get(0));
131         String response = policyClient.sendBrmsPolicy(attributes, prop, LoggingUtils.getRequestId());
132
133         Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
134             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
135         Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
136             Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0"),
137             Assertions.entry("policyConfigType", PolicyConfigType.BRMS_PARAM.name()),
138             Assertions.entry("requestID", LoggingUtils.getRequestId()));
139
140         // Check BRMS attributes present
141         Assertions.assertThat((Map<String, Object>) mapNodes.get("attributes"))
142             .containsKeys(AttributeType.MATCHING.name(), AttributeType.RULE.name());
143
144     }
145
146     @Test
147     public void testSendMicroServiceInJson()
148         throws TransformerException, BuilderException, IllegalArgumentException, IOException {
149         prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
150         String jsonToSend = "{\"test\":\"test\"}";
151         String response = policyClient.sendMicroServiceInJson(jsonToSend, prop, LoggingUtils.getRequestId());
152
153         Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
154             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
155         Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
156             Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h"),
157             Assertions.entry("policyConfigType", PolicyConfigType.MicroService.name()),
158             Assertions.entry("requestID", LoggingUtils.getRequestId()),
159             Assertions.entry("configBodyType", PolicyType.JSON.name()), Assertions.entry("onapName", "DCAE"),
160             Assertions.entry("configBody", jsonToSend));
161
162     }
163
164     @Test
165     public void testSendBasePolicyInOther() throws IllegalArgumentException, IOException {
166         String body = "test";
167         String response = policyClient.sendBasePolicyInOther(body, "myPolicy", prop, LoggingUtils.getRequestId());
168         Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
169             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
170         Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
171             Assertions.entry("policyName", "myPolicy"),
172             Assertions.entry("policyConfigType", PolicyConfigType.Base.name()),
173             Assertions.entry("requestID", LoggingUtils.getRequestId()),
174             Assertions.entry("configBodyType", PolicyType.OTHER.name()), Assertions.entry("onapName", "DCAE"),
175             Assertions.entry("configBody", body));
176     }
177
178     @Test
179     public void testSendMicroServiceInOther() throws IllegalArgumentException, IOException {
180         Tca tca = prop.getType(Tca.class);
181         String tcaJson = TcaRequestFormatter.createPolicyJson(refProp, prop);
182         String response = policyClient.sendMicroServiceInOther(tcaJson, prop);
183
184         Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
185             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
186         Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
187             Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_TCA_1d13unw"),
188             Assertions.entry("policyConfigType", PolicyConfigType.MicroService.name()),
189             Assertions.entry("configBody", tcaJson), Assertions.entry("onapName", "DCAE"));
190     }
191
192     @Test
193     public void testDeleteMicrosService() throws IllegalArgumentException, IOException {
194         Tca tca = prop.getType(Tca.class);
195         prop.setCurrentModelElementId(tca.getId());
196         String[] responses = policyClient.deleteMicrosService(prop).split("\\}\\{");
197
198         // There are 2 responses appended to the result, one for PDP one for PAP !
199         Map<String, Object> mapNodesPdp = JacksonUtils.getObjectMapperInstance()
200             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(responses[0] + "}"), Map.class);
201         Map<String, Object> mapNodesPap = JacksonUtils.getObjectMapperInstance()
202             .convertValue(JacksonUtils.getObjectMapperInstance().readTree("{" + responses[1]), Map.class);
203
204         Assertions.assertThat(mapNodesPdp).contains(
205             Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_TCA_1d13unw"),
206             Assertions.entry("policyType", PolicyConfigType.MicroService.name()),
207             Assertions.entry("policyComponent", "PDP"), Assertions.entry("deleteCondition", "ALL"));
208
209         Assertions.assertThat(mapNodesPap).contains(
210             Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_TCA_1d13unw"),
211             Assertions.entry("policyType", PolicyConfigType.MicroService.name()),
212             Assertions.entry("policyComponent", "PAP"), Assertions.entry("deleteCondition", "ALL"));
213     }
214
215     @Test
216     public void testDeleteGuard() throws IllegalArgumentException, IOException {
217         List<PolicyItem> policyItems = GuardPolicyAttributesConstructor
218             .getAllPolicyGuardsFromPolicyChain(prop.getType(Policy.class).getPolicyChains().get(0));
219         prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
220         prop.setPolicyUniqueId(prop.getType(Policy.class).getPolicyChains().get(0).getPolicyId());
221         prop.setGuardUniqueId(policyItems.get(0).getId());
222         String[] responses = policyClient.deleteGuard(prop).split("\\}\\{");
223
224         // There are 2 responses appended to the result, one for PDP one for PAP !
225         Map<String, Object> mapNodesPdp = JacksonUtils.getObjectMapperInstance()
226             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(responses[0] + "}"), Map.class);
227         Map<String, Object> mapNodesPap = JacksonUtils.getObjectMapperInstance()
228             .convertValue(JacksonUtils.getObjectMapperInstance().readTree("{" + responses[1]), Map.class);
229
230         Assertions.assertThat(mapNodesPdp).contains(
231             Assertions.entry("policyName",
232                 modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0_Guard_6TtHGPq"),
233             Assertions.entry("policyType", "Decision"), Assertions.entry("policyComponent", "PDP"),
234             Assertions.entry("deleteCondition", "ALL"));
235         Assertions.assertThat(mapNodesPap).contains(
236             Assertions.entry("policyName",
237                 modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0_Guard_6TtHGPq"),
238             Assertions.entry("policyType", "Decision"), Assertions.entry("policyComponent", "PAP"),
239             Assertions.entry("deleteCondition", "ALL"));
240     }
241
242     @Test
243     public void testDeleteBrms() throws IllegalArgumentException, IOException {
244         prop.setPolicyUniqueId(prop.getType(Policy.class).getPolicyChains().get(0).getPolicyId());
245         prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
246         String[] responses = policyClient.deleteBrms(prop).split("\\}\\{");
247
248         // There are 2 responses appended to the result, one for PDP one for PAP !
249         Map<String, Object> mapNodesPdp = JacksonUtils.getObjectMapperInstance()
250             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(responses[0] + "}"), Map.class);
251         Map<String, Object> mapNodesPap = JacksonUtils.getObjectMapperInstance()
252             .convertValue(JacksonUtils.getObjectMapperInstance().readTree("{" + responses[1]), Map.class);
253
254         Assertions.assertThat(mapNodesPdp).contains(
255             Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0"),
256             Assertions.entry("policyType", "BRMS_Param"), Assertions.entry("policyComponent", "PDP"),
257             Assertions.entry("deleteCondition", "ALL"));
258         Assertions.assertThat(mapNodesPap).contains(
259             Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0"),
260             Assertions.entry("policyType", "BRMS_Param"), Assertions.entry("policyComponent", "PAP"),
261             Assertions.entry("deleteCondition", "ALL"));
262     }
263
264     @Test
265     public void testImportToscaModel() throws IOException {
266         String toscaModelYaml = ResourceFileUtil.getResourceAsString("tosca/tca-policy-test.yaml");
267         CldsToscaModel cldsToscaModel = new CldsToscaModel();
268         cldsToscaModel.setToscaModelName("tca-policy-test");
269         cldsToscaModel.setToscaModelYaml(toscaModelYaml);
270         cldsToscaModel.setUserId("admin");
271         cldsToscaModel.setPolicyType("tca");
272         cldsToscaModel = cldsToscaModel.save(cldsDao, refProp, policyClient, "test");
273         String tosca = policyClient.importToscaModel(cldsToscaModel);
274
275         Assertions.assertThat(tosca).contains(
276             "{\"serviceName\":\"tca-policy-test\",\"description\":\"tca-policy-test\",\"requestID\":null,\"filePath\":\"/tmp/tosca-models/tca-policy-test.yml\",");
277         Assertions.assertThat(tosca).contains(toscaModelYaml);
278     }
279 }