Improve error logging when the wrong version structure is passed into a parser
This commit is contained in:
parent
8588bdfd1f
commit
5cd13bc73f
|
@ -1,9 +1,8 @@
|
|||
package ca.uhn.fhir.parser;
|
||||
|
||||
import static org.hamcrest.Matchers.stringContainsInOrder;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.hamcrest.core.StringContains;
|
||||
import org.junit.Test;
|
||||
|
||||
import ca.uhn.fhir.context.FhirContext;
|
||||
|
@ -20,13 +19,12 @@ public class MultiVersionJsonParserTest {
|
|||
p.addIdentifier("urn:sys", "001");
|
||||
p.addUndeclaredExtension(false, "http://foo#ext", new QuantityDt(2.2));
|
||||
|
||||
String str = FhirContext.forDstu2().newJsonParser().encodeResourceToString(p);
|
||||
ourLog.info(str);
|
||||
|
||||
assertThat(str, stringContainsInOrder(
|
||||
"{\"resourceType\":\"Patient\"",
|
||||
"\"extension\":[{\"url\":\"http://foo#ext\",\"valueQuantity\":{\"value\":2.2}}]",
|
||||
"\"identifier\":[{\"system\":\"urn:sys\",\"value\":\"001\"}]"));
|
||||
try {
|
||||
FhirContext.forDstu2().newJsonParser().encodeResourceToString(p);
|
||||
fail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertEquals("This parser is for FHIR version DSTU2 - Can not encode a structure for version DSTU1", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
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.core.StringContains;
|
||||
import org.junit.Test;
|
||||
|
||||
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=\"<\"", "<units value=\"g/L\"",
|
||||
"</valueQuantity></extension>"));
|
||||
|
||||
str = ourCtxDstu2.newXmlParser().encodeResourceToString(p);
|
||||
ourLog.info(str);
|
||||
assertThat(str, Matchers.stringContainsInOrder("<extension url=\"http://foo#ext\"><valueQuantity><value value=\"2.2\"", "<comparator value=\"<\"", "<units value=\"g/L\"",
|
||||
"</valueQuantity></extension>"));
|
||||
try {
|
||||
FhirContext.forDstu2().newXmlParser().encodeResourceToString(p);
|
||||
fail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertEquals("This parser is for FHIR version DSTU2 - Can not encode a structure for version DSTU1", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -16,11 +16,16 @@ public class ValidatorTest {
|
|||
@Test
|
||||
public void testValidator() {
|
||||
|
||||
FhirContext ctx = new FhirContext();
|
||||
FhirContext ctx = FhirContext.forDstu1();
|
||||
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
|
||||
assertTrue(val.isValidateAgainstStandardSchema());
|
||||
assertFalse(val.isValidateAgainstStandardSchematron());
|
||||
|
|
|
@ -369,6 +369,10 @@ public abstract class BaseParser implements IParser {
|
|||
Validate.notNull(theResource, "theResource 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);
|
||||
}
|
||||
|
||||
|
|
|
@ -82,7 +82,7 @@ public class JsonParserDstu3Test {
|
|||
@Test
|
||||
public void testEncodeContainedResource() {
|
||||
Patient patient = new Patient();
|
||||
patient.setBirthDate(new Date());
|
||||
patient.getBirthDateElement().setValueAsString("2016-04-05");
|
||||
patient.addExtension().setUrl("test").setValue(new Reference(new Condition()));
|
||||
|
||||
String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(patient);
|
||||
|
|
|
@ -721,7 +721,7 @@ public class XmlParserDstu3Test {
|
|||
@Test
|
||||
public void testEncodeContainedResource() {
|
||||
Patient patient = new Patient();
|
||||
patient.setBirthDate(new Date());
|
||||
patient.getBirthDateElement().setValueAsString("2016-04-05");
|
||||
patient.addExtension().setUrl("test").setValue(new Reference(new Condition()));
|
||||
|
||||
String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient);
|
||||
|
|
Loading…
Reference in New Issue