aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCoreBPMN/src/test/java/org/openecomp/mso/bpmn/core/JsonUtilsTest.java
blob: 58f1ae264db116ef600fccd503ea6f61c4c07ac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

/*- 
 * ============LICENSE_START======================================================= 
 * ONAP - SO 
 * ================================================================================ 
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. 
 * ================================================================================ 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * ============LICENSE_END========================================================= 
 */

package org.openecomp.mso.bpmn.core;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openecomp.mso.bpmn.core.json.JsonUtils;
import org.openecomp.mso.bpmn.core.xml.XmlTool;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.diff.DefaultNodeMatcher;
import org.xmlunit.diff.Diff;
import org.xmlunit.diff.ElementSelectors;

/**
 * @version 1.0
 */
public class JsonUtilsTest {

    private static final String EOL = "\n";
    private static final String XML_REQ =
            "<vnf-request xmlns=\"http://org.openecomp/mso/infra/vnf-request/v1\">" + EOL +
                    "  <request-info>" + EOL +
                    "    <request-id>DEV-VF-0021</request-id>" + EOL +
                    "    <action>CREATE_VF_MODULE</action>" + EOL +
                    "    <source>PORTAL</source>" + EOL +
                    "  </request-info>" + EOL +
                    "  <vnf-inputs>" + EOL +
                    "    <vnf-id>a27ce5a9-29c4-4c22-a017-6615ac73c721</vnf-id>" + EOL +
                    "    <vnf-name>STMTN5MMSC21</vnf-name>" + EOL +
                    "    <vnf-type>asc_heat-int</vnf-type>" + EOL +
                    "    <vf-module-name>STMTN5MMSC21-MMSC::module-0-0</vf-module-name>" + EOL +
                    "    <vf-module-model-name>STMTN5MMSC21-MMSC::model-1-0</vf-module-model-name>" + EOL +
                    "    <is-base-module>true</is-base-module>" + EOL +
                    "    <persona-model-id>00000000-0000-0000-0000-000000000000</persona-model-id>" + EOL +
                    "    <persona-model-version>1.0</persona-model-version>" + EOL +
                    "    <vnf-persona-model-id>999999999-0000-0000-0000-000000000000</vnf-persona-model-id>" + EOL +
                    "    <vnf-persona-model-version>1.5</vnf-persona-model-version>" + EOL +
                    "    <service-id>00000000-0000-0000-0000-000000000000</service-id>" + EOL +
                    "    <service-type>SDN-ETHERNET-INTERNET</service-type>" + EOL +
                    "    <tenant-id>fba1bd1e195a404cacb9ce17a9b2b421</tenant-id>" + EOL +
                    "    <orchestration-status>pending-delete</orchestration-status>" + EOL +
                    "    <aic-cloud-region>RDM2WAGPLCP</aic-cloud-region>" + EOL +
                    "    <asdc-service-model-version>1</asdc-service-model-version>" + EOL +
                    "  </vnf-inputs>" + EOL +
                    "  <vnf-params xmlns:tns=\"http://org.openecomp/mso/infra/vnf-request/v1\">" + EOL +
                    "    <param name=\"network\">network1111</param>" + EOL +
                    "    <param name=\"server\">server1111</param>" + EOL +
                    "  </vnf-params> " + EOL +
                    "</vnf-request>" + EOL;

    private static final String XML_REQ_NO_ATTRS =
            "<vnf-request xmlns=\"http://org.openecomp/mso/infra/vnf-request/v1\">" + EOL +
                    "  <request-info>" + EOL +
                    "    <action>DELETE_VF_MODULE</action>" + EOL +
                    "    <source>PORTAL</source>" + EOL +
                    "  </request-info>" + EOL +
                    "  <vnf-inputs>" + EOL +
                    "    <vnf-id>a27ce5a9-29c4-4c22-a017-6615ac73c721</vnf-id>" + EOL +
                    "    <vnf-name>STMTN5MMSC21</vnf-name>" + EOL +
                    "    <vnf-type>asc_heat-int</vnf-type>" + EOL +
                    "    <vf-module-id>973ed047-d251-4fb9-bf1a-65b8949e0a73</vf-module-id>" + EOL +
                    "    <vf-module-name>STMTN5MMSC21-MMSC::module-0-0</vf-module-name>" + EOL +
                    "    <service-id>00000000-0000-0000-0000-000000000000</service-id>" + EOL +
                    "    <service-type>SDN-ETHERNET-INTERNET</service-type>" + EOL +
                    "    <tenant-id>fba1bd1e195a404cacb9ce17a9b2b421</tenant-id>" + EOL +
                    "    <orchestration-status>pending-delete</orchestration-status>" + EOL +
                    "    <aic-cloud-region>RDM2WAGPLCP</aic-cloud-region>" + EOL +
                    "  </vnf-inputs>" + EOL +
                    "  <vnf-params xmlns:tns=\"http://org.openecomp/mso/infra/vnf-request/v1\"/>" + EOL +
                    "</vnf-request>" + EOL;

