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) {
CodeSystem cs = fetchCodeSystem(theContext, theCodeSystem);
if (cs != null) {
for (ConceptDefinitionComponent next : cs.getConcept()) {
if (next.getCode().equals(theCode)) {
return new CodeValidationResult(next);
}
CodeValidationResult retVal = testIfConceptIsInList(theCode, cs.getConcept());
if (retVal != null) {
return retVal;
}
}
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.assertTrue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.hl7.fhir.dstu3.model.CodeableConcept;
import org.hl7.fhir.dstu3.model.Coding;
import org.hl7.fhir.dstu3.model.Condition;
@ -35,61 +37,58 @@ public class ResourceValidatorDstu3Test {
private static FhirContext ourCtx = FhirContext.forDstu3();
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResourceValidatorDstu3Test.class);
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
/**
* Make sure that the elements that appear in all resources (meta, language, extension, etc)
* all appear in the correct order
* Make sure that the elements that appear in all resources (meta, language, extension, etc) all appear in the correct order
*/
@Test
public void testValidateResourceWithResourceElements() {
XmlParserDstu3Test.TestPatientFor327 patient = new XmlParserDstu3Test.TestPatientFor327();
patient.setBirthDate(new Date());
patient.setId("123");
patient.getText().setDivAsString("<div>FOO</div>");
patient.getText().setStatus(NarrativeStatus.GENERATED);
patient.getLanguageElement().setValue("en");
patient.addExtension().setUrl("http://foo").setValue(new StringType("MOD"));
patient.getMeta().setLastUpdated(new Date());
XmlParserDstu3Test.TestPatientFor327 patient = new XmlParserDstu3Test.TestPatientFor327();
patient.setBirthDate(new Date());
patient.setId("123");
patient.getText().setDivAsString("<div>FOO</div>");
patient.getText().setStatus(NarrativeStatus.GENERATED);
patient.getLanguageElement().setValue("en");
patient.addExtension().setUrl("http://foo").setValue(new StringType("MOD"));
patient.getMeta().setLastUpdated(new Date());
List<Reference> conditions = new ArrayList<Reference>();
Condition condition = new Condition();
condition.getPatient().setReference("Patient/123");
condition.addBodySite().setText("BODY SITE");
condition.getCode().setText("CODE");
condition.setVerificationStatus(ConditionVerificationStatus.CONFIRMED);
List<Reference> conditions = new ArrayList<Reference>();
Condition condition = new Condition();
condition.getPatient().setReference("Patient/123");
condition.addBodySite().setText("BODY SITE");
condition.getCode().setText("CODE");
condition.setVerificationStatus(ConditionVerificationStatus.CONFIRMED);
conditions.add(new Reference(condition));
patient.setCondition(conditions);
patient.addIdentifier().setSystem("http://foo").setValue("123");
String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient);
patient.setCondition(conditions);
patient.addIdentifier().setSystem("http://foo").setValue("123");
String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient);
ourLog.info(encoded);
FhirValidator val = ourCtx.newValidator();
val.registerValidatorModule(new SchemaBaseValidator(ourCtx));
val.registerValidatorModule(new SchematronBaseValidator(ourCtx));
val.registerValidatorModule(new FhirInstanceValidator());
ValidationResult result = val.validateWithResult(encoded);
OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome();
String ooencoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(operationOutcome);
ourLog.info(ooencoded);
assertTrue(result.isSuccessful());
assertThat(ooencoded, containsString("No issues"));
}
/**
* See
* https://groups.google.com/d/msgid/hapi-fhir/a266083f-6454-4cf0-a431-c6500f052bea%40googlegroups.com?utm_medium=
* email&utm_source=footer
* See https://groups.google.com/d/msgid/hapi-fhir/a266083f-6454-4cf0-a431-c6500f052bea%40googlegroups.com?utm_medium= email&utm_source=footer
*/
@Test
public void testValidateWithExtensionsXml() {
@ -104,7 +103,7 @@ public class ResourceValidatorDstu3Test {
IParser p = ourCtx.newXmlParser().setPrettyPrint(true);
String messageString = p.encodeResourceToString(myPatient);
ourLog.info(messageString);
//@formatter:off
assertThat(messageString, stringContainsInOrder(
"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://foo.com/example\""));
//@formatter:on
FhirValidator val = ourCtx.newValidator();
val.registerValidatorModule(new SchemaBaseValidator(ourCtx));
val.registerValidatorModule(new SchematronBaseValidator(ourCtx));
val.registerValidatorModule(new FhirInstanceValidator());
ValidationResult result = val.validateWithResult(messageString);
OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome();
@ -134,15 +133,32 @@ public class ResourceValidatorDstu3Test {
ourLog.info(encoded);
assertTrue(result.isSuccessful());
assertThat(messageString, containsString("valueReference"));
assertThat(messageString, not(containsString("valueResource")));
}
/**
* See
* https://groups.google.com/d/msgid/hapi-fhir/a266083f-6454-4cf0-a431-c6500f052bea%40googlegroups.com?utm_medium=
* email&utm_source=footer
* Per email from Jon Zammit
*/
@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
public void testValidateWithExtensionsJson() {
@ -177,7 +193,7 @@ public class ResourceValidatorDstu3Test {
val.registerValidatorModule(new SchemaBaseValidator(ourCtx));
val.registerValidatorModule(new SchematronBaseValidator(ourCtx));
val.registerValidatorModule(new FhirInstanceValidator());
ValidationResult result = val.validateWithResult(messageString);
OperationOutcome operationOutcome = (OperationOutcome) result.toOperationOutcome();
@ -185,7 +201,7 @@ public class ResourceValidatorDstu3Test {
ourLog.info(encoded);
assertTrue(result.isSuccessful());
assertThat(messageString, containsString("valueReference"));
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
fixing this and providing a pull request!
</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 version="1.5" date="2016-04-20">
<action type="fix" issue="339">