Fix an occasional NPE in the tests

This commit is contained in:
James Agnew 2018-11-19 09:56:40 -05:00
parent 8ec1c1a011
commit 2e1d5e4124
3 changed files with 57 additions and 54 deletions

View File

@ -348,9 +348,11 @@ public abstract class BaseClient implements IRestfulClient {
InputStream inputStreamToReturn = inputStream;
if (ourLog.isTraceEnabled() || myKeepResponses || theLogRequestAndResponse) {
String responseString = IOUtils.toString(inputStream, Charsets.UTF_8);
keepResponseAndLogIt(theLogRequestAndResponse, response, responseString);
inputStreamToReturn = new ByteArrayInputStream(responseString.getBytes(Charsets.UTF_8));
if (inputStream != null) {
String responseString = IOUtils.toString(inputStream, Charsets.UTF_8);
keepResponseAndLogIt(theLogRequestAndResponse, response, responseString);
inputStreamToReturn = new ByteArrayInputStream(responseString.getBytes(Charsets.UTF_8));
}
}
return binding.invokeClient(mimeType, inputStreamToReturn, response.getStatus(), headers);

View File

@ -1,10 +1,18 @@
package ca.uhn.fhir.rest.server;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.TimeUnit;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.annotation.ConditionalUrlParam;
import ca.uhn.fhir.rest.annotation.Delete;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.rest.gclient.IDelete;
import ca.uhn.fhir.rest.gclient.IDeleteWithQuery;
import ca.uhn.fhir.rest.gclient.IDeleteWithQueryTyped;
import ca.uhn.fhir.util.PortUtil;
import ca.uhn.fhir.util.TestUtil;
import org.apache.commons.lang3.Validate;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
@ -14,27 +22,28 @@ import org.eclipse.jetty.servlet.ServletHolder;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.Patient;
import org.junit.*;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.annotation.*;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.util.PortUtil;
import ca.uhn.fhir.util.TestUtil;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class DeleteConditionalR4Test {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(DeleteConditionalR4Test.class);
private static CloseableHttpClient ourClient;
private static FhirContext ourCtx = FhirContext.forR4();
private static IGenericClient ourHapiClient;
private static String ourLastConditionalUrl;
private static IdType ourLastIdParam;
private static boolean ourLastRequestWasDelete;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(DeleteConditionalR4Test.class);
private static int ourPort;
private static Server ourServer;
@Before
public void before() {
@ -44,29 +53,16 @@ public class DeleteConditionalR4Test {
}
@Test
public void testSearchStillWorks() throws Exception {
public void testSearchStillWorks() {
Patient patient = new Patient();
patient.addIdentifier().setValue("002");
// HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient?_pretty=true");
//
// HttpResponse status = ourClient.execute(httpGet);
//
// String responseContent = IOUtils.toString(status.getEntity().getContent());
// IOUtils.closeQuietly(status.getEntity().getContent());
//
// ourLog.info("Response was:\n{}", responseContent);
//@formatter:off
ourHapiClient
.delete()
.resourceConditionalByType(Patient.class)
.where(Patient.IDENTIFIER.exactly().systemAndIdentifier("SOMESYS","SOMEID"))
.execute();
//@formatter:on
.where(Patient.IDENTIFIER.exactly().systemAndIdentifier("SOMESYS", "SOMEID")).execute();
assertTrue(ourLastRequestWasDelete);
assertEquals(null, ourLastIdParam);
@ -74,15 +70,29 @@ public class DeleteConditionalR4Test {
}
public static class PatientProvider implements IResourceProvider {
@Delete()
public MethodOutcome deletePatient(@IdParam IdType theIdParam, @ConditionalUrlParam String theConditional) {
ourLastRequestWasDelete = true;
ourLastConditionalUrl = theConditional;
ourLastIdParam = theIdParam;
return new MethodOutcome(new IdType("Patient/001/_history/002"));
}
@Override
public Class<? extends IBaseResource> getResourceType() {
return Patient.class;
}
}
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
@ -107,22 +117,5 @@ public class DeleteConditionalR4Test {
ourHapiClient = ourCtx.newRestfulGenericClient("http://localhost:" + ourPort + "/");
ourHapiClient.registerInterceptor(new LoggingInterceptor());
}
public static class PatientProvider implements IResourceProvider {
@Delete()
public MethodOutcome deletePatient(@IdParam IdType theIdParam, @ConditionalUrlParam String theConditional) {
ourLastRequestWasDelete = true;
ourLastConditionalUrl = theConditional;
ourLastIdParam = theIdParam;
return new MethodOutcome(new IdType("Patient/001/_history/002"));
}
@Override
public Class<? extends IBaseResource> getResourceType() {
return Patient.class;
}
}
}

View File

@ -3,6 +3,7 @@ package ca.uhn.fhir.rest.server.provider;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
import ca.uhn.fhir.rest.gclient.IDeleteTyped;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.RestfulServer;
import ca.uhn.fhir.rest.server.exceptions.ResourceGoneException;
@ -89,7 +90,14 @@ public class HashMapResourceProviderTest {
assertEquals(0, myPatientResourceProvider.getCountDelete());
ourClient.delete().resourceById(id.toUnqualifiedVersionless()).execute();
IDeleteTyped iDeleteTyped = ourClient.delete().resourceById(id.toUnqualifiedVersionless());
ourLog.info("About to execute");
try {
iDeleteTyped.execute();
} catch (NullPointerException e) {
ourLog.error("NPE", e);
fail(e.toString());
}
assertEquals(1, myPatientResourceProvider.getCountDelete());