View Javadoc
1   /*
2    * Copyright 2015 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.metadata;
17  
18  import org.junit.Test;
19  import org.openehealth.ipf.commons.ihe.xds.core.SampleData;
20  import org.openehealth.ipf.commons.ihe.xds.core.requests.QueryRegistry;
21  import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FetchQuery;
22  import org.openehealth.ipf.commons.xml.XmlUtils;
23  import org.w3c.dom.Element;
24  
25  import javax.xml.bind.JAXBContext;
26  import javax.xml.parsers.DocumentBuilderFactory;
27  import java.io.ByteArrayInputStream;
28  
29  import static org.junit.Assert.*;
30  
31  /**
32   * @author Dmytro Rud
33   */
34  public class TimestampTest {
35  
36      private static void check(Timestamp ts, Timestamp.Precision precision, String s) {
37          ts.setPrecision(precision);
38          assertEquals(s, Timestamp.toHL7(ts));
39          assertEquals(precision, Timestamp.fromHL7(s).getPrecision());
40      }
41  
42      @Test
43      public void testRendering1() {
44          Timestamp ts = Timestamp.fromHL7("20150102030405.777+0200");
45          assertEquals(Timestamp.Precision.SECOND, ts.getPrecision());
46  
47          check(ts, Timestamp.Precision.SECOND, "20150102010405");
48          check(ts, Timestamp.Precision.MINUTE, "201501020104");
49          check(ts, Timestamp.Precision.HOUR, "2015010201");
50          check(ts, Timestamp.Precision.DAY, "20150102");
51          check(ts, Timestamp.Precision.MONTH, "201501");
52          check(ts, Timestamp.Precision.YEAR, "2015");
53      }
54  
55      @Test
56      public void testRendering2() {
57          Timestamp ts = Timestamp.fromHL7("2015");
58          assertEquals(Timestamp.Precision.YEAR, ts.getPrecision());
59  
60          check(ts, Timestamp.Precision.SECOND, "20150101000000");
61          check(ts, Timestamp.Precision.MINUTE, "201501010000");
62          check(ts, Timestamp.Precision.HOUR, "2015010100");
63          check(ts, Timestamp.Precision.DAY, "20150101");
64          check(ts, Timestamp.Precision.MONTH, "201501");
65          check(ts, Timestamp.Precision.YEAR, "2015");
66      }
67  
68      @Test
69      public void testNullValues() {
70          assertNull(Timestamp.fromHL7(null));
71          assertNull(Timestamp.fromHL7(""));
72          assertNull(Timestamp.toHL7(null));
73      }
74  
75      private static void expectFailure(String s) {
76          try {
77              Timestamp ts = Timestamp.fromHL7(s);
78              fail("This line must be not reachable");
79          } catch (Exception e) {
80              // ok
81          }
82      }
83  
84      @Test
85      public void testBadValue1() {
86          expectFailure("201501020304056789"); // toooo loooong
87  
88          expectFailure("2015010203040"); // incomplete value for second
89          expectFailure("20150102030");   // incomplete value for minute
90          expectFailure("201501020");     // incomplete value for hour
91          expectFailure("2015010");       // incomplete value for day
92          expectFailure("20150");         // incomplete value for month
93          expectFailure("015");           // incomplete value for year
94          expectFailure("15");            // incomplete value for yea
95          expectFailure("1");             // incomplete value for ye
96  
97          expectFailure(" ");             // philosophy can convert space into time, IPF cannot (yet)
98          expectFailure("nine o'clock");  // philology can handle natural languages, IPF cannot (yet)
99      }
100 
101     @Test
102     public void testEquivalence() {
103         Timestamp ts1 = Timestamp.fromHL7("20150102100405.777+0800");
104 
105         ts1.setPrecision(Timestamp.Precision.YEAR);
106         assertEquals(ts1, Timestamp.fromHL7("2015"));
107         assertNotEquals(ts1, Timestamp.fromHL7("2017"));
108 
109         ts1.setPrecision(Timestamp.Precision.MONTH);
110         assertEquals(ts1, Timestamp.fromHL7("201501"));
111         assertNotEquals(ts1, Timestamp.fromHL7("201507"));
112 
113         ts1.setPrecision(Timestamp.Precision.DAY);
114         assertEquals(ts1, Timestamp.fromHL7("20150102"));
115         assertNotEquals(ts1, Timestamp.fromHL7("20150107"));
116 
117         ts1.setPrecision(Timestamp.Precision.HOUR);
118         assertEquals(ts1, Timestamp.fromHL7("2015010204+0200"));
119         assertNotEquals(ts1, Timestamp.fromHL7("2015010207+0200"));
120 
121         ts1.setPrecision(Timestamp.Precision.MINUTE);
122         assertEquals(ts1, Timestamp.fromHL7("201501020404+0200"));
123         assertNotEquals(ts1, Timestamp.fromHL7("201501020407+0200"));
124 
125         ts1.setPrecision(Timestamp.Precision.SECOND);
126         assertEquals(ts1, Timestamp.fromHL7("20150102040405.123+0200"));
127         assertNotEquals(ts1, Timestamp.fromHL7("20150102040407.777+0200"));
128     }
129 
130     @Test
131     public void testNullValues2() {
132         Timestamp ts1 = Timestamp.fromHL7("20151201");
133         assertEquals(Timestamp.Precision.DAY, ts1.getPrecision());
134 
135         ts1.setPrecision(null);
136         assertEquals(Timestamp.Precision.SECOND, ts1.getPrecision());
137         assertEquals("20151201000000", Timestamp.toHL7(ts1));
138 
139         Timestamp ts2 = Timestamp.fromHL7("20151201000000");
140         assertEquals(ts1, ts2);
141 
142         assertNotEquals(ts1.hashCode(), ts2.hashCode());
143     }
144 
145     @Test
146     public void testXml() throws Exception {
147         QueryRegistry queryRegistry = SampleData.createFetchQuery();
148         FetchQuery query = (FetchQuery) queryRegistry.getQuery();
149         assertEquals(Timestamp.Precision.YEAR, query.getCreationTime().getFrom().getPrecision());
150         query.getCreationTime().getFrom().setPrecision(null);
151 
152         Element element = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument().createElement("dummy");
153         JAXBContext jaxbContext = JAXBContext.newInstance(QueryRegistry.class);
154         jaxbContext.createMarshaller().marshal(query, element);
155 
156         String s = new String(XmlUtils.serialize(element.getFirstChild()));
157         assertTrue(s.contains("<xds:from dateTime=\"1980-01-01T00:00:00Z\"/>"));
158         assertTrue(s.contains("<xds:to dateTime=\"1981-01-01T00:00:00Z\" precision=\"YEAR\"/>"));
159 
160         s = s.replace("precision=\"YEAR\"", "precision=\"some garbage\"");
161 
162         ByteArrayInputStream stream = new ByteArrayInputStream(s.getBytes());
163 
164         FetchQuery query1 = (FetchQuery) jaxbContext.createUnmarshaller().unmarshal(stream);
165         assertEquals(Timestamp.Precision.SECOND, query1.getCreationTime().getFrom().getPrecision());
166         assertEquals(Timestamp.Precision.SECOND, query1.getCreationTime().getTo().getPrecision());
167     }
168 
169 }