Initial OpenECOMP MSO commit
[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 package org.openecomp.mso.bpmn.core;
22
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28
29 import org.junit.After;
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Ignore;
33 import org.junit.Test;
34 import org.xml.sax.SAXException;
35 import org.custommonkey.xmlunit.Diff;
36
37 import org.openecomp.mso.bpmn.core.json.JsonUtils;
38 import org.openecomp.mso.bpmn.core.xml.XmlTool;
39
40 /**
41  * @version 1.0
42  */
43 public class JsonUtilsTest {
44
45         private static final String EOL = "\n";
46         private String xmlReq =
47                         "<vnf-request xmlns=\"http://ecomp.att.com/mso/infra/vnf-request/v1\">" + EOL +
48                         "  <request-info>" + EOL +
49                         "    <request-id>DEV-VF-0021</request-id>" + EOL +
50                         "    <action>CREATE_VF_MODULE</action>" + EOL +
51                         "    <source>PORTAL</source>" + EOL +
52                         "  </request-info>" + EOL +
53                         "  <vnf-inputs>" + EOL +
54                         "    <vnf-id>a27ce5a9-29c4-4c22-a017-6615ac73c721</vnf-id>" + EOL +
55                         "    <vnf-name>STMTN5MMSC21</vnf-name>" + EOL +
56                         "    <vnf-type>asc_heat-int</vnf-type>" + EOL +
57                         "    <vf-module-name>STMTN5MMSC21-MMSC::module-0-0</vf-module-name>" + EOL +
58                         "    <vf-module-model-name>STMTN5MMSC21-MMSC::model-1-0</vf-module-model-name>" + EOL +
59                         "    <is-base-module>true</is-base-module>" + EOL +
60                         "    <persona-model-id>00000000-0000-0000-0000-000000000000</persona-model-id>" + EOL +
61                         "    <persona-model-version>1.0</persona-model-version>" + EOL +
62                         "    <vnf-persona-model-id>999999999-0000-0000-0000-000000000000</vnf-persona-model-id>" + EOL +
63                         "    <vnf-persona-model-version>1.5</vnf-persona-model-version>" + EOL +
64                         "    <service-id>00000000-0000-0000-0000-000000000000</service-id>" + EOL +
65                         "    <service-type>SDN-ETHERNET-INTERNET</service-type>" + EOL +
66                         "    <tenant-id>fba1bd1e195a404cacb9ce17a9b2b421</tenant-id>" + EOL +
67                         "    <orchestration-status>pending-delete</orchestration-status>" + EOL +
68                         "    <aic-cloud-region>RDM2WAGPLCP</aic-cloud-region>" + EOL +
69                         "    <asdc-service-model-version>1</asdc-service-model-version>" + EOL +
70                         "  </vnf-inputs>" + EOL +
71                         "  <vnf-params xmlns:tns=\"http://ecomp.att.com/mso/infra/vnf-request/v1\">" + EOL +
72                         "    <param name=\"network\">network1111</param>" + EOL +
73                         "    <param name=\"server\">server1111</param>" + EOL +
74                         "  </vnf-params> " + EOL +
75                         "</vnf-request>" + EOL;
76         
77         private String xmlReqNoAttrs =
78                         "<vnf-request xmlns=\"http://ecomp.att.com/mso/infra/vnf-request/v1\">" + EOL +
79                         "  <request-info>" + EOL +
80                         "    <action>DELETE_VF_MODULE</action>" + EOL +
81                         "    <source>PORTAL</source>" + EOL +
82                         "  </request-info>" + EOL +
83                         "  <vnf-inputs>" + EOL +
84                         "    <vnf-id>a27ce5a9-29c4-4c22-a017-6615ac73c721</vnf-id>" + EOL +
85                         "    <vnf-name>STMTN5MMSC21</vnf-name>" + EOL +
86                         "    <vnf-type>asc_heat-int</vnf-type>" + EOL +
87                         "    <vf-module-id>973ed047-d251-4fb9-bf1a-65b8949e0a73</vf-module-id>" + EOL +
88                         "    <vf-module-name>STMTN5MMSC21-MMSC::module-0-0</vf-module-name>" + EOL +
89                         "    <service-id>00000000-0000-0000-0000-000000000000</service-id>" + EOL +
90                         "    <service-type>SDN-ETHERNET-INTERNET</service-type>" + EOL +
91                         "    <tenant-id>fba1bd1e195a404cacb9ce17a9b2b421</tenant-id>" + EOL +
92                         "    <orchestration-status>pending-delete</orchestration-status>" + EOL +
93                         "    <aic-cloud-region>RDM2WAGPLCP</aic-cloud-region>" + EOL +
94                         "  </vnf-inputs>" + EOL +
95                         "  <vnf-params xmlns:tns=\"http://ecomp.att.com/mso/infra/vnf-request/v1\"/>" + EOL +
96                         "</vnf-request>" + EOL;
97
98         // JSON request w/ embedded XML will be read from a file
99         private String jsonReq = null;
100         
101         @Before
102         public void initialize() {
103                 File file = new File("src/test/resources/request.json");
104                 FileInputStream fis = null;
105
106                 try {
107                         fis = new FileInputStream(file);
108                         BufferedReader br = new BufferedReader(new InputStreamReader(fis));
109                         jsonReq = br.readLine();
110                         if (jsonReq != null) {
111                                 System.out.println("initialize(): json request: " + jsonReq);                           
112                         } else {
113                                 System.out.println("initialize(): failed to read json request from src/test/resources/request.json");
114                         }
115                 } catch (IOException e) {
116                         e.printStackTrace();
117                 } finally {
118                         try {
119                                 if (fis != null)
120                                         fis.close();
121                         } catch (IOException ex) {
122                                 ex.printStackTrace();
123                         }
124                 }
125         }
126         
127         @After
128         public void cleanup(){
129         }
130
131         @Test
132         public void testConversion() {
133                 // Note: the current version of the JsonUtils.json2xml() method
134                 // does not support converting the JSONObject representation
135                 // of XML attributes (JSONArray) back to XML. So this test will
136                 // only succeed if the original XML does not contain attributes
137                 
138                 // save a copy of the xml with the namespaces removed
139                 String xmlIn = XmlTool.removeNamespaces(xmlReqNoAttrs);
140                 // strip all the whitespace
141                 xmlIn = xmlIn.replaceAll("\\s+","");
142                 String json = JsonUtils.xml2json(xmlReqNoAttrs);
143                 System.out.println("testConversion(): xml request to json: " + json);
144                 String xmlOut = JsonUtils.json2xml(json);
145                 System.out.println("testConversion(): json request back to xml: " + xmlOut);
146                 
147                 // strip all the whitespace
148                 xmlOut = xmlOut.replaceAll("\\s+","");
149 //              System.out.println("testConversion(): xml in: " + xmlIn);
150 //              System.out.println("testConversion(): xml out: " + xmlOut);
151
152                 Diff diffXml;
153                 try {
154                         diffXml = new Diff(xmlIn, xmlOut);
155                         Assert.assertTrue(diffXml.similar());
156 //                      Assert.assertTrue(diffXml.identical());
157                 } catch (SAXException e) {
158                         e.printStackTrace();
159                 } catch (IOException e) {
160                         e.printStackTrace();
161                 }
162         }
163
164         @Test
165         public void testRetrieval() {
166                 String json = JsonUtils.xml2json(xmlReq);
167                 System.out.println("testRetrieval(): xml request to json: " + json);
168                 // full JSON path
169                 String value = JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.vnf-name");
170                 Assert.assertEquals(value, "STMTN5MMSC21");
171                 value = JsonUtils.getJsonValue(json, "vnf-request.request-info.action");
172                 Assert.assertEquals(value, "CREATE_VF_MODULE");
173                 // retrieving an integer
174                 value = JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.persona-model-version");
175                 Assert.assertEquals(value, "1");
176                 // retrieving a float
177                 value = JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.vnf-persona-model-version");
178                 Assert.assertEquals(value, "1.5");
179                 // retrieving a boolean
180                 value = JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.is-base-module");
181                 Assert.assertEquals(value, "true");
182                 // attempt to retrieve a value for a non-existent field
183                 value = JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.bad");
184                 Assert.assertEquals(value, null);
185                 // retrieving a parameter value (originally a XML attribute)
186                 value = JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "server");
187 //              Assert.assertEquals(value, "[{\"content\":\"network1111\",\"name\":\"network\"},{\"content\":\"server1111\",\"name\":\"server\"}]");
188                 Assert.assertEquals(value, "server1111");
189                 value = JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "badParam");
190                 Assert.assertEquals(value, null);
191                 // by field name/key
192                 value = JsonUtils.getJsonValueForKey(json, "source");
193                 Assert.assertEquals(value, "PORTAL");
194                 value = JsonUtils.getJsonValueForKey(json, "vnf-module");
195                 Assert.assertEquals(value, null);       
196         }
197
198         @Test
199         public void testUpdate() {
200                 String json = JsonUtils.xml2json(xmlReq);
201                 System.out.println("testUpdate(): xml request to json: " + json);
202                 // the add should be successful
203                 String jsonUpd = JsonUtils.addJsonValue(json, "vnf-request.request-info.comment", "Some comment");
204 //              System.out.println("testUpdate(): post add json request: " + jsonUpd);
205                 String value = JsonUtils.getJsonValue(jsonUpd, "vnf-request.request-info.comment");
206                 Assert.assertEquals(value, "Some comment");
207                 // the add should be ignored as the field already exists
208                 jsonUpd = JsonUtils.addJsonValue(jsonUpd, "vnf-request.vnf-inputs.vnf-name", "STMTN5MMSC22");
209                 value = JsonUtils.getJsonValue(jsonUpd, "vnf-request.vnf-inputs.vnf-name");
210                 Assert.assertEquals(value, "STMTN5MMSC21");
211                 // the update should be successful
212                 jsonUpd = JsonUtils.updJsonValue(jsonUpd, "vnf-request.vnf-inputs.vnf-name", "STMTN5MMSC22");
213 //              System.out.println("testUpdate(): post update json request: " + jsonUpd);
214                 value = JsonUtils.getJsonValue(jsonUpd, "vnf-request.vnf-inputs.vnf-name");
215                 Assert.assertEquals(value, "STMTN5MMSC22");
216                 // the delete should be successful
217                 jsonUpd = JsonUtils.delJsonValue(jsonUpd, "vnf-request.request-info.comment");
218 //              System.out.println("testUpdate(): post delete json request: " + jsonUpd);
219                 value = JsonUtils.getJsonValue(jsonUpd, "vnf-request.request-info.comment");
220                 Assert.assertEquals(value, null);
221                 // the delete should fail as field 'vnf-model' does not exist
222                 String jsonCur = jsonUpd;
223                 jsonUpd = JsonUtils.delJsonValue(jsonUpd, "vnf-request.vnf-inputs.vnf-module");
224                 Assert.assertEquals(jsonCur, jsonUpd);          
225         }
226         
227         @Test
228         public void testEmbededXmlRetrievalConversion() {
229                 try {
230                         // extract the embedded XML from the request
231                         String value = JsonUtils.getJsonValue(jsonReq, "variables.bpmnRequest.value");
232                         String xmlReq = XmlTool.removeNamespaces(XmlTool.normalize(value));
233                         System.out.println("testEmbededXmlRetrievalConversion(): xml payload: " + xmlReq);
234                         // strip all the whitespace
235 //                      xmlIn = xmlIn.replaceAll("\\s+","");
236                         String json = JsonUtils.xml2json(xmlReq);
237                         System.out.println("testEmbededXmlRetrievalConversion(): xml request to json: " + json);
238                         String xmlOut = JsonUtils.json2xml(json);
239                         System.out.println("testEmbededXmlRetrievalConversion(): json request back to xml: " + xmlOut);
240                         Diff diffXml;
241                         try {
242                                 // compare the XML before and after
243                                 diffXml = new Diff(xmlReq, xmlOut);
244                                 Assert.assertTrue(diffXml.similar());
245                         } catch (SAXException e) {
246                                 e.printStackTrace();
247                         } catch (IOException e) {
248                                 e.printStackTrace();
249                         }
250                         
251                 } catch (Exception e) {
252                         e.printStackTrace();
253                 }
254         }
255 }