From 8ea04759762a7368c2bd1f741c321b49d2899f4c Mon Sep 17 00:00:00 2001 From: James Agnew Date: Mon, 27 Jul 2015 11:51:15 -0400 Subject: [PATCH] Deal with composed valuesets in QuestionnaireAnswersValidatorf --- .../QuestionnaireAnswersValidator.java | 31 ++++++++++++++++--- .../QuestionnaireAnswersValidatorTest.java | 20 +++++++++++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/validation/QuestionnaireAnswersValidator.java b/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/validation/QuestionnaireAnswersValidator.java index 2c48e0c0265..116aedf2594 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/validation/QuestionnaireAnswersValidator.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/validation/QuestionnaireAnswersValidator.java @@ -35,9 +35,13 @@ import org.hl7.fhir.instance.model.Type; import org.hl7.fhir.instance.model.UriType; import org.hl7.fhir.instance.model.ValueSet; import org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent; +import org.hl7.fhir.instance.model.ValueSet.ConceptReferenceComponent; +import org.hl7.fhir.instance.model.ValueSet.ConceptSetComponent; import org.hl7.fhir.instance.model.valuesets.IssueType; import org.hl7.fhir.instance.utils.WorkerContext; +import ca.uhn.fhir.context.FhirContext; + /** * Validates that an instance of {@link QuestionnaireAnswers} is valid against the {@link Questionnaire} that it claims to conform to. * @@ -45,8 +49,8 @@ import org.hl7.fhir.instance.utils.WorkerContext; */ public class QuestionnaireAnswersValidator extends BaseValidator { - /* ***************************************************************** - * Note to anyone working on this class - + /* + * ***************************************************************** Note to anyone working on this class - * * This class has unit tests which run within the HAPI project build. Please sync any changes here to HAPI and ensure that unit tests are run. * **************************************************************** @@ -299,8 +303,8 @@ public class QuestionnaireAnswersValidator extends BaseValidator { thePathStack.add("answer(" + answerIdx + ")"); Type nextValue = nextAnswer.getValue(); if (!allowedAnswerTypes.contains(nextValue.getClass())) { - rule(theErrors, IssueType.BUSINESSRULE, thePathStack, false, "Answer to question with linkId[{0}] found of type [{1}] but this is invalid for question of type [{2}]", linkId, nextValue - .getClass().getSimpleName(), type.toCode()); + rule(theErrors, IssueType.BUSINESSRULE, thePathStack, false, "Answer to question with linkId[{0}] found of type [{1}] but this is invalid for question of type [{2}]", linkId, + nextValue.getClass().getSimpleName(), type.toCode()); continue; } @@ -340,6 +344,25 @@ public class QuestionnaireAnswersValidator extends BaseValidator { } } } + if (!found) { + for (ConceptSetComponent nextCompose : valueSet.getCompose().getInclude()) { + if (coding.getSystem().equals(nextCompose.getSystem())) { + for (ConceptReferenceComponent next : nextCompose.getConcept()) { + if (coding.getCode().equals(next.getCode())) { + found = true; + break; + } + } + } + if (found) { + break; + } + } + } + + if (!found) { + System.out.println(FhirContext.forDstu2Hl7Org().newXmlParser().setPrettyPrint(true).encodeResourceToString(valueSet)); + } rule(theErrors, IssueType.BUSINESSRULE, thePathStack, found, "Question with linkId[{0}] has answer with system[{1}] and code[{2}] but this is not a valid answer for ValueSet[{3}]", linkId, coding.getSystem(), coding.getCode(), optionsRef); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/validation/QuestionnaireAnswersValidatorTest.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/validation/QuestionnaireAnswersValidatorTest.java index d13860fe7e5..254abc2cea4 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/validation/QuestionnaireAnswersValidatorTest.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/validation/QuestionnaireAnswersValidatorTest.java @@ -65,6 +65,7 @@ public class QuestionnaireAnswersValidatorTest { ValueSet options = new ValueSet(); options.getCodeSystem().setSystem("urn:system").addConcept().setCode("code0"); + options.getCompose().addInclude().setSystem("urn:system2").addConcept().setCode("code2"); myWorkerCtx.getValueSets().put("http://somevalueset", options); QuestionnaireAnswers qa; @@ -78,7 +79,14 @@ public class QuestionnaireAnswersValidatorTest { errors = new ArrayList(); myVal.validate(errors, qa); assertEquals(errors.toString(), 0, errors.size()); - + + qa = new QuestionnaireAnswers(); + qa.getQuestionnaire().setReference(questionnaireRef); + qa.getGroup().addQuestion().setLinkId("link0").addAnswer().setValue(new Coding().setSystem("urn:system2").setCode("code2")); + errors = new ArrayList(); + myVal.validate(errors, qa); + assertEquals(errors.toString(), 0, errors.size()); + // Bad code qa = new QuestionnaireAnswers(); @@ -89,6 +97,16 @@ public class QuestionnaireAnswersValidatorTest { ourLog.info(errors.toString()); assertThat(errors.toString(), containsString("location=QuestionnaireAnswers.group(0).question(0).answer(0)")); assertThat(errors.toString(), containsString("message=Question with linkId[link0] has answer with system[urn:system] and code[code1] but this is not a valid answer for ValueSet[http://somevalueset]")); + + qa = new QuestionnaireAnswers(); + qa.getQuestionnaire().setReference(questionnaireRef); + qa.getGroup().addQuestion().setLinkId("link0").addAnswer().setValue(new Coding().setSystem("urn:system2").setCode("code3")); + errors = new ArrayList(); + myVal.validate(errors, qa); + ourLog.info(errors.toString()); + assertThat(errors.toString(), containsString("location=QuestionnaireAnswers.group(0).question(0).answer(0)")); + assertThat(errors.toString(), containsString("message=Question with linkId[link0] has answer with system[urn:system2] and code[code3] but this is not a valid answer for ValueSet[http://somevalueset]")); + }