Switch Android library to use OkHttp by default

This commit is contained in:
James Agnew 2016-10-14 16:15:27 -04:00
parent 7caf9b5ad9
commit e56c75e80c
17 changed files with 328 additions and 243 deletions

View File

@ -55,15 +55,17 @@
<version>2.1-SNAPSHOT</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-client-okhttp</artifactId>
<version>2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-android</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-android</artifactId>
@ -161,6 +163,7 @@
<artifactSet>
<includes combine.children="append">
<include>ca.uhn.hapi.fhir:hapi-fhir-base</include>
<include>ca.uhn.hapi.fhir:hapi-fhir-client-okhttp</include>
<include>org.codehaus.woodstox:woodstox-core-asl</include>
<include>javax.xml.stream:stax-api</include>
<include>org.codehaus.woodstox:stax2-api</include>

View File

@ -1,14 +0,0 @@
package ca.uhn.fhir.android;
import ca.uhn.fhir.context.FhirContext;
public class AndroidLoader {
public static void main(String[] theArgs) {
FhirContext ctx = FhirContext.forDstu2();
ctx.newJsonParser();
ctx.newXmlParser();
ctx.newRestfulGenericClient("");
}
}

View File

@ -0,0 +1,16 @@
package ca.uhn.fhir.android;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.okhttp.client.OkHttpRestfulClientFactory;
/**
* This class exists in order to ensure that
*/
public class AndroidMarker {
public static void configureContext(FhirContext theContext) {
theContext.setRestfulClientFactory(new OkHttpRestfulClientFactory(theContext));
}
}

View File

