Remove dead code
[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 com.google.gson.reflect.TypeToken;
28 import java.io.IOException;
29 import java.lang.reflect.Type;
30 import java.util.List;
31 import java.util.Map;
32
33 import javax.xml.transform.TransformerException;
34
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;
62
63 /**
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.
67  */
68 @RunWith(SpringRunner.class)
69 @SpringBootTest
70 public class PolicyClientItCase {
71
72     private static final Type MAP_OF_STRING_TO_OBJECT_TYPE = new TypeToken<Map<String, Object>>() {}.getType();
73
74     @Autowired
75     private CldsDao cldsDao;
76     @Autowired
77     private ClampProperties refProp;
78     @Autowired
79     private PolicyClient policyClient;
80     @Autowired
81     XslTransformer cldsBpmnTransformer;
82
83     String modelProp;
84     String modelName;
85     String controlName;
86     String modelBpmnPropJson;
87     ModelProperties prop;
88
89     /**
90      * Setup method.
91      * @throws IOException thrown if resources not found
92      * @throws TransformerException thrown if invalid xml given to transformation
93      */
94     @Before
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,
102             modelProp);
103     }
104
105     @Test
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(),
116             policyItem);
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"));
123
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"));
132     }
133
134     @Test
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());
139
140         Map<String, Object> mapNodes = JsonUtils.GSON.fromJson(response, MAP_OF_STRING_TO_OBJECT_TYPE);
141
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()));
146
147         // Check BRMS attributes present
148         Assertions.assertThat((Map<String, Object>) mapNodes.get("attributes"))
149             .containsKeys(AttributeType.MATCHING.name(), AttributeType.RULE.name());
150
151     }
152
153     @Test
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());
158
159         Map<String, Object> mapNodes = JsonUtils.GSON.fromJson(response, MAP_OF_STRING_TO_OBJECT_TYPE);
160
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));
167
168     }
169
170     @Test
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);
175
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));
182     }
183
184     @Test
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);
189
190         Map<String, Object> mapNodes = JsonUtils.GSON.fromJson(response, MAP_OF_STRING_TO_OBJECT_TYPE);
191
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"));
196     }
197
198     @Test
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("\\}\\{");
203
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);
209
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"));
214
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"));
219     }
220
221     @Test
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("\\}\\{");
229
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);
234
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"));
245     }
246
247     @Test
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("\\}\\{");
252
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);
257
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"));
266     }
267
268     @Test
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);
278
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);
283     }
284 }