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  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.fail;
33  
34  /**
35   * Validates the LabCDA 2.0 schematron rule set.
36   * 
37   * @author Stefan Ivanov
38   * 
39   */
40  public class LabCDA20ValidationTest {
41  
42      private XsdValidator validator;
43      private SchematronValidator schematron;
44      private Map<String, Object> params;
45  
46      private static final String sample = "/IHE_LabReport_20070816.xml";
47      private static final String sample_errored = "/IHE_LabReport_20070816_Errored.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 testSchemaValidate() throws Exception {
59          Source testXml = new StreamSource(getClass().getResourceAsStream(sample));
60          validator.validate(testXml, CDAR2Constants.IHE_LAB_SCHEMA);
61      }
62  
63      @Test
64      public void testValidateErrors() throws Exception {
65          Source testXml = new StreamSource(getClass().getResourceAsStream(sample));
66          schematron.validate(testXml, new SchematronProfile(CDAR2Constants.IHE_LAB_20_SCHEMATRON_RULES, params));
67      }
68  
69      @Test
70      public void validateBodyPositive() throws Exception {
71          Source testXml = new StreamSource(getClass().getResourceAsStream(sample));
72          validator.validate(testXml, CDAR2Constants.IHE_LAB_SCHEMA);
73          testXml = new StreamSource(getClass().getResourceAsStream(sample));
74          schematron.validate(testXml, new SchematronProfile(
75                  "/schematron/ihe_lab_v20_20070816/templates/1.3.6.1.4.1.19376.1.3.1.sch", params));
76      }
77  
78      @Test
79      public void validateBodyErrors() throws Exception {
80          Source testXml = new StreamSource(getClass().getResourceAsStream(sample_errored));
81          try {
82              schematron.validate(testXml, new SchematronProfile(
83                      "/schematron/ihe_lab_v20_20070816/templates/1.3.6.1.4.1.19376.1.3.1.sch", params));
84              fail();
85          } catch (ValidationException ex) {
86              assertEquals(9, ex.getCauses().length);
87          }
88      }
89  
90      @Test
91      public void validateHeaderPositive() throws Exception {
92          Source testXml = new StreamSource(getClass().getResourceAsStream(sample));
93          validator.validate(testXml, CDAR2Constants.IHE_LAB_SCHEMA);
94          testXml = new StreamSource(getClass().getResourceAsStream(sample));
95          schematron.validate(testXml, new SchematronProfile(
96                  "/schematron/ihe_lab_v20_20070816/templates/1.3.6.1.4.1.19376.1.3.3.sch", params));
97      }
98  
99  }