theParams) throws InternalErrorException, InvalidRequestException {
if (theParams.size() == 0 || theParams.get(0).size() == 0) {
return "";
}
if (theParams.size() > 1 || theParams.get(0).size() > 1) {
- throw new InvalidRequestException("Multiple values detected");
+ throw new InvalidRequestException("Multiple values detected for non-repeatable parameter '" + theName + "'. This server is not configured to allow multiple (AND) values for this param.");
}
return theParams.get(0).get(0);
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 05d42adb650..4f2c9b4b14b 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
@@ -149,7 +149,7 @@ public class RestfulServer extends HttpServlet {
private void assertProviderIsValid(Object theNext) throws ConfigurationException {
if (Modifier.isPublic(theNext.getClass().getModifiers()) == false) {
- throw new ConfigurationException("Can not use provider '" + theNext.getClass() + "' - Must be public");
+ throw new ConfigurationException("Can not use provider '" + theNext.getClass() + "' - Class ust be public");
}
}
diff --git a/hapi-fhir-base/src/site/example/java/example/GenericClientExample.java b/hapi-fhir-base/src/site/example/java/example/GenericClientExample.java
index 0e70994bd67..97920283720 100644
--- a/hapi-fhir-base/src/site/example/java/example/GenericClientExample.java
+++ b/hapi-fhir-base/src/site/example/java/example/GenericClientExample.java
@@ -13,6 +13,7 @@ import ca.uhn.fhir.model.dstu.resource.Organization;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.client.IGenericClient;
+import ca.uhn.fhir.rest.method.SearchStyleEnum;
public class GenericClientExample {
@@ -100,10 +101,25 @@ Bundle response = client.search()
.forResource(Patient.class)
.where(Patient.BIRTHDATE.beforeOrEquals().day("2011-01-01"))
.and(Patient.PROVIDER.hasChainedProperty(Organization.NAME.matches().value("Health")))
- .andLogRequestAndResponse(true)
.execute();
//END SNIPPET: search
+//START SNIPPET: searchOr
+response = client.search()
+ .forResource(Patient.class)
+ .where(Patient.FAMILY.matches().values("Smith", "Smyth"))
+ .execute();
+//END SNIPPET: searchOr
+
+//START SNIPPET: searchAnd
+response = client.search()
+ .forResource(Patient.class)
+ .where(Patient.ADDRESS.matches().values("Toronto"))
+ .where(Patient.ADDRESS.matches().values("Ontario"))
+ .where(Patient.ADDRESS.matches().values("Canada"))
+ .execute();
+//END SNIPPET: searchAnd
+
//START SNIPPET: searchAdv
response = client.search()
.forResource(Patient.class)
@@ -117,6 +133,15 @@ response = client.search()
.execute();
//END SNIPPET: searchAdv
+//START SNIPPET: searchPost
+response = client.search()
+ .forResource("Patient")
+ .where(Patient.NAME.matches().value("Tester"))
+ .usingStyle(SearchStyleEnum.POST)
+ .execute();
+//END SNIPPET: searchPost
+
+
//START SNIPPET: searchComposite
response = client.search()
.forResource("Observation")
diff --git a/hapi-fhir-base/src/site/xdoc/doc_rest_client.xml b/hapi-fhir-base/src/site/xdoc/doc_rest_client.xml
index 8abdb98f29a..2a24d69c220 100644
--- a/hapi-fhir-base/src/site/xdoc/doc_rest_client.xml
+++ b/hapi-fhir-base/src/site/xdoc/doc_rest_client.xml
@@ -99,6 +99,30 @@
value="src/site/example/java/example/GenericClientExample.java" />
+ Search - Multi-valued Parameters (ANY/OR)
+
+ To search for a set of possible values where ANY should be matched,
+ you can provide multiple values to a parameter, as shown in the example below.
+ This leads to a URL resembling ?family=Smith,Smyth
+
+
+
+
+
+
+ Search - Multi-valued Parameters (ALL/AND)
+
+ To search for a set of possible values where ALL should be matched,
+ you can provide multiple instances of a marameter, as shown in the example below.
+ This leads to a URL resembling ?address=Toronto&address=Ontario&address=Canada
+
+
+
+
+
+
Search - Paging
If the server supports paging results, the client has a page method
@@ -123,7 +147,7 @@
value="src/site/example/java/example/GenericClientExample.java" />
-
Search - Query Options
+ Search - Other Query Options
The fluent search also has methods for sorting, limiting, specifying
JSON encoding, etc.
@@ -133,6 +157,19 @@
+
+
Search - Using HTTP POST or GET with _search
+
+ The FHIR specification allows several styles of search (HTTP POST, a GET with _search at the end of the URL, etc.)
+ The usingStyle()
method controls which style to use. By default, GET style is used
+ unless the client detects that the request would result in a very long URL (over 8000 chars) in which
+ case the client automatically switches to POST.
+
+
+
+
+
diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/client/GenericClientTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/client/GenericClientTest.java
index 66d86cc3e3b..6080016d255 100644
--- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/client/GenericClientTest.java
+++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/client/GenericClientTest.java
@@ -1,7 +1,11 @@
package ca.uhn.fhir.rest.client;
-import static org.junit.Assert.*;
-import static org.mockito.Mockito.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
import java.io.StringReader;
import java.net.URLEncoder;
@@ -10,10 +14,12 @@ import java.util.Arrays;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream;
+import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.HttpClient;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
@@ -41,6 +47,7 @@ import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.model.primitive.InstantDt;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.exceptions.NonFhirResponseException;
+import ca.uhn.fhir.rest.method.SearchStyleEnum;
import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
@@ -48,13 +55,10 @@ import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
public class GenericClientTest {
private static FhirContext myCtx;
+ private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(GenericClientTest.class);
private HttpClient myHttpClient;
- private HttpResponse myHttpResponse;
- @BeforeClass
- public static void beforeClass() {
- myCtx = new FhirContext();
- }
+ private HttpResponse myHttpResponse;
@Before
public void before() {
@@ -65,37 +69,52 @@ public class GenericClientTest {
myHttpResponse = mock(HttpResponse.class, new ReturnsDeepStubs());
}
- @Test
- public void testCreateWithTagNonFluent() throws Exception {
-
- Patient p1 = new Patient();
- p1.addIdentifier("foo:bar", "12345");
- p1.addName().addFamily("Smith").addGiven("John");
- TagList list = new TagList();
- list.addTag("http://hl7.org/fhir/tag", "urn:happytag", "This is a happy resource");
- ResourceMetadataKeyEnum.TAG_LIST.put(p1, list);
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 201, "OK"));
- when(myHttpResponse.getAllHeaders()).thenReturn(new Header[] { new BasicHeader(Constants.HEADER_LOCATION, "/Patient/44/_history/22") });
- 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(""), Charset.forName("UTF-8")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- MethodOutcome outcome = client.create(p1);
- assertEquals("44", outcome.getId().getIdPart());
- assertEquals("22", outcome.getId().getVersionIdPart());
-
- assertEquals("http://example.com/fhir/Patient", capt.getValue().getURI().toString());
- assertEquals("POST", capt.getValue().getMethod());
- Header catH = capt.getValue().getFirstHeader("Category");
- assertNotNull(Arrays.asList(capt.getValue().getAllHeaders()).toString(), catH);
- assertEquals("urn:happytag; label=\"This is a happy resource\"; scheme=\"http://hl7.org/fhir/tag\"", catH.getValue());
+ private String getPatientFeedWithOneResult() {
+ //@formatter:off
+ String msg = "\n" +
+ " \n" +
+ "d039f91a-cc3c-4013-988e-af4d8d0614bd \n" +
+ "1 \n" +
+ "2014-03-11T16:35:07-04:00 \n" +
+ "\n" +
+ "ca.uhn.fhir.rest.server.DummyRestfulServer \n" +
+ " \n" +
+ "\n" +
+ ""
+ + ""
+ + "John Cardinal: 444333333
"
+ + " "
+ + " "
+ + " "
+ + " "
+ + "
"
+ + " "
+ + " "
+ + " \n"
+ + " \n"
+ + " ";
+ //@formatter:on
+ return msg;
}
+ private String getResourceResult() {
+ //@formatter:off
+ String msg =
+ ""
+ + "John Cardinal: 444333333
"
+ + " "
+ + " "
+ + " "
+ + " "
+ + "
"
+ + " "
+ + " ";
+ //@formatter:on
+ return msg;
+ }
+
+
@Test
public void testCreateWithTag() throws Exception {
@@ -140,7 +159,621 @@ public class GenericClientTest {
}
+ @Test
+ public void testCreateWithTagNonFluent() throws Exception {
+
+ Patient p1 = new Patient();
+ p1.addIdentifier("foo:bar", "12345");
+ p1.addName().addFamily("Smith").addGiven("John");
+ TagList list = new TagList();
+ list.addTag("http://hl7.org/fhir/tag", "urn:happytag", "This is a happy resource");
+ ResourceMetadataKeyEnum.TAG_LIST.put(p1, list);
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 201, "OK"));
+ when(myHttpResponse.getAllHeaders()).thenReturn(new Header[] { new BasicHeader(Constants.HEADER_LOCATION, "/Patient/44/_history/22") });
+ 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(""), Charset.forName("UTF-8")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ MethodOutcome outcome = client.create(p1);
+ assertEquals("44", outcome.getId().getIdPart());
+ assertEquals("22", outcome.getId().getVersionIdPart());
+
+ assertEquals("http://example.com/fhir/Patient", capt.getValue().getURI().toString());
+ assertEquals("POST", capt.getValue().getMethod());
+ Header catH = capt.getValue().getFirstHeader("Category");
+ assertNotNull(Arrays.asList(capt.getValue().getAllHeaders()).toString(), catH);
+ assertEquals("urn:happytag; label=\"This is a happy resource\"; scheme=\"http://hl7.org/fhir/tag\"", catH.getValue());
+ }
+
+ @Test
+ public void testDelete() throws Exception {
+ OperationOutcome oo = new OperationOutcome();
+ oo.addIssue().addLocation().setValue("testDelete01");
+ String ooStr = myCtx.newXmlParser().encodeResourceToString(oo);
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 201, "OK"));
+ when(myHttpResponse.getAllHeaders()).thenReturn(new Header[] { new BasicHeader(Constants.HEADER_LOCATION, "/Patient/44/_history/22") });
+ 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(ooStr), Charset.forName("UTF-8")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ OperationOutcome outcome = client.delete().resourceById("Patient", "123").execute();
+
+ assertEquals("http://example.com/fhir/Patient/123", capt.getValue().getURI().toString());
+ assertEquals("DELETE", capt.getValue().getMethod());
+ assertEquals("testDelete01",outcome.getIssueFirstRep().getLocationFirstRep().getValue());
+
+ when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader("LKJHLKJGLKJKLL"), Charset.forName("UTF-8")));
+ outcome = client.delete().resourceById(new IdDt("Location", "123","456")).prettyPrint().encodedJson().execute();
+
+ assertEquals("http://example.com/fhir/Location/123?_format=json&_pretty=true", capt.getAllValues().get(1).getURI().toString());
+ assertEquals("DELETE", capt.getValue().getMethod());
+ assertEquals(null,outcome);
+
+ }
+
+ @Test
+ public void testGetTags() throws Exception {
+
+ TagList tagList = new TagList();
+ tagList.add(new Tag("CCC", "AAA", "BBB"));
+ String msg = myCtx.newXmlParser().encodeTagListToString(tagList);
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ //@formatter:off
+ TagList response = client.getTags()
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/_tags", capt.getValue().getURI().toString());
+ assertEquals(1, response.size());
+ assertEquals("CCC", response.get(0).getScheme());
+
+ // Now for patient
+
+ when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8")));
+ //@formatter:off
+ response = client.getTags().forResource(Patient.class)
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/Patient/_tags", capt.getValue().getURI().toString());
+ assertEquals(1, response.size());
+ assertEquals("CCC", response.get(0).getScheme());
+
+ }
+
+
+ @Test
+ public void testRead() throws Exception {
+
+ String msg = getResourceResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+ Header[] headers = new Header[2];
+ headers[0] = new BasicHeader(Constants.HEADER_LAST_MODIFIED, "Wed, 15 Nov 1995 04:58:08 GMT");
+ headers[1] = new BasicHeader(Constants.HEADER_CONTENT_LOCATION, "http://foo.com/Patient/123/_history/2333");
+ when(myHttpResponse.getAllHeaders()).thenReturn(headers);
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ //@formatter:off
+ Patient response = client.read(Patient.class, new IdDt("Patient/1234"));
+ //@formatter:on
+
+ assertThat(response.getNameFirstRep().getFamilyAsSingleString(), StringContains.containsString("Cardinal"));
+
+ assertEquals("http://foo.com/Patient/123/_history/2333", response.getId().getValue());
+
+ InstantDt lm = (InstantDt) response.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED);
+ lm.setTimeZoneZulu(true);
+ assertEquals("1995-11-15T04:58:08.000Z", lm.getValueAsString());
+
+ }
+
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchAllResources() throws Exception {
+
+ String msg = getPatientFeedWithOneResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ //@formatter:off
+ Bundle response = client.search()
+ .forAllResources()
+ .where(Patient.NAME.matches().value("james"))
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/?name=james", capt.getValue().getURI().toString());
+
+ }
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchByComposite() throws Exception {
+
+ String msg = getPatientFeedWithOneResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://foo");
+
+ //@formatter:off
+ Bundle response = client.search()
+ .forResource("Observation")
+ .where(Observation.NAME_VALUE_DATE
+ .withLeft(Observation.NAME.exactly().code("FOO$BAR"))
+ .withRight(Observation.VALUE_DATE.exactly().day("2001-01-01"))
+ )
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://foo/Observation?" + Observation.SP_NAME_VALUE_DATE + "=" + URLEncoder.encode("FOO\\$BAR$2001-01-01","UTF-8"), capt.getValue().getURI().toString());
+
+ }
+
+
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchByDate() throws Exception {
+
+ String msg = getPatientFeedWithOneResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ //@formatter:off
+ Bundle response = client.search()
+ .forResource(Patient.class)
+ .encodedJson()
+ .where(Patient.BIRTHDATE.beforeOrEquals().day("2012-01-22"))
+ .and(Patient.BIRTHDATE.after().day("2011-01-01"))
+ .include(Patient.INCLUDE_MANAGINGORGANIZATION)
+ .sort().ascending(Patient.BIRTHDATE)
+ .sort().descending(Patient.NAME)
+ .limitTo(123)
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/Patient?birthdate=%3C%3D2012-01-22&birthdate=%3E2011-01-01&_include=Patient.managingOrganization&_sort%3Aasc=birthdate&_sort%3Adesc=name&_count=123&_format=json", capt.getValue().getURI().toString());
+
+ }
+
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchByNumberExact() throws Exception {
+
+ String msg = new FhirContext().newXmlParser().encodeBundleToString(new Bundle());
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ //@formatter:off
+ Bundle response = client.search()
+ .forResource(Observation.class)
+ .where(Observation.VALUE_QUANTITY.greaterThan().number(123).andUnits("foo", "bar"))
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/Observation?value-quantity=%3E123%7Cfoo%7Cbar", capt.getValue().getURI().toString());
+
+ }
+
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchByQuantity() throws Exception {
+
+ String msg = getPatientFeedWithOneResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ //@formatter:off
+ Bundle response = client.search()
+ .forResource(Patient.class)
+ .where(Encounter.LENGTH.exactly().number(123))
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/Patient?length=123", capt.getValue().getURI().toString());
+
+ }
+
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchByReferenceProperty() throws Exception {
+
+ String msg = getPatientFeedWithOneResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+
+ //@formatter:off
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ Bundle response = client.search()
+ .forResource(Patient.class)
+ .where(Patient.PROVIDER.hasChainedProperty(Organization.NAME.matches().value("ORG0")))
+ .execute();
+
+ assertEquals("http://example.com/fhir/Patient?provider.name=ORG0", capt.getValue().getURI().toString());
+ //@formatter:on
+
+ }
+
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchByReferenceSimple() throws Exception {
+
+ String msg = getPatientFeedWithOneResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ //@formatter:off
+ Bundle response = client.search()
+ .forResource("Patient")
+ .where(Patient.PROVIDER.hasId("123"))
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/Patient?provider=123", capt.getValue().getURI().toString());
+
+ }
+
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchByString() throws Exception {
+
+ String msg = getPatientFeedWithOneResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ //@formatter:off
+ Bundle response = client.search()
+ .forResource("Patient")
+ .where(Patient.NAME.matches().value("james"))
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/Patient?name=james", capt.getValue().getURI().toString());
+
+ when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8")));
+ //@formatter:off
+ response = client.search()
+ .forResource("Patient")
+ .where(Patient.NAME.matches().values("AAA", "BBB", "C,C"))
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/Patient?name=" + URLEncoder.encode("AAA,BBB,C\\,C","UTF-8"), capt.getAllValues().get(1).getURI().toString());
+
+ }
+
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchByStringExact() throws Exception {
+
+ String msg = getPatientFeedWithOneResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ //@formatter:off
+ Bundle response = client.search()
+ .forResource("Patient")
+ .where(Patient.NAME.matchesExactly().value("james"))
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/Patient?name%3Aexact=james", capt.getValue().getURI().toString());
+
+ }
+
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchByToken() throws Exception {
+
+ String msg = getPatientFeedWithOneResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ //@formatter:off
+ Bundle response = client.search()
+ .forResource("Patient")
+ .where(Patient.IDENTIFIER.exactly().systemAndCode("http://example.com/fhir", "ZZZ"))
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/Patient?identifier=http%3A%2F%2Fexample.com%2Ffhir%7CZZZ", capt.getValue().getURI().toString());
+
+ when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8")));
+ //@formatter:off
+ response = client.search()
+ .forResource("Patient")
+ .where(Patient.IDENTIFIER.exactly().code("ZZZ"))
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/Patient?identifier=ZZZ", capt.getAllValues().get(1).getURI().toString());
+
+ when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8")));
+ //@formatter:off
+ response = client.search()
+ .forResource("Patient")
+ .where(Patient.IDENTIFIER.exactly().identifiers(new ca.uhn.fhir.model.dstu.composite.IdentifierDt("A", "B"), new ca.uhn.fhir.model.dstu.composite.IdentifierDt("C", "D")))
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/Patient?identifier=" + URLEncoder.encode("A|B,C|D", "UTF-8"), capt.getAllValues().get(2).getURI().toString());
+
+ }
+
+
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchUsingPost() throws Exception {
+
+ String msg = getPatientFeedWithOneResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ //@formatter:off
+ Bundle response = client.search()
+ .forResource("Patient")
+ .where(Patient.NAME.matches().value("james"))
+ .usingStyle(SearchStyleEnum.POST)
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/Patient/_search", capt.getValue().getURI().toString());
+
+ HttpEntityEnclosingRequestBase enc = (HttpEntityEnclosingRequestBase) capt.getValue();
+ UrlEncodedFormEntity ent = (UrlEncodedFormEntity) enc.getEntity();
+ String string = IOUtils.toString(ent.getContent());
+ ourLog.info(string);
+ assertEquals("name=james", string);
+ }
+
+
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchAutomaticallyUsesPost() throws Exception {
+
+ String msg = getPatientFeedWithOneResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ String longValue = StringUtils.leftPad("", 20000, 'B');
+
+ //@formatter:off
+ Bundle response = client.search()
+ .forResource("Patient")
+ .where(Patient.NAME.matches().value(longValue))
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/Patient/_search", capt.getValue().getURI().toString());
+
+ HttpEntityEnclosingRequestBase enc = (HttpEntityEnclosingRequestBase) capt.getValue();
+ UrlEncodedFormEntity ent = (UrlEncodedFormEntity) enc.getEntity();
+ String string = IOUtils.toString(ent.getContent());
+ ourLog.info(string);
+ assertEquals("name="+longValue, string);
+ }
+
+
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchUsingGetSearch() throws Exception {
+
+ String msg = getPatientFeedWithOneResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ //@formatter:off
+ Bundle response = client.search()
+ .forResource("Patient")
+ .where(Patient.NAME.matches().value("james"))
+ .usingStyle(SearchStyleEnum.GET_WITH_SEARCH)
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir/Patient/_search?name=james", capt.getValue().getURI().toString());
+ }
+
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchWithInternalServerError() throws Exception {
+
+ String msg = getPatientFeedWithOneResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 500, "INTERNAL ERRORS"));
+ when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_TEXT + "; charset=UTF-8"));
+ when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader("Server Issues!"), Charset.forName("UTF-8")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ try {
+ client.search().forResource(Patient.class).execute();
+ fail();
+ } catch (InternalErrorException e) {
+ assertEquals(e.getMessage(), "HTTP 500 INTERNAL ERRORS: Server Issues!");
+ assertEquals(e.getResponseBody(), "Server Issues!");
+ }
+
+ }
+
+ @SuppressWarnings("unused")
+ @Test
+ public void testSearchWithNonFhirResponse() throws Exception {
+
+ String msg = getPatientFeedWithOneResult();
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"));
+ when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_TEXT + "; charset=UTF-8"));
+ when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader("Server Issues!"), Charset.forName("UTF-8")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ try {
+ client.search().forResource(Patient.class).execute();
+ fail();
+ } catch (NonFhirResponseException e) {
+ assertThat(e.getMessage(), StringContains.containsString("Server Issues!"));
+ }
+
+ }
+
+ @Test
+ public void testTransaction() throws Exception {
+ String bundleStr = IOUtils.toString(getClass().getResourceAsStream("/bundle.json"));
+ Bundle bundle = myCtx.newJsonParser().parseBundle(bundleStr);
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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_JSON + "; charset=UTF-8"));
+ when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(bundleStr), Charset.forName("UTF-8")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ //@formatter:off
+ Bundle response = client.transaction()
+ .withBundle(bundle)
+ .execute();
+ //@formatter:on
+
+ assertEquals("http://example.com/fhir", capt.getValue().getURI().toString());
+ assertEquals(bundle.getEntries().get(0).getId(), response.getEntries().get(0).getId());
+ }
+
+
+
+ @Test
+ public void testTransactionJson() throws Exception {
+ String bundleStr = IOUtils.toString(getClass().getResourceAsStream("/bundle.json"));
+ Bundle bundle = myCtx.newJsonParser().parseBundle(bundleStr);
+
+ ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
+ when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
+ 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_JSON + "; charset=UTF-8"));
+ when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(bundleStr), Charset.forName("UTF-8")));
+
+ IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
+
+ //@formatter:off
+ Bundle response = client.transaction()
+ .withBundle(bundle)
+ .encodedJson()
+ .execute();
+ //@formatter:on
+
+ HttpEntityEnclosingRequestBase value = (HttpEntityEnclosingRequestBase) capt.getValue();
+
+ Header ct = value.getEntity().getContentType();
+ assertNotNull(ct);
+ assertEquals(Constants.CT_FHIR_JSON + "; charset=UTF-8", ct.getValue());
+
+ assertEquals("http://example.com/fhir?_format=json", value.getURI().toString());
+ assertThat(IOUtils.toString(value.getEntity().getContent()), StringContains.containsString("\"resourceType\""));
+ assertEquals(bundle.getEntries().get(0).getId(), response.getEntries().get(0).getId());
+ }
+
+
@Test
public void testUpdate() throws Exception {
@@ -202,525 +835,10 @@ public class GenericClientTest {
assertEquals(4, capt.getAllValues().size());
}
-
- @Test
- public void testDelete() throws Exception {
- OperationOutcome oo = new OperationOutcome();
- oo.addIssue().addLocation().setValue("testDelete01");
- String ooStr = myCtx.newXmlParser().encodeResourceToString(oo);
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 201, "OK"));
- when(myHttpResponse.getAllHeaders()).thenReturn(new Header[] { new BasicHeader(Constants.HEADER_LOCATION, "/Patient/44/_history/22") });
- 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(ooStr), Charset.forName("UTF-8")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- OperationOutcome outcome = client.delete().resourceById("Patient", "123").execute();
-
- assertEquals("http://example.com/fhir/Patient/123", capt.getValue().getURI().toString());
- assertEquals("DELETE", capt.getValue().getMethod());
- assertEquals("testDelete01",outcome.getIssueFirstRep().getLocationFirstRep().getValue());
-
- when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader("LKJHLKJGLKJKLL"), Charset.forName("UTF-8")));
- outcome = client.delete().resourceById(new IdDt("Location", "123","456")).prettyPrint().encodedJson().execute();
-
- assertEquals("http://example.com/fhir/Location/123?_format=json&_pretty=true", capt.getAllValues().get(1).getURI().toString());
- assertEquals("DELETE", capt.getValue().getMethod());
- assertEquals(null,outcome);
-
- }
-
-
- private String getPatientFeedWithOneResult() {
- //@formatter:off
- String msg = "\n" +
- " \n" +
- "d039f91a-cc3c-4013-988e-af4d8d0614bd \n" +
- "1 \n" +
- "2014-03-11T16:35:07-04:00 \n" +
- "\n" +
- "ca.uhn.fhir.rest.server.DummyRestfulServer \n" +
- " \n" +
- "\n" +
- ""
- + ""
- + "John Cardinal: 444333333
"
- + " "
- + " "
- + " "
- + " "
- + "
"
- + " "
- + " "
- + " \n"
- + " \n"
- + " ";
- //@formatter:on
- return msg;
- }
-
- private String getResourceResult() {
- //@formatter:off
- String msg =
- ""
- + "John Cardinal: 444333333
"
- + " "
- + " "
- + " "
- + " "
- + "
"
- + " "
- + " ";
- //@formatter:on
- return msg;
- }
-
-
- @SuppressWarnings("unused")
- @Test
- public void testSearchByString() throws Exception {
-
- String msg = getPatientFeedWithOneResult();
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- 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")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- //@formatter:off
- Bundle response = client.search()
- .forResource("Patient")
- .where(Patient.NAME.matches().value("james"))
- .execute();
- //@formatter:on
-
- assertEquals("http://example.com/fhir/Patient?name=james", capt.getValue().getURI().toString());
-
- }
-
- @SuppressWarnings("unused")
- @Test
- public void testSearchAllResources() throws Exception {
-
- String msg = getPatientFeedWithOneResult();
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- 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")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- //@formatter:off
- Bundle response = client.search()
- .forAllResources()
- .where(Patient.NAME.matches().value("james"))
- .execute();
- //@formatter:on
-
- assertEquals("http://example.com/fhir/?name=james", capt.getValue().getURI().toString());
-
- }
-
- @SuppressWarnings("unused")
- @Test
- public void testSearchByStringExact() throws Exception {
-
- String msg = getPatientFeedWithOneResult();
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- 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")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- //@formatter:off
- Bundle response = client.search()
- .forResource("Patient")
- .where(Patient.NAME.matchesExactly().value("james"))
- .execute();
- //@formatter:on
-
- assertEquals("http://example.com/fhir/Patient?name%3Aexact=james", capt.getValue().getURI().toString());
-
- }
-
- @SuppressWarnings("unused")
- @Test
- public void testSearchByNumberExact() throws Exception {
-
- String msg = new FhirContext().newXmlParser().encodeBundleToString(new Bundle());
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- 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")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- //@formatter:off
- Bundle response = client.search()
- .forResource(Observation.class)
- .where(Observation.VALUE_QUANTITY.greaterThan().number(123).andUnits("foo", "bar"))
- .execute();
- //@formatter:on
-
- assertEquals("http://example.com/fhir/Observation?value-quantity=%3E123%7Cfoo%7Cbar", capt.getValue().getURI().toString());
-
- }
-
- @SuppressWarnings("unused")
- @Test
- public void testSearchByQuantity() throws Exception {
-
- String msg = getPatientFeedWithOneResult();
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- 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")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- //@formatter:off
- Bundle response = client.search()
- .forResource(Patient.class)
- .where(Encounter.LENGTH.exactly().number(123))
- .execute();
- //@formatter:on
-
- assertEquals("http://example.com/fhir/Patient?length=123", capt.getValue().getURI().toString());
-
- }
-
- @SuppressWarnings("unused")
- @Test
- public void testSearchByToken() throws Exception {
-
- String msg = getPatientFeedWithOneResult();
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- 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")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- //@formatter:off
- Bundle response = client.search()
- .forResource("Patient")
- .where(Patient.IDENTIFIER.exactly().systemAndCode("http://example.com/fhir", "ZZZ"))
- .execute();
- //@formatter:on
-
- assertEquals("http://example.com/fhir/Patient?identifier=http%3A%2F%2Fexample.com%2Ffhir%7CZZZ", capt.getValue().getURI().toString());
-
- when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8")));
-
- //@formatter:off
- response = client.search()
- .forResource("Patient")
- .where(Patient.IDENTIFIER.exactly().code("ZZZ"))
- .execute();
- //@formatter:on
-
- assertEquals("http://example.com/fhir/Patient?identifier=ZZZ", capt.getAllValues().get(1).getURI().toString());
-
- }
-
- @SuppressWarnings("unused")
- @Test
- public void testSearchByComposite() throws Exception {
-
- String msg = getPatientFeedWithOneResult();
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- 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")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://foo");
-
- //@formatter:off
- Bundle response = client.search()
- .forResource("Observation")
- .where(Observation.NAME_VALUE_DATE
- .withLeft(Observation.NAME.exactly().code("FOO$BAR"))
- .withRight(Observation.VALUE_DATE.exactly().day("2001-01-01"))
- )
- .execute();
- //@formatter:on
-
- assertEquals("http://foo/Observation?" + Observation.SP_NAME_VALUE_DATE + "=" + URLEncoder.encode("FOO\\$BAR$2001-01-01","UTF-8"), capt.getValue().getURI().toString());
-
- }
-
- @Test
- public void testGetTags() throws Exception {
-
- TagList tagList = new TagList();
- tagList.add(new Tag("CCC", "AAA", "BBB"));
- String msg = myCtx.newXmlParser().encodeTagListToString(tagList);
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- 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")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- //@formatter:off
- TagList response = client.getTags()
- .execute();
- //@formatter:on
-
- assertEquals("http://example.com/fhir/_tags", capt.getValue().getURI().toString());
- assertEquals(1, response.size());
- assertEquals("CCC", response.get(0).getScheme());
-
- // Now for patient
-
- when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8")));
- //@formatter:off
- response = client.getTags().forResource(Patient.class)
- .execute();
- //@formatter:on
-
- assertEquals("http://example.com/fhir/Patient/_tags", capt.getValue().getURI().toString());
- assertEquals(1, response.size());
- assertEquals("CCC", response.get(0).getScheme());
-
- }
-
- @Test
- public void testTransaction() throws Exception {
- String bundleStr = IOUtils.toString(getClass().getResourceAsStream("/bundle.json"));
- Bundle bundle = myCtx.newJsonParser().parseBundle(bundleStr);
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- 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_JSON + "; charset=UTF-8"));
- when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(bundleStr), Charset.forName("UTF-8")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- //@formatter:off
- Bundle response = client.transaction()
- .withBundle(bundle)
- .execute();
- //@formatter:on
-
- assertEquals("http://example.com/fhir", capt.getValue().getURI().toString());
- assertEquals(bundle.getEntries().get(0).getId(), response.getEntries().get(0).getId());
- }
-
-
- @Test
- public void testTransactionJson() throws Exception {
- String bundleStr = IOUtils.toString(getClass().getResourceAsStream("/bundle.json"));
- Bundle bundle = myCtx.newJsonParser().parseBundle(bundleStr);
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- 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_JSON + "; charset=UTF-8"));
- when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(bundleStr), Charset.forName("UTF-8")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- //@formatter:off
- Bundle response = client.transaction()
- .withBundle(bundle)
- .encodedJson()
- .execute();
- //@formatter:on
-
- HttpEntityEnclosingRequestBase value = (HttpEntityEnclosingRequestBase) capt.getValue();
-
- Header ct = value.getEntity().getContentType();
- assertNotNull(ct);
- assertEquals(Constants.CT_FHIR_JSON + "; charset=UTF-8", ct.getValue());
-
- assertEquals("http://example.com/fhir?_format=json", value.getURI().toString());
- assertThat(IOUtils.toString(value.getEntity().getContent()), StringContains.containsString("\"resourceType\""));
- assertEquals(bundle.getEntries().get(0).getId(), response.getEntries().get(0).getId());
- }
-
- @SuppressWarnings("unused")
- @Test
- public void testSearchByReferenceSimple() throws Exception {
-
- String msg = getPatientFeedWithOneResult();
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- 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")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- //@formatter:off
- Bundle response = client.search()
- .forResource("Patient")
- .where(Patient.PROVIDER.hasId("123"))
- .execute();
- //@formatter:on
-
- assertEquals("http://example.com/fhir/Patient?provider=123", capt.getValue().getURI().toString());
-
- }
-
- @SuppressWarnings("unused")
- @Test
- public void testSearchByReferenceProperty() throws Exception {
-
- String msg = getPatientFeedWithOneResult();
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- 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")));
-
- //@formatter:off
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- Bundle response = client.search()
- .forResource(Patient.class)
- .where(Patient.PROVIDER.hasChainedProperty(Organization.NAME.matches().value("ORG0")))
- .execute();
-
- assertEquals("http://example.com/fhir/Patient?provider.name=ORG0", capt.getValue().getURI().toString());
- //@formatter:on
-
- }
-
- @SuppressWarnings("unused")
- @Test
- public void testSearchByDate() throws Exception {
-
- String msg = getPatientFeedWithOneResult();
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- 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")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- //@formatter:off
- Bundle response = client.search()
- .forResource(Patient.class)
- .encodedJson()
- .where(Patient.BIRTHDATE.beforeOrEquals().day("2012-01-22"))
- .and(Patient.BIRTHDATE.after().day("2011-01-01"))
- .include(Patient.INCLUDE_MANAGINGORGANIZATION)
- .sort().ascending(Patient.BIRTHDATE)
- .sort().descending(Patient.NAME)
- .limitTo(123)
- .execute();
- //@formatter:on
-
- assertEquals("http://example.com/fhir/Patient?birthdate=%3C%3D2012-01-22&birthdate=%3E2011-01-01&_include=Patient.managingOrganization&_sort%3Aasc=birthdate&_sort%3Adesc=name&_count=123&_format=json", capt.getValue().getURI().toString());
-
- }
-
-
-
- @Test
- public void testRead() throws Exception {
-
- String msg = getResourceResult();
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- 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")));
- Header[] headers = new Header[2];
- headers[0] = new BasicHeader(Constants.HEADER_LAST_MODIFIED, "Wed, 15 Nov 1995 04:58:08 GMT");
- headers[1] = new BasicHeader(Constants.HEADER_CONTENT_LOCATION, "http://foo.com/Patient/123/_history/2333");
- when(myHttpResponse.getAllHeaders()).thenReturn(headers);
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- //@formatter:off
- Patient response = client.read(Patient.class, new IdDt("Patient/1234"));
- //@formatter:on
-
- assertThat(response.getNameFirstRep().getFamilyAsSingleString(), StringContains.containsString("Cardinal"));
-
- assertEquals("http://foo.com/Patient/123/_history/2333", response.getId().getValue());
-
- InstantDt lm = (InstantDt) response.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED);
- lm.setTimeZoneZulu(true);
- assertEquals("1995-11-15T04:58:08.000Z", lm.getValueAsString());
-
- }
-
-
- @SuppressWarnings("unused")
- @Test
- public void testSearchWithInternalServerError() throws Exception {
-
- String msg = getPatientFeedWithOneResult();
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 500, "INTERNAL ERRORS"));
- when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_TEXT + "; charset=UTF-8"));
- when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader("Server Issues!"), Charset.forName("UTF-8")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- try {
- client.search().forResource(Patient.class).execute();
- fail();
- } catch (InternalErrorException e) {
- assertEquals(e.getMessage(), "HTTP 500 INTERNAL ERRORS: Server Issues!");
- assertEquals(e.getResponseBody(), "Server Issues!");
- }
-
- }
-
- @SuppressWarnings("unused")
- @Test
- public void testSearchWithNonFhirResponse() throws Exception {
-
- String msg = getPatientFeedWithOneResult();
-
- ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class);
- when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
- when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"));
- when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_TEXT + "; charset=UTF-8"));
- when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader("Server Issues!"), Charset.forName("UTF-8")));
-
- IGenericClient client = myCtx.newRestfulGenericClient("http://example.com/fhir");
-
- try {
- client.search().forResource(Patient.class).execute();
- fail();
- } catch (NonFhirResponseException e) {
- assertThat(e.getMessage(), StringContains.containsString("Server Issues!"));
- }
+ @BeforeClass
+ public static void beforeClass() {
+ myCtx = new FhirContext();
}
}
diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/SearchTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/SearchTest.java
index 5b1841ecc7c..2cc688c9519 100644
--- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/SearchTest.java
+++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/SearchTest.java
@@ -1,6 +1,6 @@
package ca.uhn.fhir.rest.server;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
@@ -8,10 +8,14 @@ import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.http.message.BasicNameValuePair;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
@@ -22,11 +26,16 @@ import org.junit.Test;
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.dstu.composite.CodingDt;
+import ca.uhn.fhir.model.dstu.resource.Observation;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.rest.annotation.OptionalParam;
+import ca.uhn.fhir.rest.annotation.RequiredParam;
import ca.uhn.fhir.rest.annotation.Search;
+import ca.uhn.fhir.rest.param.ReferenceParam;
import ca.uhn.fhir.rest.param.StringParam;
+import ca.uhn.fhir.rest.param.TokenOrListParam;
import ca.uhn.fhir.testutil.RandomServerPortProvider;
/**
@@ -35,24 +44,9 @@ import ca.uhn.fhir.testutil.RandomServerPortProvider;
public class SearchTest {
private static CloseableHttpClient ourClient;
+ private static FhirContext ourCtx = new FhirContext();
private static int ourPort;
private static Server ourServer;
- private static FhirContext ourCtx = new FhirContext();
-
- @Test
- public void testSearchById() throws Exception {
- HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_id=aaa");
- HttpResponse status = ourClient.execute(httpGet);
- String responseContent = IOUtils.toString(status.getEntity().getContent());
- IOUtils.closeQuietly(status.getEntity().getContent());
- assertEquals(200, status.getStatusLine().getStatusCode());
- Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
- assertEquals(1, bundle.getEntries().size());
-
- Patient p = bundle.getResources(Patient.class).get(0);
- assertEquals("idaaa", p.getNameFirstRep().getFamilyAsSingleString());
- assertEquals("IDAAA (identifier123)", bundle.getEntries().get(0).getTitle().getValue());
- }
@Test
public void testOmitEmptyOptionalParam() throws Exception {
@@ -63,13 +57,69 @@ public class SearchTest {
assertEquals(200, status.getStatusLine().getStatusCode());
Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
assertEquals(1, bundle.getEntries().size());
-
+
Patient p = bundle.getResources(Patient.class).get(0);
assertEquals(null, p.getNameFirstRep().getFamilyFirstRep().getValue());
}
+ @Test
+ public void testSearchById() throws Exception {
+ HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_id=aaa");
+ HttpResponse status = ourClient.execute(httpGet);
+ String responseContent = IOUtils.toString(status.getEntity().getContent());
+ IOUtils.closeQuietly(status.getEntity().getContent());
+ assertEquals(200, status.getStatusLine().getStatusCode());
+ Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
+ assertEquals(1, bundle.getEntries().size());
+
+ Patient p = bundle.getResources(Patient.class).get(0);
+ assertEquals("idaaa", p.getNameFirstRep().getFamilyAsSingleString());
+ assertEquals("IDAAA (identifier123)", bundle.getEntries().get(0).getTitle().getValue());
+ }
+ @Test
+ public void testSearchByPost() throws Exception {
+ HttpPost filePost = new HttpPost("http://localhost:" + ourPort + "/Patient/_search");
+
+ // add parameters to the post method
+ List parameters = new ArrayList ();
+ parameters.add(new BasicNameValuePair("_id", "aaa"));
+
+ UrlEncodedFormEntity sendentity = new UrlEncodedFormEntity(parameters, "UTF-8");
+ filePost.setEntity(sendentity);
+
+ HttpResponse status = ourClient.execute(filePost);
+ String responseContent = IOUtils.toString(status.getEntity().getContent());
+ IOUtils.closeQuietly(status.getEntity().getContent());
+ assertEquals(200, status.getStatusLine().getStatusCode());
+ Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
+ assertEquals(1, bundle.getEntries().size());
+
+ Patient p = bundle.getResources(Patient.class).get(0);
+ assertEquals("idaaa", p.getNameFirstRep().getFamilyAsSingleString());
+ assertEquals("IDAAA (identifier123)", bundle.getEntries().get(0).getTitle().getValue());
+ }
+
+ @Test
+ public void testSearchGetWithUnderscoreSearch() throws Exception {
+ HttpGet httpGet = new HttpGet("http://localhost:" + ourPort+"/Observation/_search?subject%3APatient=100&name=3141-9%2C8302-2%2C8287-5%2C39156-5");
+
+ HttpResponse status = ourClient.execute(httpGet);
+ String responseContent = IOUtils.toString(status.getEntity().getContent());
+ IOUtils.closeQuietly(status.getEntity().getContent());
+ assertEquals(200, status.getStatusLine().getStatusCode());
+ Bundle bundle = ourCtx.newXmlParser().parseBundle(responseContent);
+ assertEquals(1, bundle.getEntries().size());
+
+ Observation p = bundle.getResources(Observation.class).get(0);
+ assertEquals("Patient/100", p.getSubject().getReference().toString());
+ assertEquals(4, p.getName().getCoding().size());
+ assertEquals("3141-9", p.getName().getCoding().get(0).getCode().getValue());
+ assertEquals("8302-2", p.getName().getCoding().get(1).getCode().getValue());
+
+ }
+
@AfterClass
public static void afterClass() throws Exception {
ourServer.stop();
@@ -85,8 +135,8 @@ public class SearchTest {
ServletHandler proxyHandler = new ServletHandler();
RestfulServer servlet = new RestfulServer();
servlet.getFhirContext().setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator());
-
- servlet.setResourceProviders(patientProvider);
+
+ servlet.setResourceProviders(patientProvider, new DummyObservationResourceProvider());
ServletHolder servletHolder = new ServletHolder(servlet);
proxyHandler.addServletWithMapping(servletHolder, "/*");
ourServer.setHandler(proxyHandler);
@@ -99,6 +149,28 @@ public class SearchTest {
}
+ public static class DummyObservationResourceProvider implements IResourceProvider{
+
+ @Override
+ public Class extends IResource> getResourceType() {
+ return Observation.class;
+ }
+
+ @Search
+ public Observation search(@RequiredParam(name="subject") ReferenceParam theSubject, @RequiredParam(name="name") TokenOrListParam theName) {
+ Observation o = new Observation();
+ o.setId("1");
+
+ o.getSubject().setReference(theSubject.getResourceType() + "/" + theSubject.getIdPart());
+ for (CodingDt next : theName.getListAsCodings()) {
+ o.getName().getCoding().add(next);
+ }
+
+ return o;
+ }
+
+ }
+
/**
* Created by dsotnikov on 2/25/2014.
*/
@@ -111,8 +183,8 @@ public class SearchTest {
Patient patient = new Patient();
patient.setId("1");
patient.addIdentifier("system", "identifier123");
- if (theParam!=null) {
- patient.addName().addFamily("id"+theParam.getValue());
+ if (theParam != null) {
+ patient.addName().addFamily("id" + theParam.getValue());
}
retVal.add(patient);
return retVal;
diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/ServerExtraParametersTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/ServerExtraParametersTest.java
index b39a2923bb7..0997900c4d0 100644
--- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/ServerExtraParametersTest.java
+++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/ServerExtraParametersTest.java
@@ -1,13 +1,17 @@
package ca.uhn.fhir.rest.server;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+import java.util.Arrays;
import java.util.Collections;
import java.util.List;
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.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -19,7 +23,12 @@ import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.rest.annotation.RequiredParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.annotation.ServerBase;
+import ca.uhn.fhir.rest.client.IGenericClient;
import ca.uhn.fhir.rest.client.api.IBasicClient;
+import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
+import ca.uhn.fhir.rest.gclient.StringClientParam;
+import ca.uhn.fhir.rest.param.StringParam;
+import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.testutil.RandomServerPortProvider;
public class ServerExtraParametersTest {
@@ -58,6 +67,28 @@ public class ServerExtraParametersTest {
assertEquals("http://localhost:" + myPort, patientProvider.getServerBase());
}
+ @Test
+ public void testNonRepeatableParam() throws Exception {
+ MyServerBaseProvider patientProvider = new MyServerBaseProvider();
+ myServlet.setResourceProviders(patientProvider);
+
+ myServer.start();
+
+ FhirContext ctx = new FhirContext();
+ IGenericClient client = ctx.newRestfulGenericClient("http://localhost:" + myPort + "/");
+ client.registerInterceptor(new LoggingInterceptor(true));
+
+ try {
+ client.search().forResource("Patient").where(new StringClientParam("singleParam").matches().values(Arrays.asList("AA", "BB"))).execute();
+ fail();
+ } catch (InvalidRequestException e) {
+ assertThat(
+ e.getMessage(),
+ StringContains
+ .containsString("HTTP 400 Bad Request: Multiple values detected for non-repeatable parameter 'singleParam'. This server is not configured to allow multiple (AND/OR) values for this param."));
+ }
+ }
+
@After
public void after() throws Exception {
myServer.stop();
@@ -75,6 +106,13 @@ public class ServerExtraParametersTest {
return Patient.class;
}
+ @Search
+ public List searchSingleParam(@RequiredParam(name = "singleParam") StringParam theFooParam) {
+ Patient retVal = new Patient();
+ retVal.setId("1");
+ return Collections.singletonList(retVal);
+ }
+
@Search
public List searchForPatients(@RequiredParam(name = "fooParam") StringDt theFooParam, @ServerBase String theServerBase) {
myServerBase = theServerBase;
diff --git a/restful-server-example/.settings/org.eclipse.wst.common.component b/restful-server-example/.settings/org.eclipse.wst.common.component
index fc0c66be7d5..263e87f0f0d 100644
--- a/restful-server-example/.settings/org.eclipse.wst.common.component
+++ b/restful-server-example/.settings/org.eclipse.wst.common.component
@@ -3,7 +3,7 @@
-
+
uses