Update tests to reflect questions
This commit is contained in:
parent
15497e39a9
commit
49d209823f
|
@ -1,160 +0,0 @@
|
||||||
package ca.uhn.fhir.jpa.provider;
|
|
||||||
|
|
||||||
import ca.uhn.fhir.context.FhirContext;
|
|
||||||
import ca.uhn.fhir.context.support.DefaultProfileValidationSupport;
|
|
||||||
import ca.uhn.fhir.jpa.test.BaseJpaR4Test;
|
|
||||||
import ca.uhn.fhir.validation.FhirValidator;
|
|
||||||
import ca.uhn.fhir.validation.SingleValidationMessage;
|
|
||||||
import ca.uhn.fhir.validation.ValidationResult;
|
|
||||||
import org.hl7.fhir.common.hapi.validation.support.CachingValidationSupport;
|
|
||||||
import org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService;
|
|
||||||
import org.hl7.fhir.common.hapi.validation.support.InMemoryTerminologyServerValidationSupport;
|
|
||||||
import org.hl7.fhir.common.hapi.validation.support.PrePopulatedValidationSupport;
|
|
||||||
import org.hl7.fhir.common.hapi.validation.support.SnapshotGeneratingValidationSupport;
|
|
||||||
import org.hl7.fhir.common.hapi.validation.support.ValidationSupportChain;
|
|
||||||
import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator;
|
|
||||||
import org.hl7.fhir.r4.model.CodeType;
|
|
||||||
import org.hl7.fhir.r4.model.ElementDefinition;
|
|
||||||
import org.hl7.fhir.r4.model.Enumerations;
|
|
||||||
import org.hl7.fhir.r4.model.Observation;
|
|
||||||
import org.hl7.fhir.r4.model.Patient;
|
|
||||||
import org.hl7.fhir.r4.model.Quantity;
|
|
||||||
import org.hl7.fhir.r4.model.StructureDefinition;
|
|
||||||
import org.hl7.fhir.r4.model.UriType;
|
|
||||||
import org.hl7.fhir.r4.model.ValueSet;
|
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
public class DiscoveryValidationSupportTest extends BaseJpaR4Test {
|
|
||||||
private static final FhirContext ourCtx = FhirContext.forR4();
|
|
||||||
private static FhirValidator myFhirValidator;
|
|
||||||
|
|
||||||
private static PrePopulatedValidationSupport ourValidationSupport;
|
|
||||||
|
|
||||||
@BeforeAll
|
|
||||||
public static void setup() {
|
|
||||||
myFhirValidator = ourCtx.newValidator();
|
|
||||||
myFhirValidator.setValidateAgainstStandardSchema(false);
|
|
||||||
myFhirValidator.setValidateAgainstStandardSchematron(false);
|
|
||||||
|
|
||||||
ourValidationSupport = new PrePopulatedValidationSupport(ourCtx);
|
|
||||||
|
|
||||||
ValidationSupportChain chain = new ValidationSupportChain(
|
|
||||||
new DefaultProfileValidationSupport(ourCtx),
|
|
||||||
new SnapshotGeneratingValidationSupport(ourCtx),
|
|
||||||
new CommonCodeSystemsTerminologyService(ourCtx),
|
|
||||||
new InMemoryTerminologyServerValidationSupport(ourCtx),
|
|
||||||
ourValidationSupport);
|
|
||||||
CachingValidationSupport myValidationSupport = new CachingValidationSupport(chain, true);
|
|
||||||
FhirInstanceValidator myInstanceVal = new FhirInstanceValidator(myValidationSupport);
|
|
||||||
myFhirValidator.registerValidatorModule(myInstanceVal);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test() {
|
|
||||||
ValueSet vs = new ValueSet();
|
|
||||||
vs.setUrl("http://vs");
|
|
||||||
vs.getCompose().addInclude().setSystem("http://cs")
|
|
||||||
.addConcept(new ValueSet.ConceptReferenceComponent(new CodeType("code1")))
|
|
||||||
.addConcept(new ValueSet.ConceptReferenceComponent(new CodeType("code2")));
|
|
||||||
myValueSetDao.create(vs);
|
|
||||||
|
|
||||||
StructureDefinition sd1 = new StructureDefinition()
|
|
||||||
.setUrl("http://example.org/fhir/StructureDefinition/TestObservation").setVersion("v1")
|
|
||||||
.setBaseDefinition("http://hl7.org/fhir/StructureDefinition/Observation")
|
|
||||||
.setType("Observation")
|
|
||||||
.setDerivation(StructureDefinition.TypeDerivationRule.CONSTRAINT);
|
|
||||||
sd1.getDifferential()
|
|
||||||
.addElement()
|
|
||||||
.setPath("Observation.value[x]")
|
|
||||||
.addType(new ElementDefinition.TypeRefComponent(new UriType("Quantity")))
|
|
||||||
.setBinding(new ElementDefinition.ElementDefinitionBindingComponent().setStrength(Enumerations.BindingStrength.REQUIRED).setValueSet("http://vs1"))
|
|
||||||
.setId("Observation.value[x]");
|
|
||||||
myStructureDefinitionDao.create(sd1, mySrd);
|
|
||||||
|
|
||||||
StructureDefinition sd2 = new StructureDefinition()
|
|
||||||
.setUrl("http://example.org/fhir/StructureDefinition/TestObservation").setVersion("v2")
|
|
||||||
.setBaseDefinition("http://hl7.org/fhir/StructureDefinition/Observation")
|
|
||||||
.setType("Observation")
|
|
||||||
.setDerivation(StructureDefinition.TypeDerivationRule.CONSTRAINT);
|
|
||||||
sd2.getDifferential()
|
|
||||||
.addElement()
|
|
||||||
.setPath("Observation.value[x]")
|
|
||||||
.addType(new ElementDefinition.TypeRefComponent(new UriType("Quantity")))
|
|
||||||
.setBinding(new ElementDefinition.ElementDefinitionBindingComponent().setStrength(Enumerations.BindingStrength.REQUIRED).setValueSet("http://vs"))
|
|
||||||
.setId("Observation.value[x]");
|
|
||||||
myStructureDefinitionDao.create(sd2, mySrd);
|
|
||||||
|
|
||||||
ourValidationSupport.addStructureDefinition(sd1);
|
|
||||||
ourValidationSupport.addStructureDefinition(sd2);
|
|
||||||
|
|
||||||
Observation observation = new Observation();
|
|
||||||
observation.getMeta().addProfile("http://example.org/fhir/StructureDefinition/TestObservation");
|
|
||||||
observation.setStatus(Observation.ObservationStatus.REGISTERED);
|
|
||||||
observation.getCode().setText("new-visit");
|
|
||||||
observation.setValue(new Quantity().setSystem("http://cs").setCode("code1").setValue(123));
|
|
||||||
|
|
||||||
ValidationResult validationResult = myFhirValidator.validateWithResult(observation);
|
|
||||||
for (SingleValidationMessage message : validationResult.getMessages()) {
|
|
||||||
ourLog.info(message.toString());
|
|
||||||
}
|
|
||||||
assertTrue(validationResult.isSuccessful());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test_simple() {
|
|
||||||
StructureDefinition sdIdentifier = new StructureDefinition()
|
|
||||||
.setUrl("http://example.org/fhir/StructureDefinition/TestPatient").setVersion("1")
|
|
||||||
.setBaseDefinition("http://hl7.org/fhir/StructureDefinition/Patient-identifier")
|
|
||||||
.setType("Patient")
|
|
||||||
.setDerivation(StructureDefinition.TypeDerivationRule.CONSTRAINT);
|
|
||||||
sdIdentifier.getDifferential().addElement()
|
|
||||||
.setPath("Patient.identifier")
|
|
||||||
.setMin(1)
|
|
||||||
.setId("Patient.identifier");
|
|
||||||
myStructureDefinitionDao.create(sdIdentifier, mySrd);
|
|
||||||
|
|
||||||
StructureDefinition sdName = new StructureDefinition()
|
|
||||||
.setUrl("http://example.org/fhir/StructureDefinition/TestPatient").setVersion("2")
|
|
||||||
.setBaseDefinition("http://hl7.org/fhir/StructureDefinition/Patient-name")
|
|
||||||
.setType("Patient")
|
|
||||||
.setDerivation(StructureDefinition.TypeDerivationRule.CONSTRAINT);
|
|
||||||
sdName.getDifferential().addElement()
|
|
||||||
.setPath("Patient.name")
|
|
||||||
.setMin(1)
|
|
||||||
.setMustSupport(true)
|
|
||||||
.setId("Patient.name");
|
|
||||||
myStructureDefinitionDao.create(sdName, mySrd);
|
|
||||||
|
|
||||||
StructureDefinition sdBirthDate = new StructureDefinition()
|
|
||||||
.setUrl("http://example.org/fhir/StructureDefinition/TestPatient").setVersion("3")
|
|
||||||
.setBaseDefinition("http://hl7.org/fhir/StructureDefinition/Patient")
|
|
||||||
.setType("Patient")
|
|
||||||
.setDerivation(StructureDefinition.TypeDerivationRule.CONSTRAINT);
|
|
||||||
sdBirthDate.getDifferential().addElement()
|
|
||||||
.setPath("Patient.birthDate")
|
|
||||||
.setMin(1)
|
|
||||||
.setMustSupport(true)
|
|
||||||
.setId("Patient.birthDate");
|
|
||||||
myStructureDefinitionDao.create(sdBirthDate, mySrd);
|
|
||||||
|
|
||||||
ourValidationSupport.addStructureDefinition(sdIdentifier);
|
|
||||||
ourValidationSupport.addStructureDefinition(sdName);
|
|
||||||
ourValidationSupport.addStructureDefinition(sdBirthDate);
|
|
||||||
|
|
||||||
Patient patient = new Patient();
|
|
||||||
patient.getMeta()
|
|
||||||
.addProfile("http://example.org/fhir/StructureDefinition/TestPatient-name|1")
|
|
||||||
.addProfile("http://example.org/fhir/StructureDefinition/TestPatient-identifier|2");
|
|
||||||
myPatientDao.create(patient, mySrd);
|
|
||||||
|
|
||||||
ValidationResult validationResult = myFhirValidator.validateWithResult(patient);
|
|
||||||
for (SingleValidationMessage message : validationResult.getMessages()) {
|
|
||||||
ourLog.info(message.toString());
|
|
||||||
}
|
|
||||||
assertTrue(validationResult.isSuccessful());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,128 @@
|
||||||
|
package ca.uhn.fhir.jpa.validation;
|
||||||
|
|
||||||
|
import ca.uhn.fhir.context.FhirContext;
|
||||||
|
import ca.uhn.fhir.context.support.DefaultProfileValidationSupport;
|
||||||
|
import ca.uhn.fhir.jpa.api.model.DaoMethodOutcome;
|
||||||
|
import ca.uhn.fhir.jpa.test.BaseJpaR4Test;
|
||||||
|
import ca.uhn.fhir.validation.FhirValidator;
|
||||||
|
import ca.uhn.fhir.validation.SingleValidationMessage;
|
||||||
|
import ca.uhn.fhir.validation.ValidationResult;
|
||||||
|
import org.hl7.fhir.common.hapi.validation.support.CachingValidationSupport;
|
||||||
|
import org.hl7.fhir.common.hapi.validation.support.CommonCodeSystemsTerminologyService;
|
||||||
|
import org.hl7.fhir.common.hapi.validation.support.InMemoryTerminologyServerValidationSupport;
|
||||||
|
import org.hl7.fhir.common.hapi.validation.support.PrePopulatedValidationSupport;
|
||||||
|
import org.hl7.fhir.common.hapi.validation.support.SnapshotGeneratingValidationSupport;
|
||||||
|
import org.hl7.fhir.common.hapi.validation.support.ValidationSupportChain;
|
||||||
|
import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator;
|
||||||
|
import org.hl7.fhir.r4.model.Patient;
|
||||||
|
import org.hl7.fhir.r4.model.StructureDefinition;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class ResourceValidationR4Test extends BaseJpaR4Test {
|
||||||
|
private static final FhirContext ourCtx = FhirContext.forR4();
|
||||||
|
private static FhirValidator myFhirValidator;
|
||||||
|
private static PrePopulatedValidationSupport ourValidationSupport;
|
||||||
|
private static final String PATIENT_STRUCTURE_DEFINITION_URL = "http://example.org/fhir/StructureDefinition/TestPatient";
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void setup() {
|
||||||
|
myFhirValidator = ourCtx.newValidator();
|
||||||
|
myFhirValidator.setValidateAgainstStandardSchema(false);
|
||||||
|
myFhirValidator.setValidateAgainstStandardSchematron(false);
|
||||||
|
|
||||||
|
ourValidationSupport = new PrePopulatedValidationSupport(ourCtx);
|
||||||
|
|
||||||
|
ValidationSupportChain chain = new ValidationSupportChain(
|
||||||
|
new DefaultProfileValidationSupport(ourCtx),
|
||||||
|
new SnapshotGeneratingValidationSupport(ourCtx),
|
||||||
|
new CommonCodeSystemsTerminologyService(ourCtx),
|
||||||
|
new InMemoryTerminologyServerValidationSupport(ourCtx),
|
||||||
|
ourValidationSupport);
|
||||||
|
CachingValidationSupport myValidationSupport = new CachingValidationSupport(chain, true);
|
||||||
|
FhirInstanceValidator myInstanceVal = new FhirInstanceValidator(myValidationSupport);
|
||||||
|
myFhirValidator.registerValidatorModule(myInstanceVal);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateStructureDefinition_createMultipleWithWithSameUrl_isStoredSuccessfully() {
|
||||||
|
createPatientStructureDefinitionWithMandatoryField(PATIENT_STRUCTURE_DEFINITION_URL, "1", "Patient.identifier");
|
||||||
|
createPatientStructureDefinitionWithMandatoryField(PATIENT_STRUCTURE_DEFINITION_URL, "2", "Patient.name");
|
||||||
|
createPatientStructureDefinitionWithMandatoryField(PATIENT_STRUCTURE_DEFINITION_URL, "3", "Patient.birthdate");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidatePatient_withProfileIncludingVersion_shouldUseSpecifiedVersion() {
|
||||||
|
createPatientStructureDefinitionWithMandatoryField(PATIENT_STRUCTURE_DEFINITION_URL, "1", "Patient.identifier");
|
||||||
|
createPatientStructureDefinitionWithMandatoryField(PATIENT_STRUCTURE_DEFINITION_URL, "2", "Patient.name");
|
||||||
|
|
||||||
|
Patient patient = new Patient();
|
||||||
|
patient.getMeta().addProfile(PATIENT_STRUCTURE_DEFINITION_URL + "|1");
|
||||||
|
myPatientDao.create(patient, mySrd);
|
||||||
|
|
||||||
|
ValidationResult validationResult = myFhirValidator.validateWithResult(patient);
|
||||||
|
assertFalse(validationResult.isSuccessful());
|
||||||
|
assertEquals(1, validationResult.getMessages().size());
|
||||||
|
|
||||||
|
SingleValidationMessage message = validationResult.getMessages().iterator().next();
|
||||||
|
assertTrue(message.getMessage().contains( PATIENT_STRUCTURE_DEFINITION_URL + "|1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidatePatient_withProfileNoVersion_shouldUsesLatestVersion() {
|
||||||
|
createPatientStructureDefinitionWithMandatoryField(PATIENT_STRUCTURE_DEFINITION_URL, "1", "Patient.identifier");
|
||||||
|
createPatientStructureDefinitionWithMandatoryField(PATIENT_STRUCTURE_DEFINITION_URL, "2", "Patient.name");
|
||||||
|
|
||||||
|
Patient patient = new Patient();
|
||||||
|
patient.getMeta().addProfile(PATIENT_STRUCTURE_DEFINITION_URL);
|
||||||
|
myPatientDao.create(patient, mySrd);
|
||||||
|
|
||||||
|
ValidationResult validationResult = myFhirValidator.validateWithResult(patient);
|
||||||
|
assertFalse(validationResult.isSuccessful());
|
||||||
|
assertEquals(1, validationResult.getMessages().size());
|
||||||
|
|
||||||
|
SingleValidationMessage message = validationResult.getMessages().iterator().next();
|
||||||
|
assertTrue(message.getMessage().contains(PATIENT_STRUCTURE_DEFINITION_URL + "|2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStructureDefinition_createResourceWithMultipleProfilesSameStructureDefinition_usesFirstVersion() {
|
||||||
|
createPatientStructureDefinitionWithMandatoryField(PATIENT_STRUCTURE_DEFINITION_URL, "1", "Patient.identifier");
|
||||||
|
createPatientStructureDefinitionWithMandatoryField(PATIENT_STRUCTURE_DEFINITION_URL, "2", "Patient.name");
|
||||||
|
createPatientStructureDefinitionWithMandatoryField(PATIENT_STRUCTURE_DEFINITION_URL, "3", "Patient.birthDate");
|
||||||
|
|
||||||
|
Patient patient = new Patient();
|
||||||
|
patient.getMeta()
|
||||||
|
.addProfile(PATIENT_STRUCTURE_DEFINITION_URL + "|2")
|
||||||
|
.addProfile(PATIENT_STRUCTURE_DEFINITION_URL + "|1")
|
||||||
|
.addProfile(PATIENT_STRUCTURE_DEFINITION_URL + "|3");
|
||||||
|
myPatientDao.create(patient, mySrd);
|
||||||
|
|
||||||
|
ValidationResult validationResult = myFhirValidator.validateWithResult(patient);
|
||||||
|
assertFalse(validationResult.isSuccessful());
|
||||||
|
assertEquals(1, validationResult.getMessages().size());
|
||||||
|
|
||||||
|
SingleValidationMessage message = validationResult.getMessages().iterator().next();
|
||||||
|
assertTrue(message.getMessage().contains(PATIENT_STRUCTURE_DEFINITION_URL + "|1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createPatientStructureDefinitionWithMandatoryField(String theUrl, String theVersion, String thePath) {
|
||||||
|
StructureDefinition sd = new StructureDefinition()
|
||||||
|
.setUrl(theUrl).setVersion(theVersion)
|
||||||
|
.setBaseDefinition("http://hl7.org/fhir/StructureDefinition/Patient")
|
||||||
|
.setType("Patient")
|
||||||
|
.setDerivation(StructureDefinition.TypeDerivationRule.CONSTRAINT);
|
||||||
|
sd.getDifferential().addElement()
|
||||||
|
.setPath(thePath)
|
||||||
|
.setMin(1)
|
||||||
|
.setId(thePath);
|
||||||
|
|
||||||
|
DaoMethodOutcome outcome = myStructureDefinitionDao.create(sd, mySrd);
|
||||||
|
assertTrue(outcome.getCreated());
|
||||||
|
ourValidationSupport.addStructureDefinition(sd);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue