Escaping multi-line string 40/101840/7
authorSingal, Kapil (ks220y) <ks220y@att.com>
Mon, 17 Feb 2020 15:41:38 +0000 (10:41 -0500)
committerKAPIL SINGAL <ks220y@att.com>
Tue, 18 Feb 2020 17:47:05 +0000 (17:47 +0000)
Supporting multiline json string if embedding within Rest payload template

Change-Id: I6a96f58732734fca0127d57fa5de3ba3cb7276c4
Issue-ID: CCSDK-2103
Signed-off-by: Singal, Kapil (ks220y) <ks220y@att.com>
restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/XmlJsonUtil.java
restapi-call-node/provider/src/test/java/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java
restapi-call-node/provider/src/test/resources/testMultiLineEmbeddedTemplate.json [new file with mode: 0644]

index bc6afd8..ff32248 100644 (file)
@@ -25,6 +25,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import org.apache.commons.text.StringEscapeUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -142,7 +143,7 @@ public final class XmlJsonUtil {
         }
 
         if (o instanceof String) {
-            return escape ? escapeXml((String) o) : (String) o;
+            return escape ? StringEscapeUtils.escapeXml10((String) o) : (String) o;
         };
 
         if (o instanceof Map) {
@@ -152,7 +153,7 @@ public final class XmlJsonUtil {
                 Object v = entry.getValue();
                 String key = entry.getKey();
                 if (v instanceof String) {
-                    String s = escape ? escapeXml((String) v) : (String) v;
+                    String s = escape ? StringEscapeUtils.escapeXml10((String) v) : (String) v;
                     ss.append(pad(indent)).append('<').append(key).append('>');
                     ss.append(s);
                     ss.append("</").append(key).append('>').append('\n');
@@ -190,7 +191,7 @@ public final class XmlJsonUtil {
     @SuppressWarnings("unchecked")
     private static void generateJson(StringBuilder ss, Object o, int indent, boolean padFirst, boolean escape, boolean quotes) {
         if (o instanceof String) {
-            String s = escape ? escapeJson((String) o) : (String) o;
+            String s = escape ? StringEscapeUtils.escapeJson((String) o) : (String) o;
             if (padFirst) {
                 ss.append(pad(indent));
             }
@@ -396,21 +397,6 @@ public final class XmlJsonUtil {
         return s;
     }
 
-    private static String escapeXml(String v) {
-        String s = v.replaceAll("&", "&amp;");
-        s = s.replaceAll("<", "&lt;");
-        s = s.replaceAll("'", "&apos;");
-        s = s.replaceAll("\"", "&quot;");
-        s = s.replaceAll(">", "&gt;");
-        return s;
-    }
-
-    private static String escapeJson(String v) {
-        String s = v.replaceAll("\\\\", "\\\\\\\\");
-        s = s.replaceAll("\"", "\\\\\"");
-        return s;
-    }
-
     private static String pad(int n) {
         StringBuilder s = new StringBuilder();
         for (int i = 0; i < n; i++) {
index 8cabaad..da7b80e 100755 (executable)
@@ -528,7 +528,27 @@ public class TestRestapiCallNode {
         RestapiCallNode rcn = new RestapiCallNode();
         String request = rcn.buildXmlJsonRequest(ctx, rcn.readFile("src/test/resources/testEmbeddedTemplate.json"), Format.JSON);
         //This will throw a JSONException and fail the test case if rest api call node doesn't form valid JSON
-        JSONObject requestObj = new JSONObject(request);
+        assertNotNull(new JSONObject(request));
+    }
+
+    @Test
+    public void testMultiLineEmbeddedJsonTemplate() throws Exception {
+        SvcLogicContext ctx = new SvcLogicContext();
+        String complexObj = "{\n"
+                            + "  \"image_name\": \"Ubuntu 14.04\",\n"
+                            + "  \"service-instance-id\": \"1\",\n"
+                            + "  \"vnf-model-customization-uuid\": \"2f\",\n"
+                            + "  \"vnf-id\": \"3b\"\n"
+                            + "}";
+        ctx.setAttribute("reqId", "1235");
+        ctx.setAttribute("subReqId", "054243");
+        ctx.setAttribute("actionName", "CREATE");
+        ctx.setAttribute("myPrefix", "2016-09-09 16:30:35.0");
+        ctx.setAttribute("complexObj", complexObj);
+        RestapiCallNode rcn = new RestapiCallNode();
+        String request = rcn.buildXmlJsonRequest(ctx, rcn.readFile("src/test/resources/testMultiLineEmbeddedTemplate.json"), Format.JSON);
+        //This will throw a JSONException and fail the test case if rest api call node doesn't form valid JSON
+        assertNotNull(new JSONObject(request));
     }
 
 }
diff --git a/restapi-call-node/provider/src/test/resources/testMultiLineEmbeddedTemplate.json b/restapi-call-node/provider/src/test/resources/testMultiLineEmbeddedTemplate.json
new file mode 100644 (file)
index 0000000..84ae3a4
--- /dev/null
@@ -0,0 +1,20 @@
+{
+  "commonHeader": {
+    "origin": "earth",
+    "requestId": ${reqId},
+    "subRequestId": ${subReqId}
+  },
+  "actions": {
+    "actionName": ${actionName},
+    "mode": "sync"
+  },
+  "payload": {
+    "assignment-request": {
+      "prefix": [
+        ${myPrefix}
+      ],
+        "assignment-properties": ${complexObj}
+    }
+  }
+}
+