diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java index 082daaf122c..44550187d2b 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java @@ -230,72 +230,100 @@ public class FhirTerser { } // DSTU2 - List extensionDts = Collections.emptyList(); - if (theCurrentObj instanceof ISupportsUndeclaredExtensions) { - extensionDts = ((ISupportsUndeclaredExtensions) theCurrentObj).getUndeclaredExtensionsByUrl(extensionUrl); + if (myContext.getVersion().getVersion().isOlderThan(FhirVersionEnum.DSTU3)) { + List extensionDts = Collections.emptyList(); + if (theCurrentObj instanceof ISupportsUndeclaredExtensions) { + extensionDts = ((ISupportsUndeclaredExtensions) theCurrentObj).getUndeclaredExtensionsByUrl(extensionUrl); - if (extensionDts.isEmpty() && theCreate) { - // FIXME: Add a new extension with extensionUrl and null value. - // FIXME: Discern between extensions and modifier extensions. + if (extensionDts.isEmpty() && theCreate) { + // We assume the extension is not a modifier extension. + extensionDts = new ArrayList<>(); // Implementation of ISupportsUndeclaredExtensions.getUndeclaredExtensionsByUrl(...) returns unmodifiable list. + ExtensionDt extensionDt = ((ISupportsUndeclaredExtensions) theCurrentObj).addUndeclaredExtension(false, extensionUrl); + extensionDts.add(extensionDt); + } + } else if (theCurrentObj instanceof IBaseExtension) { + extensionDts = ((IBaseExtension) theCurrentObj).getExtension(); + + if (extensionDts.isEmpty() && theCreate) { + // We assume the extension is not a modifier extension. + ExtensionDt extensionDt = new ExtensionDt(); + extensionDt.setUrl(extensionUrl); + ((IBaseExtension) theCurrentObj).getExtension().add(extensionDt); + extensionDts.add(extensionDt); + } } - } else if (theCurrentObj instanceof IBaseExtension) { - extensionDts = ((IBaseExtension) theCurrentObj).getExtension(); - if (extensionDts.isEmpty() && theCreate) { - // FIXME: Add a new extension with extensionUrl and null value. + for (ExtensionDt next : extensionDts) { + if (theWantedClass.isAssignableFrom(next.getClass())) { + retVal.add((T) next); + } + } + } else { + // DSTU3+ + final String extensionUrlForLambda = extensionUrl; + List extensions = Collections.emptyList(); + if (theCurrentObj instanceof IBaseHasExtensions) { + extensions = ((IBaseHasExtensions) theCurrentObj).getExtension() + .stream() + .filter(t -> t.getUrl().equals(extensionUrlForLambda)) + .collect(Collectors.toList()); + + if (extensions.isEmpty() && theCreate) { + IBaseExtension extension = ((IBaseHasExtensions) theCurrentObj).addExtension(); + extension.setUrl(extensionUrl); + extensions.add(extension); + } + } + + for (IBaseExtension next : extensions) { + if (theWantedClass.isAssignableFrom(next.getClass())) { + retVal.add((T) next); + } } } - for (ExtensionDt next : extensionDts) { - if (theWantedClass.isAssignableFrom(next.getClass())) { - retVal.add((T) next); + if (theSubList.size() > 1) { + List values = retVal; + retVal = new ArrayList<>(); + for (T nextElement : values) { + BaseRuntimeElementCompositeDefinition nextChildDef = (BaseRuntimeElementCompositeDefinition) myContext.getElementDefinition((Class) nextElement.getClass()); + List foundValues = getValues(nextChildDef, nextElement, theSubList.subList(1, theSubList.size()), theWantedClass); + 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); + } + // DSTU3+ - final String extensionUrlForLambda = extensionUrl; - List extensions = Collections.emptyList(); - if (theCurrentObj instanceof IBaseHasExtensions) { - extensions = ((IBaseHasExtensions) theCurrentObj).getExtension() - .stream() - .filter(t -> t.getUrl().equals(extensionUrlForLambda)) - .distinct() - .collect(Collectors.toList()); + if (myContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.DSTU3)) { + final String extensionUrlForLambda = extensionUrl; + List extensions = Collections.emptyList(); - if (extensions.isEmpty() && theCreate) { - IBaseExtension extension = ((IBaseHasExtensions) theCurrentObj).addExtension(); - extension.setUrl(extensionUrl); - extensions.add(extension); + if (theCurrentObj instanceof IBaseHasModifierExtensions) { + extensions = ((IBaseHasModifierExtensions) theCurrentObj).getModifierExtension() + .stream() + .filter(t -> t.getUrl().equals(extensionUrlForLambda)) + .collect(Collectors.toList()); + + if (extensions.isEmpty() && theCreate) { + IBaseExtension modifierExtension = ((IBaseHasModifierExtensions) theCurrentObj).addModifierExtension(); + modifierExtension.setUrl(extensionUrl); + extensions.add(modifierExtension); + } } - } -// List modifierExtensions = Collections.emptyList(); -// if (theCurrentObj instanceof IBaseHasModifierExtensions) { -// modifierExtensions = ((IBaseHasModifierExtensions) theCurrentObj).getModifierExtension() -// .stream() -// .filter(t -> t.getUrl().equals(extensionUrlForLambda)) -// .collect(Collectors.toList()); -// -// if (modifierExtensions.isEmpty() && theCreate) { -// IBaseExtension modifierExtension = ((IBaseHasModifierExtensions) theCurrentObj).addModifierExtension(); -// modifierExtension.setUrl(extensionUrl); -// modifierExtensions.add(modifierExtension); -// } -// } -// -// List allExtensions = Stream.of(extensions, modifierExtensions) -// .flatMap(Collection::stream) -// .distinct() -// .collect(Collectors.toList()); -// -// for (IBaseExtension next : allExtensions) { -// if (theWantedClass.isAssignableFrom(next.getClass())) { -// retVal.add((T) next); -// } -// } - for (IBaseExtension next : extensions) { - if (theWantedClass.isAssignableFrom(next.getClass())) { - retVal.add((T) next); + for (IBaseExtension next : extensions) { + if (theWantedClass.isAssignableFrom(next.getClass())) { + retVal.add((T) next); + } } } @@ -360,24 +388,74 @@ public class FhirTerser { return retVal; } + /** + * Returns values stored in an element identified by its path. The list of values is of + * type {@link Object}. + * + *

