Fix validation issue

This commit is contained in:
James Agnew 2016-05-06 17:44:23 -04:00
parent fb277a0595
commit fd2671d28e
4 changed files with 1595 additions and 40 deletions

View File

@ -223,14 +223,32 @@ public class DefaultProfileValidationSupport implements IValidationSupport {
public CodeValidationResult validateCode(FhirContext theContext, String theCodeSystem, String theCode, String theDisplay) { public CodeValidationResult validateCode(FhirContext theContext, String theCodeSystem, String theCode, String theDisplay) {
CodeSystem cs = fetchCodeSystem(theContext, theCodeSystem); CodeSystem cs = fetchCodeSystem(theContext, theCodeSystem);
if (cs != null) { if (cs != null) {
for (ConceptDefinitionComponent next : cs.getConcept()) { CodeValidationResult retVal = testIfConceptIsInList(theCode, cs.getConcept());
if (next.getCode().equals(theCode)) {
return new CodeValidationResult(next); if (retVal != null) {
} return retVal;
} }
} }
return new CodeValidationResult(IssueSeverity.INFORMATION, "Unknown code: " + theCodeSystem + " / " + theCode); return new CodeValidationResult(IssueSeverity.INFORMATION, "Unknown code: " + theCodeSystem + " / " + theCode);
} }
private CodeValidationResult testIfConceptIsInList(String theCode, List<ConceptDefinitionComponent> conceptList) {
CodeValidationResult retVal = null;
for (ConceptDefinitionComponent next : conceptList) {
if (next.getCode().equals(theCode)) {
retVal = new CodeValidationResult(next);
break;
}
// recurse
retVal = testIfConceptIsInList(theCode, next.getConcept());
if (retVal != null) {
break;
}
}
return retVal;
}
} }

View File

