Improve error logging when the wrong version structure is passed into a parser

This commit is contained in:
jamesagnew 2016-04-07 13:55:38 -04:00
parent 8588bdfd1f
commit 5cd13bc73f
6 changed files with 31 additions and 21 deletions

View File

@ -1,9 +1,8 @@
package ca.uhn.fhir.parser; package ca.uhn.fhir.parser;
import static org.hamcrest.Matchers.stringContainsInOrder; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.fail;
import org.hamcrest.core.StringContains;
import org.junit.Test; import org.junit.Test;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
@ -20,13 +19,12 @@ public class MultiVersionJsonParserTest {
p.addIdentifier("urn:sys", "001"); p.addIdentifier("urn:sys", "001");
p.addUndeclaredExtension(false, "http://foo#ext", new QuantityDt(2.2)); p.addUndeclaredExtension(false, "http://foo#ext", new QuantityDt(2.2));
String str = FhirContext.forDstu2().newJsonParser().encodeResourceToString(p); try {
ourLog.info(str); FhirContext.forDstu2().newJsonParser().encodeResourceToString(p);
fail();
assertThat(str, stringContainsInOrder( } catch (IllegalArgumentException e) {
"{\"resourceType\":\"Patient\"", assertEquals("This parser is for FHIR version DSTU2 - Can not encode a structure for version DSTU1", e.getMessage());
"\"extension\":[{\"url\":\"http://foo#ext\",\"valueQuantity\":{\"value\":2.2}}]", }
"\"identifier\":[{\"system\":\"urn:sys\",\"value\":\"001\"}]"));
} }
} }

View File

@ -1,9 +1,10 @@
package ca.uhn.fhir.parser; package ca.uhn.fhir.parser;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.hamcrest.core.StringContains;
import org.junit.Test; import org.junit.Test;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
@ -31,10 +32,12 @@ public class MultiVersionXmlParserTest {
assertThat(str, Matchers.stringContainsInOrder("<extension url=\"http://foo#ext\"><valueQuantity><value value=\"2.2\"", "<comparator value=\"&lt;\"", "<units value=\"g/L\"", assertThat(str, Matchers.stringContainsInOrder("<extension url=\"http://foo#ext\"><valueQuantity><value value=\"2.2\"", "<comparator value=\"&lt;\"", "<units value=\"g/L\"",
"</valueQuantity></extension>")); "</valueQuantity></extension>"));
str = ourCtxDstu2.newXmlParser().encodeResourceToString(p); try {
ourLog.info(str); FhirContext.forDstu2().newXmlParser().encodeResourceToString(p);
assertThat(str, Matchers.stringContainsInOrder("<extension url=\"http://foo#ext\"><valueQuantity><value value=\"2.2\"", "<comparator value=\"&lt;\"", "<units value=\"g/L\"", fail();
"</valueQuantity></extension>")); } catch (IllegalArgumentException e) {
assertEquals("This parser is for FHIR version DSTU2 - Can not encode a structure for version DSTU1", e.getMessage());
}
} }
@Test @Test

View File

@ -16,10 +16,15 @@ public class ValidatorTest {
@Test @Test
public void testValidator() { public void testValidator() {
FhirContext ctx = new FhirContext(); FhirContext ctx = FhirContext.forDstu1();
FhirValidator val = ctx.newValidator(); FhirValidator val = ctx.newValidator();
val.validateWithResult(new Patient()); try {
val.validateWithResult(new Patient());
fail();
} catch (IllegalArgumentException e) {
assertEquals("This parser is for FHIR version DSTU1 - Can not encode a structure for version DSTU2", e.getMessage());
}
// Phloc is not onthe classpath // Phloc is not onthe classpath
assertTrue(val.isValidateAgainstStandardSchema()); assertTrue(val.isValidateAgainstStandardSchema());

View File

@ -369,6 +369,10 @@ public abstract class BaseParser implements IParser {
Validate.notNull(theResource, "theResource can not be null"); Validate.notNull(theResource, "theResource can not be null");
Validate.notNull(theWriter, "theWriter can not be null"); Validate.notNull(theWriter, "theWriter can not be null");
if (theResource.getStructureFhirVersionEnum() != myContext.getVersion().getVersion()) {
throw new IllegalArgumentException("This parser is for FHIR version " + myContext.getVersion().getVersion() + " - Can not encode a structure for version " + theResource.getStructureFhirVersionEnum());
}
doEncodeResourceToWriter(theResource, theWriter); doEncodeResourceToWriter(theResource, theWriter);
} }

View File

@ -82,7 +82,7 @@ public class JsonParserDstu3Test {
@Test @Test
public void testEncodeContainedResource() { public void testEncodeContainedResource() {
Patient patient = new Patient(); Patient patient = new Patient();
patient.setBirthDate(new Date()); patient.getBirthDateElement().setValueAsString("2016-04-05");
patient.addExtension().setUrl("test").setValue(new Reference(new Condition())); patient.addExtension().setUrl("test").setValue(new Reference(new Condition()));
String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(patient); String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(patient);

View File

@ -721,7 +721,7 @@ public class XmlParserDstu3Test {
@Test @Test
public void testEncodeContainedResource() { public void testEncodeContainedResource() {
Patient patient = new Patient(); Patient patient = new Patient();
patient.setBirthDate(new Date()); patient.getBirthDateElement().setValueAsString("2016-04-05");
patient.addExtension().setUrl("test").setValue(new Reference(new Condition())); patient.addExtension().setUrl("test").setValue(new Reference(new Condition()));
String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient);