Add some tests

This commit is contained in:
jamesagnew 2019-10-28 06:02:19 -04:00
parent 51a0006548
commit 8b227a59fc
3 changed files with 104 additions and 6 deletions

View File

@ -442,11 +442,7 @@ class ParserState<T> {
((IIdentifiableElement) myInstance).setElementSpecificId((theValue)); ((IIdentifiableElement) myInstance).setElementSpecificId((theValue));
} else if (myInstance instanceof IBaseElement) { } else if (myInstance instanceof IBaseElement) {
((IBaseElement) myInstance).setId(theValue); ((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 { } else {
if (myJsonMode) { if (myJsonMode) {
myErrorHandler.incorrectJsonType(null, myElementName, ValueType.OBJECT, null, ValueType.SCALAR, ScalarType.STRING); myErrorHandler.incorrectJsonType(null, myElementName, ValueType.OBJECT, null, ValueType.SCALAR, ScalarType.STRING);
@ -1251,7 +1247,7 @@ class ParserState<T> {
myErrorHandler.unknownAttribute(null, theName); myErrorHandler.unknownAttribute(null, theName);
} }
} else { } else {
myErrorHandler.unknownAttribute(null, theName); super.attributeValue(theName, theValue);
} }
} }
@ -1273,7 +1269,7 @@ class ParserState<T> {
@Override @Override
public void enteringNewElement(String theNamespaceUri, String theLocalPart) throws DataFormatException { public void enteringNewElement(String theNamespaceUri, String theLocalPart) throws DataFormatException {
myErrorHandler.unknownElement(null, theLocalPart); super.enteringNewElement(theNamespaceUri, theLocalPart);
push(new SwallowChildrenWholeState(getPreResourceState())); push(new SwallowChildrenWholeState(getPreResourceState()));
} }

View File

@ -190,6 +190,24 @@ public class XmlParserDstu2Test {
parser.parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, string); parser.parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, string);
} }
@Test
public void testIdOnComposite() {
String input = "<Patient xmlns=\"http://hl7.org/fhir\">\n" +
" <name id=\"foo\">\n" +
" <family value=\"hello\"/>" +
" </name>" +
" <active value=\"true\"/>" +
"</Patient>";
IParser p = ourCtx.newXmlParser();
Patient patient = p.parseResource(Patient.class, input);
assertTrue(patient.getActive());
assertEquals("foo", patient.getNameFirstRep().getElementSpecificId());
}
@Test @Test
public void testSetDontEncodeResourcesWithMetaSubPath() { public void testSetDontEncodeResourcesWithMetaSubPath() {
Patient p = new Patient(); Patient p = new Patient();

View File

@ -187,6 +187,66 @@ public class XmlParserDstu3Test {
} }
@Test
public void testUnknownAttributeInPrimitive() {
IParserErrorHandler errorHandler = mock(IParserErrorHandler.class);
String bundle = "<Bundle xmlns=\"http://hl7.org/fhir\">\n" +
" <total value=\"1\" foo=\"bar\"/>\n" +
"</Bundle>";
Bundle b = ourCtx.newXmlParser().setParserErrorHandler(errorHandler).parseResource(Bundle.class, bundle);
assertEquals(1, b.getTotal());
ArgumentCaptor<String> 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 = "<Bundle xmlns=\"http://hl7.org/fhir\">\n" +
" <total value=\"1\">\n" +
" <foo/>" +
" </total>\n" +
"</Bundle>";
Bundle b = ourCtx.newXmlParser().setParserErrorHandler(errorHandler).parseResource(Bundle.class, bundle);
assertEquals(1, b.getTotal());
ArgumentCaptor<String> 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 = "<Bundle xmlns=\"http://hl7.org/fhir\">\n" +
" <extension url=\"http://foo\">" +
" <valueString value=\"blah\"/>" +
" </extension>" +
" <modifierExtension url=\"http://foo\">" +
" <valueString value=\"blah\"/>" +
" </modifierExtension>" +
" <total value=\"1\"/>\n" +
"</Bundle>";
Bundle b = ourCtx.newXmlParser().setParserErrorHandler(errorHandler).parseResource(Bundle.class, bundle);
assertEquals(1, b.getTotal());
ArgumentCaptor<String> 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 @Test
public void testContainedResourceInExtensionUndeclared() { public void testContainedResourceInExtensionUndeclared() {
@ -219,6 +279,30 @@ public class XmlParserDstu3Test {
parser.parseResource(Bundle.class, string); parser.parseResource(Bundle.class, string);
} }
@Test
public void testContainedResourceWithNoId2() {
String input = "<Patient xmlns=\"http://hl7.org/fhir\">\n" +
" <contained>\n" +
" <Organization xmlns=\"http://hl7.org/fhir\">\n" +
" <name value=\"Contained Test Organization\"/>\n" +
" </Organization>\n" +
" </contained>" +
" <active value=\"true\"/>" +
"</Patient>";
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() @Test()
public void testContainedResourceWithNoIdLenient() throws IOException { public void testContainedResourceWithNoIdLenient() throws IOException {
String string = IOUtils.toString(getClass().getResourceAsStream("/bundle_with_contained_with_no_id.xml"), StandardCharsets.UTF_8); String string = IOUtils.toString(getClass().getResourceAsStream("/bundle_with_contained_with_no_id.xml"), StandardCharsets.UTF_8);