More validator tests

This commit is contained in:
James 2016-12-20 09:30:46 -05:00
parent 2a94242f22
commit 18e47c2130
3 changed files with 78 additions and 2 deletions

View File

@ -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<SingleValidationMessage> 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<SingleValidationMessage> 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<SingleValidationMessage> logResultsAndReturnNonInformationalOnes(ValidationResult theOutput) {
List<SingleValidationMessage> retVal = new ArrayList<SingleValidationMessage>();
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
*/

View File

@ -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"))

View File

@ -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<SingleValidationMessage> 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<SingleValidationMessage> 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() {