1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.commons.ihe.xds.core.requests.query;
17
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.openehealth.ipf.commons.ihe.xds.core.metadata.Code;
21 import org.w3c.dom.Node;
22
23 import javax.xml.bind.JAXBContext;
24 import javax.xml.bind.JAXBElement;
25 import javax.xml.bind.Marshaller;
26 import javax.xml.bind.Unmarshaller;
27 import javax.xml.namespace.QName;
28 import javax.xml.transform.dom.DOMResult;
29 import java.util.Arrays;
30 import java.util.List;
31
32 public class QueryListTest {
33 @Test
34 public void testXmlSerializationWithString() throws Exception {
35 List<String> innerList1 = Arrays.asList("Lisa", "Jodi", "Terry", "Cassandra");
36 List<String> innerList2 = Arrays.asList("Brock", "Thor", "Venus");
37
38 QueryList<String> queryList = new QueryList<>();
39 queryList.getOuterList().add(innerList1);
40 queryList.getOuterList().add(innerList2);
41
42 JAXBContext jaxbContext = JAXBContext.newInstance(QueryList.class);
43 checkSerialization(jaxbContext, "queryList", queryList);
44 }
45
46 @Test
47 public void testXmlSerializationWithCode() throws Exception {
48 List<Code> innerList1 = Arrays.asList(
49 new Code("Lisa", null, "Girlfriend scheme"),
50 new Code("Jodi", null, "Girlfriend scheme"),
51 new Code("Terry", null, "Girlfriend scheme"),
52 new Code("Cassandra", null, "Girlfriend scheme"));
53 List<Code> innerList2 = Arrays.asList(
54 new Code("Brock", null, "Children scheme"),
55 new Code("Thor", null, "Children scheme"),
56 new Code("Venus", null, "Children scheme"));
57
58 QueryList<Code> queryList = new QueryList<>();
59 queryList.getOuterList().add(innerList1);
60 queryList.getOuterList().add(innerList2);
61
62 JAXBContext jaxbContext = JAXBContext.newInstance(QueryList.class, Code.class);
63 checkSerialization(jaxbContext, "queryList", queryList);
64 }
65
66 @SuppressWarnings({"unchecked"})
67 private <T> void checkSerialization(JAXBContext jaxbContext, String name, T object) throws Exception {
68 QName qname = new QName("http://www.openehealth.org/ipf/xds", name);
69 JAXBElement<T> jaxbElement = new JAXBElement<>(qname, (Class<T>) object.getClass(), object);
70 Marshaller marshaller = jaxbContext.createMarshaller();
71 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
72
73
74 DOMResult domResult = new DOMResult();
75 marshaller.marshal(jaxbElement, domResult);
76 Node marshalledNode = ((org.w3c.dom.Document) domResult.getNode()).getDocumentElement();
77
78 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
79 jaxbElement = (JAXBElement<T>) unmarshaller.unmarshal(marshalledNode, QueryList.class);
80 T objectToo = jaxbElement.getValue();
81
82 Assert.assertEquals(object, objectToo);
83 }
84 }