@ -1,66 +1,43 @@
package ca.uhn.fhir.android.client;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
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.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicStatusLine;
import org.hl7.fhir.dstu3.model.*;
import org.hl7.fhir.dstu3.model.Bundle.BundleType;
import org.junit.*;
import org.hl7.fhir.dstu3.model.Binary;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.OperationOutcome;
import org.hl7.fhir.dstu3.model.Patient;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
import ca.uhn.fhir.model.primitive.DateTimeDt;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.model.primitive.UriDt;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.api.PreferReturnEnum;
import ca.uhn.fhir.rest.client.IGenericClient;
import ca.uhn.fhir.rest.client.ServerValidationModeEnum;
import ca.uhn.fhir.rest.client.exceptions.FhirClientConnectionException;
import ca.uhn.fhir.rest.client.exceptions.NonFhirResponseException;
import ca.uhn.fhir.rest.client.interceptor.UserInfoInterceptor;
import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.rest.server.EncodingEnum;
import ca.uhn.fhir.rest.server.exceptions.NotImplementedOperationException;
import ca.uhn.fhir.rest.server.exceptions.UnclassifiedServerFailureException;
import ca.uhn.fhir.util.TestUtil;
import ca.uhn.fhir.util.UrlUtil;
import ca.uhn.fhir.util.VersionUtil;
import okhttp3.*;
import okio.Buffer;
public class GenericClientDstu3IT {
@ -68,51 +45,66 @@ public class GenericClientDstu3IT {
private static FhirContext ourCtx;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(GenericClientDstu3IT.class);
private int myAnswerCount;
private HttpClient myHttpClient;
private HttpResponse myHttpResponse;
private Call.Factory myHttpClient;
private ArgumentCaptor<Request> capt;
private Response myHttpResponse;
private Request myRequest;
private Protocol myProtocol;
@Before
public void before() {
myHttpClient = mock(HttpClient.class, new ReturnsDeepStubs());
public void before() throws IOException {
myHttpClient = mock(Call.Factory.class, Mockito.RETURNS_DEEP_STUBS);
ourCtx.getRestfulClientFactory().setHttpClient(myHttpClient);
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
myHttpResponse = mock(HttpResponse.class, new ReturnsDeepStubs());
Call httpResponse = mock(Call.class, Mockito.RETURNS_DEEP_STUBS);
capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(httpResponse);
myRequest = new Request.Builder().url("http://127.0.0.1").build();
myProtocol = Protocol.HTTP_1_1;
when(httpResponse.execute()).thenAnswer(new Answer<Response>() {
@Override
public Response answer(InvocationOnMock theInvocation) throws Throwable {
myAnswerCount++;
return myHttpResponse;
}});
myAnswerCount = 0;
}
private String expectedUserAgent() {
return "HAPI-FHIR/" + VersionUtil.getVersion() + " (FHIR Client; FHIR " + FhirVersionEnum.DSTU3.getFhirVersionString() + "/DSTU3; apache)";
return "HAPI-FHIR/" + VersionUtil.getVersion() + " (FHIR Client; FHIR " + FhirVersionEnum.DSTU3.getFhirVersionString() + "/DSTU3; okhttp/3.4.1)";
}
private String extractBodyAsString(ArgumentCaptor<HttpUriRequest> capt) throws IOException {
String body = IOUtils.toString(((HttpEntityEnclosingRequestBase) capt.getAllValues().get(0)).getEntity().getContent(), "UTF-8");
return body;
private String extractBodyAsString(ArgumentCaptor<Request> capt) throws IOException {
Buffer sink = new Buffer();
capt.getValue().body().writeTo(sink);
return new String(sink.readByteArray(), "UTF-8");
}
private void validateUserAgent(ArgumentCaptor<Request> capt) {
assertEquals(expectedUserAgent(), capt.getAllValues().get(0).header("User-Agent"));
}
/**
* TODO: narratives don't work without stax
*/
@Test
@Ignore
public void testBinaryCreateWithFhirContentType() throws Exception {
IParser p = ourCtx.newXmlParser();
OperationOutcome conf = new OperationOutcome();
conf.getText().setDivAsString("OK!");
String respString = p.encodeResourceToString(conf);
final String respString = p.encodeResourceToString(conf);
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()).thenAnswer(new Answer<ReaderInputStream>() {
@Override
public ReaderInputStream answer(InvocationOnMock theInvocation) throws Throwable {
return new ReaderInputStream(new StringReader(respString), Charset.forName("UTF-8"));
}
});
myHttpResponse = new Response.Builder()
.request(myRequest)
.protocol(myProtocol)
.code(200)
.body(ResponseBody.create(MediaType.parse(Constants.CT_FHIR_XML + "; charset=UTF-8"), respString))
.build();
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
@ -124,13 +116,14 @@ public class GenericClientDstu3IT {
bin.setContentType(Constants.CT_FHIR_JSON);
client.create().resource(bin).execute();
ourLog.info(Arrays.asList(capt.getAllValues().get(0).getAllHeaders()).toString());
Request request = capt.getAllValues().get(0);
ourLog.info(request.headers().toString());
assertEquals("http://example.com/fhir/Binary", capt.getAllValues().get(0).getURI().toASCIIString());
assertEquals("http://example.com/fhir/Binary", request.url().toString());
validateUserAgent(capt);
assertEquals("application/xml+fhir;charset=utf-8", capt.getAllValues().get(0).getHeaders("Content-Type")[0].getValue().toLowerCase().replace(" ", ""));
assertEquals(Constants.CT_FHIR_XML, capt.getAllValues().get(0).getHeaders("Accept")[0].getValue());
assertEquals(Constants.CT_FHIR_XML_NEW + ";charset=utf-8", request.body().contentType().toString().toLowerCase().replace(" ", ""));
assertEquals(Constants.HEADER_ACCEPT_VALUE_XML_NON_LEGACY, request.header("Accept"));
Binary output = ourCtx.newXmlParser().parseResource(Binary.class, extractBodyAsString(capt));
assertEquals(Constants.CT_FHIR_JSON, output.getContentType());
@ -143,7 +136,7 @@ public class GenericClientDstu3IT {
*/
@Test
public void testNullAndEmptyParamValuesAreIgnored() throws Exception {
ArgumentCaptor<HttpUriRequest> capt = prepareClientForSearchResponse();
ArgumentCaptor<Request> capt = prepareClientForSearchResponse();
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
int idx = 0;
@ -160,7 +153,7 @@ public class GenericClientDstu3IT {
.execute();
//@formatter:on
assertEquals("http://example.com/fhir/Patient?_format=json", capt.getAllValues().get(idx).getURI().toString());
assertEquals("http://example.com/fhir/Patient", capt.getAllValues().get(idx).url().toString());
idx++;
}
@ -171,7 +164,6 @@ public class GenericClientDstu3IT {
* TODO: narratives don't work without stax
*/
@Test
@Ignore
public void testBinaryCreateWithNoContentType() throws Exception {
IParser p = ourCtx.newJsonParser();
@ -179,16 +171,12 @@ public class GenericClientDstu3IT {
conf.getText().setDivAsString("OK!");
final String respString = p.encodeResourceToString(conf);
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_NEW + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).thenAnswer(new Answer<ReaderInputStream>() {
@Override
public ReaderInputStream answer(InvocationOnMock theInvocation) throws Throwable {
return new ReaderInputStream(new StringReader(respString), Charset.forName("UTF-8"));
}
});
myHttpResponse = new Response.Builder()
.request(myRequest)
.protocol(myProtocol)
.code(200)
.body(ResponseBody.create(MediaType.parse(Constants.CT_FHIR_JSON_NEW + "; charset=UTF-8"), respString))
.build();
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
@ -196,13 +184,14 @@ public class GenericClientDstu3IT {
bin.setContent(new byte[] { 0, 1, 2, 3, 4 });
client.create().resource(bin).execute();
ourLog.info(Arrays.asList(capt.getAllValues().get(0).getAllHeaders()).toString());
Request request = capt.getAllValues().get(0);
ourLog.info(request.headers().toString());
assertEquals("http://example.com/fhir/Binary", capt.getAllValues().get(0).getURI().toASCIIString());
assertEquals("http://example.com/fhir/Binary", request.url().toString());
validateUserAgent(capt);
assertEquals("application/xml+fhir;charset=utf-8", capt.getAllValues().get(0).getHeaders("Content-Type")[0].getValue().toLowerCase().replace(" ", ""));
assertEquals(Constants.CT_FHIR_XML, capt.getAllValues().get(0).getHeaders("Accept")[0].getValue());
assertEquals(Constants.CT_FHIR_XML_NEW + ";charset=utf-8", request.body().contentType().toString().toLowerCase().replace(" ", ""));
assertEquals(Constants.HEADER_ACCEPT_VALUE_XML_NON_LEGACY, request.header("Accept"));
assertArrayEquals(new byte[] { 0, 1, 2, 3, 4 }, ourCtx.newXmlParser().parseResource(Binary.class, extractBodyAsString(capt)).getContent());
}
@ -210,11 +199,15 @@ public class GenericClientDstu3IT {
@SuppressWarnings("unchecked")
@Test
public void testClientFailures() throws Exception {
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()).thenThrow(IllegalStateException.class, RuntimeException.class, Exception.class);
ResponseBody body = mock(ResponseBody.class);
when(body.source()).thenThrow(IllegalStateException.class, RuntimeException.class, Exception.class);
myHttpResponse = new Response.Builder()
.request(myRequest)
.protocol(myProtocol)
.code(200)
.body(body)
.build();
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
@ -246,30 +239,20 @@ public class GenericClientDstu3IT {
* TODO: narratives don't work without stax
*/
@Test
@Ignore
public void testCreateWithPreferRepresentationServerReturnsResource() throws Exception {
final IParser p = ourCtx.newJsonParser();
final Patient resp1 = new Patient();
resp1.getText().setDivAsString("FINAL VALUE");
String respString = p.encodeResourceToString(resp1);
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.getAllHeaders()).thenAnswer(new Answer<Header[]>() {
@Override
public Header[] answer(InvocationOnMock theInvocation) throws Throwable {
return new Header[] { new BasicHeader(Constants.HEADER_LOCATION, "http://foo.com/base/Patient/222/_history/3") };
}
});
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_JSON_NEW + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).thenAnswer(new Answer<ReaderInputStream>() {
@Override
public ReaderInputStream answer(InvocationOnMock theInvocation) throws Throwable {
myAnswerCount++;
return new ReaderInputStream(new StringReader(p.encodeResourceToString(resp1)), Charset.forName("UTF-8"));
}
});
myHttpResponse = new Response.Builder()
.request(myRequest)
.protocol(myProtocol)
.code(200)
.body(ResponseBody.create(MediaType.parse(Constants.CT_FHIR_JSON_NEW + "; charset=UTF-8"), respString))
.headers(Headers.of(Constants.HEADER_LOCATION, "http://foo.com/base/Patient/222/_history/3"))
.build();
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
@ -278,19 +261,29 @@ public class GenericClientDstu3IT {
MethodOutcome outcome = client.create().resource(pt).prefer(PreferReturnEnum.REPRESENTATION).execute();
assertEquals(1, myAnswerCount);
assertNull(outcome.getOperationOutcome());
assertNotNull(outcome.getResource());
assertEquals("<div xmlns=\"http://www.w3.org/1999/xhtml\">FINAL VALUE</div>", ((Patient) outcome.getResource()).getText().getDivAsString());
assertEquals(myAnswerCount, capt.getAllValues().size());
assertEquals("http://example.com/fhir/Patient", capt.getAllValues().get(0).getURI().toASCIIString());
assertEquals("http://example.com/fhir/Patient", capt.getAllValues().get(0).url().toString());
}
private ArgumentCaptor<Request> prepareClientForSearchResponse() throws IOException, ClientProtocolException {
final String respString = "{\"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\"}]}}]}";
myHttpResponse = new Response.Builder()
.request(myRequest)
.protocol(myProtocol)
.code(200)
.body(ResponseBody.create(MediaType.parse(Constants.CT_FHIR_JSON + "; charset=UTF-8"), respString))
.headers(Headers.of(Constants.HEADER_LOCATION, "http://foo.com/base/Patient/222/_history/3"))
.build();
return capt;
}
/*
@ -304,10 +297,11 @@ public class GenericClientDstu3IT {
final Patient patient = new Patient();
patient.addName().addFamily("FAM");
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"));
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.execute().code()).thenReturn(200);
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_JSON_NEW + "; charset=UTF-8"));
when(myHttpResponse.execute().body()).thenReturn(ResponseBody.create(MediaType.parse(Constants.CT_FHIR_XML + "; charset=UTF-8"), respString));
when(myHttpResponse.getEntity().getContent()).thenAnswer(new Answer<ReaderInputStream>() {
private int myCount = 0;
@ -345,8 +339,8 @@ public class GenericClientDstu3IT {
@Test
public void testHttp499() throws Exception {
ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class);
when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 499, "Wacky Message"));
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_XML + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).thenAnswer(new Answer<InputStream>() {
@ -370,8 +364,8 @@ public class GenericClientDstu3IT {
@Test
public void testHttp501() throws Exception {
ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class);
when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 501, "Not Implemented"));
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_XML + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).thenAnswer(new Answer<InputStream>() {
@ -421,9 +415,9 @@ public class GenericClientDstu3IT {
final String encoded = p.encodeResourceToString(bundle);
assertEquals("{\"resourceType\":\"Bundle\",\"id\":\"BUNDLE1\",\"entry\":[{\"resource\":{\"resourceType\":\"Patient\",\"id\":\"PATIENT1\",\"name\":[{\"family\":[\"PATIENT1\"]}]}}]}", encoded);
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"));
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.execute().code()).thenReturn(200);
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_JSON_NEW + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).thenAnswer(new Answer<ReaderInputStream>() {
@Override
@ -456,9 +450,9 @@ public class GenericClientDstu3IT {
patient.addName().addFamily("FAM");
final String respString = p.encodeResourceToString(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"));
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.execute().code()).thenReturn(200);
when(myHttpResponse.getEntity().getContentType()).thenReturn(null);
when(myHttpResponse.getEntity().getContent()).thenAnswer(new Answer<ReaderInputStream>() {
@Override
@ -486,9 +480,9 @@ public class GenericClientDstu3IT {
patient.addName().addFamily("FAM");
final String respString = p.encodeResourceToString(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"));
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.execute().code()).thenReturn(200);
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", "text/plain"));
// when(myHttpResponse.getEntity().getContentType()).thenReturn(null);
when(myHttpResponse.getEntity().getContent()).thenAnswer(new Answer<ReaderInputStream>() {
@ -514,9 +508,9 @@ public class GenericClientDstu3IT {
public void testSearchByDate() throws Exception {
final String msg = "{\"resourceType\":\"Bundle\",\"id\":null,\"base\":\"http://localhost:57931/fhir/contextDev\",\"total\":1,\"link\":[{\"relation\":\"self\",\"url\":\"http://localhost:57931/fhir/contextDev/Patient?identifier=urn%3AMultiFhirVersionTest%7CtestSubmitPatient01&_format=json\"}],\"entry\":[{\"resource\":{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}}]}";
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"));
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.execute().code()).thenReturn(200);
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_JSON + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).then(new Answer<InputStream>() {
@Override
@ -548,9 +542,9 @@ public class GenericClientDstu3IT {
public void testSearchByString() throws Exception {
final String msg = "{\"resourceType\":\"Bundle\",\"id\":null,\"base\":\"http://localhost:57931/fhir/contextDev\",\"total\":1,\"link\":[{\"relation\":\"self\",\"url\":\"http://localhost:57931/fhir/contextDev/Patient?identifier=urn%3AMultiFhirVersionTest%7CtestSubmitPatient01&_format=json\"}],\"entry\":[{\"resource\":{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}}]}";
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"));
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.execute().code()).thenReturn(200);
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_JSON + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).then(new Answer<InputStream>() {
@Override
@ -628,9 +622,9 @@ public class GenericClientDstu3IT {
public void testSearchByUrl() throws Exception {
final String msg = "{\"resourceType\":\"Bundle\",\"id\":null,\"base\":\"http://localhost:57931/fhir/contextDev\",\"total\":1,\"link\":[{\"relation\":\"self\",\"url\":\"http://localhost:57931/fhir/contextDev/Patient?identifier=urn%3AMultiFhirVersionTest%7CtestSubmitPatient01&_format=json\"}],\"entry\":[{\"resource\":{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}}]}";
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"));
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.execute().code()).thenReturn(200);
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_JSON + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).then(new Answer<InputStream>() {
@Override
@ -658,9 +652,9 @@ public class GenericClientDstu3IT {
public void testAcceptHeaderWithEncodingSpecified() throws Exception {
final String msg = "{\"resourceType\":\"Bundle\",\"id\":null,\"base\":\"http://localhost:57931/fhir/contextDev\",\"total\":1,\"link\":[{\"relation\":\"self\",\"url\":\"http://localhost:57931/fhir/contextDev/Patient?identifier=urn%3AMultiFhirVersionTest%7CtestSubmitPatient01&_format=json\"}],\"entry\":[{\"resource\":{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}}]}";
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"));
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.execute().code()).thenReturn(200);
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_JSON + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).then(new Answer<InputStream>() {
@Override
@ -707,21 +701,6 @@ public class GenericClientDstu3IT {
}
}
private ArgumentCaptor<HttpUriRequest> prepareClientForSearchResponse() throws IOException, ClientProtocolException {
final String msg = "{\"resourceType\":\"Bundle\",\"id\":null,\"base\":\"http://localhost:57931/fhir/contextDev\",\"total\":1,\"link\":[{\"relation\":\"self\",\"url\":\"http://localhost:57931/fhir/contextDev/Patient?identifier=urn%3AMultiFhirVersionTest%7CtestSubmitPatient01&_format=json\"}],\"entry\":[{\"resource\":{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}}]}";
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()).then(new Answer<InputStream>() {
@Override
public InputStream answer(InvocationOnMock theInvocation) throws Throwable {
return new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"));
}
});
return capt;
}
@ -764,9 +743,7 @@ public class GenericClientDstu3IT {
}
/**
* TODO: narratives don't work without stax
*/
//TODO: narratives don't work without stax
@Test
@Ignore
public void testUpdateById() throws Exception {
@ -776,9 +753,9 @@ public class GenericClientDstu3IT {
conf.getText().setDivAsString("OK!");
final String respString = p.encodeResourceToString(conf);
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"));
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.execute().code()).thenReturn(200);
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_JSON_NEW + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).thenAnswer(new Answer<ReaderInputStream>() {
@Override
@ -806,9 +783,7 @@ public class GenericClientDstu3IT {
assertThat(body, containsString("<id value=\"111\"/>"));
}
/**
* TODO: narratives don't work without stax
*/
// TODO: narratives don't work without stax
@Test
@Ignore
public void testUpdateWithPreferRepresentationServerReturnsOO() throws Exception {
@ -820,9 +795,9 @@ public class GenericClientDstu3IT {
final Patient resp1 = new Patient();
resp1.getText().setDivAsString("FINAL VALUE");
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"));
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.execute().code()).thenReturn(200);
when(myHttpResponse.getAllHeaders()).thenAnswer(new Answer<Header[]>() {
@Override
public Header[] answer(InvocationOnMock theInvocation) throws Throwable {
@ -868,9 +843,9 @@ public class GenericClientDstu3IT {
final Patient resp1 = new Patient();
resp1.setActive(true);
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"));
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.execute().code()).thenReturn(200);
when(myHttpResponse.getAllHeaders()).thenAnswer(new Answer<Header[]>() {
@Override
public Header[] answer(InvocationOnMock theInvocation) throws Throwable {
@ -913,9 +888,9 @@ public class GenericClientDstu3IT {
conf.setCopyright("COPY");
final String respString = p.encodeResourceToString(conf);
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"));
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.execute().code()).thenReturn(200);
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_JSON_NEW + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).thenAnswer(new Answer<ReaderInputStream>() {
@Override
@ -932,9 +907,7 @@ public class GenericClientDstu3IT {
}
/**
* TODO: narratives don't work without stax
*/
// TODO: narratives don't work without stax
@Test
@Ignore
public void testValidate() throws Exception {
@ -943,9 +916,9 @@ public class GenericClientDstu3IT {
final OperationOutcome resp0 = new OperationOutcome();
resp0.getText().setDivAsString("OK!");
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"));
ArgumentCaptor<Request> capt = ArgumentCaptor.forClass(Request.class);
when(myHttpClient.newCall(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.execute().code()).thenReturn(200);
when(myHttpResponse.getAllHeaders()).thenAnswer(new Answer<Header[]>() {
@Override
public Header[] answer(InvocationOnMock theInvocation) throws Throwable {
@ -973,11 +946,10 @@ public class GenericClientDstu3IT {
}
private void validateUserAgent(ArgumentCaptor<HttpUriRequest> capt) {
assertEquals(1, capt.getAllValues().get(0).getHeaders("User-Agent").length);
assertEquals(expectedUserAgent(), capt.getAllValues().get(0).getHeaders("User-Agent")[0].getValue());
}
*/
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
@ -986,11 +958,10 @@ public class GenericClientDstu3IT {
@BeforeClass
public static void beforeClass() {
// Force StAX to fail like it will on android
System.setProperty("javax.xml.stream.XMLInputFactory", "FOO");
System.setProperty("javax.xml.stream.XMLOutputFactory", "FOO");
// // Force StAX to fail like it will on android
// System.setProperty("javax.xml.stream.XMLInputFactory", "FOO");
// System.setProperty("javax.xml.stream.XMLOutputFactory", "FOO");
ourCtx = FhirContext.forDstu3();
}
}

View File

@ -1,5 +1,7 @@
package ca.uhn.fhir.context;
import java.lang.reflect.Method;
/*
* #%L
* HAPI FHIR - Core Library
@ -197,6 +199,24 @@ public class FhirContext {
}
myResourceTypesToScan = theResourceTypes;
/*
* Check if we're running in Android mode and configure the context appropriately if so
*/
try {
Class<?> clazz = Class.forName("ca.uhn.fhir.android.AndroidMarker");
ourLog.info("Android mode detected, configuring FhirContext for Android operation");
try {
Method method = clazz.getMethod("configureContext", FhirContext.class);
method.invoke(null, this);
} catch (Throwable e) {
ourLog.warn("Failed to configure context for Android operation", e);
}
} catch (ClassNotFoundException e) {
ourLog.trace("Android mode not detected");
}
}
private String createUnknownResourceNameError(String theResourceName, FhirVersionEnum theVersion) {
@ -302,7 +322,6 @@ public class FhirContext {
* Returns the scanned runtime model for the given type. This is an advanced feature which is generally only needed
* for extending the core library.
*/
@SuppressWarnings("unchecked")
public RuntimeResourceDefinition getResourceDefinition(Class<? extends IBaseResource> theResourceType) {
validateInitialized();
if (theResourceType == null) {
@ -364,7 +383,6 @@ public class FhirContext {
* Note that this method is case insensitive!
* </p>
*/
@SuppressWarnings("unchecked")
public RuntimeResourceDefinition getResourceDefinition(String theResourceName) {
validateInitialized();
Validate.notBlank(theResourceName, "theResourceName must not be blank");
@ -378,7 +396,7 @@ public class FhirContext {
throw new DataFormatException(createUnknownResourceNameError(theResourceName, myVersion.getVersion()));
}
if (IBaseResource.class.isAssignableFrom(clazz)) {
retVal = scanResourceType((Class<? extends IResource>) clazz);
retVal = scanResourceType(clazz);
}
}

View File

@ -1,5 +1,25 @@
package ca.uhn.fhir.rest.gclient;
/*
* #%L
* HAPI FHIR - Core Library
* %%
* Copyright (C) 2014 - 2016 University Health Network
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import org.hl7.fhir.instance.model.api.IBaseResource;
public interface IOperationUntypedWithInput<T> extends IClientExecutable<IOperationUntypedWithInput<T>, T> {

View File

@ -1,5 +1,16 @@
package ca.uhn.fhir.okhttp.client;
import static ca.uhn.fhir.okhttp.utils.UrlStringUtils.deleteLastCharacter;
import static ca.uhn.fhir.okhttp.utils.UrlStringUtils.endsWith;
import static ca.uhn.fhir.okhttp.utils.UrlStringUtils.everythingAfterFirstQuestionMark;
import static ca.uhn.fhir.okhttp.utils.UrlStringUtils.hasQuestionMark;
import static ca.uhn.fhir.okhttp.utils.UrlStringUtils.withTrailingQuestionMarkRemoved;
import java.util.List;
import java.util.Map;
import org.hl7.fhir.instance.model.api.IBaseBinary;
/*
* #%L
* HAPI FHIR OkHttp Client
@ -31,12 +42,6 @@ import ca.uhn.fhir.rest.server.EncodingEnum;
import ca.uhn.fhir.rest.server.RestfulServerUtils;
import okhttp3.*;
import okhttp3.internal.Version;
import org.hl7.fhir.instance.model.api.IBaseBinary;
import java.util.List;
import java.util.Map;
import static ca.uhn.fhir.okhttp.utils.UrlStringUtils.*;
/**
* A Http Request based on OkHttp. This is an adapter around the class
@ -46,7 +51,7 @@ import static ca.uhn.fhir.okhttp.utils.UrlStringUtils.*;
*/
public class OkHttpRestfulClient implements IHttpClient {
private OkHttpClient myClient;
private Call.Factory myClient;
private StringBuilder myUrl;
private Map<String, List<String>> myIfNoneExistParams;
private String myIfNoneExistString;
@ -54,7 +59,7 @@ public class OkHttpRestfulClient implements IHttpClient {
private List<Header> myHeaders;
private OkHttpRestfulRequest myRequest;
public OkHttpRestfulClient(OkHttpClient theClient,
public OkHttpRestfulClient(Call.Factory theClient,
StringBuilder theUrl,
Map<String, List<String>> theIfNoneExistParams,
String theIfNoneExistString,

View File

@ -25,6 +25,7 @@ import ca.uhn.fhir.rest.api.RequestTypeEnum;
import ca.uhn.fhir.rest.client.RestfulClientFactory;
import ca.uhn.fhir.rest.client.api.Header;
import ca.uhn.fhir.rest.client.api.IHttpClient;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import java.net.InetSocketAddress;
@ -39,7 +40,7 @@ import java.util.Map;
*/
public class OkHttpRestfulClientFactory extends RestfulClientFactory {
private OkHttpClient myNativeClient;
private Call.Factory myNativeClient;
public OkHttpRestfulClientFactory() {
super();
@ -59,7 +60,7 @@ public class OkHttpRestfulClientFactory extends RestfulClientFactory {
myNativeClient = null;
}
public synchronized OkHttpClient getNativeClient() {
public synchronized Call.Factory getNativeClient() {
if (myNativeClient == null) {
myNativeClient = new OkHttpClient();
}
@ -83,13 +84,13 @@ public class OkHttpRestfulClientFactory extends RestfulClientFactory {
*/
@Override
public void setHttpClient(Object okHttpClient) {
myNativeClient = (OkHttpClient) okHttpClient;
myNativeClient = (Call.Factory) okHttpClient;
}
@Override
public void setProxy(String theHost, Integer thePort) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(theHost, thePort));
OkHttpClient.Builder builder = getNativeClient().newBuilder().proxy(proxy);
OkHttpClient.Builder builder = ((OkHttpClient)getNativeClient()).newBuilder().proxy(proxy);
setHttpClient(builder.build());
}

View File

@ -1,5 +1,9 @@
package ca.uhn.fhir.okhttp.client;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/*
* #%L
* HAPI FHIR OkHttp Client
@ -24,14 +28,10 @@ import ca.uhn.fhir.rest.api.RequestTypeEnum;
import ca.uhn.fhir.rest.client.api.IHttpRequest;
import ca.uhn.fhir.rest.client.api.IHttpResponse;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Call.Factory;
import okhttp3.Request;
import okhttp3.RequestBody;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* Adapter for building an OkHttp-specific request.
*
@ -40,12 +40,12 @@ import java.util.Map;
public class OkHttpRestfulRequest implements IHttpRequest {
private final Request.Builder myRequestBuilder;
private OkHttpClient myClient;
private Factory myClient;
private String myUrl;
private RequestTypeEnum myRequestTypeEnum;
private RequestBody myRequestBody;
public OkHttpRestfulRequest(OkHttpClient theClient, String theUrl, RequestTypeEnum theRequestTypeEnum, RequestBody theRequestBody) {
public OkHttpRestfulRequest(Call.Factory theClient, String theUrl, RequestTypeEnum theRequestTypeEnum, RequestBody theRequestBody) {
myClient = theClient;
myUrl = theUrl;
myRequestTypeEnum = theRequestTypeEnum;

View File

@ -59,11 +59,15 @@ public class OkHttpRestfulResponse implements IHttpResponse {
@Override
public String getMimeType() {
String contentType = myResponse.header(Constants.HEADER_CONTENT_TYPE);
MediaType mediaType = null;
if (contentType == null) {
return null;
if (myResponse.body() != null) {
mediaType = myResponse.body().contentType();
}
} else {
mediaType = MediaType.parse(contentType);
}
MediaType mediaType = MediaType.parse(contentType);
if (mediaType == null) {
return null;
}

View File

@ -1,6 +1,7 @@
package ca.uhn.fhir.okhttp;
import ca.uhn.fhir.okhttp.client.OkHttpRestfulClientFactory;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import org.junit.Before;
import org.junit.Test;
@ -20,14 +21,14 @@ public class OkHttpRestfulClientFactoryTest {
@Test
public void testGetNativeClient_noClientSet_returnsADefault() throws Exception {
OkHttpClient actualNativeClient = clientFactory.getNativeClient();
Call.Factory actualNativeClient = clientFactory.getNativeClient();
assertNotNull(actualNativeClient);
}
@Test
public void testGetNativeClient_noProxySet_defaultHasNoProxySet() throws Exception {
OkHttpClient actualNativeClient = clientFactory.getNativeClient();
OkHttpClient actualNativeClient = (OkHttpClient) clientFactory.getNativeClient();
assertEquals(null, actualNativeClient.proxy());
}

View File

@ -275,25 +275,6 @@
</sources>
</configuration>
</execution>
<!--
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>../hapi-fhir-structures-dstu/src/test/java</source>
<source>../hapi-fhir-structures-dstu2/src/test/java</source>
<source>../hapi-fhir-structures-hl7org-dstu2/src/test/java</source>
<source>../hapi-fhir-structures-dstu3/src/test/java</source>
<source>../hapi-fhir-jpaserver-base/src/test/java</source>
<source>../hapi-fhir-base/src/test/java</source>
</sources>
</configuration>
</execution>
-->
</executions>
</plugin>
<plugin>
@ -332,6 +313,9 @@
<testResource>
<directory>../hapi-fhir-structures-dstu3/src/test/resources</directory>
</testResource>
<testResource>
<directory>../hapi-fhir-client-okhttp/src/test/resources</directory>
</testResource>
</testResources>
</build>
<reporting>

View File

@ -1416,6 +1416,8 @@ public abstract class BaseHapiFhirDao<T extends IBaseResource> implements IDao {
myEntityManager.persist(next);
}
theEntity.toString();
} // if thePerformIndexing
theEntity = myEntityManager.merge(theEntity);

View File

@ -46,4 +46,4 @@ public class CustomObservationDstu3 extends Observation {
myEyeColour = theEyeColour;
}
}
}

View File

@ -626,6 +626,33 @@ public class FhirSystemDaoDstu3Test extends BaseJpaDstu3SystemTest {
assertEquals(new IdType(patientId).toUnqualifiedVersionless().getValue(), o.getSubject().getReference());
}
/**
* See #467
*/
@Test
public void testTransactionWithLink() {
String methodName = "testTransactionWithLink";
Bundle request = new Bundle();
Patient p = new Patient();
p.addIdentifier().setSystem("urn:system").setValue(methodName);
p.addName().addFamily("Hello");
p.setId("Patient/" + methodName);
request.addEntry().setResource(p).getRequest().setMethod(HTTPVerb.POST);
Observation o = new Observation();
o.getCode().setText("Some Observation");
o.getSubject().setReference("Patient/" + methodName);
request.addEntry().setResource(o).getRequest().setMethod(HTTPVerb.POST);
Bundle resp = mySystemDao.transaction(mySrd, request);
assertEquals(BundleType.TRANSACTIONRESPONSE, resp.getTypeElement().getValue());
assertEquals(2, resp.getEntry().size());
// o = (Observation) myObservationDao.read(new IdType(respEntry.getResponse().getLocationElement()), mySrd);
// assertEquals(new IdType(patientId).toUnqualifiedVersionless().getValue(), o.getSubject().getReference());
}
@Test
public void testTransactionCreateNoMatchUrl() {
String methodName = "testTransactionCreateNoMatchUrl";

View File

@ -40,6 +40,7 @@ import com.google.common.collect.Sets;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.parser.PatientWithExtendedContactDstu3.CustomContactComponent;
import ca.uhn.fhir.parser.XmlParserDstu3Test.TestPatientFor327;
import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.util.TestUtil;
import ca.uhn.fhir.validation.FhirValidator;
@ -88,6 +89,47 @@ public class JsonParserDstu3Test {
}
/**
* See #327
*/
@Test
public void testEncodeExtensionWithContainedResource() {
TestPatientFor327 patient = new TestPatientFor327();
patient.setBirthDateElement(new DateType("2016-04-14"));
List<Reference> conditions = new ArrayList<Reference>();
Condition condition = new Condition();
condition.addBodySite().setText("BODY SITE");
conditions.add(new Reference(condition));
patient.setCondition(conditions);
String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(patient);
ourLog.info(encoded);
//@formatter:off
assertThat(encoded, stringContainsInOrder(
"<Patient xmlns=\"http://hl7.org/fhir\">",
"<contained>",
"<Condition xmlns=\"http://hl7.org/fhir\">",
"<id value=\"1\"/>",
"<bodySite>",
"<text value=\"BODY SITE\"/>",
"</bodySite>",
"</Condition>",
"</contained>",
"<extension url=\"testCondition\">",
"<valueReference>",
"<reference value=\"#1\"/>",
"</valueReference>",
"</extension>",
"<birthDate value=\"2016-04-14\"/>",
"</Patient>"
));
//@formatter:on
}
@Test
public void testParseMissingArray() throws IOException {

View File

@ -33,6 +33,11 @@
Thanks to Pater Girard for all of his help during the connectathon
in implementing this feature!
</action>
<action type="add">
Android library now uses OkHttp client by default instead
of Apache HttpClient. This should lead to much simpler
support for Android in the future.
</action>
<action type="add">
Both client and server now use the new STU3 mime types by default
if running in STU3 mode (in other words, using an STU3