From 18e47c21300e007919bc1f6ed8b20802088a8bb8 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 20 Dec 2016 09:30:46 -0500 Subject: [PATCH] More validator tests --- .../ResourceValidatorDstu3Test.java | 53 +++++++++++++++++++ .../validation/InstanceValidator.java | 5 +- .../validation/FhirInstanceValidatorTest.java | 22 ++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/hapi-fhir-structures-dstu3/src/test/java/org/hl7/fhir/dstu3/hapi/validation/ResourceValidatorDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/org/hl7/fhir/dstu3/hapi/validation/ResourceValidatorDstu3Test.java index 35be7b58a4b..fc8f5472725 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/org/hl7/fhir/dstu3/hapi/validation/ResourceValidatorDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/org/hl7/fhir/dstu3/hapi/validation/ResourceValidatorDstu3Test.java @@ -41,6 +41,7 @@ import ca.uhn.fhir.parser.StrictErrorHandler; import ca.uhn.fhir.parser.XmlParserDstu3Test; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.validation.FhirValidator; +import ca.uhn.fhir.validation.ResultSeverityEnum; import ca.uhn.fhir.validation.SchemaBaseValidator; import ca.uhn.fhir.validation.SingleValidationMessage; import ca.uhn.fhir.validation.ValidationResult; @@ -88,6 +89,58 @@ public class ResourceValidatorDstu3Test { } } + @Test + public void testValidateCodeableConceptContainingOnlyGoodCode() { + Patient p = new Patient(); + p.getMaritalStatus().addCoding().setSystem("http://hl7.org/fhir/v3/MaritalStatus").setCode("M"); + + FhirValidator val = ourCtx.newValidator(); + val.registerValidatorModule(new SchemaBaseValidator(ourCtx)); + val.registerValidatorModule(new SchematronBaseValidator(ourCtx)); + val.registerValidatorModule(new FhirInstanceValidator()); + + ValidationResult output = val.validateWithResult(p); + List all = logResultsAndReturnNonInformationalOnes(output); + assertEquals(0, all.size()); + assertEquals(0, output.getMessages().size()); + } + + @Test + public void testValidateCodeableConceptContainingOnlyBadCode() { + Patient p = new Patient(); + p.getMaritalStatus().addCoding().setSystem("http://hl7.org/fhir/v3/MaritalStatus").setCode("FOO"); + + FhirValidator val = ourCtx.newValidator(); + val.registerValidatorModule(new SchemaBaseValidator(ourCtx)); + val.registerValidatorModule(new SchematronBaseValidator(ourCtx)); + val.registerValidatorModule(new FhirInstanceValidator()); + + ValidationResult output = val.validateWithResult(p); + List all = logResultsAndReturnNonInformationalOnes(output); + + assertEquals("None of the codes provided are in the value set http://hl7.org/fhir/ValueSet/marital-status (http://hl7.org/fhir/ValueSet/marital-status, and a code should come from this value set unless it has no suitable code) (codes = http://hl7.org/fhir/v3/MaritalStatus#FOO)", output.getMessages().get(0).getMessage()); + assertEquals(ResultSeverityEnum.WARNING, output.getMessages().get(0).getSeverity()); + } + + + + private List logResultsAndReturnNonInformationalOnes(ValidationResult theOutput) { + List retVal = new ArrayList(); + + int index = 0; + for (SingleValidationMessage next : theOutput.getMessages()) { + ourLog.info("Result {}: {} - {} - {}", + new Object[] { index, next.getSeverity(), next.getLocationString(), next.getMessage() }); + index++; + + if (next.getSeverity() != ResultSeverityEnum.INFORMATION) { + retVal.add(next); + } + } + + return retVal; + } + /** * Make sure that the elements that appear in all resources (meta, language, extension, etc) all appear in the correct order */ diff --git a/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/validation/InstanceValidator.java b/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/validation/InstanceValidator.java index 38c399bbaf3..8c453d9900c 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/validation/InstanceValidator.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/validation/InstanceValidator.java @@ -1814,8 +1814,9 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat else { if (type.equals("Identifier")) checkIdentifier(errors, ei.path, ei.element, ei.definition); - else if (type.equals("Coding")) - checkCoding(errors, ei.path, ei.element, profile, ei.definition); + else if (type.equals("Coding")) { + checkCoding(errors, ei.path, ei.element, profile, ei.definition); + } else if (type.equals("CodeableConcept")) checkCodeableConcept(errors, ei.path, ei.element, profile, ei.definition); else if (type.equals("Reference")) diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/validation/FhirInstanceValidatorTest.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/validation/FhirInstanceValidatorTest.java index db8ed1369f4..3d98457f8c4 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/validation/FhirInstanceValidatorTest.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/validation/FhirInstanceValidatorTest.java @@ -42,6 +42,7 @@ import org.junit.rules.TestWatcher; import org.junit.runner.Description; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import org.springframework.web.bind.annotation.GetMapping; import ca.uhn.fhir.context.FhirContext; @@ -400,6 +401,27 @@ public class FhirInstanceValidatorTest { } + @Test + public void testValidateCodeableConceptContainingOnlyGoodCode() { + Patient p = new Patient(); + p.getMaritalStatus().addCoding().setSystem("http://hl7.org/fhir/v3/MaritalStatus").setCode("M"); + + ValidationResult output = myVal.validateWithResult(p); + List all = logResultsAndReturnNonInformationalOnes(output); + assertEquals(0, all.size()); + } + + @Test + public void testValidateCodeableConceptContainingOnlyBadCode() { + Patient p = new Patient(); + p.getMaritalStatus().addCoding().setSystem("http://hl7.org/fhir/v3/MaritalStatus").setCode("FOO"); + + ValidationResult output = myVal.validateWithResult(p); + List all = logResultsAndReturnNonInformationalOnes(output); + assertEquals(1, all.size()); + + assertEquals("Unknown Code (http://hl7.org/fhir/v3/MaritalStatus#FOO)", output.getMessages().get(0).getMessage()); + } @Test public void testValidateResourceWithValuesetExpansion() {