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.onap.so.bpmn.core.json;
24 import static org.junit.Assert.*;
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import org.json.JSONObject;
30 import org.junit.Assert;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.so.bpmn.core.json.JsonUtils;
34 import org.onap.so.bpmn.core.xml.XmlTool;
35 import org.onap.so.exceptions.ValidationException;
36 import org.xmlunit.builder.DiffBuilder;
37 import org.xmlunit.diff.DefaultNodeMatcher;
38 import org.xmlunit.diff.Diff;
39 import org.xmlunit.diff.ElementSelectors;
44 public class JsonUtils2Test {
46 private static final String EOL = "\n";
47 private static final String XML_REQ = "<vnf-request xmlns=\"http://org.onap/so/infra/vnf-request/v1\">" + EOL
48 + " <request-info>" + EOL + " <request-id>DEV-VF-0021</request-id>" + EOL
49 + " <action>CREATE_VF_MODULE</action>" + EOL + " <source>PORTAL</source>" + EOL + " </request-info>"
50 + EOL + " <vnf-inputs>" + EOL + " <vnf-id>a27ce5a9-29c4-4c22-a017-6615ac73c721</vnf-id>" + EOL
51 + " <vnf-name>STMTN5MMSC21</vnf-name>" + EOL + " <vnf-type>asc_heat-int</vnf-type>" + EOL
52 + " <vf-module-name>STMTN5MMSC21-MMSC::module-0-0</vf-module-name>" + EOL
53 + " <vf-module-model-name>STMTN5MMSC21-MMSC::model-1-0</vf-module-model-name>" + EOL
54 + " <is-base-module>true</is-base-module>" + EOL
55 + " <persona-model-id>00000000-0000-0000-0000-000000000000</persona-model-id>" + EOL
56 + " <persona-model-version>1.0</persona-model-version>" + EOL
57 + " <vnf-persona-model-id>999999999-0000-0000-0000-000000000000</vnf-persona-model-id>" + EOL
58 + " <vnf-persona-model-version>1.5</vnf-persona-model-version>" + EOL
59 + " <service-id>00000000-0000-0000-0000-000000000000</service-id>" + EOL
60 + " <service-type>SDN-ETHERNET-INTERNET</service-type>" + EOL
61 + " <tenant-id>fba1bd1e195a404cacb9ce17a9b2b421</tenant-id>" + EOL
62 + " <orchestration-status>pending-delete</orchestration-status>" + EOL
63 + " <aic-cloud-region>RDM2WAGPLCP</aic-cloud-region>" + EOL
64 + " <asdc-service-model-version>1</asdc-service-model-version>" + EOL + " </vnf-inputs>" + EOL
65 + " <vnf-params xmlns:tns=\"http://org.onap/so/infra/vnf-request/v1\">" + EOL
66 + " <param name=\"network\">network1111</param>" + EOL + " <param name=\"server\">server1111</param>"
67 + EOL + " </vnf-params> " + EOL + "</vnf-request>" + EOL;
69 private static final String XML_REQ_NO_ATTRS = "<vnf-request xmlns=\"http://org.onap/so/infra/vnf-request/v1\">"
70 + EOL + " <request-info>" + EOL + " <action>DELETE_VF_MODULE</action>" + EOL
71 + " <source>PORTAL</source>" + EOL + " </request-info>" + EOL + " <vnf-inputs>" + EOL
72 + " <vnf-id>a27ce5a9-29c4-4c22-a017-6615ac73c721</vnf-id>" + EOL
73 + " <vnf-name>STMTN5MMSC21</vnf-name>" + EOL + " <vnf-type>asc_heat-int</vnf-type>" + EOL
74 + " <vf-module-id>973ed047-d251-4fb9-bf1a-65b8949e0a73</vf-module-id>" + EOL
75 + " <vf-module-name>STMTN5MMSC21-MMSC::module-0-0</vf-module-name>" + EOL
76 + " <service-id>00000000-0000-0000-0000-000000000000</service-id>" + EOL
77 + " <service-type>SDN-ETHERNET-INTERNET</service-type>" + EOL
78 + " <tenant-id>fba1bd1e195a404cacb9ce17a9b2b421</tenant-id>" + EOL
79 + " <orchestration-status>pending-delete</orchestration-status>" + EOL
80 + " <aic-cloud-region>RDM2WAGPLCP</aic-cloud-region>" + EOL + " </vnf-inputs>" + EOL
81 + " <vnf-params xmlns:tns=\"http://org.onap/so/infra/vnf-request/v1\"/>" + EOL + "</vnf-request>" + EOL;
83 private static final String XML_ARRAY_REQ =
84 "<ucpeInfo>" + EOL + " <outOfBandManagementModem>BROADBAND</outOfBandManagementModem>" + EOL
85 + " <internetTopology>IVLAN</internetTopology>" + EOL
86 + " <ucpeAliasHostName>SHELLUCPE31</ucpeAliasHostName>" + EOL + " <wanList>" + EOL
87 + " <wanInfo>" + EOL + " <wanType>AVPN</wanType>" + EOL
88 + " <interfaceType>1000BASE-T</interfaceType>" + EOL
89 + " <transportProviderName>ATT</transportProviderName>" + EOL
90 + " <circuitId>BT/SLIR/70911</circuitId>" + EOL
91 + " <dualMode>Active</dualMode>" + EOL
92 + " <wanPortNumber>WAN1</wanPortNumber>" + EOL
93 + " <transportManagementOption>ATT</transportManagementOption>" + EOL
94 + " <transportVendorTotalBandwidth>100</transportVendorTotalBandwidth>" + EOL
95 + " <mediaType>ELECTRICAL</mediaType>" + EOL + " </wanInfo>" + EOL
96 + " <wanInfo>" + EOL + " <wanType>AVPN</wanType>" + EOL
97 + " <interfaceType>10/100/1000BASE-T</interfaceType>" + EOL
98 + " <transportProviderName>ATT</transportProviderName>" + EOL
99 + " <circuitId>AS/KRFN/34611</circuitId>" + EOL
100 + " <dualMode>Active</dualMode>" + EOL
101 + " <wanPortNumber>WAN2</wanPortNumber>" + EOL
102 + " <transportManagementOption>ATT</transportManagementOption>" + EOL
103 + " <transportVendorTotalBandwidth>10000</transportVendorTotalBandwidth>" + EOL
104 + " <mediaType>MMF</mediaType>" + EOL + " </wanInfo>" + EOL
105 + " </wanList>" + EOL + " <ucpeActivationCode>ASD-987-M31</ucpeActivationCode>" + EOL
106 + " <ucpeHostName>USOSTCDALTX0101UJZZ31</ucpeHostName>" + EOL
107 + " <ucpePartNumber>FG-VM00*</ucpePartNumber>" + EOL + "</ucpeInfo>";
109 // JSON request w/ embedded XML will be read from a file
110 private static String jsonReq;
111 private static String jsonReqArray;
114 public static void initialize() throws Exception {
115 jsonReq = readFileToString("src/test/resources/request.json");
116 jsonReqArray = readFileToString("src/test/resources/requestArray.json");
119 private static String readFileToString(String path) throws IOException {
120 File file = new File(path);
121 return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
125 public void shouldConvertXmlToJsonAndBackToSameXml() throws Exception {
126 // Note: the current version of the JsonUtils.json2xml() method
127 // does not support converting the JSONObject representation
128 // of XML attributes (JSONArray) back to XML. So this test will
129 // only succeed if the original XML does not contain attributes
132 String xmlIn = XmlTool.removeNamespaces(XML_REQ_NO_ATTRS);
134 String json = JsonUtils.xml2json(XML_REQ_NO_ATTRS);
135 String xmlOut = JsonUtils.json2xml(json);
137 Diff diffXml = DiffBuilder.compare(xmlIn).withTest(xmlOut).ignoreWhitespace()
138 .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)).checkForSimilar().build();
140 assertFalse(diffXml.hasDifferences());
144 public void shouldReadValuesForAbsoluteJsonPaths() throws Exception {
146 String json = JsonUtils.xml2json(XML_REQ);
148 assertEquals("STMTN5MMSC21", JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.vnf-name"));
149 assertEquals("CREATE_VF_MODULE", JsonUtils.getJsonValue(json, "vnf-request.request-info.action"));
150 assertEquals("1", JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.persona-model-version"));
151 assertEquals("1.5", JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.vnf-persona-model-version"));
152 assertEquals("true", JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.is-base-module"));
156 public void shouldReturnValueForJsonKey() throws Exception {
158 String json = JsonUtils.xml2json(XML_REQ);
160 assertEquals("PORTAL", JsonUtils.getJsonValueForKey(json, "source"));
164 public void shouldReturnNullForNonexistentJsonNode() throws Exception {
166 String json = JsonUtils.xml2json(XML_REQ);
168 assertNull(JsonUtils.getJsonValueForKey(json, "nonexistent-node"));
172 public void shouldReturnNullForNonExistentParameter() throws Exception {
174 String json = JsonUtils.xml2json(XML_REQ);
176 assertNull(JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.bad"));
180 public void shouldGetJasonParametersFromArray() throws Exception {
182 String json = JsonUtils.xml2json(XML_REQ);
184 assertEquals("network", JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "name"));
185 assertEquals("network1111", JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "content"));
186 assertEquals("server", JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "name", 1));
187 assertEquals("server1111", JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "content", 1));
188 assertNull(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "badParam"));
189 assertNull(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "name", 2));
193 public void shouldAddJsonValue() throws Exception {
195 String json = JsonUtils.xml2json(XML_REQ);
196 String key = "vnf-request.request-info.comment";
197 String value = "Some comment";
199 String jsonUpd = JsonUtils.addJsonValue(json, key, value);
201 String extractedValue = JsonUtils.getJsonValue(jsonUpd, key);
202 assertEquals(value, extractedValue);
206 public void shouldIgnoreAddIfFieldAlreadyExists() throws Exception {
208 String json = JsonUtils.xml2json(XML_REQ);
209 String key = "vnf-request.vnf-inputs.vnf-name";
210 String newValue = "STMTN5MMSC22";
212 String jsonUpd = JsonUtils.addJsonValue(json, key, newValue);
214 String extractedValue = JsonUtils.getJsonValue(jsonUpd, key);
215 assertNotEquals(newValue, extractedValue);
219 public void shouldUpdateValueInJson() throws Exception {
221 String json = JsonUtils.xml2json(XML_REQ);
222 String key = "vnf-request.vnf-inputs.vnf-name";
223 String newValue = "STMTN5MMSC22";
224 String oldValue = JsonUtils.getJsonValue(json, key);
226 String jsonUpd = JsonUtils.updJsonValue(json, key, newValue);
228 String extractedValue = JsonUtils.getJsonValue(jsonUpd, key);
229 assertEquals(newValue, extractedValue);
233 public void shouldDeleteValue() throws Exception {
235 String json = JsonUtils.xml2json(XML_REQ);
236 String key = "vnf-request.vnf-inputs.vnf-name";
238 String jsonUpd = JsonUtils.delJsonValue(json, key);
240 String extractedValue = JsonUtils.getJsonValue(jsonUpd, key);
241 assertNull(extractedValue);
242 JSONObject jsonObj = new JSONObject(json);
243 Integer intValue = JsonUtils.getJsonIntValueForKey(jsonObj, "persona-model-version");
244 Assert.assertTrue(intValue == 1);
245 Boolean boolValue = JsonUtils.getJsonBooleanValueForKey(jsonObj, "is-base-module");
246 Assert.assertTrue(boolValue);
250 public void shouldReturnOriginalJsonWhenTryingToRemoveNonexistentField() throws Exception {
252 String json = JsonUtils.xml2json(XML_REQ);
253 String key = "vnf-request.vnf-inputs.does-not-exist";
255 String jsonUpd = JsonUtils.delJsonValue(json, key);
257 assertEquals(json, jsonUpd);
261 public void shouldConvertXmlToJsonAndBackToSameXmlExtractedFromTheRequest() throws Exception {
263 String value = JsonUtils.getJsonValue(jsonReq, "variables.bpmnRequest.value");
264 String xmlReq = XmlTool.removeNamespaces(XmlTool.normalize(value));
266 String json = JsonUtils.xml2json(xmlReq);
267 String xmlOut = JsonUtils.json2xml(json);
269 Diff diffXml = DiffBuilder.compare(xmlReq).withTest(xmlOut).ignoreWhitespace()
270 .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)).checkForSimilar().build();
271 assertFalse(diffXml.hasDifferences());
275 public void shouldConvertJsonContainingArrayToXml() throws Exception {
277 String jsonParm = JsonUtils.getJsonNodeValue(jsonReqArray, "requestDetails.requestParameters.ucpeInfo");
278 String xmlOut = JsonUtils.json2xml(jsonParm);
280 Diff diffXml = DiffBuilder.compare(XML_ARRAY_REQ).withTest(xmlOut).ignoreWhitespace()
281 .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)).checkForSimilar().build();
282 assertFalse(diffXml.hasDifferences());
286 // Tests the jsonSchemaValidation() method
287 public void testJsonSchemaValidation() {
289 String myReqArray = jsonReqArray;
290 String result = JsonUtils.jsonSchemaValidation(myReqArray, "src/test/resources/requestSchema.json");
291 System.out.println("Schema Validation Result: " + result);
292 Assert.assertTrue(result.contains("success"));
293 // remove a required parameter from the JSON doc so that validation fails
294 myReqArray = JsonUtils.delJsonValue(myReqArray, "requestDetails.requestParameters.ucpeInfo.ucpeHostName");
295 result = JsonUtils.jsonSchemaValidation(myReqArray, "src/test/resources/requestSchema.json");
296 System.out.println("Schema Validation Result: " + result);
297 Assert.assertTrue(result.contains("failure"));
298 Assert.assertTrue(result.contains("error: object has missing required properties ([\"ucpeHostName\"])"));
299 } catch (ValidationException e) {