    private static final String XML_ARRAY_REQ =
            "<ucpeInfo>" + EOL +
                    "       <outOfBandManagementModem>BROADBAND</outOfBandManagementModem>" + EOL +
                    "   <internetTopology>IVLAN</internetTopology>" + EOL +
                    "   <ucpeAliasHostName>SHELLUCPE31</ucpeAliasHostName>" + EOL +
                    "   <wanList>" + EOL +
                    "           <wanInfo>" + EOL +
                    "                   <wanType>AVPN</wanType>" + EOL +
                    "                   <interfaceType>1000BASE-T</interfaceType>" + EOL +
                    "                   <transportProviderName>ATT</transportProviderName>" + EOL +
                    "                   <circuitId>BT/SLIR/70911</circuitId>" + EOL +
                    "                   <dualMode>Active</dualMode>" + EOL +
                    "                   <wanPortNumber>WAN1</wanPortNumber>" + EOL +
                    "                   <transportManagementOption>ATT</transportManagementOption>" + EOL +
                    "                   <transportVendorTotalBandwidth>100</transportVendorTotalBandwidth>" + EOL +
                    "                   <mediaType>ELECTRICAL</mediaType>" + EOL +
                    "           </wanInfo>" + EOL +
                    "           <wanInfo>" + EOL +
                    "                   <wanType>AVPN</wanType>" + EOL +
                    "                   <interfaceType>10/100/1000BASE-T</interfaceType>" + EOL +
                    "                   <transportProviderName>ATT</transportProviderName>" + EOL +
                    "                   <circuitId>AS/KRFN/34611</circuitId>" + EOL +
                    "                   <dualMode>Active</dualMode>" + EOL +
                    "                   <wanPortNumber>WAN2</wanPortNumber>" + EOL +
                    "                   <transportManagementOption>ATT</transportManagementOption>" + EOL +
                    "                   <transportVendorTotalBandwidth>10000</transportVendorTotalBandwidth>" + EOL +
                    "                   <mediaType>MMF</mediaType>" + EOL +
                    "           </wanInfo>" + EOL +
                    "   </wanList>" + EOL +
                    "   <ucpeActivationCode>ASD-987-M31</ucpeActivationCode>" + EOL +
                    "   <ucpeHostName>USOSTCDALTX0101UJZZ31</ucpeHostName>" + EOL +
                    "   <ucpePartNumber>FG-VM00*</ucpePartNumber>" + EOL +
                    "</ucpeInfo>";

    // JSON request w/ embedded XML will be read from a file
    private static String jsonReq;
    private static String jsonReqArray;

    @BeforeClass
    public static void initialize() throws Exception {
        jsonReq = readFileToString("src/test/resources/request.json");
        jsonReqArray = readFileToString("src/test/resources/requestArray.json");
    }

    private static String readFileToString(String path) throws IOException {
        File file = new File(path);
        return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
    }

