improve JaxbTestHelper utility class with better type safe generics API 08/70708/1
authorMichael Vorburger <vorburger@redhat.com>
Tue, 10 Apr 2018 11:58:35 +0000 (13:58 +0200)
committerMichael Vorburger <vorburger@redhat.com>
Tue, 10 Apr 2018 11:58:35 +0000 (13:58 +0200)
Change-Id: Ie65f7838588f08310d3cc8e12c435dc66ee1623d
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
neutron-spi/src/test/java/org/opendaylight/neutron/spi/JaxbTestHelper.java

index f24bc576e7902978d77895ea9989dcf751e39ca3..e05090c2c34544c03044f301afc45f713a4723cf 100644 (file)
@@ -9,7 +9,9 @@
 package org.opendaylight.neutron.spi;
 
 import java.io.StringReader;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
@@ -23,27 +25,38 @@ public final class JaxbTestHelper {
     private JaxbTestHelper() {
     }
 
-    @SuppressWarnings({ "rawtypes", "unchecked" })
+    @Deprecated
     public static Object jaxbUnmarshall(Object schemaObject, String json) throws JAXBException {
-        Class cls = schemaObject.getClass();
-        Class[] types = new Class[1];
-        types[0] = cls;
+        return jaxbUnmarshall(schemaObject.getClass(), json);
+    }
+
+    public static <T> T jaxbUnmarshall(Class<T> schemaClass, String json) throws JAXBException {
         Map<String, String> namespacePrefixMapper = new HashMap<>(3);
         namespacePrefixMapper.put("router", "router");
         namespacePrefixMapper.put("provider", "provider");
         namespacePrefixMapper.put("binding", "binding");
+
         Map<String, Object> jaxbProperties = new HashMap<>(2);
         jaxbProperties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
         jaxbProperties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
         jaxbProperties.put(JAXBContextProperties.JSON_NAMESPACE_SEPARATOR, ':');
         jaxbProperties.put(JAXBContextProperties.NAMESPACE_PREFIX_MAPPER, namespacePrefixMapper);
-        JAXBContext jc = JAXBContext.newInstance(types, jaxbProperties);
+
+        List<Class<T>> classesToBeBound = Collections.singletonList(schemaClass);
+        JAXBContext jc = JAXBContext.newInstance(classesToBeBound.toArray(new Class[0]), jaxbProperties);
 
         Unmarshaller unmarshaller = jc.createUnmarshaller();
         unmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespacePrefixMapper);
 
-        StringReader reader = new StringReader(json);
-        StreamSource stream = new StreamSource(reader);
-        return unmarshaller.unmarshal(stream, cls).getValue();
+        try (StringReader reader = new StringReader(json)) {
+            StreamSource stream = new StreamSource(reader);
+            T object = unmarshaller.unmarshal(stream, schemaClass).getValue();
+            if (object == null) {
+                throw new IllegalStateException(
+                        "unmarshal() returned null for arguments schemaClass=" + schemaClass + ", json: " + json);
+            } else {
+                return object;
+            }
+        }
     }
 }