From c838d651dd9168f95e1278834896c6d9c4292e84 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Thu, 6 Aug 2015 18:06:32 -0400 Subject: [PATCH] Fix fluent client search call so that it can be mocked with Mockito --- hapi-fhir-android/pom.xml | 17 ++++++- .../ca/uhn/fhir/rest/gclient/IBaseQuery.java | 2 +- .../java/ca/uhn/fhir/rest/gclient/IQuery.java | 16 ++++++- .../fhir/rest/client/ClientMockingTest.java | 46 +++++++++++++++++++ 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/ClientMockingTest.java diff --git a/hapi-fhir-android/pom.xml b/hapi-fhir-android/pom.xml index c0cb2ff1195..9a229bfe59d 100644 --- a/hapi-fhir-android/pom.xml +++ b/hapi-fhir-android/pom.xml @@ -78,6 +78,11 @@ ${commons_io_version} test + + commons-codec + commons-codec + ${commons_codec_version} + @@ -153,17 +158,21 @@ dstu + ca.uhn.hapi.fhir:hapi-fhir-base ca.uhn.hapi.fhir:hapi-fhir-structures-dstu org.glassfish:javax.json org.codehaus.woodstox:woodstox-core-asl javax.xml.stream:stax-api - javax.servlet:javax.servlet-api + org.codehaus.woodstox:stax2-api + org.glassfish:javax.json @@ -201,16 +210,20 @@ dstu2 + ca.uhn.hapi.fhir:hapi-fhir-base ca.uhn.hapi.fhir:hapi-fhir-structures-dstu2 org.glassfish:javax.json org.codehaus.woodstox:woodstox-core-asl javax.xml.stream:stax-api - javax.servlet:javax.servlet-api + org.codehaus.woodstox:stax2-api + org.glassfish:javax.json diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IBaseQuery.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IBaseQuery.java index b4ba4c80551..1e1013c464a 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IBaseQuery.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IBaseQuery.java @@ -20,7 +20,7 @@ package ca.uhn.fhir.rest.gclient; * #L% */ -public interface IBaseQuery { +public interface IBaseQuery> { T where(ICriterion theCriterion); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IQuery.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IQuery.java index 862e02b7363..78b23878915 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IQuery.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IQuery.java @@ -75,5 +75,19 @@ public interface IQuery extends IClientExecutable, T>, IBaseQueryca.uhn.fhir.model.dstu2.resource.Bundle.class */ IQuery returnBundle(Class theClass); - + + /** + * {@inheritDoc} + */ + // This is here as an overridden method to allow mocking clients with Mockito to work + @Override + IQuery where(ICriterion theCriterion); + + /** + * {@inheritDoc} + */ + // This is here as an overridden method to allow mocking clients with Mockito to work + @Override + IQuery and(ICriterion theCriterion); + } diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/ClientMockingTest.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/ClientMockingTest.java new file mode 100644 index 00000000000..7c8110d3bf2 --- /dev/null +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/ClientMockingTest.java @@ -0,0 +1,46 @@ +package ca.uhn.fhir.rest.client; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import org.hl7.fhir.instance.model.api.IBaseBundle; +import org.junit.Test; +import org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs; +import org.mockito.internal.stubbing.defaultanswers.ReturnsSmartNulls; + +import ca.uhn.fhir.model.dstu2.resource.Bundle; +import ca.uhn.fhir.model.dstu2.resource.Condition; +import ca.uhn.fhir.rest.gclient.ICriterion; +import ca.uhn.fhir.rest.gclient.IQuery; + +public class ClientMockingTest { + + @SuppressWarnings("unchecked") + @Test + public void testMockingDeepStubs() { + + IGenericClient client = mock(IGenericClient.class, new ReturnsDeepStubs()); + + // System.out.println(stub.getClass()); + // System.out.println(stub.getClass()); + + Bundle retVal = new Bundle(); + + //@formatter:off + when((Object)client + .search() + .forResource(eq(Condition.class)) + .where(any(ICriterion.class)) + .returnBundle((Class)any()) + .execute()) + .thenReturn(retVal); + //@formatter:off + + Bundle actual = client.search().forResource(Condition.class).where(Condition.ASSERTER.hasId("123")).returnBundle(Bundle.class).execute(); + assertSame(retVal, actual); + + } + + + +}