Forward port #688

Merge branch 'master' into hapi3_refactor
This commit is contained in:
James 2017-08-13 09:53:17 -04:00
commit 7c39a47852
26 changed files with 1518 additions and 777 deletions

View File

@ -301,11 +301,16 @@ public class IdDt extends UriDt implements /*IPrimitiveDatatype<String>, */IIdTy
b.append(myResourceType);
}
if (b.length() > 0) {
if (b.length() > 0 && isNotBlank(myUnqualifiedId)) {
b.append('/');
}
if (isNotBlank(myUnqualifiedId)) {
b.append(myUnqualifiedId);
} else if (isNotBlank(myUnqualifiedVersionId)) {
b.append('/');
}
if (isNotBlank(myUnqualifiedVersionId)) {
b.append('/');
b.append(Constants.PARAM_HISTORY);
@ -522,8 +527,22 @@ public class IdDt extends UriDt implements /*IPrimitiveDatatype<String>, */IIdTy
int typeIndex = theValue.lastIndexOf('/', idIndex - 1);
if (typeIndex == -1) {
myResourceType = theValue.substring(0, idIndex);
} else {
if (typeIndex > 0 && '/' == theValue.charAt(typeIndex - 1)) {
typeIndex = theValue.indexOf('/', typeIndex + 1);
}
if (typeIndex >= idIndex) {
// e.g. http://example.org/foo
// 'foo' was the id but we're making that the resource type. Nullify the id part because we don't have an id.
// Also set null value to the super.setValue() and enable myHaveComponentParts so it forces getValue() to properly
// recreate the url
myResourceType = myUnqualifiedId;
myUnqualifiedId = null;
super.setValue(null);
myHaveComponentParts = true;
} else {
myResourceType = theValue.substring(typeIndex + 1, idIndex);
}
if (typeIndex > 4) {
myBaseUrl = theValue.substring(0, typeIndex);

View File

@ -28,20 +28,20 @@ public interface IBaseBundle extends IBaseResource {
* link.type field to indicate that the given link is for
* the next page of results.
*/
public static final String LINK_NEXT = "next";
String LINK_NEXT = "next";
/**
* Constant for links provided in the bundle. This constant is used in the
* link.type field to indicate that the given link is for
* the previous page of results.
*/
public static final String LINK_PREV = "previous";
String LINK_PREV = "previous";
/**
* Constant for links provided in the bundle. This constant is used in the
* link.type field to indicate that the given link is for
* this bundle.
*/
public static final String LINK_SELF = "self";
String LINK_SELF = "self";
}

View File

@ -1681,7 +1681,6 @@ public class GenericClient extends BaseClient implements IGenericClient {
@Override
public OUTPUT execute() {
Validate.notNull(myReturnBundleType, "Return bundle type mustbe specified");
Map<String, List<String>> params = getParamMap();

View File

@ -197,7 +197,15 @@ public class ResourceLink implements Serializable {
public void setTargetResourceUrl(IIdType theTargetResourceUrl) {
Validate.isTrue(theTargetResourceUrl.hasBaseUrl());
Validate.isTrue(theTargetResourceUrl.hasResourceType());
Validate.isTrue(theTargetResourceUrl.hasIdPart());
if (theTargetResourceUrl.hasIdPart()) {
// do nothing
} else {
// Must have set an url like http://example.org/something
// We treat 'something' as the resource type because of fix for #659. Prior to #659 fix, 'something' was
// treated as the id and 'example.org' was treated as the resource type
// TODO: log a warning?
}
myTargetResourceType = theTargetResourceUrl.getResourceType();
myTargetResourceUrl = theTargetResourceUrl.getValue();

View File

@ -13,6 +13,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.*;
import org.apache.http.entity.*;
import org.hl7.fhir.instance.model.api.IBaseBundle;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import org.junit.*;
@ -2059,7 +2060,7 @@ public class ResourceProviderDstu2Test extends BaseResourceProviderDstu2Test {
Observation o = new Observation();
o.getCode().setText("testSearchWithInvalidSort");
myObservationDao.create(o, mySrd);
ourClient
IBaseBundle bundle = ourClient
.search()
.forResource(Observation.class)
.sort().ascending(Observation.CODE_VALUE_QUANTITY) // composite sort not supported yet

View File

@ -84,6 +84,37 @@ public class IdDtTest {
assertEquals("foo/_history/2", id.withVersion("2").getValue());
}
@Test
public void testBaseUrlFoo1() {
IdDt id = new IdDt("http://my.org/foo");
assertEquals("http://my.org/foo", id.getValueAsString());
assertEquals(null, id.getIdPart());
assertEquals("foo", id.toUnqualified().getValueAsString());
assertEquals("foo", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals("foo", id.getResourceType());
assertEquals("http://my.org", id.getBaseUrl());
assertEquals("Patient", id.withResourceType("Patient").getValue());
assertEquals("http://foo/Patient", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("http://my.org/foo//_history/2", id.withVersion("2").getValue());
}
@Test
public void testBaseUrlFoo2() {
IdDt id = new IdDt("http://my.org/a/b/c/foo");
assertEquals("http://my.org/a/b/c/foo", id.getValueAsString());
assertEquals("foo", id.getIdPart());
assertEquals("c/foo", id.toUnqualified().getValueAsString());
assertEquals("c/foo", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals("c", id.getResourceType());
assertEquals("http://my.org/a/b", id.getBaseUrl());
assertEquals("Patient/foo", id.withResourceType("Patient").getValue());
assertEquals("http://foo/Patient/foo", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("http://my.org/a/b/c/foo/_history/2", id.withVersion("2").getValue());
}
@Test
public void testDetectIsIdPartValid() {

View File

@ -358,11 +358,16 @@ public final class IdType extends UriType implements IPrimitiveType<String>, IId
b.append(myResourceType);
}
if (b.length() > 0) {
if (b.length() > 0 && isNotBlank(myUnqualifiedId)) {
b.append('/');
}
if (isNotBlank(myUnqualifiedId)) {
b.append(myUnqualifiedId);
} else if (isNotBlank(myUnqualifiedVersionId)) {
b.append('/');
}
if (isNotBlank(myUnqualifiedVersionId)) {
b.append('/');
b.append("_history");
@ -553,8 +558,22 @@ public final class IdType extends UriType implements IPrimitiveType<String>, IId
int typeIndex = theValue.lastIndexOf('/', idIndex - 1);
if (typeIndex == -1) {
myResourceType = theValue.substring(0, idIndex);
} else {
if (typeIndex > 0 && '/' == theValue.charAt(typeIndex - 1)) {
typeIndex = theValue.indexOf('/', typeIndex + 1);
}
if (typeIndex >= idIndex) {
// e.g. http://example.org/foo
// 'foo' was the id but we're making that the resource type. Nullify the id part because we don't have an id.
// Also set null value to the super.setValue() and enable myHaveComponentParts so it forces getValue() to properly
// recreate the url
myResourceType = myUnqualifiedId;
myUnqualifiedId = null;
super.setValue(null);
myHaveComponentParts = true;
} else {
myResourceType = theValue.substring(typeIndex + 1, idIndex);
}
if (typeIndex > 4) {
myBaseUrl = theValue.substring(0, typeIndex);

View File

@ -92,6 +92,37 @@ public class IdTypeDstu2_1Test {
assertEquals("foo/_history/2", id.withVersion("2").getValue());
}
@Test
public void testBaseUrlFoo1() {
IdType id = new IdType("http://my.org/foo");
assertEquals("http://my.org/foo", id.getValueAsString());
assertEquals(null, id.getIdPart());
assertEquals("foo", id.toUnqualified().getValueAsString());
assertEquals("foo", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals("foo", id.getResourceType());
assertEquals("http://my.org", id.getBaseUrl());
assertEquals("Patient", id.withResourceType("Patient").getValue());
assertEquals("http://foo/Patient", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("http://my.org/foo//_history/2", id.withVersion("2").getValue());
}
@Test
public void testBaseUrlFoo2() {
IdType id = new IdType("http://my.org/a/b/c/foo");
assertEquals("http://my.org/a/b/c/foo", id.getValueAsString());
assertEquals("foo", id.getIdPart());
assertEquals("c/foo", id.toUnqualified().getValueAsString());
assertEquals("c/foo", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals("c", id.getResourceType());
assertEquals("http://my.org/a/b", id.getBaseUrl());
assertEquals("Patient/foo", id.withResourceType("Patient").getValue());
assertEquals("http://foo/Patient/foo", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("http://my.org/a/b/c/foo/_history/2", id.withVersion("2").getValue());
}
@Test
public void testDetectLocal() {

View File

@ -1873,6 +1873,26 @@ public class JsonParserDstu2_1Test {
Assert.assertThat(message, containsString("contained"));
}
@Test
public void testBaseUrlFooResourceCorrectlySerializedInExtensionValueReference() {
String refVal = "http://my.org/FooBar";
Patient fhirPat = new Patient();
fhirPat.addExtension().setUrl("x1").setValue(new Reference(refVal));
IParser parser = ourCtx.newJsonParser();
String output = parser.encodeResourceToString(fhirPat);
System.out.println("output: " + output);
// Deserialize then check that valueReference value is still correct
fhirPat = parser.parseResource(Patient.class, output);
List<Extension> extlst = fhirPat.getExtensionsByUrl("x1");
Assert.assertEquals(1, extlst.size());
Assert.assertEquals(refVal, ((Reference) extlst.get(0).getValue()).getReference());
}
@AfterClass

View File

@ -1,21 +1,22 @@
package ca.uhn.fhir.parser;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.parser.FooMessageHeaderWithExplicitField.FooMessageSourceComponent;
import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation;
import ca.uhn.fhir.parser.PatientWithCustomCompositeExtension.FooParentExtension;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.util.TestUtil;
import com.google.common.collect.Sets;
import org.apache.commons.io.IOUtils;
import org.hamcrest.collection.IsEmptyCollection;
import org.hamcrest.core.StringContains;
import org.hamcrest.text.StringContainsInOrder;
import org.hl7.fhir.dstu2016may.model.*;
import org.hl7.fhir.dstu2016may.model.Address.AddressUse;
import org.hl7.fhir.dstu2016may.model.*;
import org.hl7.fhir.dstu2016may.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.dstu2016may.model.Bundle.BundleType;
import org.hl7.fhir.dstu2016may.model.ContactPoint.ContactPointSystem;
@ -32,20 +33,20 @@ import org.junit.*;
import org.mockito.ArgumentCaptor;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.Input;
import org.xmlunit.diff.*;
import org.xmlunit.diff.ComparisonControllers;
import org.xmlunit.diff.DefaultNodeMatcher;
import org.xmlunit.diff.Diff;
import org.xmlunit.diff.ElementSelectors;
import com.google.common.collect.Sets;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.parser.FooMessageHeaderWithExplicitField.FooMessageSourceComponent;
import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation;
import ca.uhn.fhir.parser.PatientWithCustomCompositeExtension.FooParentExtension;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.util.TestUtil;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
public class XmlParserDstu2_1Test {
private static FhirContext ourCtx = FhirContext.forDstu2_1();
@ -2662,6 +2663,26 @@ public class XmlParserDstu2_1Test {
}
@Test
public void testBaseUrlFooResourceCorrectlySerializedInExtensionValueReference() {
String refVal = "http://my.org/FooBar";
Patient fhirPat = new Patient();
fhirPat.addExtension().setUrl("x1").setValue(new Reference(refVal));
IParser parser = ourCtx.newXmlParser();
String output = parser.encodeResourceToString(fhirPat);
System.out.println("output: " + output);
// Deserialize then check that valueReference value is still correct
fhirPat = parser.parseResource(Patient.class, output);
List<Extension> extlst = fhirPat.getExtensionsByUrl("x1");
Assert.assertEquals(1, extlst.size());
Assert.assertEquals(refVal, ((Reference) extlst.get(0).getValue()).getReference());
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();

View File

@ -136,7 +136,7 @@ public class BaseResourceReferenceDtTest {
new ResourceReferenceDt("http://foo/123123").loadResource(client);
fail();
} catch (DataFormatException e) {
assertEquals("Unknown resource name \"foo\" (this name is not known in FHIR version \"DSTU2\")", e.getMessage());
assertEquals("Unknown resource name \"123123\" (this name is not known in FHIR version \"DSTU2\")", e.getMessage());
}
try {

View File

@ -1948,4 +1948,24 @@ public class JsonParserDstu2Test {
assertEquals("myName", ((StringDt) newPatient.getUndeclaredExtensionsByUrl("http://www.example.com/petname").get(0).getValue()).getValue());
}
@Test
public void testBaseUrlFooResourceCorrectlySerializedInExtensionValueReference() {
String refVal = "http://my.org/FooBar";
Patient fhirPat = new Patient();
fhirPat.addUndeclaredExtension(false, "x1").setValue(new ResourceReferenceDt(refVal));
IParser parser = ourCtx.newJsonParser();
String output = parser.encodeResourceToString(fhirPat);
System.out.println("output: " + output);
// Deserialize then check that valueReference value is still correct
fhirPat = parser.parseResource(Patient.class, output);
List<ExtensionDt> extlst = fhirPat.getUndeclaredExtensionsByUrl("x1");
Assert.assertEquals(1, extlst.size());
Assert.assertEquals(refVal, ((ResourceReferenceDt) extlst.get(0).getValue()).getReference().getValue());
}
}

View File

@ -17,6 +17,7 @@ import org.hamcrest.text.StringContainsInOrder;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
@ -2790,6 +2791,26 @@ public class XmlParserDstu2Test {
}
@Test
public void testBaseUrlFooResourceCorrectlySerializedInExtensionValueReference() {
String refVal = "http://my.org/FooBar";
Patient fhirPat = new Patient();
fhirPat.addUndeclaredExtension(false, "x1").setValue(new ResourceReferenceDt(refVal));
IParser parser = ourCtx.newXmlParser();
String output = parser.encodeResourceToString(fhirPat);
System.out.println("output: " + output);
// Deserialize then check that valueReference value is still correct
fhirPat = parser.parseResource(Patient.class, output);
List<ExtensionDt> extlst = fhirPat.getUndeclaredExtensionsByUrl("x1");
Assert.assertEquals(1, extlst.size());
Assert.assertEquals(refVal, ((ResourceReferenceDt) extlst.get(0).getValue()).getReference().getValue());
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();

View File

@ -351,11 +351,16 @@ public final class IdType extends UriType implements IPrimitiveType<String>, IId
b.append(myResourceType);
}
if (b.length() > 0) {
if (b.length() > 0 && isNotBlank(myUnqualifiedId)) {
b.append('/');
}
if (isNotBlank(myUnqualifiedId)) {
b.append(myUnqualifiedId);
} else if (isNotBlank(myUnqualifiedVersionId)) {
b.append('/');
}
if (isNotBlank(myUnqualifiedVersionId)) {
b.append('/');
b.append("_history");
@ -546,8 +551,22 @@ public final class IdType extends UriType implements IPrimitiveType<String>, IId
int typeIndex = theValue.lastIndexOf('/', idIndex - 1);
if (typeIndex == -1) {
myResourceType = theValue.substring(0, idIndex);
} else {
if (typeIndex > 0 && '/' == theValue.charAt(typeIndex - 1)) {
typeIndex = theValue.indexOf('/', typeIndex + 1);
}
if (typeIndex >= idIndex) {
// e.g. http://example.org/foo
// 'foo' was the id but we're making that the resource type. Nullify the id part because we don't have an id.
// Also set null value to the super.setValue() and enable myHaveComponentParts so it forces getValue() to properly
// recreate the url
myResourceType = myUnqualifiedId;
myUnqualifiedId = null;
super.setValue(null);
myHaveComponentParts = true;
} else {
myResourceType = theValue.substring(typeIndex + 1, idIndex);
}
if (typeIndex > 4) {
myBaseUrl = theValue.substring(0, typeIndex);

View File

@ -92,6 +92,38 @@ public class IdTypeDstu3Test {
assertEquals("foo/_history/2", id.withVersion("2").getValue());
}
@Test
public void testBaseUrlFoo1() {
IdType id = new IdType("http://my.org/foo");
assertEquals("http://my.org/foo", id.getValueAsString());
assertEquals(null, id.getIdPart());
assertEquals("foo", id.toUnqualified().getValueAsString());
assertEquals("foo", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals("foo", id.getResourceType());
assertEquals("http://my.org", id.getBaseUrl());
assertEquals("Patient", id.withResourceType("Patient").getValue());
assertEquals("http://foo/Patient", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("http://my.org/foo//_history/2", id.withVersion("2").getValue());
}
@Test
public void testBaseUrlFoo2() {
IdType id = new IdType("http://my.org/a/b/c/foo");
assertEquals("http://my.org/a/b/c/foo", id.getValueAsString());
assertEquals("foo", id.getIdPart());
assertEquals("c/foo", id.toUnqualified().getValueAsString());
assertEquals("c/foo", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals("c", id.getResourceType());
assertEquals("http://my.org/a/b", id.getBaseUrl());
assertEquals("Patient/foo", id.withResourceType("Patient").getValue());
assertEquals("http://foo/Patient/foo", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("http://my.org/a/b/c/foo/_history/2", id.withVersion("2").getValue());
}
@Test
public void testDetectLocal() {
IdType id;

View File

@ -2414,6 +2414,26 @@ public class JsonParserDstu3Test {
assertTrue(result.isSuccessful());
}
@Test
public void testBaseUrlFooResourceCorrectlySerializedInExtensionValueReference() {
String refVal = "http://my.org/FooBar";
Patient fhirPat = new Patient();
fhirPat.addExtension().setUrl("x1").setValue(new Reference(refVal));
IParser parser = ourCtx.newJsonParser();
String output = parser.encodeResourceToString(fhirPat);
System.out.println("output: " + output);
// Deserialize then check that valueReference value is still correct
fhirPat = parser.parseResource(Patient.class, output);
List<Extension> extlst = fhirPat.getExtensionsByUrl("x1");
Assert.assertEquals(1, extlst.size());
Assert.assertEquals(refVal, ((Reference) extlst.get(0).getValue()).getReference());
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();

View File

@ -3334,6 +3334,26 @@ public class XmlParserDstu3Test {
}
@Test
public void testBaseUrlFooResourceCorrectlySerializedInExtensionValueReference() {
String refVal = "http://my.org/FooBar";
Patient fhirPat = new Patient();
fhirPat.addExtension().setUrl("x1").setValue(new Reference(refVal));
IParser parser = ourCtx.newXmlParser();
String output = parser.encodeResourceToString(fhirPat);
System.out.println("output: " + output);
// Deserialize then check that valueReference value is still correct
fhirPat = parser.parseResource(Patient.class, output);
List<Extension> extlst = fhirPat.getExtensionsByUrl("x1");
Assert.assertEquals(1, extlst.size());
Assert.assertEquals(refVal, ((Reference) extlst.get(0).getValue()).getReference());
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();

View File

@ -380,11 +380,16 @@ public final class IdType extends UriType implements IPrimitiveType<String>, IId
b.append(myResourceType);
}
if (b.length() > 0) {
if (b.length() > 0 && isNotBlank(myUnqualifiedId)) {
b.append('/');
}
if (isNotBlank(myUnqualifiedId)) {
b.append(myUnqualifiedId);
} else if (isNotBlank(myUnqualifiedVersionId)) {
b.append('/');
}
if (isNotBlank(myUnqualifiedVersionId)) {
b.append('/');
b.append("_history");
@ -570,8 +575,22 @@ public final class IdType extends UriType implements IPrimitiveType<String>, IId
int typeIndex = theValue.lastIndexOf('/', idIndex - 1);
if (typeIndex == -1) {
myResourceType = theValue.substring(0, idIndex);
} else {
if (typeIndex > 0 && '/' == theValue.charAt(typeIndex - 1)) {
typeIndex = theValue.indexOf('/', typeIndex + 1);
}
if (typeIndex >= idIndex) {
// e.g. http://example.org/foo
// 'foo' was the id but we're making that the resource type. Nullify the id part because we don't have an id.
// Also set null value to the super.setValue() and enable myHaveComponentParts so it forces getValue() to properly
// recreate the url
myResourceType = myUnqualifiedId;
myUnqualifiedId = null;
super.setValue(null);
myHaveComponentParts = true;
} else {
myResourceType = theValue.substring(typeIndex + 1, idIndex);
}
if (typeIndex > 4) {
myBaseUrl = theValue.substring(0, typeIndex);

View File

@ -84,6 +84,54 @@ public class IdTypeTest {
}
@Test
public void testNormal() {
IdType id = new IdType("foo");
assertEquals("foo", id.getValueAsString());
assertEquals("foo", id.getIdPart());
assertEquals("foo", id.toUnqualified().getValueAsString());
assertEquals("foo", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals(null, id.getResourceType());
assertEquals(null, id.getBaseUrl());
assertEquals("Patient/foo", id.withResourceType("Patient").getValue());
assertEquals("http://foo/Patient/foo", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("foo/_history/2", id.withVersion("2").getValue());
}
@Test
public void testBaseUrlFoo1() {
IdType id = new IdType("http://my.org/foo");
assertEquals("http://my.org/foo", id.getValueAsString());
assertEquals(null, id.getIdPart());
assertEquals("foo", id.toUnqualified().getValueAsString());
assertEquals("foo", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals("foo", id.getResourceType());
assertEquals("http://my.org", id.getBaseUrl());
assertEquals("Patient", id.withResourceType("Patient").getValue());
assertEquals("http://foo/Patient", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("http://my.org/foo//_history/2", id.withVersion("2").getValue());
}
@Test
public void testBaseUrlFoo2() {
IdType id = new IdType("http://my.org/a/b/c/foo");
assertEquals("http://my.org/a/b/c/foo", id.getValueAsString());
assertEquals("foo", id.getIdPart());
assertEquals("c/foo", id.toUnqualified().getValueAsString());
assertEquals("c/foo", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals("c", id.getResourceType());
assertEquals("http://my.org/a/b", id.getBaseUrl());
assertEquals("Patient/foo", id.withResourceType("Patient").getValue());
assertEquals("http://foo/Patient/foo", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("http://my.org/a/b/c/foo/_history/2", id.withVersion("2").getValue());
}
@Test
public void testDetermineBase() {

View File

@ -1,13 +1,13 @@
package ca.uhn.fhir.parser;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.*;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.narrative.INarrativeGenerator;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.util.TestUtil;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import org.apache.commons.io.IOUtils;
import org.hamcrest.core.IsNot;
import org.hamcrest.core.StringContains;
@ -22,19 +22,23 @@ import org.hl7.fhir.instance.model.Narrative.NarrativeStatus;
import org.hl7.fhir.instance.model.Patient.ContactComponent;
import org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent;
import org.hl7.fhir.instance.model.ValueSet.ValueSetCodeSystemComponent;
import org.hl7.fhir.instance.model.api.*;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import org.hl7.fhir.instance.model.api.INarrative;
import org.hl7.fhir.instance.model.api.IPrimitiveType;
import org.hl7.fhir.utilities.xhtml.XhtmlNode;
import org.junit.*;
import org.xml.sax.SAXException;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.narrative.INarrativeGenerator;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.util.TestUtil;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
public class JsonParserHl7OrgDstu2Test {
private static FhirContext ourCtx = FhirContext.forDstu2Hl7Org();
@ -1274,6 +1278,26 @@ public class JsonParserHl7OrgDstu2Test {
}
@Test
public void testBaseUrlFooResourceCorrectlySerializedInExtensionValueReference() {
String refVal = "http://my.org/FooBar";
Patient fhirPat = new Patient();
fhirPat.addExtension().setUrl("x1").setValue(new Reference(refVal));
IParser parser = ourCtx.newJsonParser();
String output = parser.encodeResourceToString(fhirPat);
System.out.println("output: " + output);
// Deserialize then check that valueReference value is still correct
fhirPat = parser.parseResource(Patient.class, output);
List<Extension> extlst = fhirPat.getExtension();
Assert.assertEquals(1, extlst.size());
Assert.assertEquals(refVal, ((Reference) extlst.get(0).getValue()).getReference());
}
@ResourceDef(name = "Patient")
public static class MyPatientWithOneDeclaredAddressExtension extends Patient {

View File

@ -21,6 +21,24 @@ import org.hl7.fhir.instance.model.Identifier.IdentifierUse;
import org.hl7.fhir.instance.model.Narrative.NarrativeStatus;
import org.hl7.fhir.instance.model.api.*;
import org.junit.*;
import org.hl7.fhir.instance.model.Observation;
import org.hl7.fhir.instance.model.Organization;
import org.hl7.fhir.instance.model.Patient;
import org.hl7.fhir.instance.model.PrimitiveType;
import org.hl7.fhir.instance.model.Reference;
import org.hl7.fhir.instance.model.Resource;
import org.hl7.fhir.instance.model.SimpleQuantity;
import org.hl7.fhir.instance.model.Specimen;
import org.hl7.fhir.instance.model.StringType;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import org.hl7.fhir.instance.model.api.INarrative;
import org.hl7.fhir.instance.model.api.IPrimitiveType;
import org.junit.After;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.xml.sax.SAXException;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.Input;
@ -1738,6 +1756,26 @@ public class XmlParserHl7OrgDstu2Test {
}
@Test
public void testBaseUrlFooResourceCorrectlySerializedInExtensionValueReference() {
String refVal = "http://my.org/FooBar";
Patient fhirPat = new Patient();
fhirPat.addExtension().setUrl("x1").setValue(new Reference(refVal));
IParser parser = ourCtx.newXmlParser();
String output = parser.encodeResourceToString(fhirPat);
System.out.println("output: " + output);
// Deserialize then check that valueReference value is still correct
fhirPat = parser.parseResource(Patient.class, output);
List<Extension> extlst = fhirPat.getExtension();
Assert.assertEquals(1, extlst.size());
Assert.assertEquals(refVal, ((Reference) extlst.get(0).getValue()).getReference());
}
@BeforeClass
public static void beforeClass() {
ourCtx = FhirContext.forDstu2Hl7Org();

View File

@ -70,89 +70,89 @@ import org.hl7.fhir.instance.model.api.*;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
/**
* This class represents the logical identity for a resource, or as much of that
* identity is known. In FHIR, every resource must have a "logical ID" which is
* defined by the FHIR specification as:
* <p>
* <code>A whole number in the range 0 to 2^64-1 (optionally represented in hex),
* a uuid, an oid, or any other combination of lowercase letters, numerals, "-"
* and ".", with a length limit of 36 characters</code>
* </p>
* <p>
* This class contains that logical ID, and can optionally also contain a
* relative or absolute URL representing the resource identity. For example, the
* following are all valid values for IdType, and all might represent the same
* resource:
* </p>
* <ul>
* <li><code>123</code> (just a resource's ID)</li>
* <li><code>Patient/123</code> (a relative identity)</li>
* <li><code>http://example.com/Patient/123 (an absolute identity)</code></li>
* <li>
* <code>http://example.com/Patient/123/_history/1 (an absolute identity with a version id)</code>
* </li>
* <li>
* <code>Patient/123/_history/1 (a relative identity with a version id)</code>
* </li>
* </ul>
* <p>
* In most situations, you only need to populate the resource's ID (e.g.
* <code>123</code>) in resources you are constructing and the encoder will
* infer the rest from the context in which the object is being used. On the
* other hand, the parser will always try to populate the complete absolute
* identity on objects it creates as a convenience.
* </p>
* <p>
* Regex for ID: [a-z0-9\-\.]{1,36}
* </p>
*/
@DatatypeDef(name = "id", profileOf=StringType.class)
* This class represents the logical identity for a resource, or as much of that
* identity is known. In FHIR, every resource must have a "logical ID" which is
* defined by the FHIR specification as:
* <p>
* <code>A whole number in the range 0 to 2^64-1 (optionally represented in hex),
* a uuid, an oid, or any other combination of lowercase letters, numerals, "-"
* and ".", with a length limit of 36 characters</code>
* </p>
* <p>
* This class contains that logical ID, and can optionally also contain a
* relative or absolute URL representing the resource identity. For example, the
* following are all valid values for IdType, and all might represent the same
* resource:
* </p>
* <ul>
* <li><code>123</code> (just a resource's ID)</li>
* <li><code>Patient/123</code> (a relative identity)</li>
* <li><code>http://example.com/Patient/123 (an absolute identity)</code></li>
* <li>
* <code>http://example.com/Patient/123/_history/1 (an absolute identity with a version id)</code>
* </li>
* <li>
* <code>Patient/123/_history/1 (a relative identity with a version id)</code>
* </li>
* </ul>
* <p>
* In most situations, you only need to populate the resource's ID (e.g.
* <code>123</code>) in resources you are constructing and the encoder will
* infer the rest from the context in which the object is being used. On the
* other hand, the parser will always try to populate the complete absolute
* identity on objects it creates as a convenience.
* </p>
* <p>
* Regex for ID: [a-z0-9\-\.]{1,36}
* </p>
*/
@DatatypeDef(name = "id", profileOf = StringType.class)
public final class IdType extends UriType implements IPrimitiveType<String>, IIdType {
public static final String URN_PREFIX = "urn:";
public static final String URN_PREFIX = "urn:";
/**
/**
* This is the maximum length for the ID
*/
public static final int MAX_LENGTH = 64; // maximum length
public static final int MAX_LENGTH = 64; // maximum length
private static final long serialVersionUID = 2L;
private String myBaseUrl;
private boolean myHaveComponentParts;
private String myResourceType;
private String myUnqualifiedId;
private String myUnqualifiedVersionId;
private static final long serialVersionUID = 2L;
private String myBaseUrl;
private boolean myHaveComponentParts;
private String myResourceType;
private String myUnqualifiedId;
private String myUnqualifiedVersionId;
/**
/**
* Create a new empty ID
*/
public IdType() {
public IdType() {
super();
}
}
/**
/**
* Create a new ID, using a BigDecimal input. Uses
* {@link BigDecimal#toPlainString()} to generate the string representation.
*/
public IdType(BigDecimal thePid) {
public IdType(BigDecimal thePid) {
if (thePid != null) {
setValue(toPlainStringWithNpeThrowIfNeeded(thePid));
} else {
setValue(null);
}
}
}
/**
/**
* Create a new ID using a long
*/
public IdType(long theId) {
public IdType(long theId) {
setValue(Long.toString(theId));
}
}
/**
/**
* Create a new ID using a string. This String may contain a simple ID (e.g.
* "1234") or it may contain a complete URL
* (http://example.com/fhir/Patient/1234).
*
* <p>
* <p>
* <b>Description</b>: A whole number in the range 0 to 2^64-1 (optionally
* represented in hex), a uuid, an oid, or any other combination of lowercase
@ -162,73 +162,60 @@ public IdType(long theId) {
* regex: [a-z0-9\-\.]{1,36}
* </p>
*/
public IdType(String theValue) {
public IdType(String theValue) {
setValue(theValue);
}
}
/**
/**
* Constructor
*
* @param theResourceType
* The resource type (e.g. "Patient")
* @param theIdPart
* The ID (e.g. "123")
* @param theResourceType The resource type (e.g. "Patient")
* @param theIdPart The ID (e.g. "123")
*/
public IdType(String theResourceType, BigDecimal theIdPart) {
public IdType(String theResourceType, BigDecimal theIdPart) {
this(theResourceType, toPlainStringWithNpeThrowIfNeeded(theIdPart));
}
}
/**
/**
* Constructor
*
* @param theResourceType
* The resource type (e.g. "Patient")
* @param theIdPart
* The ID (e.g. "123")
* @param theResourceType The resource type (e.g. "Patient")
* @param theIdPart The ID (e.g. "123")
*/
public IdType(String theResourceType, Long theIdPart) {
public IdType(String theResourceType, Long theIdPart) {
this(theResourceType, toPlainStringWithNpeThrowIfNeeded(theIdPart));
}
}
/**
/**
* Constructor
*
* @param theResourceType
* The resource type (e.g. "Patient")
* @param theId
* The ID (e.g. "123")
* @param theResourceType The resource type (e.g. "Patient")
* @param theId The ID (e.g. "123")
*/
public IdType(String theResourceType, String theId) {
public IdType(String theResourceType, String theId) {
this(theResourceType, theId, null);
}
}
/**
/**
* Constructor
*
* @param theResourceType
* The resource type (e.g. "Patient")
* @param theId
* The ID (e.g. "123")
* @param theVersionId
* The version ID ("e.g. "456")
* @param theResourceType The resource type (e.g. "Patient")
* @param theId The ID (e.g. "123")
* @param theVersionId The version ID ("e.g. "456")
*/
public IdType(String theResourceType, String theId, String theVersionId) {
public IdType(String theResourceType, String theId, String theVersionId) {
this(null, theResourceType, theId, theVersionId);
}
}
/**
/**
* Constructor
*
* @param theBaseUrl
* The server base URL (e.g. "http://example.com/fhir")
* @param theResourceType
* The resource type (e.g. "Patient")
* @param theId
* The ID (e.g. "123")
* @param theVersionId
* The version ID ("e.g. "456")
* @param theBaseUrl The server base URL (e.g. "http://example.com/fhir")
* @param theResourceType The resource type (e.g. "Patient")
* @param theId The ID (e.g. "123")
* @param theVersionId The version ID ("e.g. "456")
*/
public IdType(String theBaseUrl, String theResourceType, String theId, String theVersionId) {
public IdType(String theBaseUrl, String theResourceType, String theId, String theVersionId) {
myBaseUrl = theBaseUrl;
myResourceType = theResourceType;
myUnqualifiedId = theId;
@ -237,51 +224,51 @@ public IdType(String theBaseUrl, String theResourceType, String theId, String th
if (isBlank(myBaseUrl) && isBlank(myResourceType) && isBlank(myUnqualifiedId) && isBlank(myUnqualifiedVersionId)) {
myHaveComponentParts = false;
}
}
}
/**
/**
* Creates an ID based on a given URL
*/
public IdType(UriType theUrl) {
public IdType(UriType theUrl) {
setValue(theUrl.getValueAsString());
}
}
public void applyTo(IBaseResource theResouce) {
public void applyTo(IBaseResource theResouce) {
if (theResouce == null) {
throw new NullPointerException("theResource can not be null");
} else {
theResouce.setId(new IdType(getValue()));
}
}
}
/**
/**
* @deprecated Use {@link #getIdPartAsBigDecimal()} instead (this method was
* deprocated because its name is ambiguous)
*/
@Deprecated
public BigDecimal asBigDecimal() {
@Deprecated
public BigDecimal asBigDecimal() {
return getIdPartAsBigDecimal();
}
}
@Override
public IdType copy() {
@Override
public IdType copy() {
return new IdType(getValue());
}
}
@Override
public boolean equals(Object theArg0) {
@Override
public boolean equals(Object theArg0) {
if (!(theArg0 instanceof IdType)) {
return false;
}
return StringUtils.equals(getValueAsString(), ((IdType)theArg0).getValueAsString());
}
return StringUtils.equals(getValueAsString(), ((IdType) theArg0).getValueAsString());
}
/**
/**
* Returns true if this IdType matches the given IdType in terms of resource
* type and ID, but ignores the URL base
*/
@SuppressWarnings("deprecation")
public boolean equalsIgnoreBase(IdType theId) {
@SuppressWarnings("deprecation")
public boolean equalsIgnoreBase(IdType theId) {
if (theId == null) {
return false;
}
@ -291,9 +278,9 @@ public boolean equalsIgnoreBase(IdType theId) {
return ObjectUtils.equals(getResourceType(), theId.getResourceType())
&& ObjectUtils.equals(getIdPart(), theId.getIdPart())
&& ObjectUtils.equals(getVersionIdPart(), theId.getVersionIdPart());
}
}
/**
/**
* Returns the portion of this resource ID which corresponds to the server
* base URL. For example given the resource ID
* <code>http://example.com/fhir/Patient/123</code> the base URL would be
@ -302,66 +289,64 @@ public boolean equalsIgnoreBase(IdType theId) {
* This method may return null if the ID contains no base (e.g. "Patient/123")
* </p>
*/
@Override
public String getBaseUrl() {
@Override
public String getBaseUrl() {
return myBaseUrl;
}
}
/**
/**
* Returns only the logical ID part of this ID. For example, given the ID
* "http://example,.com/fhir/Patient/123/_history/456", this method would
* return "123".
*/
@Override
public String getIdPart() {
@Override
public String getIdPart() {
return myUnqualifiedId;
}
}
/**
/**
* Returns the unqualified portion of this ID as a big decimal, or
* <code>null</code> if the value is null
*
* @throws NumberFormatException
* If the value is not a valid BigDecimal
* @throws NumberFormatException If the value is not a valid BigDecimal
*/
public BigDecimal getIdPartAsBigDecimal() {
public BigDecimal getIdPartAsBigDecimal() {
String val = getIdPart();
if (isBlank(val)) {
return null;
}
return new BigDecimal(val);
}
}
/**
/**
* Returns the unqualified portion of this ID as a {@link Long}, or
* <code>null</code> if the value is null
*
* @throws NumberFormatException
* If the value is not a valid Long
* @throws NumberFormatException If the value is not a valid Long
*/
@Override
public Long getIdPartAsLong() {
@Override
public Long getIdPartAsLong() {
String val = getIdPart();
if (isBlank(val)) {
return null;
}
return Long.parseLong(val);
}
}
@Override
public String getResourceType() {
@Override
public String getResourceType() {
return myResourceType;
}
}
/**
/**
* Returns the value of this ID. Note that this value may be a fully qualified
* URL, a relative/partial URL, or a simple ID. Use {@link #getIdPart()} to
* get just the ID portion.
*
* @see #getIdPart()
*/
@Override
public String getValue() {
@Override
public String getValue() {
String retVal = super.getValue();
if (retVal == null && myHaveComponentParts) {
@ -381,11 +366,16 @@ public String getValue() {
b.append(myResourceType);
}
if (b.length() > 0) {
if (b.length() > 0 && isNotBlank(myUnqualifiedId)) {
b.append('/');
}
if (isNotBlank(myUnqualifiedId)) {
b.append(myUnqualifiedId);
} else if (isNotBlank(myUnqualifiedVersionId)) {
b.append('/');
}
if (isNotBlank(myUnqualifiedVersionId)) {
b.append('/');
b.append("_history");
@ -396,76 +386,76 @@ public String getValue() {
super.setValue(retVal);
}
return retVal;
}
}
@Override
public String getValueAsString() {
@Override
public String getValueAsString() {
return getValue();
}
}
@Override
public String getVersionIdPart() {
@Override
public String getVersionIdPart() {
return myUnqualifiedVersionId;
}
}
public Long getVersionIdPartAsLong() {
public Long getVersionIdPartAsLong() {
if (!hasVersionIdPart()) {
return null;
} else {
return Long.parseLong(getVersionIdPart());
}
}
}
/**
/**
* Returns true if this ID has a base url
*
* @see #getBaseUrl()
*/
public boolean hasBaseUrl() {
public boolean hasBaseUrl() {
return isNotBlank(myBaseUrl);
}
}
@Override
public int hashCode() {
@Override
public int hashCode() {
HashCodeBuilder b = new HashCodeBuilder();
b.append(getValueAsString());
return b.toHashCode();
}
}
@Override
public boolean hasIdPart() {
@Override
public boolean hasIdPart() {
return isNotBlank(getIdPart());
}
}
@Override
public boolean hasResourceType() {
@Override
public boolean hasResourceType() {
return isNotBlank(myResourceType);
}
}
@Override
public boolean hasVersionIdPart() {
@Override
public boolean hasVersionIdPart() {
return isNotBlank(getVersionIdPart());
}
}
/**
/**
* Returns <code>true</code> if this ID contains an absolute URL (in other
* words, a URL starting with "http://" or "https://"
*/
@Override
public boolean isAbsolute() {
@Override
public boolean isAbsolute() {
if (StringUtils.isBlank(getValue())) {
return false;
}
return isUrlAbsolute(getValue());
}
}
@Override
public boolean isEmpty() {
@Override
public boolean isEmpty() {
return isBlank(getValue());
}
}
@Override
public boolean isIdPartValid() {
@Override
public boolean isIdPartValid() {
String id = getIdPart();
if (StringUtils.isBlank(id)) {
return false;
@ -490,38 +480,38 @@ public boolean isIdPartValid() {
return false;
}
return true;
}
}
/**
* Returns <code>true</code> if the unqualified ID is a valid {@link Long}
* value (in other words, it consists only of digits)
*/
@Override
public boolean isIdPartValidLong() {
@Override
public boolean isIdPartValidLong() {
return isValidLong(getIdPart());
}
}
/**
/**
* Returns <code>true</code> if the ID is a local reference (in other words,
* it begins with the '#' character)
*/
@Override
public boolean isLocal() {
@Override
public boolean isLocal() {
return defaultString(myUnqualifiedId).startsWith("#");
}
}
public boolean isUrn() {
public boolean isUrn() {
return defaultString(myUnqualifiedId).startsWith(URN_PREFIX);
}
}
@Override
public boolean isVersionIdPartValidLong() {
@Override
public boolean isVersionIdPartValidLong() {
return isValidLong(getVersionIdPart());
}
}
/**
/**
* Set the value
*
* <p>
* <p>
* <b>Description</b>: A whole number in the range 0 to 2^64-1 (optionally
* represented in hex), a uuid, an oid, or any other combination of lowercase
@ -531,8 +521,8 @@ public boolean isVersionIdPartValidLong() {
* regex: [a-z0-9\-\.]{1,36}
* </p>
*/
@Override
public IdType setValue(String theValue) {
@Override
public IdType setValue(String theValue) {
// TODO: add validation
super.setValue(theValue);
myHaveComponentParts = false;
@ -576,8 +566,22 @@ public IdType setValue(String theValue) {
int typeIndex = theValue.lastIndexOf('/', idIndex - 1);
if (typeIndex == -1) {
myResourceType = theValue.substring(0, idIndex);
} else {
if (typeIndex > 0 && '/' == theValue.charAt(typeIndex - 1)) {
typeIndex = theValue.indexOf('/', typeIndex + 1);
}
if (typeIndex >= idIndex) {
// e.g. http://example.org/foo
// 'foo' was the id but we're making that the resource type. Nullify the id part because we don't have an id.
// Also set null value to the super.setValue() and enable myHaveComponentParts so it forces getValue() to properly
// recreate the url
myResourceType = myUnqualifiedId;
myUnqualifiedId = null;
super.setValue(null);
myHaveComponentParts = true;
} else {
myResourceType = theValue.substring(typeIndex + 1, idIndex);
}
if (typeIndex > 4) {
myBaseUrl = theValue.substring(0, typeIndex);
@ -588,11 +592,11 @@ public IdType setValue(String theValue) {
}
return this;
}
}
/**
/**
* Set the value
*
* <p>
* <p>
* <b>Description</b>: A whole number in the range 0 to 2^64-1 (optionally
* represented in hex), a uuid, an oid, or any other combination of lowercase
@ -602,87 +606,84 @@ public IdType setValue(String theValue) {
* regex: [a-z0-9\-\.]{1,36}
* </p>
*/
@Override
public void setValueAsString(String theValue) {
@Override
public void setValueAsString(String theValue) {
setValue(theValue);
}
}
@Override
public String toString() {
@Override
public String toString() {
return getValue();
}
}
/**
/**
* Returns a new IdType containing this IdType's values but with no server
* base URL if one is present in this IdType. For example, if this IdType
* contains the ID "http://foo/Patient/1", this method will return a new
* IdType containing ID "Patient/1".
*/
@Override
public IdType toUnqualified() {
@Override
public IdType toUnqualified() {
if (isLocal() || isUrn()) {
return new IdType(getValueAsString());
}
return new IdType(getResourceType(), getIdPart(), getVersionIdPart());
}
}
@Override
public IdType toUnqualifiedVersionless() {
@Override
public IdType toUnqualifiedVersionless() {
if (isLocal() || isUrn()) {
return new IdType(getValueAsString());
}
return new IdType(getResourceType(), getIdPart());
}
}
@Override
public IdType toVersionless() {
@Override
public IdType toVersionless() {
if (isLocal() || isUrn()) {
return new IdType(getValueAsString());
}
return new IdType(getBaseUrl(), getResourceType(), getIdPart(), null);
}
}
@Override
public IdType withResourceType(String theResourceName) {
@Override
public IdType withResourceType(String theResourceName) {
if (isLocal() || isUrn()) {
return new IdType(getValueAsString());
}
return new IdType(theResourceName, getIdPart(), getVersionIdPart());
}
}
/**
/**
* Returns a view of this ID as a fully qualified URL, given a server base and
* resource name (which will only be used if the ID does not already contain
* those respective parts). Essentially, because IdType can contain either a
* complete URL or a partial one (or even jut a simple ID), this method may be
* used to translate into a complete URL.
*
* @param theServerBase
* The server base (e.g. "http://example.com/fhir")
* @param theResourceType
* The resource name (e.g. "Patient")
* @param theServerBase The server base (e.g. "http://example.com/fhir")
* @param theResourceType The resource name (e.g. "Patient")
* @return A fully qualified URL for this ID (e.g.
* "http://example.com/fhir/Patient/1")
*/
@Override
public IdType withServerBase(String theServerBase, String theResourceType) {
@Override
public IdType withServerBase(String theServerBase, String theResourceType) {
if (isLocal() || isUrn()) {
return new IdType(getValueAsString());
}
return new IdType(theServerBase, theResourceType, getIdPart(), getVersionIdPart());
}
}
/**
/**
* Creates a new instance of this ID which is identical, but refers to the
* specific version of this resource ID noted by theVersion.
*
* @param theVersion
* The actual version string, e.g. "1"
* @param theVersion The actual version string, e.g. "1"
* @return A new instance of IdType which is identical, but refers to the
* specific version of this resource ID noted by theVersion.
*/
@Override
public IdType withVersion(String theVersion) {
@Override
public IdType withVersion(String theVersion) {
Validate.notBlank(theVersion, "Version may not be null or empty");
if (isLocal() || isUrn()) {
@ -700,14 +701,14 @@ public IdType withVersion(String theVersion) {
}
return new IdType(value + '/' + "_history" + '/' + theVersion);
}
}
private static boolean isUrlAbsolute(String theValue) {
private static boolean isUrlAbsolute(String theValue) {
String value = theValue.toLowerCase();
return value.startsWith("http://") || value.startsWith("https://");
}
}
private static boolean isValidLong(String id) {
private static boolean isValidLong(String id) {
if (StringUtils.isBlank(id)) {
return false;
}
@ -717,20 +718,20 @@ private static boolean isValidLong(String id) {
}
}
return true;
}
}
/**
/**
* Construct a new ID with with form "urn:uuid:[UUID]" where [UUID] is a new,
* randomly created UUID generated by {@link UUID#randomUUID()}
*/
public static IdType newRandomUuid() {
public static IdType newRandomUuid() {
return new IdType("urn:uuid:" + UUID.randomUUID().toString());
}
}
/**
/**
* Retrieves the ID from the given resource instance
*/
public static IdType of(IBaseResource theResouce) {
public static IdType of(IBaseResource theResouce) {
if (theResouce == null) {
throw new NullPointerException("theResource can not be null");
} else {
@ -743,21 +744,21 @@ public static IdType of(IBaseResource theResouce) {
return new IdType(retVal.getValue());
}
}
}
}
private static String toPlainStringWithNpeThrowIfNeeded(BigDecimal theIdPart) {
private static String toPlainStringWithNpeThrowIfNeeded(BigDecimal theIdPart) {
if (theIdPart == null) {
throw new NullPointerException("BigDecimal ID can not be null");
}
return theIdPart.toPlainString();
}
}
private static String toPlainStringWithNpeThrowIfNeeded(Long theIdPart) {
private static String toPlainStringWithNpeThrowIfNeeded(Long theIdPart) {
if (theIdPart == null) {
throw new NullPointerException("Long ID can not be null");
}
return theIdPart.toString();
}
}
public String fhirType() {
return "id";

View File

@ -1216,7 +1216,7 @@ public class ClientR4Test {
TestUtil.clearAllStaticFieldsForUnitTest();
}
private static String getPatientFeedWithOneResult() {
static String getPatientFeedWithOneResult() {
return getPatientFeedWithOneResult(ourCtx);
}

View File

@ -108,6 +108,30 @@ public class GenericClientR4Test {
return capt;
}
@Test
public void testSearchWithNoExplicitBundleReturnType() throws Exception {
String msg = ClientR4Test.getPatientFeedWithOneResult();
ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class);
when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"));
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_XML + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8")));
// httpResponse = new BasicHttpResponse(statusline, catalog, locale)
when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
IGenericClient client = ourCtx.newRestfulGenericClient("http://foo");
Bundle response = (Bundle) client.search().forResource(Patient.class).execute();
assertEquals("http://foo/Patient", capt.getValue().getURI().toString());
Patient patient = (Patient) response.getEntry().get(0).getResource();
assertEquals("PRP1660", patient.getIdentifier().get(0).getValueElement().getValue());
}
@Test
public void testAcceptHeaderWithEncodingSpecified() throws Exception {
final String msg = "{\"resourceType\":\"Bundle\",\"id\":null,\"base\":\"http://localhost:57931/fhir/contextDev\",\"total\":1,\"link\":[{\"relation\":\"self\",\"url\":\"http://localhost:57931/fhir/contextDev/Patient?identifier=urn%3AMultiFhirVersionTest%7CtestSubmitPatient01&_format=json\"}],\"entry\":[{\"resource\":{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}}]}";

View File

@ -0,0 +1,336 @@
package org.hl7.fhir.r4.model;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.util.TestUtil;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.math.BigDecimal;
import static org.junit.Assert.*;
public class IdTypeR4Test {
private static FhirContext ourCtx;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(IdTypeR4Test.class);
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
@Test
public void testBaseUrlFoo1() {
IdType id = new IdType("http://my.org/foo");
assertEquals("http://my.org/foo", id.getValueAsString());
assertEquals(null, id.getIdPart());
assertEquals("foo", id.toUnqualified().getValueAsString());
assertEquals("foo", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals("foo", id.getResourceType());
assertEquals("http://my.org", id.getBaseUrl());
assertEquals("Patient", id.withResourceType("Patient").getValue());
assertEquals("http://foo/Patient", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("http://my.org/foo//_history/2", id.withVersion("2").getValue());
}
@Test
public void testBaseUrlFoo2() {
IdType id = new IdType("http://my.org/a/b/c/foo");
assertEquals("http://my.org/a/b/c/foo", id.getValueAsString());
assertEquals("foo", id.getIdPart());
assertEquals("c/foo", id.toUnqualified().getValueAsString());
assertEquals("c/foo", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals("c", id.getResourceType());
assertEquals("http://my.org/a/b", id.getBaseUrl());
assertEquals("Patient/foo", id.withResourceType("Patient").getValue());
assertEquals("http://foo/Patient/foo", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("http://my.org/a/b/c/foo/_history/2", id.withVersion("2").getValue());
}
@Test
public void testUuid() {
IdType id = new IdType("urn:uuid:1234-5678");
assertEquals("urn:uuid:1234-5678", id.getValueAsString());
assertEquals("urn:uuid:1234-5678", id.getIdPart());
assertEquals("urn:uuid:1234-5678", id.toUnqualified().getValueAsString());
assertEquals("urn:uuid:1234-5678", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals(null, id.getResourceType());
assertEquals(null, id.getBaseUrl());
assertEquals("urn:uuid:1234-5678", id.withResourceType("Patient").getValue());
assertEquals("urn:uuid:1234-5678", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("urn:uuid:1234-5678", id.withVersion("2").getValue());
}
@Test
public void testOid() {
IdType id = new IdType("urn:oid:1.2.3.4");
assertEquals("urn:oid:1.2.3.4", id.getValueAsString());
assertEquals("urn:oid:1.2.3.4", id.getIdPart());
assertEquals("urn:oid:1.2.3.4", id.toUnqualified().getValueAsString());
assertEquals("urn:oid:1.2.3.4", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals(null, id.getResourceType());
assertEquals(null, id.getBaseUrl());
assertEquals("urn:oid:1.2.3.4", id.withResourceType("Patient").getValue());
assertEquals("urn:oid:1.2.3.4", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("urn:oid:1.2.3.4", id.withVersion("2").getValue());
}
@Test
public void testLocal() {
IdType id = new IdType("#foo");
assertEquals("#foo", id.getValueAsString());
assertEquals("#foo", id.getIdPart());
assertEquals("#foo", id.toUnqualified().getValueAsString());
assertEquals("#foo", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals(null, id.getResourceType());
assertEquals(null, id.getBaseUrl());
assertEquals("#foo", id.withResourceType("Patient").getValue());
assertEquals("#foo", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("#foo", id.withVersion("2").getValue());
}
@Test
public void testNormal() {
IdType id = new IdType("foo");
assertEquals("foo", id.getValueAsString());
assertEquals("foo", id.getIdPart());
assertEquals("foo", id.toUnqualified().getValueAsString());
assertEquals("foo", id.toUnqualifiedVersionless().getValueAsString());
assertEquals(null, id.getVersionIdPart());
assertEquals(null, id.getResourceType());
assertEquals(null, id.getBaseUrl());
assertEquals("Patient/foo", id.withResourceType("Patient").getValue());
assertEquals("http://foo/Patient/foo", id.withServerBase("http://foo", "Patient").getValue());
assertEquals("foo/_history/2", id.withVersion("2").getValue());
}
@Test
public void testDetectLocal() {
IdType id;
id = new IdType("#123");
assertEquals("#123", id.getValue());
assertTrue(id.isLocal());
id = new IdType("#Medication/499059CE-CDD4-48BC-9014-528A35D15CED/_history/1");
assertEquals("#Medication/499059CE-CDD4-48BC-9014-528A35D15CED/_history/1", id.getValue());
assertTrue(id.isLocal());
id = new IdType("http://example.com/Patient/33#123");
assertEquals("http://example.com/Patient/33#123", id.getValue());
assertFalse(id.isLocal());
}
@Test
public void testConstructorsWithNullArguments() {
IdType id = new IdType(null, null, null);
assertEquals(null, id.getValue());
}
@Test
public void testDetectLocalBase() {
assertEquals("urn:uuid:180f219f-97a8-486d-99d9-ed631fe4fc57", new IdType("urn:uuid:180f219f-97a8-486d-99d9-ed631fe4fc57").getValue());
assertEquals(null, new IdType("urn:uuid:180f219f-97a8-486d-99d9-ed631fe4fc57").getBaseUrl());
assertEquals("urn:uuid:180f219f-97a8-486d-99d9-ed631fe4fc57", new IdType("urn:uuid:180f219f-97a8-486d-99d9-ed631fe4fc57").getIdPart());
assertEquals("cid:180f219f-97a8-486d-99d9-ed631fe4fc57", new IdType("cid:180f219f-97a8-486d-99d9-ed631fe4fc57").getValue());
assertEquals(null, new IdType("cid:180f219f-97a8-486d-99d9-ed631fe4fc57").getBaseUrl());
assertEquals("cid:180f219f-97a8-486d-99d9-ed631fe4fc57", new IdType("cid:180f219f-97a8-486d-99d9-ed631fe4fc57").getIdPart());
assertEquals("#180f219f-97a8-486d-99d9-ed631fe4fc57", new IdType("#180f219f-97a8-486d-99d9-ed631fe4fc57").getValue());
assertEquals(null, new IdType("#180f219f-97a8-486d-99d9-ed631fe4fc57").getBaseUrl());
assertEquals("#180f219f-97a8-486d-99d9-ed631fe4fc57", new IdType("#180f219f-97a8-486d-99d9-ed631fe4fc57").getIdPart());
}
/**
* See #67
*/
@Test
public void testComplicatedLocal() {
IdType id = new IdType("#Patient/cid:Patient-72/_history/1");
assertTrue(id.isLocal());
assertEquals(null, id.getBaseUrl());
assertNull(id.getResourceType());
assertNull(id.getVersionIdPart());
assertEquals("#Patient/cid:Patient-72/_history/1", id.getIdPart());
IdType id2 = new IdType("#Patient/cid:Patient-72/_history/1");
assertEquals(id, id2);
id2 = id2.toUnqualified();
assertTrue(id2.isLocal());
assertNull(id2.getBaseUrl());
assertNull(id2.getResourceType());
assertNull(id2.getVersionIdPart());
assertEquals("#Patient/cid:Patient-72/_history/1", id2.getIdPart());
}
@Test
public void testDetermineBase() {
IdType rr;
rr = new IdType("http://foo/fhir/Organization/123");
assertEquals("http://foo/fhir", rr.getBaseUrl());
rr = new IdType("http://foo/fhir/Organization/123/_history/123");
assertEquals("http://foo/fhir", rr.getBaseUrl());
rr = new IdType("Organization/123/_history/123");
assertEquals(null, rr.getBaseUrl());
}
@Test
public void testParseValueAbsolute() {
Patient patient = new Patient();
IdType rr = new IdType();
rr.setValue("http://foo/fhir/Organization/123");
patient.setManagingOrganization(new Reference(rr));
Patient actual = parseAndEncode(patient);
Reference ref = actual.getManagingOrganization();
assertEquals("Organization", ref.getReferenceElement().getResourceType());
assertEquals("123", ref.getReferenceElement().getIdPart());
}
@Test
public void testBigDecimalIds() {
IdType id = new IdType(new BigDecimal("123"));
assertEquals(id.getIdPartAsBigDecimal(), new BigDecimal("123"));
}
@Test
public void testParseValueAbsoluteWithVersion() {
Patient patient = new Patient();
IdType rr = new IdType();
rr.setValue("http://foo/fhir/Organization/123/_history/999");
patient.setManagingOrganization(new Reference(rr));
Patient actual = parseAndEncode(patient);
Reference ref = actual.getManagingOrganization();
assertEquals("Organization", ref.getReferenceElement().getResourceType());
assertEquals("123", ref.getReferenceElement().getIdPart());
assertEquals(null, ref.getReferenceElement().getVersionIdPart());
}
@Test
public void testViewMethods() {
IdType i = new IdType("http://foo/fhir/Organization/123/_history/999");
assertEquals("Organization/123/_history/999", i.toUnqualified().getValue());
assertEquals("http://foo/fhir/Organization/123", i.toVersionless().getValue());
assertEquals("Organization/123", i.toUnqualifiedVersionless().getValue());
}
@Test
public void testParseValueWithVersion() {
Patient patient = new Patient();
IdType rr = new IdType();
rr.setValue("/123/_history/999");
patient.setManagingOrganization(new Reference(rr));
Patient actual = parseAndEncode(patient);
Reference ref = actual.getManagingOrganization();
assertEquals(null, ref.getReferenceElement().getResourceType());
assertEquals("123", ref.getReferenceElement().getIdPart());
assertEquals(null, ref.getReferenceElement().getVersionIdPart());
}
@Test
public void testParseValueMissingType1() {
Patient patient = new Patient();
IdType rr = new IdType();
rr.setValue("/123");
patient.setManagingOrganization(new Reference(rr));
Patient actual = parseAndEncode(patient);
Reference ref = actual.getManagingOrganization();
assertEquals(null, ref.getReferenceElement().getResourceType());
assertEquals("123", ref.getReferenceElement().getIdPart());
}
@Test
public void testParseValueMissingType2() {
Patient patient = new Patient();
IdType rr = new IdType();
rr.setValue("123");
patient.setManagingOrganization(new Reference(rr));
Patient actual = parseAndEncode(patient);
Reference ref = actual.getManagingOrganization();
assertEquals(null, ref.getReferenceElement().getResourceType());
assertEquals("123", ref.getReferenceElement().getIdPart());
}
@Test
public void testParseValueRelative1() {
Patient patient = new Patient();
IdType rr = new IdType();
rr.setValue("Organization/123");
patient.setManagingOrganization(new Reference(rr));
Patient actual = parseAndEncode(patient);
Reference ref = actual.getManagingOrganization();
assertEquals("Organization", ref.getReferenceElement().getResourceType());
assertEquals("123", ref.getReferenceElement().getIdPart());
}
@Test
public void testEncodeParts() {
IdType id = new IdType("http://foo", "Patient", "123", "456");
assertEquals("http://foo/Patient/123/_history/456", id.getValue());
assertEquals("http://foo/Patient/123/_history/9", id.withVersion("9").getValue());
}
@Test
public void testParseValueRelative2() {
Patient patient = new Patient();
IdType rr = new IdType();
rr.setValue("/Organization/123");
patient.setManagingOrganization(new Reference(rr));
Patient actual = parseAndEncode(patient);
Reference ref = actual.getManagingOrganization();
assertEquals("Organization", ref.getReferenceElement().getResourceType());
assertEquals("123", ref.getReferenceElement().getIdPart());
}
private Patient parseAndEncode(Patient patient) {
String encoded = ourCtx.newXmlParser().encodeResourceToString(patient);
ourLog.info("\n" + encoded);
return ourCtx.newXmlParser().parseResource(Patient.class, encoded);
}
@BeforeClass
public static void beforeClass() {
ourCtx = FhirContext.forR4();
}
}

View File

@ -1,30 +0,0 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%file:%line] - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.eclipse" additivity="false" level="info">
<appender-ref ref="STDOUT" />
</logger>
<logger name="org.apache" additivity="false" level="info">
<appender-ref ref="STDOUT" />
</logger>
<logger name="org.thymeleaf" additivity="false" level="warn">
<appender-ref ref="STDOUT" />
</logger>
<!--
<logger name="ca.uhn.fhir.rest.client" additivity="false" level="trace">
<appender-ref ref="STDOUT" />
</logger>
-->
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>