d0a0fd9828f379267c755c9cf88150dbe46c900c
[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     @Before
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,
89             modelProp);
90     }
91
92     @Test
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(),
103             policyItem);
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"));
111
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"));
120     }
121
122     @Test
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());
128
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()));
135
136         // Check BRMS attributes present
137         Assertions.assertThat((Map<String, Object>) mapNodes.get("attributes"))
138             .containsKeys(AttributeType.MATCHING.name(), AttributeType.RULE.name());
139
140     }
141
142     @Test
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());
148
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));
157
158     }
159
160     @Test
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));
172     }
173
174     @Test
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);
179
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"));
186     }
187
188     @Test
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("\\}\\{");
193
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);
199
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"));
204
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"));
209     }
210
211     @Test
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("\\}\\{");
219
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);
225
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"));
236     }
237
238     @Test
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("\\}\\{");
243
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);
249
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"));
258
259     }
260 }