8a2b0a55782c47134d1dfd3752cee021dbe456ab
[so.git] / bpmn / MSOCoreBPMN / src / test / java / org / openecomp / mso / bpmn / core / JsonUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 /*- 
22  * ============LICENSE_START======================================================= 
23  * OPENECOMP - MSO 
24  * ================================================================================ 
25  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. 
26  * ================================================================================ 
27  * Licensed under the Apache License, Version 2.0 (the "License"); 
28  * you may not use this file except in compliance with the License. 
29  * You may obtain a copy of the License at 
30  * 
31  *      http://www.apache.org/licenses/LICENSE-2.0 
32  * 
33  * Unless required by applicable law or agreed to in writing, software 
34  * distributed under the License is distributed on an "AS IS" BASIS, 
35  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
36  * See the License for the specific language governing permissions and 
37  * limitations under the License. 
38  * ============LICENSE_END========================================================= 
39  */ 
40
41 package org.openecomp.mso.bpmn.core;
42
43 import java.io.BufferedReader;
44 import java.io.File;
45 import java.io.FileInputStream;
46 import java.io.IOException;
47 import java.io.InputStreamReader;
48 import java.nio.CharBuffer;
49
50 import org.junit.After;
51 import org.junit.Assert;
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.xml.sax.SAXException;
55 import org.custommonkey.xmlunit.Diff;
56
57 import org.openecomp.mso.bpmn.core.json.JsonUtils;
58 import org.openecomp.mso.bpmn.core.xml.XmlTool;
59
60 /**
61  * @version 1.0
62  */
63 public class JsonUtilsTest {
64
65     private static final String EOL = "\n";
66     private String xmlReq =
67                     "<vnf-request xmlns=\"http://org.openecomp/mso/infra/vnf-request/v1\">" + EOL +
68                     "  <request-info>" + EOL +
69                     "    <request-id>DEV-VF-0021</request-id>" + EOL +
70                     "    <action>CREATE_VF_MODULE</action>" + EOL +
71                     "    <source>PORTAL</source>" + EOL +
72                     "  </request-info>" + EOL +
73                     "  <vnf-inputs>" + EOL +
74                     "    <vnf-id>a27ce5a9-29c4-4c22-a017-6615ac73c721</vnf-id>" + EOL +
75                     "    <vnf-name>STMTN5MMSC21</vnf-name>" + EOL +
76                     "    <vnf-type>asc_heat-int</vnf-type>" + EOL +
77                     "    <vf-module-name>STMTN5MMSC21-MMSC::module-0-0</vf-module-name>" + EOL +
78                     "    <vf-module-model-name>STMTN5MMSC21-MMSC::model-1-0</vf-module-model-name>" + EOL +
79                     "    <is-base-module>true</is-base-module>" + EOL +
80                     "    <persona-model-id>00000000-0000-0000-0000-000000000000</persona-model-id>" + EOL +
81                     "    <persona-model-version>1.0</persona-model-version>" + EOL +
82                     "    <vnf-persona-model-id>999999999-0000-0000-0000-000000000000</vnf-persona-model-id>" + EOL +
83                     "    <vnf-persona-model-version>1.5</vnf-persona-model-version>" + EOL +
84                     "    <service-id>00000000-0000-0000-0000-000000000000</service-id>" + EOL +
85                     "    <service-type>SDN-ETHERNET-INTERNET</service-type>" + EOL +
86                     "    <tenant-id>fba1bd1e195a404cacb9ce17a9b2b421</tenant-id>" + EOL +
87                     "    <orchestration-status>pending-delete</orchestration-status>" + EOL +
88                     "    <aic-cloud-region>RDM2WAGPLCP</aic-cloud-region>" + EOL +
89                     "    <asdc-service-model-version>1</asdc-service-model-version>" + EOL +
90                     "  </vnf-inputs>" + EOL +
91                     "  <vnf-params xmlns:tns=\"http://org.openecomp/mso/infra/vnf-request/v1\">" + EOL +
92                     "    <param name=\"network\">network1111</param>" + EOL +
93                     "    <param name=\"server\">server1111</param>" + EOL +
94                     "  </vnf-params> " + EOL +
95                     "</vnf-request>" + EOL;
96     
97     private String xmlReqNoAttrs =
98                     "<vnf-request xmlns=\"http://org.openecomp/mso/infra/vnf-request/v1\">" + EOL +
99                     "  <request-info>" + EOL +
100                     "    <action>DELETE_VF_MODULE</action>" + EOL +
101                     "    <source>PORTAL</source>" + EOL +
102                     "  </request-info>" + EOL +
103                     "  <vnf-inputs>" + EOL +
104                     "    <vnf-id>a27ce5a9-29c4-4c22-a017-6615ac73c721</vnf-id>" + EOL +
105                     "    <vnf-name>STMTN5MMSC21</vnf-name>" + EOL +
106                     "    <vnf-type>asc_heat-int</vnf-type>" + EOL +
107                     "    <vf-module-id>973ed047-d251-4fb9-bf1a-65b8949e0a73</vf-module-id>" + EOL +
108                     "    <vf-module-name>STMTN5MMSC21-MMSC::module-0-0</vf-module-name>" + EOL +
109                     "    <service-id>00000000-0000-0000-0000-000000000000</service-id>" + EOL +
110                     "    <service-type>SDN-ETHERNET-INTERNET</service-type>" + EOL +
111                     "    <tenant-id>fba1bd1e195a404cacb9ce17a9b2b421</tenant-id>" + EOL +
112                     "    <orchestration-status>pending-delete</orchestration-status>" + EOL +
113                     "    <aic-cloud-region>RDM2WAGPLCP</aic-cloud-region>" + EOL +
114                     "  </vnf-inputs>" + EOL +
115                     "  <vnf-params xmlns:tns=\"http://org.openecomp/mso/infra/vnf-request/v1\"/>" + EOL +
116                     "</vnf-request>" + EOL;
117
118     private String xmlArrayReq =
119                     "<ucpeInfo>" + EOL +
120                     "       <outOfBandManagementModem>BROADBAND</outOfBandManagementModem>" + EOL +
121                 "   <internetTopology>IVLAN</internetTopology>" + EOL +
122                 "   <ucpeAliasHostName>SHELLUCPE31</ucpeAliasHostName>" + EOL +
123                 "   <wanList>" + EOL +
124                 "           <wanInfo>" + EOL +
125                 "                   <wanType>AVPN</wanType>" + EOL +
126                 "                   <interfaceType>1000BASE-T</interfaceType>" + EOL +
127                 "                   <transportProviderName>ATT</transportProviderName>" + EOL +
128                 "                   <circuitId>BT/SLIR/70911</circuitId>" + EOL +
129                 "                   <dualMode>Active</dualMode>" + EOL +
130                 "                   <wanPortNumber>WAN1</wanPortNumber>" + EOL +
131                 "                   <transportManagementOption>ATT</transportManagementOption>" + EOL +
132                 "                   <transportVendorTotalBandwidth>100</transportVendorTotalBandwidth>" + EOL +
133                 "                   <mediaType>ELECTRICAL</mediaType>" + EOL +
134                 "           </wanInfo>" + EOL +
135                 "           <wanInfo>" + EOL +
136                 "                   <wanType>AVPN</wanType>" + EOL +
137                 "                   <interfaceType>10/100/1000BASE-T</interfaceType>" + EOL +
138                 "                   <transportProviderName>ATT</transportProviderName>" + EOL +
139                 "                   <circuitId>AS/KRFN/34611</circuitId>" + EOL +
140                 "                   <dualMode>Active</dualMode>" + EOL +
141                 "                   <wanPortNumber>WAN2</wanPortNumber>" + EOL +
142                 "                   <transportManagementOption>ATT</transportManagementOption>" + EOL +
143                 "                   <transportVendorTotalBandwidth>10000</transportVendorTotalBandwidth>" + EOL +
144                 "                   <mediaType>MMF</mediaType>" + EOL +
145                 "           </wanInfo>" + EOL +
146                 "   </wanList>" + EOL +
147                 "   <ucpeActivationCode>ASD-987-M31</ucpeActivationCode>" + EOL +
148                 "   <ucpeHostName>USOSTCDALTX0101UJZZ31</ucpeHostName>" + EOL +
149                 "   <ucpePartNumber>FG-VM00*</ucpePartNumber>" + EOL +
150                     "</ucpeInfo>";
151
152     // JSON request w/ embedded XML will be read from a file
153     private String jsonReq = null;
154     private String jsonReqArray = null;
155     
156     @Before
157     public void initialize() {
158             File file = new File("src/test/resources/request.json");
159             File file2 = new File("src/test/resources/requestArray.json");
160             FileInputStream fis = null;
161
162             try {
163                     fis = new FileInputStream(file);
164                     BufferedReader br = new BufferedReader(new InputStreamReader(fis));
165 //                  jsonReq = br.readLine();
166                     CharBuffer cbuf = CharBuffer.allocate((int)file.length()+1);
167                     br.read(cbuf);
168                     cbuf.flip();
169                     jsonReq = cbuf.toString();
170                     if (jsonReq != null) {
171                             System.out.println("initialize(): json request: " + jsonReq);                           
172                     } else {
173                             System.out.println("initialize(): failed to read json request from src/test/resources/request.json");
174                     }
175                     fis.close();
176                     fis = new FileInputStream(file2);
177                     br = new BufferedReader(new InputStreamReader(fis));
178 //                  jsonReqArray = br.readLine();
179                     cbuf = CharBuffer.allocate((int)file.length()+1);
180                     br.read(cbuf);
181                     cbuf.flip();
182                     jsonReqArray = cbuf.toString();
183                     if (jsonReq != null) {
184                             System.out.println("initialize(): json request w/ array: " + jsonReqArray);                             
185                     } else {
186                             System.out.println("initialize(): failed to read json request from src/test/resources/request2.json");
187                     }
188             } catch (IOException e) {
189                     e.printStackTrace();
190             } finally {
191                     try {
192                             if (fis != null)
193                                     fis.close();
194                     } catch (IOException ex) {
195                             ex.printStackTrace();
196                     }
197             }
198     }
199     
200     @After
201     public void cleanup(){
202     }
203
204     @Test
205 //  @Ignore
206     public void testConversion() {
207             // Note: the current version of the JsonUtils.json2xml() method
208             // does not support converting the JSONObject representation
209             // of XML attributes (JSONArray) back to XML. So this test will
210             // only succeed if the original XML does not contain attributes
211             
212             // save a copy of the xml with the namespaces removed
213             String xmlIn = XmlTool.removeNamespaces(xmlReqNoAttrs);
214             // strip all the non-data whitespace
215             xmlIn = xmlIn.replaceAll(">\\s*<", "><");
216             String json = JsonUtils.xml2json(xmlReqNoAttrs);
217             System.out.println("testConversion(): xml request to json: " + json);
218             String xmlOut = JsonUtils.json2xml(json);
219             System.out.println("testConversion(): json request back to xml: " + xmlOut);
220             
221             // strip all the non-data whitespace
222             xmlOut = xmlOut.replaceAll(">\\s*<", "><");
223 //          System.out.println("testConversion(): xml in: " + xmlIn);
224 //          System.out.println("testConversion(): xml out: " + xmlOut);
225
226             Diff diffXml;
227             try {
228                     diffXml = new Diff(xmlIn, xmlOut);
229                     Assert.assertTrue(diffXml.similar());
230 //                  Assert.assertTrue(diffXml.identical());
231             } catch (SAXException e) {
232                     e.printStackTrace();
233             } catch (IOException e) {
234                     e.printStackTrace();
235             }
236     }
237
238     @Test
239 //  @Ignore
240     public void testRetrieval() {
241             String json = JsonUtils.xml2json(xmlReq);
242             System.out.println("testRetrieval(): xml request to json: " + json);
243             // full JSON path
244             String value = JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.vnf-name");
245             Assert.assertEquals(value, "STMTN5MMSC21");
246             value = JsonUtils.getJsonValue(json, "vnf-request.request-info.action");
247             Assert.assertEquals(value, "CREATE_VF_MODULE");
248             // retrieving an integer
249             value = JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.persona-model-version");
250             Assert.assertEquals(value, "1");
251             // retrieving a float
252             value = JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.vnf-persona-model-version");
253             Assert.assertEquals(value, "1.5");
254             // retrieving a boolean
255             value = JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.is-base-module");
256             Assert.assertEquals(value, "true");
257             // attempt to retrieve a value for a non-existent field
258             value = JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.bad");
259             Assert.assertEquals(value, null);
260             // retrieving a parameter/array value (originally a XML attribute)
261             value = JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "name");
262 //          Assert.assertEquals(value, "[{\"content\":\"network1111\",\"name\":\"network\"},{\"content\":\"server1111\",\"name\":\"server\"}]");
263             Assert.assertEquals(value, "network");
264             value = JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "content");
265             Assert.assertEquals(value, "network1111");
266             // retrieving a parameter/array value by index
267             value = JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "content", 1);
268             Assert.assertEquals(value, "server1111");
269             value = JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "name", 1);
270             Assert.assertEquals(value, "server");
271             // failure due to invalid parameter name
272             value = JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "badParam");
273             Assert.assertEquals(value, null);
274             // failure due to array index out of bounds
275             value = JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "name", 2);
276             Assert.assertEquals(value, null);
277             // by field name/key
278             value = JsonUtils.getJsonValueForKey(json, "source");
279             Assert.assertEquals(value, "PORTAL");
280             value = JsonUtils.getJsonValueForKey(json, "vnf-module");
281             Assert.assertEquals(value, null);       
282     }
283
284     @Test
285 //  @Ignore
286     public void testUpdate() {
287             String json = JsonUtils.xml2json(xmlReq);
288             System.out.println("testUpdate(): xml request to json: " + json);
289             // the add should be successful
290             String jsonUpd = JsonUtils.addJsonValue(json, "vnf-request.request-info.comment", "Some comment");
291 //          System.out.println("testUpdate(): post add json request: " + jsonUpd);
292             String value = JsonUtils.getJsonValue(jsonUpd, "vnf-request.request-info.comment");
293             Assert.assertEquals(value, "Some comment");
294             // the add should be ignored as the field already exists
295             jsonUpd = JsonUtils.addJsonValue(jsonUpd, "vnf-request.vnf-inputs.vnf-name", "STMTN5MMSC22");
296             value = JsonUtils.getJsonValue(jsonUpd, "vnf-request.vnf-inputs.vnf-name");
297             Assert.assertEquals(value, "STMTN5MMSC21");
298             // the update should be successful
299             jsonUpd = JsonUtils.updJsonValue(jsonUpd, "vnf-request.vnf-inputs.vnf-name", "STMTN5MMSC22");
300 //          System.out.println("testUpdate(): post update json request: " + jsonUpd);
301             value = JsonUtils.getJsonValue(jsonUpd, "vnf-request.vnf-inputs.vnf-name");
302             Assert.assertEquals(value, "STMTN5MMSC22");
303             // the delete should be successful
304             jsonUpd = JsonUtils.delJsonValue(jsonUpd, "vnf-request.request-info.comment");
305 //          System.out.println("testUpdate(): post delete json request: " + jsonUpd);
306             value = JsonUtils.getJsonValue(jsonUpd, "vnf-request.request-info.comment");
307             Assert.assertEquals(value, null);
308             // the delete should fail as field 'vnf-model' does not exist
309             String jsonCur = jsonUpd;
310             jsonUpd = JsonUtils.delJsonValue(jsonUpd, "vnf-request.vnf-inputs.vnf-module");
311             Assert.assertEquals(jsonCur, jsonUpd);          
312     }
313     
314     @Test
315 //  @Ignore
316     public void testEmbededXmlRetrievalConversion() {
317             try {
318                     // extract the embedded XML from the request
319                     String value = JsonUtils.getJsonValue(jsonReq, "variables.bpmnRequest.value");
320                     String xmlReq = XmlTool.removeNamespaces(XmlTool.normalize(value));
321                     System.out.println("testEmbededXmlRetrievalConversion(): xml payload: " + xmlReq);
322                     String json = JsonUtils.xml2json(xmlReq);
323                     System.out.println("testEmbededXmlRetrievalConversion(): xml request to json: " + json);
324                     String xmlOut = JsonUtils.json2xml(json);
325                     System.out.println("testEmbededXmlRetrievalConversion(): json request back to xml: " + xmlOut);
326                     Diff diffXml;
327                     try {
328                             // compare the XML before and after
329                             diffXml = new Diff(xmlReq, xmlOut);
330                             Assert.assertTrue(diffXml.similar());
331                     } catch (SAXException e) {
332                             e.printStackTrace();
333                     } catch (IOException e) {
334                             e.printStackTrace();
335                     }
336                     
337             } catch (Exception e) {
338                     e.printStackTrace();
339             }
340     }
341
342     @Test
343 //  @Ignore
344     // Tests the conversion of a JSON Doc containing a JSON Array to XML
345     public void testConversionArray() {
346             try {
347                     String jsonParm = JsonUtils.getJsonNodeValue(jsonReqArray, "requestDetails.requestParameters.ucpeInfo");
348                     System.out.println("testConversionArray(): json value: " + JsonUtils.prettyJson(jsonParm));
349                     String xmlOut = JsonUtils.json2xml(jsonParm);
350                     System.out.println("testConversionArray(): json parameters to xml: " + xmlOut);
351                     // strip all the non-data whitespace
352                     xmlOut = xmlOut.replaceAll(">\\s*<", "><");
353                     System.out.println("testConversionArray(): XML after removing whitespace:" + xmlOut);
354                     String xmlTest = xmlArrayReq.replaceAll(">\\s*<", "><");
355 //                  System.out.println("testConversion(): xml test: " + xmlTest);
356 //                  System.out.println("testConversion(): xml out: " + xmlOut);
357
358                     Diff diffXml;
359                     try {
360                             diffXml = new Diff(xmlTest, xmlOut);
361                           Assert.assertTrue(diffXml.similar());
362 //                            Assert.assertTrue(diffXml.identical());
363                     } catch (SAXException e) {
364                             e.printStackTrace();
365                     } catch (IOException e) {
366                             e.printStackTrace();
367                     }
368             } catch (Exception e) {
369                     e.printStackTrace();
370             }
371     }
372 }