Add test and credit for #518

This commit is contained in:
James Agnew 2016-11-25 18:28:18 -05:00
parent fc55c2cae8
commit a9d7b8d636
4 changed files with 162 additions and 0 deletions

View File

@ -0,0 +1,75 @@
package ca.uhn.fhir.rest.client;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Map;
import org.junit.AfterClass;
import org.junit.Test;
import org.mockito.Mockito;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.model.dstu2.resource.Conformance;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.rest.api.RequestTypeEnum;
import ca.uhn.fhir.rest.client.apache.ApacheRestfulClientFactory;
import ca.uhn.fhir.rest.client.api.IHttpClient;
import ca.uhn.fhir.rest.client.api.IHttpRequest;
import ca.uhn.fhir.rest.client.api.IHttpResponse;
import ca.uhn.fhir.rest.server.EncodingEnum;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import ca.uhn.fhir.util.TestUtil;
public class RestfulClientFactoryDstu2Test {
private static FhirContext ourCtx = FhirContext.forDstu2();
/**
* See #518
*/
@SuppressWarnings({ "unchecked", "cast" })
@Test
public void testMissingCapabilityStatementDstu2() throws Exception {
FhirContext ctx = mock(FhirContext.class);
IRestfulClientFactory restfulClientFactory = mock(IRestfulClientFactory.class);
IHttpClient httpClient = mock(IHttpClient.class);
when(ctx.getResourceDefinition(Mockito.eq("CapabilityStatement"))).thenThrow(new DataFormatException());
when(ctx.getResourceDefinition(Mockito.eq("Conformance"))).thenReturn(ourCtx.getResourceDefinition("Conformance"));
when(ctx.getResourceDefinition(Conformance.class)).thenReturn(ourCtx.getResourceDefinition("Conformance"));
when(ctx.getVersion()).thenReturn(FhirVersionEnum.DSTU2.getVersionImplementation());
when(ctx.getRestfulClientFactory()).thenReturn(restfulClientFactory);
when(restfulClientFactory.getHttpClient(any(StringBuilder.class), (Map<String, List<String>>)any(Map.class), any(String.class), any(RequestTypeEnum.class), any(List.class))).thenReturn(httpClient);
IHttpRequest httpRequest = mock(IHttpRequest.class);
when(httpClient.createGetRequest(any(FhirContext.class), any(EncodingEnum.class))).thenReturn(httpRequest);
IHttpResponse httpResponse = mock(IHttpResponse.class);
when(httpRequest.execute()).thenReturn(httpResponse);
when(httpResponse.getStatus()).thenReturn(404);
ApacheRestfulClientFactory cf = new ApacheRestfulClientFactory(ctx);
IHttpClient client = mock(IHttpClient.class);
BaseClient baseClient = mock(BaseClient.class);
try {
cf.validateServerBase("http://localhost:9999", client, baseClient);
fail();
} catch (ResourceNotFoundException e) {
// ok
}
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -0,0 +1,76 @@
package ca.uhn.fhir.rest.client;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Map;
import org.hl7.fhir.dstu3.model.Conformance;
import org.junit.AfterClass;
import org.junit.Test;
import org.mockito.Mockito;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.rest.api.RequestTypeEnum;
import ca.uhn.fhir.rest.client.apache.ApacheRestfulClientFactory;
import ca.uhn.fhir.rest.client.api.IHttpClient;
import ca.uhn.fhir.rest.client.api.IHttpRequest;
import ca.uhn.fhir.rest.client.api.IHttpResponse;
import ca.uhn.fhir.rest.server.EncodingEnum;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import ca.uhn.fhir.util.TestUtil;
@SuppressWarnings("deprecation")
public class RestfulClientFactoryTest {
private static FhirContext ourCtx = FhirContext.forDstu3();
/**
* See #518
*/
@SuppressWarnings({ "unchecked", "cast" })
@Test
public void testMissingCapabilityStatement() throws Exception {
FhirContext ctx = mock(FhirContext.class);
IRestfulClientFactory restfulClientFactory = mock(IRestfulClientFactory.class);
IHttpClient httpClient = mock(IHttpClient.class);
when(ctx.getResourceDefinition(Mockito.eq("CapabilityStatement"))).thenThrow(new DataFormatException());
when(ctx.getResourceDefinition(Mockito.eq("Conformance"))).thenReturn(ourCtx.getResourceDefinition("Conformance"));
when(ctx.getResourceDefinition(Conformance.class)).thenReturn(ourCtx.getResourceDefinition("Conformance"));
when(ctx.getVersion()).thenReturn(FhirVersionEnum.DSTU3.getVersionImplementation());
when(ctx.getRestfulClientFactory()).thenReturn(restfulClientFactory);
when(restfulClientFactory.getHttpClient(any(StringBuilder.class), (Map<String, List<String>>)any(Map.class), any(String.class), any(RequestTypeEnum.class), any(List.class))).thenReturn(httpClient);
IHttpRequest httpRequest = mock(IHttpRequest.class);
when(httpClient.createGetRequest(any(FhirContext.class), any(EncodingEnum.class))).thenReturn(httpRequest);
IHttpResponse httpResponse = mock(IHttpResponse.class);
when(httpRequest.execute()).thenReturn(httpResponse);
when(httpResponse.getStatus()).thenReturn(404);
ApacheRestfulClientFactory cf = new ApacheRestfulClientFactory(ctx);
IHttpClient client = mock(IHttpClient.class);
BaseClient baseClient = mock(BaseClient.class);
try {
cf.validateServerBase("http://localhost:9999", client, baseClient);
fail();
} catch (ResourceNotFoundException e) {
// ok
}
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
}

View File

@ -274,6 +274,11 @@
<id>vadi2</id> <id>vadi2</id>
<name>Vadim Peretokin</name> <name>Vadim Peretokin</name>
</developer> </developer>
<developer>
<id>lawley</id>
<name>Michael Lawley</name>
<organization>CSIRO</organization>
</developer>
</developers> </developers>
<licenses> <licenses>

View File

@ -55,6 +55,12 @@
and Stefan Evinance for reporting and providing and Stefan Evinance for reporting and providing
a test case! a test case!
</action> </action>
<action type="add" issue="518">
Allow client to gracefully handle running in DSTU3 mode
but with a structures JAR that does not contain a
CapabilityStatement resource. Thanks to Michael Lawley
for the pull request!
</action>
</release> </release>
<release version="2.1" date="2016-11-11"> <release version="2.1" date="2016-11-11">
<action type="add"> <action type="add">