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