    @Test
    public void shouldConvertXmlToJsonAndBackToSameXml() throws Exception {
        // Note: the current version of the JsonUtils.json2xml() method
        // does not support converting the JSONObject representation
        // of XML attributes (JSONArray) back to XML. So this test will
        // only succeed if the original XML does not contain attributes

        // given
        String xmlIn = XmlTool.removeNamespaces(XML_REQ_NO_ATTRS);
        // when
        String json = JsonUtils.xml2json(XML_REQ_NO_ATTRS);
        String xmlOut = JsonUtils.json2xml(json);
        // then
        Diff diffXml = DiffBuilder.compare(xmlIn).withTest(xmlOut).ignoreWhitespace()
                .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)).checkForSimilar().build();

        assertThat(diffXml.hasDifferences()).withFailMessage(diffXml.toString()).isFalse();
    }

    @Test
    public void shouldReadValuesForAbsoluteJsonPaths() throws Exception {
        // given
        String json = JsonUtils.xml2json(XML_REQ);
        // when, then
        assertThat(JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.vnf-name")).isEqualTo("STMTN5MMSC21");
        assertThat(JsonUtils.getJsonValue(json, "vnf-request.request-info.action")).isEqualTo("CREATE_VF_MODULE");
        assertThat(JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.persona-model-version")).isEqualTo("1");
        assertThat(JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.vnf-persona-model-version")).isEqualTo("1.5");
        assertThat(JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.is-base-module")).isEqualTo("true");
    }

    @Test
    public void shouldReturnValueForJsonKey() throws Exception {
        // given
        String json = JsonUtils.xml2json(XML_REQ);
        // when, then
        assertThat(JsonUtils.getJsonValueForKey(json, "source")).isEqualTo("PORTAL");
    }

    @Test
    public void shouldReturnNullForNonexistentJsonNode() throws Exception {
        // given
        String json = JsonUtils.xml2json(XML_REQ);
        // when, then
        assertThat(JsonUtils.getJsonValueForKey(json, "nonexistent-node")).isNull();
    }

    @Test
    public void shouldReturnNullForNonExistentParameter() throws Exception {
        // given
        String json = JsonUtils.xml2json(XML_REQ);
        // when, then
        assertThat(JsonUtils.getJsonValue(json, "vnf-request.vnf-inputs.bad")).isNull();
    }

    @Test
    public void shouldGetJasonParametersFromArray() throws Exception {
        // given
        String json = JsonUtils.xml2json(XML_REQ);
        // when, then
        assertThat(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "name")).isEqualTo("network");
        assertThat(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "content"))
                .isEqualTo("network1111");
        assertThat(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "name", 1)).isEqualTo("server");
        assertThat(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "content", 1))
                .isEqualTo("server1111");
        assertThat(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "badParam"))
                .withFailMessage("Expected null for nonexistent param").isNull();
        assertThat(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "name", 2))
                .withFailMessage("Expected null for index out of bound").isNull();
    }

    @Test
    public void shouldAddJsonValue() throws Exception {
        // given
        String json = JsonUtils.xml2json(XML_REQ);
        String key = "vnf-request.request-info.comment";
        String value = "Some comment";
        // when
        String jsonUpd = JsonUtils.addJsonValue(json, key, value);
        // then
        String extractedValue = JsonUtils.getJsonValue(jsonUpd, key);
        assertThat(extractedValue).isEqualTo(value);
    }

    @Test
    public void shouldIgnoreAddIfFieldAlreadyExists() throws Exception {
        // given
        String json = JsonUtils.xml2json(XML_REQ);
        String key = "vnf-request.vnf-inputs.vnf-name";
        String newValue = "STMTN5MMSC22";
        String oldValue = JsonUtils.getJsonValue(json, key);
        // when
        String jsonUpd = JsonUtils.addJsonValue(json, key, newValue);
        // then
        String extractedValue = JsonUtils.getJsonValue(jsonUpd, key);
        assertThat(extractedValue).isEqualTo(oldValue).isNotEqualTo(newValue);
    }

    @Test
    public void shouldUpdateValueInJson() throws Exception {
        // given
        String json = JsonUtils.xml2json(XML_REQ);
        String key = "vnf-request.vnf-inputs.vnf-name";
        String newValue = "STMTN5MMSC22";
        String oldValue = JsonUtils.getJsonValue(json, key);
        // when
        String jsonUpd = JsonUtils.updJsonValue(json, key, newValue);
        // then
        String extractedValue = JsonUtils.getJsonValue(jsonUpd, key);
        assertThat(extractedValue).isNotEqualTo(oldValue).isEqualTo(newValue);
    }

    @Test
    public void shouldDeleteValue() throws Exception {
        // given
        String json = JsonUtils.xml2json(XML_REQ);
        String key = "vnf-request.vnf-inputs.vnf-name";
        String oldValue = JsonUtils.getJsonValue(json, key);
        // when
        String jsonUpd = JsonUtils.delJsonValue(json, key);
        // then
        String extractedValue = JsonUtils.getJsonValue(jsonUpd, key);
        assertThat(extractedValue).isNotEqualTo(oldValue).isNull();
    }

    @Test
    public void shouldReturnOriginalJsonWhenTryingToRemoveNonexistentField() throws Exception {
        // given
        String json = JsonUtils.xml2json(XML_REQ);
        String key = "vnf-request.vnf-inputs.does-not-exist";
        // when
        String jsonUpd = JsonUtils.delJsonValue(json, key);
        // then
        assertThat(jsonUpd).isEqualTo(json);
    }

    @Test
    public void shouldConvertXmlToJsonAndBackToSameXmlExtractedFromTheRequest() throws Exception {
        // given
        String value = JsonUtils.getJsonValue(jsonReq, "variables.bpmnRequest.value");
        String xmlReq = XmlTool.removeNamespaces(XmlTool.normalize(value));
        // when
        String json = JsonUtils.xml2json(xmlReq);
        String xmlOut = JsonUtils.json2xml(json);
        // then
        Diff diffXml = DiffBuilder.compare(xmlReq).withTest(xmlOut).ignoreWhitespace()
                .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)).checkForSimilar().build();
        assertThat(diffXml.hasDifferences()).withFailMessage(diffXml.toString()).isFalse();
    }

    @Test
    public void shouldConvertJsonContainingArrayToXml() throws Exception {
        // when
        String jsonParm = JsonUtils.getJsonNodeValue(jsonReqArray, "requestDetails.requestParameters.ucpeInfo");
        String xmlOut = JsonUtils.json2xml(jsonParm);
        // then
        Diff diffXml = DiffBuilder.compare(XML_ARRAY_REQ).withTest(xmlOut).ignoreWhitespace()
                .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)).checkForSimilar().build();
        assertThat(diffXml.hasDifferences()).withFailMessage(diffXml.toString()).isFalse();
    }
}