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.platform.camel.ihe.continua.hrn;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertNull;
21  import static org.junit.Assert.assertTrue;
22  
23  import javax.activation.DataHandler;
24  
25  import org.junit.Before;
26  import org.junit.Test;
27  import org.openehealth.ipf.commons.spring.core.config.SpringTypeConverter;
28  import org.openehealth.ipf.commons.ihe.xds.core.SampleData;
29  import org.openehealth.ipf.commons.ihe.xds.core.metadata.AssigningAuthority;
30  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Document;
31  import org.openehealth.ipf.commons.ihe.xds.core.metadata.DocumentEntry;
32  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Identifiable;
33  import org.openehealth.ipf.platform.camel.ihe.continua.hrn.converters.ByteArrayToClinicalDocumentConverter;
34  import org.openehealth.ipf.platform.camel.ihe.continua.hrn.converters.ByteArrayToDomConverter;
35  import org.openehealth.ipf.platform.camel.ihe.continua.hrn.converters.ByteArrayToStringConverter;
36  import org.openehealth.ipf.platform.camel.ihe.continua.hrn.converters.DataHandlerToByteArrayConverter;
37  import org.springframework.core.convert.support.GenericConversionService;
38  
39  /**
40   * Tests conversion services
41   * 
42   * @author Stefan Ivanov
43   */
44  public class DocumentTest {
45      private DocumentEntry docEntry;
46      private DataHandler someData;
47      
48      @Before
49      public void setUp() throws Exception {
50          Identifiable somePatientID = new Identifiable("id1", new AssigningAuthority("1.3"));
51          someData = SampleData.createDataHandler();
52          docEntry = SampleData.createDocumentEntry(somePatientID);
53  
54          GenericConversionService conversionService = new GenericConversionService();
55          conversionService.addConverter(new DataHandlerToByteArrayConverter());
56          conversionService.addConverter(new ByteArrayToStringConverter());
57          conversionService.addConverter(new ByteArrayToClinicalDocumentConverter());
58          conversionService.addConverter(new ByteArrayToDomConverter());
59          Document.setConversionService(new SpringTypeConverter(conversionService));
60      }
61  
62      @Test
63      public final void constructor() {
64          Document doc = new Document(docEntry, someData);
65          assertTrue(someData.equals(doc.getContent(DataHandler.class)));
66      }
67      
68      @Test
69      public final void getContentsClassOfDataHandler() {
70          Document doc = new Document(docEntry, someData);
71          assertTrue(someData.equals(doc.getContent(DataHandler.class)));
72      }
73      
74      @Test
75      public final void getNullContents() {
76          Document doc = new Document(docEntry, null);
77          assertNull(doc.getContent(DataHandler.class));
78          assertNull(doc.getContent(String.class));
79      }
80      
81      @Test
82      public final void addContents() {
83          Document doc = new Document(docEntry, null);
84          assertNull(doc.setContent(DataHandler.class, someData));
85          assertTrue(someData.equals(doc.getContent(DataHandler.class)));
86      }
87      
88      @Test
89      public final void removeContents() {
90          Document doc = new Document(docEntry, null);
91          doc.setContent(DataHandler.class, someData);
92          doc.getContent(byte[].class);
93          doc.getContent(String.class);
94          doc.removeContent(String.class);
95          assertNotNull(doc.getContent(String.class));
96      }
97  
98      @Test
99      public final void getContentsClassOfT() {
100         String testContent = "data";
101         Document doc = new Document(docEntry, someData);
102         doc.setContent(String.class, testContent);
103         String modContent = testContent.replace('a', 'c');
104         doc.setContent(String.class, modContent);
105         assertEquals("String content shoud be \'" + modContent + "\'", modContent,
106             doc.getContent(String.class));
107     }
108     
109     @Test
110     public final void getContentsSize() {
111         Document doc = new Document(docEntry, someData);
112         doc.setContent(String.class, "data1");
113         doc.setContent(Integer.class, 2);
114         doc.setContent(Integer.class, 4);
115         assertEquals("Size of the contents should be 3!", 3, doc.getContentsCount());
116     }
117 
118     @Test
119     public final void getContentsKeySet() {
120         Document doc = new Document(docEntry, someData);
121         doc.setContent(String.class, "data1");
122         doc.setContent(Integer.class, 2);
123         doc.setContent(Integer.class, 4);
124         Class<?>[] classArray = new Class<?>[] {String.class, Integer.class, DataHandler.class};
125         for (Class<?> clazz : classArray) {
126             assertTrue(doc.hasContent(clazz));
127         }
128         assertEquals(classArray.length, doc.getContentsCount());
129     }
130     
131     @Test
132     public final void getContentsMissing() {
133         Document doc = new Document(docEntry, someData);
134         doc.getContent(byte[].class);
135         doc.getContent(String.class);
136         assertTrue("DataHandler should be converted to String",
137                 doc.hasContent(String.class));
138     }
139 
140     @Test
141     public final void equalsPositive() {
142         Document doc1 = new Document(docEntry, someData);
143         Document doc2 = new Document(docEntry, null);
144         doc2.setContent(DataHandler.class, someData);
145         assertTrue(doc1.equals(doc2));
146     }
147 }