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.Test;
20  import org.openehealth.ipf.commons.core.modules.api.ValidationException;
21  import org.openehealth.ipf.commons.xml.SchematronProfile;
22  import org.openehealth.ipf.commons.xml.SchematronValidator;
23  import org.openehealth.ipf.commons.xml.XsdValidator;
24  import org.openehealth.ipf.modules.cda.CDAR2Constants;
25  
26  import javax.xml.transform.Source;
27  import javax.xml.transform.stream.StreamSource;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  /**
32   * Validates the LabCDA schematron rule set.
33   * 
34   * @author Stefan Ivanov
35   * 
36   */
37  public class LabCDAValidationTest {
38      
39      private XsdValidator validator;
40      private SchematronValidator schematron;
41      private Map<String, Object> params;
42      
43      private String sample = "/IHE_LabReport_20080103.xml";
44      private String sample_extended = "/IHE_LabReport_21_Extended.xml";
45      private String sample2 = "/IHE_LabReport_20080103_Errored.xml";
46  
47      @Before
48      public void setUp() throws Exception {
49          validator = new XsdValidator();
50          schematron = new SchematronValidator();
51          params = new HashMap<>();
52          params.put("phase", "errors");
53      }
54      
55      @Test
56      public void testSchemaValidate() throws Exception {
57          Source testXml = new StreamSource(getClass().getResourceAsStream(sample));
58          validator.validate(testXml, CDAR2Constants.IHE_LAB_SCHEMA);
59      }
60      
61      @Test
62      public void testSchemaValidateExtended() throws Exception {
63          Source testXml = new StreamSource(getClass().getResourceAsStream(sample_extended));
64          validator.validate(testXml, CDAR2Constants.IHE_LAB_SCHEMA);
65      }
66      
67      @Test
68      public void testValidateErrors() throws Exception {
69          Source testXml = new StreamSource(getClass().getResourceAsStream(sample));
70          schematron.validate(testXml, new SchematronProfile(CDAR2Constants.IHE_LAB_SCHEMATRON_RULES,
71              params));
72      }
73      
74      @Test
75      public void testValidateErrorsExtended() throws Exception {
76          Source testXml = new StreamSource(getClass().getResourceAsStream(sample_extended));
77          schematron.validate(testXml, new SchematronProfile(CDAR2Constants.IHE_LAB_SCHEMATRON_RULES,
78              params));
79      }    
80      
81      @Test(expected = ValidationException.class)
82      public void testValidateOnlyErrors() throws Exception {
83          Source testXml = new StreamSource(getClass().getResourceAsStream(sample2));
84          schematron.validate(testXml, new SchematronProfile(CDAR2Constants.IHE_LAB_SCHEMATRON_RULES,
85              params));
86      }
87  
88      @Test(expected = ValidationException.class)
89      public void testValidateWarnings() throws Exception {
90          Source testXml = new StreamSource(getClass().getResourceAsStream(sample));
91          schematron
92              .validate(testXml, new SchematronProfile(CDAR2Constants.IHE_LAB_SCHEMATRON_RULES));
93      }
94  
95  
96  }