@ -6,10 +6,12 @@ import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.apache.commons.io.IOUtils;
import org.hl7.fhir.dstu3.model.CodeableConcept; import org.hl7.fhir.dstu3.model.CodeableConcept;
import org.hl7.fhir.dstu3.model.Coding; import org.hl7.fhir.dstu3.model.Coding;
import org.hl7.fhir.dstu3.model.Condition; import org.hl7.fhir.dstu3.model.Condition;
@ -35,61 +37,58 @@ public class ResourceValidatorDstu3Test {
private static FhirContext ourCtx = FhirContext.forDstu3(); private static FhirContext ourCtx = FhirContext.forDstu3();
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResourceValidatorDstu3Test.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResourceValidatorDstu3Test.class);
@AfterClass @AfterClass
public static void afterClassClearContext() { public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest(); TestUtil.clearAllStaticFieldsForUnitTest();
} }
/** /**
* Make sure that the elements that appear in all resources (meta, language, extension, etc) * Make sure that the elements that appear in all resources (meta, language, extension, etc) all appear in the correct order
* all appear in the correct order
*/ */
@Test @Test
public void testValidateResourceWithResourceElements() { public void testValidateResourceWithResourceElements() {
XmlParserDstu3Test.TestPatientFor327 patient = new XmlParserDstu3Test.TestPatientFor327(); XmlParserDstu3Test.TestPatientFor327 patient = new XmlParserDstu3Test.TestPatientFor327();
patient.setBirthDate(new Date()); patient.setBirthDate(new Date());
patient.setId("123"); patient.setId("123");
patient.getText().setDivAsString("<div>FOO</div>"); patient.getText().setDivAsString("<div>FOO</div>");
patient.getText().setStatus(NarrativeStatus.GENERATED); patient.getText().setStatus(NarrativeStatus.GENERATED);
patient.getLanguageElement().setValue("en"); patient.getLanguageElement().setValue("en");
patient.addExtension().setUrl("http://foo").setValue(new StringType("MOD")); patient.addExtension().setUrl("http://foo").setValue(new StringType("MOD"));
patient.getMeta().setLastUpdated(new Date()); patient.getMeta().setLastUpdated(new Date());
List<Reference> conditions = new ArrayList<Reference>(); List<Reference> conditions = new ArrayList<Reference>();
Condition condition = new Condition(); Condition condition = new Condition();
condition.getPatient().setReference("Patient/123"); condition.getPatient().setReference("Patient/123");
condition.addBodySite().setText("BODY SITE"); condition.addBodySite().setText("BODY SITE");
condition.getCode().setText("CODE"); condition.getCode().setText("CODE");
condition.setVerificationStatus(ConditionVerificationStatus.CONFIRMED); condition.setVerificationStatus(ConditionVerificationStatus.CONFIRMED);
conditions.add(new Reference(condition)); conditions.add(new Reference(condition));
patient.setCondition(conditions); patient.setCondition(conditions);
patient.addIdentifier().setSystem("http://foo").setValue("123"); patient.addIdentifier().setSystem("http://foo").setValue("123");
String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient);
ourLog.info(encoded); ourLog.info(encoded);
FhirValidator val = ourCtx.newValidator(); FhirValidator val = ourCtx.newValidator();
val.registerValidatorModule(new SchemaBaseValidator(ourCtx)); val.registerValidatorModule(new SchemaBaseValidator(ourCtx));
val.registerValidatorModule(new SchematronBaseValidator(ourCtx)); val.registerValidatorModule(new SchematronBaseValidator(ourCtx));
val.registerValidatorModule(new FhirInstanceValidator()); val.registerValidatorModule(new FhirInstanceValidator());
ValidationResult result = val.validateWithResult(encoded); ValidationResult result = val.validateWithResult(encoded);
OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome(); OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome();
String ooencoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome); String ooencoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome);
ourLog.info(ooencoded); ourLog.info(ooencoded);
assertTrue(result.isSuccessful()); assertTrue(result.isSuccessful());
assertThat(ooencoded, containsString("No issues")); assertThat(ooencoded, containsString("No issues"));
} }
/** /**
* See * See https://groups.google.com/d/msgid/hapi-fhir/a266083f-6454-4cf0-a431-c6500f052bea%40googlegroups.com?utm_medium= email&utm_source=footer
* https://groups.google.com/d/msgid/hapi-fhir/a266083f-6454-4cf0-a431-c6500f052bea%40googlegroups.com?utm_medium=
* email&utm_source=footer
*/ */
@Test @Test
public void testValidateWithExtensionsXml() { public void testValidateWithExtensionsXml() {
@ -104,7 +103,7 @@ public class ResourceValidatorDstu3Test {
IParser p = ourCtx.newXmlParser().setPrettyPrint(true); IParser p = ourCtx.newXmlParser().setPrettyPrint(true);
String messageString = p.encodeResourceToString(myPatient); String messageString = p.encodeResourceToString(myPatient);
ourLog.info(messageString); ourLog.info(messageString);
//@formatter:off //@formatter:off
assertThat(messageString, stringContainsInOrder( assertThat(messageString, stringContainsInOrder(
"meta", "meta",
@ -121,12 +120,12 @@ public class ResourceValidatorDstu3Test {
assertThat(messageString, containsString("url=\"http://ahr.copa.inso.tuwien.ac.at/StructureDefinition/Patient#animal-colorSecondary\"")); assertThat(messageString, containsString("url=\"http://ahr.copa.inso.tuwien.ac.at/StructureDefinition/Patient#animal-colorSecondary\""));
assertThat(messageString, containsString("url=\"http://foo.com/example\"")); assertThat(messageString, containsString("url=\"http://foo.com/example\""));
//@formatter:on //@formatter:on
FhirValidator val = ourCtx.newValidator(); FhirValidator val = ourCtx.newValidator();
val.registerValidatorModule(new SchemaBaseValidator(ourCtx)); val.registerValidatorModule(new SchemaBaseValidator(ourCtx));
val.registerValidatorModule(new SchematronBaseValidator(ourCtx)); val.registerValidatorModule(new SchematronBaseValidator(ourCtx));
val.registerValidatorModule(new FhirInstanceValidator()); val.registerValidatorModule(new FhirInstanceValidator());
ValidationResult result = val.validateWithResult(messageString); ValidationResult result = val.validateWithResult(messageString);
OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome(); OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome();
@ -134,15 +133,32 @@ public class ResourceValidatorDstu3Test {
ourLog.info(encoded); ourLog.info(encoded);
assertTrue(result.isSuccessful()); assertTrue(result.isSuccessful());
assertThat(messageString, containsString("valueReference")); assertThat(messageString, containsString("valueReference"));
assertThat(messageString, not(containsString("valueResource"))); assertThat(messageString, not(containsString("valueResource")));
} }
/** /**
* See * Per email from Jon Zammit
* https://groups.google.com/d/msgid/hapi-fhir/a266083f-6454-4cf0-a431-c6500f052bea%40googlegroups.com?utm_medium= */
* email&utm_source=footer @Test
public void testValidateQuestionnaire() throws IOException {
String input = IOUtils.toString(getClass().getResourceAsStream("/questionnaire_jon_z_20160506.xml"));
FhirValidator val = ourCtx.newValidator();
val.registerValidatorModule(new FhirInstanceValidator());
ValidationResult result = val.validateWithResult(input);
OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome();
String ooencoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome);
ourLog.info(ooencoded);
assertTrue(result.isSuccessful());
}
/**
* See https://groups.google.com/d/msgid/hapi-fhir/a266083f-6454-4cf0-a431-c6500f052bea%40googlegroups.com?utm_medium= email&utm_source=footer
*/ */
@Test @Test
public void testValidateWithExtensionsJson() { public void testValidateWithExtensionsJson() {
@ -177,7 +193,7 @@ public class ResourceValidatorDstu3Test {
val.registerValidatorModule(new SchemaBaseValidator(ourCtx)); val.registerValidatorModule(new SchemaBaseValidator(ourCtx));
val.registerValidatorModule(new SchematronBaseValidator(ourCtx)); val.registerValidatorModule(new SchematronBaseValidator(ourCtx));
val.registerValidatorModule(new FhirInstanceValidator()); val.registerValidatorModule(new FhirInstanceValidator());
ValidationResult result = val.validateWithResult(messageString); ValidationResult result = val.validateWithResult(messageString);
OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome(); OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome();
@ -185,7 +201,7 @@ public class ResourceValidatorDstu3Test {
ourLog.info(encoded); ourLog.info(encoded);
assertTrue(result.isSuccessful()); assertTrue(result.isSuccessful());
assertThat(messageString, containsString("valueReference")); assertThat(messageString, containsString("valueReference"));
assertThat(messageString, not(containsString("valueResource"))); assertThat(messageString, not(containsString("valueResource")));
} }

File diff suppressed because it is too large Load Diff

View File

@ -107,6 +107,12 @@
Properly handle null arrays when parsing JSON resources. Thanks to Subhro for Properly handle null arrays when parsing JSON resources. Thanks to Subhro for
fixing this and providing a pull request! fixing this and providing a pull request!
</action> </action>
<action type="fix">
STU3 validator failed to validate codes where the
code was a child code within the code system that contained it
(i.e. not a top level code). Thanks to Jon
Zammit for reporting!
</action>
</release> </release>
<release version="1.5" date="2016-04-20"> <release version="1.5" date="2016-04-20">
<action type="fix" issue="339"> <action type="fix" issue="339">