Merge pull request #1097 from jamesagnew/fhirterser-getvalues-enhancements

Fhirterser getvalues enhancements
This commit is contained in:
James Agnew 2018-10-29 09:58:54 -05:00 committed by GitHub
commit 3b8629617b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 1622 additions and 97 deletions

View File

@ -20,10 +20,10 @@ package ca.uhn.fhir.model.api;
* #L%
*/
import java.util.List;
import org.hl7.fhir.instance.model.api.IBaseDatatype;
import java.util.List;
public interface ISupportsUndeclaredExtensions extends IElement {
/**
@ -42,7 +42,8 @@ public interface ISupportsUndeclaredExtensions extends IElement {
/**
* Returns an <b>immutable</b> list containing all extensions (modifier and non-modifier).
*
* @see #getUndeclaredExtensions() To return a mutable list which may be used to remove extensions
* @see #getUndeclaredExtensions() To return a mutable list which may be used to remove undeclared non-modifier extensions
* @see #getUndeclaredModifierExtensions() To return a mutable list which may be used to remove undeclared modifier extensions
*/
List<ExtensionDt> getAllUndeclaredExtensions();

View File

@ -1,8 +1,23 @@
package ca.uhn.fhir.util;
import static org.apache.commons.lang3.StringUtils.defaultString;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import ca.uhn.fhir.context.*;
import ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum;
import ca.uhn.fhir.model.api.ExtensionDt;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.ISupportsUndeclaredExtensions;
import ca.uhn.fhir.model.base.composite.BaseContainedDt;
import ca.uhn.fhir.model.base.composite.BaseResourceReferenceDt;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.parser.DataFormatException;
import org.apache.commons.lang3.Validate;
import org.hl7.fhir.instance.model.api.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static org.apache.commons.lang3.StringUtils.*;
/*
* #%L
@ -23,23 +38,6 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank;
* limitations under the License.
* #L%
*/
import java.util.*;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.Validate;
import org.hl7.fhir.instance.model.api.*;
import ca.uhn.fhir.context.*;
import ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum;
import ca.uhn.fhir.model.api.ExtensionDt;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.ISupportsUndeclaredExtensions;
import ca.uhn.fhir.model.base.composite.BaseContainedDt;
import ca.uhn.fhir.model.base.composite.BaseResourceReferenceDt;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.parser.DataFormatException;
public class FhirTerser {
@ -61,6 +59,40 @@ public class FhirTerser {
return newList;
}
private ExtensionDt createEmptyExtensionDt(IBaseExtension theBaseExtension, String theUrl) {
return createEmptyExtensionDt(theBaseExtension, false, theUrl);
}
@SuppressWarnings("unchecked")
private ExtensionDt createEmptyExtensionDt(IBaseExtension theBaseExtension, boolean theIsModifier, String theUrl) {
ExtensionDt retVal = new ExtensionDt(theIsModifier, theUrl);
theBaseExtension.getExtension().add(retVal);
return retVal;
}
private ExtensionDt createEmptyExtensionDt(ISupportsUndeclaredExtensions theSupportsUndeclaredExtensions, String theUrl) {
return createEmptyExtensionDt(theSupportsUndeclaredExtensions, false, theUrl);
}
private ExtensionDt createEmptyExtensionDt(ISupportsUndeclaredExtensions theSupportsUndeclaredExtensions, boolean theIsModifier, String theUrl) {
return theSupportsUndeclaredExtensions.addUndeclaredExtension(theIsModifier, theUrl);
}
private IBaseExtension createEmptyExtension(IBaseHasExtensions theBaseHasExtensions, String theUrl) {
return (IBaseExtension) theBaseHasExtensions.addExtension().setUrl(theUrl);
}
private IBaseExtension createEmptyModifierExtension(IBaseHasModifierExtensions theBaseHasModifierExtensions, String theUrl) {
return (IBaseExtension) theBaseHasModifierExtensions.addModifierExtension().setUrl(theUrl);
}
private ExtensionDt createEmptyModifierExtensionDt(IBaseExtension theBaseExtension, String theUrl) {
return createEmptyExtensionDt(theBaseExtension, true, theUrl);
}
private ExtensionDt createEmptyModifierExtensionDt(ISupportsUndeclaredExtensions theSupportsUndeclaredExtensions, String theUrl) {
return createEmptyExtensionDt(theSupportsUndeclaredExtensions, true, theUrl);
}
/**
* Clones all values from a source object into the equivalent fields in a target object
@ -215,8 +247,12 @@ public class FhirTerser {
return retVal.get(0);
}
@SuppressWarnings("unchecked")
private <T> List<T> getValues(BaseRuntimeElementCompositeDefinition<?> theCurrentDef, Object theCurrentObj, List<String> theSubList, Class<T> theWantedClass) {
return getValues(theCurrentDef, theCurrentObj, theSubList, theWantedClass, false, false);
}
@SuppressWarnings("unchecked")
private <T> List<T> getValues(BaseRuntimeElementCompositeDefinition<?> theCurrentDef, Object theCurrentObj, List<String> theSubList, Class<T> theWantedClass, boolean theCreate, boolean theAddExtension) {
String name = theSubList.get(0);
List<T> retVal = new ArrayList<>();
@ -227,25 +263,161 @@ public class FhirTerser {
extensionUrl = extensionUrl.substring(0, endIndex);
}
List<ExtensionDt> extensions= Collections.emptyList();
if (myContext.getVersion().getVersion().isOlderThan(FhirVersionEnum.DSTU3)) {
// DTSU2
final String extensionDtUrlForLambda = extensionUrl;
List<ExtensionDt> extensionDts = Collections.emptyList();
if (theCurrentObj instanceof ISupportsUndeclaredExtensions) {
extensions = ((ISupportsUndeclaredExtensions) theCurrentObj).getUndeclaredExtensionsByUrl(extensionUrl);
} else if (theCurrentObj instanceof IBaseExtension) {
extensions = ((IBaseExtension)theCurrentObj).getExtension();
extensionDts = ((ISupportsUndeclaredExtensions) theCurrentObj).getUndeclaredExtensions()
.stream()
.filter(t -> t.getUrl().equals(extensionDtUrlForLambda))
.collect(Collectors.toList());
if (theAddExtension
&& (!(theCurrentObj instanceof IBaseExtension) || (extensionDts.isEmpty() && theSubList.size() == 1))) {
extensionDts.add(createEmptyExtensionDt((ISupportsUndeclaredExtensions) theCurrentObj, extensionUrl));
}
for (ExtensionDt next : extensions) {
if (extensionDts.isEmpty() && theCreate) {
extensionDts.add(createEmptyExtensionDt((ISupportsUndeclaredExtensions) theCurrentObj, extensionUrl));
}
} else if (theCurrentObj instanceof IBaseExtension) {
extensionDts = ((IBaseExtension) theCurrentObj).getExtension();
if (theAddExtension
&& (extensionDts.isEmpty() && theSubList.size() == 1)) {
extensionDts.add(createEmptyExtensionDt((IBaseExtension) theCurrentObj, extensionUrl));
}
if (extensionDts.isEmpty() && theCreate) {
extensionDts.add(createEmptyExtensionDt((IBaseExtension) theCurrentObj, extensionUrl));
}
}
for (ExtensionDt next : extensionDts) {
if (theWantedClass.isAssignableFrom(next.getClass())) {
retVal.add((T) next);
}
}
} else {
// DSTU3+
final String extensionUrlForLambda = extensionUrl;
List<IBaseExtension> extensions = Collections.emptyList();
if (theCurrentObj instanceof IBaseHasExtensions) {
extensions = ((IBaseHasExtensions) theCurrentObj).getExtension()
.stream()
.filter(t -> t.getUrl().equals(extensionUrlForLambda))
.collect(Collectors.toList());
if (theAddExtension
&& (!(theCurrentObj instanceof IBaseExtension) || (extensions.isEmpty() && theSubList.size() == 1))) {
extensions.add(createEmptyExtension((IBaseHasExtensions) theCurrentObj, extensionUrl));
}
if (extensions.isEmpty() && theCreate) {
extensions.add(createEmptyExtension((IBaseHasExtensions) theCurrentObj, extensionUrl));
}
}
for (IBaseExtension next : extensions) {
if (theWantedClass.isAssignableFrom(next.getClass())) {
retVal.add((T) next);
}
}
}
if (theSubList.size() > 1) {
List<T> values = retVal;
retVal = new ArrayList<>();
for (T nextElement : values) {
BaseRuntimeElementCompositeDefinition<?> nextChildDef = (BaseRuntimeElementCompositeDefinition<?>) myContext.getElementDefinition((Class<? extends IBase>) nextElement.getClass());
List<T> foundValues = getValues(nextChildDef, nextElement, theSubList.subList(1, theSubList.size()), theWantedClass);
List<T> foundValues = getValues(nextChildDef, nextElement, theSubList.subList(1, theSubList.size()), theWantedClass, theCreate, theAddExtension);
retVal.addAll(foundValues);
}
}
return retVal;
}
if (name.startsWith("modifierExtension('")) {
String extensionUrl = name.substring("modifierExtension('".length());
int endIndex = extensionUrl.indexOf('\'');
if (endIndex != -1) {
extensionUrl = extensionUrl.substring(0, endIndex);
}
if (myContext.getVersion().getVersion().isOlderThan(FhirVersionEnum.DSTU3)) {
// DSTU2
final String extensionDtUrlForLambda = extensionUrl;
List<ExtensionDt> extensionDts = Collections.emptyList();
if (theCurrentObj instanceof ISupportsUndeclaredExtensions) {
extensionDts = ((ISupportsUndeclaredExtensions) theCurrentObj).getUndeclaredModifierExtensions()
.stream()
.filter(t -> t.getUrl().equals(extensionDtUrlForLambda))
.collect(Collectors.toList());
if (theAddExtension
&& (!(theCurrentObj instanceof IBaseExtension) || (extensionDts.isEmpty() && theSubList.size() == 1))) {
extensionDts.add(createEmptyModifierExtensionDt((ISupportsUndeclaredExtensions) theCurrentObj, extensionUrl));
}
if (extensionDts.isEmpty() && theCreate) {
extensionDts.add(createEmptyModifierExtensionDt((ISupportsUndeclaredExtensions) theCurrentObj, extensionUrl));
}
} else if (theCurrentObj instanceof IBaseExtension) {
extensionDts = ((IBaseExtension) theCurrentObj).getExtension();
if (theAddExtension
&& (extensionDts.isEmpty() && theSubList.size() == 1)) {
extensionDts.add(createEmptyExtensionDt((IBaseExtension) theCurrentObj, extensionUrl));
}
if (extensionDts.isEmpty() && theCreate) {
extensionDts.add(createEmptyExtensionDt((IBaseExtension) theCurrentObj, extensionUrl));
}
}
for (ExtensionDt next : extensionDts) {
if (theWantedClass.isAssignableFrom(next.getClass())) {
retVal.add((T) next);
}
}
} else {
// DSTU3+
final String extensionUrlForLambda = extensionUrl;
List<IBaseExtension> extensions = Collections.emptyList();
if (theCurrentObj instanceof IBaseHasModifierExtensions) {
extensions = ((IBaseHasModifierExtensions) theCurrentObj).getModifierExtension()
.stream()
.filter(t -> t.getUrl().equals(extensionUrlForLambda))
.collect(Collectors.toList());
if (theAddExtension
&& (!(theCurrentObj instanceof IBaseExtension) || (extensions.isEmpty() && theSubList.size() == 1))) {
extensions.add(createEmptyModifierExtension((IBaseHasModifierExtensions) theCurrentObj, extensionUrl));
}
if (extensions.isEmpty() && theCreate) {
extensions.add(createEmptyModifierExtension((IBaseHasModifierExtensions) theCurrentObj, extensionUrl));
}
}
for (IBaseExtension next : extensions) {
if (theWantedClass.isAssignableFrom(next.getClass())) {
retVal.add((T) next);
}
}
}
if (theSubList.size() > 1) {
List<T> values = retVal;
retVal = new ArrayList<>();
for (T nextElement : values) {
BaseRuntimeElementCompositeDefinition<?> nextChildDef = (BaseRuntimeElementCompositeDefinition<?>) myContext.getElementDefinition((Class<? extends IBase>) nextElement.getClass());
List<T> foundValues = getValues(nextChildDef, nextElement, theSubList.subList(1, theSubList.size()), theWantedClass, theCreate, theAddExtension);
retVal.addAll(foundValues);
}
}
@ -256,6 +428,14 @@ public class FhirTerser {
BaseRuntimeChildDefinition nextDef = theCurrentDef.getChildByNameOrThrowDataFormatException(name);
List<? extends IBase> values = nextDef.getAccessor().getValues(theCurrentObj);
if (values.isEmpty() && theCreate) {
IBase value = nextDef.getChildByName(name).newInstance();
nextDef.getMutator().addValue(theCurrentObj, value);
List<IBase> list = new ArrayList<>();
list.add(value);
values = list;
}
if (theSubList.size() == 1) {
if (nextDef instanceof RuntimeChildChoiceDefinition) {
for (IBase next : values) {
@ -286,26 +466,109 @@ public class FhirTerser {
} else {
for (IBase nextElement : values) {
BaseRuntimeElementCompositeDefinition<?> nextChildDef = (BaseRuntimeElementCompositeDefinition<?>) myContext.getElementDefinition(nextElement.getClass());
List<T> foundValues = getValues(nextChildDef, nextElement, theSubList.subList(1, theSubList.size()), theWantedClass);
List<T> foundValues = getValues(nextChildDef, nextElement, theSubList.subList(1, theSubList.size()), theWantedClass, theCreate, theAddExtension);
retVal.addAll(foundValues);
}
}
return retVal;
}
/**
* Returns values stored in an element identified by its path. The list of values is of
* type {@link Object}.
*
* @param theResource The resource instance to be accessed. Must not be null.
* @param thePath The path for the element to be accessed.
* @return A list of values of type {@link Object}.
*/
public List<Object> getValues(IBaseResource theResource, String thePath) {
Class<Object> wantedClass = Object.class;
return getValues(theResource, thePath, wantedClass);
}
/**
* Returns values stored in an element identified by its path. The list of values is of
* type {@link Object}.
*
* @param theResource The resource instance to be accessed. Must not be null.
* @param thePath The path for the element to be accessed.
* @param theCreate When set to <code>true</code>, the terser will create a null-valued element where none exists.
* @return A list of values of type {@link Object}.
*/
public List<Object> getValues(IBaseResource theResource, String thePath, boolean theCreate) {
Class<Object> wantedClass = Object.class;
return getValues(theResource, thePath, wantedClass, theCreate);
}
/**
* Returns values stored in an element identified by its path. The list of values is of
* type {@link Object}.
*
* @param theResource The resource instance to be accessed. Must not be null.
* @param thePath The path for the element to be accessed.
* @param theCreate When set to <code>true</code>, the terser will create a null-valued element where none exists.
* @param theAddExtension When set to <code>true</code>, the terser will add a null-valued extension where one or more such extensions already exist.
* @return A list of values of type {@link Object}.
*/
public List<Object> getValues(IBaseResource theResource, String thePath, boolean theCreate, boolean theAddExtension) {
Class<Object> wantedClass = Object.class;
return getValues(theResource, thePath, wantedClass, theCreate, theAddExtension);
}
/**
* Returns values stored in an element identified by its path. The list of values is of
* type <code>theWantedClass</code>.
*
* @param theResource The resource instance to be accessed. Must not be null.
* @param thePath The path for the element to be accessed.
* @param theWantedClass The desired class to be returned in a list.
* @param <T> Type declared by <code>theWantedClass</code>
* @return A list of values of type <code>theWantedClass</code>.
*/
public <T> List<T> getValues(IBaseResource theResource, String thePath, Class<T> theWantedClass) {
RuntimeResourceDefinition def = myContext.getResourceDefinition(theResource);
List<String> parts = parsePath(def, thePath);
return getValues(def, theResource, parts, theWantedClass);
}
/**
* Returns values stored in an element identified by its path. The list of values is of
* type <code>theWantedClass</code>.
*
* @param theResource The resource instance to be accessed. Must not be null.
* @param thePath The path for the element to be accessed.
* @param theWantedClass The desired class to be returned in a list.
* @param theCreate When set to <code>true</code>, the terser will create a null-valued element where none exists.
* @param <T> Type declared by <code>theWantedClass</code>
* @return A list of values of type <code>theWantedClass</code>.
*/
public <T> List<T> getValues(IBaseResource theResource, String thePath, Class<T> theWantedClass, boolean theCreate) {
RuntimeResourceDefinition def = myContext.getResourceDefinition(theResource);
List<String> parts = parsePath(def, thePath);
return getValues(def, theResource, parts, theWantedClass, theCreate, false);
}
/**
* Returns values stored in an element identified by its path. The list of values is of
* type <code>theWantedClass</code>.
*
* @param theResource The resource instance to be accessed. Must not be null.
* @param thePath The path for the element to be accessed.
* @param theWantedClass The desired class to be returned in a list.
* @param theCreate When set to <code>true</code>, the terser will create a null-valued element where none exists.
* @param theAddExtension When set to <code>true</code>, the terser will add a null-valued extension where one or more such extensions already exist.
* @param <T> Type declared by <code>theWantedClass</code>
* @return A list of values of type <code>theWantedClass</code>.
*/
public <T> List<T> getValues(IBaseResource theResource, String thePath, Class<T> theWantedClass, boolean theCreate, boolean theAddExtension) {
RuntimeResourceDefinition def = myContext.getResourceDefinition(theResource);
List<String> parts = parsePath(def, thePath);
return getValues(def, theResource, parts, theWantedClass, theCreate, theAddExtension);
}
private List<String> parsePath(BaseRuntimeElementCompositeDefinition<?> theElementDef, String thePath) {
List<String> parts = new ArrayList<>();

View File

@ -1,24 +1,5 @@
package ca.uhn.fhir.util;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseReference;
import org.junit.AfterClass;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
import ca.uhn.fhir.context.FhirContext;
@ -31,9 +12,26 @@ import ca.uhn.fhir.model.dstu2.resource.Bundle;
import ca.uhn.fhir.model.dstu2.resource.Observation;
import ca.uhn.fhir.model.dstu2.resource.Organization;
import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.model.primitive.BooleanDt;
import ca.uhn.fhir.model.primitive.MarkdownDt;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.parser.DataFormatException;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseExtension;
import org.hl7.fhir.instance.model.api.IBaseReference;
import org.hl7.fhir.instance.model.api.IPrimitiveType;
import org.junit.AfterClass;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class FhirTerserDstu2Test {
@ -181,6 +179,597 @@ public class FhirTerserDstu2Test {
assertSame(ref, refs.get(0));
}
@Test
public void testGetValues() {
Patient p = new Patient();
p.setActive(true);
p.addUndeclaredExtension(false, "http://acme.org/extension", new StringDt("value"));
p.addUndeclaredExtension(false, "http://acme.org/otherExtension", new StringDt("otherValue"));
p.addUndeclaredExtension(true, "http://acme.org/modifierExtension", new StringDt("modifierValue"));
p.addUndeclaredExtension(false, "http://acme.org/parentExtension").addUndeclaredExtension(false, "http://acme.org/childExtension", new StringDt("nestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<Object> values = ourCtx.newTerser().getValues(p, "Patient.active");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IPrimitiveType);
assertTrue(values.get(0) instanceof BooleanDt);
assertTrue(((BooleanDt) values.get(0)).getValue());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("value", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("modifierValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("nestedValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
}
@Test
public void testGetValuesAndModify() {
Patient p = new Patient();
p.setActive(true);
p.addUndeclaredExtension(false, "http://acme.org/extension", new StringDt("value"));
p.addUndeclaredExtension(false, "http://acme.org/otherExtension", new StringDt("otherValue"));
p.addUndeclaredExtension(true, "http://acme.org/modifierExtension", new StringDt("modifierValue"));
p.addUndeclaredExtension(false, "http://acme.org/parentExtension").addUndeclaredExtension(false, "http://acme.org/childExtension", new StringDt("nestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<Object> values = ourCtx.newTerser().getValues(p, "Patient.active");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IPrimitiveType);
assertTrue(values.get(0) instanceof BooleanDt);
assertTrue(((BooleanDt) values.get(0)).getValue());
((BooleanDt) values.get(0)).setValue(Boolean.FALSE);
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
values = ourCtx.newTerser().getValues(p, "Patient.active");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IPrimitiveType);
assertTrue(values.get(0) instanceof BooleanDt);
assertFalse(((BooleanDt) values.get(0)).getValue());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("value", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
((ExtensionDt) values.get(0)).setValue(new StringDt("modifiedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("modifiedValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("modifierValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
((ExtensionDt) values.get(0)).setValue(new StringDt("modifiedModifierValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("modifiedModifierValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("nestedValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
((ExtensionDt) values.get(0)).setValue(new StringDt("modifiedNestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("modifiedNestedValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
}
@Test
public void testGetValuesMultiple() {
Patient p = new Patient();
p.addUndeclaredExtension(false, "http://acme.org/extension", new StringDt("value1"));
p.addUndeclaredExtension(false, "http://acme.org/extension", new StringDt("value2"));
p.addUndeclaredExtension(false, "http://acme.org/otherExtension", new StringDt("otherValue"));
p.addUndeclaredExtension(true, "http://acme.org/modifierExtension", new StringDt("modifierValue1"));
p.addUndeclaredExtension(true, "http://acme.org/modifierExtension", new StringDt("modifierValue2"));
p.addUndeclaredExtension(false, "http://acme.org/parentExtension").addUndeclaredExtension(false, "http://acme.org/childExtension", new StringDt("nestedValue1"));
p.addUndeclaredExtension(false, "http://acme.org/parentExtension").addUndeclaredExtension(false, "http://acme.org/childExtension", new StringDt("nestedValue2"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<Object> values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')");
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("value1", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(1)).getUrl());
assertEquals("value2", ((StringDt) ((ExtensionDt) values.get(1)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')");
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("modifierValue1", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(1)).getUrl());
assertEquals("modifierValue2", ((StringDt) ((ExtensionDt) values.get(1)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')");
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("nestedValue1", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(1)).getUrl());
assertEquals("nestedValue2", ((StringDt) ((ExtensionDt) values.get(1)).getValue()).getValueAsString());
}
@Test
public void testGetValuesWithWantedClass() {
Patient p = new Patient();
p.setActive(true);
p.addUndeclaredExtension(false, "http://acme.org/extension", new StringDt("value"));
p.addUndeclaredExtension(false, "http://acme.org/otherExtension", new StringDt("otherValue"));
p.addUndeclaredExtension(true, "http://acme.org/modifierExtension", new StringDt("modifierValue"));
p.addUndeclaredExtension(false, "http://acme.org/parentExtension").addUndeclaredExtension(false, "http://acme.org/childExtension", new StringDt("nestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<IPrimitiveType> values = ourCtx.newTerser().getValues(p, "Patient.active", IPrimitiveType.class);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof BooleanDt);
assertTrue(((BooleanDt) values.get(0)).getValue());
List<ExtensionDt> extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", ExtensionDt.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringDt);
assertEquals("http://acme.org/extension", extValues.get(0).getUrl());
assertEquals("value", ((StringDt) extValues.get(0).getValue()).getValueAsString());
extValues = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", ExtensionDt.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringDt);
assertEquals("http://acme.org/modifierExtension", extValues.get(0).getUrl());
assertEquals("modifierValue", ((StringDt) (extValues.get(0).getValue())).getValueAsString());
extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", ExtensionDt.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringDt);
assertEquals("http://acme.org/childExtension", extValues.get(0).getUrl());
assertEquals("nestedValue", ((StringDt) extValues.get(0).getValue()).getValueAsString());
}
@Test
public void testGetValuesWithWantedClassAndModify() {
Patient p = new Patient();
p.setActive(true);
p.addUndeclaredExtension(false, "http://acme.org/extension", new StringDt("value"));
p.addUndeclaredExtension(false, "http://acme.org/otherExtension", new StringDt("otherValue"));
p.addUndeclaredExtension(true, "http://acme.org/modifierExtension", new StringDt("modifierValue"));
p.addUndeclaredExtension(false, "http://acme.org/parentExtension").addUndeclaredExtension(false, "http://acme.org/childExtension", new StringDt("nestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<IPrimitiveType> values = ourCtx.newTerser().getValues(p, "Patient.active", IPrimitiveType.class);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof BooleanDt);
assertTrue(((BooleanDt) values.get(0)).getValue());
((BooleanDt) values.get(0)).setValue(Boolean.FALSE);
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
values = ourCtx.newTerser().getValues(p, "Patient.active", IPrimitiveType.class);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof BooleanDt);
assertFalse(((BooleanDt) values.get(0)).getValue());
List<ExtensionDt> extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", ExtensionDt.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringDt);
assertEquals("http://acme.org/extension", extValues.get(0).getUrl());
assertEquals("value", ((StringDt) (extValues.get(0).getValue())).getValueAsString());
extValues.get(0).setValue(new StringDt("modifiedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", ExtensionDt.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringDt);
assertEquals("http://acme.org/extension", extValues.get(0).getUrl());
assertEquals("modifiedValue", ((StringDt) (extValues.get(0).getValue())).getValueAsString());
extValues = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", ExtensionDt.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringDt);
assertEquals("http://acme.org/modifierExtension", extValues.get(0).getUrl());
assertEquals("modifierValue", ((StringDt) (extValues.get(0).getValue())).getValueAsString());
extValues.get(0).setValue(new StringDt("modifiedModifierValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
extValues = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", ExtensionDt.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringDt);
assertEquals("http://acme.org/modifierExtension", extValues.get(0).getUrl());
assertEquals("modifiedModifierValue", ((StringDt) (extValues.get(0).getValue())).getValueAsString());
extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", ExtensionDt.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringDt);
assertEquals("http://acme.org/childExtension", extValues.get(0).getUrl());
assertEquals("nestedValue", ((StringDt) extValues.get(0).getValue()).getValueAsString());
extValues.get(0).setValue(new StringDt("modifiedNestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", ExtensionDt.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringDt);
assertEquals("http://acme.org/childExtension", extValues.get(0).getUrl());
assertEquals("modifiedNestedValue", ((StringDt) extValues.get(0).getValue()).getValueAsString());
}
@Test
public void testGetValuesWithWantedClassAndTheCreate() {
Patient p = new Patient();
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<IPrimitiveType> values = ourCtx.newTerser().getValues(p, "Patient.active", IPrimitiveType.class, true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof BooleanDt);
assertNull(((BooleanDt) values.get(0)).getValue());
List<ExtensionDt> extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", ExtensionDt.class, true);
assertEquals(1, extValues.size());
assertEquals("http://acme.org/extension", extValues.get(0).getUrl());
assertNull(extValues.get(0).getValue());
extValues = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", ExtensionDt.class, true);
assertEquals(1, extValues.size());
assertEquals("http://acme.org/modifierExtension", extValues.get(0).getUrl());
assertNull(extValues.get(0).getValue());
extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", ExtensionDt.class, true);
assertEquals(1, extValues.size());
assertEquals("http://acme.org/childExtension", extValues.get(0).getUrl());
assertNull(extValues.get(0).getValue());
}
@Test
public void testGetValuesWithTheAddExtensionAndModify() {
Patient p = new Patient();
p.setActive(true);
p.addUndeclaredExtension(false, "http://acme.org/extension", new StringDt("value"));
p.addUndeclaredExtension(false, "http://acme.org/otherExtension", new StringDt("otherValue"));
p.addUndeclaredExtension(true, "http://acme.org/modifierExtension", new StringDt("modifierValue"));
p.addUndeclaredExtension(false, "http://acme.org/parentExtension").addUndeclaredExtension(false, "http://acme.org/childExtension", new StringDt("nestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<Object> values = ourCtx.newTerser().getValues(p, "Patient.active");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IPrimitiveType);
assertTrue(values.get(0) instanceof BooleanDt);
assertTrue(((BooleanDt) values.get(0)).getValue());
// No change.
values = ourCtx.newTerser().getValues(p, "Patient.active", false, true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IPrimitiveType);
assertTrue(values.get(0) instanceof BooleanDt);
assertTrue(((BooleanDt) values.get(0)).getValue());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("value", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", false, true);
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("value", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(1)).getUrl());
assertNull(((ExtensionDt) values.get(1)).getValue());
((ExtensionDt) values.get(1)).setValue(new StringDt("addedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(1)).getUrl());
assertEquals("addedValue", ((StringDt) ((ExtensionDt) values.get(1)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("modifierValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", false, true);
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("modifierValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(1)).getUrl());
assertNull(((ExtensionDt) values.get(1)).getValue());
((ExtensionDt) values.get(1)).setValue(new StringDt("addedModifierValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(1)).getUrl());
assertEquals("addedModifierValue", ((StringDt) ((ExtensionDt) values.get(1)).getValue()).getValueAsString());
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("nestedValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", false, true);
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("nestedValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(1)).getUrl());
assertNull(((ExtensionDt) values.get(1)).getValue());
((ExtensionDt) values.get(1)).setValue(new StringDt("addedNestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(1)).getUrl());
assertEquals("addedNestedValue", ((StringDt) ((ExtensionDt) values.get(1)).getValue()).getValueAsString());
}
@Test
public void testGetValuesWithTheCreate() {
Patient p = new Patient();
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<Object> values = ourCtx.newTerser().getValues(p, "Patient.active", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IPrimitiveType);
assertTrue(values.get(0) instanceof BooleanDt);
assertNull(((BooleanDt) values.get(0)).getValue());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(0)).getUrl());
assertNull(((ExtensionDt) values.get(0)).getValue());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(0)).getUrl());
assertNull(((ExtensionDt) values.get(0)).getValue());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(0)).getUrl());
assertNull(((ExtensionDt) values.get(0)).getValue());
}
@Test
public void testGetValuesWithTheCreateAndTheAddExtensionAndModify() {
Patient p = new Patient();
p.setActive(true);
p.addUndeclaredExtension(false, "http://acme.org/extension", new StringDt("value"));
p.addUndeclaredExtension(false, "http://acme.org/otherExtension", new StringDt("otherValue"));
p.addUndeclaredExtension(true, "http://acme.org/modifierExtension", new StringDt("modifierValue"));
p.addUndeclaredExtension(false, "http://acme.org/parentExtension").addUndeclaredExtension(false, "http://acme.org/childExtension", new StringDt("nestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<Object> values = ourCtx.newTerser().getValues(p, "Patient.active");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IPrimitiveType);
assertTrue(values.get(0) instanceof BooleanDt);
assertTrue(((BooleanDt) values.get(0)).getValue());
// No change.
values = ourCtx.newTerser().getValues(p, "Patient.active", true, true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IPrimitiveType);
assertTrue(values.get(0) instanceof BooleanDt);
assertTrue(((BooleanDt) values.get(0)).getValue());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("value", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", true, true);
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("value", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(1)).getUrl());
assertNull(((ExtensionDt) values.get(1)).getValue());
((ExtensionDt) values.get(1)).setValue(new StringDt("addedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(1)).getUrl());
assertEquals("addedValue", ((StringDt) ((ExtensionDt) values.get(1)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("modifierValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", true, true);
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("modifierValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(1)).getUrl());
assertNull(((ExtensionDt) values.get(1)).getValue());
((ExtensionDt) values.get(1)).setValue(new StringDt("addedModifierValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(1)).getUrl());
assertEquals("addedModifierValue", ((StringDt) ((ExtensionDt) values.get(1)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("nestedValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", true, true);
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("nestedValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(1)).getUrl());
assertNull(((ExtensionDt) values.get(1)).getValue());
((ExtensionDt) values.get(1)).setValue(new StringDt("addedNestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(1)).getUrl());
assertEquals("addedNestedValue", ((StringDt) ((ExtensionDt) values.get(1)).getValue()).getValueAsString());
}
@Test
public void testGetValuesWithTheCreateAndNoOverwrite() {
Patient p = new Patient();
p.setActive(true);
p.addUndeclaredExtension(false, "http://acme.org/extension", new StringDt("value"));
p.addUndeclaredExtension(false, "http://acme.org/otherExtension", new StringDt("otherValue"));
p.addUndeclaredExtension(true, "http://acme.org/modifierExtension", new StringDt("modifierValue"));
p.addUndeclaredExtension(false, "http://acme.org/parentExtension").addUndeclaredExtension(false, "http://acme.org/childExtension", new StringDt("nestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<Object> values = ourCtx.newTerser().getValues(p, "Patient.active", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IPrimitiveType);
assertTrue(values.get(0) instanceof BooleanDt);
assertTrue(((BooleanDt) values.get(0)).getValue());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/extension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("value", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/modifierExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("modifierValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof ExtensionDt);
assertEquals("http://acme.org/childExtension", ((ExtensionDt) values.get(0)).getUrl());
assertEquals("nestedValue", ((StringDt) ((ExtensionDt) values.get(0)).getValue()).getValueAsString());
}
@Test
public void testVisitWithModelVisitor2() {
IModelVisitor2 visitor = mock(IModelVisitor2.class);

View File

@ -1,41 +1,27 @@
package ca.uhn.fhir.util;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.DataFormatException;
import org.hl7.fhir.dstu3.model.*;
import org.hl7.fhir.dstu3.model.Patient.LinkType;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseExtension;
import org.hl7.fhir.instance.model.api.IBaseReference;
import org.junit.AfterClass;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.Extension;
import org.hl7.fhir.dstu3.model.Identifier;
import org.hl7.fhir.dstu3.model.MarkdownType;
import org.hl7.fhir.dstu3.model.Money;
import org.hl7.fhir.dstu3.model.Observation;
import org.hl7.fhir.dstu3.model.Organization;
import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.Patient.LinkType;
import org.hl7.fhir.dstu3.model.Quantity;
import org.hl7.fhir.dstu3.model.Reference;
import org.hl7.fhir.dstu3.model.StringType;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseReference;
import org.junit.AfterClass;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.DataFormatException;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class FhirTerserDstu3Test {
@ -183,6 +169,683 @@ public class FhirTerserDstu3Test {
assertSame(ref, refs.get(0));
}
@Test
public void testGetValues() {
Patient p = new Patient();
p.setActive(true);
p.addExtension()
.setUrl("http://acme.org/extension")
.setValue(new StringType("value"));
p.addExtension()
.setUrl("http://acme.org/otherExtension")
.setValue(new StringType("otherValue"));
p.addModifierExtension()
.setUrl("http://acme.org/modifierExtension")
.setValue(new StringType("modifierValue"));
p.addExtension()
.setUrl("http://acme.org/parentExtension")
.addExtension()
.setUrl("http://acme.org/childExtension")
.setValue(new StringType("nestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<Object> values = ourCtx.newTerser().getValues(p, "Patient.active");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof PrimitiveType);
assertTrue(values.get(0) instanceof BooleanType);
assertTrue(((BooleanType) values.get(0)).booleanValue());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(0)).getUrl());
assertEquals("value", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(0)).getUrl());
assertEquals("modifierValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(0)).getUrl());
assertEquals("nestedValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
}
@Test
public void testGetValuesAndModify() {
Patient p = new Patient();
p.setActive(true);
p.addExtension()
.setUrl("http://acme.org/extension")
.setValue(new StringType("value"));
p.addExtension()
.setUrl("http://acme.org/otherExtension")
.setValue(new StringType("otherValue"));
p.addModifierExtension()
.setUrl("http://acme.org/modifierExtension")
.setValue(new StringType("modifierValue"));
p.addExtension()
.setUrl("http://acme.org/parentExtension")
.addExtension()
.setUrl("http://acme.org/childExtension")
.setValue(new StringType("nestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<Object> values = ourCtx.newTerser().getValues(p, "Patient.active");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof PrimitiveType);
assertTrue(values.get(0) instanceof BooleanType);
assertTrue(((BooleanType) values.get(0)).booleanValue());
((BooleanType) values.get(0)).setValue(Boolean.FALSE);
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
values = ourCtx.newTerser().getValues(p, "Patient.active");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof PrimitiveType);
assertTrue(values.get(0) instanceof BooleanType);
assertFalse(((BooleanType) values.get(0)).booleanValue());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(0)).getUrl());
assertEquals("value", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
((Extension) values.get(0)).setValue(new StringType("modifiedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(0)).getUrl());
assertEquals("modifiedValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(0)).getUrl());
assertEquals("modifierValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
((Extension) values.get(0)).setValue(new StringType("modifiedModifierValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(0)).getUrl());
assertEquals("modifiedModifierValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(0)).getUrl());
assertEquals("nestedValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
((Extension) values.get(0)).setValue(new StringType("modifiedNestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(0)).getUrl());
assertEquals("modifiedNestedValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
}
@Test
public void testGetValuesMultiple() {
Patient p = new Patient();
p.addExtension()
.setUrl("http://acme.org/extension")
.setValue(new StringType("value1"));
p.addExtension()
.setUrl("http://acme.org/extension")
.setValue(new StringType("value2"));
p.addExtension()
.setUrl("http://acme.org/otherExtension")
.setValue(new StringType("otherValue"));
p.addModifierExtension()
.setUrl("http://acme.org/modifierExtension")
.setValue(new StringType("modifierValue1"));
p.addModifierExtension()
.setUrl("http://acme.org/modifierExtension")
.setValue(new StringType("modifierValue2"));
p.addExtension()
.setUrl("http://acme.org/parentExtension")
.addExtension()
.setUrl("http://acme.org/childExtension")
.setValue(new StringType("nestedValue1"));
p.addExtension()
.setUrl("http://acme.org/parentExtension")
.addExtension()
.setUrl("http://acme.org/childExtension")
.setValue(new StringType("nestedValue2"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<Object> values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')");
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(0)).getUrl());
assertEquals("value1", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(1)).getUrl());
assertEquals("value2", ((StringType) ((Extension) values.get(1)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')");
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(0)).getUrl());
assertEquals("modifierValue1", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(1)).getUrl());
assertEquals("modifierValue2", ((StringType) ((Extension) values.get(1)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')");
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(0)).getUrl());
assertEquals("nestedValue1", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(1)).getUrl());
assertEquals("nestedValue2", ((StringType) ((Extension) values.get(1)).getValue()).getValueAsString());
}
@Test
public void testGetValuesWithWantedClass() {
Patient p = new Patient();
p.setActive(true);
p.addExtension()
.setUrl("http://acme.org/extension")
.setValue(new StringType("value"));
p.addExtension()
.setUrl("http://acme.org/otherExtension")
.setValue(new StringType("otherValue"));
p.addModifierExtension()
.setUrl("http://acme.org/modifierExtension")
.setValue(new StringType("modifierValue"));
p.addExtension()
.setUrl("http://acme.org/parentExtension")
.addExtension()
.setUrl("http://acme.org/childExtension")
.setValue(new StringType("nestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<PrimitiveType> values = ourCtx.newTerser().getValues(p, "Patient.active", PrimitiveType.class);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof BooleanType);
assertTrue(((BooleanType) values.get(0)).booleanValue());
List<Extension> extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", Extension.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringType);
assertEquals("http://acme.org/extension", extValues.get(0).getUrl());
assertEquals("value", ((StringType) (extValues.get(0).getValue())).getValueAsString());
extValues = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", Extension.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringType);
assertEquals("http://acme.org/modifierExtension", extValues.get(0).getUrl());
assertEquals("modifierValue", ((StringType) (extValues.get(0).getValue())).getValueAsString());
extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", Extension.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringType);
assertEquals("http://acme.org/childExtension", extValues.get(0).getUrl());
assertEquals("nestedValue", ((StringType) extValues.get(0).getValue()).getValueAsString());
}
@Test
public void testGetValuesWithWantedClassAndModify() {
Patient p = new Patient();
p.setActive(true);
p.addExtension()
.setUrl("http://acme.org/extension")
.setValue(new StringType("value"));
p.addExtension()
.setUrl("http://acme.org/otherExtension")
.setValue(new StringType("otherValue"));
p.addModifierExtension()
.setUrl("http://acme.org/modifierExtension")
.setValue(new StringType("modifierValue"));
p.addExtension()
.setUrl("http://acme.org/parentExtension")
.addExtension()
.setUrl("http://acme.org/childExtension")
.setValue(new StringType("nestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<PrimitiveType> values = ourCtx.newTerser().getValues(p, "Patient.active", PrimitiveType.class);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof BooleanType);
assertTrue(((BooleanType) values.get(0)).booleanValue());
((BooleanType) values.get(0)).setValue(Boolean.FALSE);
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
values = ourCtx.newTerser().getValues(p, "Patient.active", PrimitiveType.class);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof BooleanType);
assertFalse(((BooleanType) values.get(0)).booleanValue());
List<Extension> extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", Extension.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringType);
assertEquals("http://acme.org/extension", extValues.get(0).getUrl());
assertEquals("value", ((StringType) (extValues.get(0).getValue())).getValueAsString());
extValues.get(0).setValue(new StringType("modifiedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", Extension.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringType);
assertEquals("http://acme.org/extension", extValues.get(0).getUrl());
assertEquals("modifiedValue", ((StringType) (extValues.get(0).getValue())).getValueAsString());
extValues = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", Extension.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringType);
assertEquals("http://acme.org/modifierExtension", extValues.get(0).getUrl());
assertEquals("modifierValue", ((StringType) (extValues.get(0).getValue())).getValueAsString());
extValues.get(0).setValue(new StringType("modifiedModifierValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
extValues = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", Extension.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringType);
assertEquals("http://acme.org/modifierExtension", extValues.get(0).getUrl());
assertEquals("modifiedModifierValue", ((StringType) (extValues.get(0).getValue())).getValueAsString());
extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", Extension.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringType);
assertEquals("http://acme.org/childExtension", extValues.get(0).getUrl());
assertEquals("nestedValue", ((StringType) extValues.get(0).getValue()).getValueAsString());
extValues.get(0).setValue(new StringType("modifiedNestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", Extension.class);
assertEquals(1, extValues.size());
assertTrue(extValues.get(0).getValue() instanceof StringType);
assertEquals("http://acme.org/childExtension", extValues.get(0).getUrl());
assertEquals("modifiedNestedValue", ((StringType) extValues.get(0).getValue()).getValueAsString());
}
@Test
public void testGetValuesWithWantedClassAndTheCreate() {
Patient p = new Patient();
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<PrimitiveType> values = ourCtx.newTerser().getValues(p, "Patient.active", PrimitiveType.class, true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof BooleanType);
assertNull(((BooleanType) values.get(0)).getValue());
List<Extension> extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", Extension.class, true);
assertEquals(1, extValues.size());
assertEquals("http://acme.org/extension", extValues.get(0).getUrl());
assertNull(extValues.get(0).getValue());
extValues = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", Extension.class, true);
assertEquals(1, extValues.size());
assertEquals("http://acme.org/modifierExtension", extValues.get(0).getUrl());
assertNull(extValues.get(0).getValue());
extValues = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", Extension.class, true);
assertEquals(1, extValues.size());
assertEquals("http://acme.org/childExtension", extValues.get(0).getUrl());
assertNull(extValues.get(0).getValue());
}
@Test
public void testGetValuesWithTheAddExtensionAndModify() {
Patient p = new Patient();
p.setActive(true);
p.addExtension()
.setUrl("http://acme.org/extension")
.setValue(new StringType("value"));
p.addExtension()
.setUrl("http://acme.org/otherExtension")
.setValue(new StringType("otherValue"));
p.addModifierExtension()
.setUrl("http://acme.org/modifierExtension")
.setValue(new StringType("modifierValue"));
p.addExtension()
.setUrl("http://acme.org/parentExtension")
.addExtension()
.setUrl("http://acme.org/childExtension")
.setValue(new StringType("nestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<Object> values = ourCtx.newTerser().getValues(p, "Patient.active");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof PrimitiveType);
assertTrue(values.get(0) instanceof BooleanType);
assertTrue(((BooleanType) values.get(0)).booleanValue());
// No change.
values = ourCtx.newTerser().getValues(p, "Patient.active", false, true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof PrimitiveType);
assertTrue(values.get(0) instanceof BooleanType);
assertTrue(((BooleanType) values.get(0)).booleanValue());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(0)).getUrl());
assertEquals("value", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", false, true);
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(0)).getUrl());
assertEquals("value", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(1)).getUrl());
assertNull(((Extension) values.get(1)).getValue());
((Extension) values.get(1)).setValue(new StringType("addedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(1)).getUrl());
assertEquals("addedValue", ((StringType) ((Extension) values.get(1)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(0)).getUrl());
assertEquals("modifierValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", false, true);
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(0)).getUrl());
assertEquals("modifierValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(1)).getUrl());
assertNull(((Extension) values.get(1)).getValue());
((Extension) values.get(1)).setValue(new StringType("addedModifierValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(1)).getUrl());
assertEquals("addedModifierValue", ((StringType) ((Extension) values.get(1)).getValue()).getValueAsString());
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(0)).getUrl());
assertEquals("nestedValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", false, true);
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(0)).getUrl());
assertEquals("nestedValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(1)).getUrl());
assertNull(((Extension) values.get(1)).getValue());
((Extension) values.get(1)).setValue(new StringType("addedNestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(1)).getUrl());
assertEquals("addedNestedValue", ((StringType) ((Extension) values.get(1)).getValue()).getValueAsString());
}
@Test
public void testGetValuesWithTheCreate() {
Patient p = new Patient();
List<Object> values = ourCtx.newTerser().getValues(p, "Patient.active", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof PrimitiveType);
assertTrue(values.get(0) instanceof BooleanType);
assertNull(((BooleanType) values.get(0)).getValue());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(0)).getUrl());
assertNull(((Extension) values.get(0)).getValue());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(0)).getUrl());
assertNull(((Extension) values.get(0)).getValue());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(0)).getUrl());
assertNull(((Extension) values.get(0)).getValue());
}
@Test
public void testGetValuesWithTheCreateAndTheAddExtensionAndModify() {
Patient p = new Patient();
p.setActive(true);
p.addExtension()
.setUrl("http://acme.org/extension")
.setValue(new StringType("value"));
p.addExtension()
.setUrl("http://acme.org/otherExtension")
.setValue(new StringType("otherValue"));
p.addModifierExtension()
.setUrl("http://acme.org/modifierExtension")
.setValue(new StringType("modifierValue"));
p.addExtension()
.setUrl("http://acme.org/parentExtension")
.addExtension()
.setUrl("http://acme.org/childExtension")
.setValue(new StringType("nestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<Object> values = ourCtx.newTerser().getValues(p, "Patient.active");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof PrimitiveType);
assertTrue(values.get(0) instanceof BooleanType);
assertTrue(((BooleanType) values.get(0)).booleanValue());
// No change.
values = ourCtx.newTerser().getValues(p, "Patient.active", true, true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof PrimitiveType);
assertTrue(values.get(0) instanceof BooleanType);
assertTrue(((BooleanType) values.get(0)).booleanValue());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(0)).getUrl());
assertEquals("value", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", true, true);
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(0)).getUrl());
assertEquals("value", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(1)).getUrl());
assertNull(((Extension) values.get(1)).getValue());
((Extension) values.get(1)).setValue(new StringType("addedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(1)).getUrl());
assertEquals("addedValue", ((StringType) ((Extension) values.get(1)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(0)).getUrl());
assertEquals("modifierValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", true, true);
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(0)).getUrl());
assertEquals("modifierValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(1)).getUrl());
assertNull(((Extension) values.get(1)).getValue());
((Extension) values.get(1)).setValue(new StringType("addedModifierValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(1)).getUrl());
assertEquals("addedModifierValue", ((StringType) ((Extension) values.get(1)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')");
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(0)).getUrl());
assertEquals("nestedValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", true, true);
assertEquals(2, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(0)).getUrl());
assertEquals("nestedValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(1)).getUrl());
assertNull(((Extension) values.get(1)).getValue());
((Extension) values.get(1)).setValue(new StringType("addedNestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
assertTrue(values.get(1) instanceof IBaseExtension);
assertTrue(values.get(1) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(1)).getUrl());
assertEquals("addedNestedValue", ((StringType) ((Extension) values.get(1)).getValue()).getValueAsString());
}
@Test
public void testGetValuesWithTheCreateAndNoOverwrite() {
Patient p = new Patient();
p.setActive(true);
p.addExtension()
.setUrl("http://acme.org/extension")
.setValue(new StringType("value"));
p.addExtension()
.setUrl("http://acme.org/otherExtension")
.setValue(new StringType("otherValue"));
p.addModifierExtension()
.setUrl("http://acme.org/modifierExtension")
.setValue(new StringType("modifierValue"));
p.addExtension()
.setUrl("http://acme.org/parentExtension")
.addExtension()
.setUrl("http://acme.org/childExtension")
.setValue(new StringType("nestedValue"));
System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p));
List<Object> values = ourCtx.newTerser().getValues(p, "Patient.active", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof PrimitiveType);
assertTrue(values.get(0) instanceof BooleanType);
assertTrue(((BooleanType) values.get(0)).booleanValue());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/extension')", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/extension", ((Extension) values.get(0)).getUrl());
assertEquals("value", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.modifierExtension('http://acme.org/modifierExtension')", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/modifierExtension", ((Extension) values.get(0)).getUrl());
assertEquals("modifierValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
values = ourCtx.newTerser().getValues(p, "Patient.extension('http://acme.org/parentExtension').extension('http://acme.org/childExtension')", true);
assertEquals(1, values.size());
assertTrue(values.get(0) instanceof IBaseExtension);
assertTrue(values.get(0) instanceof Extension);
assertEquals("http://acme.org/childExtension", ((Extension) values.get(0)).getUrl());
assertEquals("nestedValue", ((StringType) ((Extension) values.get(0)).getValue()).getValueAsString());
}
@Test
public void testVisitWithModelVisitor2() {
IModelVisitor2 visitor = mock(IModelVisitor2.class);

View File

@ -116,6 +116,15 @@
<![CDATA[<a href="https://github.com/hapifhir/hapi-fhir-jpaserver-starter/blob/master/src/main/java/ca/uhn/fhir/jpa/demo/FhirServerConfig.java#L62">the example project</a>]]>
if they are not already.
</action>
<action type="add">
The FhirTerser <![CDATA[<code>getValues(...)</code>]]> methods have been overloaded. The terser can now be
used to create a null-valued element where none exists. Additionally, the terser can now add a null-valued
extension where one or more such extensions already exist. These changes allow better population of FHIR
elements provided an arbitrary FHIR path.
</action>
<action type="fix">
The FhirTerser <![CDATA[<code>getValues(...)</code>]]> methods were not properly handling modifier
extensions for verions of FHIR prior to DSTU3. This has been corrected.
<action type="fix">
When updating resources in the JPA server, a bug caused index table entries to be refreshed
sometimes even though the index value hadn't changed. This issue did not cause incorrect search