fix for bug 2928
[neutron.git] / neutron-spi / src / test / java / org / opendaylight / neutron / spi / JaxbTestHelper.java
1 /*
2  * Copyright (C) 2015 IBM, Inc.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.neutron.spi;
10
11 import java.io.StringReader;
12
13 import java.util.HashMap;
14 import java.util.Map;
15
16 import javax.xml.bind.JAXBContext;
17 import javax.xml.bind.JAXBException;
18 import javax.xml.bind.Unmarshaller;
19
20 import javax.xml.transform.stream.StreamSource;
21
22 import org.eclipse.persistence.jaxb.JAXBContextProperties;
23 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
24
25 public class JaxbTestHelper {
26
27     @SuppressWarnings({ "rawtypes", "unchecked" })
28     public static Object jaxbUnmarshall(Object schemaObject, String json) throws JAXBException {
29         Class c = schemaObject.getClass();
30         Class[] types = new Class[1];
31         types[0] = c;
32         Map<String, String> namespacePrefixMapper = new HashMap<String, String>(3);
33         namespacePrefixMapper.put("router", "router");
34         namespacePrefixMapper.put("provider", "provider");
35         namespacePrefixMapper.put("binding", "binding");
36         Map<String, Object> jaxbProperties = new HashMap<String, Object>(2);
37         jaxbProperties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
38         jaxbProperties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
39         jaxbProperties.put(JAXBContextProperties.JSON_NAMESPACE_SEPARATOR, ':');
40         jaxbProperties.put(JAXBContextProperties.NAMESPACE_PREFIX_MAPPER, namespacePrefixMapper);
41         JAXBContext jc = JAXBContext.newInstance(types, jaxbProperties);
42
43         Unmarshaller unmarshaller = jc.createUnmarshaller();
44         unmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespacePrefixMapper);
45         
46         StringReader reader = new StringReader(json);
47         StreamSource stream = new StreamSource(reader);
48         return unmarshaller.unmarshal(stream, c).getValue();
49     }
50 }