forked from amida-tech/cda2r4
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ValidatorTest.java
268 lines (239 loc) · 12.2 KB
/
ValidatorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package tr.com.srdc.cda2fhir;
/*
* #%L
* CDA to FHIR Transformer Library
* %%
* Copyright (C) 2016 SRDC Yazilim Arastirma ve Gelistirme ve Danismanlik Tic. A.S.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.Bundle.BundleType;
import org.hl7.fhir.r4.model.Identifier;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.openhealthtools.mdht.uml.cda.consol.ConsolPackage;
import org.openhealthtools.mdht.uml.cda.consol.ContinuityOfCareDocument;
import org.openhealthtools.mdht.uml.cda.util.CDAUtil;
import tr.com.srdc.cda2fhir.conf.Config;
import tr.com.srdc.cda2fhir.transform.CCDTransformerImpl;
import tr.com.srdc.cda2fhir.transform.ICDATransformer;
import tr.com.srdc.cda2fhir.util.FHIRUtil;
import tr.com.srdc.cda2fhir.util.IdGeneratorEnum;
import tr.com.srdc.cda2fhir.validation.IValidator;
import tr.com.srdc.cda2fhir.validation.ValidatorImpl;
public class ValidatorTest {
@BeforeClass
public static void init() {
// Load MDHT CDA packages. Otherwise ContinuityOfCareDocument and similar
// documents will not be recognised.
// This has to be called before loading the document; otherwise will have no
// effect.
CDAUtil.loadPackages();
}
// 170.315_b1_toc_gold_sample2_v1.xml without profile
@Test
public void testGoldSampleBundleWithoutProfile() throws Exception {
String cdaResourcePath = "src/test/resources/170.315_b1_toc_gold_sample2_v1.xml";
String targetPathForFHIRResource = "src/test/resources/output/170.315_b1_toc_gold_sample2_v1-wo-profile-validation.xml";
String targetPathForResultFile = "src/test/resources/output/validation-result-wo-profile-for-170.315_b1_toc_gold_sample2_v1.html";
boolean generateDAFProfileMetadata = false;
transformAndValidate(cdaResourcePath, targetPathForFHIRResource, targetPathForResultFile,
generateDAFProfileMetadata, true);
}
// 170.315_b1_toc_gold_sample2_v1.xml with profile
@Test
public void testGoldSampleBundleWithProfile() throws Exception {
String cdaResourcePath = "src/test/resources/170.315_b1_toc_gold_sample2_v1.xml";
String targetPathForFHIRResource = "src/test/resources/output/170.315_b1_toc_gold_sample2_v1-w-profile-validation.xml";
String targetPathForResultFile = "src/test/resources/output/validation-result-w-profile-for-170.315_b1_toc_gold_sample2_v1.html";
boolean generateDAFProfileMetadata = true;
transformAndValidate(cdaResourcePath, targetPathForFHIRResource, targetPathForResultFile,
generateDAFProfileMetadata, false);
}
// 170.315_b1_toc_inp_ccd_r21_sample1_v5.xml without profile
@Test
public void testInpSampleBundleWithoutProfile() throws Exception {
String cdaResourcePath = "src/test/resources/170.315_b1_toc_inp_ccd_r21_sample1_v5.xml";
String targetPathForFHIRResource = "src/test/resources/output/170.315_b1_toc_inp_ccd_r21_sample1_v5-wo-profile-validation.xml";
String targetPathForResultFile = "src/test/resources/output/validation-result-wo-profile-for-170.315_b1_toc_inp_ccd_r21_sample1_v5.html";
boolean generateDAFProfileMetadata = false;
transformAndValidate(cdaResourcePath, targetPathForFHIRResource, targetPathForResultFile,
generateDAFProfileMetadata, false);
}
// 170.315_b1_toc_inp_ccd_r21_sample1_v5.xml without profile
@Test
public void testInpSampleBundleWithProfile() throws Exception {
String cdaResourcePath = "src/test/resources/170.315_b1_toc_inp_ccd_r21_sample1_v5.xml";
String targetPathForFHIRResource = "src/test/resources/output/170.315_b1_toc_inp_ccd_r21_sample1_v5-w-profile-validation.xml";
String targetPathForResultFile = "src/test/resources/output/validation-result-w-profile-for-170.315_b1_toc_inp_ccd_r21_sample1_v5.html";
boolean generateDAFProfileMetadata = true;
transformAndValidate(cdaResourcePath, targetPathForFHIRResource, targetPathForResultFile,
generateDAFProfileMetadata, false);
}
// C-CDA_R2-1_CCD.xml without DAF profile
@Test
public void testReferenceCCDBundleWithoutProfile() throws Exception {
String cdaResourcePath = "src/test/resources/C-CDA_R2-1_CCD.xml";
String targetPathForFHIRResource = "src/test/resources/output/C-CDA_R2-1_CCD-wo-profile-validation.xml";
String targetPathForResultFile = "src/test/resources/output/validation-result-wo-profile-for-C-CDA_R2-1_CCD.html";
boolean generateDAFProfileMetadata = false;
transformAndValidate(cdaResourcePath, targetPathForFHIRResource, targetPathForResultFile,
generateDAFProfileMetadata, false);
}
// C-CDA_R2-1_CCD.xml with provenance
@Test
public void testReferenceCCDBundleWithProvenance() throws Exception {
String cdaResourcePath = "src/test/resources/C-CDA_R2-1_CCD.xml";
String targetPathForFHIRResource = "src/test/resources/output/C-CDA_R2-1_CCD-w-provenance.xml";
String targetPathForResultFile = "src/test/resources/output/validation-result-w-provenance-for-C-CDA_R2-1_CCD.html";
boolean generateDAFProfileMetadata = false;
transformAndValidate(cdaResourcePath, targetPathForFHIRResource, targetPathForResultFile,
generateDAFProfileMetadata, true);
}
// C-CDA_R2-1_CCD.xml with DAF profile
@Test
public void testReferenceCCDBundleWithProfile() throws Exception {
String cdaResourcePath = "src/test/resources/C-CDA_R2-1_CCD.xml";
String targetPathForFHIRResource = "src/test/resources/output/C-CDA_R2-1_CCD-w-profile-validation.xml";
String targetPathForResultFile = "src/test/resources/output/validation-result-w-profile-for-C-CDA_R2-1_CCD.html";
boolean generateDAFProfileMetadata = false;
transformAndValidate(cdaResourcePath, targetPathForFHIRResource, targetPathForResultFile,
generateDAFProfileMetadata, false);
}
// Vitera_CCDA_SMART_Sample.xml without profile
@Test
public void testViteraBundleWithoutProfile() throws Exception {
String cdaResourcePath = "src/test/resources/Vitera_CCDA_SMART_Sample.xml";
String targetPathForFHIRResource = "src/test/resources/output/Vitera_CCDA_SMART_Sample-wo-profile-validation.xml";
String targetPathForResultFile = "src/test/resources/output/validation-result-wo-profile-for-Vitera_CCDA_SMART_Sample.html";
boolean generateDAFProfileMetadata = false;
transformAndValidate(cdaResourcePath, targetPathForFHIRResource, targetPathForResultFile,
generateDAFProfileMetadata, false);
}
// Vitera_CCDA_SMART_Sample.xml with profile
@Test
public void testViteraBundleWithProfile() throws Exception {
String cdaResourcePath = "src/test/resources/Vitera_CCDA_SMART_Sample.xml";
String targetPathForFHIRResource = "src/test/resources/output/Vitera_CCDA_SMART_Sample-w-profile-validation.xml";
String targetPathForResultFile = "src/test/resources/output/validation-result-w-profile-for-Vitera_CCDA_SMART_Sample.html";
boolean generateDAFProfileMetadata = true;
transformAndValidate(cdaResourcePath, targetPathForFHIRResource, targetPathForResultFile,
generateDAFProfileMetadata, false);
}
// HannahBanana_EpicCCD.xml
@Ignore
public void testHannahBanana() throws Exception {
String cdaResourcePath = "src/test/resources/Epic/HannahBanana_EpicCCD-pretty.xml";
String targetPathForFHIRResource = "src/test/resources/output/Epic/HannahBanana_EpicCCD-pretty.fhir.xml";
String targetPathForResultFile = "src/test/resources/output/Epic/HannahBanana_EpicCCD-pretty.validation-result.html";
boolean generateDAFProfileMetadata = true;
transformAndValidate(cdaResourcePath, targetPathForFHIRResource, targetPathForResultFile,
generateDAFProfileMetadata, false);
}
// robust CCD.XML
@Ignore
public void testRobust() throws Exception {
String cdaResourcePath = "src/test/resources/Epic/robust CCD.XML";
String targetPathForFHIRResource = "src/test/resources/output/Epic/robust CCD.fhir.xml";
String targetPathForResultFile = "src/test/resources/output/Epic/robust CCD.validation-result.html";
boolean generateDAFProfileMetadata = true;
transformAndValidate(cdaResourcePath, targetPathForFHIRResource, targetPathForResultFile,
generateDAFProfileMetadata, false);
}
// Person-RAKIA_TEST_DOC0001 (1).xml
@Ignore
public void testRakia() throws Exception {
String cdaResourcePath = "src/test/resources/Cerner/Person-RAKIA_TEST_DOC00001 (1).xml";
String targetPathForFHIRResource = "src/test/resources/output/Cerner/Person-RAKIA_TEST_DOC00001 (1).fhir.xml";
String targetPathForResultFile = "src/test/resources/output/Cerner/Person-RAKIA_TEST_DOC00001 (1).validation-result.html";
boolean generateDAFProfileMetadata = true;
transformAndValidate(cdaResourcePath, targetPathForFHIRResource, targetPathForResultFile,
generateDAFProfileMetadata, false);
}
/**
* Transforms a CDA resource to a FHIR resource, validates the FHIR resource and
* prints the validation result to the target path.
*
* @param cdaResourcePath A file path of the CDA resource that is to
* be transformed
* @param targetPathForFHIRResource A file path where the FHIR resource is to
* be created
* @param targetPathForResultFile A file path where the validation result
* file is to be created
* @param generateDAFProfileMetadata A boolean indicating whether the generated
* resources will include DAF profile
* declarations in meta.profile
* @throws Exception
*/
private void transformAndValidate(String cdaResourcePath, String targetPathForFHIRResource,
String targetPathForResultFile, boolean generateDAFProfileMetadata, boolean generateProvenance)
throws Exception {
IValidator validator = new ValidatorImpl();
ByteArrayOutputStream os = null;
// file to be transformed
FileInputStream fis = new FileInputStream(cdaResourcePath);
ContinuityOfCareDocument cda = (ContinuityOfCareDocument) CDAUtil.loadAs(fis,
ConsolPackage.eINSTANCE.getContinuityOfCareDocument());
ICDATransformer ccdTransformer = new CCDTransformerImpl(IdGeneratorEnum.COUNTER);
// set whether DAF Profile URLs will be created in meta.profile of relevant
// resources
Config.setGenerateDafProfileMetadata(generateDAFProfileMetadata);
Config.setAddedZerosToDateTime(true);
Bundle bundle = null;
if (generateProvenance) {
// CDAUtil closes the stream, re-open it.
FileInputStream fisStream = new FileInputStream(cdaResourcePath);
byte[] buffer = new byte[10];
StringBuilder sb = new StringBuilder();
while (fisStream.read(buffer) != -1) {
sb.append(new String(buffer));
buffer = new byte[10];
}
fisStream.close();
String content = sb.toString();
Identifier assemblerDevice = new Identifier();
assemblerDevice.setValue("Higgs");
assemblerDevice.setSystem("http://www.amida.com");
bundle = ccdTransformer.transformDocument(cda, BundleType.TRANSACTION, null, content, assemblerDevice);
} else {
bundle = ccdTransformer.transformDocument(cda, null, null);
}
Config.setAddedZerosToDateTime(false);
Assert.assertNotNull(bundle);
// print the bundle for checking against validation results
// printed as XML, because HL7 FHIR Validator works with XML encoded resources
FHIRUtil.printXML(bundle, targetPathForFHIRResource);
os = (ByteArrayOutputStream) validator.validateBundle(bundle);
// ValidationResult fileResult =
// validator.validateFile(targetPathForFHIRResource);
// Assert.assertNotNull(fileResult);
// Assert.assertTrue(fileResult.isSuccessful()); TODO: Investigate and fix why
// this is failing
if (os != null) {
File validationFile = new File(targetPathForResultFile);
validationFile.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(validationFile);
os.writeTo(fos);
os.close();
fos.close();
}
}
}