Added unit tests - Case when hasAnswer is false doesn't work with

InstanceValidator yet.
This commit is contained in:
Eeva Turkka 2018-11-01 15:58:05 +02:00
parent 3d008aee5d
commit 0c0f887ddf
5 changed files with 219 additions and 1 deletions

View File

@ -0,0 +1,60 @@
package org.hl7.fhir.dstu3.validation;
import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemComponent;
import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemEnableWhenComponent;
import java.util.List;
import org.hl7.fhir.dstu3.model.QuestionnaireResponse;
import org.hl7.fhir.dstu3.model.QuestionnaireResponse.QuestionnaireResponseItemComponent;
public class DefaultEnableWhenEvaluator implements IEnableWhenEvaluator {
@Override
public boolean isQuestionEnabled(QuestionnaireItemComponent item, List<QuestionnaireResponseItemComponent> resp) {
boolean enabled = true;
if(item.hasEnableWhen()) {
enabled = false;
for( QuestionnaireItemEnableWhenComponent enable : item.getEnableWhen()) {
if(enable.getHasAnswer()) {
// check if referenced question has answer
String itemId = enable.getQuestion();
for(QuestionnaireResponseItemComponent respItem : resp) {
if(respItem.getLinkId().equalsIgnoreCase(itemId) && respItem.hasAnswer()) {
//TODO check answer value
enabled = true;
}
}
} else {
// and if not
String itemId = enable.getQuestion();
for(QuestionnaireResponseItemComponent respItem : resp) {
if(respItem.getLinkId().equalsIgnoreCase(itemId) && !respItem.hasAnswer()) {
//TODO check answer value
enabled = true;
}
}
}
}
}
return enabled;
}
}

View File

@ -0,0 +1,13 @@
package org.hl7.fhir.dstu3.validation;
import java.util.List;
import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemComponent;
import org.hl7.fhir.dstu3.model.QuestionnaireResponse;
import org.hl7.fhir.dstu3.model.QuestionnaireResponse.QuestionnaireResponseItemComponent;
public interface IEnableWhenEvaluator {
public boolean isQuestionEnabled(QuestionnaireItemComponent item, List<QuestionnaireResponseItemComponent> theResponseItems);
}

View File

@ -2539,6 +2539,7 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
validateQuestionannaireResponseItem(qsrc, qItem, errors, mapItem, stack, inProgress);
else
rule(errors, IssueType.REQUIRED, element.line(), element.col(), stack.getLiteralPath(), !qItem.getRequired(), "No response found for required item "+qItem.getLinkId());
}
}

View File

