diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/RestHookSubscriptionDstu2Interceptor.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/RestHookSubscriptionDstu2Interceptor.java index cd1d4d31de5..417b2442f65 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/RestHookSubscriptionDstu2Interceptor.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/RestHookSubscriptionDstu2Interceptor.java @@ -80,7 +80,8 @@ public class RestHookSubscriptionDstu2Interceptor extends BaseRestHookSubscripti */ private void checkSubscriptions(IIdType idType, String resourceType, RestOperationTypeEnum theOperation) { //avoid a ConcurrentModificationException by copying to an array - for (Object object : myRestHookSubscriptions.toArray()) { + Object[] subscriptions = myRestHookSubscriptions.toArray(); + for (Object object : subscriptions) { if (object == null) { continue; } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/r4/RestHookSubscriptionR4Interceptor.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/r4/RestHookSubscriptionR4Interceptor.java index 15ea72799bf..ba8745fb8ca 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/r4/RestHookSubscriptionR4Interceptor.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/interceptor/r4/RestHookSubscriptionR4Interceptor.java @@ -314,9 +314,10 @@ public class RestHookSubscriptionR4Interceptor extends BaseRestHookSubscriptionI Subscription subscription = (Subscription) theResource; if (subscription.getChannel() != null && subscription.getChannel().getType() == Subscription.SubscriptionChannelType.RESTHOOK - && subscription.getStatus() == Subscription.SubscriptionStatus.ACTIVE) { + && subscription.getStatus() == Subscription.SubscriptionStatus.REQUESTED) { removeLocalSubscription(subscription.getIdElement().getIdPart()); myRestHookSubscriptions.add(subscription); + subscription.setStatus(Subscription.SubscriptionStatus.ACTIVE); ourLog.info("Subscription was added, id: {} - Have {}", subscription.getIdElement().getIdPart(), myRestHookSubscriptions.size()); } } else { diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/BaseResourceProviderDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/BaseResourceProviderDstu2Test.java index 297805566d8..0831a17db58 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/BaseResourceProviderDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/BaseResourceProviderDstu2Test.java @@ -116,7 +116,7 @@ public abstract class BaseResourceProviderDstu2Test extends BaseJpaDstu2Test { server.start(); ourClient = myFhirCtx.newRestfulGenericClient(ourServerBase); - ourClient.registerInterceptor(new LoggingInterceptor(true)); + ourClient.registerInterceptor(new LoggingInterceptor()); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); @@ -160,4 +160,4 @@ public abstract class BaseResourceProviderDstu2Test extends BaseJpaDstu2Test { TestUtil.clearAllStaticFieldsForUnitTest(); } -} \ No newline at end of file +} diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/BaseResourceProviderR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/BaseResourceProviderR4Test.java index f76425a016e..36eb014ef51 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/BaseResourceProviderR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/BaseResourceProviderR4Test.java @@ -151,7 +151,7 @@ public abstract class BaseResourceProviderR4Test extends BaseJpaR4Test { myFhirCtx.getRestfulClientFactory().setSocketTimeout(5000000); ourClient = myFhirCtx.newRestfulGenericClient(ourServerBase); if (shouldLogClient()) { - ourClient.registerInterceptor(new LoggingInterceptor(true)); + ourClient.registerInterceptor(new LoggingInterceptor()); } PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); @@ -195,4 +195,4 @@ public abstract class BaseResourceProviderR4Test extends BaseJpaR4Test { TestUtil.clearAllStaticFieldsForUnitTest(); } -} \ No newline at end of file +} diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/RestHookTestDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/RestHookTestDstu2Test.java index b8a5fc32084..abf6d4e503d 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/RestHookTestDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/RestHookTestDstu2Test.java @@ -1,23 +1,6 @@ package ca.uhn.fhir.jpa.subscription; -import static org.junit.Assert.*; - -import java.util.List; - -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.servlet.ServletContextHandler; -import org.eclipse.jetty.servlet.ServletHolder; -import org.hl7.fhir.instance.model.api.IBaseResource; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import com.google.common.collect.Lists; - import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.jpa.dao.DaoConfig; import ca.uhn.fhir.jpa.provider.BaseResourceProviderDstu2Test; @@ -38,6 +21,19 @@ 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.util.PortUtil; +import com.google.common.collect.Lists; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IIdType; +import org.junit.*; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; /** * Test the rest-hook subscriptions @@ -51,12 +47,19 @@ public class RestHookTestDstu2Test extends BaseResourceProviderDstu2Test { private static String ourListenerServerBase; private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(RestHookTestDstu2Test.class); private static List ourUpdatedObservations = Lists.newArrayList(); + private List mySubscriptionIds = new ArrayList(); @After public void afterUnregisterRestHookListener() { + for (IIdType next : mySubscriptionIds){ + ourClient.delete().resourceById(next).execute(); + } + mySubscriptionIds.clear(); + myDaoConfig.setAllowMultipleDelete(true); ourLog.info("Deleting all subscriptions"); ourClient.delete().resourceConditionalByUrl("Subscription?status=active").execute(); + ourClient.delete().resourceConditionalByUrl("Observation?code:missing=false").execute(); ourLog.info("Done deleting all subscriptions"); myDaoConfig.setAllowMultipleDelete(new DaoConfig().isAllowMultipleDelete()); @@ -77,7 +80,7 @@ public class RestHookTestDstu2Test extends BaseResourceProviderDstu2Test { private Subscription createSubscription(String criteria, String payload, String endpoint) { Subscription subscription = new Subscription(); subscription.setReason("Monitor new neonatal function (note, age will be determined by the monitor)"); - subscription.setStatus(SubscriptionStatusEnum.ACTIVE); + subscription.setStatus(SubscriptionStatusEnum.REQUESTED); subscription.setCriteria(criteria); Channel channel = new Channel(); @@ -88,6 +91,7 @@ public class RestHookTestDstu2Test extends BaseResourceProviderDstu2Test { MethodOutcome methodOutcome = ourClient.create().resource(subscription).execute(); subscription.setId(methodOutcome.getId().getIdPart()); + mySubscriptionIds.add(methodOutcome.getId()); return subscription; } @@ -136,9 +140,9 @@ public class RestHookTestDstu2Test extends BaseResourceProviderDstu2Test { Observation observation2 = sendObservation(code, "SNOMED-CT"); - // Should see two subscription notifications + // Should see one subscription notification Thread.sleep(500); - assertEquals(3, ourCreatedObservations.size()); + assertEquals(2, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); ourClient.delete().resourceById(new IdDt("Subscription/" + subscription2.getId())).execute(); @@ -147,7 +151,7 @@ public class RestHookTestDstu2Test extends BaseResourceProviderDstu2Test { // Should see only one subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); Observation observation3 = ourClient.read(Observation.class, observationTemp3.getId()); @@ -160,7 +164,7 @@ public class RestHookTestDstu2Test extends BaseResourceProviderDstu2Test { // Should see no subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); Observation observation3a = ourClient.read(Observation.class, observationTemp3.getId()); @@ -174,7 +178,7 @@ public class RestHookTestDstu2Test extends BaseResourceProviderDstu2Test { // Should see only one subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(1, ourUpdatedObservations.size()); Assert.assertFalse(subscription1.getId().equals(subscription2.getId())); @@ -222,9 +226,9 @@ public class RestHookTestDstu2Test extends BaseResourceProviderDstu2Test { Observation observation2 = sendObservation(code, "SNOMED-CT"); - // Should see two subscription notifications + // Should see one subscription notification Thread.sleep(500); - assertEquals(3, ourCreatedObservations.size()); + assertEquals(ourCreatedObservations.toString(), 2, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); ourClient.delete().resourceById(new IdDt("Subscription/" + subscription2.getId())).execute(); @@ -233,7 +237,7 @@ public class RestHookTestDstu2Test extends BaseResourceProviderDstu2Test { // Should see only one subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); Observation observation3 = ourClient.read(Observation.class, observationTemp3.getId()); @@ -246,7 +250,7 @@ public class RestHookTestDstu2Test extends BaseResourceProviderDstu2Test { // Should see no subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); Observation observation3a = ourClient.read(Observation.class, observationTemp3.getId()); @@ -260,7 +264,7 @@ public class RestHookTestDstu2Test extends BaseResourceProviderDstu2Test { // Should see only one subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(1, ourUpdatedObservations.size()); Assert.assertFalse(subscription1.getId().equals(subscription2.getId())); diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/RestHookTestDstu3Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/RestHookTestDstu3Test.java index 18edb889c10..d639af6eb80 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/RestHookTestDstu3Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/RestHookTestDstu3Test.java @@ -1,57 +1,63 @@ package ca.uhn.fhir.jpa.subscription; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -import java.util.ArrayList; -import java.util.List; - -import javax.servlet.http.HttpServletRequest; +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.jpa.dao.DaoConfig; +import ca.uhn.fhir.jpa.provider.dstu3.BaseResourceProviderDstu3Test; +import ca.uhn.fhir.rest.annotation.Create; +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.rest.server.IResourceProvider; +import ca.uhn.fhir.rest.server.RestfulServer; +import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; +import ca.uhn.fhir.util.PortUtil; +import com.google.common.collect.Lists; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.hl7.fhir.dstu3.model.*; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IIdType; import org.junit.*; -import com.google.common.collect.Lists; +import javax.servlet.http.HttpServletRequest; +import java.util.ArrayList; +import java.util.List; -import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.jpa.dao.DaoConfig; -import ca.uhn.fhir.jpa.provider.dstu3.BaseResourceProviderDstu3Test; -import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider; -import ca.uhn.fhir.model.primitive.IdDt; -import ca.uhn.fhir.rest.annotation.*; -import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.rest.server.IResourceProvider; -import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; /** * Test the rest-hook subscriptions */ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { - private static List ourContentTypes = new ArrayList(); private static List ourCreatedObservations = Lists.newArrayList(); private static int ourListenerPort; private static RestfulServer ourListenerRestServer; private static Server ourListenerServer; private static String ourListenerServerBase; - private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(RestHookTestDstu3Test.class); - + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(RestHookTestDstu2Test.class); private static List ourUpdatedObservations = Lists.newArrayList(); + private List mySubscriptionIds = new ArrayList<>(); + private static List ourContentTypes = new ArrayList<>(); @After public void afterUnregisterRestHookListener() { + for (IIdType next : mySubscriptionIds){ + ourClient.delete().resourceById(next).execute(); + } + mySubscriptionIds.clear(); + myDaoConfig.setAllowMultipleDelete(true); ourLog.info("Deleting all subscriptions"); ourClient.delete().resourceConditionalByUrl("Subscription?status=active").execute(); + ourClient.delete().resourceConditionalByUrl("Observation?code:missing=false").execute(); ourLog.info("Done deleting all subscriptions"); myDaoConfig.setAllowMultipleDelete(new DaoConfig().isAllowMultipleDelete()); - + ourRestServer.unregisterInterceptor(ourRestHookSubscriptionInterceptor); } @@ -85,7 +91,7 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { private Subscription createSubscription(String theCriteria, String thePayload, String theEndpoint) { Subscription subscription = new Subscription(); subscription.setReason("Monitor new neonatal function (note, age will be determined by the monitor)"); - subscription.setStatus(Subscription.SubscriptionStatus.ACTIVE); + subscription.setStatus(Subscription.SubscriptionStatus.REQUESTED); subscription.setCriteria(theCriteria); Subscription.SubscriptionChannelComponent channel = new Subscription.SubscriptionChannelComponent(); @@ -96,6 +102,7 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { MethodOutcome methodOutcome = ourClient.create().resource(subscription).execute(); subscription.setId(methodOutcome.getId().getIdPart()); + mySubscriptionIds.add(methodOutcome.getId()); return subscription; } @@ -163,21 +170,20 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { subscriptionTemp.setCriteria(criteria1); ourClient.update().resource(subscriptionTemp).withId(subscriptionTemp.getIdElement()).execute(); - Observation observation2 = sendObservation(code, "SNOMED-CT"); - // Should see two subscription notifications + // Should see one subscription notification Thread.sleep(500); - assertEquals(3, ourCreatedObservations.size()); + assertEquals(2, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); - - ourClient.delete().resourceById(new IdDt("Subscription", subscription2.getId())).execute(); + + ourClient.delete().resourceById(new IdType("Subscription/" + subscription2.getId())).execute(); Observation observationTemp3 = sendObservation(code, "SNOMED-CT"); // Should see only one subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); Observation observation3 = ourClient.read(Observation.class, observationTemp3.getId()); @@ -190,7 +196,7 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { // Should see no subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); Observation observation3a = ourClient.read(Observation.class, observationTemp3.getId()); @@ -204,7 +210,7 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { // Should see only one subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(1, ourUpdatedObservations.size()); Assert.assertFalse(subscription1.getId().equals(subscription2.getId())); @@ -232,7 +238,6 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { assertEquals(Constants.CT_FHIR_XML_NEW, ourContentTypes.get(0)); } - @Test public void testRestHookSubscriptionApplicationXml() throws Exception { String payload = "application/xml"; @@ -258,21 +263,20 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { subscriptionTemp.setCriteria(criteria1); ourClient.update().resource(subscriptionTemp).withId(subscriptionTemp.getIdElement()).execute(); - Observation observation2 = sendObservation(code, "SNOMED-CT"); - // Should see two subscription notifications + // Should see one subscription notification Thread.sleep(500); - assertEquals(3, ourCreatedObservations.size()); + assertEquals(ourCreatedObservations.toString(), 2, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); - - ourClient.delete().resourceById(new IdDt("Subscription", subscription2.getId())).execute(); + + ourClient.delete().resourceById(new IdType("Subscription/" + subscription2.getId())).execute(); Observation observationTemp3 = sendObservation(code, "SNOMED-CT"); // Should see only one subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); Observation observation3 = ourClient.read(Observation.class, observationTemp3.getId()); @@ -285,7 +289,7 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { // Should see no subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); Observation observation3a = ourClient.read(Observation.class, observationTemp3.getId()); @@ -299,17 +303,17 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { // Should see only one subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(1, ourUpdatedObservations.size()); Assert.assertFalse(subscription1.getId().equals(subscription2.getId())); Assert.assertFalse(observation1.getId().isEmpty()); Assert.assertFalse(observation2.getId().isEmpty()); } - + @BeforeClass public static void startListenerServer() throws Exception { - ourListenerPort = RandomServerPortProvider.findFreePort(); + ourListenerPort = PortUtil.findFreePort(); ourListenerRestServer = new RestfulServer(FhirContext.forDstu3()); ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/r4/RestHookTestR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/r4/RestHookTestR4Test.java index 02be65c1095..ae77bbb3a6b 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/r4/RestHookTestR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/r4/RestHookTestR4Test.java @@ -1,57 +1,63 @@ - package ca.uhn.fhir.jpa.subscription.r4; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -import java.util.ArrayList; -import java.util.List; - -import javax.servlet.http.HttpServletRequest; -import ca.uhn.fhir.rest.api.Constants; -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.servlet.ServletContextHandler; -import org.eclipse.jetty.servlet.ServletHolder; -import org.hl7.fhir.r4.model.*; -import org.hl7.fhir.instance.model.api.IBaseResource; -import org.junit.*; - -import com.google.common.collect.Lists; - import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.jpa.dao.DaoConfig; import ca.uhn.fhir.jpa.provider.r4.BaseResourceProviderR4Test; -import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider; -import ca.uhn.fhir.model.primitive.IdDt; -import ca.uhn.fhir.rest.annotation.*; +import ca.uhn.fhir.jpa.subscription.RestHookTestDstu2Test; +import ca.uhn.fhir.rest.annotation.Create; +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.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; +import ca.uhn.fhir.util.PortUtil; +import com.google.common.collect.Lists; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IIdType; +import org.hl7.fhir.r4.model.*; +import org.junit.*; + +import javax.servlet.http.HttpServletRequest; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; /** * Test the rest-hook subscriptions */ public class RestHookTestR4Test extends BaseResourceProviderR4Test { - private static List ourContentTypes = new ArrayList(); + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(RestHookTestDstu2Test.class); private static List ourCreatedObservations = Lists.newArrayList(); private static int ourListenerPort; private static RestfulServer ourListenerRestServer; private static Server ourListenerServer; private static String ourListenerServerBase; - private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(RestHookTestR4Test.class); - private static List ourUpdatedObservations = Lists.newArrayList(); + private static List ourContentTypes = new ArrayList<>(); + private List mySubscriptionIds = new ArrayList<>(); @After public void afterUnregisterRestHookListener() { + for (IIdType next : mySubscriptionIds) { + ourClient.delete().resourceById(next).execute(); + } + mySubscriptionIds.clear(); + myDaoConfig.setAllowMultipleDelete(true); ourLog.info("Deleting all subscriptions"); ourClient.delete().resourceConditionalByUrl("Subscription?status=active").execute(); + ourClient.delete().resourceConditionalByUrl("Observation?code:missing=false").execute(); ourLog.info("Done deleting all subscriptions"); myDaoConfig.setAllowMultipleDelete(new DaoConfig().isAllowMultipleDelete()); - + ourRestServer.unregisterInterceptor(ourRestHookSubscriptionInterceptor); } @@ -66,7 +72,7 @@ public class RestHookTestR4Test extends BaseResourceProviderR4Test { ourUpdatedObservations.clear(); ourContentTypes.clear(); } - + @Test public void testRestHookSubscriptionInvalidCriteria() throws Exception { String payload = "application/xml"; @@ -81,11 +87,10 @@ public class RestHookTestR4Test extends BaseResourceProviderR4Test { } } - private Subscription createSubscription(String theCriteria, String thePayload, String theEndpoint) { Subscription subscription = new Subscription(); subscription.setReason("Monitor new neonatal function (note, age will be determined by the monitor)"); - subscription.setStatus(Subscription.SubscriptionStatus.ACTIVE); + subscription.setStatus(Subscription.SubscriptionStatus.REQUESTED); subscription.setCriteria(theCriteria); Subscription.SubscriptionChannelComponent channel = new Subscription.SubscriptionChannelComponent(); @@ -96,6 +101,7 @@ public class RestHookTestR4Test extends BaseResourceProviderR4Test { MethodOutcome methodOutcome = ourClient.create().resource(subscription).execute(); subscription.setId(methodOutcome.getId().getIdPart()); + mySubscriptionIds.add(methodOutcome.getId()); return subscription; } @@ -117,7 +123,7 @@ public class RestHookTestR4Test extends BaseResourceProviderR4Test { return observation; } - + @Test public void testRestHookSubscriptionApplicationFhirJson() throws Exception { String payload = "application/fhir+json"; @@ -137,7 +143,7 @@ public class RestHookTestR4Test extends BaseResourceProviderR4Test { assertEquals(0, ourUpdatedObservations.size()); assertEquals(Constants.CT_FHIR_JSON_NEW, ourContentTypes.get(0)); } - + @Test public void testRestHookSubscriptionApplicationJson() throws Exception { String payload = "application/json"; @@ -156,28 +162,27 @@ public class RestHookTestR4Test extends BaseResourceProviderR4Test { assertEquals(1, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); assertEquals(Constants.CT_FHIR_JSON_NEW, ourContentTypes.get(0)); - + Subscription subscriptionTemp = ourClient.read(Subscription.class, subscription2.getId()); Assert.assertNotNull(subscriptionTemp); subscriptionTemp.setCriteria(criteria1); ourClient.update().resource(subscriptionTemp).withId(subscriptionTemp.getIdElement()).execute(); - Observation observation2 = sendObservation(code, "SNOMED-CT"); - // Should see two subscription notifications + // Should see one subscription notification Thread.sleep(500); - assertEquals(3, ourCreatedObservations.size()); + assertEquals(2, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); - - ourClient.delete().resourceById(new IdDt("Subscription", subscription2.getId())).execute(); + + ourClient.delete().resourceById(new IdType("Subscription/" + subscription2.getId())).execute(); Observation observationTemp3 = sendObservation(code, "SNOMED-CT"); // Should see only one subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); Observation observation3 = ourClient.read(Observation.class, observationTemp3.getId()); @@ -190,7 +195,7 @@ public class RestHookTestR4Test extends BaseResourceProviderR4Test { // Should see no subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); Observation observation3a = ourClient.read(Observation.class, observationTemp3.getId()); @@ -204,7 +209,7 @@ public class RestHookTestR4Test extends BaseResourceProviderR4Test { // Should see only one subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(1, ourUpdatedObservations.size()); Assert.assertFalse(subscription1.getId().equals(subscription2.getId())); @@ -232,7 +237,6 @@ public class RestHookTestR4Test extends BaseResourceProviderR4Test { assertEquals(Constants.CT_FHIR_XML_NEW, ourContentTypes.get(0)); } - @Test public void testRestHookSubscriptionApplicationXml() throws Exception { String payload = "application/xml"; @@ -251,28 +255,27 @@ public class RestHookTestR4Test extends BaseResourceProviderR4Test { assertEquals(1, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); assertEquals(Constants.CT_FHIR_XML_NEW, ourContentTypes.get(0)); - + Subscription subscriptionTemp = ourClient.read(Subscription.class, subscription2.getId()); Assert.assertNotNull(subscriptionTemp); subscriptionTemp.setCriteria(criteria1); ourClient.update().resource(subscriptionTemp).withId(subscriptionTemp.getIdElement()).execute(); - Observation observation2 = sendObservation(code, "SNOMED-CT"); - // Should see two subscription notifications + // Should see one subscription notification Thread.sleep(500); - assertEquals(3, ourCreatedObservations.size()); + assertEquals(ourCreatedObservations.toString(), 2, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); - - ourClient.delete().resourceById(new IdDt("Subscription", subscription2.getId())).execute(); + + ourClient.delete().resourceById(new IdType("Subscription/" + subscription2.getId())).execute(); Observation observationTemp3 = sendObservation(code, "SNOMED-CT"); // Should see only one subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); Observation observation3 = ourClient.read(Observation.class, observationTemp3.getId()); @@ -285,7 +288,7 @@ public class RestHookTestR4Test extends BaseResourceProviderR4Test { // Should see no subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(0, ourUpdatedObservations.size()); Observation observation3a = ourClient.read(Observation.class, observationTemp3.getId()); @@ -299,17 +302,17 @@ public class RestHookTestR4Test extends BaseResourceProviderR4Test { // Should see only one subscription notification Thread.sleep(500); - assertEquals(4, ourCreatedObservations.size()); + assertEquals(3, ourCreatedObservations.size()); assertEquals(1, ourUpdatedObservations.size()); Assert.assertFalse(subscription1.getId().equals(subscription2.getId())); Assert.assertFalse(observation1.getId().isEmpty()); Assert.assertFalse(observation2.getId().isEmpty()); } - + @BeforeClass public static void startListenerServer() throws Exception { - ourListenerPort = RandomServerPortProvider.findFreePort(); + ourListenerPort = PortUtil.findFreePort(); ourListenerRestServer = new RestfulServer(FhirContext.forR4()); ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context";