Correctly handle declared extensions with multiple possible types
Squashed commit of the following: commitd6ea0552b7
Author: James <jamesagnew@gmail.com> Date: Thu Jan 26 09:04:58 2017 -0500 FIx test commite5876f7d86
Author: James <jamesagnew@gmail.com> Date: Thu Jan 26 08:35:38 2017 -0500 Work on extensison commit7e573c2299
Author: James <jamesagnew@gmail.com> Date: Thu Jan 26 07:27:25 2017 -0500 Work on extensions commit234b4a0c1c
Author: James Agnew <jamesagnew@gmail.com> Date: Thu Jan 26 06:25:47 2017 -0500 More work on these extensison commit2eb780a690
Author: James Agnew <jamesagnew@gmail.com> Date: Wed Jan 25 22:35:57 2017 -0500 Work on multitype extensions
This commit is contained in:
parent
8e444fab93
commit
12598b4e3b
|
@ -216,18 +216,6 @@ public abstract class BaseRuntimeElementCompositeDefinition<T extends IBase> ext
|
|||
orderToElementDef = newOrderToExtensionDef;
|
||||
}
|
||||
|
||||
// while (orderToElementDef.size() > 0 && orderToElementDef.firstKey() <
|
||||
// 0) {
|
||||
// BaseRuntimeDeclaredChildDefinition elementDef =
|
||||
// orderToElementDef.remove(orderToElementDef.firstKey());
|
||||
// if (elementDef.getElementName().equals("identifier")) {
|
||||
// orderToElementDef.put(theIdentifierOrder, elementDef);
|
||||
// } else {
|
||||
// throw new ConfigurationException("Don't know how to handle element: "
|
||||
// + elementDef.getElementName());
|
||||
// }
|
||||
// }
|
||||
|
||||
TreeSet<Integer> orders = new TreeSet<Integer>();
|
||||
orders.addAll(orderToElementDef.keySet());
|
||||
orders.addAll(orderToExtensionDef.keySet());
|
||||
|
@ -372,7 +360,7 @@ public abstract class BaseRuntimeElementCompositeDefinition<T extends IBase> ext
|
|||
def = new RuntimeChildDirectResource(nextField, childAnnotation, descriptionAnnotation, elementName);
|
||||
} else {
|
||||
childIsChoiceType |= choiceTypes.size() > 1;
|
||||
if (childIsChoiceType && !BaseResourceReferenceDt.class.isAssignableFrom(nextElementType) && !IBaseReference.class.isAssignableFrom(nextElementType)) {
|
||||
if (extensionAttr == null && childIsChoiceType && !BaseResourceReferenceDt.class.isAssignableFrom(nextElementType) && !IBaseReference.class.isAssignableFrom(nextElementType)) {
|
||||
def = new RuntimeChildChoiceDefinition(nextField, elementName, childAnnotation, descriptionAnnotation, choiceTypes);
|
||||
} else if (extensionAttr != null) {
|
||||
/*
|
||||
|
|
|
@ -38,6 +38,9 @@ public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefini
|
|||
private String myReferenceSuffix;
|
||||
private List<Class<? extends IBaseResource>> myResourceTypes;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public RuntimeChildChoiceDefinition(Field theField, String theElementName, Child theChildAnnotation, Description theDescriptionAnnotation, List<Class<? extends IBase>> theChoiceTypes) {
|
||||
super(theField, theChildAnnotation, theDescriptionAnnotation, theElementName);
|
||||
|
||||
|
@ -45,6 +48,8 @@ public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefini
|
|||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* For extension, if myChoiceTypes will be set some other way
|
||||
*/
|
||||
RuntimeChildChoiceDefinition(Field theField, String theElementName, Child theChildAnnotation, Description theDescriptionAnnotation) {
|
||||
|
@ -143,6 +148,7 @@ public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefini
|
|||
if (IBaseResource.class.isAssignableFrom(next) || IBaseReference.class.isAssignableFrom(next)) {
|
||||
next = theContext.getVersion().getResourceReferenceType();
|
||||
elementName = getElementName() + myReferenceSuffix;
|
||||
myNameToChildDefinition.put(elementName, nextDef);
|
||||
}
|
||||
|
||||
myDatatypeToElementDefinition.put(next, nextDef);
|
||||
|
|
|
@ -10,7 +10,7 @@ package ca.uhn.fhir.context;
|
|||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
@ -22,49 +22,66 @@ package ca.uhn.fhir.context;
|
|||
import static org.apache.commons.lang3.StringUtils.isNotBlank;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hl7.fhir.instance.model.api.IBase;
|
||||
import org.hl7.fhir.instance.model.api.IBaseResource;
|
||||
|
||||
import ca.uhn.fhir.model.api.IResource;
|
||||
import ca.uhn.fhir.model.api.IElement;
|
||||
import ca.uhn.fhir.model.api.annotation.Child;
|
||||
import ca.uhn.fhir.model.api.annotation.Description;
|
||||
import ca.uhn.fhir.model.api.annotation.Extension;
|
||||
import ca.uhn.fhir.util.ReflectionUtil;
|
||||
|
||||
public class RuntimeChildDeclaredExtensionDefinition extends BaseRuntimeDeclaredChildDefinition {
|
||||
public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceDefinition {
|
||||
|
||||
private BaseRuntimeElementDefinition<?> myChildDef;
|
||||
private Class<? extends IBase> myChildType;
|
||||
private String myDatatypeChildName;
|
||||
private boolean myDefinedLocally;
|
||||
private String myExtensionUrl;
|
||||
private boolean myModifier;
|
||||
private Map<String, RuntimeChildDeclaredExtensionDefinition> myUrlToChildExtension;
|
||||
private volatile Object myInstanceConstructorArguments;
|
||||
private Class<?> myEnumerationType;
|
||||
private Class<? extends IBase> myChildType;
|
||||
private RuntimeResourceBlockDefinition myChildResourceBlock;
|
||||
private BaseRuntimeElementDefinition<?> myChildDef;
|
||||
|
||||
/**
|
||||
* @param theBoundTypeBinder
|
||||
* If the child is of a type that requires a constructor argument to instantiate, this is the argument to
|
||||
* use
|
||||
* If the child is of a type that requires a constructor argument to instantiate, this is the argument to
|
||||
* use
|
||||
* @param theDefinedLocally
|
||||
* See {@link Extension#definedLocally()}
|
||||
* See {@link Extension#definedLocally()}
|
||||
*/
|
||||
RuntimeChildDeclaredExtensionDefinition(Field theField, Child theChild, Description theDescriptionAnnotation, Extension theExtension, String theElementName, String theExtensionUrl, Class<? extends IBase> theChildType, Object theBoundTypeBinder)
|
||||
RuntimeChildDeclaredExtensionDefinition(Field theField, Child theChild, Description theDescriptionAnnotation, Extension theExtension, String theElementName, String theExtensionUrl,
|
||||
Class<? extends IBase> theChildType, Object theBoundTypeBinder)
|
||||
throws ConfigurationException {
|
||||
super(theField, theChild, theDescriptionAnnotation, theElementName);
|
||||
super(theField, theElementName, theChild, theDescriptionAnnotation);
|
||||
assert isNotBlank(theExtensionUrl);
|
||||
myExtensionUrl = theExtensionUrl;
|
||||
myChildType = theChildType;
|
||||
myDefinedLocally = theExtension.definedLocally();
|
||||
myModifier = theExtension.isModifier();
|
||||
myInstanceConstructorArguments = theBoundTypeBinder;
|
||||
|
||||
List<Class<? extends IBase>> choiceTypes = new ArrayList<Class<? extends IBase>>();
|
||||
for (Class<? extends IElement> next : theChild.type()) {
|
||||
choiceTypes.add(next);
|
||||
}
|
||||
|
||||
if (Modifier.isAbstract(theChildType.getModifiers()) == false) {
|
||||
choiceTypes.add(theChildType);
|
||||
}
|
||||
|
||||
setChoiceTypes(choiceTypes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementName() {
|
||||
return "value";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -82,58 +99,66 @@ public class RuntimeChildDeclaredExtensionDefinition extends BaseRuntimeDeclared
|
|||
}
|
||||
|
||||
@Override
|
||||
public BaseRuntimeElementDefinition<?> getChildByName(String theName) {
|
||||
if (myDatatypeChildName != null) {
|
||||
if (myDatatypeChildName.equals(theName)) {
|
||||
return myChildDef;
|
||||
} else {
|
||||
return null;
|
||||
public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
|
||||
|
||||
String retVal = super.getChildNameByDatatype(theDatatype);
|
||||
|
||||
if (retVal != null) {
|
||||
BaseRuntimeElementDefinition<?> childDef = super.getChildElementDefinitionByDatatype(theDatatype);
|
||||
if (childDef instanceof RuntimeResourceBlockDefinition) {
|
||||
// Child is a newted extension
|
||||
retVal = null;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (retVal == null) {
|
||||
if (myModifier) {
|
||||
return "modifierExtension";
|
||||
} else {
|
||||
return "extension";
|
||||
}
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theType) {
|
||||
if (myChildType.equals(theType)) {
|
||||
return myChildDef;
|
||||
public BaseRuntimeElementDefinition<?> getChildByName(String theName) {
|
||||
String name = theName;
|
||||
if ("extension".equals(name)||"modifierExtension".equals(name)) {
|
||||
if (myChildResourceBlock != null) {
|
||||
return myChildResourceBlock;
|
||||
}
|
||||
if (myChildDef != null) {
|
||||
return myChildDef;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
if (getValidChildNames().contains(name) == false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return super.getChildByName(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) {
|
||||
if (myChildResourceBlock != null) {
|
||||
if (myChildResourceBlock.getImplementingClass().equals(theDatatype)) {
|
||||
return myChildResourceBlock;
|
||||
}
|
||||
}
|
||||
return super.getChildElementDefinitionByDatatype(theDatatype);
|
||||
}
|
||||
|
||||
public RuntimeChildDeclaredExtensionDefinition getChildExtensionForUrl(String theUrl) {
|
||||
return myUrlToChildExtension.get(theUrl);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
|
||||
if (myChildType.equals(theDatatype) && myDatatypeChildName != null) {
|
||||
return myDatatypeChildName;
|
||||
} else {
|
||||
return "extension";
|
||||
}
|
||||
}
|
||||
|
||||
public Class<? extends IBase> getChildType() {
|
||||
return myChildType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExtensionUrl() {
|
||||
return myExtensionUrl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseRuntimeElementDefinition<?> getSingleChildOrThrow() {
|
||||
return myChildDef;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getValidChildNames() {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
public boolean isDefinedLocally() {
|
||||
return myDefinedLocally;
|
||||
}
|
||||
|
@ -143,50 +168,52 @@ public class RuntimeChildDeclaredExtensionDefinition extends BaseRuntimeDeclared
|
|||
return myModifier;
|
||||
}
|
||||
|
||||
public IBase newInstance() {
|
||||
try {
|
||||
return myChildType.newInstance();
|
||||
} catch (InstantiationException e) {
|
||||
throw new ConfigurationException("Failed to instantiate type:" + myChildType.getName(), e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new ConfigurationException("Failed to instantiate type:" + myChildType.getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
|
||||
myUrlToChildExtension = new HashMap<String, RuntimeChildDeclaredExtensionDefinition>();
|
||||
|
||||
|
||||
BaseRuntimeElementDefinition<?> elementDef = theClassToElementDefinitions.get(myChildType);
|
||||
|
||||
|
||||
/*
|
||||
* This will happen for any type that isn't defined in the base set of
|
||||
* This will happen for any type that isn't defined in the base set of
|
||||
* built-in types, e.g. custom structures or custom extensions
|
||||
*/
|
||||
if (elementDef == null) {
|
||||
elementDef = theContext.getElementDefinition(myChildType);
|
||||
}
|
||||
|
||||
if (elementDef instanceof RuntimePrimitiveDatatypeDefinition || elementDef instanceof RuntimeCompositeDatatypeDefinition) {
|
||||
myDatatypeChildName = "value" + elementDef.getName().substring(0, 1).toUpperCase() + elementDef.getName().substring(1);
|
||||
if ("valueResourceReference".equals(myDatatypeChildName)) {
|
||||
// Per one of the examples here: http://hl7.org/implement/standards/fhir/extensibility.html#extension
|
||||
myDatatypeChildName = "valueResource";
|
||||
List<Class<? extends IBaseResource>> types = new ArrayList<Class<? extends IBaseResource>>();
|
||||
types.add(IResource.class);
|
||||
myChildDef = findResourceReferenceDefinition(theClassToElementDefinitions);
|
||||
} else {
|
||||
myChildDef = elementDef;
|
||||
if (Modifier.isAbstract(myChildType.getModifiers()) == false) {
|
||||
elementDef = theContext.getElementDefinition(myChildType);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
|
||||
if (elementDef instanceof RuntimePrimitiveDatatypeDefinition || elementDef instanceof RuntimeCompositeDatatypeDefinition) {
|
||||
// myDatatypeChildName = "value" + elementDef.getName().substring(0, 1).toUpperCase() + elementDef.getName().substring(1);
|
||||
// if ("valueResourceReference".equals(myDatatypeChildName)) {
|
||||
// Per one of the examples here: http://hl7.org/implement/standards/fhir/extensibility.html#extension
|
||||
// myDatatypeChildName = "valueResource";
|
||||
// List<Class<? extends IBaseResource>> types = new ArrayList<Class<? extends IBaseResource>>();
|
||||
// types.add(IBaseResource.class);
|
||||
// myChildDef = findResourceReferenceDefinition(theClassToElementDefinitions);
|
||||
// } else {
|
||||
myChildDef = elementDef;
|
||||
// }
|
||||
} else if (elementDef instanceof RuntimeResourceBlockDefinition) {
|
||||
RuntimeResourceBlockDefinition extDef = ((RuntimeResourceBlockDefinition) elementDef);
|
||||
for (RuntimeChildDeclaredExtensionDefinition next : extDef.getExtensions()) {
|
||||
myUrlToChildExtension.put(next.getExtensionUrl(), next);
|
||||
}
|
||||
myChildDef = extDef;
|
||||
myChildResourceBlock = (RuntimeResourceBlockDefinition) elementDef;
|
||||
}
|
||||
|
||||
myUrlToChildExtension = Collections.unmodifiableMap(myUrlToChildExtension);
|
||||
|
||||
super.sealAndInitialize(theContext, theClassToElementDefinitions);
|
||||
}
|
||||
|
||||
public IBase newInstance() {
|
||||
return ReflectionUtil.newInstance(myChildType);
|
||||
}
|
||||
|
||||
public Class<? extends IBase> getChildType() {
|
||||
return myChildType;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -53,8 +53,6 @@ public class DaoConfig {
|
|||
// ***
|
||||
private long myExpireSearchResultsAfterMillis = DateUtils.MILLIS_PER_HOUR;
|
||||
|
||||
private int myHardSearchLimit = 1000;
|
||||
|
||||
private int myHardTagListLimit = 1000;
|
||||
|
||||
private int myIncludeLimit = 2000;
|
||||
|
@ -325,8 +323,17 @@ public class DaoConfig {
|
|||
myExpireSearchResultsAfterMillis = theExpireSearchResultsAfterMillis;
|
||||
}
|
||||
|
||||
public void setHardSearchLimit(int theHardSearchLimit) {
|
||||
myHardSearchLimit = theHardSearchLimit;
|
||||
/**
|
||||
* Do not call this method, it exists only for legacy reasons. It
|
||||
* will be removed in a future version. Configure the page size on your
|
||||
* paging provider instead.
|
||||
*
|
||||
* @deprecated This method does not do anything. Configure the page size on your
|
||||
* paging provider instead. Deprecated in HAPI FHIR 2.3 (Jan 2017)
|
||||
*/
|
||||
@Deprecated
|
||||
public void setHardSearchLimit(@SuppressWarnings("unused") int theHardSearchLimit) {
|
||||
// this method does nothing
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -54,7 +54,6 @@ public class TestDstu3Config extends BaseJavaConfigDstu3 {
|
|||
retVal.setAllowExternalReferences(true);
|
||||
retVal.getTreatBaseUrlsAsLocal().add("http://fhirtest.uhn.ca/baseDstu3");
|
||||
retVal.getTreatBaseUrlsAsLocal().add("https://fhirtest.uhn.ca/baseDstu3");
|
||||
retVal.setHardSearchLimit(500);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
|
|
@ -375,15 +375,17 @@ public class FhirDstu1 implements IFhirVersion {
|
|||
RuntimeCompositeDatatypeDefinition pdef = (RuntimeCompositeDatatypeDefinition) nextChild.getSingleChildOrThrow();
|
||||
defn.getDefinition().addType().setCode(DataTypeEnum.VALUESET_BINDER.fromCodeString(pdef.getName()));
|
||||
} else {
|
||||
RuntimeResourceBlockDefinition pdef = (RuntimeResourceBlockDefinition) nextChild.getSingleChildOrThrow();
|
||||
scanForExtensions(theProfile, pdef, theExtensionDefToCode);
|
||||
|
||||
for (RuntimeChildDeclaredExtensionDefinition nextChildExt : pdef.getExtensions()) {
|
||||
StructureElementDefinitionType type = defn.getDefinition().addType();
|
||||
type.setCode(DataTypeEnum.EXTENSION);
|
||||
type.setProfile("#" + theExtensionDefToCode.get(nextChildExt));
|
||||
BaseRuntimeElementDefinition<?> singleChildOrThrow = nextChild.getSingleChildOrThrow();
|
||||
if (singleChildOrThrow instanceof RuntimeResourceBlockDefinition) {
|
||||
RuntimeResourceBlockDefinition pdef = (RuntimeResourceBlockDefinition) singleChildOrThrow;
|
||||
scanForExtensions(theProfile, pdef, theExtensionDefToCode);
|
||||
|
||||
for (RuntimeChildDeclaredExtensionDefinition nextChildExt : pdef.getExtensions()) {
|
||||
StructureElementDefinitionType type = defn.getDefinition().addType();
|
||||
type.setCode(DataTypeEnum.EXTENSION);
|
||||
type.setProfile("#" + theExtensionDefToCode.get(nextChildExt));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
package ca.uhn.fhir.context;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import ca.uhn.fhir.model.api.annotation.ResourceDef;
|
||||
import ca.uhn.fhir.model.dstu.resource.Patient;
|
||||
import ca.uhn.fhir.model.dstu.resource.Profile;
|
||||
|
@ -10,14 +19,6 @@ import ca.uhn.fhir.model.dstu.resource.ValueSet;
|
|||
import ca.uhn.fhir.model.dstu.valueset.DataTypeEnum;
|
||||
import ca.uhn.fhir.util.TestUtil;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class RuntimeResourceDefinitionTest {
|
||||
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(RuntimeResourceDefinitionTest.class);
|
||||
|
||||
|
@ -32,6 +33,7 @@ public class RuntimeResourceDefinitionTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testToProfileExtensions() throws Exception {
|
||||
FhirContext ctx = new FhirContext(ResourceWithExtensionsA.class, Profile.class);
|
||||
RuntimeResourceDefinition def = ctx.getResourceDefinition(ResourceWithExtensionsA.class);
|
||||
|
|
|
@ -919,7 +919,7 @@ public class XmlParserDstu2Test {
|
|||
public void testEncodeAndParseProfiledDatatypeChoice() throws Exception {
|
||||
IParser xmlParser = ourCtx.newXmlParser();
|
||||
|
||||
String input = IOUtils.toString(XmlParser.class.getResourceAsStream("/medicationstatement_invalidelement.xml"));
|
||||
String input = IOUtils.toString(XmlParser.class.getResourceAsStream("/medicationstatement_invalidelement.xml"), StandardCharsets.UTF_8);
|
||||
MedicationStatement ms = xmlParser.parseResource(MedicationStatement.class, input);
|
||||
SimpleQuantityDt q = (SimpleQuantityDt) ms.getDosage().get(0).getQuantity();
|
||||
assertEquals("1", q.getValueElement().getValueAsString());
|
||||
|
@ -1869,7 +1869,7 @@ public class XmlParserDstu2Test {
|
|||
|
||||
@Test
|
||||
public void testParseAndEncodeBundle() throws Exception {
|
||||
String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml"));
|
||||
String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml"), StandardCharsets.UTF_8);
|
||||
|
||||
Bundle parsed = ourCtx.newXmlParser().parseBundle(content);
|
||||
assertEquals("Bundle/example/_history/1", parsed.getId().getValue());
|
||||
|
@ -1904,7 +1904,7 @@ public class XmlParserDstu2Test {
|
|||
|
||||
@Test
|
||||
public void testParseAndEncodeBundleNewStyle() throws Exception {
|
||||
String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml"));
|
||||
String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml"), StandardCharsets.UTF_8);
|
||||
|
||||
IParser newXmlParser = ourCtx.newXmlParser();
|
||||
ca.uhn.fhir.model.dstu2.resource.Bundle parsed = newXmlParser.parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, content);
|
||||
|
@ -2411,12 +2411,14 @@ public class XmlParserDstu2Test {
|
|||
// TODO: implement this test, make sure we handle ID and meta correctly in Binary
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* See #191
|
||||
*/
|
||||
@Test
|
||||
public void testParseBundleWithLinksOfUnknownRelation() throws Exception {
|
||||
String input = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle_orion.xml"));
|
||||
String input = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle_orion.xml"), StandardCharsets.UTF_8);
|
||||
ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, input);
|
||||
|
||||
Link link = parsed.getLink().get(0);
|
||||
|
|
|
@ -28,7 +28,18 @@ public class TestOutcomeTest {
|
|||
outcome.element2 = nameDt;
|
||||
|
||||
IParser parser = FhirContext.forDstu2().newXmlParser();
|
||||
String outcomeString = parser.setPrettyPrint(true).encodeResourceToString(outcome);
|
||||
String outcomeString = parser.setPrettyPrint(true).encodeResourceToString(outcome);
|
||||
ourLog.info(outcomeString);
|
||||
|
||||
assertEquals("<OperationOutcome xmlns=\"http://hl7.org/fhir\">" +
|
||||
"<meta>" +
|
||||
"<profile value=\"http://hl7.org/fhir/profiles/custom-operation-outcome\"/>" +
|
||||
"</meta>" +
|
||||
"<extension url=\"#someElement2\">" +
|
||||
"<valueString value=\"testText\"/>" +
|
||||
"</extension>" +
|
||||
"</OperationOutcome>", parser.setPrettyPrint(false).encodeResourceToString(outcome));
|
||||
|
||||
CustomOperationOutcome parsedOutcome = parser.parseResource(CustomOperationOutcome.class, outcomeString);
|
||||
ourLog.info(outcomeString);
|
||||
|
||||
|
|
|
@ -10,13 +10,7 @@ import static org.junit.Assert.assertThat;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hl7.fhir.dstu3.model.Bundle;
|
||||
import org.hl7.fhir.dstu3.model.DateTimeType;
|
||||
import org.hl7.fhir.dstu3.model.Medication;
|
||||
import org.hl7.fhir.dstu3.model.MedicationRequest;
|
||||
import org.hl7.fhir.dstu3.model.Patient;
|
||||
import org.hl7.fhir.dstu3.model.Quantity;
|
||||
import org.hl7.fhir.dstu3.model.StringType;
|
||||
import org.hl7.fhir.dstu3.model.*;
|
||||
import org.hl7.fhir.instance.model.api.IBase;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
|
@ -49,6 +43,63 @@ public class CustomTypeDstu3Test {
|
|||
TestUtil.clearAllStaticFieldsForUnitTest();
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@ResourceDef
|
||||
public static class PatientWithExtensionWithTwoTypes extends Patient {
|
||||
@Child(name = "foo", type = { DateTimeType.class, Period.class }, min = 0, max = 1)
|
||||
@Extension(url = "http://example.com/extension#foo", definedLocally = true, isModifier = false)
|
||||
@Description(shortDefinition = "Some description")
|
||||
private Type foo;
|
||||
|
||||
public Type getFoo() {
|
||||
return foo;
|
||||
}
|
||||
|
||||
public void setFoo(Type theFoo) {
|
||||
foo = theFoo;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtensionWithTwoTypes() {
|
||||
PatientWithExtensionWithTwoTypes pt = new PatientWithExtensionWithTwoTypes();
|
||||
pt.setFoo(new DateTimeType("2011-01-01T00:00:00Z"));
|
||||
|
||||
String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(pt);
|
||||
ourLog.info(encoded);
|
||||
|
||||
pt = ourCtx.newXmlParser().parseResource(PatientWithExtensionWithTwoTypes.class, encoded);
|
||||
assertEquals("2011-01-01T00:00:00Z", ((DateTimeType)pt.getFoo()).getValueAsString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@ResourceDef
|
||||
public static class PatientWithExtensionWithOneTypes extends Patient {
|
||||
@Child(name = "foo", type = { DateTimeType.class }, min = 0, max = 1)
|
||||
@Extension(url = "http://example.com/extension#foo", definedLocally = true, isModifier = false)
|
||||
@Description(shortDefinition = "Some description")
|
||||
private Type foo;
|
||||
|
||||
public Type getFoo() {
|
||||
return foo;
|
||||
}
|
||||
|
||||
public void setFoo(Type theFoo) {
|
||||
foo = theFoo;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtensionWithOneTypes() {
|
||||
PatientWithExtensionWithOneTypes pt = new PatientWithExtensionWithOneTypes();
|
||||
pt.setFoo(new DateTimeType("2011-01-01T00:00:00Z"));
|
||||
|
||||
String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(pt);
|
||||
ourLog.info(encoded);
|
||||
|
||||
pt = ourCtx.newXmlParser().parseResource(PatientWithExtensionWithOneTypes.class, encoded);
|
||||
assertEquals("2011-01-01T00:00:00Z", ((DateTimeType)pt.getFoo()).getValueAsString());
|
||||
}
|
||||
|
||||
/**
|
||||
* See #364
|
||||
|
@ -68,15 +119,14 @@ public class CustomTypeDstu3Test {
|
|||
|
||||
//@formatter:on
|
||||
assertThat(xml, stringContainsInOrder(
|
||||
"<CustomResource xmlns=\"http://hl7.org/fhir\">",
|
||||
"<meta><profile value=\"http://hl7.org/fhir/profiles/custom-resource\"/></meta>",
|
||||
"<baseValueCustomDate><date value=\"2016-05-13\"/></baseValueCustomDate>",
|
||||
"</CustomResource>"
|
||||
));
|
||||
"<CustomResource xmlns=\"http://hl7.org/fhir\">",
|
||||
"<meta><profile value=\"http://hl7.org/fhir/profiles/custom-resource\"/></meta>",
|
||||
"<baseValueCustomDate><date value=\"2016-05-13\"/></baseValueCustomDate>",
|
||||
"</CustomResource>"));
|
||||
//@formatter:on
|
||||
|
||||
|
||||
CustomResource364Dstu3 parsedResource = parser.parseResource(CustomResource364Dstu3.class, xml);
|
||||
assertEquals("2016-05-13", ((CustomResource364CustomDate)parsedResource.getBaseValues()).getDate().getValueAsString());
|
||||
assertEquals("2016-05-13", ((CustomResource364CustomDate) parsedResource.getBaseValues()).getDate().getValueAsString());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -86,7 +136,7 @@ public class CustomTypeDstu3Test {
|
|||
public void testCustomTypeWithPrimitiveType() {
|
||||
FhirContext context = FhirContext.forDstu3();
|
||||
context.registerCustomTypes(new ArrayList<Class<? extends IBase>>());
|
||||
|
||||
|
||||
IParser parser = context.newXmlParser();
|
||||
|
||||
CustomResource364Dstu3 resource = new CustomResource364Dstu3();
|
||||
|
@ -96,32 +146,30 @@ public class CustomTypeDstu3Test {
|
|||
|
||||
//@formatter:on
|
||||
assertThat(xml, stringContainsInOrder(
|
||||
"<CustomResource xmlns=\"http://hl7.org/fhir\">",
|
||||
"<meta><profile value=\"http://hl7.org/fhir/profiles/custom-resource\"/></meta>",
|
||||
"<baseValueString value=\"2016-05-13\"/>",
|
||||
"</CustomResource>"
|
||||
));
|
||||
"<CustomResource xmlns=\"http://hl7.org/fhir\">",
|
||||
"<meta><profile value=\"http://hl7.org/fhir/profiles/custom-resource\"/></meta>",
|
||||
"<baseValueString value=\"2016-05-13\"/>",
|
||||
"</CustomResource>"));
|
||||
//@formatter:on
|
||||
|
||||
|
||||
CustomResource364Dstu3 parsedResource = parser.parseResource(CustomResource364Dstu3.class, xml);
|
||||
assertEquals("2016-05-13", ((StringType)parsedResource.getBaseValues()).getValueAsString());
|
||||
assertEquals("2016-05-13", ((StringType) parsedResource.getBaseValues()).getValueAsString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void parseBundleWithResourceDirective() {
|
||||
String input = createBundle(createResource(false), createResource(true));
|
||||
|
||||
|
||||
FhirContext ctx = FhirContext.forDstu3();
|
||||
ctx.setDefaultTypeForProfile("http://example.com/foo", MyCustomPatient.class);
|
||||
|
||||
|
||||
Bundle bundle = ctx.newXmlParser().parseResource(Bundle.class, input);
|
||||
|
||||
|
||||
Patient res0 = (Patient) bundle.getEntry().get(0).getResource();
|
||||
assertEquals(0, res0.getMeta().getProfile().size());
|
||||
List<org.hl7.fhir.dstu3.model.Extension> exts = res0.getExtensionsByUrl("http://example.com/Weight");
|
||||
assertEquals(1, exts.size());
|
||||
assertEquals("185 cm", ((StringType)exts.get(0).getValue()).getValue());
|
||||
assertEquals("185 cm", ((StringType) exts.get(0).getValue()).getValue());
|
||||
|
||||
MyCustomPatient res1 = (MyCustomPatient) bundle.getEntry().get(1).getResource();
|
||||
assertEquals(1, res1.getMeta().getProfile().size());
|
||||
|
@ -130,39 +178,38 @@ public class CustomTypeDstu3Test {
|
|||
assertEquals(0, exts.size());
|
||||
assertEquals("185 cm", res1.getWeight().getValue());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void parseResourceWithDirective() {
|
||||
String input = createResource(true);
|
||||
|
||||
|
||||
FhirContext ctx = FhirContext.forDstu3();
|
||||
ctx.setDefaultTypeForProfile("http://example.com/foo", MyCustomPatient.class);
|
||||
|
||||
|
||||
MyCustomPatient parsed = (MyCustomPatient) ctx.newXmlParser().parseResource(input);
|
||||
assertEquals(1, parsed.getMeta().getProfile().size());
|
||||
assertEquals("http://example.com/foo", parsed.getMeta().getProfile().get(0).getValue());
|
||||
|
||||
|
||||
List<org.hl7.fhir.dstu3.model.Extension> exts = parsed.getExtensionsByUrl("http://example.com/Weight");
|
||||
assertEquals(0, exts.size());
|
||||
|
||||
|
||||
assertEquals("185 cm", parsed.getWeight().getValue());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void parseResourceWithNoDirective() {
|
||||
String input = createResource(true);
|
||||
|
||||
|
||||
FhirContext ctx = FhirContext.forDstu3();
|
||||
Patient parsed = (Patient) ctx.newXmlParser().parseResource(input);
|
||||
assertEquals(1, parsed.getMeta().getProfile().size());
|
||||
assertEquals("http://example.com/foo", parsed.getMeta().getProfile().get(0).getValue());
|
||||
|
||||
|
||||
List<org.hl7.fhir.dstu3.model.Extension> exts = parsed.getExtensionsByUrl("http://example.com/Weight");
|
||||
assertEquals(1, exts.size());
|
||||
assertEquals("185 cm", ((StringType)exts.get(0).getValue()).getValue());
|
||||
assertEquals("185 cm", ((StringType) exts.get(0).getValue()).getValue());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testAccessEmptyMetaLists() {
|
||||
Patient p = new Patient();
|
||||
|
@ -175,7 +222,7 @@ public class CustomTypeDstu3Test {
|
|||
assertThat(p.getMeta().getTag(), empty());
|
||||
assertThat(p.getMeta().getTag("foo", "bar"), nullValue());
|
||||
assertThat(p.getMeta().getVersionId(), nullValue());
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -187,10 +234,10 @@ public class CustomTypeDstu3Test {
|
|||
p.getMeta().addSecurity().setSystem("SEC_S2").setCode("SEC_C2").setDisplay("SED_D2");
|
||||
p.getMeta().addTag().setSystem("TAG_S1").setCode("TAG_C1").setDisplay("TAG_D1");
|
||||
p.getMeta().addTag().setSystem("TAG_S2").setCode("TAG_C2").setDisplay("TAG_D2");
|
||||
|
||||
|
||||
String out = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(p);
|
||||
ourLog.info(out);
|
||||
|
||||
|
||||
//@formatter:off
|
||||
assertThat(out, stringContainsInOrder(
|
||||
"<meta>",
|
||||
|
@ -216,9 +263,9 @@ public class CustomTypeDstu3Test {
|
|||
"</tag>",
|
||||
"</meta>"));
|
||||
//@formatter:on
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEncodeWithCustomType() {
|
||||
|
||||
|
@ -241,7 +288,7 @@ public class CustomTypeDstu3Test {
|
|||
String messageString = p.encodeResourceToString(patient);
|
||||
|
||||
ourLog.info(messageString);
|
||||
|
||||
|
||||
//@formatter:off
|
||||
assertThat(messageString, stringContainsInOrder(
|
||||
"<meta>",
|
||||
|
@ -265,7 +312,7 @@ public class CustomTypeDstu3Test {
|
|||
|
||||
patient.getMeta().addProfile("http://example.com/foo");
|
||||
patient.getMeta().addProfile("http://example.com/bar");
|
||||
|
||||
|
||||
patient.addIdentifier().setSystem("urn:system").setValue("1234");
|
||||
patient.addName().setFamily("Rossi").addGiven("Mario");
|
||||
patient.setInsulinLevel(new Quantity());
|
||||
|
@ -283,7 +330,7 @@ public class CustomTypeDstu3Test {
|
|||
String messageString = p.encodeResourceToString(patient);
|
||||
|
||||
ourLog.info(messageString);
|
||||
|
||||
|
||||
//@formatter:off
|
||||
assertThat(messageString, stringContainsInOrder(
|
||||
"<meta>",
|
||||
|
@ -328,10 +375,10 @@ public class CustomTypeDstu3Test {
|
|||
+ "</medication> "
|
||||
+ "</MedicationRequest>";
|
||||
//@formatter:on
|
||||
|
||||
|
||||
FhirContext ctx = FhirContext.forDstu3();
|
||||
ctx.setDefaultTypeForProfile("http://fhir.something.com/StructureDefinition/our-medication", MyMedication.class);
|
||||
|
||||
|
||||
MedicationRequest mo = ctx.newXmlParser().parseResource(MedicationRequest.class, input);
|
||||
assertEquals(MyMedication.class, mo.getContained().get(0).getClass());
|
||||
}
|
||||
|
@ -411,8 +458,7 @@ public class CustomTypeDstu3Test {
|
|||
b.append(" <given value=\"Mario\"/>\n");
|
||||
b.append(" </name>\n");
|
||||
b.append("</Patient>");
|
||||
String input =
|
||||
b.toString();
|
||||
String input = b.toString();
|
||||
return input;
|
||||
}
|
||||
|
||||
|
@ -595,13 +641,11 @@ public class CustomTypeDstu3Test {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ResourceDef()
|
||||
public static class MyMedication extends Medication
|
||||
{
|
||||
public static class MyMedication extends Medication {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue