f849a6c06f152d7d4b3311c543ece997ba93c88a
[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.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;
58
59 /**
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.
63  */
64 @RunWith(SpringRunner.class)
65 @SpringBootTest
66 public class PolicyClientItCase {
67
68     @Autowired
69     private ClampProperties refProp;
70     @Autowired
71     private PolicyClient policyClient;
72     @Autowired
73     XslTransformer cldsBpmnTransformer;
74
75     String modelProp;
76     String modelName;
77     String controlName;
78     String modelBpmnPropJson;
79     ModelProperties prop;
80
81     /**
82      * Initialize Test.
83      *
84      * @throws TransformerException
85      */
86     @Before
87     public void setUp() throws IOException, TransformerException {
88         modelProp = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/model-properties.json");
89         modelName = "example-model06";
90         controlName = "ClosedLoop_FRWL_SIG_fad4dcae_e498_11e6_852e_0050568c4ccf";
91         modelBpmnPropJson = cldsBpmnTransformer.doXslTransformToString(
92             ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-template.xml"));
93         prop = new ModelProperties(modelName, controlName, CldsEvent.ACTION_SUBMIT, false, modelBpmnPropJson,
94             modelProp);
95     }
96
97     @Test
98     public void testSendGuardPolicy() throws TransformerException, IOException {
99         // Normally there is only one Guard
100         List<PolicyItem> policyItems = GuardPolicyAttributesConstructor
101             .getAllPolicyGuardsFromPolicyChain(prop.getType(Policy.class).getPolicyChains().get(0));
102         PolicyItem policyItem = policyItems.get(0);
103         prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
104         prop.setPolicyUniqueId(prop.getType(Policy.class).getPolicyChains().get(0).getPolicyId());
105         prop.setGuardUniqueId(policyItem.getId());
106         String response = policyClient.sendGuardPolicy(
107             GuardPolicyAttributesConstructor.formatAttributes(prop, policyItem), prop, LoggingUtils.getRequestId(),
108             policyItem);
109         Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
110             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
111         Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Decision"),
112             Assertions.entry("policyName",
113                 modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0_Guard_6TtHGPq"),
114             Assertions.entry("policyDescription", "from clds"), Assertions.entry("onapName", "PDPD"),
115             Assertions.entry("requestID", LoggingUtils.getRequestId()), Assertions.entry("ruleProvider", "GUARD_YAML"));
116
117         // Check Guard attributes
118         Assertions.assertThat((Map<String, Object>) mapNodes.get("attributes"))
119             .containsKey(AttributeType.MATCHING.name());
120         Assertions.assertThat(
121             (Map<String, Object>) ((Map<String, Object>) mapNodes.get("attributes")).get(AttributeType.MATCHING.name()))
122             .contains(Assertions.entry(GuardPolicyAttributesConstructor.ACTOR, "APPC"),
123                 Assertions.entry(GuardPolicyAttributesConstructor.CLNAME, controlName + "_0"),
124                 Assertions.entry(GuardPolicyAttributesConstructor.TIME_WINDOW, "10"));
125     }
126
127     @Test
128     public void testSendBrmsPolicy()
129         throws TransformerException, BuilderException, IllegalArgumentException, IOException {
130         Map<AttributeType, Map<String, String>> attributes = OperationalPolicyAttributesConstructor.formatAttributes(
131             refProp, prop, prop.getType(Policy.class).getId(), prop.getType(Policy.class).getPolicyChains().get(0));
132         String response = policyClient.sendBrmsPolicy(attributes, prop, LoggingUtils.getRequestId());
133
134         Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
135             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
136         Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
137             Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0"),
138             Assertions.entry("policyConfigType", PolicyConfigType.BRMS_PARAM.name()),
139             Assertions.entry("requestID", LoggingUtils.getRequestId()));
140
141         // Check BRMS attributes present
142         Assertions.assertThat((Map<String, Object>) mapNodes.get("attributes"))
143             .containsKeys(AttributeType.MATCHING.name(), AttributeType.RULE.name());
144
145     }
146
147     @Test
148     public void testSendMicroServiceInJson()
149         throws TransformerException, BuilderException, IllegalArgumentException, IOException {
150         prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
151         String jsonToSend = "{\"test\":\"test\"}";
152         String response = policyClient.sendMicroServiceInJson(jsonToSend, prop, LoggingUtils.getRequestId());
153
154         Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
155             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
156         Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
157             Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h"),
158             Assertions.entry("policyConfigType", PolicyConfigType.MicroService.name()),
159             Assertions.entry("requestID", LoggingUtils.getRequestId()),
160             Assertions.entry("configBodyType", PolicyType.JSON.name()), Assertions.entry("onapName", "DCAE"),
161             Assertions.entry("configBody", jsonToSend));
162
163     }
164
165     @Test
166     public void testSendBasePolicyInOther() throws IllegalArgumentException, IOException {
167         String body = "test";
168         String response = policyClient.sendBasePolicyInOther(body, "myPolicy", prop, LoggingUtils.getRequestId());
169         Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
170             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
171         Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
172             Assertions.entry("policyName", "myPolicy"),
173             Assertions.entry("policyConfigType", PolicyConfigType.Base.name()),
174             Assertions.entry("requestID", LoggingUtils.getRequestId()),
175             Assertions.entry("configBodyType", PolicyType.OTHER.name()), Assertions.entry("onapName", "DCAE"),
176             Assertions.entry("configBody", body));
177     }
178
179     @Test
180     public void testSendMicroServiceInOther() throws IllegalArgumentException, IOException {
181         Tca tca = prop.getType(Tca.class);
182         String tcaJson = TcaRequestFormatter.createPolicyJson(refProp, prop);
183         String response = policyClient.sendMicroServiceInOther(tcaJson, prop);
184
185         Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance()
186             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class);
187         Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"),
188             Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_TCA_1d13unw"),
189             Assertions.entry("policyConfigType", PolicyConfigType.MicroService.name()),
190             Assertions.entry("configBody", tcaJson), Assertions.entry("onapName", "DCAE"));
191     }
192
193     @Test
194     public void testDeleteMicrosService() throws IllegalArgumentException, IOException {
195         Tca tca = prop.getType(Tca.class);
196         prop.setCurrentModelElementId(tca.getId());
197         String[] responses = policyClient.deleteMicrosService(prop).split("\\}\\{");
198
199         // There are 2 responses appended to the result, one for PDP one for PAP !
200         Map<String, Object> mapNodesPdp = JacksonUtils.getObjectMapperInstance()
201             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(responses[0] + "}"), Map.class);
202         Map<String, Object> mapNodesPap = JacksonUtils.getObjectMapperInstance()
203             .convertValue(JacksonUtils.getObjectMapperInstance().readTree("{" + responses[1]), Map.class);
204
205         Assertions.assertThat(mapNodesPdp).contains(
206             Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_TCA_1d13unw"),
207             Assertions.entry("policyType", PolicyConfigType.MicroService.name()),
208             Assertions.entry("policyComponent", "PDP"), Assertions.entry("deleteCondition", "ALL"));
209
210         Assertions.assertThat(mapNodesPap).contains(
211             Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_TCA_1d13unw"),
212             Assertions.entry("policyType", PolicyConfigType.MicroService.name()),
213             Assertions.entry("policyComponent", "PAP"), Assertions.entry("deleteCondition", "ALL"));
214     }
215
216     @Test
217     public void testDeleteGuard() throws IllegalArgumentException, IOException {
218         List<PolicyItem> policyItems = GuardPolicyAttributesConstructor
219             .getAllPolicyGuardsFromPolicyChain(prop.getType(Policy.class).getPolicyChains().get(0));
220         prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
221         prop.setPolicyUniqueId(prop.getType(Policy.class).getPolicyChains().get(0).getPolicyId());
222         prop.setGuardUniqueId(policyItems.get(0).getId());
223         String[] responses = policyClient.deleteGuard(prop).split("\\}\\{");
224
225         // There are 2 responses appended to the result, one for PDP one for PAP !
226         Map<String, Object> mapNodesPdp = JacksonUtils.getObjectMapperInstance()
227             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(responses[0] + "}"), Map.class);
228         Map<String, Object> mapNodesPap = JacksonUtils.getObjectMapperInstance()
229             .convertValue(JacksonUtils.getObjectMapperInstance().readTree("{" + responses[1]), Map.class);
230
231         Assertions.assertThat(mapNodesPdp).contains(
232             Assertions.entry("policyName",
233                 modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0_Guard_6TtHGPq"),
234             Assertions.entry("policyType", "Decision"), Assertions.entry("policyComponent", "PDP"),
235             Assertions.entry("deleteCondition", "ALL"));
236         Assertions.assertThat(mapNodesPap).contains(
237             Assertions.entry("policyName",
238                 modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0_Guard_6TtHGPq"),
239             Assertions.entry("policyType", "Decision"), Assertions.entry("policyComponent", "PAP"),
240             Assertions.entry("deleteCondition", "ALL"));
241     }
242
243     @Test
244     public void testDeleteBrms() throws IllegalArgumentException, IOException {
245         prop.setPolicyUniqueId(prop.getType(Policy.class).getPolicyChains().get(0).getPolicyId());
246         prop.setCurrentModelElementId(prop.getType(Policy.class).getId());
247         String[] responses = policyClient.deleteBrms(prop).split("\\}\\{");
248
249         // There are 2 responses appended to the result, one for PDP one for PAP !
250         Map<String, Object> mapNodesPdp = JacksonUtils.getObjectMapperInstance()
251             .convertValue(JacksonUtils.getObjectMapperInstance().readTree(responses[0] + "}"), Map.class);
252         Map<String, Object> mapNodesPap = JacksonUtils.getObjectMapperInstance()
253             .convertValue(JacksonUtils.getObjectMapperInstance().readTree("{" + responses[1]), Map.class);
254
255         Assertions.assertThat(mapNodesPdp).contains(
256             Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0"),
257             Assertions.entry("policyType", "BRMS_Param"), Assertions.entry("policyComponent", "PDP"),
258             Assertions.entry("deleteCondition", "ALL"));
259         Assertions.assertThat(mapNodesPap).contains(
260             Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0"),
261             Assertions.entry("policyType", "BRMS_Param"), Assertions.entry("policyComponent", "PAP"),
262             Assertions.entry("deleteCondition", "ALL"));
263
264     }
265 }