Add test for #205

This commit is contained in:
James Agnew 2015-08-06 11:14:52 -04:00
parent af9c8faf89
commit 76c076a2d1
4 changed files with 146 additions and 118 deletions

View File

@ -81,13 +81,13 @@ public class JsonParserTest {
private void parseAndEncode(String name) throws IOException {
String msg = IOUtils.toString(XmlParser.class.getResourceAsStream(name));
// ourLog.info(msg);
// ourLog.info(msg);
IParser p = ourCtx.newJsonParser();
Profile res = p.parseResource(Profile.class, msg);
String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(res);
// ourLog.info(encoded);
// ourLog.info(encoded);
JSON expected = JSONSerializer.toJSON(msg.trim());
JSON actual = JSONSerializer.toJSON(encoded.trim());
@ -158,36 +158,12 @@ public class JsonParserTest {
String enc = ourCtx.newJsonParser().encodeResourceToString(patient);
assertThat(enc, org.hamcrest.Matchers.stringContainsInOrder("{\"resourceType\":\"Patient\",",
"\"extension\":[{\"url\":\"http://example.com/extensions#someext\",\"valueDateTime\":\"2011-01-02T11:13:15\"}",
"{\"url\":\"http://example.com#parent\",\"extension\":[{\"url\":\"http://example.com#child\",\"valueString\":\"value1\"},{\"url\":\"http://example.com#child\",\"valueString\":\"value2\"}]}"
));
assertThat(enc, org.hamcrest.Matchers.stringContainsInOrder("\"modifierExtension\":[" +
"{" +
"\"url\":\"http://example.com/extensions#modext\"," +
"\"valueDate\":\"1995-01-02\"" +
"}" +
"],"));
assertThat(enc, containsString("\"_given\":[" +
"{" +
"\"extension\":[" +
"{" +
"\"url\":\"http://examples.com#givenext\"," +
"\"valueString\":\"given\"" +
"}" +
"]" +
"}," +
"{" +
"\"extension\":[" +
"{" +
"\"url\":\"http://examples.com#givenext_parent\"," +
"\"extension\":[" +
"{" +
"\"url\":\"http://examples.com#givenext_child\"," +
"\"valueString\":\"CHILD\"" +
"}" +
"]" +
"}" +
"]" +
"}"));
"{\"url\":\"http://example.com#parent\",\"extension\":[{\"url\":\"http://example.com#child\",\"valueString\":\"value1\"},{\"url\":\"http://example.com#child\",\"valueString\":\"value2\"}]}"));
assertThat(enc, org.hamcrest.Matchers.stringContainsInOrder("\"modifierExtension\":[" + "{" + "\"url\":\"http://example.com/extensions#modext\"," + "\"valueDate\":\"1995-01-02\"" + "}" + "],"));
assertThat(enc,
containsString("\"_given\":[" + "{" + "\"extension\":[" + "{" + "\"url\":\"http://examples.com#givenext\"," + "\"valueString\":\"given\"" + "}" + "]" + "}," + "{" + "\"extension\":[" + "{"
+ "\"url\":\"http://examples.com#givenext_parent\"," + "\"extension\":[" + "{" + "\"url\":\"http://examples.com#givenext_child\"," + "\"valueString\":\"CHILD\"" + "}" + "]" + "}"
+ "]" + "}"));
/*
* Now parse this back
@ -227,8 +203,6 @@ public class JsonParserTest {
}
@Test
public void testEncodeBinaryResource() {
@ -241,8 +215,6 @@ public class JsonParserTest {
}
@Test
public void testEncodeBundle() throws InterruptedException {
Bundle b = new Bundle();
@ -285,7 +257,6 @@ public class JsonParserTest {
}
@Test
public void testEncodeBundleCategory() {
@ -311,7 +282,6 @@ public class JsonParserTest {
}
@Test
public void testEncodeBundleEntryCategory() {
@ -382,7 +352,7 @@ public class JsonParserTest {
// And re-encode once more, with the references cleared
patient.getContained().getContainedResources().clear();
patient.getManagingOrganization().setReference((IdDt)null);
patient.getManagingOrganization().setReference((IdDt) null);
encoded = xmlParser.encodeResourceToString(patient);
ourLog.info(encoded);
assertThat(encoded, stringContainsInOrder(Arrays.asList("\"contained\":[", "\"id\":\"1\"", "\"identifier\"", "\"reference\":\"#1\"")));
@ -390,7 +360,7 @@ public class JsonParserTest {
// And re-encode once more, with the references cleared and a manually set local ID
patient.getContained().getContainedResources().clear();
patient.getManagingOrganization().setReference((IdDt)null);
patient.getManagingOrganization().setReference((IdDt) null);
patient.getManagingOrganization().getResource().setId(new IdDt("#333"));
encoded = xmlParser.encodeResourceToString(patient);
ourLog.info(encoded);
@ -440,7 +410,6 @@ public class JsonParserTest {
}
@Test
public void testEncodeContainedResourcesMore() {
@ -542,8 +511,7 @@ public class JsonParserTest {
ourLog.info(encoded);
assertThat(encoded, not(containsString("123456")));
assertEquals(
"{\"resourceType\":\"ValueSet\",\"define\":{\"concept\":[{\"extension\":[{\"url\":\"urn:alt\",\"valueString\":\"alt name\"}],\"code\":\"someCode\",\"display\":\"someDisplay\"}]}}",
assertEquals("{\"resourceType\":\"ValueSet\",\"define\":{\"concept\":[{\"extension\":[{\"url\":\"urn:alt\",\"valueString\":\"alt name\"}],\"code\":\"someCode\",\"display\":\"someDisplay\"}]}}",
encoded);
}
@ -642,7 +610,7 @@ public class JsonParserTest {
@Test
public void testEncodeIds() {
Patient pt =new Patient();
Patient pt = new Patient();
pt.addIdentifier("sys", "val");
ListResource list = new ListResource();
@ -654,14 +622,13 @@ public class JsonParserTest {
assertThat(enc, containsString("\"id\":\"1\""));
ListResource parsed = ourCtx.newJsonParser().parseResource(ListResource.class,enc);
ListResource parsed = ourCtx.newJsonParser().parseResource(ListResource.class, enc);
assertEquals(Patient.class, parsed.getEntryFirstRep().getItem().getResource().getClass());
enc = enc.replace("\"id\"", "\"_id\"");
parsed = ourCtx.newJsonParser().parseResource(ListResource.class,enc);
parsed = ourCtx.newJsonParser().parseResource(ListResource.class, enc);
assertEquals(Patient.class, parsed.getEntryFirstRep().getItem().getResource().getClass());
}
}
@Test
public void testEncodeInvalidChildGoodException() {
@ -711,7 +678,7 @@ public class JsonParserTest {
patient.addIdentifier("urn:mrns", "253345");
patient.getManagingOrganization().setResource(org);
Bundle b = Bundle.withResources(Collections.singletonList((IResource)patient), ourCtx, "http://foo");
Bundle b = Bundle.withResources(Collections.singletonList((IResource) patient), ourCtx, "http://foo");
String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeBundleToString(b);
ourLog.info(encoded);
assertThat(encoded, not(containsString("contained")));
@ -743,9 +710,6 @@ public class JsonParserTest {
assertThat(enc, containsString("\"http://example.com/base/Patient/1\""));
}
@Test
public void testEncodeQuery() {
Query q = new Query();
@ -955,7 +919,8 @@ public class JsonParserTest {
@Override
public void setFhirContext(FhirContext theFhirContext) {
// nothing
}};
}
};
FhirContext context = ourCtx;
context.setNarrativeGenerator(gen);
@ -1034,8 +999,8 @@ public class JsonParserTest {
String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeBundleToString(bundle);
ourLog.info(encoded);
assertEquals("http://fhir.healthintersections.com.au/open/DiagnosticReport/_search?_format=application/json+fhir&search-id=46d5f0e7-9240-4d4f-9f51-f8ac975c65&search-sort=_id", bundle
.getLinkSelf().getValue());
assertEquals("http://fhir.healthintersections.com.au/open/DiagnosticReport/_search?_format=application/json+fhir&search-id=46d5f0e7-9240-4d4f-9f51-f8ac975c65&search-sort=_id",
bundle.getLinkSelf().getValue());
assertEquals("urn:uuid:0b754ff9-03cf-4322-a119-15019af8a3", bundle.getBundleId().getValue());
BundleEntry entry = bundle.getEntries().get(0);

View File

@ -199,24 +199,6 @@ public class JsonParserDstu2Test {
assertEquals(new Tag("scheme2", "term2", "label2"), tagList.get(1));
}
/**
* #158
*/
@Test
public void testEncodeEmptyTag2() {
TagList tagList = new TagList();
tagList.addTag("scheme", "code", null);
tagList.addTag(null, null, "Label");
Patient p = new Patient();
ResourceMetadataKeyEnum.TAG_LIST.put(p, tagList);
String encoded = ourCtx.newJsonParser().encodeResourceToString(p);
assertThat(encoded, containsString("tag"));
assertThat(encoded, containsString("scheme"));
assertThat(encoded, not(containsString("Label")));
}
@Test
public void testEncodeAndParseSecurityLabels() {
Patient p = new Patient();
@ -282,7 +264,6 @@ public class JsonParserDstu2Test {
assertEquals("VERSION2", label.getVersion());
}
@Test
public void testEncodeBundleNewBundleNoText() {
@ -304,7 +285,6 @@ public class JsonParserDstu2Test {
}
@Test
public void testEncodeBundleOldBundleNoText() {
@ -324,6 +304,7 @@ public class JsonParserDstu2Test {
}
/**
* Fixing #89
*/
@ -343,6 +324,7 @@ public class JsonParserDstu2Test {
//@formatter:on
}
/**
* #158
*/
@ -359,6 +341,43 @@ public class JsonParserDstu2Test {
assertThat(encoded, not(containsString("tag")));
}
/**
* #158
*/
@Test
public void testEncodeEmptyTag2() {
TagList tagList = new TagList();
tagList.addTag("scheme", "code", null);
tagList.addTag(null, null, "Label");
Patient p = new Patient();
ResourceMetadataKeyEnum.TAG_LIST.put(p, tagList);
String encoded = ourCtx.newJsonParser().encodeResourceToString(p);
assertThat(encoded, containsString("tag"));
assertThat(encoded, containsString("scheme"));
assertThat(encoded, not(containsString("Label")));
}
/**
* See #205
*/
@Test
public void testEncodeTags() {
Patient pt = new Patient();
pt.addIdentifier().setSystem("sys").setValue("val");
TagList tagList = new TagList();
tagList.addTag("scheme", "term", "display");
ResourceMetadataKeyEnum.TAG_LIST.put(pt, tagList);
String enc = ourCtx.newJsonParser().encodeResourceToString(pt);
ourLog.info(enc);
assertEquals("{\"resourceType\":\"Patient\",\"meta\":{\"tag\":[{\"system\":\"scheme\",\"code\":\"term\",\"display\":\"display\"}]},\"identifier\":[{\"system\":\"sys\",\"value\":\"val\"}]}", enc);
}
@Test
public void testEncodingNullExtension() {
Patient p = new Patient();

View File

@ -6,6 +6,7 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
@ -15,11 +16,9 @@ import org.apache.commons.lang3.Validate;
import org.hl7.fhir.instance.model.Attachment;
import org.hl7.fhir.instance.model.BooleanType;
import org.hl7.fhir.instance.model.Coding;
import org.hl7.fhir.instance.model.DataElement;
import org.hl7.fhir.instance.model.DateTimeType;
import org.hl7.fhir.instance.model.DateType;
import org.hl7.fhir.instance.model.DecimalType;
import org.hl7.fhir.instance.model.ElementDefinition;
import org.hl7.fhir.instance.model.Extension;
import org.hl7.fhir.instance.model.InstantType;
import org.hl7.fhir.instance.model.IntegerType;
@ -52,14 +51,17 @@ import org.hl7.fhir.instance.utils.WorkerContext;
*/
public class QuestionnaireAnswersValidator extends BaseValidator {
//@formatter:off
/*
* ***************************************************************** Note to anyone working on this class -
* *****************************************************************
* Note to anyone working on this class -
*
* This class has unit tests which run within the HAPI project build. Please sync any changes here to HAPI and ensure that unit tests are run.
* This class has unit tests which run within the HAPI project build.
* Please sync any changes here to HAPI and ensure that unit tests are run.
* ****************************************************************
*/
//@formatter:on
private static final List<String> EMPTY_PATH = Collections.emptyList();
private WorkerContext myWorkerCtx;
public QuestionnaireAnswersValidator(WorkerContext theWorkerCtx) {
@ -126,6 +128,22 @@ public class QuestionnaireAnswersValidator extends BaseValidator {
pathStack.removeLast();
pathStack.add("group[0]");
validateGroup(theErrors, questionnaire.getGroup(), theAnswers.getGroup(), pathStack, theAnswers, validateRequired);
/*
* If we found any fatal errors, any other errors will be removed since the fatal error means
* the parsing was invalid
*/
for (ValidationMessage next : theErrors) {
if (next.getLevel() == IssueSeverity.FATAL) {
for (Iterator<ValidationMessage> iter = theErrors.iterator(); iter.hasNext(); ) {
if (iter.next().getLevel() != IssueSeverity.FATAL) {
iter.remove();
}
}
break;
}
}
}
private Questionnaire getQuestionnaire(QuestionnaireAnswers theAnswers, Reference theQuestionnaireRef) {
@ -273,7 +291,11 @@ public class QuestionnaireAnswersValidator extends BaseValidator {
for (GroupComponent nextQuestionGroup : theQuestionGroups) {
String nextLinkId = StringUtils.defaultString(nextQuestionGroup.getLinkId());
if (!linkIds.add(nextLinkId)) {
rule(theErrors, IssueType.BUSINESSRULE, thePathStack, false, "Questionnaire in invalid, unable to validate QuestionnaireAnswers: Multiple groups found at this position with linkId[{0}]", nextLinkId);
if (isBlank(nextLinkId)) {
fail(theErrors, IssueType.BUSINESSRULE, thePathStack, false, "Questionnaire in invalid, unable to validate QuestionnaireAnswers: Multiple groups found at this position with blank/missing linkId", nextLinkId);
} else {
fail(theErrors, IssueType.BUSINESSRULE, thePathStack, false, "Questionnaire in invalid, unable to validate QuestionnaireAnswers: Multiple groups found at this position with linkId[{0}]", nextLinkId);
}
}
}

View File

@ -93,6 +93,28 @@ public class QuestionnaireAnswersValidatorTest {
assertThat(errors.toString(), containsString("Answer to question with linkId[link0] found of type [StringType] but this is invalid for question of type [boolean]"));
}
@Test
public void testMultipleGroupsWithNoLinkIdInQuestionnaire() {
Questionnaire q = new Questionnaire();
GroupComponent qGroup = q.getGroup().addGroup();
qGroup.addQuestion().setLinkId("link0").setRequired(true).setType(AnswerFormat.BOOLEAN);
qGroup = q.getGroup().addGroup();
qGroup.addQuestion().setLinkId("link1").setRequired(true).setType(AnswerFormat.BOOLEAN);
QuestionnaireAnswers qa = new QuestionnaireAnswers();
qa.getQuestionnaire().setReference("http://example.com/Questionnaire/q1");
org.hl7.fhir.instance.model.QuestionnaireAnswers.GroupComponent qaGroup = qa.getGroup().addGroup();
qaGroup.addQuestion().setLinkId("link0").addAnswer().setValue(new StringType("FOO"));
myWorkerCtx.getQuestionnaires().put(qa.getQuestionnaire().getReference(), q);
List<ValidationMessage> errors = new ArrayList<ValidationMessage>();
myVal.validate(errors, qa);
ourLog.info(errors.toString());
assertThat(errors.toString(), containsString("ValidationMessage[level=FATAL,type=BUSINESSRULE,location=//QuestionnaireAnswers/group[0],message=Questionnaire in invalid, unable to validate QuestionnaireAnswers: Multiple groups found at this position with blank/missing linkId]"));
assertEquals(1, errors.size());
}
@Test
public void testMultipleGroupsWithNoLinkIdInQuestionnaireAnswers() {
Questionnaire q = new Questionnaire();