Merge pull request #892 from hdconradi/validate-questionnaire-response-item-type-for-text

Allow text items in questionnaire
This commit is contained in:
James Agnew 2018-05-24 06:03:18 -04:00 committed by GitHub
commit 1ead3f715e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 166 additions and 1 deletions

View File

@ -2582,7 +2582,7 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
validateQuestionnaireResponseItemType(errors, answer, ns, "Reference");
break;
case QUANTITY:
if (validateQuestionnaireResponseItemType(errors, answer, ns, "Quantity").equals("Quantity"))
if ("Quantity".equals(validateQuestionnaireResponseItemType(errors, answer, ns, "Quantity")))
if (qItem.hasExtension("???"))
validateQuestionnaireResponseItemQuantity(errors, answer, ns);
break;
@ -2692,6 +2692,11 @@ public class InstanceValidator extends BaseValidator implements IResourceValidat
private String validateQuestionnaireResponseItemType(List<ValidationMessage> errors, Element element, NodeStack stack, String... types) {
List<Element> values = new ArrayList<Element>();
element.getNamedChildrenWithWildcard("value[x]", values);
for (int i = 0; i < types.length; i++) {
if (types[i].equals("text")) {
types[i] = "string";
}
}
if (values.size() > 0) {
NodeStack ns = stack.push(values.get(0), -1, null, null);
CommaSeparatedStringBuilder l = new CommaSeparatedStringBuilder();

View File

@ -16,6 +16,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.QuestionnaireItemOptionComponent;
import org.hl7.fhir.dstu3.model.Questionnaire.QuestionnaireItemType;
import org.hl7.fhir.dstu3.model.QuestionnaireResponse.QuestionnaireResponseItemComponent;
import org.hl7.fhir.dstu3.model.QuestionnaireResponse.QuestionnaireResponseStatus;
@ -24,6 +25,8 @@ import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static org.hamcrest.Matchers.containsString;
@ -77,6 +80,83 @@ public class QuestionnaireResponseValidatorDstu3Test {
return new ValidationResult(ourCtx, messages);
}
@Test
public void testAnswerWithCorrectType() {
CodeSystem codeSystem = new CodeSystem();
codeSystem.setContent(CodeSystemContentMode.COMPLETE);
codeSystem.setUrl("http://codesystems.com/system");
codeSystem.addConcept().setCode("code0");
when(myValSupport.fetchCodeSystem(any(FhirContext.class), eq("http://codesystems.com/system"))).thenReturn(codeSystem);
ValueSet options = new ValueSet();
options.getCompose().addInclude().setSystem("http://codesystems.com/system").addConcept().setCode("code0");
when(myValSupport.fetchResource(any(FhirContext.class), eq(ValueSet.class), eq("http://somevalueset"))).thenReturn(options);
int itemCnt = 16;
QuestionnaireItemType[] questionnaireItemTypes = new QuestionnaireItemType[itemCnt];
questionnaireItemTypes[0] = QuestionnaireItemType.BOOLEAN;
questionnaireItemTypes[1] = QuestionnaireItemType.DECIMAL;
questionnaireItemTypes[2] = QuestionnaireItemType.INTEGER;
questionnaireItemTypes[3] = QuestionnaireItemType.DATE;
questionnaireItemTypes[4] = QuestionnaireItemType.DATETIME;
questionnaireItemTypes[5] = QuestionnaireItemType.TIME;
questionnaireItemTypes[6] = QuestionnaireItemType.STRING;
questionnaireItemTypes[7] = QuestionnaireItemType.TEXT;
questionnaireItemTypes[8] = QuestionnaireItemType.TEXT;
questionnaireItemTypes[9] = QuestionnaireItemType.URL;
questionnaireItemTypes[10] = QuestionnaireItemType.CHOICE;
questionnaireItemTypes[11] = QuestionnaireItemType.OPENCHOICE;
questionnaireItemTypes[12] = QuestionnaireItemType.OPENCHOICE;
questionnaireItemTypes[13] = QuestionnaireItemType.ATTACHMENT;
questionnaireItemTypes[14] = QuestionnaireItemType.REFERENCE;
questionnaireItemTypes[15] = QuestionnaireItemType.QUANTITY;
Type[] answerValues = new Type[itemCnt];
answerValues[0] = new BooleanType(true);
answerValues[1] = new DecimalType(42.0);
answerValues[2] = new IntegerType(42);
answerValues[3] = new DateType(new Date());
answerValues[4] = new DateTimeType(new Date());
answerValues[5] = new TimeType("04:47:12");
answerValues[6] = new StringType("some text");
answerValues[7] = new StringType("some text");
answerValues[8] = new MarkdownType("some text");
answerValues[9] = new UriType("http://example.com");
answerValues[10] = new Coding().setSystem("http://codesystems.com/system").setCode("code0");
answerValues[11] = new Coding().setSystem("http://codesystems.com/system").setCode("code0");
answerValues[12] = new StringType("some value");
answerValues[13] = new Attachment().setData("some data".getBytes()).setContentType("txt");
answerValues[14] = new Reference("http://example.com/Questionnaire/q1");
answerValues[15] = new Quantity(42);
for (int i = 0; i < itemCnt; i++) {
if (questionnaireItemTypes[i] == null) continue;
String linkId = "link" + i;
Questionnaire q = new Questionnaire();
QuestionnaireItemComponent questionnaireItemComponent =
q.addItem().setLinkId(linkId).setRequired(true).setType(questionnaireItemTypes[i]);
if (i == 10 || i == 11) {
questionnaireItemComponent.setOptions(new Reference("http://somevalueset"));
} else if (i == 12) {
questionnaireItemComponent.setOption(
Arrays.asList(new QuestionnaireItemOptionComponent(new StringType("some value"))));
}
QuestionnaireResponse qa = new QuestionnaireResponse();
qa.setStatus(QuestionnaireResponseStatus.INPROGRESS);
qa.getQuestionnaire().setReference("http://example.com/Questionnaire/q1");
qa.addItem().setLinkId(linkId).addAnswer().setValue(answerValues[i]);
when(myValSupport.fetchResource(any(FhirContext.class), eq(Questionnaire.class),
eq(qa.getQuestionnaire().getReference()))).thenReturn(q);
ValidationResult errors = myVal.validateWithResult(qa);
ourLog.info(errors.toString());
assertThat("index[" + i + "]: " + errors.toString(), errors.getMessages(), empty());
}
}
@Test
public void testAnswerWithWrongType() {
Questionnaire q = new Questionnaire();

View File

@ -19,6 +19,7 @@ import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.r4.model.CodeSystem.CodeSystemContentMode;
import org.hl7.fhir.r4.model.CodeSystem.ConceptDefinitionComponent;
import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent;
import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemOptionComponent;
import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemType;
import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemComponent;
import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseStatus;
@ -28,6 +29,8 @@ import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static org.hamcrest.Matchers.containsString;
@ -81,6 +84,83 @@ public class QuestionnaireResponseValidatorR4Test {
return new ValidationResult(ourCtx, messages);
}
@Test
public void testAnswerWithCorrectType() {
CodeSystem codeSystem = new CodeSystem();
codeSystem.setContent(CodeSystemContentMode.COMPLETE);
codeSystem.setUrl("http://codesystems.com/system");
codeSystem.addConcept().setCode("code0");
when(myValSupport.fetchCodeSystem(any(FhirContext.class), eq("http://codesystems.com/system"))).thenReturn(codeSystem);
ValueSet options = new ValueSet();
options.getCompose().addInclude().setSystem("http://codesystems.com/system").addConcept().setCode("code0");
when(myValSupport.fetchResource(any(FhirContext.class), eq(ValueSet.class), eq("http://somevalueset"))).thenReturn(options);
int itemCnt = 16;
QuestionnaireItemType[] questionnaireItemTypes = new QuestionnaireItemType[itemCnt];
questionnaireItemTypes[0] = QuestionnaireItemType.BOOLEAN;
questionnaireItemTypes[1] = QuestionnaireItemType.DECIMAL;
questionnaireItemTypes[2] = QuestionnaireItemType.INTEGER;
questionnaireItemTypes[3] = QuestionnaireItemType.DATE;
questionnaireItemTypes[4] = QuestionnaireItemType.DATETIME;
questionnaireItemTypes[5] = QuestionnaireItemType.TIME;
questionnaireItemTypes[6] = QuestionnaireItemType.STRING;
questionnaireItemTypes[7] = QuestionnaireItemType.TEXT;
questionnaireItemTypes[8] = QuestionnaireItemType.TEXT;
questionnaireItemTypes[9] = QuestionnaireItemType.URL;
questionnaireItemTypes[10] = QuestionnaireItemType.CHOICE;
questionnaireItemTypes[11] = QuestionnaireItemType.OPENCHOICE;
questionnaireItemTypes[12] = QuestionnaireItemType.OPENCHOICE;
questionnaireItemTypes[13] = QuestionnaireItemType.ATTACHMENT;
questionnaireItemTypes[14] = QuestionnaireItemType.REFERENCE;
questionnaireItemTypes[15] = QuestionnaireItemType.QUANTITY;
Type[] answerValues = new Type[itemCnt];
answerValues[0] = new BooleanType(true);
answerValues[1] = new DecimalType(42.0);
answerValues[2] = new IntegerType(42);
answerValues[3] = new DateType(new Date());
answerValues[4] = new DateTimeType(new Date());
answerValues[5] = new TimeType("04:47:12");
answerValues[6] = new StringType("some text");
answerValues[7] = new StringType("some text");
answerValues[8] = new MarkdownType("some text");
answerValues[9] = new UriType("http://example.com");
answerValues[10] = new Coding().setSystem("http://codesystems.com/system").setCode("code0");
answerValues[11] = new Coding().setSystem("http://codesystems.com/system").setCode("code0");
answerValues[12] = new StringType("some value");
answerValues[13] = new Attachment().setData("some data".getBytes()).setContentType("txt");
answerValues[14] = new Reference("http://example.com/Questionnaire/q1");
answerValues[15] = new Quantity(42);
for (int i = 0; i < itemCnt; i++) {
if (questionnaireItemTypes[i] == null) continue;
String linkId = "link" + i;
Questionnaire q = new Questionnaire();
QuestionnaireItemComponent questionnaireItemComponent =
q.addItem().setLinkId(linkId).setRequired(true).setType(questionnaireItemTypes[i]);
if (i == 10 || i == 11) {
questionnaireItemComponent.setOptions("http://somevalueset");
} else if (i == 12) {
questionnaireItemComponent.setOption(
Arrays.asList(new QuestionnaireItemOptionComponent(new StringType("some value"))));
}
QuestionnaireResponse qa = new QuestionnaireResponse();
qa.setStatus(QuestionnaireResponseStatus.INPROGRESS);
qa.setQuestionnaire("http://example.com/Questionnaire/q1");
qa.addItem().setLinkId(linkId).addAnswer().setValue(answerValues[i]);
when(myValSupport.fetchResource(any(FhirContext.class), eq(Questionnaire.class),
eq(qa.getQuestionnaire()))).thenReturn(q);
ValidationResult errors = myVal.validateWithResult(qa);
ourLog.info(errors.toString());
assertThat("index[" + i + "]: " + errors.toString(), errors.getMessages(), empty());
}
}
@Test
public void testAnswerWithWrongType() {
Questionnaire q = new Questionnaire();