View Javadoc
1   /*
2    * Copyright 2012 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.modules.cda.support;
17  
18  import org.junit.Before;
19  import org.junit.Ignore;
20  import org.junit.Test;
21  import org.openehealth.ipf.commons.core.modules.api.ValidationException;
22  import org.openehealth.ipf.commons.xml.SchematronProfile;
23  import org.openehealth.ipf.commons.xml.SchematronValidator;
24  import org.openehealth.ipf.commons.xml.XsdValidator;
25  import org.openehealth.ipf.modules.cda.CDAR2Constants;
26  
27  import javax.xml.transform.Source;
28  import javax.xml.transform.stream.StreamSource;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.fail;
34  
35  /**
36   * Validates the PHMR schematron rule set.
37   *
38   * @author Stefan Ivanov
39   */
40  public class PHMRValidationTest {
41  
42      private XsdValidator validator;
43      private SchematronValidator schematron;
44      private Map<String, Object> params;
45  
46      private static final String sample_good = "/CDA_PHMR_GOOD.xml";
47      private static final String sample_wrong = "/CDA_PHMR_WRONG.xml";
48  
49      @Before
50      public void setUp() throws Exception {
51          validator = new XsdValidator();
52          schematron = new SchematronValidator();
53          params = new HashMap<>();
54          params.put("phase", "errors");
55      }
56  
57      @Test
58      public void validateSchemaGoodSample() throws Exception {
59          Source testXml = new StreamSource(getClass().getResourceAsStream(sample_good));
60          validator.validate(testXml, CDAR2Constants.CDAR2_SCHEMA);
61      }
62  
63      @Test
64      public void validateSchemaWrongSample() throws Exception {
65          Source testXml = new StreamSource(getClass().getResourceAsStream(sample_wrong));
66          validator.validate(testXml, CDAR2Constants.CDAR2_SCHEMA);
67      }
68  
69      /* validate header template */
70      @Test
71      public void validateHeaderPositive() throws Exception {
72          Source testXml = new StreamSource(getClass().getResourceAsStream(sample_good));
73          schematron.validate(testXml, new SchematronProfile(
74                  "/schematron/cda_phmr/templates/2.16.840.1.113883.10.20.9.sch", params));
75      }
76  
77      @Test
78      public void validateHeaderErrors() throws Exception {
79          Source testXml = new StreamSource(getClass().getResourceAsStream(sample_wrong));
80          try {
81              schematron.validate(testXml, new SchematronProfile(
82                      "/schematron/cda_phmr/templates/2.16.840.1.113883.10.20.9.sch", params));
83              fail();
84          } catch (ValidationException ex) {
85              assertEquals(11, ex.getCauses().length);
86          }
87      }
88  
89      @Ignore
90      public void validateHeaderWarnings() {
91          Source testXml = new StreamSource(getClass().getResourceAsStream(sample_wrong));
92          params.put("phase", "warnings");
93          try {
94              schematron.validate(testXml, new SchematronProfile(
95                      "/schematron/cda_phmr/templates/2.16.840.1.113883.10.20.9.sch", params));
96              fail();
97          } catch (ValidationException ex) {
98              assertEquals(4, ex.getCauses().length);
99          }
100     }
101 
102     @Test
103     public void validateHeaderAll() throws Exception {
104         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_wrong));
105         try {
106             schematron.validate(testXml, new SchematronProfile(
107                     "/schematron/cda_phmr/templates/2.16.840.1.113883.10.20.9.sch"));
108             fail();
109         } catch (ValidationException ex) {
110             assertEquals(17, ex.getCauses().length);
111         }
112     }
113 
114     @Test
115     public void validateBodyPositive() throws Exception {
116         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_good));
117         schematron.validate(testXml, new SchematronProfile(
118                 "/schematron/cda_phmr/templates/2.16.840.1.113883.10.20.9.sch", params));
119     }
120 
121     @Test
122     public void validateBodyErrors() throws Exception {
123         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_wrong));
124         try {
125             schematron.validate(testXml, new SchematronProfile(
126                     "/schematron/cda_phmr/templates/2.16.840.1.113883.10.20.9.sch", params));
127             fail();
128         } catch (ValidationException ex) {
129             assertEquals(11, ex.getCauses().length);
130         }
131     }
132 
133     @Test
134     public void validateProductInstancePositive() throws Exception {
135         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_good));
136         schematron.validate(testXml, new SchematronProfile(
137                 "/schematron/cda_phmr/templates/2.16.840.1.113883.10.20.9.9.sch", params));
138     }
139 
140     @Test
141     public void validateDeviceDefinitionOrganizerPositive() throws Exception {
142         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_good));
143         schematron.validate(testXml, new SchematronProfile(
144                 "/schematron/cda_phmr/templates/2.16.840.1.113883.10.20.9.4.sch", params));
145     }
146 
147     @Test
148     public void validateProductInstanceErrors() throws Exception {
149         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_wrong));
150         try {
151             schematron.validate(testXml, new SchematronProfile(
152                     "/schematron/cda_phmr/templates/2.16.840.1.113883.10.20.9.9.sch", params));
153             fail();
154         } catch (ValidationException ex) {
155             assertEquals(2, ex.getCauses().length);
156         }
157     }
158 
159     @Test
160     public void validateNumericObservationPositive() throws Exception {
161         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_good));
162         schematron.validate(testXml, new SchematronProfile(
163                 "/schematron/cda_phmr/templates/2.16.840.1.113883.10.20.9.8.sch", params));
164     }
165 
166     @Test
167     public void validateNumericObservationErrors() throws Exception {
168         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_wrong));
169         try {
170             schematron.validate(testXml, new SchematronProfile(
171                     "/schematron/cda_phmr/templates/2.16.840.1.113883.10.20.9.8.sch", params));
172             fail();
173         } catch (ValidationException ex) {
174             assertEquals(1, ex.getCauses().length);
175         }
176     }
177 
178     @Test
179     public void validateMedicalEquipmentErrors() throws Exception {
180         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_wrong));
181         schematron.validate(testXml, new SchematronProfile(
182                 "/schematron/cda_phmr/templates/2.16.840.1.113883.10.20.9.1.sch"));
183     }
184 
185     @Test
186     public void validateVitalSignsErrors() throws Exception {
187         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_wrong));
188         try {
189             schematron.validate(testXml, new SchematronProfile(
190                     "/schematron/cda_phmr/templates/2.16.840.1.113883.10.20.9.2.sch"));
191             fail();
192         } catch (ValidationException ex) {
193             assertEquals(2, ex.getCauses().length);
194         }
195     }
196 
197     @Test
198     public void validateResultsWarnings() throws Exception {
199         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_wrong));
200         try {
201             schematron.validate(testXml, new SchematronProfile(
202                     "/schematron/cda_phmr/templates/2.16.840.1.113883.10.20.9.14.sch"));
203             fail();
204         } catch (ValidationException ex) {
205             assertEquals(4, ex.getCauses().length);
206         }
207     }
208 
209     @Test
210     public void validateCompleteGood() throws Exception {
211         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_good));
212         schematron.validate(testXml, new SchematronProfile(
213                 CDAR2Constants.CDA_PHMR_SCHEMATRON_RULES, params));
214     }
215 
216     @Test
217     public void validateCompleteGoodWithWarnings() throws Exception {
218         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_good));
219         try {
220             schematron.validate(testXml, new SchematronProfile(
221                     CDAR2Constants.CDA_PHMR_SCHEMATRON_RULES));
222         } catch (ValidationException ex) {
223             assertEquals(11, ex.getCauses().length);
224         }
225     }
226 
227     @Test
228     public void validateCompleteWrong() throws Exception {
229         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_wrong));
230         try {
231             schematron.validate(testXml, new SchematronProfile(
232                     CDAR2Constants.CDA_PHMR_SCHEMATRON_RULES, params));
233             fail();
234         } catch (ValidationException ex) {
235             assertEquals(17, ex.getCauses().length);
236         }
237     }
238 
239     @Test
240     public void validateAllCompleteWrong() throws Exception {
241         Source testXml = new StreamSource(getClass().getResourceAsStream(sample_wrong));
242         try {
243             schematron.validate(testXml, new SchematronProfile(
244                     CDAR2Constants.CDA_PHMR_SCHEMATRON_RULES));
245             fail();
246         } catch (ValidationException ex) {
247             assertEquals(27, ex.getCauses().length);
248         }
249     }
250 
251 }