Resolving root level augment nodes 71/69971/2
authorjanani b <janani.b@huawei.com>
Mon, 8 Oct 2018 08:47:51 +0000 (14:17 +0530)
committerJanani B <janani.b@huawei.com>
Mon, 8 Oct 2018 09:14:37 +0000 (09:14 +0000)
Issue fix for root level augment nodes being added to the data format

Issue-ID: CCSDK-324

Change-Id: I1c69f97739c6cb56c74b5f22bc4283f2f927719b
Signed-off-by: janani b <janani.b@huawei.com>
restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeWalker.java
restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java
restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java

index 8497805..f234526 100644 (file)
@@ -22,6 +22,7 @@ package org.onap.ccsdk.sli.plugins.yangserializers.pnserializer;
 
 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
+import java.util.Collection;
 import java.util.Map;
 
 /**
@@ -58,6 +59,32 @@ public class DefaultPropertiesNodeWalker<T extends NodeChild> implements Propert
                 listener.exitPropertiesNode(node);
             }
         }
+        if (propertiesNode instanceof RootNode) {
+            processAugments(propertiesNode, listener);
+        }
+    }
+
+    /**
+     * Processes the augments present in the root node.
+     *
+     * @param node     root node
+     * @param listener properties node listener
+     * @throws SvcLogicException when augment node walking fails
+     */
+    private void processAugments(PropertiesNode node,
+                                 PropertiesNodeListener listener)
+            throws SvcLogicException {
+        for (Map.Entry<Object, Collection<PropertiesNode>>
+                augToChild : node.augmentations().asMap().entrySet()) {
+            Collection<PropertiesNode> child = augToChild.getValue();
+            if (!child.isEmpty()) {
+                for (PropertiesNode p : child) {
+                    listener.enterPropertiesNode(p);
+                    walkChildNode(listener, p);
+                    listener.exitPropertiesNode(p);
+                }
+            }
+        }
     }
 
     /**
index 6c11206..40f4c4b 100644 (file)
@@ -50,6 +50,7 @@ import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormat
 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_ID;
 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_RPC;
 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_YANG;
+import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_YANG_AUG_POST;
 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_ID;
 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_RPC;
 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_YANG;
@@ -217,6 +218,25 @@ public class DataFormatSerializerTest {
         assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG));
     }
 
+    /**
+     * Verifies encoding of parameters to JSON data format with augment as
+     * root child.
+     *
+     * @throws SvcLogicException when test case fails
+     */
+    @Test
+    public void encodeToJsonWithAugAsRootChild() throws SvcLogicException {
+        String pre = "test-yang:cont1.cont2.";
+        SvcLogicContext ctx = createAttListYang(pre);
+        p.put("dirPath", "src/test/resources");
+        p.put("format", "json");
+        p.put("httpMethod", "post");
+        p.put("restapiUrl", "http://echo.getpostman" +
+                ".com/restconf/operations/test-yang:cont1/cont2/cont4");
+        restconf.sendRequest(p, ctx);
+        assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG_AUG_POST));
+    }
+
     /**
      * Verifies decoding of parameters from JSON data format with containers,
      * grouping and augment.
index c7efdeb..7e6445f 100644 (file)
@@ -100,6 +100,46 @@ public final class DataFormatUtilsTest {
             "yangid:physical</interface>\n" +
             "</con1>\n";
 
+    static final String ENCODE_TO_JSON_YANG_COMMON = "\n    " +
+            "\"test-augment:cont13\": {\n" +
+            "        \"ll9\": [\n" +
+            "            \"abc\",\n" +
+            "            \"abc\"\n" +
+            "        ],\n" +
+            "        \"list9\": [\n" +
+            "            {\n" +
+            "                \"leaf27\": \"abc\"\n" +
+            "            },\n" +
+            "            {\n" +
+            "                \"leaf27\": \"abc\"\n" +
+            "            }\n" +
+            "        ],\n" +
+            "        \"leaf28\": \"abc\",\n" +
+            "        \"cont12\": {\n" +
+            "            \"leaf26\": \"abc\"\n" +
+            "        }\n" +
+            "    },\n" +
+            "    \"test-augment:list7\": [\n" +
+            "        {\n" +
+            "            \"leaf14\": \"test\"\n" +
+            "        },\n" +
+            "        {\n" +
+            "            \"leaf14\": \"create\"\n" +
+            "        }\n" +
+            "    ],\n" +
+            "    \"test-augment:leaf15\": \"abc\",\n" +
+            "    \"test-augment:ll6\": [\n" +
+            "        \"unbounded\",\n" +
+            "        \"8\"\n" +
+            "    ],\n" +
+            "    \"test-augment:cont5\": {\n" +
+            "        \"leaf13\": \"true\"\n" +
+            "    }";
+
+    static final String ENCODE_TO_JSON_YANG_AUG_POST = "{\n" +
+            "    \"test-yang:leaf10\": \"abc\"," +
+            ENCODE_TO_JSON_YANG_COMMON + "\n}";
+
     static final String ENCODE_TO_JSON_YANG = "{\n" +
             "    \"test-yang:cont2\": {\n" +
             "        \"list1\": [\n" +
@@ -177,41 +217,8 @@ public final class DataFormatUtilsTest {
             "            \"abc\"\n" +
             "        ],\n" +
             "        \"cont4\": {\n" +
-            "            \"leaf10\": \"abc\",\n" +
-            "            \"test-augment:cont13\": {\n" +
-            "                \"ll9\": [\n" +
-            "                    \"abc\",\n" +
-            "                    \"abc\"\n" +
-            "                ],\n" +
-            "                \"list9\": [\n" +
-            "                    {\n" +
-            "                        \"leaf27\": \"abc\"\n" +
-            "                    },\n" +
-            "                    {\n" +
-            "                        \"leaf27\": \"abc\"\n" +
-            "                    }\n" +
-            "                ],\n" +
-            "                \"leaf28\": \"abc\",\n" +
-            "                \"cont12\": {\n" +
-            "                    \"leaf26\": \"abc\"\n" +
-            "                }\n" +
-            "            },\n" +
-            "            \"test-augment:list7\": [\n" +
-            "                {\n" +
-            "                    \"leaf14\": \"test\"\n" +
-            "                },\n" +
-            "                {\n" +
-            "                    \"leaf14\": \"create\"\n" +
-            "                }\n" +
-            "            ],\n" +
-            "            \"test-augment:leaf15\": \"abc\",\n" +
-            "            \"test-augment:ll6\": [\n" +
-            "                \"unbounded\",\n" +
-            "                \"8\"\n" +
-            "            ],\n" +
-            "            \"test-augment:cont5\": {\n" +
-            "                \"leaf13\": \"true\"\n" +
-            "            }\n" +
+            "            \"leaf10\": \"abc\"," +
+            addSpace(ENCODE_TO_JSON_YANG_COMMON, 8) + "\n" +
             "        },\n" +
             "        \"ll4\": [\n" +
             "            \"abc\",\n" +
@@ -501,4 +508,19 @@ public final class DataFormatUtilsTest {
             "        <leaf28>abc</leaf28>\n" +
             "    </cont13>\n" +
             "</output>";
+
+    /**
+     * Adds the specified number of space required for a req in each line.
+     *
+     * @param req request message
+     * @param i   number of space
+     * @return space appended string
+     */
+    private static String addSpace(String req, int i) {
+        StringBuilder space = new StringBuilder("");
+        for (int sp = 0; sp < i; sp++) {
+            space = space.append(" ");
+        }
+        return req.replaceAll("\n", "\n" + space.toString());
+    }
 }