Testing missing required items in QuestionnaireResponses

This commit is contained in:
Heinz-Dieter Conradi 2018-03-07 23:01:07 +01:00
parent 2d55b1f886
commit 41207e5555
2 changed files with 62 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package org.hl7.fhir.dstu3.hapi.validation;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.util.TestUtil;
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.apache.commons.io.IOUtils;
@ -26,6 +27,7 @@ import java.util.List;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
@ -272,6 +274,35 @@ public class QuestionnaireResponseValidatorDstu3Test {
assertThat(errors.toString(), containsString("No issues"));
}
@Test
public void testMissingRequiredAnswer() {
Questionnaire q = new Questionnaire();
q.addItem().setLinkId("link0")
.setType(QuestionnaireItemType.STRING)
.setRequired(true);
String reference = "http://example.com/Questionnaire/q1";
when(myValSupport.fetchResource(any(FhirContext.class), eq(Questionnaire.class), eq(reference)))
.thenReturn(q);
QuestionnaireResponse qa = new QuestionnaireResponse();
qa.getQuestionnaire().setReference(reference);
qa.addItem().setLinkId("link0");
qa.setStatus(QuestionnaireResponseStatus.INPROGRESS);
ValidationResult errors = myVal.validateWithResult(qa);
ourLog.info(errors.toString());
assertThat(errors.getMessages(), hasSize(1));
assertEquals(ResultSeverityEnum.WARNING, errors.getMessages().get(0).getSeverity());
qa.setStatus(QuestionnaireResponseStatus.COMPLETED);
errors = myVal.validateWithResult(qa);
ourLog.info(errors.toString());
assertThat(errors.getMessages(), hasSize(1));
assertEquals(ResultSeverityEnum.ERROR, errors.getMessages().get(0).getSeverity());
}
@Test
public void testOpenchoiceAnswer() {
String questionnaireRef = "http://example.com/Questionnaire/q1";

View File

@ -3,6 +3,7 @@ package org.hl7.fhir.r4.validation;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.util.TestUtil;
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.apache.commons.io.IOUtils;
@ -30,6 +31,7 @@ import java.util.List;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
@ -218,6 +220,35 @@ public class QuestionnaireResponseValidatorR4Test {
assertThat(errors.toString(), containsString("No response found for required item link0"));
}
@Test
public void testMissingRequiredAnswer() {
Questionnaire q = new Questionnaire();
q.addItem().setLinkId("link0")
.setType(QuestionnaireItemType.STRING)
.setRequired(true);
String reference = "http://example.com/Questionnaire/q1";
when(myValSupport.fetchResource(any(FhirContext.class), eq(Questionnaire.class), eq(reference)))
.thenReturn(q);
QuestionnaireResponse qa = new QuestionnaireResponse();
qa.setQuestionnaire(reference);
qa.addItem().setLinkId("link0");
qa.setStatus(QuestionnaireResponseStatus.INPROGRESS);
ValidationResult errors = myVal.validateWithResult(qa);
ourLog.info(errors.toString());
assertThat(errors.getMessages(), hasSize(1));
assertEquals(ResultSeverityEnum.WARNING, errors.getMessages().get(0).getSeverity());
qa.setStatus(QuestionnaireResponseStatus.COMPLETED);
errors = myVal.validateWithResult(qa);
ourLog.info(errors.toString());
assertThat(errors.getMessages(), hasSize(1));
assertEquals(ResultSeverityEnum.ERROR, errors.getMessages().get(0).getSeverity());
}
@Test
public void testOpenchoiceAnswer() {
String questionnaireRef = "http://example.com/Questionnaire/q1";