Note: this method does not support creation of null-valued modifier extensions for + * versions of FHIR prior to DSTU3.

+ * + * @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 getValues(IBaseResource theResource, String thePath) { Class 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}. + * + *

Note: this method does not support creation of null-valued modifier extensions for + * versions of FHIR prior to DSTU3.

+ * + * @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 true, the terser will create a null-valued element where none exists. + * @return A list of values of type {@link Object}. + */ public List getValues(IBaseResource theResource, String thePath, boolean theCreate) { Class 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 theWantedClass. + * + *

Note: this method does not support creation of null-valued modifier extensions for + * versions of FHIR prior to DSTU3.

+ * + * @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 Type declared by theWantedClass + * @return A list of values of type theWantedClass. + */ public List getValues(IBaseResource theResource, String thePath, Class theWantedClass) { RuntimeResourceDefinition def = myContext.getResourceDefinition(theResource); List 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 theWantedClass. + * + *

Note: this method does not support creation of null-valued modifier extensions for + * versions of FHIR prior to DSTU3.

+ * + * @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 true, the terser will create a null-valued element where none exists. + * @param Type declared by theWantedClass + * @return A list of values of type theWantedClass. + */ public List getValues(IBaseResource theResource, String thePath, Class theWantedClass, boolean theCreate) { RuntimeResourceDefinition def = myContext.getResourceDefinition(theResource); List parts = parsePath(def, thePath); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/util/FhirTerserDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/util/FhirTerserDstu2Test.java index d4816152d0a..0139b2d6b13 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/util/FhirTerserDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/util/FhirTerserDstu2Test.java @@ -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 { @@ -180,6 +178,215 @@ public class FhirTerserDstu2Test { assertEquals(1, refs.size()); 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")); + + System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p)); + + List 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()); + } + + @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")); + + System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p)); + + List 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()); + } + + @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")); + + System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p)); + + List 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()); + } + + @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")); + + System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p)); + + List 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 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()); + } + + @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")); + + System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p)); + + List 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 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()); + } + + @Test + public void testGetValuesWithWantedClassAndTheCreate() { + Patient p = new Patient(); + + System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p)); + + List 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 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()); + } + + @Test + public void testGetValuesWithTheCreate() { + Patient p = new Patient(); + + System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p)); + + List 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()); + } + + @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")); + + System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p)); + + List 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')"); + 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()); + } @Test public void testVisitWithModelVisitor2() { diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/util/FhirTerserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/util/FhirTerserDstu3Test.java index 2cdfcba8030..bf0ffde4ee0 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/util/FhirTerserDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/util/FhirTerserDstu3Test.java @@ -179,9 +179,9 @@ public class FhirTerserDstu3Test { p.addExtension() .setUrl("http://acme.org/otherExtension") .setValue(new StringType("otherValue")); -// p.addModifierExtension() -// .setUrl("http://acme.org/extension") -// .setValue(new StringType("modifierValue")); + p.addModifierExtension() + .setUrl("http://acme.org/modifierExtension") + .setValue(new StringType("modifierValue")); System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p)); @@ -198,12 +198,12 @@ public class FhirTerserDstu3Test { 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/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()); } @Test @@ -216,6 +216,9 @@ public class FhirTerserDstu3Test { p.addExtension() .setUrl("http://acme.org/otherExtension") .setValue(new StringType("otherValue")); + p.addModifierExtension() + .setUrl("http://acme.org/modifierExtension") + .setValue(new StringType("modifierValue")); System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p)); @@ -252,6 +255,68 @@ public class FhirTerserDstu3Test { 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()); + } + + @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")); + + System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p)); + + List 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()); } @Test @@ -264,6 +329,9 @@ public class FhirTerserDstu3Test { p.addExtension() .setUrl("http://acme.org/otherExtension") .setValue(new StringType("otherValue")); + p.addModifierExtension() + .setUrl("http://acme.org/modifierExtension") + .setValue(new StringType("modifierValue")); System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p)); @@ -277,6 +345,12 @@ public class FhirTerserDstu3Test { 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()); } @Test @@ -289,6 +363,9 @@ public class FhirTerserDstu3Test { p.addExtension() .setUrl("http://acme.org/otherExtension") .setValue(new StringType("otherValue")); + p.addModifierExtension() + .setUrl("http://acme.org/modifierExtension") + .setValue(new StringType("modifierValue")); System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p)); @@ -321,6 +398,22 @@ public class FhirTerserDstu3Test { 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()); } @Test @@ -338,6 +431,11 @@ public class FhirTerserDstu3Test { 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()); } @Test @@ -358,6 +456,13 @@ public class FhirTerserDstu3Test { 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()); } @Test @@ -370,6 +475,9 @@ public class FhirTerserDstu3Test { p.addExtension() .setUrl("http://acme.org/otherExtension") .setValue(new StringType("otherValue")); + p.addModifierExtension() + .setUrl("http://acme.org/modifierExtension") + .setValue(new StringType("modifierValue")); System.out.println(ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(p)); @@ -385,6 +493,13 @@ public class FhirTerserDstu3Test { 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()); } @Test