Merge pull request #1572 from jamesagnew/ks-source-subscriptions

fixed in-memory search by _source for Dstu3
This commit is contained in:
Ken Stevens 2019-10-30 16:20:14 -04:00 committed by GitHub
commit 1fd3948b12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 91 additions and 7 deletions

View File

@ -26,18 +26,47 @@ import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.rest.api.Constants;
import org.hl7.fhir.instance.model.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
public class MetaUtil {
private static final Logger ourLog = LoggerFactory.getLogger(MetaUtil.class);
private MetaUtil() {
// non-instantiable
}
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<? extends IBaseExtension<?, ?>> extensions = metaWithExtensions.getExtension();
for (IBaseExtension extension : extensions) {
if (Constants.EXT_META_SOURCE.equals(extension.getUrl())) {
IPrimitiveType<String> value = (IPrimitiveType<String>) 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<IBase> sourceValues = sourceChild.getAccessor().getValues(theMeta);
String retVal = null;
if (sourceValues.size() > 0) {
@ -66,6 +95,8 @@ public class MetaUtil {
IPrimitiveType<String> value = (IPrimitiveType<String>) theContext.getElementDefinition("uri").newInstance();
value.setValue(theValue);
sourceExtension.setValue(value);
} else {
ourLog.error(MetaUtil.class.getSimpleName() + ".setSource() not supported on FHIR Version " + theContext.getVersion().getVersion());
}
}

View File

@ -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<Coding> 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

View File

@ -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()));
}
}