Add mandatory empty construct to json template 87/69787/2
authorshashikanth.vh <shashikanth.vh@huawei.com>
Thu, 4 Oct 2018 05:10:18 +0000 (10:40 +0530)
committerShashikanth VH <shashikanth.vh@huawei.com>
Thu, 4 Oct 2018 05:44:01 +0000 (05:44 +0000)
Restapicallnode remove empty parameter from json template,
if template mandates empty parameter to be present then it
should not be removed.

Change-Id: I075e0858f956eb3aff87d2a7387b55caa3310568
Issue-ID: CCSDK-607
Signed-off-by: shashikanth.vh <shashikanth.vh@huawei.com>
restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java
restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/XmlJsonUtil.java
restapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/sshapicall/TestRestapiCallNode.java
restapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/sshapicall/TestXmlJsonUtil.java
restapi-call-node/provider/src/test/resources/sdwan-site.json [new file with mode: 0644]
restapi-call-node/provider/src/test/resources/sdwan-vpn-topology.json [new file with mode: 0644]

index e550343..9a89af6 100644 (file)
@@ -392,6 +392,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin {
         throws SvcLogicException {
         log.info("Building {} started", format);
         long t1 = System.currentTimeMillis();
+        String originalTemplate = template;
 
         template = expandRepeats(ctx, template, 1);
 
@@ -439,7 +440,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin {
         }
 
         String req = format == Format.XML
-            ? XmlJsonUtil.removeEmptyStructXml(ss.toString()) : XmlJsonUtil.removeEmptyStructJson(ss.toString());
+            ? XmlJsonUtil.removeEmptyStructXml(ss.toString()) : XmlJsonUtil.removeEmptyStructJson(originalTemplate, ss.toString());
 
         if (format == Format.JSON) {
             req = XmlJsonUtil.removeLastCommaJson(req);
index 8a0ca8d..4712b42 100644 (file)
@@ -272,7 +272,7 @@ public final class XmlJsonUtil {
         return sb.toString();
     }
 
-    public static String removeEmptyStructJson(String s) {
+    public static String removeEmptyStructJson(String template, String s) {
         int k = 0;
         while (k < s.length()) {
             boolean curly = true;
@@ -304,8 +304,13 @@ public final class XmlJsonUtil {
                         if (i5 < 0)
                             i5 = s.length();
 
-                        s = s.substring(0, i4) + s.substring(i5);
-                        k = 0;
+                        /*If template mandates empty construct to be present, those should not be removed.*/
+                        if ((template != null) && template.contains(s.substring(i4))) {
+                            k = i1 + 1;
+                        } else {
+                            s = s.substring(0, i4) + s.substring(i5);
+                            k = 0;
+                        }
                     } else
                         k = i1 + 1;
                 } else
index 5c6c4e2..4008c56 100644 (file)
@@ -51,6 +51,61 @@ public class TestRestapiCallNode {
         rcn.sendRequest(p, ctx);
     }
 
+    @Test
+    public void testJsonSdwanVpnTopologyTemplate() throws SvcLogicException {
+        SvcLogicContext ctx = new SvcLogicContext();
+
+        ctx.setAttribute("prop.topology", "topoType");
+
+        ctx.setAttribute("prop.roles_length", "1");
+        ctx.setAttribute("prop.roles[0]", "role1");
+
+        ctx.setAttribute("prop.siteAttachement_length", "2");
+
+        ctx.setAttribute("prop.siteAttachement[0].siteId", "site1");
+        ctx.setAttribute("prop.siteAttachement[0].roles_length", "0");
+        ctx.setAttribute("prop.siteAttachement[0].roles[0]", "role1");
+        ctx.setAttribute("prop.siteAttachement[0].roles[1]", "role3");
+
+        ctx.setAttribute("prop.siteAttachement[1].siteId", "site2");
+        ctx.setAttribute("prop.siteAttachement[1].roles_length", "1");
+        ctx.setAttribute("prop.siteAttachement[1].roles[0]", "role2");
+
+        Map<String, String> p = new HashMap<String, String>();
+        p.put("templateFileName", "src/test/resources/sdwan-vpn-topology.json");
+        p.put("restapiUrl", "http://echo.getpostman.com");
+        p.put("restapiUser", "user1");
+        p.put("restapiPassword", "abc123");
+        p.put("format", "json");
+        p.put("httpMethod", "post");
+        p.put("responsePrefix", "response");
+        p.put("skipSending", "true");
+
+        RestapiCallNode rcn = new RestapiCallNode();
+        rcn.sendRequest(p, ctx);
+    }
+
+    @Test
+    public void testJsonSdwanSiteTemplate() throws SvcLogicException {
+        SvcLogicContext ctx = new SvcLogicContext();
+
+        ctx.setAttribute("prop.name", "site1");
+
+
+        Map<String, String> p = new HashMap<String, String>();
+        p.put("templateFileName", "src/test/resources/sdwan-site.json");
+        p.put("restapiUrl", "http://echo.getpostman.com");
+        p.put("restapiUser", "user1");
+        p.put("restapiPassword", "abc123");
+        p.put("format", "json");
+        p.put("httpMethod", "post");
+        p.put("responsePrefix", "response");
+        p.put("skipSending", "true");
+
+        RestapiCallNode rcn = new RestapiCallNode();
+        rcn.sendRequest(p, ctx);
+    }
+
     @Test
     public void testJsonTemplate() throws SvcLogicException {
         SvcLogicContext ctx = new SvcLogicContext();
index bd9ff1c..31fa2f9 100644 (file)
@@ -254,7 +254,7 @@ public class TestXmlJsonUtil {
                 "}\r\n" +
                 "";
 
-        String xmlout = XmlJsonUtil.removeEmptyStructJson(xmlin);
+        String xmlout = XmlJsonUtil.removeEmptyStructJson(null, xmlin);
         log.info(xmlout);
 
         Assert.assertEquals(xmloutexpected, xmlout);
diff --git a/restapi-call-node/provider/src/test/resources/sdwan-site.json b/restapi-call-node/provider/src/test/resources/sdwan-site.json
new file mode 100644 (file)
index 0000000..238ec76
--- /dev/null
@@ -0,0 +1,10 @@
+{
+    "sites": [
+        {
+            "name": ${prop.name},
+            "controlPoints": []
+        }
+    ]
+}
+
+
diff --git a/restapi-call-node/provider/src/test/resources/sdwan-vpn-topology.json b/restapi-call-node/provider/src/test/resources/sdwan-vpn-topology.json
new file mode 100644 (file)
index 0000000..4260d2f
--- /dev/null
@@ -0,0 +1,23 @@
+{
+  "vpnId": ${prop.vpn1Id},
+  "areas": [
+            {
+              "name": "area1",
+              "areaType": "leaf",
+              "topologyType": ${prop.topology},
+              "siteAttachements": [${repeat:prop.siteAttachement_length:
+              {
+                "siteId": ${prop.siteAttachement[${1}].siteId},
+                "role": [${repeat:prop.siteAttachement[${1}].roles_length:
+                         ${prop.siteAttachement[${1}].roles[${2}]},
+                        }
+                        ],
+                "workMode": "with-interconnection",
+                "sitePriority": "active",
+                "borders": "null"
+              },
+            }
+          ]
+        }
+  ]
+}
\ No newline at end of file