Skip to content

Commit

Permalink
hapi fhir upgrade to 4.1.0, changes to validation related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hhund committed Nov 22, 2019
1 parent 634d64f commit b70c207
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void testTree() throws Exception
FhirContext context = FhirContext.forR4();

Map<String, StructureDefinition> structureDefinitionsByUrl = Files
.list(Paths.get("src/test/resources/profiles"))
.list(Paths.get("src/test/resources/profiles")).filter(Files::isRegularFile)
.map(p -> readStructureDefinition(context, p).setSnapshot(null))
.collect(Collectors.toMap(StructureDefinition::getUrl, Function.identity()));

Expand Down
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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ public void testOrganizationProfileNotValid3() throws Exception

assertEquals(2,
result.getMessages().stream().filter(m -> ResultSeverityEnum.ERROR.equals(m.getSeverity())).count());
assertEquals(1, result.getMessages().stream().filter(m -> ResultSeverityEnum.WARNING.equals(m.getSeverity())
&& "ValueSet http://highmed.org/fhir/ValueSet/highmed-organization not found".equals(m.getMessage()))
.count());
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<tyrus.version>1.15</tyrus.version>
<spring.version>5.2.1.RELEASE</spring.version>

<hapi.version>4.0.3</hapi.version>
<hapi.version>4.1.0</hapi.version>
</properties>

<licenses>
Expand Down

0 comments on commit b70c207

Please sign in to comment.