Deal with composed valuesets in QuestionnaireAnswersValidatorf
This commit is contained in:
parent
75798cf9fe
commit
8ea0475976
|
@ -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);
|
||||
|
|
|
@ -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<ValidationMessage>();
|
||||
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<ValidationMessage>();
|
||||
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<ValidationMessage>();
|
||||
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]"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue