From 8b227a59fc83045a8c7a7a8a3290be2f9a3f34f2 Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Mon, 28 Oct 2019 06:02:19 -0400 Subject: [PATCH] Add some tests --- .../java/ca/uhn/fhir/parser/ParserState.java | 8 +- .../uhn/fhir/parser/XmlParserDstu2Test.java | 18 ++++ .../uhn/fhir/parser/XmlParserDstu3Test.java | 84 +++++++++++++++++++ 3 files changed, 104 insertions(+), 6 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java index 9c45752c3fd..a40266923fc 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java @@ -442,11 +442,7 @@ class ParserState { ((IIdentifiableElement) myInstance).setElementSpecificId((theValue)); } else if (myInstance instanceof IBaseElement) { ((IBaseElement) myInstance).setId(theValue); - } else if (myInstance instanceof IBaseResource) { - new IdDt(theValue).applyTo((IBaseResource) myInstance); } - } else if ("url".equals(theName) && myInstance instanceof ExtensionDt) { - ((ExtensionDt) myInstance).setUrl(theValue); } else { if (myJsonMode) { myErrorHandler.incorrectJsonType(null, myElementName, ValueType.OBJECT, null, ValueType.SCALAR, ScalarType.STRING); @@ -1251,7 +1247,7 @@ class ParserState { myErrorHandler.unknownAttribute(null, theName); } } else { - myErrorHandler.unknownAttribute(null, theName); + super.attributeValue(theName, theValue); } } @@ -1273,7 +1269,7 @@ class ParserState { @Override public void enteringNewElement(String theNamespaceUri, String theLocalPart) throws DataFormatException { - myErrorHandler.unknownElement(null, theLocalPart); + super.enteringNewElement(theNamespaceUri, theLocalPart); push(new SwallowChildrenWholeState(getPreResourceState())); } diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java index 7f6b0370b7a..bec503d05a8 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java @@ -190,6 +190,24 @@ public class XmlParserDstu2Test { parser.parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, string); } + @Test + public void testIdOnComposite() { + String input = "\n" + + " \n" + + " " + + " " + + " " + + ""; + + IParser p = ourCtx.newXmlParser(); + + Patient patient = p.parseResource(Patient.class, input); + assertTrue(patient.getActive()); + assertEquals("foo", patient.getNameFirstRep().getElementSpecificId()); + + } + + @Test public void testSetDontEncodeResourcesWithMetaSubPath() { Patient p = new 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 8f49131707a..b94659d8480 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 @@ -187,6 +187,66 @@ public class XmlParserDstu3Test { } + @Test + public void testUnknownAttributeInPrimitive() { + + IParserErrorHandler errorHandler = mock(IParserErrorHandler.class); + + String bundle = "\n" + + " \n" + + ""; + + Bundle b = ourCtx.newXmlParser().setParserErrorHandler(errorHandler).parseResource(Bundle.class, bundle); + assertEquals(1, b.getTotal()); + + ArgumentCaptor attributeCaptor = ArgumentCaptor.forClass(String.class); + verify(errorHandler, times(1)).unknownAttribute(any(), attributeCaptor.capture()); + assertEquals("foo", attributeCaptor.getValue()); + } + + @Test + public void testUnknownElementInPrimitive() { + + IParserErrorHandler errorHandler = mock(IParserErrorHandler.class); + + String bundle = "\n" + + " \n" + + " " + + " \n" + + ""; + + Bundle b = ourCtx.newXmlParser().setParserErrorHandler(errorHandler).parseResource(Bundle.class, bundle); + assertEquals(1, b.getTotal()); + + ArgumentCaptor attributeCaptor = ArgumentCaptor.forClass(String.class); + verify(errorHandler, times(1)).unknownElement(any(), attributeCaptor.capture()); + assertEquals("foo", attributeCaptor.getValue()); + } + + @Test + public void testExtensionInInvalidSpot() { + + IParserErrorHandler errorHandler = mock(IParserErrorHandler.class); + + String bundle = "\n" + + " " + + " " + + " " + + " " + + " " + + " " + + " \n" + + ""; + + Bundle b = ourCtx.newXmlParser().setParserErrorHandler(errorHandler).parseResource(Bundle.class, bundle); + assertEquals(1, b.getTotal()); + + ArgumentCaptor attributeCaptor = ArgumentCaptor.forClass(String.class); + verify(errorHandler, times(2)).unknownElement(any(), attributeCaptor.capture()); + assertEquals("extension", attributeCaptor.getAllValues().get(0)); + assertEquals("modifierExtension", attributeCaptor.getAllValues().get(1)); + } + @Test public void testContainedResourceInExtensionUndeclared() { @@ -219,6 +279,30 @@ public class XmlParserDstu3Test { parser.parseResource(Bundle.class, string); } + @Test + public void testContainedResourceWithNoId2() { + String input = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " " + + " " + + ""; + + IParserErrorHandler errorHandler = mock(IParserErrorHandler.class); + + IParser p = ourCtx.newXmlParser(); + p.setParserErrorHandler(errorHandler); + + Patient patient = p.parseResource(Patient.class, input); + assertTrue(patient.getActive()); + + verify(errorHandler, times(1)).containedResourceWithNoId(nullable(IParseLocation.class)); + + } + + @Test() public void testContainedResourceWithNoIdLenient() throws IOException { String string = IOUtils.toString(getClass().getResourceAsStream("/bundle_with_contained_with_no_id.xml"), StandardCharsets.UTF_8);