From 78ab11220489173907a0e35c9668ebb627a0cb6c Mon Sep 17 00:00:00 2001 From: Matti Uusitalo Date: Fri, 9 Nov 2018 12:55:13 +0200 Subject: [PATCH] Added check for answering questions which are not enabled --- .../fhir/r4/validation/InstanceValidator.java | 1 + ...estionnaireResponseValidatorDstu3Test.java | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/hapi-fhir-validation/src/main/java/org/hl7/fhir/r4/validation/InstanceValidator.java b/hapi-fhir-validation/src/main/java/org/hl7/fhir/r4/validation/InstanceValidator.java index 26f95174350..1f6de3dd1e1 100644 --- a/hapi-fhir-validation/src/main/java/org/hl7/fhir/r4/validation/InstanceValidator.java +++ b/hapi-fhir-validation/src/main/java/org/hl7/fhir/r4/validation/InstanceValidator.java @@ -2778,6 +2778,7 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat for (QuestionnaireItemComponent qItem : qItems) { List mapItem = map.get(qItem.getLinkId()); if (mapItem != null){ + rule(errors, IssueType.INVALID, element.line(), element.col(), stack.getLiteralPath(), myEnableWhenEvaluator.isQuestionEnabled(qItem, element), "Item has answer, even though it is not enabled "+qItem.getLinkId()); validateQuestionannaireResponseItem(qsrc, qItem, errors, mapItem, stack, inProgress); } else { //item is missing, is the question enabled? diff --git a/hapi-fhir-validation/src/test/java/org/hl7/fhir/dstu3/hapi/validation/QuestionnaireResponseValidatorDstu3Test.java b/hapi-fhir-validation/src/test/java/org/hl7/fhir/dstu3/hapi/validation/QuestionnaireResponseValidatorDstu3Test.java index 56baede0432..65346e03eab 100644 --- a/hapi-fhir-validation/src/test/java/org/hl7/fhir/dstu3/hapi/validation/QuestionnaireResponseValidatorDstu3Test.java +++ b/hapi-fhir-validation/src/test/java/org/hl7/fhir/dstu3/hapi/validation/QuestionnaireResponseValidatorDstu3Test.java @@ -7,6 +7,8 @@ import ca.uhn.fhir.validation.FhirValidator; import ca.uhn.fhir.validation.ResultSeverityEnum; import ca.uhn.fhir.validation.SingleValidationMessage; import ca.uhn.fhir.validation.ValidationResult; + +import org.hamcrest.Matchers; import org.hl7.fhir.dstu3.hapi.ctx.IValidationSupport; import org.hl7.fhir.dstu3.hapi.ctx.IValidationSupport.CodeValidationResult; import org.hl7.fhir.dstu3.model.*; @@ -414,6 +416,24 @@ public class QuestionnaireResponseValidatorDstu3Test { ourLog.info(errors.toString()); assertThat(errors.toString(), containsString("No issues")); } + + @Test + public void testGivenQuestionIsNotEnabledWithEnableWhenAnswersAreReportedAsErrors() throws Exception { + Questionnaire q = new Questionnaire(); + q.addItem().setLinkId("link0").setRequired(false).setType(QuestionnaireItemType.STRING); + q.addItem().setLinkId("link2").setRequired(false).setType(QuestionnaireItemType.STRING).addEnableWhen().setQuestion("link0").setHasAnswer(true); + + QuestionnaireResponse qr = new QuestionnaireResponse(); + qr.setStatus(QuestionnaireResponseStatus.COMPLETED); + qr.getQuestionnaire().setReference("http://example.com/Questionnaire/q1"); + + qr.addItem().setLinkId("link2").addAnswer().setValue(new StringType("FOO")); + + String reference = qr.getQuestionnaire().getReference(); + when(myValSupport.fetchResource(any(FhirContext.class), eq(Questionnaire.class), eq(reference))).thenReturn(q); + ValidationResult errors = myVal.validateWithResult(qr); + assertThat(errors.toString(), Matchers.not(containsString("No issues"))); + } @Test public void testEmbeddedItemInChoice() {