/*
 * Copyright (C) 2015 IBM, Inc.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.neutron.spi;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;

public final class JaxbTestHelper {

    private JaxbTestHelper() {
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static Object jaxbUnmarshall(Object schemaObject, String json) throws JAXBException {
        Class cls = schemaObject.getClass();
        Class[] types = new Class[1];
        types[0] = cls;
        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);

        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();
    }
}

@@ -343,23 +343,76 @@
 va.util.
-HashMap
+Collections;%0Aimport java.util.HashMap;%0Aimport java.util.List
 ;%0Aimport
@@ -787,53 +787,18 @@
    @
-SuppressWarnings(%7B %22rawtypes%22, %22unchecked%22 %7D)
+Deprecated
 %0A   
@@ -903,20 +903,30 @@
     
-Class cls = 
+return jaxbUnmarshall(
 sche
@@ -948,71 +948,120 @@
 ss()
+, json)
 ;%0A    
+%7D%0A%0A
     
-Class%5B%5D types = new Class%5B1%5D;%0A        types%5B0%5D = cls;
+public static %3CT%3E T jaxbUnmarshall(Class%3CT%3E schemaClass, String json) throws JAXBException %7B
 %0A   
@@ -1294,24 +1294,25 @@
 %22binding%22);%0A
+%0A
         Map%3C
@@ -1694,24 +1694,107 @@
 efixMapper);
+%0A%0A        List%3CClass%3CT%3E%3E classesToBeBound = Collections.singletonList(schemaClass);
 %0A        JAX
@@ -1835,13 +1835,46 @@
 nce(
-types
+classesToBeBound.toArray(new Class%5B0%5D)
 , ja
@@ -2069,16 +2069,21 @@
         
+try (
 StringRe
@@ -2114,26 +2114,32 @@
 Reader(json)
-;%0A
+) %7B%0A    
         Stre
@@ -2190,22 +2190,30 @@
         
-return
+    T object =
  unmarsh
@@ -2236,18 +2236,26 @@
 stream, 
-cl
+schemaClas
 s).getVa
@@ -2257,20 +2257,294 @@
 getValue();%0A
+            if (object == null) %7B%0A                throw new IllegalStateException(%0A                        %22unmarshal() returned null for arguments schemaClass=%22 + schemaClass + %22, json: %22 + json);%0A            %7D else %7B%0A                return object;%0A            %7D%0A        %7D%0A
     %7D%0A%7D%0A

improve JaxbTestHelper utility class with better type safe generics API