diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseHttpClientInvocationWithContents.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseHttpClientInvocationWithContents.java index 8dd3c345baa..fc3d1eef5dc 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseHttpClientInvocationWithContents.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseHttpClientInvocationWithContents.java @@ -41,6 +41,7 @@ import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.api.TagList; import ca.uhn.fhir.model.dstu.resource.Binary; +import ca.uhn.fhir.model.valueset.BundleTypeEnum; import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; @@ -64,6 +65,7 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca private final String myContents; private boolean myContentsIsBundle; private Map> myParams; + private final BundleTypeEnum myBundleType; public BaseHttpClientInvocationWithContents(FhirContext theContext, IResource theResource, String theUrlPath) { super(); @@ -74,6 +76,7 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca myResources = null; myBundle = null; myContents = null; + myBundleType = null; } public BaseHttpClientInvocationWithContents(FhirContext theContext, TagList theTagList, String... theUrlPath) { @@ -88,11 +91,12 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca myResources = null; myBundle = null; myContents = null; + myBundleType = null; myUrlPath = StringUtils.join(theUrlPath, '/'); } - public BaseHttpClientInvocationWithContents(FhirContext theContext, List theResources) { + public BaseHttpClientInvocationWithContents(FhirContext theContext, List theResources, BundleTypeEnum theBundleType) { myContext = theContext; myResource = null; myTagList = null; @@ -100,6 +104,7 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca myResources = theResources; myBundle = null; myContents = null; + myBundleType = theBundleType; } public BaseHttpClientInvocationWithContents(FhirContext theContext, Bundle theBundle) { @@ -110,6 +115,7 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca myResources = null; myBundle = theBundle; myContents = null; + myBundleType = null; } public BaseHttpClientInvocationWithContents(FhirContext theContext, String theContents, boolean theIsBundle, String theUrlPath) { @@ -121,6 +127,7 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca myBundle = null; myContents = theContents; myContentsIsBundle = theIsBundle; + myBundleType = null; } public BaseHttpClientInvocationWithContents(FhirContext theContext, Map> theParams, String... theUrlPath) { @@ -133,6 +140,7 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca myContents = null; myContentsIsBundle = false; myParams = theParams; + myBundleType = null; } @Override @@ -201,7 +209,7 @@ abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvoca contents = parser.encodeBundleToString(myBundle); contentType = encoding.getBundleContentType(); } else if (myResources != null) { - Bundle bundle = RestfulServer.createBundleFromResourceList(myContext, "", myResources, "", "", myResources.size()); + Bundle bundle = RestfulServer.createBundleFromResourceList(myContext, "", myResources, "", "", myResources.size(), myBundleType); contents = parser.encodeBundleToString(bundle); contentType = encoding.getBundleContentType(); } else if (myContents != null) { diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBinding.java index 0002e36ffa0..3dafdde18fe 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBinding.java @@ -98,37 +98,6 @@ abstract class BaseOutcomeReturningMethodBinding extends BaseMethodBinding= 0; i--) { IServerInterceptor next = theServer.getInterceptors().get(i); @@ -276,6 +277,11 @@ abstract class BaseResourceReturningMethodBinding extends BaseMethodBinding getParameters() { List retVal = new ArrayList(super.getParameters()); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HistoryMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HistoryMethodBinding.java index 5b78672a579..d00fabd4f6d 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HistoryMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HistoryMethodBinding.java @@ -35,6 +35,7 @@ import ca.uhn.fhir.model.dstu.valueset.RestfulOperationTypeEnum; import ca.uhn.fhir.model.primitive.DateTimeDt; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.InstantDt; +import ca.uhn.fhir.model.valueset.BundleTypeEnum; import ca.uhn.fhir.rest.annotation.History; import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.server.Constants; @@ -125,6 +126,11 @@ public class HistoryMethodBinding extends BaseResourceReturningMethodBinding { return retVal; } + @Override + protected BundleTypeEnum getResponseBundleType() { + return BundleTypeEnum.HISTORY; + } + public static HttpGetClientInvocation createHistoryInvocation(String theResourceName, IdDt theId, DateTimeDt theSince, Integer theLimit) { StringBuilder b = new StringBuilder(); if (theResourceName != null) { diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpPostClientInvocation.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpPostClientInvocation.java index 97a3357c28c..97a8a152eaf 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpPostClientInvocation.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpPostClientInvocation.java @@ -30,6 +30,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.api.TagList; +import ca.uhn.fhir.model.valueset.BundleTypeEnum; public class HttpPostClientInvocation extends BaseHttpClientInvocationWithContents { @@ -43,8 +44,8 @@ public class HttpPostClientInvocation extends BaseHttpClientInvocationWithConten } - public HttpPostClientInvocation(FhirContext theContext, List theResources) { - super(theContext, theResources); + public HttpPostClientInvocation(FhirContext theContext, List theResources, BundleTypeEnum theBundleType) { + super(theContext, theResources, theBundleType); } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ReadMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ReadMethodBinding.java index b2933fffd8d..69f7c402c0c 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ReadMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ReadMethodBinding.java @@ -40,6 +40,7 @@ import ca.uhn.fhir.model.dstu.resource.Binary; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationSystemEnum; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationTypeEnum; import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.model.valueset.BundleTypeEnum; import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.Read; import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType; @@ -237,4 +238,9 @@ public class ReadMethodBinding extends BaseResourceReturningMethodBinding implem return new HttpGetClientInvocation(new IdDt(theResourceName, theId.getIdPart(), theId.getVersionIdPart()).getValue()); } + @Override + protected BundleTypeEnum getResponseBundleType() { + return null; + } + } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/SearchMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/SearchMethodBinding.java index ff6e25c5076..30c0b7c1c00 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/SearchMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/SearchMethodBinding.java @@ -42,6 +42,7 @@ import ca.uhn.fhir.model.api.annotation.Description; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationSystemEnum; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationTypeEnum; import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.model.valueset.BundleTypeEnum; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.param.BaseQueryParameter; @@ -475,4 +476,9 @@ public class SearchMethodBinding extends BaseResourceReturningMethodBinding { DELETE, GET, OPTIONS, POST, PUT } + @Override + protected BundleTypeEnum getResponseBundleType() { + return BundleTypeEnum.SEARCHSET; + } + } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/TransactionMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/TransactionMethodBinding.java index bac1a8aa7ed..3b2566bebc5 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/TransactionMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/TransactionMethodBinding.java @@ -36,6 +36,7 @@ import ca.uhn.fhir.model.base.resource.BaseOperationOutcome; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationSystemEnum; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationTypeEnum; import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.model.valueset.BundleTypeEnum; import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.rest.annotation.TransactionParam; import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; @@ -172,11 +173,16 @@ public class TransactionMethodBinding extends BaseResourceReturningMethodBinding } public static BaseHttpClientInvocation createTransactionInvocation(List theResources, FhirContext theContext) { - return new HttpPostClientInvocation(theContext, theResources); + return new HttpPostClientInvocation(theContext, theResources, BundleTypeEnum.TRANSACTION); } public static BaseHttpClientInvocation createTransactionInvocation(Bundle theBundle, FhirContext theContext) { return new HttpPostClientInvocation(theContext, theBundle); } + @Override + protected BundleTypeEnum getResponseBundleType() { + return BundleTypeEnum.TRANSACTION_RESPONSE; + } + } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/Constants.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/Constants.java index cc7868c470b..477d9a00b0a 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/Constants.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/Constants.java @@ -37,6 +37,7 @@ public class Constants { public static final String CT_JSON = "application/json"; public static final String CT_OCTET_STREAM = "application/octet-stream"; public static final String CT_TEXT = "text/plain"; + public static final String CT_TEXT_WITH_UTF8 = CT_TEXT + "; charset=UTF-8"; public static final String CT_XML = "application/xml"; public static final String ENCODING_GZIP = "gzip"; public static final String FORMAT_JSON = "json"; diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServer.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServer.java index c9e8d9953b2..fd050459ee4 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServer.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/RestfulServer.java @@ -32,6 +32,7 @@ import ca.uhn.fhir.model.dstu.resource.Binary; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.InstantDt; import ca.uhn.fhir.model.valueset.BundleEntryStatusEnum; +import ca.uhn.fhir.model.valueset.BundleTypeEnum; import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.rest.annotation.Destroy; import ca.uhn.fhir.rest.annotation.IdParam; @@ -426,7 +427,7 @@ public class RestfulServer extends HttpServlet { NarrativeModeEnum narrativeMode = determineNarrativeMode(theRequest); boolean respondGzip = theRequest.isRespondGzip(); - Bundle bundle = createBundleFromBundleProvider(this, theResponse, resultList, responseEncoding, theRequest.getFhirServerBase(), theRequest.getCompleteUrl(), prettyPrint, requestIsBrowser, narrativeMode, start, count, thePagingAction); + Bundle bundle = createBundleFromBundleProvider(this, theResponse, resultList, responseEncoding, theRequest.getFhirServerBase(), theRequest.getCompleteUrl(), prettyPrint, requestIsBrowser, narrativeMode, start, count, thePagingAction, null); for (int i = getInterceptors().size() - 1; i >= 0; i--) { IServerInterceptor next = getInterceptors().get(i); @@ -1031,7 +1032,7 @@ public class RestfulServer extends HttpServlet { } public static Bundle createBundleFromBundleProvider(RestfulServer theServer, HttpServletResponse theHttpResponse, IBundleProvider theResult, EncodingEnum theResponseEncoding, String theServerBase, String theCompleteUrl, boolean thePrettyPrint, boolean theRequestIsBrowser, - NarrativeModeEnum theNarrativeMode, int theOffset, Integer theLimit, String theSearchId) { + NarrativeModeEnum theNarrativeMode, int theOffset, Integer theLimit, String theSearchId, BundleTypeEnum theBundleType) { theHttpResponse.setStatus(200); if (theRequestIsBrowser && theServer.isUseBrowserFriendlyContentTypes()) { @@ -1093,7 +1094,7 @@ public class RestfulServer extends HttpServlet { } } - Bundle bundle = createBundleFromResourceList(theServer.getFhirContext(), theServer.getServerName(), resourceList, theServerBase, theCompleteUrl, theResult.size()); + Bundle bundle = createBundleFromResourceList(theServer.getFhirContext(), theServer.getServerName(), resourceList, theServerBase, theCompleteUrl, theResult.size(), theBundleType); bundle.setPublished(theResult.getPublished()); @@ -1115,16 +1116,24 @@ public class RestfulServer extends HttpServlet { return bundle; } - public static Bundle createBundleFromResourceList(FhirContext theContext, String theAuthor, List theResult, String theServerBase, String theCompleteUrl, int theTotalResults) { + public static Bundle createBundleFromResourceList(FhirContext theContext, String theAuthor, List theResult, String theServerBase, String theCompleteUrl, int theTotalResults, BundleTypeEnum theBundleType) { Bundle bundle = new Bundle(); bundle.getAuthorName().setValue(theAuthor); bundle.getBundleId().setValue(UUID.randomUUID().toString()); bundle.getPublished().setToCurrentTimeInLocalTimeZone(); bundle.getLinkBase().setValue(theServerBase); bundle.getLinkSelf().setValue(theCompleteUrl); + bundle.getType().setValueAsEnum(theBundleType); List includedResources = new ArrayList(); Set addedResourceIds = new HashSet(); + + for (IResource next : theResult) { + if (next.getId().isEmpty() == false) { + addedResourceIds.add(next.getId()); + } + } + for (IResource next : theResult) { Set containedIds = new HashSet(); diff --git a/hapi-fhir-jpaserver-test/src/test/java/ca/uhn/fhir/jpa/test/CompleteResourceProviderTest.java b/hapi-fhir-jpaserver-test/src/test/java/ca/uhn/fhir/jpa/test/CompleteResourceProviderTest.java index b7ae1066ecd..1677869582a 100644 --- a/hapi-fhir-jpaserver-test/src/test/java/ca/uhn/fhir/jpa/test/CompleteResourceProviderTest.java +++ b/hapi-fhir-jpaserver-test/src/test/java/ca/uhn/fhir/jpa/test/CompleteResourceProviderTest.java @@ -40,6 +40,7 @@ import ca.uhn.fhir.model.dev.valueset.EncounterStateEnum; import ca.uhn.fhir.model.dstu.valueset.NarrativeStatusEnum; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.valueset.BundleEntryStatusEnum; +import ca.uhn.fhir.model.valueset.BundleTypeEnum; import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.client.IGenericClient; @@ -111,6 +112,7 @@ public class CompleteResourceProviderTest { { Bundle returned = ourClient.search().forResource(Patient.class).encodedXml().execute(); assertThat(returned.size(), greaterThan(1)); + assertEquals(BundleTypeEnum.SEARCHSET, returned.getType().getValueAsEnum()); } { Bundle returned = ourClient.search().forResource(Patient.class).encodedJson().execute(); @@ -141,8 +143,8 @@ public class CompleteResourceProviderTest { assertEquals(2, found.size()); assertEquals(Patient.class, found.getEntries().get(0).getResource().getClass()); - assertEquals(null, found.getEntries().get(0).getStatus().getValueAsEnum()); - assertEquals(null, found.getEntries().get(0).getResource().getResourceMetadata().get(ResourceMetadataKeyEnum.ENTRY_STATUS)); + assertEquals(BundleEntryStatusEnum.MATCH, found.getEntries().get(0).getStatus().getValueAsEnum()); + assertEquals(BundleEntryStatusEnum.MATCH, found.getEntries().get(0).getResource().getResourceMetadata().get(ResourceMetadataKeyEnum.ENTRY_STATUS)); assertEquals(Organization.class, found.getEntries().get(1).getResource().getClass()); assertEquals(BundleEntryStatusEnum.INCLUDE, found.getEntries().get(1).getStatus().getValueAsEnum()); assertEquals(BundleEntryStatusEnum.INCLUDE, found.getEntries().get(1).getResource().getResourceMetadata().get(ResourceMetadataKeyEnum.ENTRY_STATUS)); diff --git a/hapi-fhir-structures-dev/src/test/java/ca/uhn/fhir/rest/client/BundleTypeTest.java b/hapi-fhir-structures-dev/src/test/java/ca/uhn/fhir/rest/client/BundleTypeTest.java new file mode 100644 index 00000000000..2ac9e5d8d3b --- /dev/null +++ b/hapi-fhir-structures-dev/src/test/java/ca/uhn/fhir/rest/client/BundleTypeTest.java @@ -0,0 +1,78 @@ +package ca.uhn.fhir.rest.client; + +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.StringReader; +import java.nio.charset.Charset; +import java.util.Arrays; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.input.ReaderInputStream; +import org.apache.http.HttpResponse; +import org.apache.http.ProtocolVersion; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.message.BasicHeader; +import org.apache.http.message.BasicStatusLine; +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.api.Bundle; +import ca.uhn.fhir.model.api.IResource; +import ca.uhn.fhir.model.dev.resource.Patient; +import ca.uhn.fhir.model.valueset.BundleTypeEnum; +import ca.uhn.fhir.rest.server.Constants; + +public class BundleTypeTest { + + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BundleTypeTest.class); + private FhirContext ourCtx; + private HttpClient ourHttpClient; + + private HttpResponse ourHttpResponse; + + @Before + public void before() { + ourCtx = FhirContext.forDstu2(); + + ourHttpClient = mock(HttpClient.class, new ReturnsDeepStubs()); + ourCtx.getRestfulClientFactory().setHttpClient(ourHttpClient); + ourCtx.getRestfulClientFactory().setServerValidationModeEnum(ServerValidationModeEnum.NEVER); + + ourHttpResponse = mock(HttpResponse.class, new ReturnsDeepStubs()); + } + + @Test + public void testTransaction() throws Exception { + String retVal = ourCtx.newXmlParser().encodeBundleToString(new Bundle()); + + ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class); + when(ourHttpClient.execute(capt.capture())).thenReturn(ourHttpResponse); + when(ourHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK")); + when(ourHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_ATOM_XML + "; charset=UTF-8")); + when(ourHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(retVal), Charset.forName("UTF-8"))); + + Patient p1 = new Patient(); + p1.addIdentifier().setSystem("urn:system").setValue("value"); + + IGenericClient client = ourCtx.newRestfulGenericClient("http://foo"); + client.transaction().withResources(Arrays.asList((IResource) p1)).execute(); + + HttpUriRequest value = capt.getValue(); + + assertTrue("Expected request of type POST on long params list", value instanceof HttpPost); + HttpPost post = (HttpPost) value; + String body = IOUtils.toString(post.getEntity().getContent()); + ourLog.info(body); + + assertThat(body, Matchers.containsString(" findPatient() { + ArrayList retVal = new ArrayList(); + + Patient patient = new Patient(); + patient.setId("1"); + patient.addIdentifier().setSystem("system").setValue("identifier123"); + retVal.add(patient); + return retVal; + } + + @Override + public Class getResourceType() { + return Patient.class; + } + + } + +} diff --git a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/parser/ContainedResourceEncodingTest.java b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/parser/ContainedResourceEncodingTest.java index ca8c2cb2ce8..2cd2c25fcfc 100644 --- a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/parser/ContainedResourceEncodingTest.java +++ b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/parser/ContainedResourceEncodingTest.java @@ -196,7 +196,7 @@ public class ContainedResourceEncodingTest { List list = new ArrayList(); list.add(dr); - Bundle bundle = RestfulServer.createBundleFromResourceList(new FhirContext(), null, list, null, null, 0); + Bundle bundle = RestfulServer.createBundleFromResourceList(new FhirContext(), null, list, null, null, 0, null); IParser parser = this.ctx.newXmlParser().setPrettyPrint(true); String xml = parser.encodeBundleToString(bundle); @@ -235,7 +235,7 @@ public class ContainedResourceEncodingTest { List list = new ArrayList(); list.add(dr); - Bundle bundle = RestfulServer.createBundleFromResourceList(new FhirContext(), null, list, null, null, 0); + Bundle bundle = RestfulServer.createBundleFromResourceList(new FhirContext(), null, list, null, null, 0, null); IParser parser = this.ctx.newXmlParser().setPrettyPrint(true); String xml = parser.encodeBundleToString(bundle); diff --git a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java index e30326a7c01..c607d942166 100644 --- a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java +++ b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java @@ -1,6 +1,8 @@ package ca.uhn.fhir.parser; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.stringContainsInOrder; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; @@ -30,7 +32,6 @@ import org.hamcrest.core.StringContains; import org.hamcrest.text.StringContainsInOrder; import org.junit.BeforeClass; import org.junit.Test; -import org.mockito.Matchers; import org.xml.sax.SAXException; import ca.uhn.fhir.context.ConfigurationException; @@ -277,6 +278,7 @@ public class XmlParserTest { } + @Test public void testEncodeContained() { IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true); diff --git a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/client/SearchTest.java b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/client/SearchTest.java index f1a022b0bd9..518fa51b59c 100644 --- a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/client/SearchTest.java +++ b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/client/SearchTest.java @@ -44,56 +44,39 @@ import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException; public class SearchTest { - private FhirContext ctx; - private HttpClient httpClient; - private HttpResponse httpResponse; + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SearchTest.class); + + private FhirContext ourCtx; + private HttpClient ourHttpClient; + private HttpResponse ourHttpResponse; @Before public void before() { - ctx = new FhirContext(Patient.class, Conformance.class); + ourCtx = new FhirContext(Patient.class, Conformance.class); - httpClient = mock(HttpClient.class, new ReturnsDeepStubs()); - ctx.getRestfulClientFactory().setHttpClient(httpClient); - ctx.getRestfulClientFactory().setServerValidationModeEnum(ServerValidationModeEnum.NEVER); + ourHttpClient = mock(HttpClient.class, new ReturnsDeepStubs()); + ourCtx.getRestfulClientFactory().setHttpClient(ourHttpClient); + ourCtx.getRestfulClientFactory().setServerValidationModeEnum(ServerValidationModeEnum.NEVER); - httpResponse = mock(HttpResponse.class, new ReturnsDeepStubs()); + ourHttpResponse = mock(HttpResponse.class, new ReturnsDeepStubs()); } - @Test - public void testReturnTypedList() throws Exception { - String retVal = "<id>bc59fca7-0a8f-4caf-abef-45c8d53ece6a</id><link rel=\"self\" href=\"http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Encounter?identifier=urn%3Aoid%3A1.3.6.1.4.1.12201.2%7C11410000159&_include=Encounter.participant&_include=Encounter.location.location&_include=Encounter.subject\"/><link rel=\"fhir-base\" href=\"http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2\"/><os:totalResults xmlns:os=\"http://a9.com/-/spec/opensearch/1.1/\">1</os:totalResults><published>2014-08-08T14:46:16.497-04:00</published><author><name>HAPI FHIR Server</name></author><entry><title>Encounter 5994268http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Encounter/59942682014-08-05T12:00:11.000-04:002014-08-05T11:59:21.000-04:00
No narrative template available for resource profile: http://fhir.connectinggta.ca/Profile/encounter
Patient 5993715http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Patient/59937152014-08-08T14:46:16-04:00
Person CHA
IdentifierUHN MRN 7018614
Address100 Dundas street west
Toronto ON Can
Date of birth01 January 1988
Practitioner Practitioner/5738815http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Practitioner/57388152014-08-08T13:53:52.000-04:002009-12-04T13:43:11.000-05:00
No narrative template available for resource profile: http://hl7.org/fhir/profiles/Practitioner
Location Location/5994269http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Location/59942692014-08-08T14:46:16-04:00
No narrative template available for resource profile: http://hl7.org/fhir/profiles/Location
"; - - ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class); - when(httpClient.execute(capt.capture())).thenReturn(httpResponse); - when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK")); - when(httpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_ATOM_XML+ "; charset=UTF-8")); - when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(retVal), Charset.forName("UTF-8"))); - - ITestClient client = ctx.newRestfulClient(ITestClient.class, "http://foo"); - List found = client.search(); - assertEquals(1, found.size()); - - Encounter encounter = found.get(0); - assertNotNull(encounter.getSubject().getResource()); - - } - @Test public void testPostOnLongParamsList() throws Exception { String retVal = "<id>bc59fca7-0a8f-4caf-abef-45c8d53ece6a</id><link rel=\"self\" href=\"http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Encounter?identifier=urn%3Aoid%3A1.3.6.1.4.1.12201.2%7C11410000159&_include=Encounter.participant&_include=Encounter.location.location&_include=Encounter.subject\"/><link rel=\"fhir-base\" href=\"http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2\"/><os:totalResults xmlns:os=\"http://a9.com/-/spec/opensearch/1.1/\">1</os:totalResults><published>2014-08-08T14:46:16.497-04:00</published><author><name>HAPI FHIR Server</name></author><entry><title>Encounter 5994268http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Encounter/59942682014-08-05T12:00:11.000-04:002014-08-05T11:59:21.000-04:00
No narrative template available for resource profile: http://fhir.connectinggta.ca/Profile/encounter
Patient 5993715http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Patient/59937152014-08-08T14:46:16-04:00
Person CHA
IdentifierUHN MRN 7018614
Address100 Dundas street west
Toronto ON Can
Date of birth01 January 1988
Practitioner Practitioner/5738815http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Practitioner/57388152014-08-08T13:53:52.000-04:002009-12-04T13:43:11.000-05:00
No narrative template available for resource profile: http://hl7.org/fhir/profiles/Practitioner
Location Location/5994269http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Location/59942692014-08-08T14:46:16-04:00
No narrative template available for resource profile: http://hl7.org/fhir/profiles/Location
"; ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class); - when(httpClient.execute(capt.capture())).thenReturn(httpResponse); - when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK")); - when(httpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_ATOM_XML+ "; charset=UTF-8")); - when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(retVal), Charset.forName("UTF-8"))); + when(ourHttpClient.execute(capt.capture())).thenReturn(ourHttpResponse); + when(ourHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK")); + when(ourHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_ATOM_XML + "; charset=UTF-8")); + when(ourHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(retVal), Charset.forName("UTF-8"))); - ITestClient client = ctx.newRestfulClient(ITestClient.class, "http://foo"); + ITestClient client = ourCtx.newRestfulClient(ITestClient.class, "http://foo"); Set includes = new HashSet(); includes.add(new Include("one")); includes.add(new Include("two")); TokenOrListParam params = new TokenOrListParam(); - for(int i=0; i<1000; i++){ + for (int i = 0; i < 1000; i++) { params.add(new TokenParam("system", "value")); } List found = client.searchByList(params, includes); @@ -107,23 +90,38 @@ public class SearchTest { assertTrue("Expected request of type POST on long params list", value instanceof HttpPost); HttpPost post = (HttpPost) value; String body = IOUtils.toString(post.getEntity().getContent()); - System.out.println(body); + ourLog.info(body); assertThat(body, Matchers.containsString("_include=one")); assertThat(body, Matchers.containsString("_include=two")); } - - private interface ITestClient extends IBasicClient - { + + @Test + public void testReturnTypedList() throws Exception { + String retVal = "<id>bc59fca7-0a8f-4caf-abef-45c8d53ece6a</id><link rel=\"self\" href=\"http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Encounter?identifier=urn%3Aoid%3A1.3.6.1.4.1.12201.2%7C11410000159&_include=Encounter.participant&_include=Encounter.location.location&_include=Encounter.subject\"/><link rel=\"fhir-base\" href=\"http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2\"/><os:totalResults xmlns:os=\"http://a9.com/-/spec/opensearch/1.1/\">1</os:totalResults><published>2014-08-08T14:46:16.497-04:00</published><author><name>HAPI FHIR Server</name></author><entry><title>Encounter 5994268http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Encounter/59942682014-08-05T12:00:11.000-04:002014-08-05T11:59:21.000-04:00
No narrative template available for resource profile: http://fhir.connectinggta.ca/Profile/encounter
Patient 5993715http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Patient/59937152014-08-08T14:46:16-04:00
Person CHA
IdentifierUHN MRN 7018614
Address100 Dundas street west
Toronto ON Can
Date of birth01 January 1988
Practitioner Practitioner/5738815http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Practitioner/57388152014-08-08T13:53:52.000-04:002009-12-04T13:43:11.000-05:00
No narrative template available for resource profile: http://hl7.org/fhir/profiles/Practitioner
Location Location/5994269http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Location/59942692014-08-08T14:46:16-04:00
No narrative template available for resource profile: http://hl7.org/fhir/profiles/Location
"; + + ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class); + when(ourHttpClient.execute(capt.capture())).thenReturn(ourHttpResponse); + when(ourHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK")); + when(ourHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_ATOM_XML + "; charset=UTF-8")); + when(ourHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(retVal), Charset.forName("UTF-8"))); + + ITestClient client = ourCtx.newRestfulClient(ITestClient.class, "http://foo"); + List found = client.search(); + assertEquals(1, found.size()); + + Encounter encounter = found.get(0); + assertNotNull(encounter.getSubject().getResource()); + + } + + private interface ITestClient extends IBasicClient { @Search List search(); - + @Search - List searchByList( - @RequiredParam(name = Encounter.SP_IDENTIFIER) TokenOrListParam tokenOrListParam, - @IncludeParam Set theIncludes - ) throws BaseServerResponseException; + List searchByList(@RequiredParam(name = Encounter.SP_IDENTIFIER) TokenOrListParam tokenOrListParam, @IncludeParam Set theIncludes) throws BaseServerResponseException; } - + } diff --git a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/CreateTest.java b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/CreateTest.java index 9afbe21420e..aba4a74adaf 100644 --- a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/CreateTest.java +++ b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/CreateTest.java @@ -18,6 +18,7 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletHandler; import org.eclipse.jetty.servlet.ServletHolder; +import org.hamcrest.core.StringContains; import org.hl7.fhir.instance.model.annotations.ResourceDef; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -70,6 +71,7 @@ public class CreateTest { assertEquals(201, status.getStatusLine().getStatusCode()); assertEquals("http://localhost:" + ourPort + "/Patient/001/_history/002", status.getFirstHeader("location").getValue()); + assertThat(status.getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue(), StringContains.containsString("UTF-8")); } diff --git a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/RestfulServerMethodTest.java b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/RestfulServerMethodTest.java index 589f70542bd..2577f70b5b5 100644 --- a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/RestfulServerMethodTest.java +++ b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/RestfulServerMethodTest.java @@ -89,6 +89,25 @@ public class RestfulServerMethodTest { private static Server ourServer; private static RestfulServer ourRestfulServer; + @Test + public void testCreateBundleDoesntCreateDoubleEntries() { + + List resources = new ArrayList(); + + Patient p = new Patient(); + p.setId("Patient/1"); + resources.add(p); + + Organization o = new Organization(); + o.setId("Organization/2"); + resources.add(o); + + p.getManagingOrganization().setResource(o); + + Bundle bundle = RestfulServer.createBundleFromResourceList(ourCtx, "", resources, "http://foo", "http://foo", 2, null); + assertEquals(2, bundle.getEntries().size()); + } + @Test public void test404IsPropagatedCorrectly() throws Exception {