From 21c591eaf2f147477eb2eb176daf39a94bdef1ff Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Wed, 30 Oct 2019 13:49:10 -0400 Subject: [PATCH] ready for review --- .../main/java/ca/uhn/fhir/util/MetaUtil.java | 29 +++++++++++- .../resthook/RestHookTestDstu3Test.java | 46 ++++++++++++++++--- .../java/ca/uhn/fhir/util/MetaUtilTest.java | 19 ++++++++ 3 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/util/MetaUtilTest.java diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/MetaUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/MetaUtil.java index 99818b33fc0..5ff5d6f6937 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/MetaUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/MetaUtil.java @@ -36,8 +36,33 @@ public class MetaUtil { } public static String getSource(FhirContext theContext, IBaseMetaType theMeta) { - BaseRuntimeElementCompositeDefinition elementDef = (BaseRuntimeElementCompositeDefinition) theContext.getElementDefinition(theMeta.getClass()); + if (theContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.R4)) { + return getSourceR4Plus(theContext, theMeta); + } else if (theContext.getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) { + return getSourceDstu3((IBaseHasExtensions) theMeta); + } else { + throw new UnsupportedOperationException(MetaUtil.class.getSimpleName() + ".getSource() not supported on FHIR Version " + theContext.getVersion().getVersion()); + } + } + + private static String getSourceDstu3(IBaseHasExtensions theMeta) { + IBaseHasExtensions metaWithExtensions = theMeta; + List> extensions = metaWithExtensions.getExtension(); + for (IBaseExtension extension : extensions) { + if (Constants.EXT_META_SOURCE.equals(extension.getUrl())) { + IPrimitiveType value = (IPrimitiveType) extension.getValue(); + return value.getValueAsString(); + } + } + return null; + } + + private static String getSourceR4Plus(FhirContext theFhirContext, IBaseMetaType theMeta) { + BaseRuntimeElementCompositeDefinition elementDef = (BaseRuntimeElementCompositeDefinition) theFhirContext.getElementDefinition(theMeta.getClass()); BaseRuntimeChildDefinition sourceChild = elementDef.getChildByName("source"); + if (sourceChild == null) { + return null; + } List sourceValues = sourceChild.getAccessor().getValues(theMeta); String retVal = null; if (sourceValues.size() > 0) { @@ -66,6 +91,8 @@ public class MetaUtil { IPrimitiveType value = (IPrimitiveType) theContext.getElementDefinition("uri").newInstance(); value.setValue(theValue); sourceExtension.setValue(value); + } else { + throw new UnsupportedOperationException(MetaUtil.class.getSimpleName() + ".setSource() not supported on FHIR Version " + theContext.getVersion().getVersion()); } } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestDstu3Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestDstu3Test.java index 02fde230ef3..28f36960a5e 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestDstu3Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestDstu3Test.java @@ -18,6 +18,7 @@ import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; import ca.uhn.fhir.test.utilities.JettyUtil; +import ca.uhn.fhir.util.MetaUtil; import com.google.common.collect.Lists; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -242,6 +243,33 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { assertEquals(Constants.CT_FHIR_JSON_NEW, ourContentTypes.get(0)); } + + @Test + public void testRestHookSubscriptionSource() throws Exception { + String payload = "application/fhir+json"; + + String source = "foosource"; + String criteria = "Observation?_source=" + source; + + Subscription subscription = newSubscription(criteria, payload, ourListenerServerBase, null); + MethodOutcome methodOutcome = ourClient.create().resource(subscription).execute(); + Subscription savedSub = (Subscription) methodOutcome.getResource(); + assertInMemoryTag(savedSub); + mySubscriptionIds.add(methodOutcome.getId()); + + waitForQueueToDrain(); + + Observation observation = new Observation(); + MetaUtil.setSource(myFhirCtx, observation, source); + ourClient.create().resource(observation).execute(); + + // Should see 1 subscription notification + waitForQueueToDrain(); + waitForSize(0, ourCreatedObservations); + waitForSize(1, ourUpdatedObservations); + assertEquals(Constants.CT_FHIR_JSON_NEW, ourContentTypes.get(0)); + } + @Test public void testRestHookSubscriptionApplicationJson() throws Exception { String payload = "application/json"; @@ -474,11 +502,17 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { assertEquals("In-memory", tag.getDisplay()); // Wait for subscription to be moved to active - await().until(()-> Subscription.SubscriptionStatus.ACTIVE.equals(ourClient.read().resource(Subscription.class).withId(subscriptionId.toUnqualifiedVersionless()).execute().getStatus())); + await().until(() -> Subscription.SubscriptionStatus.ACTIVE.equals(ourClient.read().resource(Subscription.class).withId(subscriptionId.toUnqualifiedVersionless()).execute().getStatus())); Subscription subscriptionActivated = ourClient.read().resource(Subscription.class).withId(subscriptionId.toUnqualifiedVersionless()).execute(); assertEquals(Subscription.SubscriptionStatus.ACTIVE, subscriptionActivated.getStatus()); - tags = subscriptionActivated.getMeta().getTag(); + assertInMemoryTag(subscriptionActivated); + } + + private void assertInMemoryTag(Subscription theSubscription) { + List tags; + Coding tag; + tags = theSubscription.getMeta().getTag(); assertEquals(1, tags.size()); tag = tags.get(0); assertEquals(JpaConstants.EXT_SUBSCRIPTION_MATCHING_STRATEGY, tag.getSystem()); @@ -501,7 +535,7 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { assertEquals("Database", tag.getDisplay()); // Wait for subscription to be moved to active - await().until(()-> Subscription.SubscriptionStatus.ACTIVE.equals(ourClient.read().resource(Subscription.class).withId(subscriptionId.toUnqualifiedVersionless()).execute().getStatus())); + await().until(() -> Subscription.SubscriptionStatus.ACTIVE.equals(ourClient.read().resource(Subscription.class).withId(subscriptionId.toUnqualifiedVersionless()).execute().getStatus())); Subscription subscription = ourClient.read().resource(Subscription.class).withId(subscriptionId.toUnqualifiedVersionless()).execute(); assertEquals(Subscription.SubscriptionStatus.ACTIVE, subscription.getStatus()); @@ -624,9 +658,9 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { ourListenerServer.setHandler(proxyHandler); JettyUtil.startServer(ourListenerServer); - ourListenerPort = JettyUtil.getPortForStartedServer(ourListenerServer); - ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; - ourNotificationListenerServer = "http://localhost:" + ourListenerPort + "/fhir/subscription"; + ourListenerPort = JettyUtil.getPortForStartedServer(ourListenerServer); + ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; + ourNotificationListenerServer = "http://localhost:" + ourListenerPort + "/fhir/subscription"; } @AfterClass diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/util/MetaUtilTest.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/util/MetaUtilTest.java new file mode 100644 index 00000000000..cf9924a5c55 --- /dev/null +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/util/MetaUtilTest.java @@ -0,0 +1,19 @@ +package ca.uhn.fhir.util; + +import ca.uhn.fhir.context.FhirContext; +import org.hl7.fhir.dstu3.model.Observation; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class MetaUtilTest { + FhirContext ourFhirContext = FhirContext.forDstu3(); + + @Test + public void testSetGetDstu3() { + String source = "testSource"; + Observation observation = new Observation(); + MetaUtil.setSource(ourFhirContext, observation, source); + assertEquals(source, MetaUtil.getSource(ourFhirContext, observation.getMeta())); + } +}