diff --git a/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/FhirInstanceValidatorR4Test.java b/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/FhirInstanceValidatorR4Test.java index da6f97b3192..87e3a9e2337 100644 --- a/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/FhirInstanceValidatorR4Test.java +++ b/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/FhirInstanceValidatorR4Test.java @@ -233,7 +233,6 @@ public void testValidateStorageResponseCodeBad() { List all = logResultsAndReturnErrorOnes(result); assertThat(result.isSuccessful()).as(all.toString()).isFalse(); assertThat(result.getMessages().get(0).getMessage()).startsWith("Unknown code 'https://hapifhir.io/fhir/CodeSystem/hapi-fhir-storage-response-code#foo'"); - } @Test diff --git a/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/ParserWithValidationR4Test.java b/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/ParserWithValidationR4Test.java index 780562ee882..2b06db5209c 100644 --- a/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/ParserWithValidationR4Test.java +++ b/hapi-fhir-validation/src/test/java/org/hl7/fhir/r4/validation/ParserWithValidationR4Test.java @@ -4,15 +4,22 @@ import ca.uhn.fhir.context.support.DefaultProfileValidationSupport; import ca.uhn.fhir.context.support.IValidationSupport; import ca.uhn.fhir.fhirpath.BaseValidationTestWithInlineMocks; +import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.validation.FhirValidator; +import ca.uhn.fhir.validation.ResultSeverityEnum; +import ca.uhn.fhir.validation.ValidationOptions; import ca.uhn.fhir.validation.ValidationResult; import org.hl7.fhir.common.hapi.validation.support.ValidationSupportChain; import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator; import org.hl7.fhir.r4.model.MedicationRequest; +import org.hl7.fhir.r4.model.Patient; import org.junit.jupiter.api.Test; import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + public class ParserWithValidationR4Test extends BaseValidationTestWithInlineMocks { private static final FhirContext ourCtx = FhirContext.forR4(); @@ -30,6 +37,44 @@ public void testActivityDefinitionElementsOrder() throws IOException { validationResult.getMessages().forEach(System.out::println); } + // https://www.hl7.org/fhir/r4/resource-operation-validate.html + @Test + public void validate_withUnknownProfile_shouldFail() { + // setup + IParser parser = ourCtx.newJsonParser(); + Patient patient; + { + String patientStr = """ + { + "resourceType": "Patient", + "name": [{ + "family": "Hirasawa", + "given": [ "yui" ] + }] + } + """; + patient = parser.parseResource(Patient.class, patientStr); + } + + // test + ValidationOptions options = new ValidationOptions(); + options.addProfile("http://tempuri.org/does-not-exist"); + + final FhirInstanceValidator instanceValidator = new FhirInstanceValidator(ourCtx); + FhirValidator validator = ourCtx.newValidator(); + + // test + validator.registerValidatorModule(instanceValidator); + ValidationResult validationResult = validator.validateWithResult(patient, options); + + // verify + assertFalse(validationResult.isSuccessful()); + assertTrue(validationResult.getMessages().stream() + .anyMatch(r -> r.getSeverity() == ResultSeverityEnum.ERROR)); + // TODO - add validation for the specific error to be thrown + } + + private IValidationSupport getValidationSupport() { return new ValidationSupportChain(new DefaultProfileValidationSupport(ourCtx)); }