forked from highmed/highmed-dsf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hapi fhir upgrade to 4.1.0, changes to validation related tests
- Loading branch information
Showing
4 changed files
with
65 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 63 additions & 25 deletions
88
dsf-fhir/dsf-fhir-server/src/test/java/org/highmed/dsf/fhir/hapi/ValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,116 @@ | ||
package org.highmed.dsf.fhir.hapi; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.highmed.dsf.fhir.service.DefaultProfileValidationSupportWithCustomResources; | ||
import org.highmed.dsf.fhir.service.ResourceValidator; | ||
import org.highmed.dsf.fhir.service.ResourceValidatorImpl; | ||
import org.highmed.dsf.fhir.service.SnapshotGenerator.SnapshotWithValidationMessages; | ||
import org.highmed.dsf.fhir.service.SnapshotGeneratorImpl; | ||
import org.highmed.dsf.fhir.service.StructureDefinitionReader; | ||
import org.hl7.fhir.r4.hapi.validation.FhirInstanceValidator; | ||
import org.hl7.fhir.r4.model.Patient; | ||
import org.hl7.fhir.r4.model.StructureDefinition; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import ca.uhn.fhir.context.FhirContext; | ||
import ca.uhn.fhir.validation.FhirValidator; | ||
import ca.uhn.fhir.validation.ResultSeverityEnum; | ||
import ca.uhn.fhir.validation.ValidationResult; | ||
|
||
public class ValidationTest | ||
{ | ||
private static final Logger logger = LoggerFactory.getLogger(ValidationTest.class); | ||
|
||
private static final FhirContext context = FhirContext.forR4(); | ||
private static List<StructureDefinition> snapshots; | ||
|
||
@BeforeClass | ||
public static void beforeClass() throws Exception | ||
{ | ||
snapshots = createSnapshots(readStructureDefinitions(context), context); | ||
} | ||
|
||
private static List<StructureDefinition> createSnapshots(List<StructureDefinition> diffs, FhirContext fhirContext) | ||
{ | ||
List<StructureDefinition> snapshots = new ArrayList<StructureDefinition>(diffs.size()); | ||
for (StructureDefinition diff : diffs) | ||
{ | ||
SnapshotGeneratorImpl generator = new SnapshotGeneratorImpl(fhirContext, | ||
new DefaultProfileValidationSupportWithCustomResources(snapshots, Collections.emptyList(), | ||
Collections.emptyList())); | ||
SnapshotWithValidationMessages result = generator.generateSnapshot(diff); | ||
assertTrue(result.getMessages().isEmpty()); | ||
|
||
snapshots.add(result.getSnapshot()); | ||
} | ||
|
||
return snapshots; | ||
} | ||
|
||
private static List<StructureDefinition> readStructureDefinitions(FhirContext context) | ||
{ | ||
StructureDefinitionReader reader = new StructureDefinitionReader(context); | ||
|
||
return reader.readXml(Paths.get("src/test/resources/profiles/DeBasis/AddressDeBasis.xml"), | ||
Paths.get("src/test/resources/profiles/DeBasis/patient-de-basis-0.2.1.xml")); | ||
} | ||
|
||
private Patient createNonValidPatient() | ||
{ | ||
Patient patient = new Patient(); | ||
patient.getMeta().addProfile("http://fhir.de/StructureDefinition/patient-de-basis/0.2.1"); | ||
patient.getAddressFirstRep().setDistrict("district"); | ||
return patient; | ||
} | ||
|
||
@Test | ||
public void testValidation() throws Exception | ||
public void testHapiValidation() throws Exception | ||
{ | ||
FhirContext context = FhirContext.forR4(); | ||
FhirValidator validator = context.newValidator(); | ||
|
||
FhirInstanceValidator instanceValidator = new FhirInstanceValidator(); | ||
validator.registerValidatorModule(instanceValidator); | ||
|
||
instanceValidator.setValidationSupport(new DefaultProfileValidationSupportWithCustomResources( | ||
readStructureDefinitions(context), Collections.emptyList(), Collections.emptyList())); | ||
instanceValidator.setValidationSupport(new DefaultProfileValidationSupportWithCustomResources(snapshots, | ||
Collections.emptyList(), Collections.emptyList())); | ||
|
||
Patient patient = createNonValidPatient(); | ||
|
||
ValidationResult result = validator.validateWithResult(patient); | ||
|
||
assertFalse(result.isSuccessful()); | ||
assertEquals(1, result.getMessages().size()); | ||
assertEquals(ResultSeverityEnum.ERROR, result.getMessages().get(0).getSeverity()); | ||
|
||
result.getMessages().forEach(r -> logger.info("Validation Issue: {} - {} - {}", r.getSeverity(), | ||
r.getLocationString(), r.getMessage())); | ||
} | ||
|
||
private List<StructureDefinition> readStructureDefinitions(FhirContext context) | ||
{ | ||
StructureDefinitionReader reader = new StructureDefinitionReader(context); | ||
|
||
return reader.readXml(Paths.get("src/test/resources/profiles/patient-de-basis-0.2.xml"), | ||
Paths.get("src/test/resources/profiles/address-de-basis-0.2.xml")); | ||
} | ||
|
||
private Patient createNonValidPatient() | ||
{ | ||
Patient patient = new Patient(); | ||
patient.getMeta().addProfile("http://fhir.de/StructureDefinition/patient-de-basis/0.2"); | ||
patient.getAddressFirstRep().setDistrict("district"); | ||
return patient; | ||
} | ||
|
||
@Test | ||
public void testNonValidPatient() | ||
public void testValidatorImpl() | ||
{ | ||
FhirContext context = FhirContext.forR4(); | ||
List<StructureDefinition> readStructureDefinitions = readStructureDefinitions(context); | ||
ResourceValidator validator = new ResourceValidatorImpl(context, | ||
new DefaultProfileValidationSupportWithCustomResources(readStructureDefinitions, | ||
Collections.emptyList(), Collections.emptyList())); | ||
new DefaultProfileValidationSupportWithCustomResources(snapshots, Collections.emptyList(), | ||
Collections.emptyList())); | ||
|
||
ValidationResult result = validator.validate(createNonValidPatient()); | ||
|
||
assertFalse(result.isSuccessful()); | ||
assertEquals(1, result.getMessages().size()); | ||
assertEquals(ResultSeverityEnum.ERROR, result.getMessages().get(0).getSeverity()); | ||
|
||
result.getMessages().forEach(r -> logger.info("Validation Issue: {} - {} - {}", r.getSeverity(), | ||
r.getLocationString(), r.getMessage())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters