always convert values toString for xml marshalling 82/85882/1
authorBenjamin, Max (mb388a) <mb388a@us.att.com>
Tue, 16 Apr 2019 21:21:23 +0000 (17:21 -0400)
committerSeshu Kumar M <seshu.kumar.m@huawei.com>
Sat, 20 Apr 2019 07:49:40 +0000 (07:49 +0000)
always convert values toString for xml marshalling
added in null check and object mapper call

Change-Id: Ie91c313dce110d53586e6773e2223d63be799a54
Issue-ID: SO-1786
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
adapters/mso-adapters-rest-interface/src/main/java/org/onap/so/adapters/vnfrest/VfResponseCommon.java
adapters/mso-adapters-rest-interface/src/main/java/org/onap/so/openstack/mappers/MapElements.java
adapters/mso-adapters-rest-interface/src/test/java/org/onap/so/openstack/mappers/JAXBMarshallingTest.java
adapters/mso-adapters-rest-interface/src/test/resources/VfRequest-marshalled-with-complex-object.xml [new file with mode: 0644]
adapters/mso-adapters-rest-interface/src/test/resources/createVfModuleRequest-with-params.xml

index 23bbbb3..7a2d4ec 100644 (file)
@@ -24,6 +24,8 @@ package org.onap.so.adapters.vnfrest;
 
 
 import java.io.ByteArrayOutputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.Marshaller;
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -71,7 +73,7 @@ public abstract class VfResponseCommon {
     public String toXmlString() {
         try {
             ByteArrayOutputStream bs = new ByteArrayOutputStream();
-            JAXBContext context = JAXBContext.newInstance(this.getClass());
+            JAXBContext context = JAXBContext.newInstance(this.getClass(), ArrayList.class, HashMap.class);
             Marshaller marshaller = context.createMarshaller();
             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // pretty print XML
             marshaller.marshal(this, bs);
index d20d2b7..0327fd6 100644 (file)
 
 package org.onap.so.openstack.mappers;
 
+import java.util.List;
+import java.util.Map;
 import javax.xml.bind.annotation.XmlElement;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
 
 public class MapElements {
+    private static final Logger logger = LoggerFactory.getLogger(MapElements.class);
     @XmlElement
     public String key;
     @XmlElement
@@ -32,6 +39,21 @@ public class MapElements {
 
     public MapElements(String key, Object value) {
         this.key = key;
-        this.value = value;
+        // this is required to handle marshalling raw json
+        // always write values as strings for XML
+        if (value != null) {
+            if (value instanceof List || value instanceof Map) {
+                try {
+                    this.value = new ObjectMapper().writeValueAsString(value);
+                } catch (JsonProcessingException e) {
+                    logger.warn("could not marshal value to json, calling toString");
+                    this.value = value.toString();
+                }
+            } else {
+                this.value = value;
+            }
+        } else {
+            this.value = value;
+        }
     }
 }
index 8d27e54..189d4cc 100644 (file)
@@ -24,17 +24,19 @@ import static org.junit.Assert.assertEquals;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
+import java.util.Arrays;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import org.junit.Test;
 import org.onap.so.adapters.vnfrest.CreateVfModuleRequest;
+import com.fasterxml.jackson.databind.ObjectMapper;
 
 
 public class JAXBMarshallingTest {
 
 
     @Test
-    public void xmlMarshalTest() throws IOException, JAXBException {
+    public void xmlUnMarshalTest() throws IOException, JAXBException {
         JAXBContext context = JAXBContext.newInstance(CreateVfModuleRequest.class);
 
         CreateVfModuleRequest request = (CreateVfModuleRequest) context.createUnmarshaller().unmarshal(
@@ -43,6 +45,20 @@ public class JAXBMarshallingTest {
         assertEquals("ubuntu-16-04-cloud-amd64", request.getVfModuleParams().get("vcpe_image_name"));
         assertEquals("10.2.0.0/24", request.getVfModuleParams().get("cpe_public_net_cidr"));
         assertEquals("", request.getVfModuleParams().get("workload_context"));
+        assertEquals("[\"a\",\"b\",\"c\"]", request.getVfModuleParams().get("raw-json-param"));
+    }
+
+    @Test
+    public void xmlMarshalTest() throws IOException, JAXBException {
+
+        CreateVfModuleRequest request = new CreateVfModuleRequest();
+        request.getVfModuleParams().put("test-null", null);
+        request.getVfModuleParams().put("test array", Arrays.asList("a", "b", "c"));
+
+        assertEquals("documents are equal",
+                new String(Files
+                        .readAllBytes(Paths.get("src/test/resources/VfRequest-marshalled-with-complex-object.xml"))),
+                request.toXmlString());
 
     }
 
diff --git a/adapters/mso-adapters-rest-interface/src/test/resources/VfRequest-marshalled-with-complex-object.xml b/adapters/mso-adapters-rest-interface/src/test/resources/VfRequest-marshalled-with-complex-object.xml
new file mode 100644 (file)
index 0000000..ce17512
--- /dev/null
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<createVfModuleRequest>
+    <failIfExists>false</failIfExists>
+    <backout>true</backout>
+    <vfModuleParams>
+        <entry>
+            <key>test array</key>
+            <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">["a","b","c"]</value>
+        </entry>
+        <entry>
+            <key>test-null</key>
+        </entry>
+    </vfModuleParams>
+    <msoRequest/>
+</createVfModuleRequest>
index 1ff24a5..2718f1d 100644 (file)
@@ -1,4 +1,4 @@
-<createVfModuleRequest>
+  <createVfModuleRequest>
        <cloudSiteId>RegionOne</cloudSiteId>
        <cloudOwner>CloudOwner</cloudOwner>
        <tenantId>09d8566ea45e43aa974cf447ed591d77</tenantId>
                        <key>vf_module_index</key>
                        <value>0</value>
                </entry>
-
+        <entry>
+            <key>raw-json-param</key>
+            <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">["a","b","c"]</value>
+        </entry>
        </vfModuleParams>
        <msoRequest>
                <requestId>11c8ec20-a1f8-4aa2-926f-e55d67a30f8b</requestId>