@Test public void testGetAllTagsPatient() throws Exception { TagList tagList = new TagList(); tagList.add(new Tag("CCC", "AAA", "BBB")); String ser = ctx.newXmlParser().encodeTagListToString(tagList); ArgumentCaptor<HttpUriRequest> 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_FHIR_XML + "; charset=UTF-8")); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(ser), Charset.forName("UTF-8"))); IClient client = ctx.newRestfulClient(IClient.class, "http://foo"); TagList response = client.getAllTagsPatient(); assertEquals(tagList, response); assertEquals(HttpGet.class, capt.getValue().getClass()); HttpGet get = (HttpGet) capt.getValue(); assertEquals("http://foo/Patient/_tags", get.getURI().toString()); }
private void assertInputConversion_viaCharsetName(String charsetName, boolean conversionErrorsExpected) throws Exception { byte[] originalBytes = createBytes(); { // byte array as byte stream InputStream originalByteStream = new ByteArrayInputStream(originalBytes); // byte stream as character stream Reader originalReader = new InputStreamReader(originalByteStream, charsetName); // modifying reader (we don't modify here) Reader modifyingReader = new ModifyingReader(originalReader, new RegexModifier("a", 0, "a")); // character stream as byte stream InputStream modifyingByteStream = new ReaderInputStream(modifyingReader, charsetName); // byte stream as byte array byte[] modifiedBytes = IOUtils.toByteArray(modifyingByteStream); assertBytes(originalBytes, modifiedBytes, conversionErrorsExpected); } }
/** * @param flush * @return Returns true if the actual output is equals to the expected * output. * @throws Exception */ private boolean rewriteContent(boolean flush, String domainPrefix) throws Exception { String contentPart = StringUtils.repeat("text", 2); String oldUrl = domainPrefix + "something"; String expectedNewUrl = domainPrefix + "anything"; String oldHtml = "<html><body>" + contentPart + oldUrl + contentPart + "</body></html>"; String expectedNewHtml = "<html><body>" + contentPart + expectedNewUrl + contentPart + "</body></html>"; String encoding = "UTF-8"; ByteArrayOutputStream os = new ByteArrayOutputStream(); long written = rewriteContent(new ReaderInputStream(new StringReader(oldHtml)), os, encoding, flush); System.out.println("written: " + written); System.out.println("oldHtml.length(): " + oldHtml.length()); System.out.println("expectedNewHtml.length(): " + expectedNewHtml.length()); System.out.println("expectedNewHtml: \n" + expectedNewHtml); os.flush(); String newHtml = new String(os.toByteArray(), encoding); System.out.println(newHtml); return expectedNewHtml.equals(newHtml); }
private static InputStream getInputStreamFromJar(final InputStream jarInputStream, final String prefix, final String suffix) throws IOException { ZipInputStream zis = new ZipInputStream(jarInputStream); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { final String entryName = entry.getName(); if (entryName.startsWith(prefix) && entryName.endsWith(suffix)) { return new ReaderInputStream( new InputStreamReader( zis, "UTF-8")); } } throw new FileNotFoundException("Could not find '" + prefix + "/*/" + suffix + "' in the jar."); }
@Test public void testAddTagsPatientVersion() throws Exception { ArgumentCaptor<HttpUriRequest> 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().getElements()).thenReturn(new HeaderElement[0]); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(""), Charset.forName("UTF-8"))); TagList tagList = new TagList(); tagList.add(new Tag("CCC", "AAA", "BBB")); IClient client = ctx.newRestfulClient(IClient.class, "http://foo"); client.addTags(new IdDt("111"), new IdDt("222"),tagList); assertEquals(HttpPost.class, capt.getValue().getClass()); HttpPost post = (HttpPost) capt.getValue(); assertEquals("http://foo/Patient/111/_history/222/_tags", post.getURI().toString()); String ser = IOUtils.toString(post.getEntity().getContent()); TagList actualTagList = ctx.newXmlParser().parseTagList(ser); assertEquals(tagList, actualTagList); }
@Test public void testTransaction() throws Exception { String bundleStr = IOUtils.toString(getClass().getResourceAsStream("/bundle.json")); Bundle bundle = ourCtx.newJsonParser().parseBundle(bundleStr); ArgumentCaptor<HttpUriRequest> 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 = ourCtx.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()); assertEquals(EncodingEnum.XML.getBundleContentType() + Constants.HEADER_SUFFIX_CT_UTF_8, capt.getAllValues().get(0).getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue()); }
@Test public void testDeleteTagsPatientVersion() throws Exception { ArgumentCaptor<HttpUriRequest> 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().getElements()).thenReturn(new HeaderElement[0]); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(""), Charset.forName("UTF-8"))); TagList tagList = new TagList(); tagList.add(new Tag("CCC", "AAA", "BBB")); IClient client = ctx.newRestfulClient(IClient.class, "http://foo"); client.deleteTags(new IdDt("111"), new IdDt("222"),tagList); assertEquals(HttpPost.class, capt.getValue().getClass()); HttpPost post = (HttpPost) capt.getValue(); assertEquals("http://foo/Patient/111/_history/222/_tags/_delete", post.getURI().toString()); String ser = IOUtils.toString(post.getEntity().getContent()); TagList actualTagList = ctx.newXmlParser().parseTagList(ser); assertEquals(tagList, actualTagList); }
@Test public void testCreateBad() throws Exception { Patient patient = new Patient(); patient.addIdentifier("urn:foo", "123"); ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class); when(httpClient.execute(capt.capture())).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 400, "foobar")); when(httpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_TEXT + "; charset=UTF-8")); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader("foobar"), Charset.forName("UTF-8"))); try { ctx.newRestfulClient(ITestClient.class, "http://foo").createPatient(patient); fail(); } catch (InvalidRequestException e) { assertThat(e.getMessage(), StringContains.containsString("foobar")); } }
@Test public void testSearchByString() throws Exception { String msg = "{\"resourceType\":\"Bundle\",\"id\":null,\"base\":\"http://localhost:57931/fhir/contextDev\",\"total\":1,\"link\":[{\"relation\":\"self\",\"url\":\"http://localhost:57931/fhir/contextDev/Patient?identifier=urn%3AMultiFhirVersionTest%7CtestSubmitPatient01&_format=json\"}],\"entry\":[{\"resource\":{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}}]}"; ArgumentCaptor<HttpUriRequest> 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(msg), Charset.forName("UTF-8"))); IGenericClient client = ourCtx.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()); assertEquals(Patient.class, response.getEntries().get(0).getResource().getClass()); }
@SuppressWarnings("unused") @Test public void testSearchWithClientEncodingAndPrettyPrintConfig() throws Exception { String msg = getPatientFeedWithOneResult(); ArgumentCaptor<HttpUriRequest> 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"))); GenericClient client = (GenericClient) ourCtx.newRestfulGenericClient("http://example.com/fhir"); client.setPrettyPrint(true); client.setEncoding(EncodingEnum.JSON); //@formatter:off Bundle response = client.search() .forResource(Patient.class) .execute(); //@formatter:on assertEquals("http://example.com/fhir/Patient?_format=json&_pretty=true", capt.getValue().getURI().toString()); }
@Test public void testDelete() throws Exception { OperationOutcome oo = new OperationOutcome(); oo.addIssue().setDetails("Hello"); String resp = new FhirContext().newXmlParser().encodeResourceToString(oo); ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class); when(httpClient.execute(capt.capture())).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 201, "OK")); when(httpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_XML + "; charset=UTF-8")); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(resp), Charset.forName("UTF-8"))); ITestClient client = ctx.newRestfulClient(ITestClient.class, "http://foo"); MethodOutcome response = client.deletePatient(new IdDt("1234")); assertEquals(HttpDelete.class, capt.getValue().getClass()); assertEquals("http://foo/Patient/1234", capt.getValue().getURI().toString()); assertEquals("Hello", response.getOperationOutcome().getIssueFirstRep().getDetailsElement().getValue()); }
@Test public void testSearchWithAbsoluteUrlAndType() throws Exception { String msg = getPatientFeedWithOneResult(); ArgumentCaptor<HttpUriRequest> 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 = ourCtx.newRestfulGenericClient("http://example.com/fhir"); Bundle response = client .search(Patient.class, new UriDt( "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")); 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()); assertEquals(1, response.size()); }
@Test public void testReadFailureInternalError() throws Exception { ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class); when(httpClient.execute(capt.capture())).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 500, "INTERNAL")); Header[] headers = new Header[1]; headers[0] = new BasicHeader(Constants.HEADER_LAST_MODIFIED, "2011-01-02T22:01:02"); when(httpResponse.getAllHeaders()).thenReturn(headers); when(httpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_TEXT)); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader("Internal Failure"), Charset.forName("UTF-8"))); ITestClient client = ctx.newRestfulClient(ITestClient.class, "http://foo"); try { client.getPatientById(new IdDt("111")); fail(); } catch (InternalErrorException e) { assertThat(e.getMessage(), containsString("INTERNAL")); assertThat(e.getResponseBody(), containsString("Internal Failure")); } }
@Test public void testReadFailureNoCharset() throws Exception { //@formatter:off String msg = "<OperationOutcome xmlns=\"http://hl7.org/fhir\"></OperationOutcome>"; //@formatter:on ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class); when(httpClient.execute(capt.capture())).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 404, "NOT FOUND")); Header[] headers = new Header[1]; headers[0] = new BasicHeader(Constants.HEADER_LAST_MODIFIED, "2011-01-02T22:01:02"); when(httpResponse.getAllHeaders()).thenReturn(headers); when(httpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_XML)); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"))); ITestClient client = ctx.newRestfulClient(ITestClient.class, "http://foo"); try { client.getPatientById(new IdDt("111")); fail(); } catch (ResourceNotFoundException e) { // good } }
@Test public void testSearchByDateRange() throws Exception { String msg = getPatientFeedWithOneResult(); ArgumentCaptor<HttpUriRequest> 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_FHIR_XML + "; charset=UTF-8")); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"))); ITestClient client = ctx.newRestfulClient(ITestClient.class, "http://foo"); DateRangeParam param = new DateRangeParam(); param.setLowerBound(new DateParam(QuantityCompararatorEnum.GREATERTHAN_OR_EQUALS, "2011-01-01")); param.setUpperBound(new DateParam(QuantityCompararatorEnum.LESSTHAN_OR_EQUALS, "2021-01-01")); client.getPatientByDateRange(param); assertEquals("http://foo/Patient?dateRange=%3E%3D2011-01-01&dateRange=%3C%3D2021-01-01", capt.getValue().getURI().toString()); }
@Test public void testSearchByToken() throws Exception { String msg = getPatientFeedWithOneResult(); ArgumentCaptor<HttpUriRequest> 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_FHIR_XML + "; charset=UTF-8")); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"))); ITestClient client = ctx.newRestfulClient(ITestClient.class, "http://foo"); Patient response = client.findPatientByMrn(new TokenParam("urn:foo", "123")); assertEquals("http://foo/Patient?identifier=urn%3Afoo%7C123", capt.getValue().getURI().toString()); assertEquals("PRP1660", response.getIdentifier().get(0).getValue().getValue()); }
@Test public void testSearchOrList() throws Exception { String msg = getPatientFeedWithOneResult(); ArgumentCaptor<HttpUriRequest> 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_FHIR_XML + "; charset=UTF-8")); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"))); ITestClient client = ctx.newRestfulClient(ITestClient.class, "http://foo"); TokenOrListParam identifiers = new TokenOrListParam(); identifiers.add(new CodingDt("foo", "bar")); identifiers.add(new CodingDt("baz", "boz")); client.getPatientMultipleIdentifiers(identifiers); assertEquals("http://foo/Patient?ids=foo%7Cbar%2Cbaz%7Cboz", capt.getValue().getURI().toString()); }
@Test public void testSearchNamedQueryOneParam() throws Exception { String msg = getPatientFeedWithOneResult(); ArgumentCaptor<HttpUriRequest> 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_FHIR_XML + "; charset=UTF-8")); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"))); ITestClient client = ctx.newRestfulClient(ITestClient.class, "http://foo"); client.getPatientOneParam(new StringParam("BB")); assertEquals("http://foo/Patient?_query=someQueryOneParam¶m1=BB", capt.getValue().getURI().toString()); }
@Test public void testWithParam() throws Exception { ArgumentCaptor<HttpUriRequest> 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(createBundle()), Charset.forName("UTF-8"))); IGenericClient client = ctx.newRestfulGenericClient( "http://foo"); Bundle bundle = client.search().forResource("Patient").execute(); assertEquals(HttpGet.class, capt.getValue().getClass()); HttpGet get = (HttpGet) capt.getValue(); assertEquals("http://foo/Patient", get.getURI().toString()); assertEquals(3, bundle.size()); Patient p = (Patient) bundle.getEntries().get(0).getResource(); List<ExtensionDt> exts = p.getUndeclaredExtensionsByUrl("http://foo"); assertEquals(1,exts.size()); ExtensionDt ext = exts.get(0); ResourceReferenceDt ref = (ResourceReferenceDt) ext.getValue(); assertEquals("Organization/o1", ref.getReference().getValue()); assertNotNull(ref.getResource()); }
@Test public void testSearchWithCustomTypeList() throws Exception { String msg = getPatientFeedWithOneResult(); ArgumentCaptor<HttpUriRequest> 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_FHIR_XML + "; charset=UTF-8")); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"))); ITestClientWithCustomTypeList client = ctx.newRestfulClient(ITestClientWithCustomTypeList.class, "http://foo"); List<CustomPatient> response = client.getPatientByDob(new DateParam(QuantityCompararatorEnum.GREATERTHAN_OR_EQUALS, "2011-01-02")); assertEquals("http://foo/Patient?birthdate=%3E%3D2011-01-02", capt.getValue().getURI().toString()); assertEquals("PRP1660", response.get(0).getIdentifier().get(0).getValue().getValue()); }
@Test public void testSearchWithIncludes() throws Exception { String msg = getPatientFeedWithOneResult(); ArgumentCaptor<HttpUriRequest> 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_FHIR_XML + "; charset=UTF-8")); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"))); ITestClient client = ctx.newRestfulClient(ITestClient.class, "http://foo"); client.getPatientWithIncludes(new StringParam("aaa"), Arrays.asList(new Include[] { new Include("inc1"), new Include("inc2") })); assertEquals("http://foo/Patient?withIncludes=aaa&_include=inc1&_include=inc2", capt.getValue().getURI().toString()); }
@Test public void testSearchByCompositeParam() throws Exception { String msg = getPatientFeedWithOneResult(); ArgumentCaptor<HttpUriRequest> 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_FHIR_XML + "; charset=UTF-8")); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"))); ITestClient client = ctx.newRestfulClient(ITestClient.class, "http://foo"); StringParam str = new StringParam("FOO$BAR"); DateParam date = new DateParam("2001-01-01"); client.getObservationByNameValueDate(new CompositeParam<StringParam, DateParam>(str, date)); 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 testGetAllTags() throws Exception { TagList tagList = new TagList(); tagList.add(new Tag("CCC", "AAA", "BBB")); String ser = ctx.newXmlParser().encodeTagListToString(tagList); ArgumentCaptor<HttpUriRequest> 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_FHIR_XML + "; charset=UTF-8")); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(ser), Charset.forName("UTF-8"))); IClient client = ctx.newRestfulClient(IClient.class, "http://foo"); TagList response = client.getAllTags(); assertEquals(tagList, response); assertEquals(HttpGet.class, capt.getValue().getClass()); HttpGet get = (HttpGet) capt.getValue(); assertEquals("http://foo/_tags", get.getURI().toString()); }
@Test public void testUpdate() throws Exception { Patient patient = new Patient(); patient.addIdentifier("urn:foo", "123"); ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class); when(httpClient.execute(capt.capture())).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 201, "OK")); when(httpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_TEXT + "; charset=UTF-8")); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(""), Charset.forName("UTF-8"))); when(httpResponse.getAllHeaders()).thenReturn(toHeaderArray("Location", "http://example.com/fhir/Patient/100/_history/200")); ITestClient client = ctx.newRestfulClient(ITestClient.class, "http://foo"); MethodOutcome response = client.updatePatient(new IdDt("100"), patient); assertEquals(HttpPut.class, capt.getValue().getClass()); HttpPut post = (HttpPut) capt.getValue(); assertThat(post.getURI().toASCIIString(), StringEndsWith.endsWith("/Patient/100")); assertThat(IOUtils.toString(post.getEntity().getContent()), StringContains.containsString("<Patient")); assertEquals("http://example.com/fhir/Patient/100/_history/200", response.getId().getValue()); assertEquals("200", response.getId().getVersionIdPart()); assertEquals(EncodingEnum.XML.getResourceContentType() + Constants.HEADER_SUFFIX_CT_UTF_8, capt.getAllValues().get(0).getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue()); }
/** * Return a FHIR content type, but no content and make sure we handle this without crashing */ @Test public void testUpdateWithEmptyResponse() throws Exception { Patient patient = new Patient(); patient.addIdentifier("urn:foo", "123"); ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class); when(httpClient.execute(capt.capture())).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 201, "OK")); when(httpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_XML + "; charset=UTF-8")); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(""), Charset.forName("UTF-8"))); when(httpResponse.getAllHeaders()).thenReturn(toHeaderArray("Content-Location", "http://example.com/fhir/Patient/100/_history/200")); ITestClient client = ctx.newRestfulClient(ITestClient.class, "http://foo"); client.updatePatient(new IdDt("Patient/100/_history/200"), patient); assertEquals(HttpPut.class, capt.getValue().getClass()); HttpPut post = (HttpPut) capt.getValue(); assertEquals("http://foo/Patient/100", post.getURI().toASCIIString()); Header h = post.getFirstHeader("content-location"); assertEquals("Patient/100/_history/200", h.getValue()); }
@Test public void testFail500WithPlainMessage() throws Exception { String msg = "Help I'm a bug"; String contentType = Constants.CT_TEXT; ArgumentCaptor<HttpUriRequest> 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 Error")); when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", contentType + "; charset=UTF-8")); when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"))); IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); try { client.read(Patient.class, new IdDt("Patient/1234")); fail(); } catch (InternalErrorException e) { assertThat(e.getMessage(), StringContains.containsString("HTTP 500 Internal Error")); assertThat(e.getMessage(), StringContains.containsString("Help I'm a bug")); } }
@Test public void testFail500WithOperationOutcomeMessage() throws Exception { OperationOutcome oo = new OperationOutcome(); oo.getIssueFirstRep().getDetails().setValue("Help I'm a bug"); String msg = ourCtx.newXmlParser().encodeResourceToString(oo); String contentType = Constants.CT_FHIR_XML; ArgumentCaptor<HttpUriRequest> 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 Error")); when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", contentType + "; charset=UTF-8")); when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"))); IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); try { client.read(Patient.class, new IdDt("Patient/1234")); fail(); } catch (InternalErrorException e) { assertThat(e.getMessage(), StringContains.containsString("HTTP 500 Internal Error")); assertThat(e.getMessage(), StringContains.containsString("Help I'm a bug")); } }
@Test public void testFail500WithUnexpectedResource() throws Exception { Patient patient = new Patient(); patient.addIdentifier().setSystem("foo").setValue("bar"); String msg = ourCtx.newXmlParser().encodeResourceToString(patient); String contentType = Constants.CT_FHIR_XML; ArgumentCaptor<HttpUriRequest> 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 Error")); when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", contentType + "; charset=UTF-8")); when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"))); IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); try { client.read(Patient.class, new IdDt("Patient/1234")); fail(); } catch (InternalErrorException e) { assertEquals("HTTP 500 Internal Error", e.getMessage()); assertThat(e.getResponseBody(), StringContains.containsString("value=\"foo\"")); } }
@Test public void testFail500WithOperationOutcomeMessageJson() throws Exception { OperationOutcome oo = new OperationOutcome(); oo.getIssueFirstRep().getDetails().setValue("Help I'm a bug"); String msg = ourCtx.newJsonParser().encodeResourceToString(oo); String contentType = Constants.CT_FHIR_JSON; ArgumentCaptor<HttpUriRequest> 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 Error")); when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", contentType + "; charset=UTF-8")); when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"))); IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); try { client.read(Patient.class, new IdDt("Patient/1234")); fail(); } catch (InternalErrorException e) { assertThat(e.getMessage(), StringContains.containsString("HTTP 500 Internal Error")); assertThat(e.getMessage(), StringContains.containsString("Help I'm a bug")); assertNotNull(e.getOperationOutcome()); assertEquals("Help I'm a bug",e.getOperationOutcome().getIssueFirstRep().getDetailsElement().getValue()); } }
@Test public void testFail500WithOperationOutcomeMessageGeneric() throws Exception { OperationOutcome oo = new OperationOutcome(); oo.getIssueFirstRep().getDetails().setValue("Help I'm a bug"); String msg = ourCtx.newJsonParser().encodeResourceToString(oo); String contentType = Constants.CT_FHIR_JSON; ArgumentCaptor<HttpUriRequest> 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 Error")); when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", contentType + "; charset=UTF-8")); when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"))); IMyClient client = ourCtx.newRestfulClient(IMyClient.class,"http://example.com/fhir"); try { client.read(new IdDt("Patient/1234")); fail(); } catch (InternalErrorException e) { assertThat(e.getMessage(), StringContains.containsString("HTTP 500 Internal Error")); assertThat(e.getMessage(), StringContains.containsString("Help I'm a bug")); assertNotNull(e.getOperationOutcome()); assertEquals("Help I'm a bug",e.getOperationOutcome().getIssueFirstRep().getDetailsElement().getValue()); } }
@Test public void testCreatePopulatesIsCreated() throws Exception { Patient p1 = new Patient(); p1.addIdentifier("foo:bar", "12345"); p1.addName().addFamily("Smith").addGiven("John"); ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class); when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse); 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 = ourCtx.newRestfulGenericClient("http://example.com/fhir"); when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 201, "OK")); MethodOutcome resp = client.create().resource(ourCtx.newXmlParser().encodeResourceToString(p1)).execute(); assertTrue(resp.getCreated()); when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK")); resp = client.create().resource(ourCtx.newXmlParser().encodeResourceToString(p1)).execute(); assertNull(resp.getCreated()); }
/** * Test for issue #60 */ @Test public void testCreateWithUtf8Characters() throws Exception { String name = "測試醫院"; Organization org = new Organization(); org.setName(name); org.addIdentifier("urn:system", "testCreateWithUtf8Characters_01"); ArgumentCaptor<HttpUriRequest> 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 = ourCtx.newRestfulGenericClient("http://example.com/fhir"); int count = 0; client.create().resource(org).prettyPrint().encodedXml().execute(); assertEquals(1, capt.getAllValues().get(count).getHeaders(Constants.HEADER_CONTENT_TYPE).length); assertEquals(EncodingEnum.XML.getResourceContentType() + Constants.HEADER_SUFFIX_CT_UTF_8, capt.getAllValues().get(count).getFirstHeader(Constants.HEADER_CONTENT_TYPE).getValue()); assertThat(extractBody(capt, count), containsString("<name value=\"測試醫院\"/>")); count++; }
@Test public void testSetDefaultEncoding() throws Exception { String msg = ourCtx.newJsonParser().encodeResourceToString(new Patient()); ArgumentCaptor<HttpUriRequest> 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(msg), Charset.forName("UTF-8"))); // Header[] headers = new Header[] { new BasicHeader(Constants.HEADER_LAST_MODIFIED, "Wed, 15 Nov 1995 04:58:08 GMT"), // new BasicHeader(Constants.HEADER_CONTENT_LOCATION, "http://foo.com/Patient/123/_history/2333"), // new BasicHeader(Constants.HEADER_CATEGORY, "http://foo/tagdefinition.html; scheme=\"http://hl7.org/fhir/tag\"; label=\"Some tag\"") }; // when(myHttpResponse.getAllHeaders()).thenReturn(headers); IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); (client).setEncoding(EncodingEnum.JSON); int count = 0; client.read(Patient.class, new IdDt("Patient/1234")); assertEquals("http://example.com/fhir/Patient/1234?_format=json", capt.getAllValues().get(count).getURI().toString()); count++; }
@SuppressWarnings("unused") @Test public void testSearchAllResources() throws Exception { String msg = getPatientFeedWithOneResult(); ArgumentCaptor<HttpUriRequest> 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 = ourCtx.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()); }
@Test public void testGetAllTagsPatientIdVersion() throws Exception { TagList tagList = new TagList(); tagList.add(new Tag("CCC", "AAA", "BBB")); String ser = ctx.newXmlParser().encodeTagListToString(tagList); ArgumentCaptor<HttpUriRequest> 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_FHIR_XML + "; charset=UTF-8")); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(ser), Charset.forName("UTF-8"))); IClient client = ctx.newRestfulClient(IClient.class, "http://foo"); TagList response = client.getAllTagsPatientId(new IdDt("Patient", "111", "222")); assertEquals(tagList, response); assertEquals(HttpGet.class, capt.getValue().getClass()); HttpGet get = (HttpGet) capt.getValue(); assertEquals("http://foo/Patient/111/_history/222/_tags", get.getURI().toString()); }
@Test public void testAddTagsPatient() throws Exception { ArgumentCaptor<HttpUriRequest> 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().getElements()).thenReturn(new HeaderElement[0]); when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(""), Charset.forName("UTF-8"))); TagList tagList = new TagList(); tagList.add(new Tag("CCC", "AAA", "BBB")); IClient client = ctx.newRestfulClient(IClient.class, "http://foo"); client.addTags(new IdDt("111"), tagList); assertEquals(HttpPost.class, capt.getValue().getClass()); HttpPost post = (HttpPost) capt.getValue(); assertEquals("http://foo/Patient/111/_tags", post.getURI().toString()); String ser = IOUtils.toString(post.getEntity().getContent()); TagList actualTagList = ctx.newXmlParser().parseTagList(ser); assertEquals(tagList, actualTagList); }
@Test public void testReturnTypedList() throws Exception { String retVal = "<feed xmlns=\"http://www.w3.org/2005/Atom\"><title/><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><author><name>HAPI FHIR Server</name></author><entry><title>Encounter 5994268</title><id>http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Encounter/5994268</id><updated>2014-08-05T12:00:11.000-04:00</updated><published>2014-08-05T11:59:21.000-04:00</published><link rel=\"self\" href=\"http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Encounter/5994268\"/><content type=\"text/xml\"><Encounter xmlns=\"http://hl7.org/fhir\"><extension url=\"http://fhir.connectinggta.ca/Profile/encounter#rehabrecheck\"><valueCoding><system value=\"urn:tri:qcpr:rehab_recheck_codes\"/><code value=\"N\"/></valueCoding></extension><text><status value=\"empty\"/><div xmlns=\"http://www.w3.org/1999/xhtml\">No narrative template available for resource profile: http://fhir.connectinggta.ca/Profile/encounter</div></text><contained><Organization xmlns=\"http://hl7.org/fhir\" id=\"1\"><name value=\"General Internal Medicine\"/></Organization></contained><identifier><use value=\"official\"/><label value=\"UHN Visit Number 11410000159\"/><system value=\"urn:oid:1.3.6.1.4.1.12201.2\"/><value value=\"11410000159\"/></identifier><status value=\"finished\"/><class value=\"I\"/><subject><reference value=\"Patient/5993715\"/></subject><participant><type><coding><code value=\"attending\"/></coding></type><individual><reference value=\"Practitioner/5738815\"/></individual></participant><participant><type><coding><code value=\"referring\"/></coding></type><individual><reference value=\"Practitioner/5738815\"/></individual></participant><period><start value=\"2014-08-05T11:59:00-04:00\"/><end value=\"2014-08-05T11:59:00-04:00\"/></period><reason><coding><display value=\"sick\"/></coding><text value=\"sick\"/></reason><location><location><reference value=\"Location/5994269\"/></location></location><serviceProvider><reference value=\"#1\"/></serviceProvider></Encounter></content></entry><entry><title>Patient 5993715</title><id>http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Patient/5993715</id><published>2014-08-08T14:46:16-04:00</published><link rel=\"self\" href=\"http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Patient/5993715\"/><content type=\"text/xml\"><Patient xmlns=\"http://hl7.org/fhir\"><text><status value=\"generated\"/><div xmlns=\"http://www.w3.org/1999/xhtml\"><div class=\"hapiHeaderText\"> Person <b>CHA </b></div><table class=\"hapiPropertyTable\"><tbody><tr><td>Identifier</td><td>UHN MRN 7018614</td></tr><tr><td>Address</td><td><span>100 Dundas street west </span><br/><span>Toronto </span><span>ON </span><span>Can </span></td></tr><tr><td>Date of birth</td><td><span>01 January 1988</span></td></tr></tbody></table></div></text><identifier><use value=\"official\"/><label value=\"UHN MRN 7018614\"/><system value=\"urn:oid:2.16.840.1.113883.3.239.18.148\"/><value value=\"7018614\"/><assigner><reference value=\"Organization/1.3.6.1.4.1.12201\"/></assigner></identifier><identifier><use value=\"secondary\"/><label value=\"OHIP 2341213425\"/><system value=\"urn:oid:2.16.840.1.113883.4.595\"/><value value=\"2341213425\"/></identifier><name><family value=\"Cha\"/><given value=\"Person\"/></name><telecom><system value=\"phone\"/><value value=\"(416)221-4324\"/><use value=\"home\"/></telecom><telecom><system value=\"phone\"/><use value=\"work\"/></telecom><telecom><system value=\"phone\"/><use value=\"mobile\"/></telecom><telecom><system value=\"email\"/><use value=\"home\"/></telecom><gender><coding><system value=\"http://hl7.org/fhir/v3/AdministrativeGender\"/><code value=\"F\"/></coding></gender><birthDate value=\"1988-01-01T00:00:00\"/><address><use value=\"home\"/><line value=\"100 Dundas street west\"/><city value=\"Toronto\"/><state value=\"ON\"/><zip value=\"L4A 3M9\"/><country value=\"Can\"/></address><managingOrganization><reference value=\"Organization/1.3.6.1.4.1.12201\"/></managingOrganization></Patient></content></entry><entry><title>Practitioner Practitioner/5738815</title><id>http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Practitioner/5738815</id><updated>2014-08-08T13:53:52.000-04:00</updated><published>2009-12-04T13:43:11.000-05:00</published><link rel=\"self\" href=\"http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Practitioner/5738815\"/><content type=\"text/xml\"><Practitioner xmlns=\"http://hl7.org/fhir\"><text><status value=\"empty\"/><div xmlns=\"http://www.w3.org/1999/xhtml\">No narrative template available for resource profile: http://hl7.org/fhir/profiles/Practitioner</div></text><identifier><system value=\"urn:uhn:qcpr:user_ids\"/><value value=\"5186\"/></identifier><name><family value=\"Generic\"/><given value=\"Physician\"/></name></Practitioner></content></entry><entry><title>Location Location/5994269</title><id>http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Location/5994269</id><published>2014-08-08T14:46:16-04:00</published><link rel=\"self\" href=\"http://uhnvesb01d.uhn.on.ca:25180/uhn-fhir-service-1.2/Location/5994269\"/><content type=\"text/xml\"><Location xmlns=\"http://hl7.org/fhir\"><text><status value=\"empty\"/><div xmlns=\"http://www.w3.org/1999/xhtml\">No narrative template available for resource profile: http://hl7.org/fhir/profiles/Location</div></text><name value=\"ES14 414 2\"/><type><coding><code value=\"bed\"/></coding></type></Location></content></entry></feed>"; ArgumentCaptor<HttpUriRequest> 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<Encounter> found = client.search(); assertEquals(1, found.size()); Encounter encounter = found.get(0); assertNotNull(encounter.getSubject().getResource()); }
@SuppressWarnings("unused") @Test public void testSearchByNumberExact() throws Exception { String msg = new FhirContext().newXmlParser().encodeBundleToString(new Bundle()); ArgumentCaptor<HttpUriRequest> 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 = ourCtx.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<HttpUriRequest> 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 = ourCtx.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<HttpUriRequest> 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 = ourCtx.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 }