Add some tests to GenericClient

This commit is contained in:
jamesagnew 2016-06-16 07:42:06 -04:00
parent 9392d8c104
commit c9f85111ab
9 changed files with 283 additions and 20 deletions

View File

@ -44,6 +44,7 @@ import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.TreeSet; import java.util.TreeSet;
import org.apache.commons.io.IOUtils;
import org.hl7.fhir.instance.model.api.IAnyResource; import org.hl7.fhir.instance.model.api.IAnyResource;
import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseBackboneElement; import org.hl7.fhir.instance.model.api.IBaseBackboneElement;
@ -574,6 +575,8 @@ class ModelScanner {
} }
} catch (IOException e) { } catch (IOException e) {
throw new ConfigurationException("Failed to load model property file from classpath: " + "/ca/uhn/fhir/model/dstu/model.properties"); throw new ConfigurationException("Failed to load model property file from classpath: " + "/ca/uhn/fhir/model/dstu/model.properties");
} finally {
IOUtils.closeQuietly(str);
} }
return retVal; return retVal;

View File

@ -70,6 +70,13 @@ import ca.uhn.fhir.util.OperationOutcomeUtil;
public abstract class BaseClient implements IRestfulClient { public abstract class BaseClient implements IRestfulClient {
/**
* This property is used by unit tests - do not rely on it in production code
* as it may change at any time. If you want to capture responses in a reliable
* way in your own code, just use client interceptors
*/
static final String HAPI_CLIENT_KEEPRESPONSES = "hapi.client.keepresponses";
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseClient.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseClient.class);
private final IHttpClient myClient; private final IHttpClient myClient;
@ -89,6 +96,15 @@ public abstract class BaseClient implements IRestfulClient {
myClient = theClient; myClient = theClient;
myUrlBase = theUrlBase; myUrlBase = theUrlBase;
myFactory = theFactory; myFactory = theFactory;
/*
* This property is used by unit tests - do not rely on it in production code
* as it may change at any time. If you want to capture responses in a reliable
* way in your own code, just use client interceptors
*/
if ("true".equals(System.getProperty(HAPI_CLIENT_KEEPRESPONSES))) {
setKeepResponses(true);
}
} }
protected Map<String, List<String>> createExtraParams() { protected Map<String, List<String>> createExtraParams() {

View File

@ -76,6 +76,7 @@ import ca.uhn.fhir.rest.api.SummaryEnum;
import ca.uhn.fhir.rest.client.api.IHttpClient; import ca.uhn.fhir.rest.client.api.IHttpClient;
import ca.uhn.fhir.rest.client.api.IHttpRequest; import ca.uhn.fhir.rest.client.api.IHttpRequest;
import ca.uhn.fhir.rest.client.exceptions.NonFhirResponseException; import ca.uhn.fhir.rest.client.exceptions.NonFhirResponseException;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.rest.gclient.IClientExecutable; import ca.uhn.fhir.rest.gclient.IClientExecutable;
import ca.uhn.fhir.rest.gclient.ICreate; import ca.uhn.fhir.rest.gclient.ICreate;
import ca.uhn.fhir.rest.gclient.ICreateTyped; import ca.uhn.fhir.rest.gclient.ICreateTyped;
@ -174,8 +175,8 @@ public class GenericClient extends BaseClient implements IGenericClient {
@Override @Override
public IBaseConformance conformance() { public IBaseConformance conformance() {
if (myContext.getVersion().getVersion().equals(FhirVersionEnum.DSTU2_HL7ORG)) { if (myContext.getVersion().getVersion().isRi()) {
throw new IllegalArgumentException("Must call fetchConformance() instead of conformance() for RI/DSTU3+ structures"); throw new IllegalArgumentException("Must call fetchConformance() instead of conformance() for RI/STU3+ structures");
} }
HttpGetClientInvocation invocation = MethodUtil.createConformanceInvocation(getFhirContext()); HttpGetClientInvocation invocation = MethodUtil.createConformanceInvocation(getFhirContext());
@ -376,7 +377,7 @@ public class GenericClient extends BaseClient implements IGenericClient {
} }
} }
throw new RuntimeException(myContext.getLocalizer().getMessage(I18N_CANNOT_DETEMINE_RESOURCE_TYPE, theUrl.getValueAsString())); throw new IllegalArgumentException(myContext.getLocalizer().getMessage(I18N_CANNOT_DETEMINE_RESOURCE_TYPE, theUrl.getValueAsString()));
} }
@ -385,6 +386,12 @@ public class GenericClient extends BaseClient implements IGenericClient {
// return doReadOrVRead(theType, theId, false, null, null); // return doReadOrVRead(theType, theId, false, null, null);
// } // }
/**
* @deprecated Use {@link LoggingInterceptor} as a client interceptor registered to your
* client instead, as this provides much more fine-grained control over what is logged. This
* method will be removed at some point (deprecated in HAPI 1.6 - 2016-06-16)
*/
@Deprecated
public boolean isLogRequestAndResponse() { public boolean isLogRequestAndResponse() {
return myLogRequestAndResponse; return myLogRequestAndResponse;
} }

View File

@ -37,6 +37,7 @@ import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.api.IRestfulClient; import ca.uhn.fhir.rest.client.api.IRestfulClient;
import ca.uhn.fhir.rest.client.exceptions.FhirClientConnectionException; import ca.uhn.fhir.rest.client.exceptions.FhirClientConnectionException;
import ca.uhn.fhir.rest.client.exceptions.FhirClientInappropriateForServerException; import ca.uhn.fhir.rest.client.exceptions.FhirClientInappropriateForServerException;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.rest.gclient.ICreate; import ca.uhn.fhir.rest.gclient.ICreate;
import ca.uhn.fhir.rest.gclient.IDelete; import ca.uhn.fhir.rest.gclient.IDelete;
import ca.uhn.fhir.rest.gclient.IFetchConformanceUntyped; import ca.uhn.fhir.rest.gclient.IFetchConformanceUntyped;
@ -288,7 +289,11 @@ public interface IGenericClient extends IRestfulClient {
* *
* @param theLogRequestAndResponse * @param theLogRequestAndResponse
* Should requests and responses be logged * Should requests and responses be logged
* @deprecated Use {@link LoggingInterceptor} as a client interceptor registered to your
* client instead, as this provides much more fine-grained control over what is logged. This
* method will be removed at some point (deprecated in HAPI 1.6 - 2016-06-16)
*/ */
@Deprecated
void setLogRequestAndResponse(boolean theLogRequestAndResponse); void setLogRequestAndResponse(boolean theLogRequestAndResponse);
/** /**

View File

@ -15,7 +15,9 @@ import java.io.InputStream;
import java.io.StringReader; import java.io.StringReader;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream; import org.apache.commons.io.input.ReaderInputStream;
@ -33,6 +35,7 @@ import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicStatusLine; import org.apache.http.message.BasicStatusLine;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.hamcrest.core.StringContains; import org.hamcrest.core.StringContains;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -82,6 +85,8 @@ public class GenericClientTest {
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
myHttpResponse = mock(HttpResponse.class, new ReturnsDeepStubs()); myHttpResponse = mock(HttpResponse.class, new ReturnsDeepStubs());
System.setProperty(BaseClient.HAPI_CLIENT_KEEPRESPONSES, "true");
} }
private String extractBody(ArgumentCaptor<HttpUriRequest> capt, int count) throws IOException { private String extractBody(ArgumentCaptor<HttpUriRequest> capt, int count) throws IOException {
@ -89,6 +94,24 @@ public class GenericClientTest {
return body; return body;
} }
@Test
public void testInvalidCalls() {
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
try {
client.meta();
fail();
} catch (IllegalStateException e) {
assertEquals("Can not call $meta operations on a DSTU1 client", e.getMessage());
}
try {
client.operation();
fail();
} catch (IllegalStateException e) {
assertEquals("Operations are only supported in FHIR DSTU2 and later. This client was created using a context configured for DSTU1", e.getMessage());
}
}
private String getPatientFeedWithOneResult() { private String getPatientFeedWithOneResult() {
//@formatter:off //@formatter:off
String msg = "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n" + String msg = "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n" +
@ -155,6 +178,9 @@ public class GenericClientTest {
resp = client.create().resource(ourCtx.newXmlParser().encodeResourceToString(p1)).execute(); resp = client.create().resource(ourCtx.newXmlParser().encodeResourceToString(p1)).execute();
assertNull(resp.getCreated()); assertNull(resp.getCreated());
ourLog.info("lastRequest: {}", ((GenericClient)client).getLastRequest());
ourLog.info("lastResponse: {}", ((GenericClient)client).getLastResponse());
ourLog.info("lastResponseBody: {}", ((GenericClient)client).getLastResponseBody());
} }
@Test @Test
@ -1471,6 +1497,25 @@ public class GenericClientTest {
} }
@SuppressWarnings("deprecation")
@Test
public void testTransactionOldStyle() 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");
List<IBaseResource> input = new ArrayList<IBaseResource>();
input.addAll(bundle.toListOfResources());
client.transaction(input);
}
@Test @Test
public void testTransactionJson() throws Exception { public void testTransactionJson() throws Exception {
String bundleStr = IOUtils.toString(getClass().getResourceAsStream("/bundle.json")); String bundleStr = IOUtils.toString(getClass().getResourceAsStream("/bundle.json"));

View File

@ -11,6 +11,7 @@ import ca.uhn.fhir.context.RuntimeResourceDefinition;
import ca.uhn.fhir.model.dstu2.resource.Patient; import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.model.dstu2.valueset.AdministrativeGenderEnum; import ca.uhn.fhir.model.dstu2.valueset.AdministrativeGenderEnum;
import ca.uhn.fhir.model.dstu2.valueset.MaritalStatusCodesEnum; import ca.uhn.fhir.model.dstu2.valueset.MaritalStatusCodesEnum;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.TestUtil;
public class FhirContextDstu2Test { public class FhirContextDstu2Test {
@ -23,6 +24,12 @@ public class FhirContextDstu2Test {
TestUtil.clearAllStaticFieldsForUnitTest(); TestUtil.clearAllStaticFieldsForUnitTest();
} }
@Test(expected=DataFormatException.class)
public void testScanInvalid() {
FhirContext ctx = FhirContext.forDstu2();
ctx.getResourceDefinition("InvalidResource");
}
@Test @Test
public void testQueryBoundCode() { public void testQueryBoundCode() {

View File

@ -19,6 +19,7 @@ import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@ -48,15 +49,16 @@ import org.mockito.stubbing.Answer;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.ExtensionDt; import ca.uhn.fhir.model.api.ExtensionDt;
import ca.uhn.fhir.model.api.IQueryParameterType;
import ca.uhn.fhir.model.api.Include; import ca.uhn.fhir.model.api.Include;
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum; import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
import ca.uhn.fhir.model.dstu2.composite.IdentifierDt; import ca.uhn.fhir.model.dstu2.composite.IdentifierDt;
import ca.uhn.fhir.model.dstu2.composite.MetaDt; import ca.uhn.fhir.model.dstu2.composite.MetaDt;
import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry; import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
import ca.uhn.fhir.model.dstu2.resource.Bundle.Link; import ca.uhn.fhir.model.dstu2.resource.Bundle.Link;
import ca.uhn.fhir.model.dstu2.resource.Conformance;
import ca.uhn.fhir.model.dstu2.resource.Conformance.Rest; import ca.uhn.fhir.model.dstu2.resource.Conformance.Rest;
import ca.uhn.fhir.model.dstu2.resource.Conformance.RestSecurity; import ca.uhn.fhir.model.dstu2.resource.Conformance.RestSecurity;
import ca.uhn.fhir.model.dstu2.resource.Conformance;
import ca.uhn.fhir.model.dstu2.resource.Encounter; import ca.uhn.fhir.model.dstu2.resource.Encounter;
import ca.uhn.fhir.model.dstu2.resource.Observation; import ca.uhn.fhir.model.dstu2.resource.Observation;
import ca.uhn.fhir.model.dstu2.resource.OperationOutcome; import ca.uhn.fhir.model.dstu2.resource.OperationOutcome;
@ -68,6 +70,7 @@ import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.model.primitive.InstantDt; import ca.uhn.fhir.model.primitive.InstantDt;
import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.model.primitive.UriDt; import ca.uhn.fhir.model.primitive.UriDt;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.parser.XmlParserDstu2Test; import ca.uhn.fhir.parser.XmlParserDstu2Test;
import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.api.MethodOutcome;
@ -77,6 +80,7 @@ import ca.uhn.fhir.rest.client.apache.ApacheRestfulClientFactory;
import ca.uhn.fhir.rest.client.exceptions.InvalidResponseException; import ca.uhn.fhir.rest.client.exceptions.InvalidResponseException;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.rest.method.SearchStyleEnum; import ca.uhn.fhir.rest.method.SearchStyleEnum;
import ca.uhn.fhir.rest.param.DateParam;
import ca.uhn.fhir.rest.param.DateRangeParam; import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.Constants;
import ca.uhn.fhir.rest.server.EncodingEnum; import ca.uhn.fhir.rest.server.EncodingEnum;
@ -97,6 +101,23 @@ public class GenericClientDstu2Test {
TestUtil.clearAllStaticFieldsForUnitTest(); TestUtil.clearAllStaticFieldsForUnitTest();
} }
@Test
public void testReadForUnknownType() throws Exception {
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
try {
client.read(new UriDt("1"));
fail();
} catch (IllegalArgumentException e) {
assertEquals("The given URI is not an absolute URL and is not usable for this operation: 1", e.getMessage());
}
try {
client.read(new UriDt("http://example.com/InvalidResource/1"));
fail();
} catch (IllegalArgumentException e) {
assertEquals("Unable to determine the resource type from the given URI: ?aaaa", e.getMessage());
}
}
@Before @Before
public void before() { public void before() {
@ -113,7 +134,6 @@ public class GenericClientDstu2Test {
return body; return body;
} }
private String getPatientFeedWithOneResult() { private String getPatientFeedWithOneResult() {
//@formatter:off //@formatter:off
String msg = "<Bundle xmlns=\"http://hl7.org/fhir\">\n" + String msg = "<Bundle xmlns=\"http://hl7.org/fhir\">\n" +
@ -196,10 +216,10 @@ public class GenericClientDstu2Test {
IGenericClient client = ourCtx.newRestfulGenericClient("http://localhost:8080/fhir"); IGenericClient client = ourCtx.newRestfulGenericClient("http://localhost:8080/fhir");
Conformance conf = client.fetchConformance().ofType(Conformance.class).execute(); Conformance conf = client.fetchConformance().ofType(Conformance.class).execute();
Rest rest = conf.getRest().get(0); Rest rest = conf.getRest().get(0);
RestSecurity security = rest.getSecurity(); RestSecurity security = rest.getSecurity();
List<ExtensionDt> ext = security.getUndeclaredExtensionsByUrl("http://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris"); List<ExtensionDt> ext = security.getUndeclaredExtensionsByUrl("http://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris");
List<ExtensionDt> tokenExts = ext.get(0).getUndeclaredExtensionsByUrl("token"); List<ExtensionDt> tokenExts = ext.get(0).getUndeclaredExtensionsByUrl("token");
ExtensionDt tokenExt = tokenExts.get(0); ExtensionDt tokenExt = tokenExts.get(0);
@ -208,8 +228,6 @@ public class GenericClientDstu2Test {
} }
/** /**
* See #322 * See #322
*/ */
@ -229,10 +247,10 @@ public class GenericClientDstu2Test {
IGenericClient client = ourCtx.newRestfulGenericClient("http://localhost:8080/fhir"); IGenericClient client = ourCtx.newRestfulGenericClient("http://localhost:8080/fhir");
Conformance conf = client.fetchConformance().ofType(Conformance.class).execute(); Conformance conf = client.fetchConformance().ofType(Conformance.class).execute();
Rest rest = conf.getRest().get(0); Rest rest = conf.getRest().get(0);
RestSecurity security = rest.getSecurity(); RestSecurity security = rest.getSecurity();
List<ExtensionDt> ext = security.getUndeclaredExtensionsByUrl("http://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris"); List<ExtensionDt> ext = security.getUndeclaredExtensionsByUrl("http://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris");
List<ExtensionDt> tokenExts = ext.get(0).getUndeclaredExtensionsByUrl("token"); List<ExtensionDt> tokenExts = ext.get(0).getUndeclaredExtensionsByUrl("token");
ExtensionDt tokenExt = tokenExts.get(0); ExtensionDt tokenExt = tokenExts.get(0);
@ -604,6 +622,73 @@ public class GenericClientDstu2Test {
} }
@Test
public void testDeleteByResource() 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), Constants.STATUS_HTTP_204_NO_CONTENT, ""));
when(myHttpResponse.getEntity().getContent()).then(new Answer<ReaderInputStream>() {
@Override
public ReaderInputStream answer(InvocationOnMock theInvocation) throws Throwable {
return new ReaderInputStream(new StringReader(""), Charset.forName("UTF-8"));
}
});
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
int idx = 0;
Patient pat = new Patient();
pat.setId("Patient/123");
client.delete().resource(pat).execute();
assertEquals("DELETE", capt.getAllValues().get(idx).getMethod());
assertEquals("http://example.com/fhir/Patient/123", capt.getAllValues().get(idx).getURI().toString());
}
@Test
public void testDeleteInvalidRequest() throws Exception {
Patient pat = new Patient();
pat.setId("123");
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
try {
client.delete().resource(pat).execute();
fail();
} catch (IllegalArgumentException e){
assertEquals("theResource.getId() must contain a resource type and logical ID at a minimum (e.g. Patient/1234), found: 123", e.getMessage());
}
try {
client.delete().resourceById(new IdDt("123")).execute();
fail();
} catch (IllegalArgumentException e){
assertEquals("theId must contain a resource type and logical ID at a minimum (e.g. Patient/1234)found: 123", e.getMessage());
}
try {
client.delete().resourceById("", "123").execute();
fail();
} catch (IllegalArgumentException e){
assertEquals("theResourceType can not be blank/null", e.getMessage());
}
try {
client.delete().resourceById("Patient", "").execute();
fail();
} catch (IllegalArgumentException e){
assertEquals("theLogicalId can not be blank/null", e.getMessage());
}
try {
client.delete().resourceConditionalByType("InvalidType");
fail();
} catch (DataFormatException e){
assertEquals("Unknown resource name \"InvalidType\" (this name is not known in FHIR version \"DSTU2\")", e.getMessage());
}
}
@Test @Test
public void testHistory() throws Exception { public void testHistory() throws Exception {
@ -1504,10 +1589,10 @@ public class GenericClientDstu2Test {
} }
@Test @Test
public void testProviderWhereWeForgotToSetTheContext() throws Exception { public void testProviderWhereWeForgotToSetTheContext() throws Exception {
ApacheRestfulClientFactory clientFactory = new ApacheRestfulClientFactory(); // no ctx ApacheRestfulClientFactory clientFactory = new ApacheRestfulClientFactory(); // no ctx
clientFactory.setServerValidationMode(ServerValidationModeEnum.NEVER); clientFactory.setServerValidationMode(ServerValidationModeEnum.NEVER);
ourCtx.setRestfulClientFactory(clientFactory); ourCtx.setRestfulClientFactory(clientFactory);
try { try {
@ -1729,7 +1814,7 @@ public class GenericClientDstu2Test {
ourLog.info(Arrays.asList(capt.getValue().getAllHeaders()).toString()); ourLog.info(Arrays.asList(capt.getValue().getAllHeaders()).toString());
ourLog.info(capt.getValue().toString()); ourLog.info(capt.getValue().toString());
HttpEntityEnclosingRequestBase v = (HttpEntityEnclosingRequestBase) capt.getValue(); HttpEntityEnclosingRequestBase v = (HttpEntityEnclosingRequestBase) capt.getValue();
String req = IOUtils.toString(v.getEntity().getContent(), "UTF-8"); String req = IOUtils.toString(v.getEntity().getContent(), "UTF-8");
assertEquals("name=james", req); assertEquals("name=james", req);
@ -1772,7 +1857,7 @@ public class GenericClientDstu2Test {
ourLog.info(Arrays.asList(capt.getValue().getAllHeaders()).toString()); ourLog.info(Arrays.asList(capt.getValue().getAllHeaders()).toString());
ourLog.info(capt.getValue().toString()); ourLog.info(capt.getValue().toString());
HttpEntityEnclosingRequestBase v = (HttpEntityEnclosingRequestBase) capt.getValue(); HttpEntityEnclosingRequestBase v = (HttpEntityEnclosingRequestBase) capt.getValue();
String req = IOUtils.toString(v.getEntity().getContent(), "UTF-8"); String req = IOUtils.toString(v.getEntity().getContent(), "UTF-8");
assertEquals("name=james", req); assertEquals("name=james", req);
@ -1817,11 +1902,12 @@ public class GenericClientDstu2Test {
@Override @Override
public InputStream answer(InvocationOnMock theInvocation) throws Throwable { public InputStream answer(InvocationOnMock theInvocation) throws Throwable {
return new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8")); return new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8"));
}}); }
});
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir"); IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
int idx = 0; int idx = 0;
//@formatter:off //@formatter:off
client.search() client.search()
.forResource("Encounter") .forResource("Encounter")
@ -2035,6 +2121,27 @@ public class GenericClientDstu2Test {
} }
@Test
public void testSearchWithMap() 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");
HashMap<String, List<IQueryParameterType>> params = new HashMap<String, List<IQueryParameterType>>();
params.put("foo", Arrays.asList((IQueryParameterType)new DateParam("2001")));
Bundle response = client.search(Patient.class, params);
assertEquals("http://example.com/fhir/Patient?foo=2001", capt.getValue().getURI().toString());
assertEquals(Patient.class, response.getEntries().get(0).getResource().getClass());
}
@Test @Test
public void testSearchWithProfileAndSecurity() throws Exception { public void testSearchWithProfileAndSecurity() 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\"}]}}]}"; String msg = "{\"resourceType\":\"Bundle\",\"id\":null,\"base\":\"http://localhost:57931/fhir/contextDev\",\"total\":1,\"link\":[{\"relation\":\"self\",\"url\":\"http://localhost:57931/fhir/contextDev/Patient?identifier=urn%3AMultiFhirVersionTest%7CtestSubmitPatient01&_format=json\"}],\"entry\":[{\"resource\":{\"resourceType\":\"Patient\",\"id\":\"1\",\"meta\":{\"versionId\":\"1\",\"lastUpdated\":\"2014-12-20T18:41:29.706-05:00\"},\"identifier\":[{\"system\":\"urn:MultiFhirVersionTest\",\"value\":\"testSubmitPatient01\"}]}}]}";

View File

@ -24,7 +24,7 @@ public class FhirContextDstu3Test {
public static void afterClassClearContext() { public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest(); TestUtil.clearAllStaticFieldsForUnitTest();
} }
/** /**
* See #344 * See #344
*/ */

View File

@ -1,13 +1,13 @@
package ca.uhn.fhir.rest.client; package ca.uhn.fhir.rest.client;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -22,7 +22,6 @@ import java.util.List;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream; import org.apache.commons.io.input.ReaderInputStream;
import org.apache.commons.lang3.time.FastDateParser;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion; import org.apache.http.ProtocolVersion;
@ -56,6 +55,7 @@ import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
import ca.uhn.fhir.model.dstu.valueset.QuantityCompararatorEnum; import ca.uhn.fhir.model.dstu.valueset.QuantityCompararatorEnum;
import ca.uhn.fhir.model.primitive.DateTimeDt; import ca.uhn.fhir.model.primitive.DateTimeDt;
import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.model.primitive.UriDt;
import ca.uhn.fhir.parser.CustomTypeDstu3Test; import ca.uhn.fhir.parser.CustomTypeDstu3Test;
import ca.uhn.fhir.parser.CustomTypeDstu3Test.MyCustomPatient; import ca.uhn.fhir.parser.CustomTypeDstu3Test.MyCustomPatient;
import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.parser.IParser;
@ -97,6 +97,18 @@ public class GenericClientDstu3Test {
return body; return body;
} }
@SuppressWarnings("deprecation")
@Test
public void testInvalidConformanceCall() {
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
try {
client.conformance();
fail();
} catch (IllegalArgumentException e) {
assertEquals("Must call fetchConformance() instead of conformance() for RI/STU3+ structures", e.getMessage());
}
}
@Test @Test
public void testBinaryCreateWithFhirContentType() throws Exception { public void testBinaryCreateWithFhirContentType() throws Exception {
IParser p = ourCtx.newXmlParser(); IParser p = ourCtx.newXmlParser();
@ -588,6 +600,19 @@ public class GenericClientDstu3Test {
assertEquals("http://example.com/fhir/Patient/222", capt.getAllValues().get(0).getURI().toASCIIString()); assertEquals("http://example.com/fhir/Patient/222", capt.getAllValues().get(0).getURI().toASCIIString());
} }
@Test
public void testSearchForUnknownType() throws Exception {
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
try {
client.search(new UriDt("?aaaa"));
fail();
} catch (IllegalArgumentException e) {
assertEquals("Unable to determine the resource type from the given URI: ?aaaa", e.getMessage());
}
}
@Test @Test
public void testUserAgentForBinary() throws Exception { public void testUserAgentForBinary() throws Exception {
IParser p = ourCtx.newXmlParser(); IParser p = ourCtx.newXmlParser();
@ -705,6 +730,54 @@ public class GenericClientDstu3Test {
validateUserAgent(capt); validateUserAgent(capt);
} }
@Test
public void testForceConformance() throws Exception {
final IParser p = ourCtx.newXmlParser();
final Conformance conf = new Conformance();
conf.setCopyright("COPY");
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"));
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_XML + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).thenAnswer(new Answer<ReaderInputStream>() {
private int myCount = 0;
@Override
public ReaderInputStream answer(InvocationOnMock theInvocation) throws Throwable {
final String respString;
if (myCount == 1 || myCount == 2) {
ourLog.info("Encoding patient");
respString = p.encodeResourceToString(patient);
} else {
ourLog.info("Encoding conformance");
respString = p.encodeResourceToString(conf);
}
myCount++;
return new ReaderInputStream(new StringReader(respString), Charset.forName("UTF-8"));
}
});
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.ONCE);
IGenericClient client = ourCtx.newRestfulGenericClient("http://testForceConformance.com/fhir");
client.read().resource("Patient").withId("1").execute();
assertEquals(2, capt.getAllValues().size());
assertEquals("http://testForceConformance.com/fhir/metadata", capt.getAllValues().get(0).getURI().toASCIIString());
assertEquals("http://testForceConformance.com/fhir/Patient/1", capt.getAllValues().get(1).getURI().toASCIIString());
client.read().resource("Patient").withId("1").execute();
assertEquals(3, capt.getAllValues().size());
assertEquals("http://testForceConformance.com/fhir/Patient/1", capt.getAllValues().get(2).getURI().toASCIIString());
client.forceConformanceCheck();
assertEquals(4, capt.getAllValues().size());
assertEquals("http://testForceConformance.com/fhir/metadata", capt.getAllValues().get(3).getURI().toASCIIString());
}
private List<Class<? extends IBaseResource>> toTypeList(Class<? extends IBaseResource> theClass) { private List<Class<? extends IBaseResource>> toTypeList(Class<? extends IBaseResource> theClass) {
ArrayList<Class<? extends IBaseResource>> retVal = new ArrayList<Class<? extends IBaseResource>>(); ArrayList<Class<? extends IBaseResource>> retVal = new ArrayList<Class<? extends IBaseResource>>();
retVal.add(theClass); retVal.add(theClass);