diff --git a/hapi-fhir-structures-dstu2/pom.xml b/hapi-fhir-structures-dstu2/pom.xml index c787fd6d0e7..0d164e14ebb 100644 --- a/hapi-fhir-structures-dstu2/pom.xml +++ b/hapi-fhir-structures-dstu2/pom.xml @@ -210,6 +210,12 @@ spring-web test + + ca.uhn.hapi.fhir + hapi-fhir-test-utilities + ${project.version} + test + diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/PreferTest.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/PreferTest.java index 72781c267f2..57bade29e4a 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/PreferTest.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/PreferTest.java @@ -1,14 +1,18 @@ package ca.uhn.fhir.rest.server; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.emptyOrNullString; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; - -import java.util.concurrent.TimeUnit; - +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.api.IResource; +import ca.uhn.fhir.model.dstu2.resource.OperationOutcome; +import ca.uhn.fhir.model.dstu2.resource.Patient; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.annotation.Create; +import ca.uhn.fhir.rest.annotation.IdParam; +import ca.uhn.fhir.rest.annotation.ResourceParam; +import ca.uhn.fhir.rest.annotation.Update; +import ca.uhn.fhir.rest.api.Constants; +import ca.uhn.fhir.rest.api.MethodOutcome; +import ca.uhn.fhir.test.utilities.JettyUtil; +import ca.uhn.fhir.util.TestUtil; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; @@ -26,19 +30,12 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.model.api.IResource; -import ca.uhn.fhir.model.dstu2.resource.OperationOutcome; -import ca.uhn.fhir.model.dstu2.resource.Patient; -import ca.uhn.fhir.model.primitive.IdDt; -import ca.uhn.fhir.rest.annotation.Create; -import ca.uhn.fhir.rest.annotation.IdParam; -import ca.uhn.fhir.rest.annotation.ResourceParam; -import ca.uhn.fhir.rest.annotation.Update; -import ca.uhn.fhir.rest.api.Constants; -import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.test.utilities.JettyUtil; -import ca.uhn.fhir.util.TestUtil; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.emptyOrNullString; +import static org.junit.Assert.*; public class PreferTest { private static CloseableHttpClient ourClient; diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/InterceptorUserDataMapDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/InterceptorUserDataMapDstu2Test.java index 3ce63c22b01..4bf1dd72d99 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/InterceptorUserDataMapDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/InterceptorUserDataMapDstu2Test.java @@ -1,19 +1,27 @@ package ca.uhn.fhir.rest.server.interceptor; -import static org.hamcrest.Matchers.contains; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; - -import java.util.*; -import java.util.concurrent.TimeUnit; - +import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.interceptor.api.Hook; import ca.uhn.fhir.interceptor.api.Interceptor; import ca.uhn.fhir.interceptor.api.Pointcut; +import ca.uhn.fhir.model.dstu2.composite.HumanNameDt; +import ca.uhn.fhir.model.dstu2.composite.IdentifierDt; +import ca.uhn.fhir.model.dstu2.resource.Bundle; +import ca.uhn.fhir.model.dstu2.resource.Patient; +import ca.uhn.fhir.model.dstu2.valueset.AdministrativeGenderEnum; +import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum; +import ca.uhn.fhir.model.primitive.DateDt; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.model.primitive.UriDt; +import ca.uhn.fhir.rest.annotation.*; +import ca.uhn.fhir.rest.api.server.RequestDetails; +import ca.uhn.fhir.rest.server.IResourceProvider; +import ca.uhn.fhir.rest.server.RestfulServer; +import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; +import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor.ActionRequestDetails; import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails; +import ca.uhn.fhir.test.utilities.JettyUtil; +import ca.uhn.fhir.util.TestUtil; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.CloseableHttpResponse; @@ -24,37 +32,30 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletHandler; import org.eclipse.jetty.servlet.ServletHolder; -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.model.dstu2.composite.HumanNameDt; -import ca.uhn.fhir.model.dstu2.composite.IdentifierDt; -import ca.uhn.fhir.model.dstu2.resource.Bundle; -import ca.uhn.fhir.model.dstu2.resource.Patient; -import ca.uhn.fhir.model.dstu2.valueset.AdministrativeGenderEnum; -import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum; -import ca.uhn.fhir.model.primitive.*; -import ca.uhn.fhir.rest.annotation.*; -import ca.uhn.fhir.rest.api.server.RequestDetails; -import ca.uhn.fhir.rest.server.IResourceProvider; -import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; -import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor.ActionRequestDetails; -import ca.uhn.fhir.test.utilities.JettyUtil; -import ca.uhn.fhir.util.TestUtil; +import java.util.*; +import java.util.concurrent.TimeUnit; + +import static org.awaitility.Awaitility.await; +import static org.hamcrest.Matchers.contains; +import static org.junit.Assert.*; public class InterceptorUserDataMapDstu2Test { + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(InterceptorUserDataMapDstu2Test.class); private static CloseableHttpClient ourClient; private static FhirContext ourCtx = FhirContext.forDstu2(); - private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(InterceptorUserDataMapDstu2Test.class); private static int ourPort; private static Server ourServer; private static RestfulServer servlet; private final Object myKey = "KEY"; + private final Object myValue = "VALUE"; private Map myMap; private Set myMapCheckMethods; - private final Object myValue = "VALUE"; @Before public void before() { @@ -66,10 +67,10 @@ public class InterceptorUserDataMapDstu2Test { @Before public void beforePurgeMap() { myMap = null; - myMapCheckMethods= new LinkedHashSet<>(); + myMapCheckMethods = new LinkedHashSet<>(); } - + @Test public void testException() throws Exception { @@ -78,7 +79,7 @@ public class InterceptorUserDataMapDstu2Test { IOUtils.closeQuietly(status.getEntity().getContent()); ourLog.info(myMapCheckMethods.toString()); - assertThat(myMapCheckMethods, contains("incomingRequestPostProcessed", "incomingRequestPreHandled", "preProcessOutgoingException", "handleException", "processingCompleted")); + await().until(() -> myMapCheckMethods, contains("incomingRequestPostProcessed", "incomingRequestPreHandled", "preProcessOutgoingException", "handleException", "processingCompleted")); } @Test @@ -94,9 +95,8 @@ public class InterceptorUserDataMapDstu2Test { } } - - ourLog.info(myMapCheckMethods.toString()); - assertThat(myMapCheckMethods.toString(), myMapCheckMethods, contains("incomingRequestPostProcessed", "incomingRequestPreHandled", "outgoingResponse", "processingCompletedNormally", "processingCompleted")); + + await().until(() -> myMapCheckMethods, contains("incomingRequestPostProcessed", "incomingRequestPreHandled", "outgoingResponse", "processingCompletedNormally", "processingCompleted")); } private void updateMapUsing(Map theUserData, String theMethod) { @@ -111,125 +111,6 @@ public class InterceptorUserDataMapDstu2Test { myMapCheckMethods.add(theMethod); } - @AfterClass - public static void afterClassClearContext() throws Exception { - JettyUtil.closeServer(ourServer); - TestUtil.clearAllStaticFieldsForUnitTest(); - } - - @BeforeClass - public static void beforeClass() throws Exception { - ourServer = new Server(0); - - ServletHandler proxyHandler = new ServletHandler(); - servlet = new RestfulServer(ourCtx); - - servlet.setResourceProviders(new DummyPatientResourceProvider()); - servlet.setPlainProviders(new PlainProvider()); - - ServletHolder servletHolder = new ServletHolder(servlet); - proxyHandler.addServletWithMapping(servletHolder, "/*"); - ourServer.setHandler(proxyHandler); - JettyUtil.startServer(ourServer); - ourPort = JettyUtil.getPortForStartedServer(ourServer); - - PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); - HttpClientBuilder builder = HttpClientBuilder.create(); - builder.setConnectionManager(connectionManager); - ourClient = builder.build(); - - } - - public static class DummyPatientResourceProvider implements IResourceProvider { - - private Patient createPatient1() { - Patient patient = new Patient(); - patient.addIdentifier(); - patient.getIdentifier().get(0).setUse(IdentifierUseEnum.OFFICIAL); - patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns")); - patient.getIdentifier().get(0).setValue("00001"); - patient.addName(); - patient.getName().get(0).addFamily("Test"); - patient.getName().get(0).addGiven("PatientOne"); - patient.setGender(AdministrativeGenderEnum.MALE); - patient.getId().setValue("1"); - return patient; - } - - public Map getIdToPatient() { - Map idToPatient = new HashMap(); - { - Patient patient = createPatient1(); - idToPatient.put("1", patient); - } - { - Patient patient = new Patient(); - patient.getIdentifier().add(new IdentifierDt()); - patient.getIdentifier().get(0).setUse(IdentifierUseEnum.OFFICIAL); - patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns")); - patient.getIdentifier().get(0).setValue("00002"); - patient.getName().add(new HumanNameDt()); - patient.getName().get(0).addFamily("Test"); - patient.getName().get(0).addGiven("PatientTwo"); - patient.setGender(AdministrativeGenderEnum.FEMALE); - patient.getId().setValue("2"); - idToPatient.put("2", patient); - } - return idToPatient; - } - - /** - * Retrieve the resource by its identifier - * - * @param theId - * The resource identity - * @return The resource - */ - @Read() - public Patient getResourceById(@IdParam IdDt theId) { - if (theId.getIdPart().equals("EX")) { - throw new InvalidRequestException("FOO"); - } - String key = theId.getIdPart(); - Patient retVal = getIdToPatient().get(key); - return retVal; - } - - /** - * Retrieve the resource by its identifier - * - * @param theId - * The resource identity - * @return The resource - */ - @Search() - public List getResourceById(@RequiredParam(name = "_id") String theId) { - throw new InvalidRequestException("FOO"); - } - - @Override - public Class getResourceType() { - return Patient.class; - } - - @Operation(name = "$everything", idempotent = true) - public Bundle patientTypeOperation(@OperationParam(name = "start") DateDt theStart, @OperationParam(name = "end") DateDt theEnd) { - - Bundle retVal = new Bundle(); - // Populate bundle with matching resources - return retVal; - } - - @Operation(name = "$everything", idempotent = true) - public Bundle patientTypeOperation(@IdParam IdDt theId, @OperationParam(name = "start") DateDt theStart, @OperationParam(name = "end") DateDt theEnd) { - - Bundle retVal = new Bundle(); - // Populate bundle with matching resources - return retVal; - } - - } - @Interceptor public class MyInterceptor { @@ -276,6 +157,94 @@ public class InterceptorUserDataMapDstu2Test { } + public static class DummyPatientResourceProvider implements IResourceProvider { + + private Patient createPatient1() { + Patient patient = new Patient(); + patient.addIdentifier(); + patient.getIdentifier().get(0).setUse(IdentifierUseEnum.OFFICIAL); + patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns")); + patient.getIdentifier().get(0).setValue("00001"); + patient.addName(); + patient.getName().get(0).addFamily("Test"); + patient.getName().get(0).addGiven("PatientOne"); + patient.setGender(AdministrativeGenderEnum.MALE); + patient.getId().setValue("1"); + return patient; + } + + public Map getIdToPatient() { + Map idToPatient = new HashMap(); + { + Patient patient = createPatient1(); + idToPatient.put("1", patient); + } + { + Patient patient = new Patient(); + patient.getIdentifier().add(new IdentifierDt()); + patient.getIdentifier().get(0).setUse(IdentifierUseEnum.OFFICIAL); + patient.getIdentifier().get(0).setSystem(new UriDt("urn:hapitest:mrns")); + patient.getIdentifier().get(0).setValue("00002"); + patient.getName().add(new HumanNameDt()); + patient.getName().get(0).addFamily("Test"); + patient.getName().get(0).addGiven("PatientTwo"); + patient.setGender(AdministrativeGenderEnum.FEMALE); + patient.getId().setValue("2"); + idToPatient.put("2", patient); + } + return idToPatient; + } + + /** + * Retrieve the resource by its identifier + * + * @param theId The resource identity + * @return The resource + */ + @Read() + public Patient getResourceById(@IdParam IdDt theId) { + if (theId.getIdPart().equals("EX")) { + throw new InvalidRequestException("FOO"); + } + String key = theId.getIdPart(); + Patient retVal = getIdToPatient().get(key); + return retVal; + } + + /** + * Retrieve the resource by its identifier + * + * @param theId The resource identity + * @return The resource + */ + @Search() + public List getResourceById(@RequiredParam(name = "_id") String theId) { + throw new InvalidRequestException("FOO"); + } + + @Override + public Class getResourceType() { + return Patient.class; + } + + @Operation(name = "$everything", idempotent = true) + public Bundle patientTypeOperation(@OperationParam(name = "start") DateDt theStart, @OperationParam(name = "end") DateDt theEnd) { + + Bundle retVal = new Bundle(); + // Populate bundle with matching resources + return retVal; + } + + @Operation(name = "$everything", idempotent = true) + public Bundle patientTypeOperation(@IdParam IdDt theId, @OperationParam(name = "start") DateDt theStart, @OperationParam(name = "end") DateDt theEnd) { + + Bundle retVal = new Bundle(); + // Populate bundle with matching resources + return retVal; + } + + } + public static class PlainProvider { @Operation(name = "$everything", idempotent = true) @@ -287,5 +256,34 @@ public class InterceptorUserDataMapDstu2Test { } } - + + @AfterClass + public static void afterClassClearContext() throws Exception { + JettyUtil.closeServer(ourServer); + TestUtil.clearAllStaticFieldsForUnitTest(); + } + + @BeforeClass + public static void beforeClass() throws Exception { + ourServer = new Server(0); + + ServletHandler proxyHandler = new ServletHandler(); + servlet = new RestfulServer(ourCtx); + + servlet.setResourceProviders(new DummyPatientResourceProvider()); + servlet.setPlainProviders(new PlainProvider()); + + ServletHolder servletHolder = new ServletHolder(servlet); + proxyHandler.addServletWithMapping(servletHolder, "/*"); + ourServer.setHandler(proxyHandler); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); + + PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); + HttpClientBuilder builder = HttpClientBuilder.create(); + builder.setConnectionManager(connectionManager); + ourClient = builder.build(); + + } + } diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SummaryParamR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SummaryParamR4Test.java index 2e85e6508a6..16c3506a56c 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SummaryParamR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SummaryParamR4Test.java @@ -18,6 +18,7 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletHandler; import org.eclipse.jetty.servlet.ServletHolder; +import org.hamcrest.CoreMatchers; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.*; import org.junit.AfterClass; @@ -31,6 +32,8 @@ import java.util.List; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; +import static org.hamcrest.CoreMatchers.containsStringIgnoringCase; +import static org.hamcrest.CoreMatchers.containsStringIgnoringCase; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -220,7 +223,7 @@ public class SummaryParamR4Test { assertThat(responseContent, (containsString("entry"))); assertThat(responseContent, (containsString(">TEXT<"))); assertThat(responseContent, (containsString("Medication/123"))); - assertThat(responseContent, not(containsStringIgnoringCase("note"))); + assertThat(responseContent, not(CoreMatchers.containsStringIgnoringCase("note"))); } ); diff --git a/hapi-fhir-test-utilities/pom.xml b/hapi-fhir-test-utilities/pom.xml index f68d07c606f..4336272c1d5 100644 --- a/hapi-fhir-test-utilities/pom.xml +++ b/hapi-fhir-test-utilities/pom.xml @@ -46,6 +46,11 @@ junit junit + + + org.awaitility + awaitility + diff --git a/pom.xml b/pom.xml index 0f48b01b040..d780efa1aa5 100755 --- a/pom.xml +++ b/pom.xml @@ -1022,6 +1022,25 @@ velocity-tools 2.0 + + org.awaitility + awaitility + 3.1.6 + + + org.hamcrest + java-hamcrest + + + org.hamcrest + hamcrest-core + + + org.hamcrest + hamcrest-library + + + org.codehaus.plexus plexus-compiler-api