58f1ae264db116ef600fccd503ea6f61c4c07ac2
[so.git] / bpmn / MSOCoreBPMN / src / test / java / org / openecomp / mso / bpmn / core / JsonUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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  * ONAP - SO 
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 static org.assertj.core.api.AssertionsForClassTypes.assertThat;
44
45 import java.io.File;
46 import java.io.IOException;
47 import java.nio.charset.StandardCharsets;
48 import java.nio.file.Files;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51 import org.openecomp.mso.bpmn.core.json.JsonUtils;
52 import org.openecomp.mso.bpmn.core.xml.XmlTool;
53 import org.xmlunit.builder.DiffBuilder;
54 import org.xmlunit.diff.DefaultNodeMatcher;
55 import org.xmlunit.diff.Diff;
56 import org.xmlunit.diff.ElementSelectors;
57
58 /**
59  * @version 1.0
60  */
61 public class JsonUtilsTest {
62
63     private static final String EOL = "\n";
64     private static final String XML_REQ =
65             "<vnf-request xmlns=\"http://org.openecomp/mso/infra/vnf-request/v1\">" + EOL +
66                     "  <request-info>" + EOL +
67                     "    <request-id>DEV-VF-0021</request-id>" + EOL +
68                     "    <action>CREATE_VF_MODULE</action>" + EOL +
69                     "    <source>PORTAL</source>" + EOL +
70                     "  </request-info>" + EOL +
71                     "  <vnf-inputs>" + EOL +
72                     "    <vnf-id>a27ce5a9-29c4-4c22-a017-6615ac73c721</vnf-id>" + EOL +
73                     "    <vnf-name>STMTN5MMSC21</vnf-name>" + EOL +
74                     "    <vnf-type>asc_heat-int</vnf-type>" + EOL +
75                     "    <vf-module-name>STMTN5MMSC21-MMSC::module-0-0</vf-module-name>" + EOL +
76                     "    <vf-module-model-name>STMTN5MMSC21-MMSC::model-1-0</vf-module-model-name>" + EOL +
77                     "    <is-base-module>true</is-base-module>" + EOL +
78                     "    <persona-model-id>00000000-0000-0000-0000-000000000000</persona-model-id>" + EOL +
79                     "    <persona-model-version>1.0</persona-model-version>" + EOL +
80                     "    <vnf-persona-model-id>999999999-0000-0000-0000-000000000000</vnf-persona-model-id>" + EOL +
81                     "    <vnf-persona-model-version>1.5</vnf-persona-model-version>" + EOL +
82                     "    <service-id>00000000-0000-0000-0000-000000000000</service-id>" + EOL +
83                     "    <service-type>SDN-ETHERNET-INTERNET</service-type>" + EOL +
84                     "    <tenant-id>fba1bd1e195a404cacb9ce17a9b2b421</tenant-id>" + EOL +
85                     "    <orchestration-status>pending-delete</orchestration-status>" + EOL +
86                     "    <aic-cloud-region>RDM2WAGPLCP</aic-cloud-region>" + EOL +
87                     "    <asdc-service-model-version>1</asdc-service-model-version>" + EOL +
88                     "  </vnf-inputs>" + EOL +
89                     "  <vnf-params xmlns:tns=\"http://org.openecomp/mso/infra/vnf-request/v1\">" + EOL +
90                     "    <param name=\"network\">network1111</param>" + EOL +
91                     "    <param name=\"server\">server1111</param>" + EOL +
92                     "  </vnf-params> " + EOL +
93                     "</vnf-request>" + EOL;
94
95     private static final String XML_REQ_NO_ATTRS =
96             "<vnf-request xmlns=\"http://org.openecomp/mso/infra/vnf-request/v1\">" + EOL +
97                     "  <request-info>" + EOL +
98                     "    <action>DELETE_VF_MODULE</action>" + EOL +
99                     "    <source>PORTAL</source>" + EOL +
100                     "  </request-info>" + EOL +
101                     "  <vnf-inputs>" + EOL +
102                     "    <vnf-id>a27ce5a9-29c4-4c22-a017-6615ac73c721</vnf-id>" + EOL +
103                     "    <vnf-name>STMTN5MMSC21</vnf-name>" + EOL +
104                     "    <vnf-type>asc_heat-int</vnf-type>" + EOL +
105                     "    <vf-module-id>973ed047-d251-4fb9-bf1a-65b8949e0a73</vf-module-id>" + EOL +
106                     "    <vf-module-name>STMTN5MMSC21-MMSC::module-0-0</vf-module-name>" + EOL +
107                     "    <service-id>00000000-0000-0000-0000-000000000000</service-id>" + EOL +
108                     "    <service-type>SDN-ETHERNET-INTERNET</service-type>" + EOL +
109                     "    <tenant-id>fba1bd1e195a404cacb9ce17a9b2b421</tenant-id>" + EOL +
110                     "    <orchestration-status>pending-delete</orchestration-status>" + EOL +
111                     "    <aic-cloud-region>RDM2WAGPLCP</aic-cloud-region>" + EOL +
112                     "  </vnf-inputs>" + EOL +
113                     "  <vnf-params xmlns:tns=\"http://org.openecomp/mso/infra/vnf-request/v1\"/>" + EOL +
114                     "</vnf-request>" + EOL;
115
116     private static final String XML_ARRAY_REQ =
117             "<ucpeInfo>" + EOL +
118                     "       <outOfBandManagementModem>BROADBAND</outOfBandManagementModem>" + EOL +
119                     "   <internetTopology>IVLAN</internetTopology>" + EOL +
120                     "   <ucpeAliasHostName>SHELLUCPE31</ucpeAliasHostName>" + EOL +
121                     "   <wanList>" + EOL +
122                     "           <wanInfo>" + EOL +
123                     "                   <wanType>AVPN</wanType>" + EOL +
124                     "                   <interfaceType>1000BASE-T</interfaceType>" + EOL +
125                     "                   <transportProviderName>ATT</transportProviderName>" + EOL +
126                     "                   <circuitId>BT/SLIR/70911</circuitId>" + EOL +
127                     "                   <dualMode>Active</dualMode>" + EOL +
128                     "                   <wanPortNumber>WAN1</wanPortNumber>" + EOL +
129                     "                   <transportManagementOption>ATT</transportManagementOption>" + EOL +
130                     "                   <transportVendorTotalBandwidth>100</transportVendorTotalBandwidth>" + EOL +
131                     "                   <mediaType>ELECTRICAL</mediaType>" + EOL +
132                     "           </wanInfo>" + EOL +
133                     "           <wanInfo>" + EOL +
134                     "                   <wanType>AVPN</wanType>" + EOL +
135                     "                   <interfaceType>10/100/1000BASE-T</interfaceType>" + EOL +
136                     "                   <transportProviderName>ATT</transportProviderName>" + EOL +
137                     "                   <circuitId>AS/KRFN/34611</circuitId>" + EOL +
138                     "                   <dualMode>Active</dualMode>" + EOL +
139                     "                   <wanPortNumber>WAN2</wanPortNumber>" + EOL +
140                     "                   <transportManagementOption>ATT</transportManagementOption>" + EOL +
141                     "                   <transportVendorTotalBandwidth>10000</transportVendorTotalBandwidth>" + EOL +
142                     "                   <mediaType>MMF</mediaType>" + EOL +
143                     "           </wanInfo>" + EOL +
144                     "   </wanList>" + EOL +
145                     "   <ucpeActivationCode>ASD-987-M31</ucpeActivationCode>" + EOL +
146                     "   <ucpeHostName>USOSTCDALTX0101UJZZ31</ucpeHostName>" + EOL +
147                     "   <ucpePartNumber>FG-VM00*</ucpePartNumber>" + EOL +
148                     "</ucpeInfo>";
149
150     // JSON request w/ embedded XML will be read from a file
151     private static String jsonReq;
152     private static String jsonReqArray;
153
154     @BeforeClass
155     public static void initialize() throws Exception {
156         jsonReq = readFileToString("src/test/resources/request.json");
157         jsonReqArray = readFileToString("src/test/resources/requestArray.json");
158     }
159
160     private static String readFileToString(String path) throws IOException {
161         File file = new File(path);
162         return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
163     }
164
165     @Test
166     public void shouldConvertXmlToJsonAndBackToSameXml() throws Exception {
167         // Note: the current version of the JsonUtils.json2xml() method
168         // does not support converting the JSONObject representation
169         // of XML attributes (JSONArray) back to XML. So this test will
170         // only succeed if the original XML does not contain attributes
171
172         // given
173         String xmlIn = XmlTool.removeNamespaces(XML_REQ_NO_ATTRS);
174         // when
175         String json = JsonUtils.xml2json(XML_REQ_NO_ATTRS);
176         String xmlOut = JsonUtils.json2xml(json);
177         // then
178         Diff diffXml = DiffBuilder.compare(xmlIn).withTest(xmlOut).ignoreWhitespace()
179                 .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)).checkForSimilar().build();
180
181         assertThat(diffXml.hasDifferences()).withFailMessage(diffXml.toString()).isFalse();
182     }
183
184     @Test
185     public void shouldReadValuesForAbsoluteJsonPaths() throws Exception {
186         // given
187         String json = JsonUtils.xml2json(XML_REQ);
188         // when, then
189         assertThat(JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.vnf-name")).isEqualTo("STMTN5MMSC21");
190         assertThat(JsonUtils.getJsonValue(json, "vnf-request.request-info.action")).isEqualTo("CREATE_VF_MODULE");
191         assertThat(JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.persona-model-version")).isEqualTo("1");
192         assertThat(JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.vnf-persona-model-version")).isEqualTo("1.5");
193         assertThat(JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.is-base-module")).isEqualTo("true");
194     }
195
196     @Test
197     public void shouldReturnValueForJsonKey() throws Exception {
198         // given
199         String json = JsonUtils.xml2json(XML_REQ);
200         // when, then
201         assertThat(JsonUtils.getJsonValueForKey(json, "source")).isEqualTo("PORTAL");
202     }
203
204     @Test
205     public void shouldReturnNullForNonexistentJsonNode() throws Exception {
206         // given
207         String json = JsonUtils.xml2json(XML_REQ);
208         // when, then
209         assertThat(JsonUtils.getJsonValueForKey(json, "nonexistent-node")).isNull();
210     }
211
212     @Test
213     public void shouldReturnNullForNonExistentParameter() throws Exception {
214         // given
215         String json = JsonUtils.xml2json(XML_REQ);
216         // when, then
217         assertThat(JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.bad")).isNull();
218     }
219
220     @Test
221     public void shouldGetJasonParametersFromArray() throws Exception {
222         // given
223         String json = JsonUtils.xml2json(XML_REQ);
224         // when, then
225         assertThat(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "name")).isEqualTo("network");
226         assertThat(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "content"))
227                 .isEqualTo("network1111");
228         assertThat(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "name", 1)).isEqualTo("server");
229         assertThat(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "content", 1))
230                 .isEqualTo("server1111");
231         assertThat(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "badParam"))
232                 .withFailMessage("Expected null for nonexistent param").isNull();
233         assertThat(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "name", 2))
234                 .withFailMessage("Expected null for index out of bound").isNull();
235     }
236
237     @Test
238     public void shouldAddJsonValue() throws Exception {
239         // given
240         String json = JsonUtils.xml2json(XML_REQ);
241         String key = "vnf-request.request-info.comment";
242         String value = "Some comment";
243         // when
244         String jsonUpd = JsonUtils.addJsonValue(json, key, value);
245         // then
246         String extractedValue = JsonUtils.getJsonValue(jsonUpd, key);
247         assertThat(extractedValue).isEqualTo(value);
248     }
249
250     @Test
251     public void shouldIgnoreAddIfFieldAlreadyExists() throws Exception {
252         // given
253         String json = JsonUtils.xml2json(XML_REQ);
254         String key = "vnf-request.vnf-inputs.vnf-name";
255         String newValue = "STMTN5MMSC22";
256         String oldValue = JsonUtils.getJsonValue(json, key);
257         // when
258         String jsonUpd = JsonUtils.addJsonValue(json, key, newValue);
259         // then
260         String extractedValue = JsonUtils.getJsonValue(jsonUpd, key);
261         assertThat(extractedValue).isEqualTo(oldValue).isNotEqualTo(newValue);
262     }
263
264     @Test
265     public void shouldUpdateValueInJson() throws Exception {
266         // given
267         String json = JsonUtils.xml2json(XML_REQ);
268         String key = "vnf-request.vnf-inputs.vnf-name";
269         String newValue = "STMTN5MMSC22";
270         String oldValue = JsonUtils.getJsonValue(json, key);
271         // when
272         String jsonUpd = JsonUtils.updJsonValue(json, key, newValue);
273         // then
274         String extractedValue = JsonUtils.getJsonValue(jsonUpd, key);
275         assertThat(extractedValue).isNotEqualTo(oldValue).isEqualTo(newValue);
276     }
277
278     @Test
279     public void shouldDeleteValue() throws Exception {
280         // given
281         String json = JsonUtils.xml2json(XML_REQ);
282         String key = "vnf-request.vnf-inputs.vnf-name";
283         String oldValue = JsonUtils.getJsonValue(json, key);
284         // when
285         String jsonUpd = JsonUtils.delJsonValue(json, key);
286         // then
287         String extractedValue = JsonUtils.getJsonValue(jsonUpd, key);
288         assertThat(extractedValue).isNotEqualTo(oldValue).isNull();
289     }
290
291     @Test
292     public void shouldReturnOriginalJsonWhenTryingToRemoveNonexistentField() throws Exception {
293         // given
294         String json = JsonUtils.xml2json(XML_REQ);
295         String key = "vnf-request.vnf-inputs.does-not-exist";
296         // when
297         String jsonUpd = JsonUtils.delJsonValue(json, key);
298         // then
299         assertThat(jsonUpd).isEqualTo(json);
300     }
301
302     @Test
303     public void shouldConvertXmlToJsonAndBackToSameXmlExtractedFromTheRequest() throws Exception {
304         // given
305         String value = JsonUtils.getJsonValue(jsonReq, "variables.bpmnRequest.value");
306         String xmlReq = XmlTool.removeNamespaces(XmlTool.normalize(value));
307         // when
308         String json = JsonUtils.xml2json(xmlReq);
309         String xmlOut = JsonUtils.json2xml(json);
310         // then
311         Diff diffXml = DiffBuilder.compare(xmlReq).withTest(xmlOut).ignoreWhitespace()
312                 .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)).checkForSimilar().build();
313         assertThat(diffXml.hasDifferences()).withFailMessage(diffXml.toString()).isFalse();
314     }
315
316     @Test
317     public void shouldConvertJsonContainingArrayToXml() throws Exception {
318         // when
319         String jsonParm = JsonUtils.getJsonNodeValue(jsonReqArray, "requestDetails.requestParameters.ucpeInfo");
320         String xmlOut = JsonUtils.json2xml(jsonParm);
321         // then
322         Diff diffXml = DiffBuilder.compare(XML_ARRAY_REQ).withTest(xmlOut).ignoreWhitespace()
323                 .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)).checkForSimilar().build();
324         assertThat(diffXml.hasDifferences()).withFailMessage(diffXml.toString()).isFalse();
325     }
326 }