2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.bpmn.core;
23 import java.io.BufferedReader;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
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;
37 import org.openecomp.mso.bpmn.core.json.JsonUtils;
38 import org.openecomp.mso.bpmn.core.xml.XmlTool;
43 public class JsonUtilsTest {
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;
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;
98 // JSON request w/ embedded XML will be read from a file
99 private String jsonReq = null;
102 public void initialize() {
103 File file = new File("src/test/resources/request.json");
104 FileInputStream fis = null;
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);
113 System.out.println("initialize(): failed to read json request from src/test/resources/request.json");
115 } catch (IOException e) {
121 } catch (IOException ex) {
122 ex.printStackTrace();
128 public void cleanup(){
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
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);
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);
154 diffXml = new Diff(xmlIn, xmlOut);
155 Assert.assertTrue(diffXml.similar());
156 // Assert.assertTrue(diffXml.identical());
157 } catch (SAXException e) {
159 } catch (IOException e) {
165 public void testRetrieval() {
166 String json = JsonUtils.xml2json(xmlReq);
167 System.out.println("testRetrieval(): xml request to json: " + json);
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);
192 value = JsonUtils.getJsonValueForKey(json, "source");
193 Assert.assertEquals(value, "PORTAL");
194 value = JsonUtils.getJsonValueForKey(json, "vnf-module");
195 Assert.assertEquals(value, null);
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);
228 public void testEmbededXmlRetrievalConversion() {
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);
242 // compare the XML before and after
243 diffXml = new Diff(xmlReq, xmlOut);
244 Assert.assertTrue(diffXml.similar());
245 } catch (SAXException e) {
247 } catch (IOException e) {
251 } catch (Exception e) {