From 44ec091b3504d7d275a98aa17993bf7f1597b34c Mon Sep 17 00:00:00 2001 From: James Agnew Date: Wed, 13 Dec 2017 13:29:24 -0500 Subject: [PATCH] Still more test tweaks --- ...rResourceDaoDstu3SearchPageExpiryTest.java | 10 ++ ...FhirResourceDaoR4SearchPageExpiryTest.java | 39 +++-- .../fhir/rest/client/ClientHeadersR4Test.java | 133 ++++++++++++++++++ 3 files changed, 169 insertions(+), 13 deletions(-) create mode 100644 hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/ClientHeadersR4Test.java diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchPageExpiryTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchPageExpiryTest.java index dabf3a5ded5..6d453eb90c6 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchPageExpiryTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchPageExpiryTest.java @@ -2,6 +2,8 @@ package ca.uhn.fhir.jpa.dao.dstu3; import ca.uhn.fhir.jpa.dao.DaoConfig; 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.search.StaleSearchDeletingSvcImpl; import ca.uhn.fhir.jpa.util.StopWatch; @@ -82,6 +84,8 @@ public class FhirResourceDaoDstu3SearchPageExpiryTest extends BaseJpaDstu3Test { Validate.notBlank(searchUuid1); } + waitForSearchToSave(searchUuid1); + final String searchUuid2; { 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); + } } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchPageExpiryTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchPageExpiryTest.java index c6089d150e0..70c25ef63bb 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchPageExpiryTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoR4SearchPageExpiryTest.java @@ -1,6 +1,7 @@ package ca.uhn.fhir.jpa.dao.r4; 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.search.StaleSearchDeletingSvcImpl; import ca.uhn.fhir.jpa.util.StopWatch; @@ -174,19 +175,7 @@ public class FhirResourceDaoR4SearchPageExpiryTest extends BaseJpaR4Test { params.setCount(1); final IBundleProvider bundleProvider = myPatientDao.search(params); - newTxTemplate().execute(new TransactionCallbackWithoutResult() { - @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); - } - }); + waitForSearchToSave(bundleProvider.getUuid()); myDaoConfig.setExpireSearchResults(false); 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 public void testExpirePagesAfterSingleUse2() throws Exception { IIdType pid1; @@ -244,6 +255,8 @@ public class FhirResourceDaoR4SearchPageExpiryTest extends BaseJpaR4Test { Validate.notBlank(searchUuid1); } + waitForSearchToSave(searchUuid1); + String searchUuid2; { SearchParameterMap params = new SearchParameterMap(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/ClientHeadersR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/ClientHeadersR4Test.java new file mode 100644 index 00000000000..9d4fdf78a12 --- /dev/null +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/ClientHeadersR4Test.java @@ -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> 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 capt) throws IOException { + byte[] body = IOUtils.toByteArray(((HttpEntityEnclosingRequestBase) capt.getAllValues().get(0)).getEntity().getContent()); + return body; + } + + private String extractBodyAsString(ArgumentCaptor 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 names = req.getHeaderNames(); + while (names.hasMoreElements()) { + String nextName = names.nextElement(); + ourHeaders.put(nextName, new ArrayList()); + Enumeration values = req.getHeaders(nextName); + while (values.hasMoreElements()) { + ourHeaders.get(nextName).add(values.nextElement()); + } + } + + resp.setStatus(200); + } + + } + +}