2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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.ccsdk.sli.plugins.yangserializers.dfserializer;
24 * Unit test case utilities for data format serializer and restconf api call
27 public final class DataFormatUtilsTest {
29 static final String ENCODE_TO_JSON_ID_COMMON = "\n \"interfaces\"" +
32 " \"int-list\": [\n" +
34 " \"iden\": \"optical\",\n" +
35 " \"available\": {\n" +
38 " \"identity-types:Loopback\",\n" +
39 " \"identity-types-second:Ethernet" +
42 " \"leaf1\": \"58\",\n" +
43 " \"leaf2\": \"identity-types-second:iden" +
48 " \"iden\": \"214748364\",\n" +
49 " \"available\": {\n" +
52 " \"identity-types:Loopback\",\n" +
53 " \"identity-types-second:Ethernet" +
56 " \"leaf1\": \"8888\",\n" +
57 " \"leaf2\": \"identity-types-second:ide" +
63 " \"interface\": \"identity-types:physical\"\n" +
66 static final String ENCODE_TO_JSON_ID = "{\n" +
67 " \"identity-test:con1\": {" + ENCODE_TO_JSON_ID_COMMON +
69 " \"identity-test:l\": \"abc\"\n" +
72 static final String ENCODE_TO_JSON_ID_PUT = "{\n" +
73 " \"identity-test:test\": {\n" +
74 " \"con1\": {" + addSpace(ENCODE_TO_JSON_ID_COMMON, 4) +
80 static final String ENCODE_TO_XML_ID_COMMON = "\n <interfaces>\n" +
82 " <iden>optical</iden>\n" +
85 " <ll xmlns:yangid=\"identity:list:ns:test:json:se" +
86 "r\">yangid:Loopback</ll>\n" +
87 " <ll xmlns:yangid=\"identity:list:second:ns:test" +
88 ":json:ser\">yangid:Ethernet</ll>\n" +
89 " <leaf1>58</leaf1>\n" +
90 " <leaf2 xmlns:yangid=\"identity:list:second:ns:t" +
91 "est:json:ser\">yangid:iden2</leaf2>\n" +
95 " <iden>214748364</iden>\n" +
98 " <ll xmlns:yangid=\"identity:list:ns:test:json:s" +
99 "er\">yangid:Loopback</ll>\n" +
100 " <ll xmlns:yangid=\"identity:list:second:ns:test" +
101 ":json:ser\">yangid:Ethernet</ll>\n" +
102 " <leaf1>8888</leaf1>\n" +
103 " <leaf2 xmlns:yangid=\"identity:list:second:ns:t" +
104 "est:json:ser\">yangid:iden2</leaf2>\n" +
108 " <interface xmlns:yangid=\"identity:list:ns:test:json:ser\">" +
109 "yangid:physical</interface>";
111 static final String ENCODE_TO_XML_ID = "<?xml version=\"1.0\" encoding=" +
112 "\"UTF-8\" standalone=\"no\"?>\n" +
113 "<con1 xmlns=\"identity:ns:test:json:ser\">" +
114 ENCODE_TO_XML_ID_COMMON + "\n</con1>\n";
116 static final String ENCODE_TO_XML_ID_PUT = "<?xml version=\"1.0\" enco" +
117 "ding=\"UTF-8\" standalone=\"no\"?>\n" +
118 "<test xmlns=\"identity:ns:test:json:ser\">\n" +
119 " <con1>" + addSpace(ENCODE_TO_XML_ID_COMMON, 4)
123 static final String ENCODE_TO_JSON_YANG_COMMON = "\n " +
124 "\"test-augment:ll6\": [\n" +
125 " \"unbounded\",\n" +
128 " \"test-augment:cont13\": {\n" +
135 " \"leaf27\": \"abc\"\n" +
138 " \"leaf27\": \"abc\"\n" +
141 " \"leaf28\": \"abc\",\n" +
143 " \"leaf26\": \"abc\"\n" +
146 " \"test-augment:list7\": [\n" +
148 " \"leaf14\": \"test\"\n" +
151 " \"leaf14\": \"create\"\n" +
154 " \"test-augment:leaf15\": \"abc\",\n" +
155 " \"test-augment:cont5\": {\n" +
156 " \"leaf13\": \"true\"\n" +
159 static final String ENCODE_TO_JSON_YANG_AUG_POST = "{\n" +
160 " \"test-yang:leaf10\": \"abc\"," +
161 ENCODE_TO_JSON_YANG_COMMON + "\n}";
163 static final String ENCODE_TO_JSON_YANG = "{\n" +
164 " \"test-yang:cont2\": {\n" +
171 " \"leaf1\": \"true\",\n" +
178 " \"leaf9\": \"abc\"\n" +
181 " \"leaf9\": \"abc\"\n" +
184 " \"leaf3\": \"abc\",\n" +
185 " \"leaf2\": \"abc\",\n" +
188 " \"leaf8\": \"abc\"\n" +
191 " \"leaf8\": \"abc\"\n" +
195 " \"leaf11\": \"abc\"\n" +
203 " \"leaf1\": \"true\",\n" +
208 " \"leaf3\": \"abc\",\n" +
211 " \"leaf9\": \"abc\"\n" +
214 " \"leaf9\": \"abc\"\n" +
219 " \"leaf8\": \"abc\"\n" +
222 " \"leaf8\": \"abc\"\n" +
225 " \"leaf2\": \"abc\",\n" +
227 " \"leaf11\": \"abc\"\n" +
240 " \"leaf10\": \"abc\"," +
241 addSpace(ENCODE_TO_JSON_YANG_COMMON, 8) + "\n" +
248 " \"leaf10\": \"abc\"\n" +
250 " \"leaf5\": \"abc\",\n" +
253 " \"leaf4\": \"abc\"\n" +
256 " \"leaf4\": \"abc\"\n" +
259 " \"leaf12\": \"abc\",\n" +
260 " \"leaf6\": \"abc\",\n" +
263 " \"leaf11\": \"abc\"\n" +
266 " \"leaf11\": \"abc\"\n" +
272 static final String ENCODE_TO_JSON_YANG_PUT = "{\n" +
273 " \"test-yang:cont4\": {" + addSpace(
274 ENCODE_TO_JSON_YANG_COMMON, 4) + ",\n" +
275 " \"leaf10\": \"abc\"\n" +
279 static final String ENCODE_TO_XML_YANG_COMMON = "\n" +
280 "<ll6 xmlns=\"urn:opendaylight:params:xml:ns:yang:aug" +
281 "ment\">unbounded</ll6>\n" +
282 "<ll6 xmlns=\"urn:opendaylight:params:xml:ns:yang:aug" +
284 "<cont13 xmlns=\"urn:opendaylight:params:xml:ns:yang:" +
286 " <ll9>abc</ll9>\n" +
287 " <ll9>abc</ll9>\n" +
289 " <leaf27>abc</leaf27>\n" +
292 " <leaf27>abc</leaf27>\n" +
294 " <leaf28>abc</leaf28>\n" +
296 " <leaf26>abc</leaf26>\n" +
299 "<list7 xmlns=\"urn:opendaylight:params:xml:ns:yang:a" +
301 " <leaf14>test</leaf14>\n" +
303 "<list7 xmlns=\"urn:opendaylight:params:xml:ns:yang:a" +
305 " <leaf14>create</leaf14>\n" +
307 "<leaf15 xmlns=\"urn:opendaylight:params:xml:ns:yang:" +
308 "augment\">abc</leaf15>\n" +
309 "<cont5 xmlns=\"urn:opendaylight:params:xml:ns:yang:a" +
311 " <leaf13>true</leaf13>\n" +
314 static final String ENCODE_TO_XML_YANG_AUG_POST = "<?xml version=\"1.0\"" +
315 " encoding=\"UTF-8\" standalone=\"no\"?>\n" +
316 "<leaf10 xmlns=\"urn:opendaylight:params:xml:ns:yang:test\">abc" +
318 ENCODE_TO_XML_YANG_COMMON + "\n";
320 static final String ENCODE_TO_XML_YANG_PUT = "<?xml version=\"1.0\" enc" +
321 "oding=\"UTF-8\" standalone=\"no\"?>\n" +
322 "<cont4 xmlns=\"urn:opendaylight:params:xml:ns:yang:test\">\n" +
323 " <leaf10>abc</leaf10>" +
324 addSpace(ENCODE_TO_XML_YANG_COMMON, 4) + "\n</cont4>\n";
326 static final String ENCODE_TO_XML_YANG = "<?xml version=\"1.0\" encoding" +
327 "=\"UTF-8\" standalone=\"no\"?>\n" +
328 "<cont2 xmlns=\"urn:opendaylight:params:xml:ns:yang:test\">\n" +
330 " <ll1>abc</ll1>\n" +
331 " <ll1>abc</ll1>\n" +
332 " <leaf1>true</leaf1>\n" +
333 " <ll2>abc</ll2>\n" +
334 " <ll2>abc</ll2>\n" +
336 " <leaf9>abc</leaf9>\n" +
339 " <leaf9>abc</leaf9>\n" +
341 " <leaf3>abc</leaf3>\n" +
342 " <leaf2>abc</leaf2>\n" +
344 " <leaf8>abc</leaf8>\n" +
347 " <leaf8>abc</leaf8>\n" +
350 " <leaf11>abc</leaf11>\n" +
354 " <ll1>abc</ll1>\n" +
355 " <ll1>abc</ll1>\n" +
356 " <leaf1>true</leaf1>\n" +
357 " <ll2>abc</ll2>\n" +
358 " <ll2>abc</ll2>\n" +
359 " <leaf3>abc</leaf3>\n" +
361 " <leaf9>abc</leaf9>\n" +
364 " <leaf9>abc</leaf9>\n" +
367 " <leaf8>abc</leaf8>\n" +
370 " <leaf8>abc</leaf8>\n" +
372 " <leaf2>abc</leaf2>\n" +
374 " <leaf11>abc</leaf11>\n" +
377 " <ll3>abc</ll3>\n" +
378 " <ll3>abc</ll3>\n" +
379 " <ll5>abc</ll5>\n" +
380 " <ll5>abc</ll5>\n" +
382 " <leaf10>abc</leaf10>"+
383 addSpace(ENCODE_TO_XML_YANG_COMMON, 8) + "\n" +
385 " <ll4>abc</ll4>\n" +
386 " <ll4>abc</ll4>\n" +
388 " <leaf10>abc</leaf10>\n" +
390 " <leaf5>abc</leaf5>\n" +
392 " <leaf4>abc</leaf4>\n" +
395 " <leaf4>abc</leaf4>\n" +
397 " <leaf12>abc</leaf12>\n" +
398 " <leaf6>abc</leaf6>\n" +
400 " <leaf11>abc</leaf11>\n" +
403 " <leaf11>abc</leaf11>\n" +
407 static final String ENCODE_TO_JSON_RPC = "{\n" +
408 " \"test-yang:input\": {\n" +
409 " \"leaf30\": \"abc\",\n" +
412 " \"leaf29\": \"abc\"\n" +
415 " \"leaf29\": \"abc\"\n" +
419 " \"leaf31\": \"abc\"\n" +
422 " \"leaf28\": \"abc\"\n" +
427 " \"leaf27\": \"abc\"\n" +
430 " \"leaf27\": \"abc\"\n" +
437 " \"leaf28\": \"abc\"\n" +
446 static final String DECODE_FROM_JSON_RPC = "{\n" +
447 " \"test-yang:output\": {\n" +
449 " \"leaf32\": \"abc\"\n" +
453 " \"leaf33\": \"abc\"\n" +
456 " \"leaf33\": \"abc\"\n" +
459 " \"leaf34\": \"abc\",\n" +
465 " \"leaf35\": \"abc\"\n" +
469 " \"leaf26\": \"abc\"\n" +
473 " \"leaf27\": \"abc\"\n" +
476 " \"leaf27\": \"abc\"\n" +
483 " \"leaf28\": \"abc\"\n" +
488 static final String ENCODE_TO_XML_RPC = "<?xml version=\"1.0\" encoding" +
489 "=\"UTF-8\" standalone=\"no\"?>\n" +
490 "<input xmlns=\"urn:opendaylight:params:xml:ns:yang:test\">\n" +
491 " <leaf30>abc</leaf30>\n" +
493 " <leaf29>abc</leaf29>\n" +
496 " <leaf29>abc</leaf29>\n" +
499 " <leaf31>abc</leaf31>\n" +
502 " <leaf28>abc</leaf28>\n" +
506 " <leaf27>abc</leaf27>\n" +
509 " <leaf27>abc</leaf27>\n" +
511 " <ll9>abc</ll9>\n" +
512 " <ll9>abc</ll9>\n" +
513 " <leaf28>abc</leaf28>\n" +
515 " <ll10>abc</ll10>\n" +
516 " <ll10>abc</ll10>\n" +
519 static final String DECODE_FROM_XML_RPC = "<?xml version=\"1.0\" encodi" +
520 "ng=\"UTF-8\" standalone=\"no\"?>\n" +
521 "<output xmlns=\"urn:opendaylight:params:xml:ns:yang:test\">\n" +
523 " <leaf32>abc</leaf32>\n" +
526 " <leaf33>abc</leaf33>\n" +
529 " <leaf33>abc</leaf33>\n" +
531 " <leaf34>abc</leaf34>\n" +
532 " <ll11>abc</ll11>\n" +
533 " <ll11>abc</ll11>\n" +
535 " <leaf35>abc</leaf35>\n" +
539 " <leaf26>abc</leaf26>\n" +
542 " <leaf27>abc</leaf27>\n" +
545 " <leaf27>abc</leaf27>\n" +
547 " <ll9>abc</ll9>\n" +
548 " <ll9>abc</ll9>\n" +
549 " <leaf28>abc</leaf28>\n" +
554 * Adds the specified number of space required for a req in each line.
556 * @param req request message
557 * @param i number of space
558 * @return space appended string
560 public static String addSpace(String req, int i) {
561 StringBuilder space = new StringBuilder();
562 for (int sp = 0; sp < i; sp++) {
563 space = space.append(" ");
565 return req.replaceAll("\n", "\n" + space.toString());