Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed the issue of meta.source field inconsistently populated in subscription messages for different requests #4524

Merged
Prev Previous commit
Next Next commit
Addressing suggestion
  • Loading branch information
Qingyixia committed Feb 8, 2023
commit b99714451c7b6709bb28821749a56beed36785a1
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import ca.uhn.fhir.jpa.subscription.model.ResourceDeliveryMessage;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.api.server.SystemRequestDetails;
import ca.uhn.fhir.util.BundleBuilder;
import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.text.StringSubstitutor;
Expand Down Expand Up @@ -140,24 +141,10 @@ protected IBaseBundle createDeliveryBundleForPayloadSearchCriteria(CanonicalSubs
return builder.getBundle();
}

protected IBaseResource updateDeliveryResourceWithMetaSource(CanonicalSubscription theSubscription, IBaseResource thePayloadResource) {
protected IBaseResource updateDeliveryResourceWithMetaSource(IBaseResource thePayloadResource) {
String resType = thePayloadResource.fhirType();
IFhirResourceDao<?> dao = myDaoRegistry.getResourceDao(resType);
RuntimeResourceDefinition resourceDefinition = myFhirContext.getResourceDefinition(resType);

String payloadUrl = resType + "?" + Constants.PARAM_ID + "=" + thePayloadResource.getIdElement().getValue();
SearchParameterMap payloadSearchMap = myMatchUrlService.translateMatchUrl(payloadUrl, resourceDefinition);

if (payloadSearchMap == null) {
return null;
}
payloadSearchMap.setLoadSynchronous(true);
IBundleProvider searchResults = dao.search(payloadSearchMap, createRequestDetailForPartitionedRequest(theSubscription));

if (searchResults == null) {
return null;
}
IBaseResource resourceWithMetaSource = searchResults.getAllResources().get(0);
IBaseResource resourceWithMetaSource = dao.read(thePayloadResource.getIdElement(), new SystemRequestDetails());

return resourceWithMetaSource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected void doDelivery(ResourceDeliveryMessage theSourceMessage, CanonicalSub
if (isNotBlank(theSubscription.getPayloadSearchCriteria())) {
payloadResource = createDeliveryBundleForPayloadSearchCriteria(theSubscription, theWrappedMessageToSend.getPayload().getPayload(myFhirContext));
} else if (! theWrappedMessageToSend.getPayload().getPayloadString().contains(HapiExtensions.EXT_META_SOURCE)){
payloadResource = updateDeliveryResourceWithMetaSource(theSubscription, theWrappedMessageToSend.getPayload().getPayload(myFhirContext));
payloadResource = updateDeliveryResourceWithMetaSource(theWrappedMessageToSend.getPayload().getPayload(myFhirContext));
}

if (payloadResource != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,16 +406,6 @@ public void testSubscriptionMessageContainsMetaSourceField() throws URISyntaxExc
//Given: we have a subscription message that contains a patient resource
Patient p1 = generatePatient();
p1.addName().setFamily("p1-family");
p1.getMeta().setSource("#example-source");
IBundleProvider bundleProvider = new SimpleBundleProvider(List.of(p1));

//When
when(myInterceptorBroadcaster.callHooks(eq(Pointcut.SUBSCRIPTION_BEFORE_MESSAGE_DELIVERY), ArgumentMatchers.any(HookParams.class))).thenReturn(true);
when(myInterceptorBroadcaster.callHooks(eq(Pointcut.SUBSCRIPTION_AFTER_MESSAGE_DELIVERY), any())).thenReturn(false);
when(myChannelFactory.getOrCreateProducer(any(), any(), any())).thenReturn(myChannelProducer);
when(myDaoRegistry.getResourceDao(anyString())).thenReturn(myResourceDao);
when(myMatchUrlService.translateMatchUrl(any(), any(), any())).thenReturn(new SearchParameterMap());
when(myResourceDao.search(any(), any())).thenReturn(bundleProvider);

CanonicalSubscription subscription = generateSubscription();
subscription.setCriteriaString("[*]");
Expand All @@ -425,6 +415,15 @@ public void testSubscriptionMessageContainsMetaSourceField() throws URISyntaxExc
payload.setPayload(myCtx, p1, EncodingEnum.JSON);
payload.setOperationType(ResourceModifiedMessage.OperationTypeEnum.CREATE);

//When
when(myInterceptorBroadcaster.callHooks(eq(Pointcut.SUBSCRIPTION_BEFORE_MESSAGE_DELIVERY), ArgumentMatchers.any(HookParams.class))).thenReturn(true);
when(myInterceptorBroadcaster.callHooks(eq(Pointcut.SUBSCRIPTION_AFTER_MESSAGE_DELIVERY), any())).thenReturn(false);
when(myChannelFactory.getOrCreateProducer(any(), any(), any())).thenReturn(myChannelProducer);
when(myDaoRegistry.getResourceDao(anyString())).thenReturn(myResourceDao);

p1.getMeta().setSource("#example-source");
when(myResourceDao.read(any(), any())).thenReturn(p1);

//Then: meta.source field will be included in the message
myMessageSubscriber.handleMessage(payload);

Expand Down