Skip to content

Commit

Permalink
failing test for invalid profile during validation
Browse files Browse the repository at this point in the history
  • Loading branch information
leif stawnyczy committed Jan 9, 2025
1 parent 544f832 commit 97660a3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ public void testValidateStorageResponseCodeBad() {
List<SingleValidationMessage> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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));
}
Expand Down

0 comments on commit 97660a3

Please sign in to comment.