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