Fixing some checkstyle issues.
[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.junit4.SpringRunner;
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     /**
58      * Initial test setup.
59      */
60     @Before
61     public void setUp() throws IOException {
62         String url = refProp.getStringValue("sdc.serviceUrl");
63         String catalogUrl = refProp.getStringValue("sdc.catalog.url");
64         String basicAuth = SdcReq.getSdcBasicAuth(refProp);
65         System.out.println("value of string and basicAuth:" + url + basicAuth);
66         CldsSdcServiceDetail cldsservicedetail = new CldsSdcServiceDetail();
67         // cldsservicedetail.set
68         String globalProps = refProp.getStringValue("globalPropsTest");
69         globalPropsPartial = refProp.getStringValue("globalPropsPartialTest");
70         mapper = new ObjectMapper();
71     }
72
73     /**
74      * List services from SDC. List meta data for a particular service from SDC.
75      * Test uploading artifact to a VF in SDC.
76      */
77     @Test
78     public void testSdc() throws Exception {
79         // String createEmptySharedObject = createEmptySharedObject();
80         // System.out.println("value of emptySharedObject:" +
81         // createEmptySharedObject);
82         sampleJsonObject();
83         System.out.println(createTestEmptySharedObject());
84     }
85
86     private void sampleJsonObject() throws JsonProcessingException {
87
88         /**
89          * Create three JSON Objects objectNode1, objectNode2, objectNode3.
90          * Add all these three objects in the array
91          */
92         ObjectNode objectNode1 = mapper.createObjectNode();
93         objectNode1.put("bookName", "Java");
94         objectNode1.put("price", "100");
95
96         ObjectNode objectNode2 = mapper.createObjectNode();
97         objectNode2.put("bookName", "Spring");
98         objectNode2.put("price", "200");
99
100         ObjectNode objectNode3 = mapper.createObjectNode();
101         objectNode3.put("bookName", "Liferay");
102         objectNode3.put("price", "500");
103
104         /**
105          * Array contains JSON Objects.
106          */
107         ArrayNode arrayNode = mapper.createArrayNode();
108         arrayNode.add(objectNode1);
109         arrayNode.add(objectNode2);
110         arrayNode.add(objectNode3);
111
112         /**
113          * We can directly write the JSON in the console. But it wont be pretty
114          * JSON String
115          */
116         System.out.println(arrayNode.toString());
117
118         /**
119          * To make the JSON String pretty use the below code.
120          */
121         System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(arrayNode));
122     }
123
124     private String createEmptySharedObject() throws JsonProcessingException {
125
126         /**
127          * Empty Object initialization.
128          * "": { "vf": { "": "" }, "location": { "": "" }, "alarmCondition": {
129          * "": "" } }
130          */
131         ObjectNode emptyObjectNode = mapper.createObjectNode();
132         emptyObjectNode.put("", "");
133         ObjectNode vfObjectNode = mapper.createObjectNode();
134         vfObjectNode.putPOJO("vf", emptyObjectNode);
135         ObjectNode locationObjectNode = mapper.createObjectNode();
136         locationObjectNode.putPOJO("location", emptyObjectNode);
137         ObjectNode alarmConditionObjectNode = mapper.createObjectNode();
138         alarmConditionObjectNode.putPOJO("alarmCondition", emptyObjectNode);
139         ArrayNode samArrayNode = mapper.createArrayNode();
140         samArrayNode.add(vfObjectNode);
141         samArrayNode.add(locationObjectNode);
142         samArrayNode.add(alarmConditionObjectNode);
143         ObjectNode emptyServiceObjectNode = mapper.createObjectNode();
144         emptyServiceObjectNode.putPOJO("", samArrayNode);
145
146         /**
147          * Object initialization.
148          * "vf": { " ": " ", "DCAE_CLAMP_DEMO3 1": "DCAE_CLAMP_DEMO3" }
149          *
150          */
151         ObjectNode dcaeClampDemo3Node = mapper.createObjectNode();
152         dcaeClampDemo3Node.put("DCAE_CLAMP_DEMO3", "DCAE_CLAMP_DEMO3");
153         ArrayNode vfArrayNode = mapper.createArrayNode();
154         vfArrayNode.add(emptyObjectNode);
155         vfArrayNode.add(dcaeClampDemo3Node);
156         ObjectNode vfObjectNode2 = mapper.createObjectNode();
157         vfObjectNode2.putPOJO("vf", vfArrayNode);
158
159         /**
160          * Object initialization.
161          * "location": { "SNDGCA64": "San Diego SAN3", "ALPRGAED":
162          * "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.
177          * "alarmCondition": { "A+Fallback+Operation+will+soon+be+started":
178          * "A Fallback Operation will soon be started",
179          * "BRM%2C+Auto+Export+Backup+Failed": "BRM, Auto 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.
210          * "byVf": { "": { "vfc": { "": "" },
211          * "03596c12-c7e3-44b7-8994-5cdfeda8afdd": { "vfc": { " ": " " } } } }
212          */
213         ObjectNode vfCObjectNode = mapper.createObjectNode();
214         vfCObjectNode.putPOJO("vfC", emptyObjectNode);
215         ObjectNode vfcIdObjectNode = mapper.createObjectNode();
216         vfcIdObjectNode.putPOJO("03596c12-c7e3-44b7-8994-5cdfeda8afdd", vfCObjectNode);
217         ArrayNode emptyvfcArrayNode = mapper.createArrayNode();
218         emptyvfcArrayNode.add(vfCObjectNode);
219         emptyvfcArrayNode.add(vfcIdObjectNode);
220         ObjectNode emptyvfcobjectNode = mapper.createObjectNode();
221         emptyvfcobjectNode.putPOJO("", emptyvfcArrayNode);
222
223         ObjectNode byVfCBasicNode = mapper.createObjectNode();
224         byVfCBasicNode.putPOJO("byVf", emptyvfcobjectNode);
225
226         ArrayNode finalSharedArrayObject = mapper.createArrayNode();
227
228         finalSharedArrayObject.add(byServiceBasicObjetNode);
229         finalSharedArrayObject.add(byVfCBasicNode);
230
231         ObjectNode finalSharedObjectNode = mapper.createObjectNode();
232         finalSharedObjectNode.putPOJO("shared", finalSharedArrayObject);
233
234         System.out.println("value :" + finalSharedObjectNode.toString());
235         String testFinal = finalSharedObjectNode.toString();
236         testFinal = testFinal.replaceFirst("\\{", ",");
237         return globalPropsPartial + testFinal;
238     }
239
240     private String createTestEmptySharedObject() throws IOException {
241
242         /**
243          * Object initialization.
244          * "": { "vf": { "": "" }, "location": { "": "" }, "alarmCondition": {
245          * "": "" } }
246          */
247         ObjectNode emptyObjectNode = mapper.createObjectNode();
248         emptyObjectNode.put("", "");
249         ObjectNode vfObjectNode = mapper.createObjectNode();
250         vfObjectNode.putPOJO("vf", emptyObjectNode);
251         vfObjectNode.putPOJO("location", emptyObjectNode);
252         vfObjectNode.putPOJO("alarmCondition", emptyObjectNode);
253         ObjectNode emptyServiceObjectNode = mapper.createObjectNode();
254         emptyServiceObjectNode.putPOJO("", vfObjectNode);
255
256         /**
257          * Object initialization.
258          * "vf": { " ": " ", "DCAE_CLAMP_DEMO3 1": "DCAE_CLAMP_DEMO3" }
259          *
260          */
261         ObjectNode vfObjectNode2 = mapper.createObjectNode();
262         ObjectNode dcaeClampDemo3Node = mapper.createObjectNode();
263         dcaeClampDemo3Node.put("", "");
264         dcaeClampDemo3Node.put("DCAE_CLAMP_DEMO3", "DCAE_CLAMP_DEMO3");
265         vfObjectNode2.putPOJO("vf", dcaeClampDemo3Node);
266
267         /**
268          * Object initialization.
269          * "location": { "SNDGCA64": "San Diego SAN3", "ALPRGAED":
270          * "Alpharetta PDK1", "LSLEILAA": "Lisle DPA3" },
271          */
272         // ObjectNode sandiegoLocationNode = mapper.createObjectNode();
273         // sandiegoLocationNode.put("SNDGCA64","San Diego SAN3");
274         // sandiegoLocationNode.put("ALPRGAED","Alpharetta PDK1");
275         String locationStringValue = refProp.getStringValue("ui.location.default");
276         ObjectNode locationJsonNode = (ObjectNode) mapper.readValue(locationStringValue, JsonNode.class);
277         vfObjectNode2.putPOJO("location", locationJsonNode);
278
279         /**
280          * Object initialization.
281          * "alarmCondition": { "A+Fallback+Operation+will+soon+be+started":
282          * "A Fallback Operation will soon be started",
283          * "BRM%2C+Auto+Export+Backup+Failed": "BRM, Auto Export Backup Failed",
284          */
285         // ObjectNode alamrCondition1 = mapper.createObjectNode();
286         // alamrCondition1.put("A+Fallback+Operation+will+soon+be+started","A
287         // Fallback Operation will soon be started");
288         // alamrCondition1.put("BRM%2C+Scheduled+Backup+Failed","BRM, Scheduled
289         // Backup Failed");
290         String alarmStringValue = refProp.getStringValue("ui.alarm.default");
291         ObjectNode alarmStringJsonNode = (ObjectNode) mapper.readValue(alarmStringValue, JsonNode.class);
292         vfObjectNode2.putPOJO("alarmCondition", alarmStringJsonNode);
293         emptyServiceObjectNode.putPOJO("c989a551-69f7-4b30-b10a-2e85bb227c30", vfObjectNode2);
294         ObjectNode byServiceBasicObjetNode = mapper.createObjectNode();
295         byServiceBasicObjetNode.putPOJO("byService", emptyServiceObjectNode);
296
297         /**
298          * Object initialization.
299          * "byVf": { "": { "vfc": { "": "" },
300          * "03596c12-c7e3-44b7-8994-5cdfeda8afdd": { "vfc": { " ": " " } } } }
301          */
302         ObjectNode vfCObjectNode = mapper.createObjectNode();
303         vfCObjectNode.putPOJO("vfC", emptyObjectNode);
304         ObjectNode subVfCObjectNode = mapper.createObjectNode();
305         subVfCObjectNode.putPOJO("vfc", emptyObjectNode);
306         vfCObjectNode.putPOJO("03596c12-c7e3-44b7-8994-5cdfeda8afdd", subVfCObjectNode);
307         ObjectNode emptyvfcobjectNode = mapper.createObjectNode();
308         emptyvfcobjectNode.putPOJO("", vfCObjectNode);
309         byServiceBasicObjetNode.putPOJO("byVf", emptyvfcobjectNode);
310
311         ObjectNode readTree = (ObjectNode) mapper.readValue(globalPropsPartial, JsonNode.class);
312         readTree.putPOJO("shared", byServiceBasicObjetNode);
313         System.out.println("valuie of objNode:" + readTree);
314         return readTree.toString();
315     }
316
317     private String createCldsSharedObject(CldsSdcServiceDetail CldsSdcServiceDetail) throws IOException {
318         /**
319          * Object initialization.
320          * "": { "vf": { "": "" }, "location": { "": "" }, "alarmCondition": {
321          * "": "" } }
322          */
323         ObjectNode emptyObjectNode = mapper.createObjectNode();
324         emptyObjectNode.put("", "");
325         ObjectNode vfObjectNode = mapper.createObjectNode();
326         vfObjectNode.putPOJO("vf", emptyObjectNode);
327         vfObjectNode.putPOJO("location", emptyObjectNode);
328         vfObjectNode.putPOJO("alarmCondition", emptyObjectNode);
329         ObjectNode emptyServiceObjectNode = mapper.createObjectNode();
330         emptyServiceObjectNode.putPOJO("", vfObjectNode);
331
332         /**
333          * Object initialization.
334          * "vf": { " ": " ", "DCAE_CLAMP_DEMO3 1": "DCAE_CLAMP_DEMO3" }
335          *
336          */
337         ObjectNode vfObjectNode2 = mapper.createObjectNode();
338         ObjectNode dcaeClampDemo3Node = mapper.createObjectNode();
339         dcaeClampDemo3Node.put("", "");
340         dcaeClampDemo3Node.put("DCAE_CLAMP_DEMO3", "DCAE_CLAMP_DEMO3");
341         vfObjectNode2.putPOJO("vf", dcaeClampDemo3Node);
342
343         /**
344          * Object initialization.
345          * "location": { "SNDGCA64": "San Diego SAN3", "ALPRGAED":
346          * "Alpharetta PDK1", "LSLEILAA": "Lisle DPA3" },
347          */
348         ObjectNode sandiegoLocationNode = mapper.createObjectNode();
349         sandiegoLocationNode.put("SNDGCA64", "San Diego SAN3");
350         sandiegoLocationNode.put("ALPRGAED", "Alpharetta PDK1");
351         vfObjectNode2.putPOJO("location", sandiegoLocationNode);
352
353         /**
354          * Object initialization.
355          * "alarmCondition": { "A+Fallback+Operation+will+soon+be+started":
356          * "A Fallback Operation will soon be started",
357          * "BRM%2C+Auto+Export+Backup+Failed": "BRM, Auto Export Backup Failed",
358          */
359         ObjectNode alamrCondition1 = mapper.createObjectNode();
360         alamrCondition1.put("A+Fallback+Operation+will+soon+be+started", "A Fallback Operation will soon be started");
361         alamrCondition1.put("BRM%2C+Scheduled+Backup+Failed", "BRM, Scheduled Backup Failed");
362         vfObjectNode2.putPOJO("alarmCondition", alamrCondition1);
363         emptyServiceObjectNode.putPOJO("c989a551-69f7-4b30-b10a-2e85bb227c30", vfObjectNode2);
364         ObjectNode byServiceBasicObjetNode = mapper.createObjectNode();
365         byServiceBasicObjetNode.putPOJO("byService", emptyServiceObjectNode);
366
367         /**
368          * Object initialization.
369          * "byVf": { "": { "vfc": { "": "" },
370          * "03596c12-c7e3-44b7-8994-5cdfeda8afdd": { "vfc": { " ": " " } } } }
371          */
372
373         ObjectNode vfCObjectNode = mapper.createObjectNode();
374         vfCObjectNode.putPOJO("vfC", emptyObjectNode);
375         ObjectNode subVfCObjectNode = mapper.createObjectNode();
376         subVfCObjectNode.putPOJO("vfc", emptyObjectNode);
377         vfCObjectNode.putPOJO("03596c12-c7e3-44b7-8994-5cdfeda8afdd", subVfCObjectNode);
378         ObjectNode emptyvfcobjectNode = mapper.createObjectNode();
379         emptyvfcobjectNode.putPOJO("", vfCObjectNode);
380         byServiceBasicObjetNode.putPOJO("byVf", emptyvfcobjectNode);
381
382         ObjectNode readTree = (ObjectNode) mapper.readValue(globalPropsPartial, JsonNode.class);
383
384         readTree.putPOJO("shared", byServiceBasicObjetNode);
385         System.out.println("valuie of objNode:" + readTree);
386         return readTree.toString();
387     }
388 }