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 = "{\n" +
 
  30             "    \"identity-test:con1\": {\n" +
 
  31             "        \"interfaces\": {\n" +
 
  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" +
 
  65             "    \"identity-test:l\": \"abc\"\n" +
 
  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" +
 
  73             "            <iden>optical</iden>\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" +
 
  86             "            <iden>214748364</iden>\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" +
 
  99             "    <interface xmlns:yangid=\"identity:list:ns:test:json:ser\">" +
 
 100             "yangid:physical</interface>\n" +
 
 103     static final String ENCODE_TO_JSON_YANG_COMMON = "\n    " +
 
 104             "\"test-augment:cont13\": {\n" +
 
 111             "                \"leaf27\": \"abc\"\n" +
 
 114             "                \"leaf27\": \"abc\"\n" +
 
 117             "        \"leaf28\": \"abc\",\n" +
 
 119             "            \"leaf26\": \"abc\"\n" +
 
 122             "    \"test-augment:list7\": [\n" +
 
 124             "            \"leaf14\": \"test\"\n" +
 
 127             "            \"leaf14\": \"create\"\n" +
 
 130             "    \"test-augment:leaf15\": \"abc\",\n" +
 
 131             "    \"test-augment:ll6\": [\n" +
 
 132             "        \"unbounded\",\n" +
 
 135             "    \"test-augment:cont5\": {\n" +
 
 136             "        \"leaf13\": \"true\"\n" +
 
 139     static final String ENCODE_TO_JSON_YANG_AUG_POST = "{\n" +
 
 140             "    \"test-yang:leaf10\": \"abc\"," +
 
 141             ENCODE_TO_JSON_YANG_COMMON + "\n}";
 
 143     static final String ENCODE_TO_JSON_YANG = "{\n" +
 
 144             "    \"test-yang:cont2\": {\n" +
 
 151             "                \"leaf1\": \"true\",\n" +
 
 158             "                        \"leaf9\": \"abc\"\n" +
 
 161             "                        \"leaf9\": \"abc\"\n" +
 
 164             "                \"leaf3\": \"abc\",\n" +
 
 165             "                \"leaf2\": \"abc\",\n" +
 
 168             "                        \"leaf8\": \"abc\"\n" +
 
 171             "                        \"leaf8\": \"abc\"\n" +
 
 175             "                    \"leaf11\": \"abc\"\n" +
 
 183             "                \"leaf1\": \"true\",\n" +
 
 188             "                \"leaf3\": \"abc\",\n" +
 
 191             "                        \"leaf9\": \"abc\"\n" +
 
 194             "                        \"leaf9\": \"abc\"\n" +
 
 199             "                        \"leaf8\": \"abc\"\n" +
 
 202             "                        \"leaf8\": \"abc\"\n" +
 
 205             "                \"leaf2\": \"abc\",\n" +
 
 207             "                    \"leaf11\": \"abc\"\n" +
 
 220             "            \"leaf10\": \"abc\"," +
 
 221             addSpace(ENCODE_TO_JSON_YANG_COMMON, 8) + "\n" +
 
 228             "            \"leaf10\": \"abc\"\n" +
 
 230             "        \"leaf5\": \"abc\",\n" +
 
 233             "                \"leaf4\": \"abc\"\n" +
 
 236             "                \"leaf4\": \"abc\"\n" +
 
 239             "        \"leaf12\": \"abc\",\n" +
 
 240             "        \"leaf6\": \"abc\",\n" +
 
 243             "                \"leaf11\": \"abc\"\n" +
 
 246             "                \"leaf11\": \"abc\"\n" +
 
 252     static final String ENCODE_TO_XML_YANG_COMMON = "\n" +
 
 253             "<cont13 xmlns=\"urn:opendaylight:params:xml:ns:yang:" +
 
 255             "    <ll9>abc</ll9>\n" +
 
 256             "    <ll9>abc</ll9>\n" +
 
 258             "        <leaf27>abc</leaf27>\n" +
 
 261             "        <leaf27>abc</leaf27>\n" +
 
 263             "    <leaf28>abc</leaf28>\n" +
 
 265             "        <leaf26>abc</leaf26>\n" +
 
 268             "<list7 xmlns=\"urn:opendaylight:params:xml:ns:yang:a" +
 
 270             "    <leaf14>test</leaf14>\n" +
 
 272             "<list7 xmlns=\"urn:opendaylight:params:xml:ns:yang:a" +
 
 274             "    <leaf14>create</leaf14>\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" +
 
 282             "<cont5 xmlns=\"urn:opendaylight:params:xml:ns:yang:a" +
 
 284             "    <leaf13>true</leaf13>\n" +
 
 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" +
 
 291             ENCODE_TO_XML_YANG_COMMON + "\n";
 
 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" +
 
 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" +
 
 303             "            <leaf9>abc</leaf9>\n" +
 
 306             "            <leaf9>abc</leaf9>\n" +
 
 308             "        <leaf3>abc</leaf3>\n" +
 
 309             "        <leaf2>abc</leaf2>\n" +
 
 311             "            <leaf8>abc</leaf8>\n" +
 
 314             "            <leaf8>abc</leaf8>\n" +
 
 317             "            <leaf11>abc</leaf11>\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" +
 
 328             "            <leaf9>abc</leaf9>\n" +
 
 331             "            <leaf9>abc</leaf9>\n" +
 
 334             "            <leaf8>abc</leaf8>\n" +
 
 337             "            <leaf8>abc</leaf8>\n" +
 
 339             "        <leaf2>abc</leaf2>\n" +
 
 341             "            <leaf11>abc</leaf11>\n" +
 
 344             "    <ll3>abc</ll3>\n" +
 
 345             "    <ll3>abc</ll3>\n" +
 
 346             "    <ll5>abc</ll5>\n" +
 
 347             "    <ll5>abc</ll5>\n" +
 
 349             "        <leaf10>abc</leaf10>"+
 
 350             addSpace(ENCODE_TO_XML_YANG_COMMON, 8) + "\n" +
 
 352             "    <ll4>abc</ll4>\n" +
 
 353             "    <ll4>abc</ll4>\n" +
 
 355             "        <leaf10>abc</leaf10>\n" +
 
 357             "    <leaf5>abc</leaf5>\n" +
 
 359             "        <leaf4>abc</leaf4>\n" +
 
 362             "        <leaf4>abc</leaf4>\n" +
 
 364             "    <leaf12>abc</leaf12>\n" +
 
 365             "    <leaf6>abc</leaf6>\n" +
 
 367             "        <leaf11>abc</leaf11>\n" +
 
 370             "        <leaf11>abc</leaf11>\n" +
 
 374     static final String ENCODE_TO_JSON_RPC = "{\n" +
 
 375             "    \"test-yang:input\": {\n" +
 
 376             "        \"leaf30\": \"abc\",\n" +
 
 379             "                \"leaf29\": \"abc\"\n" +
 
 382             "                \"leaf29\": \"abc\"\n" +
 
 386             "            \"leaf31\": \"abc\"\n" +
 
 389             "            \"leaf28\": \"abc\"\n" +
 
 394             "                    \"leaf27\": \"abc\"\n" +
 
 397             "                    \"leaf27\": \"abc\"\n" +
 
 404             "            \"leaf28\": \"abc\"\n" +
 
 413     static final String DECODE_FROM_JSON_RPC = "{\n" +
 
 414             "    \"test-yang:output\": {\n" +
 
 416             "            \"leaf32\": \"abc\"\n" +
 
 420             "                \"leaf33\": \"abc\"\n" +
 
 423             "                \"leaf33\": \"abc\"\n" +
 
 426             "        \"leaf34\": \"abc\",\n" +
 
 432             "            \"leaf35\": \"abc\"\n" +
 
 436             "                \"leaf26\": \"abc\"\n" +
 
 440             "                    \"leaf27\": \"abc\"\n" +
 
 443             "                    \"leaf27\": \"abc\"\n" +
 
 450             "            \"leaf28\": \"abc\"\n" +
 
 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" +
 
 460             "        <leaf29>abc</leaf29>\n" +
 
 463             "        <leaf29>abc</leaf29>\n" +
 
 466             "        <leaf31>abc</leaf31>\n" +
 
 469             "        <leaf28>abc</leaf28>\n" +
 
 473             "            <leaf27>abc</leaf27>\n" +
 
 476             "            <leaf27>abc</leaf27>\n" +
 
 478             "        <ll9>abc</ll9>\n" +
 
 479             "        <ll9>abc</ll9>\n" +
 
 480             "        <leaf28>abc</leaf28>\n" +
 
 482             "    <ll10>abc</ll10>\n" +
 
 483             "    <ll10>abc</ll10>\n" +
 
 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" +
 
 490             "        <leaf32>abc</leaf32>\n" +
 
 493             "        <leaf33>abc</leaf33>\n" +
 
 496             "        <leaf33>abc</leaf33>\n" +
 
 498             "    <leaf34>abc</leaf34>\n" +
 
 499             "    <ll11>abc</ll11>\n" +
 
 500             "    <ll11>abc</ll11>\n" +
 
 502             "        <leaf35>abc</leaf35>\n" +
 
 506             "            <leaf26>abc</leaf26>\n" +
 
 509             "            <leaf27>abc</leaf27>\n" +
 
 512             "            <leaf27>abc</leaf27>\n" +
 
 514             "        <ll9>abc</ll9>\n" +
 
 515             "        <ll9>abc</ll9>\n" +
 
 516             "        <leaf28>abc</leaf28>\n" +
 
 521      * Adds the specified number of space required for a req in each line.
 
 523      * @param req request message
 
 524      * @param i   number of space
 
 525      * @return space appended string
 
 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(" ");
 
 532         return req.replaceAll("\n", "\n" + space.toString());