View Javadoc
1   /*
2    * Copyright 2011 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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          // marshaller.marshal(jaxbElement, System.out);
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  }