Restapi-call-node: Support for "" values in JSON 04/125804/1 honolulu
authorStan Bonev (sb5356) <sb5356@att.com>
Fri, 19 Nov 2021 20:33:12 +0000 (15:33 -0500)
committerKAPIL SINGAL <ks220y@att.com>
Fri, 19 Nov 2021 22:40:05 +0000 (22:40 +0000)
Issue-ID: CCSDK-3529
Signed-off-by: Stan Bonev (sb5356) <sb5356@att.com>
Change-Id: I30e903a4b7028c707f73f3516ef11eecf032c5c2
(cherry picked from commit d4c076118ff231d9ab5165a873185515896380eb)

plugins/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java
plugins/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/XmlJsonUtil.java
plugins/restapi-call-node/provider/src/test/java/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java

index e42dfa4..1f38b7f 100755 (executable)
@@ -605,8 +605,9 @@ public class RestapiCallNode implements SvcLogicJavaPlugin {
             }
 
             String var1 = template.substring(i1 + 2, i2);
+            boolean keepEmpty = var1.startsWith("~");
             String value1 = format == Format.XML ? XmlJsonUtil.getXml(mm, var1) : XmlJsonUtil.getJson(mm, var1);
-            if (value1 == null || value1.trim().length() == 0) {
+            if (value1 == null || (value1.trim().length() == 0 && !keepEmpty)) {
                 // delete the whole element (line)
                 int i3 = template.lastIndexOf('\n', i1);
                 if (i3 < 0) {
index 9870221..d6ef746 100644 (file)
@@ -51,6 +51,12 @@ public final class XmlJsonUtil {
     }
 
     public static String getJson(Map<String, String> varmap, String var) {
+        boolean keepEmpty = false;
+        if (var.startsWith("~")) {
+            var = var.substring(1);
+            keepEmpty = true;
+        }
+
         boolean escape = true;
         if (var.startsWith("'")) {
             var = var.substring(1);
@@ -64,7 +70,7 @@ public final class XmlJsonUtil {
         }
 
         Object o = createStructure(varmap, var);
-        return generateJson(o, escape, quotes);
+        return generateJson(o, escape, quotes, keepEmpty);
     }
 
     private static Object createStructure(Map<String, String> flatmap, String var) {
@@ -286,11 +292,11 @@ public final class XmlJsonUtil {
 
         return null;
     }
-    private static String generateJson(Object o, boolean escape, boolean quotes) {
+    private static String generateJson(Object o, boolean escape, boolean quotes, boolean keepEmpty) {
         if (o == null) {
             return null;
         }
-        if (o instanceof String && ((String) o).length() == 0) {
+        if (o instanceof String && ((String) o).length() == 0 && !keepEmpty) {
             return null;
         }
 
index a993bb9..595fc1d 100755 (executable)
@@ -601,4 +601,59 @@ public class TestRestapiCallNode {
         assertTrue(RestapiCallNode.containsMultipleUrls("https://wiki.onap.org/test=4,5,6,http://localhost:7001/test=1,2,3,http://wiki.onap.org/test=7,8,9,10"));
     }
 
+    @Test
+    public void testKeepEmptyValue() throws Exception {
+        log.info("================= Testing keeping empty values =======================");
+
+        String template = "{\n" +
+            "   \"name1\": \"value1\",\n" +
+            "   \"name2\": ${empty},\n" +
+            "   \"name3\": ${~empty},\n" +
+            "   \"name4\": {\n" +
+            "       \"name41\": \"value41\",\n" +
+            "       \"name42\": ${~empty},\n" +
+            "       \"name43\": ${~not_empty}\n" +
+            "   },\n" +
+            "   \"name5\": {\n"+
+            "       \"name51\": ${~empty},\n"+
+            "       \"name52\": ${empty}\n"+
+            "   },\n" +
+            "   \"name6\": {\n"+
+            "       \"name61\": ${empty},\n"+
+            "       \"name62\": ${empty}\n"+
+            "   },\n" +
+            "   \"name7\": \"${\"not_empty}\",\n" +
+            "   \"name8\": \"${~\"not_empty}\",\n" +
+            "   \"name9\": \"${\"empty}\",\n" +
+            "   \"name10\": \"${~\"empty}\"\n" +
+            "}";
+
+        String expect = "{\n" +
+            "   \"name1\": \"value1\",\n" +
+            "   \"name3\": \"\",\n" +
+            "   \"name4\": {\n" +
+            "       \"name41\": \"value41\",\n" +
+            "       \"name42\": \"\",\n" +
+            "       \"name43\": \"some value\"\n" +
+            "   },\n" +
+            "   \"name5\": {\n" +
+            "       \"name51\": \"\"\n" +
+            "   },\n" +
+            "   \"name7\": \"some value\",\n" +
+            "   \"name8\": \"some value\",\n" +
+            "   \"name10\": \"\"\n" +
+            "}";
+
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("empty", "");
+        ctx.setAttribute("not_empty", "some value");
+        
+        RestapiCallNode rcn = new RestapiCallNode();
+        String req = rcn.buildXmlJsonRequest(ctx, template, Format.JSON);
+        
+        log.info("Result:\n" + req);
+        log.info("==================================================================");
+
+        assertEquals(expect, req);
+    }
 }