Rework of the unit tests (mainly IT)
[clamp.git] / src / test / java / org / onap / clamp / clds / it / PropJsonBuilderIT.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 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  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.it;
25
26 import java.io.IOException;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.onap.clamp.clds.AbstractIT;
32 import org.onap.clamp.clds.client.req.SdcReq;
33 import org.onap.clamp.clds.model.CldsSdcServiceDetail;
34 import org.springframework.boot.test.context.SpringBootTest;
35 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
36 import org.springframework.test.context.junit4.SpringRunner;
37
38 import com.fasterxml.jackson.core.JsonProcessingException;
39 import com.fasterxml.jackson.databind.JsonNode;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41 import com.fasterxml.jackson.databind.node.ArrayNode;
42 import com.fasterxml.jackson.databind.node.ObjectNode;
43
44 /**
45  * Test SDC API - stand alone (except for some config). Replicates
46  * getSdcServices and getSdcServicesByUUID in the CldsService Adds test of
47  * putting putting an artifact to VF. TODO Also needs update and perhaps delete
48  * tests.
49  */
50 @RunWith(SpringRunner.class)
51 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
52 public class PropJsonBuilderIT extends AbstractIT {
53
54     private String       globalPropsPartial;
55     private ObjectMapper mapper;
56
57     @Before
58     public void setUp() throws IOException {
59         String url = refProp.getStringValue("sdc.serviceUrl");
60         String catalogUrl = refProp.getStringValue("sdc.catalog.url");
61         String basicAuth = SdcReq.getSdcBasicAuth(refProp);
62         System.out.println("value of string and basicAuth:" + url + basicAuth);
63         CldsSdcServiceDetail cldsservicedetail = new CldsSdcServiceDetail();
64         // cldsservicedetail.set
65         String globalProps = refProp.getStringValue("globalPropsTest");
66         globalPropsPartial = refProp.getStringValue("globalPropsPartialTest");
67         mapper = new ObjectMapper();
68     }
69
70     /**
71      * List services from SDC. List meta data for a particular service from SDC.
72      * Test uploading artifact to a VF in SDC.
73      */
74     @Test
75     public void testSdc() throws Exception {
76         // String createEmptySharedObject = createEmptySharedObject();
77         // System.out.println("value of emptySharedObject:" +
78         // createEmptySharedObject);
79         sampleJsonObject();
80         System.out.println(createTestEmptySharedObject());
81     }
82
83     private void sampleJsonObject() throws JsonProcessingException {
84         ArrayNode arrayNode = mapper.createArrayNode();
85
86         /**
87          * Create three JSON Objects objectNode1, objectNode2, objectNode3 Add
88          * all these three objects in the array
89          */
90
91         ObjectNode objectNode1 = mapper.createObjectNode();
92         objectNode1.put("bookName", "Java");
93         objectNode1.put("price", "100");
94
95         ObjectNode objectNode2 = mapper.createObjectNode();
96         objectNode2.put("bookName", "Spring");
97         objectNode2.put("price", "200");
98
99         ObjectNode objectNode3 = mapper.createObjectNode();
100         objectNode3.put("bookName", "Liferay");
101         objectNode3.put("price", "500");
102
103         /**
104          * Array contains JSON Objects
105          */
106         arrayNode.add(objectNode1);
107         arrayNode.add(objectNode2);
108         arrayNode.add(objectNode3);
109
110         /**
111          * We can directly write the JSON in the console. But it wont be pretty
112          * JSON String
113          */
114         System.out.println(arrayNode.toString());
115
116         /**
117          * To make the JSON String pretty use the below code
118          */
119         System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(arrayNode));
120     }
121
122     private String createEmptySharedObject() throws JsonProcessingException {
123
124         /**
125          * "": { "vf": { "": "" }, "location": { "": "" }, "alarmCondition": {
126          * "": "" } }
127          */
128         ObjectNode emptyObjectNode = mapper.createObjectNode();
129         emptyObjectNode.put("", "");
130         ObjectNode vfObjectNode = mapper.createObjectNode();
131         vfObjectNode.putPOJO("vf", emptyObjectNode);
132         ObjectNode locationObjectNode = mapper.createObjectNode();
133         locationObjectNode.putPOJO("location", emptyObjectNode);
134         ObjectNode alarmConditionObjectNode = mapper.createObjectNode();
135         alarmConditionObjectNode.putPOJO("alarmCondition", emptyObjectNode);
136         ObjectNode emptyServiceObjectNode = mapper.createObjectNode();
137         ArrayNode samArrayNode = mapper.createArrayNode();
138         samArrayNode.add(vfObjectNode);
139         samArrayNode.add(locationObjectNode);
140         samArrayNode.add(alarmConditionObjectNode);
141         emptyServiceObjectNode.putPOJO("", samArrayNode);
142
143         /**
144          * "vf": { " ": " ", "DCAE_CLAMP_DEMO3 1": "DCAE_CLAMP_DEMO3" }
145          *
146          */
147         ObjectNode vfObjectNode2 = mapper.createObjectNode();
148         ObjectNode dcaeClampDemo3Node = mapper.createObjectNode();
149         dcaeClampDemo3Node.put("DCAE_CLAMP_DEMO3", "DCAE_CLAMP_DEMO3");
150         ArrayNode vfArrayNode = mapper.createArrayNode();
151         vfArrayNode.add(emptyObjectNode);
152         vfArrayNode.add(dcaeClampDemo3Node);
153         vfObjectNode2.putPOJO("vf", vfArrayNode);
154
155         /**
156          * "location": { "SNDGCA64": "San Diego SAN3", "ALPRGAED":
157          * "Alpharetta PDK1", "LSLEILAA": "Lisle DPA3" },
158          */
159         ObjectNode locationObjectNode2 = mapper.createObjectNode();
160         ObjectNode sandiegoLocationNode = mapper.createObjectNode();
161         sandiegoLocationNode.put("SNDGCA64", "San Diego SAN3");
162         ObjectNode alpharettaNode = mapper.createObjectNode();
163         alpharettaNode.put("ALPRGAED", "Alpharetta PDK1");
164         ArrayNode locationArrayNode = mapper.createArrayNode();
165         locationArrayNode.add(emptyObjectNode);
166         locationArrayNode.add(sandiegoLocationNode);
167         locationArrayNode.add(alpharettaNode);
168         locationObjectNode2.putPOJO("location", locationArrayNode);
169
170         /**
171          * "alarmCondition": { "A+Fallback+Operation+will+soon+be+started":
172          * "A Fallback Operation will soon be started",
173          * "BRM%2C+Auto+Export+Backup+Failed": "BRM, Auto Export Backup Failed",
174          */
175         ObjectNode alarmConditionObjectNode2 = mapper.createObjectNode();
176         ObjectNode alamrCondition1 = mapper.createObjectNode();
177         alamrCondition1.put("A+Fallback+Operation+will+soon+be+started", "A Fallback Operation will soon be started");
178         ObjectNode alarmConditon2 = mapper.createObjectNode();
179         alarmConditon2.put("BRM%2C+Scheduled+Backup+Failed", "BRM, Scheduled Backup Failed");
180         ArrayNode alarmArrayNode = mapper.createArrayNode();
181         alarmArrayNode.add(emptyObjectNode);
182         alarmArrayNode.add(alamrCondition1);
183         alarmArrayNode.add(alarmConditon2);
184         alarmConditionObjectNode2.putPOJO("alarmCondition", alarmArrayNode);
185
186         ArrayNode byServiceIdArrayNode = mapper.createArrayNode();
187         byServiceIdArrayNode.add(vfObjectNode2);
188         byServiceIdArrayNode.add(locationObjectNode2);
189         byServiceIdArrayNode.add(alarmConditionObjectNode2);
190
191         ObjectNode byServiceIdNode = mapper.createObjectNode();
192         byServiceIdNode.putPOJO("c989a551-69f7-4b30-b10a-2e85bb227c30", byServiceIdArrayNode);
193
194         ArrayNode byServiceBasicArrayNode = mapper.createArrayNode();
195         byServiceBasicArrayNode.add(emptyServiceObjectNode);
196         byServiceBasicArrayNode.add(byServiceIdNode);
197
198         ObjectNode byServiceBasicObjetNode = mapper.createObjectNode();
199
200         byServiceBasicObjetNode.putPOJO("byService", byServiceBasicArrayNode);
201
202         /**
203          * "byVf": { "": { "vfc": { "": "" },
204          * "03596c12-c7e3-44b7-8994-5cdfeda8afdd": { "vfc": { " ": " " } } } }
205          */
206
207         ObjectNode byVfCBasicNode = mapper.createObjectNode();
208         ObjectNode emptyvfcobjectNode = mapper.createObjectNode();
209         ObjectNode vfCObjectNode = mapper.createObjectNode();
210         vfCObjectNode.putPOJO("vfC", emptyObjectNode);
211         ObjectNode vfcIdObjectNode = mapper.createObjectNode();
212         vfcIdObjectNode.putPOJO("03596c12-c7e3-44b7-8994-5cdfeda8afdd", vfCObjectNode);
213         ArrayNode emptyvfcArrayNode = mapper.createArrayNode();
214         emptyvfcArrayNode.add(vfCObjectNode);
215         emptyvfcArrayNode.add(vfcIdObjectNode);
216         emptyvfcobjectNode.putPOJO("", emptyvfcArrayNode);
217
218         byVfCBasicNode.putPOJO("byVf", emptyvfcobjectNode);
219
220         ArrayNode finalSharedArrayObject = mapper.createArrayNode();
221
222         finalSharedArrayObject.add(byServiceBasicObjetNode);
223         finalSharedArrayObject.add(byVfCBasicNode);
224
225         ObjectNode finalSharedObjectNode = mapper.createObjectNode();
226         finalSharedObjectNode.putPOJO("shared", finalSharedArrayObject);
227
228         System.out.println("value :" + finalSharedObjectNode.toString());
229         String testFinal = finalSharedObjectNode.toString();
230         testFinal = testFinal.replaceFirst("\\{", ",");
231         return globalPropsPartial + testFinal;
232     }
233
234     private String createTestEmptySharedObject() throws IOException {
235         String locationStringValue = refProp.getStringValue("ui.location.default");
236         String alarmStringValue = refProp.getStringValue("ui.alarm.default");
237
238         ObjectNode locationJsonNode = (ObjectNode) mapper.readValue(locationStringValue, JsonNode.class);
239         ObjectNode alarmStringJsonNode = (ObjectNode) mapper.readValue(alarmStringValue, JsonNode.class);
240         /**
241          * "": { "vf": { "": "" }, "location": { "": "" }, "alarmCondition": {
242          * "": "" } }
243          */
244         ObjectNode emptyObjectNode = mapper.createObjectNode();
245         emptyObjectNode.put("", "");
246         ObjectNode vfObjectNode = mapper.createObjectNode();
247         vfObjectNode.putPOJO("vf", emptyObjectNode);
248         vfObjectNode.putPOJO("location", emptyObjectNode);
249         vfObjectNode.putPOJO("alarmCondition", emptyObjectNode);
250         ObjectNode emptyServiceObjectNode = mapper.createObjectNode();
251         emptyServiceObjectNode.putPOJO("", vfObjectNode);
252
253         /**
254          * "vf": { " ": " ", "DCAE_CLAMP_DEMO3 1": "DCAE_CLAMP_DEMO3" }
255          *
256          */
257         ObjectNode vfObjectNode2 = mapper.createObjectNode();
258         ObjectNode dcaeClampDemo3Node = mapper.createObjectNode();
259         dcaeClampDemo3Node.put("", "");
260         dcaeClampDemo3Node.put("DCAE_CLAMP_DEMO3", "DCAE_CLAMP_DEMO3");
261         vfObjectNode2.putPOJO("vf", dcaeClampDemo3Node);
262
263         /**
264          * "location": { "SNDGCA64": "San Diego SAN3", "ALPRGAED":
265          * "Alpharetta PDK1", "LSLEILAA": "Lisle DPA3" },
266          */
267         // ObjectNode sandiegoLocationNode = mapper.createObjectNode();
268         // sandiegoLocationNode.put("SNDGCA64","San Diego SAN3");
269         // sandiegoLocationNode.put("ALPRGAED","Alpharetta PDK1");
270         vfObjectNode2.putPOJO("location", locationJsonNode);
271
272         /**
273          * "alarmCondition": { "A+Fallback+Operation+will+soon+be+started":
274          * "A Fallback Operation will soon be started",
275          * "BRM%2C+Auto+Export+Backup+Failed": "BRM, Auto Export Backup Failed",
276          */
277         // ObjectNode alamrCondition1 = mapper.createObjectNode();
278         // alamrCondition1.put("A+Fallback+Operation+will+soon+be+started","A
279         // Fallback Operation will soon be started");
280         // alamrCondition1.put("BRM%2C+Scheduled+Backup+Failed","BRM, Scheduled
281         // Backup Failed");
282         vfObjectNode2.putPOJO("alarmCondition", alarmStringJsonNode);
283         emptyServiceObjectNode.putPOJO("c989a551-69f7-4b30-b10a-2e85bb227c30", vfObjectNode2);
284         ObjectNode byServiceBasicObjetNode = mapper.createObjectNode();
285         byServiceBasicObjetNode.putPOJO("byService", emptyServiceObjectNode);
286
287         /**
288          * "byVf": { "": { "vfc": { "": "" },
289          * "03596c12-c7e3-44b7-8994-5cdfeda8afdd": { "vfc": { " ": " " } } } }
290          */
291
292         ObjectNode emptyvfcobjectNode = mapper.createObjectNode();
293         ObjectNode vfCObjectNode = mapper.createObjectNode();
294         vfCObjectNode.putPOJO("vfC", emptyObjectNode);
295         ObjectNode subVfCObjectNode = mapper.createObjectNode();
296         subVfCObjectNode.putPOJO("vfc", emptyObjectNode);
297         vfCObjectNode.putPOJO("03596c12-c7e3-44b7-8994-5cdfeda8afdd", subVfCObjectNode);
298         emptyvfcobjectNode.putPOJO("", vfCObjectNode);
299         byServiceBasicObjetNode.putPOJO("byVf", emptyvfcobjectNode);
300
301         ObjectNode readTree = (ObjectNode) mapper.readValue(globalPropsPartial, JsonNode.class);
302
303         readTree.putPOJO("shared", byServiceBasicObjetNode);
304         System.out.println("valuie of objNode:" + readTree);
305         return readTree.toString();
306     }
307
308     private String createCldsSharedObject(CldsSdcServiceDetail CldsSdcServiceDetail) throws IOException {
309         /**
310          * "": { "vf": { "": "" }, "location": { "": "" }, "alarmCondition": {
311          * "": "" } }
312          */
313         ObjectNode emptyObjectNode = mapper.createObjectNode();
314         emptyObjectNode.put("", "");
315         ObjectNode vfObjectNode = mapper.createObjectNode();
316         vfObjectNode.putPOJO("vf", emptyObjectNode);
317         vfObjectNode.putPOJO("location", emptyObjectNode);
318         vfObjectNode.putPOJO("alarmCondition", emptyObjectNode);
319         ObjectNode emptyServiceObjectNode = mapper.createObjectNode();
320         emptyServiceObjectNode.putPOJO("", vfObjectNode);
321
322         /**
323          * "vf": { " ": " ", "DCAE_CLAMP_DEMO3 1": "DCAE_CLAMP_DEMO3" }
324          *
325          */
326         ObjectNode vfObjectNode2 = mapper.createObjectNode();
327         ObjectNode dcaeClampDemo3Node = mapper.createObjectNode();
328         dcaeClampDemo3Node.put("", "");
329         dcaeClampDemo3Node.put("DCAE_CLAMP_DEMO3", "DCAE_CLAMP_DEMO3");
330         vfObjectNode2.putPOJO("vf", dcaeClampDemo3Node);
331
332         /**
333          * "location": { "SNDGCA64": "San Diego SAN3", "ALPRGAED":
334          * "Alpharetta PDK1", "LSLEILAA": "Lisle DPA3" },
335          */
336         ObjectNode sandiegoLocationNode = mapper.createObjectNode();
337         sandiegoLocationNode.put("SNDGCA64", "San Diego SAN3");
338         sandiegoLocationNode.put("ALPRGAED", "Alpharetta PDK1");
339         vfObjectNode2.putPOJO("location", sandiegoLocationNode);
340
341         /**
342          * "alarmCondition": { "A+Fallback+Operation+will+soon+be+started":
343          * "A Fallback Operation will soon be started",
344          * "BRM%2C+Auto+Export+Backup+Failed": "BRM, Auto Export Backup Failed",
345          */
346         ObjectNode alamrCondition1 = mapper.createObjectNode();
347         alamrCondition1.put("A+Fallback+Operation+will+soon+be+started", "A Fallback Operation will soon be started");
348         alamrCondition1.put("BRM%2C+Scheduled+Backup+Failed", "BRM, Scheduled Backup Failed");
349         vfObjectNode2.putPOJO("alarmCondition", alamrCondition1);
350         emptyServiceObjectNode.putPOJO("c989a551-69f7-4b30-b10a-2e85bb227c30", vfObjectNode2);
351         ObjectNode byServiceBasicObjetNode = mapper.createObjectNode();
352         byServiceBasicObjetNode.putPOJO("byService", emptyServiceObjectNode);
353
354         /**
355          * "byVf": { "": { "vfc": { "": "" },
356          * "03596c12-c7e3-44b7-8994-5cdfeda8afdd": { "vfc": { " ": " " } } } }
357          */
358
359         ObjectNode emptyvfcobjectNode = mapper.createObjectNode();
360         ObjectNode vfCObjectNode = mapper.createObjectNode();
361         vfCObjectNode.putPOJO("vfC", emptyObjectNode);
362         ObjectNode subVfCObjectNode = mapper.createObjectNode();
363         subVfCObjectNode.putPOJO("vfc", emptyObjectNode);
364         vfCObjectNode.putPOJO("03596c12-c7e3-44b7-8994-5cdfeda8afdd", subVfCObjectNode);
365         emptyvfcobjectNode.putPOJO("", vfCObjectNode);
366         byServiceBasicObjetNode.putPOJO("byVf", emptyvfcobjectNode);
367
368         ObjectNode readTree = (ObjectNode) mapper.readValue(globalPropsPartial, JsonNode.class);
369
370         readTree.putPOJO("shared", byServiceBasicObjetNode);
371         System.out.println("valuie of objNode:" + readTree);
372         return readTree.toString();
373     }
374 }