View Javadoc
1   /*
2    * Copyright 2014 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.transform;
17  
18  import org.junit.Test;
19  import org.openehealth.ipf.commons.ihe.xds.core.metadata.DocumentEntry;
20  
21  import javax.xml.bind.JAXBContext;
22  import javax.xml.bind.Marshaller;
23  import java.io.StringReader;
24  import java.io.StringWriter;
25  import java.util.Arrays;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import static org.junit.Assert.assertEquals;
31  
32  /**
33   * Test for marshalling and unmarshalling of extra metadata in the simplified XDS data model.
34   * @author Anurag Shrivastava
35   */
36  public class ExtraMetadataMarshalingTest {
37  
38      private static final String KEY_1 = "urn:emc:em1";
39      private static final List<String> VALUES_1 = Arrays.asList("One", "Two", "Three");
40  
41      private static final String KEY_2 = "urn:xyz";
42      private static final List<String> VALUES_2 = Arrays.asList("12345", "67890");
43  
44  
45      @Test
46      public void testDocumentEntryUnMarshalling() throws Exception {
47          Map<String, List<String>> extraMetaData = new HashMap<>();
48          extraMetaData.put(KEY_1, VALUES_1);
49          extraMetaData.put(KEY_2, VALUES_2);
50  
51          DocumentEntry original = new DocumentEntry();
52          original.setExtraMetadata(extraMetaData);
53  
54          JAXBContext context = JAXBContext.newInstance(DocumentEntry.class);
55          StringWriter writer = new StringWriter();
56          Marshaller marshaller = context.createMarshaller();
57          marshaller.setProperty("jaxb.formatted.output", true);
58          marshaller.marshal(original, writer);
59          String s = writer.toString();
60  
61          DocumentEntry result = (DocumentEntry) context.createUnmarshaller().unmarshal(new StringReader(s));
62  
63          assertEquals(2, result.getExtraMetadata().size());
64          assertEquals(extraMetaData, result.getExtraMetadata());
65          assertEquals(original, result);
66          assertEquals(original.toString(), result.toString());
67      }
68  
69  }