@ -55,6 +55,18 @@ public class QuestionnaireResponseValidator extends BaseValidator {
*/
private IWorkerContext myWorkerCtx;
private IEnableWhenEvaluator myEnableWhenEvaluator = new DefaultEnableWhenEvaluator();
// this is here not to introduce enabledWhen validation unless wanted
private boolean skipEnabledCheck = true;
public boolean isSkipEnabledCheck() {
return skipEnabledCheck;
}
public void setSkipEnabledCheck(boolean skipEnabledCheck) {
this.skipEnabledCheck = skipEnabledCheck;
}
public QuestionnaireResponseValidator(IWorkerContext theWorkerCtx) {
this.myWorkerCtx = theWorkerCtx;
@ -215,7 +227,7 @@ public class QuestionnaireResponseValidator extends BaseValidator {
List<QuestionnaireResponseItemComponent> responseItems = findResponsesByLinkId(theResponseItems, linkId);
if (responseItems.isEmpty()) {
if (nextQuestionnaireItem.getRequired()) {
if ((skipEnabledCheck || myEnableWhenEvaluator.isQuestionEnabled(nextQuestionnaireItem, theResponseItems)) && nextQuestionnaireItem.getRequired() ) {
if (theValidateRequired) {
rule(theErrors, IssueType.BUSINESSRULE, thePathStack, false, "Missing required {0} with linkId[{1}]", itemType, linkId);
} else {

View File

@ -17,6 +17,7 @@ import org.hl7.fhir.dstu3.model.*;
import org.hl7.fhir.dstu3.model.CodeSystem.CodeSystemContentMode;
import org.hl7.fhir.dstu3.model.CodeSystem.ConceptDefinitionComponent;
import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemComponent;
import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemEnableWhenComponent;
import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemOptionComponent;
import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemType;
import org.hl7.fhir.dstu3.model.QuestionnaireResponse.QuestionnaireResponseItemComponent;
@ -304,6 +305,137 @@ public class QuestionnaireResponseValidatorDstu3Test {
assertThat(errors.toString(), containsString("No response found for required item link0"));
}
@Test
public void testRequiredQuestionWithEnableWhenHasAnswerTrue() {
Questionnaire q = new Questionnaire();
q.addItem().setLinkId("link0").setRequired(true).setType(QuestionnaireItemType.STRING);
// create the questionnaire
QuestionnaireItemComponent item1 = new QuestionnaireItemComponent();
item1.setLinkId("link1").setRequired(true);
q.addItem(item1);
QuestionnaireItemEnableWhenComponent enable = new QuestionnaireItemEnableWhenComponent();
item1.addEnableWhen(enable);
enable.setQuestion("link0");
enable.setHasAnswer(true);
//q.getItemFirstRep().addEnableWhen().
//q.addItem().setLinkId("link1").setRequired(true).setType(QuestionnaireItemType.STRING);
QuestionnaireResponse qa = new QuestionnaireResponse();
qa.setStatus(QuestionnaireResponseStatus.COMPLETED);
qa.getQuestionnaire().setReference("http://example.com/Questionnaire/q1");
qa.addItem().setLinkId("link0").addAnswer().setValue(new StringType("FOO"));
String reference = qa.getQuestionnaire().getReference();
when(myValSupport.fetchResource(any(FhirContext.class), eq(Questionnaire.class), eq(reference))).thenReturn(q);
ValidationResult errors = myVal.validateWithResult(qa);
ourLog.info(errors.toString());
assertThat(errors.toString(), containsString("No response found for required item link1"));
}
@Test
public void testRequiredQuestionWithEnableWhenHidesQuestion() {
Questionnaire q = new Questionnaire();
q.addItem().setLinkId("link0").setRequired(false).setType(QuestionnaireItemType.STRING);
// create the questionnaire
QuestionnaireItemComponent item1 = new QuestionnaireItemComponent();
item1.setLinkId("link1").setRequired(true);
q.addItem(item1);
QuestionnaireItemEnableWhenComponent enable = new QuestionnaireItemEnableWhenComponent();
item1.addEnableWhen(enable);
enable.setQuestion("link0");
enable.setHasAnswer(true);
//q.getItemFirstRep().addEnableWhen().
//q.addItem().setLinkId("link1").setRequired(true).setType(QuestionnaireItemType.STRING);
QuestionnaireResponse qa = new QuestionnaireResponse();
qa.setStatus(QuestionnaireResponseStatus.COMPLETED);
qa.getQuestionnaire().setReference("http://example.com/Questionnaire/q1");
//qa.addItem().setLinkId("link0").addAnswer().setValue(new StringType("FOO"));
String reference = qa.getQuestionnaire().getReference();
when(myValSupport.fetchResource(any(FhirContext.class), eq(Questionnaire.class), eq(reference))).thenReturn(q);
ValidationResult errors = myVal.validateWithResult(qa);
ourLog.info(errors.toString());
assertThat(errors.toString(), containsString("No response found for required item link1"));
}
@Test
public void testRequiredQuestionWithEnableWhenHasAnswerTrueWithAnswer() {
Questionnaire q = new Questionnaire();
q.addItem().setLinkId("link0").setRequired(true).setType(QuestionnaireItemType.STRING);
// create the questionnaire
QuestionnaireItemComponent item1 = new QuestionnaireItemComponent();
item1.setLinkId("link1").setRequired(true).setType(QuestionnaireItemType.STRING);
q.addItem(item1);
QuestionnaireItemEnableWhenComponent enable = new QuestionnaireItemEnableWhenComponent();
item1.addEnableWhen(enable);
enable.setQuestion("link0");
enable.setHasAnswer(true);
//q.getItemFirstRep().addEnableWhen().
//q.addItem().setLinkId("link1").setRequired(true).setType(QuestionnaireItemType.STRING);
QuestionnaireResponse qa = new QuestionnaireResponse();
qa.setStatus(QuestionnaireResponseStatus.COMPLETED);
qa.getQuestionnaire().setReference("http://example.com/Questionnaire/q1");
qa.addItem().setLinkId("link0").addAnswer().setValue(new StringType("FOO"));
qa.addItem().setLinkId("link1").addAnswer().setValue(new StringType("BAR"));
String reference = qa.getQuestionnaire().getReference();
when(myValSupport.fetchResource(any(FhirContext.class), eq(Questionnaire.class), eq(reference))).thenReturn(q);
ValidationResult errors = myVal.validateWithResult(qa);
ourLog.info(errors.toString());
assertThat(errors.toString(), containsString("No issues"));
}
@Test
public void testRequiredQuestionWithEnableWhenHasAnswerFalse() {
Questionnaire q = new Questionnaire();
q.addItem().setLinkId("link0").setRequired(true).setType(QuestionnaireItemType.STRING);
// create the questionnaire
QuestionnaireItemComponent item1 = new QuestionnaireItemComponent();
item1.setLinkId("link1").setRequired(true);
q.addItem(item1);
QuestionnaireItemEnableWhenComponent enable = new QuestionnaireItemEnableWhenComponent();
item1.addEnableWhen(enable);
enable.setQuestion("link0");
enable.setHasAnswer(false);
//q.getItemFirstRep().addEnableWhen().
//q.addItem().setLinkId("link1").setRequired(true).setType(QuestionnaireItemType.STRING);
QuestionnaireResponse qa = new QuestionnaireResponse();
qa.setStatus(QuestionnaireResponseStatus.COMPLETED);
qa.getQuestionnaire().setReference("http://example.com/Questionnaire/q1");
qa.addItem().setLinkId("link0").addAnswer().setValue(new StringType("FOO"));
String reference = qa.getQuestionnaire().getReference();
when(myValSupport.fetchResource(any(FhirContext.class), eq(Questionnaire.class), eq(reference))).thenReturn(q);
ValidationResult errors = myVal.validateWithResult(qa);
ourLog.info(errors.toString());
// FIXME: should be no assert, fix the assert
assertThat(errors.toString(), containsString("No response found for required item link2"));
}
@Test
public void testEmbeddedItemInChoice() {
String questionnaireRef = "http://example.com/Questionnaire/q1";