From 5cd13bc73f588339ea7228e8321f5ba09a5ad010 Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Thu, 7 Apr 2016 13:55:38 -0400 Subject: [PATCH] Improve error logging when the wrong version structure is passed into a parser --- .../parser/MultiVersionJsonParserTest.java | 18 ++++++++---------- .../fhir/parser/MultiVersionXmlParserTest.java | 15 +++++++++------ .../ca/uhn/fhir/testmindeps/ValidatorTest.java | 11 ++++++++--- .../java/ca/uhn/fhir/parser/BaseParser.java | 4 ++++ .../uhn/fhir/parser/JsonParserDstu3Test.java | 2 +- .../ca/uhn/fhir/parser/XmlParserDstu3Test.java | 2 +- 6 files changed, 31 insertions(+), 21 deletions(-) diff --git a/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/parser/MultiVersionJsonParserTest.java b/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/parser/MultiVersionJsonParserTest.java index 80c9309a928..35c8e2d4516 100644 --- a/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/parser/MultiVersionJsonParserTest.java +++ b/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/parser/MultiVersionJsonParserTest.java @@ -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()); + } } } diff --git a/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/parser/MultiVersionXmlParserTest.java b/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/parser/MultiVersionXmlParserTest.java index ab5991d952d..7ff77978357 100644 --- a/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/parser/MultiVersionXmlParserTest.java +++ b/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/parser/MultiVersionXmlParserTest.java @@ -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("")); - str = ourCtxDstu2.newXmlParser().encodeResourceToString(p); - ourLog.info(str); - assertThat(str, Matchers.stringContainsInOrder("")); + 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 diff --git a/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/testmindeps/ValidatorTest.java b/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/testmindeps/ValidatorTest.java index 14cfd9c065c..363ab56097a 100644 --- a/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/testmindeps/ValidatorTest.java +++ b/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/testmindeps/ValidatorTest.java @@ -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()); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java index 0d1569e0820..c26b473f388 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java @@ -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); } diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java index 1d2e6b09c81..d0385a225c7 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java @@ -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); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java index aa648fd4077..ff0d1ea5369 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java @@ -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);