Appending dummy root node to XML data format
[ccsdk/sli/plugins.git] / restconf-client / provider / src / test / java / org / onap / ccsdk / sli / plugins / yangserializers / dfserializer / DataFormatUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CCSDK
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
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 package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer;
22
23 /**
24  * Unit test case utilities for data format serializer and restconf api call
25  * node.
26  */
27 public final class DataFormatUtilsTest {
28
29     static final String ENCODE_TO_JSON_ID = "{\n" +
30             "    \"identity-test:con1\": {\n" +
31             "        \"interfaces\": {\n" +
32             "            \"int-list\": [\n" +
33             "                {\n" +
34             "                    \"iden\": \"optical\",\n" +
35             "                    \"available\": {\n" +
36             "                        \"ll\": [\n" +
37             "                            \"Giga\",\n" +
38             "                            \"identity-types:Loopback\",\n" +
39             "                            \"identity-types-second:Ethernet" +
40             "\"\n" +
41             "                        ],\n" +
42             "                        \"leaf1\": \"58\",\n" +
43             "                        \"leaf2\": \"identity-types-second:iden" +
44             "2\"\n" +
45             "                    }\n" +
46             "                },\n" +
47             "                {\n" +
48             "                    \"iden\": \"214748364\",\n" +
49             "                    \"available\": {\n" +
50             "                        \"ll\": [\n" +
51             "                            \"Giga\",\n" +
52             "                            \"identity-types:Loopback\",\n" +
53             "                            \"identity-types-second:Ethernet" +
54             "\"\n" +
55             "                        ],\n" +
56             "                        \"leaf1\": \"8888\",\n" +
57             "                        \"leaf2\": \"identity-types-second:ide" +
58             "n2\"\n" +
59             "                    }\n" +
60             "                }\n" +
61             "            ]\n" +
62             "        },\n" +
63             "        \"interface\": \"identity-types:physical\"\n" +
64             "    },\n" +
65             "    \"identity-test:l\": \"abc\"\n" +
66             "}";
67
68     static final String ENCODE_TO_XML_ID = "<?xml version=\"1.0\" encoding=" +
69             "\"UTF-8\" standalone=\"no\"?>\n" +
70             "<con1 xmlns=\"identity:ns:test:json:ser\">\n" +
71             "    <interfaces>\n" +
72             "        <int-list>\n" +
73             "            <iden>optical</iden>\n" +
74             "            <available>\n" +
75             "                <ll>Giga</ll>\n" +
76             "                <ll xmlns:yangid=\"identity:list:ns:test:json:se" +
77             "r\">yangid:Loopback</ll>\n" +
78             "                <ll xmlns:yangid=\"identity:list:second:ns:test" +
79             ":json:ser\">yangid:Ethernet</ll>\n" +
80             "                <leaf1>58</leaf1>\n" +
81             "                <leaf2 xmlns:yangid=\"identity:list:second:ns:t" +
82             "est:json:ser\">yangid:iden2</leaf2>\n" +
83             "            </available>\n" +
84             "        </int-list>\n" +
85             "        <int-list>\n" +
86             "            <iden>214748364</iden>\n" +
87             "            <available>\n" +
88             "                <ll>Giga</ll>\n" +
89             "                <ll xmlns:yangid=\"identity:list:ns:test:json:s" +
90             "er\">yangid:Loopback</ll>\n" +
91             "                <ll xmlns:yangid=\"identity:list:second:ns:test" +
92             ":json:ser\">yangid:Ethernet</ll>\n" +
93             "                <leaf1>8888</leaf1>\n" +
94             "                <leaf2 xmlns:yangid=\"identity:list:second:ns:t" +
95             "est:json:ser\">yangid:iden2</leaf2>\n" +
96             "            </available>\n" +
97             "        </int-list>\n" +
98             "    </interfaces>\n" +
99             "    <interface xmlns:yangid=\"identity:list:ns:test:json:ser\">" +
100             "yangid:physical</interface>\n" +
101             "</con1>\n";
102
103     static final String ENCODE_TO_JSON_YANG_COMMON = "\n    " +
104             "\"test-augment:cont13\": {\n" +
105             "        \"ll9\": [\n" +
106             "            \"abc\",\n" +
107             "            \"abc\"\n" +
108             "        ],\n" +
109             "        \"list9\": [\n" +
110             "            {\n" +
111             "                \"leaf27\": \"abc\"\n" +
112             "            },\n" +
113             "            {\n" +
114             "                \"leaf27\": \"abc\"\n" +
115             "            }\n" +
116             "        ],\n" +
117             "        \"leaf28\": \"abc\",\n" +
118             "        \"cont12\": {\n" +
119             "            \"leaf26\": \"abc\"\n" +
120             "        }\n" +
121             "    },\n" +
122             "    \"test-augment:list7\": [\n" +
123             "        {\n" +
124             "            \"leaf14\": \"test\"\n" +
125             "        },\n" +
126             "        {\n" +
127             "            \"leaf14\": \"create\"\n" +
128             "        }\n" +
129             "    ],\n" +
130             "    \"test-augment:leaf15\": \"abc\",\n" +
131             "    \"test-augment:ll6\": [\n" +
132             "        \"unbounded\",\n" +
133             "        \"8\"\n" +
134             "    ],\n" +
135             "    \"test-augment:cont5\": {\n" +
136             "        \"leaf13\": \"true\"\n" +
137             "    }";
138
139     static final String ENCODE_TO_JSON_YANG_AUG_POST = "{\n" +
140             "    \"test-yang:leaf10\": \"abc\"," +
141             ENCODE_TO_JSON_YANG_COMMON + "\n}";
142
143     static final String ENCODE_TO_JSON_YANG = "{\n" +
144             "    \"test-yang:cont2\": {\n" +
145             "        \"list1\": [\n" +
146             "            {\n" +
147             "                \"ll1\": [\n" +
148             "                    \"abc\",\n" +
149             "                    \"abc\"\n" +
150             "                ],\n" +
151             "                \"leaf1\": \"true\",\n" +
152             "                \"ll2\": [\n" +
153             "                    \"abc\",\n" +
154             "                    \"abc\"\n" +
155             "                ],\n" +
156             "                \"list5\": [\n" +
157             "                    {\n" +
158             "                        \"leaf9\": \"abc\"\n" +
159             "                    },\n" +
160             "                    {\n" +
161             "                        \"leaf9\": \"abc\"\n" +
162             "                    }\n" +
163             "                ],\n" +
164             "                \"leaf3\": \"abc\",\n" +
165             "                \"leaf2\": \"abc\",\n" +
166             "                \"list4\": [\n" +
167             "                    {\n" +
168             "                        \"leaf8\": \"abc\"\n" +
169             "                    },\n" +
170             "                    {\n" +
171             "                        \"leaf8\": \"abc\"\n" +
172             "                    }\n" +
173             "                ],\n" +
174             "                \"cont4\": {\n" +
175             "                    \"leaf11\": \"abc\"\n" +
176             "                }\n" +
177             "            },\n" +
178             "            {\n" +
179             "                \"ll1\": [\n" +
180             "                    \"abc\",\n" +
181             "                    \"abc\"\n" +
182             "                ],\n" +
183             "                \"leaf1\": \"true\",\n" +
184             "                \"ll2\": [\n" +
185             "                    \"abc\",\n" +
186             "                    \"abc\"\n" +
187             "                ],\n" +
188             "                \"leaf3\": \"abc\",\n" +
189             "                \"list5\": [\n" +
190             "                    {\n" +
191             "                        \"leaf9\": \"abc\"\n" +
192             "                    },\n" +
193             "                    {\n" +
194             "                        \"leaf9\": \"abc\"\n" +
195             "                    }\n" +
196             "                ],\n" +
197             "                \"list4\": [\n" +
198             "                    {\n" +
199             "                        \"leaf8\": \"abc\"\n" +
200             "                    },\n" +
201             "                    {\n" +
202             "                        \"leaf8\": \"abc\"\n" +
203             "                    }\n" +
204             "                ],\n" +
205             "                \"leaf2\": \"abc\",\n" +
206             "                \"cont4\": {\n" +
207             "                    \"leaf11\": \"abc\"\n" +
208             "                }\n" +
209             "            }\n" +
210             "        ],\n" +
211             "        \"ll3\": [\n" +
212             "            \"abc\",\n" +
213             "            \"abc\"\n" +
214             "        ],\n" +
215             "        \"ll5\": [\n" +
216             "            \"abc\",\n" +
217             "            \"abc\"\n" +
218             "        ],\n" +
219             "        \"cont4\": {\n" +
220             "            \"leaf10\": \"abc\"," +
221             addSpace(ENCODE_TO_JSON_YANG_COMMON, 8) + "\n" +
222             "        },\n" +
223             "        \"ll4\": [\n" +
224             "            \"abc\",\n" +
225             "            \"abc\"\n" +
226             "        ],\n" +
227             "        \"cont3\": {\n" +
228             "            \"leaf10\": \"abc\"\n" +
229             "        },\n" +
230             "        \"leaf5\": \"abc\",\n" +
231             "        \"list2\": [\n" +
232             "            {\n" +
233             "                \"leaf4\": \"abc\"\n" +
234             "            },\n" +
235             "            {\n" +
236             "                \"leaf4\": \"abc\"\n" +
237             "            }\n" +
238             "        ],\n" +
239             "        \"leaf12\": \"abc\",\n" +
240             "        \"leaf6\": \"abc\",\n" +
241             "        \"list6\": [\n" +
242             "            {\n" +
243             "                \"leaf11\": \"abc\"\n" +
244             "            },\n" +
245             "            {\n" +
246             "                \"leaf11\": \"abc\"\n" +
247             "            }\n" +
248             "        ]\n" +
249             "    }\n" +
250             "}";
251
252     static final String ENCODE_TO_XML_YANG_COMMON = "\n" +
253             "<cont13 xmlns=\"urn:opendaylight:params:xml:ns:yang:" +
254             "augment\">\n" +
255             "    <ll9>abc</ll9>\n" +
256             "    <ll9>abc</ll9>\n" +
257             "    <list9>\n" +
258             "        <leaf27>abc</leaf27>\n" +
259             "    </list9>\n" +
260             "    <list9>\n" +
261             "        <leaf27>abc</leaf27>\n" +
262             "    </list9>\n" +
263             "    <leaf28>abc</leaf28>\n" +
264             "    <cont12>\n" +
265             "        <leaf26>abc</leaf26>\n" +
266             "    </cont12>\n" +
267             "</cont13>\n" +
268             "<list7 xmlns=\"urn:opendaylight:params:xml:ns:yang:a" +
269             "ugment\">\n" +
270             "    <leaf14>test</leaf14>\n" +
271             "</list7>\n" +
272             "<list7 xmlns=\"urn:opendaylight:params:xml:ns:yang:a" +
273             "ugment\">\n" +
274             "    <leaf14>create</leaf14>\n" +
275             "</list7>\n" +
276             "<leaf15 xmlns=\"urn:opendaylight:params:xml:ns:yang:" +
277             "augment\">abc</leaf15>\n" +
278             "<ll6 xmlns=\"urn:opendaylight:params:xml:ns:yang:aug" +
279             "ment\">unbounded</ll6>\n" +
280             "<ll6 xmlns=\"urn:opendaylight:params:xml:ns:yang:aug" +
281             "ment\">8</ll6>\n" +
282             "<cont5 xmlns=\"urn:opendaylight:params:xml:ns:yang:a" +
283             "ugment\">\n" +
284             "    <leaf13>true</leaf13>\n" +
285             "</cont5>";
286
287     static final String ENCODE_TO_XML_YANG_AUG_POST = "<?xml version=\"1.0\"" +
288             " encoding=\"UTF-8\" standalone=\"no\"?>\n" +
289             "<leaf10 xmlns=\"urn:opendaylight:params:xml:ns:yang:test\">abc" +
290             "</leaf10>" +
291             ENCODE_TO_XML_YANG_COMMON + "\n";
292
293     static final String ENCODE_TO_XML_YANG = "<?xml version=\"1.0\" encoding" +
294             "=\"UTF-8\" standalone=\"no\"?>\n" +
295             "<cont2 xmlns=\"urn:opendaylight:params:xml:ns:yang:test\">\n" +
296             "    <list1>\n" +
297             "        <ll1>abc</ll1>\n" +
298             "        <ll1>abc</ll1>\n" +
299             "        <leaf1>true</leaf1>\n" +
300             "        <ll2>abc</ll2>\n" +
301             "        <ll2>abc</ll2>\n" +
302             "        <list5>\n" +
303             "            <leaf9>abc</leaf9>\n" +
304             "        </list5>\n" +
305             "        <list5>\n" +
306             "            <leaf9>abc</leaf9>\n" +
307             "        </list5>\n" +
308             "        <leaf3>abc</leaf3>\n" +
309             "        <leaf2>abc</leaf2>\n" +
310             "        <list4>\n" +
311             "            <leaf8>abc</leaf8>\n" +
312             "        </list4>\n" +
313             "        <list4>\n" +
314             "            <leaf8>abc</leaf8>\n" +
315             "        </list4>\n" +
316             "        <cont4>\n" +
317             "            <leaf11>abc</leaf11>\n" +
318             "        </cont4>\n" +
319             "    </list1>\n" +
320             "    <list1>\n" +
321             "        <ll1>abc</ll1>\n" +
322             "        <ll1>abc</ll1>\n" +
323             "        <leaf1>true</leaf1>\n" +
324             "        <ll2>abc</ll2>\n" +
325             "        <ll2>abc</ll2>\n" +
326             "        <leaf3>abc</leaf3>\n" +
327             "        <list5>\n" +
328             "            <leaf9>abc</leaf9>\n" +
329             "        </list5>\n" +
330             "        <list5>\n" +
331             "            <leaf9>abc</leaf9>\n" +
332             "        </list5>\n" +
333             "        <list4>\n" +
334             "            <leaf8>abc</leaf8>\n" +
335             "        </list4>\n" +
336             "        <list4>\n" +
337             "            <leaf8>abc</leaf8>\n" +
338             "        </list4>\n" +
339             "        <leaf2>abc</leaf2>\n" +
340             "        <cont4>\n" +
341             "            <leaf11>abc</leaf11>\n" +
342             "        </cont4>\n" +
343             "    </list1>\n" +
344             "    <ll3>abc</ll3>\n" +
345             "    <ll3>abc</ll3>\n" +
346             "    <ll5>abc</ll5>\n" +
347             "    <ll5>abc</ll5>\n" +
348             "    <cont4>\n" +
349             "        <leaf10>abc</leaf10>"+
350             addSpace(ENCODE_TO_XML_YANG_COMMON, 8) + "\n" +
351             "    </cont4>\n" +
352             "    <ll4>abc</ll4>\n" +
353             "    <ll4>abc</ll4>\n" +
354             "    <cont3>\n" +
355             "        <leaf10>abc</leaf10>\n" +
356             "    </cont3>\n" +
357             "    <leaf5>abc</leaf5>\n" +
358             "    <list2>\n" +
359             "        <leaf4>abc</leaf4>\n" +
360             "    </list2>\n" +
361             "    <list2>\n" +
362             "        <leaf4>abc</leaf4>\n" +
363             "    </list2>\n" +
364             "    <leaf12>abc</leaf12>\n" +
365             "    <leaf6>abc</leaf6>\n" +
366             "    <list6>\n" +
367             "        <leaf11>abc</leaf11>\n" +
368             "    </list6>\n" +
369             "    <list6>\n" +
370             "        <leaf11>abc</leaf11>\n" +
371             "    </list6>\n" +
372             "</cont2>\n";
373
374     static final String ENCODE_TO_JSON_RPC = "{\n" +
375             "    \"test-yang:input\": {\n" +
376             "        \"leaf30\": \"abc\",\n" +
377             "        \"list10\": [\n" +
378             "            {\n" +
379             "                \"leaf29\": \"abc\"\n" +
380             "            },\n" +
381             "            {\n" +
382             "                \"leaf29\": \"abc\"\n" +
383             "            }\n" +
384             "        ],\n" +
385             "        \"cont15\": {\n" +
386             "            \"leaf31\": \"abc\"\n" +
387             "        },\n" +
388             "        \"cont14\": {\n" +
389             "            \"leaf28\": \"abc\"\n" +
390             "        },\n" +
391             "        \"cont13\": {\n" +
392             "            \"list9\": [\n" +
393             "                {\n" +
394             "                    \"leaf27\": \"abc\"\n" +
395             "                },\n" +
396             "                {\n" +
397             "                    \"leaf27\": \"abc\"\n" +
398             "                }\n" +
399             "            ],\n" +
400             "            \"ll9\": [\n" +
401             "                \"abc\",\n" +
402             "                \"abc\"\n" +
403             "            ],\n" +
404             "            \"leaf28\": \"abc\"\n" +
405             "        },\n" +
406             "        \"ll10\": [\n" +
407             "            \"abc\",\n" +
408             "            \"abc\"\n" +
409             "        ]\n" +
410             "    }\n" +
411             "}";
412
413     static final String DECODE_FROM_JSON_RPC = "{\n" +
414             "    \"test-yang:output\": {\n" +
415             "        \"cont16\": {\n" +
416             "            \"leaf32\": \"abc\"\n" +
417             "        },\n" +
418             "        \"list11\": [\n" +
419             "            {\n" +
420             "                \"leaf33\": \"abc\"\n" +
421             "            },\n" +
422             "            {\n" +
423             "                \"leaf33\": \"abc\"\n" +
424             "            }\n" +
425             "        ],\n" +
426             "        \"leaf34\": \"abc\",\n" +
427             "        \"ll11\": [\n" +
428             "            \"abc\",\n" +
429             "            \"abc\"\n" +
430             "        ],\n" +
431             "        \"cont17\": {\n" +
432             "            \"leaf35\": \"abc\"\n" +
433             "        },\n" +
434             "        \"cont13\": {\n" +
435             "            \"cont12\": {\n" +
436             "                \"leaf26\": \"abc\"\n" +
437             "            },\n" +
438             "            \"list9\": [\n" +
439             "                {\n" +
440             "                    \"leaf27\": \"abc\"\n" +
441             "                },\n" +
442             "                {\n" +
443             "                    \"leaf27\": \"abc\"\n" +
444             "                }\n" +
445             "            ],\n" +
446             "            \"ll9\": [\n" +
447             "                \"abc\",\n" +
448             "                \"abc\"\n" +
449             "            ],\n" +
450             "            \"leaf28\": \"abc\"\n" +
451             "        }\n" +
452             "    }\n" +
453             "}";
454
455     static final String ENCODE_TO_XML_RPC = "<?xml version=\"1.0\" encoding" +
456             "=\"UTF-8\" standalone=\"no\"?>\n" +
457             "<input xmlns=\"urn:opendaylight:params:xml:ns:yang:test\">\n" +
458             "    <leaf30>abc</leaf30>\n" +
459             "    <list10>\n" +
460             "        <leaf29>abc</leaf29>\n" +
461             "    </list10>\n" +
462             "    <list10>\n" +
463             "        <leaf29>abc</leaf29>\n" +
464             "    </list10>\n" +
465             "    <cont15>\n" +
466             "        <leaf31>abc</leaf31>\n" +
467             "    </cont15>\n" +
468             "    <cont14>\n" +
469             "        <leaf28>abc</leaf28>\n" +
470             "    </cont14>\n" +
471             "    <cont13>\n" +
472             "        <list9>\n" +
473             "            <leaf27>abc</leaf27>\n" +
474             "        </list9>\n" +
475             "        <list9>\n" +
476             "            <leaf27>abc</leaf27>\n" +
477             "        </list9>\n" +
478             "        <ll9>abc</ll9>\n" +
479             "        <ll9>abc</ll9>\n" +
480             "        <leaf28>abc</leaf28>\n" +
481             "    </cont13>\n" +
482             "    <ll10>abc</ll10>\n" +
483             "    <ll10>abc</ll10>\n" +
484             "</input>\n";
485
486     static final String DECODE_FROM_XML_RPC = "<?xml version=\"1.0\" encodi" +
487             "ng=\"UTF-8\" standalone=\"no\"?>\n" +
488             "<output xmlns=\"urn:opendaylight:params:xml:ns:yang:test\">\n" +
489             "    <cont16>\n" +
490             "        <leaf32>abc</leaf32>\n" +
491             "    </cont16>\n" +
492             "    <list11>\n" +
493             "        <leaf33>abc</leaf33>\n" +
494             "    </list11>\n" +
495             "    <list11>\n" +
496             "        <leaf33>abc</leaf33>\n" +
497             "    </list11>\n" +
498             "    <leaf34>abc</leaf34>\n" +
499             "    <ll11>abc</ll11>\n" +
500             "    <ll11>abc</ll11>\n" +
501             "    <cont17>\n" +
502             "        <leaf35>abc</leaf35>\n" +
503             "    </cont17>\n" +
504             "    <cont13>\n" +
505             "        <cont12>\n" +
506             "            <leaf26>abc</leaf26>\n" +
507             "        </cont12>\n" +
508             "        <list9>\n" +
509             "            <leaf27>abc</leaf27>\n" +
510             "        </list9>\n" +
511             "        <list9>\n" +
512             "            <leaf27>abc</leaf27>\n" +
513             "        </list9>\n" +
514             "        <ll9>abc</ll9>\n" +
515             "        <ll9>abc</ll9>\n" +
516             "        <leaf28>abc</leaf28>\n" +
517             "    </cont13>\n" +
518             "</output>";
519
520     /**
521      * Adds the specified number of space required for a req in each line.
522      *
523      * @param req request message
524      * @param i   number of space
525      * @return space appended string
526      */
527     private static String addSpace(String req, int i) {
528         StringBuilder space = new StringBuilder("");
529         for (int sp = 0; sp < i; sp++) {
530             space = space.append(" ");
531         }
532         return req.replaceAll("\n", "\n" + space.toString());
533     }
534 }