Test questionnaire with embedded items for DSTU3.

The test for an item embedded in a text item works as expected.
However, the embedded item in a choice or openchoice case
triggers an NPE.
This commit is contained in:
Heinz-Dieter Conradi 2018-03-12 11:08:23 +01:00 committed by Heinz-Dieter Conradi
parent f766156ca3
commit eeb4fe62b7
1 changed files with 151 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package org.hl7.fhir.dstu3.hapi.validation; package org.hl7.fhir.dstu3.hapi.validation;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.TestUtil;
import ca.uhn.fhir.validation.FhirValidator; import ca.uhn.fhir.validation.FhirValidator;
import ca.uhn.fhir.validation.SingleValidationMessage; import ca.uhn.fhir.validation.SingleValidationMessage;
@ -216,6 +217,156 @@ public class QuestionnaireResponseValidatorDstu3Test {
assertThat(errors.toString(), containsString("No response found for required item link0")); assertThat(errors.toString(), containsString("No response found for required item link0"));
} }
@Test
public void testEmbeddedItemInChoice() {
String questionnaireRef = "http://example.com/Questionnaire/q1";
String valueSetRef = "http://somevalueset";
String codeSystemUrl = "http://codesystems.com/system";
String codeValue = "code0";
// create the questionnaire
QuestionnaireItemComponent item1 = new QuestionnaireItemComponent();
item1.setLinkId("link1")
.setType(QuestionnaireItemType.CHOICE)
.setOptions(new Reference(valueSetRef));
item1.addItem().setLinkId("link11")
.setType(QuestionnaireItemType.TEXT);
Questionnaire q = new Questionnaire();
q.addItem(item1);
when(myValSupport.fetchResource(any(FhirContext.class), eq(Questionnaire.class), eq(questionnaireRef)))
.thenReturn(q);
CodeSystem codeSystem = new CodeSystem();
codeSystem.setContent(CodeSystemContentMode.COMPLETE);
codeSystem.setUrl(codeSystemUrl);
codeSystem.addConcept().setCode(codeValue);
when(myValSupport.fetchCodeSystem(any(FhirContext.class), eq(codeSystemUrl)))
.thenReturn(codeSystem);
ValueSet options = new ValueSet();
options.getCompose().addInclude().setSystem(codeSystemUrl).addConcept().setCode(codeValue);
when(myValSupport.fetchResource(any(FhirContext.class), eq(ValueSet.class), eq(valueSetRef)))
.thenReturn(options);
when(myValSupport.validateCode(any(FhirContext.class), eq(codeSystemUrl), eq(codeValue), any(String.class)))
.thenReturn(new CodeValidationResult(new ConceptDefinitionComponent(new CodeType(codeValue))));
IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true);
String qXml = xmlParser.encodeResourceToString(q);
ourLog.info(qXml);
// create the response
QuestionnaireResponse qa = new QuestionnaireResponse();
qa.setStatus(QuestionnaireResponseStatus.INPROGRESS);
qa.getQuestionnaire().setReference(questionnaireRef);
qa.addItem().setLinkId("link1")
.addAnswer()
.addItem().setLinkId("link11");
String rXml = xmlParser.encodeResourceToString(qa);
ourLog.info(rXml);
ValidationResult errors = myVal.validateWithResult(qa);
ourLog.info(errors.toString());
assertThat(errors.getMessages(), empty());
}
@Test
public void testEmbeddedItemInOpenChoice() {
String questionnaireRef = "http://example.com/Questionnaire/q1";
String valueSetRef = "http://somevalueset";
String codeSystemUrl = "http://codesystems.com/system";
String codeValue = "code0";
// create the questionnaire
QuestionnaireItemComponent item1 = new QuestionnaireItemComponent();
item1.setLinkId("link1")
.setType(QuestionnaireItemType.OPENCHOICE)
.setOptions(new Reference(valueSetRef));
item1.addItem().setLinkId("link11")
.setType(QuestionnaireItemType.TEXT);
Questionnaire q = new Questionnaire();
q.addItem(item1);
when(myValSupport.fetchResource(any(FhirContext.class), eq(Questionnaire.class), eq(questionnaireRef)))
.thenReturn(q);
CodeSystem codeSystem = new CodeSystem();
codeSystem.setContent(CodeSystemContentMode.COMPLETE);
codeSystem.setUrl(codeSystemUrl);
codeSystem.addConcept().setCode(codeValue);
when(myValSupport.fetchCodeSystem(any(FhirContext.class), eq(codeSystemUrl)))
.thenReturn(codeSystem);
ValueSet options = new ValueSet();
options.getCompose().addInclude().setSystem(codeSystemUrl).addConcept().setCode(codeValue);
when(myValSupport.fetchResource(any(FhirContext.class), eq(ValueSet.class), eq(valueSetRef)))
.thenReturn(options);
when(myValSupport.validateCode(any(FhirContext.class), eq(codeSystemUrl), eq(codeValue), any(String.class)))
.thenReturn(new CodeValidationResult(new ConceptDefinitionComponent(new CodeType(codeValue))));
IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true);
String qXml = xmlParser.encodeResourceToString(q);
ourLog.info(qXml);
// create the response
QuestionnaireResponse qa = new QuestionnaireResponse();
qa.setStatus(QuestionnaireResponseStatus.INPROGRESS);
qa.getQuestionnaire().setReference(questionnaireRef);
qa.addItem().setLinkId("link1")
.addAnswer()
.addItem().setLinkId("link11");
String rXml = xmlParser.encodeResourceToString(qa);
ourLog.info(rXml);
ValidationResult errors = myVal.validateWithResult(qa);
ourLog.info(errors.toString());
assertThat(errors.getMessages(), empty());
}
@Test
public void testEmbeddedItemInString() {
String questionnaireRef = "http://example.com/Questionnaire/q1";
// create the questionnaire
QuestionnaireItemComponent item1 = new QuestionnaireItemComponent();
item1.setLinkId("link1")
.setType(QuestionnaireItemType.TEXT);
item1.addItem().setLinkId("link11")
.setType(QuestionnaireItemType.TEXT);
Questionnaire q = new Questionnaire();
q.addItem(item1);
when(myValSupport.fetchResource(any(FhirContext.class), eq(Questionnaire.class), eq(questionnaireRef)))
.thenReturn(q);
IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true);
String qXml = xmlParser.encodeResourceToString(q);
ourLog.info(qXml);
// create the response
QuestionnaireResponse qa = new QuestionnaireResponse();
qa.setStatus(QuestionnaireResponseStatus.INPROGRESS);
qa.getQuestionnaire().setReference(questionnaireRef);
qa.addItem().setLinkId("link1")
.addAnswer()
.addItem().setLinkId("link11");
String rXml = xmlParser.encodeResourceToString(qa);
ourLog.info(rXml);
ValidationResult errors = myVal.validateWithResult(qa);
ourLog.info(errors.toString());
assertThat(errors.getMessages(), empty());
}
@Test @Test
public void testValidateQuestionnaireResponseWithValueSetChoiceAnswer() { public void testValidateQuestionnaireResponseWithValueSetChoiceAnswer() {
/* /*