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  
25  import javax.xml.transform.Source;
26  import javax.xml.transform.stream.StreamSource;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.fail;
32  import static org.openehealth.ipf.modules.cda.CDAR2Constants.HITSP_37_SCHEMATRON_RULES;
33  import static org.openehealth.ipf.modules.cda.CDAR2Constants.IHE_LAB_SCHEMA;
34  
35  /**
36   * Validates the HITSP C37 schematron rule set.
37   * 
38   * @author Stefan Ivanov
39   * 
40   */
41  public class HITSPC37ValidationTest {
42  
43  	private XsdValidator validator;
44  	private SchematronValidator schematron;
45  	private Map<String, Object> params;
46  
47  	private static final String sample_good = "/HITSP_C37_With_CBC_GTT_GS_Sensitivity.xml";
48  	private static final String sample_lab = "/IHE_LabReport_20080103.xml";
49  	private static final String sample_wrong = "/CDA_PHMR_WRONG.xml";
50  
51  	@Before
52  	public void setUp() throws Exception {
53  		validator = new XsdValidator();
54  		schematron = new SchematronValidator();
55  		params = new HashMap<>();
56  		params.put("phase", "errors");
57  	}
58  
59  	@Test
60  	public void validateSchemaGoodSample() throws Exception {
61  		Source testXml = new StreamSource(getClass().getResourceAsStream(sample_good));
62  		validator.validate(testXml, IHE_LAB_SCHEMA);
63  	}
64  
65  	@Test
66  	public void validateComplete() throws Exception {
67          Source testXml = new StreamSource(getClass().getResourceAsStream(sample_good));
68  		schematron.validate(testXml, new SchematronProfile(
69  				HITSP_37_SCHEMATRON_RULES, params));
70  	}
71  
72  	@Test
73  	public void validateCompleteWrong() throws Exception {
74  		try {
75              Source testXml = new StreamSource(getClass().getResourceAsStream(sample_wrong));
76  			schematron.validate(testXml, new SchematronProfile(
77  					HITSP_37_SCHEMATRON_RULES, params));
78  			fail();
79  		} catch (ValidationException ex) {
80  			assertEquals(148, ex.getCauses().length);
81  		}
82  	}
83  
84  	@Test
85  	public void validateLab() throws Exception {
86          Source testXml = new StreamSource(getClass().getResourceAsStream(sample_lab));
87  		schematron.validate(testXml, new SchematronProfile(
88  				HITSP_37_SCHEMATRON_RULES, params));
89  	}
90  
91  }