Still more test tweaks
This commit is contained in:
parent
3b101dfe1f
commit
44ec091b35
|
@ -2,6 +2,8 @@ package ca.uhn.fhir.jpa.dao.dstu3;
|
||||||
|
|
||||||
import ca.uhn.fhir.jpa.dao.DaoConfig;
|
import ca.uhn.fhir.jpa.dao.DaoConfig;
|
||||||
import ca.uhn.fhir.jpa.dao.SearchParameterMap;
|
import ca.uhn.fhir.jpa.dao.SearchParameterMap;
|
||||||
|
import ca.uhn.fhir.jpa.dao.data.ISearchDao;
|
||||||
|
import ca.uhn.fhir.jpa.dao.r4.FhirResourceDaoR4SearchPageExpiryTest;
|
||||||
import ca.uhn.fhir.jpa.entity.Search;
|
import ca.uhn.fhir.jpa.entity.Search;
|
||||||
import ca.uhn.fhir.jpa.search.StaleSearchDeletingSvcImpl;
|
import ca.uhn.fhir.jpa.search.StaleSearchDeletingSvcImpl;
|
||||||
import ca.uhn.fhir.jpa.util.StopWatch;
|
import ca.uhn.fhir.jpa.util.StopWatch;
|
||||||
|
@ -82,6 +84,8 @@ public class FhirResourceDaoDstu3SearchPageExpiryTest extends BaseJpaDstu3Test {
|
||||||
Validate.notBlank(searchUuid1);
|
Validate.notBlank(searchUuid1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
waitForSearchToSave(searchUuid1);
|
||||||
|
|
||||||
final String searchUuid2;
|
final String searchUuid2;
|
||||||
{
|
{
|
||||||
SearchParameterMap params = new SearchParameterMap();
|
SearchParameterMap params = new SearchParameterMap();
|
||||||
|
@ -377,4 +381,10 @@ public class FhirResourceDaoDstu3SearchPageExpiryTest extends BaseJpaDstu3Test {
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void waitForSearchToSave(final String theUuid) {
|
||||||
|
final ISearchDao searchEntityDao = mySearchEntityDao;
|
||||||
|
TransactionTemplate txTemplate = newTxTemplate();
|
||||||
|
FhirResourceDaoR4SearchPageExpiryTest.waitForSearchToSave(theUuid, searchEntityDao, txTemplate);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package ca.uhn.fhir.jpa.dao.r4;
|
package ca.uhn.fhir.jpa.dao.r4;
|
||||||
|
|
||||||
import ca.uhn.fhir.jpa.dao.SearchParameterMap;
|
import ca.uhn.fhir.jpa.dao.SearchParameterMap;
|
||||||
|
import ca.uhn.fhir.jpa.dao.data.ISearchDao;
|
||||||
import ca.uhn.fhir.jpa.entity.Search;
|
import ca.uhn.fhir.jpa.entity.Search;
|
||||||
import ca.uhn.fhir.jpa.search.StaleSearchDeletingSvcImpl;
|
import ca.uhn.fhir.jpa.search.StaleSearchDeletingSvcImpl;
|
||||||
import ca.uhn.fhir.jpa.util.StopWatch;
|
import ca.uhn.fhir.jpa.util.StopWatch;
|
||||||
|
@ -174,19 +175,7 @@ public class FhirResourceDaoR4SearchPageExpiryTest extends BaseJpaR4Test {
|
||||||
params.setCount(1);
|
params.setCount(1);
|
||||||
final IBundleProvider bundleProvider = myPatientDao.search(params);
|
final IBundleProvider bundleProvider = myPatientDao.search(params);
|
||||||
|
|
||||||
newTxTemplate().execute(new TransactionCallbackWithoutResult() {
|
waitForSearchToSave(bundleProvider.getUuid());
|
||||||
@Override
|
|
||||||
protected void doInTransactionWithoutResult(TransactionStatus theArg0) {
|
|
||||||
Search search = null;
|
|
||||||
for (int i = 0; i < 20 && search == null; i++) {
|
|
||||||
search = mySearchEntityDao.findByUuid(bundleProvider.getUuid());
|
|
||||||
if (search == null) {
|
|
||||||
sleepAtLeast(100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assertNotNull(search);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
myDaoConfig.setExpireSearchResults(false);
|
myDaoConfig.setExpireSearchResults(false);
|
||||||
StaleSearchDeletingSvcImpl.setNowForUnitTests(System.currentTimeMillis() + DateUtils.MILLIS_PER_DAY);
|
StaleSearchDeletingSvcImpl.setNowForUnitTests(System.currentTimeMillis() + DateUtils.MILLIS_PER_DAY);
|
||||||
|
@ -213,6 +202,28 @@ public class FhirResourceDaoR4SearchPageExpiryTest extends BaseJpaR4Test {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void waitForSearchToSave(final String theUuid) {
|
||||||
|
final ISearchDao searchEntityDao = mySearchEntityDao;
|
||||||
|
TransactionTemplate txTemplate = newTxTemplate();
|
||||||
|
FhirResourceDaoR4SearchPageExpiryTest.waitForSearchToSave(theUuid, searchEntityDao, txTemplate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void waitForSearchToSave(final String theUuid, final ISearchDao theSearchEntityDao, TransactionTemplate theTxTemplate) {
|
||||||
|
theTxTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||||
|
@Override
|
||||||
|
protected void doInTransactionWithoutResult(TransactionStatus theArg0) {
|
||||||
|
Search search = null;
|
||||||
|
for (int i = 0; i < 20 && search == null; i++) {
|
||||||
|
search = theSearchEntityDao.findByUuid(theUuid);
|
||||||
|
if (search == null) {
|
||||||
|
sleepAtLeast(100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertNotNull(search);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExpirePagesAfterSingleUse2() throws Exception {
|
public void testExpirePagesAfterSingleUse2() throws Exception {
|
||||||
IIdType pid1;
|
IIdType pid1;
|
||||||
|
@ -244,6 +255,8 @@ public class FhirResourceDaoR4SearchPageExpiryTest extends BaseJpaR4Test {
|
||||||
Validate.notBlank(searchUuid1);
|
Validate.notBlank(searchUuid1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
waitForSearchToSave(searchUuid1);
|
||||||
|
|
||||||
String searchUuid2;
|
String searchUuid2;
|
||||||
{
|
{
|
||||||
SearchParameterMap params = new SearchParameterMap();
|
SearchParameterMap params = new SearchParameterMap();
|
||||||
|
|
|
@ -0,0 +1,133 @@
|
||||||
|
package ca.uhn.fhir.rest.client;
|
||||||
|
|
||||||
|
import ca.uhn.fhir.context.FhirContext;
|
||||||
|
import ca.uhn.fhir.context.FhirVersionEnum;
|
||||||
|
import ca.uhn.fhir.rest.api.Constants;
|
||||||
|
import ca.uhn.fhir.rest.api.MethodOutcome;
|
||||||
|
import ca.uhn.fhir.rest.client.api.IGenericClient;
|
||||||
|
import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum;
|
||||||
|
import ca.uhn.fhir.util.RandomServerPortProvider;
|
||||||
|
import ca.uhn.fhir.util.TestUtil;
|
||||||
|
import ca.uhn.fhir.util.VersionUtil;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
||||||
|
import org.apache.http.client.methods.HttpUriRequest;
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
|
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||||
|
import org.eclipse.jetty.servlet.ServletHolder;
|
||||||
|
import org.hl7.fhir.r4.model.Patient;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class ClientHeadersR4Test {
|
||||||
|
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ClientHeadersR4Test.class);
|
||||||
|
private static FhirContext ourCtx;
|
||||||
|
private static Server ourServer;
|
||||||
|
private static String ourServerBase;
|
||||||
|
private static HashMap<String, List<String>> ourHeaders;
|
||||||
|
private static IGenericClient ourClient;
|
||||||
|
private static String ourMethod;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before() {
|
||||||
|
ourHeaders = null;
|
||||||
|
ourMethod = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String expectedUserAgent() {
|
||||||
|
return "HAPI-FHIR/" + VersionUtil.getVersion() + " (FHIR Client; FHIR " + FhirVersionEnum.R4.getFhirVersionString() + "/R4; apache)";
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] extractBodyAsByteArray(ArgumentCaptor<HttpUriRequest> capt) throws IOException {
|
||||||
|
byte[] body = IOUtils.toByteArray(((HttpEntityEnclosingRequestBase) capt.getAllValues().get(0)).getEntity().getContent());
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String extractBodyAsString(ArgumentCaptor<HttpUriRequest> capt) throws IOException {
|
||||||
|
String body = IOUtils.toString(((HttpEntityEnclosingRequestBase) capt.getAllValues().get(0)).getEntity().getContent(), "UTF-8");
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateWithPreferRepresentationServerReturnsResource() throws Exception {
|
||||||
|
|
||||||
|
final Patient resp1 = new Patient();
|
||||||
|
resp1.setActive(true);
|
||||||
|
|
||||||
|
MethodOutcome resp = ourClient.create().resource(resp1).execute();
|
||||||
|
|
||||||
|
assertNotNull(resp);
|
||||||
|
assertEquals(1, ourHeaders.get(Constants.HEADER_CONTENT_TYPE).size());
|
||||||
|
assertEquals("application/fhir+xml; charset=UTF-8", ourHeaders.get(Constants.HEADER_CONTENT_TYPE).get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void afterClassClearContext() {
|
||||||
|
TestUtil.clearAllStaticFieldsForUnitTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void beforeClass() throws Exception {
|
||||||
|
ourCtx = FhirContext.forR4();
|
||||||
|
|
||||||
|
int myPort = RandomServerPortProvider.findFreePort();
|
||||||
|
ourServer = new Server(myPort);
|
||||||
|
|
||||||
|
ServletContextHandler proxyHandler = new ServletContextHandler();
|
||||||
|
proxyHandler.setContextPath("/");
|
||||||
|
|
||||||
|
ourServerBase = "http://localhost:" + myPort + "/fhir/context";
|
||||||
|
ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
|
||||||
|
ourClient = ourCtx.newRestfulGenericClient(ourServerBase);
|
||||||
|
|
||||||
|
ServletHolder servletHolder = new ServletHolder();
|
||||||
|
servletHolder.setServlet(new TestServlet());
|
||||||
|
proxyHandler.addServlet(servletHolder, "/fhir/context/*");
|
||||||
|
|
||||||
|
ourServer.setHandler(proxyHandler);
|
||||||
|
ourServer.start();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class TestServlet extends HttpServlet {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
|
||||||
|
if (ourHeaders != null) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
ourHeaders = new HashMap<>();
|
||||||
|
ourMethod = req.getMethod();
|
||||||
|
Enumeration<String> names = req.getHeaderNames();
|
||||||
|
while (names.hasMoreElements()) {
|
||||||
|
String nextName = names.nextElement();
|
||||||
|
ourHeaders.put(nextName, new ArrayList<String>());
|
||||||
|
Enumeration<String> values = req.getHeaders(nextName);
|
||||||
|
while (values.hasMoreElements()) {
|
||||||
|
ourHeaders.get(nextName).add(values.nextElement());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp.setStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue