Tweaks to the way that subscriptions are serialized

This commit is contained in:
James Agnew 2019-02-17 19:17:51 -05:00
parent bd4eb36eda
commit 72c4726328
3 changed files with 36 additions and 16 deletions

View File

@ -51,6 +51,10 @@ public abstract class BaseSubscriptionDeliverySubscriber implements MessageHandl
ResourceDeliveryMessage msg = (ResourceDeliveryMessage) theMessage.getPayload();
String subscriptionId = msg.getSubscriptionId(myFhirContext);
if (subscriptionId == null) {
ourLog.warn("Subscription has no ID, ignoring");
return;
}
ActiveSubscription updatedSubscription = mySubscriptionRegistry.get(msg.getSubscription().getIdElement(myFhirContext).getIdPart());
if (updatedSubscription != null) {

View File

@ -27,7 +27,6 @@ import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.Gson;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
@ -39,10 +38,8 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank;
@JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
public class ResourceDeliveryMessage extends BaseResourceMessage implements IResourceMessage {
@JsonIgnore
private transient CanonicalSubscription mySubscription;
@JsonProperty("subscription")
private String mySubscriptionString;
@JsonProperty("canonicalSubscription")
private CanonicalSubscription mySubscription;
@JsonProperty("payload")
private String myPayloadString;
@JsonIgnore
@ -85,17 +82,11 @@ public class ResourceDeliveryMessage extends BaseResourceMessage implements IRes
}
public CanonicalSubscription getSubscription() {
if (mySubscription == null && mySubscriptionString != null) {
mySubscription = new Gson().fromJson(mySubscriptionString, CanonicalSubscription.class);
}
return mySubscription;
}
public void setSubscription(CanonicalSubscription theSubscription) {
mySubscription = theSubscription;
if (mySubscription != null) {
mySubscriptionString = new Gson().toJson(mySubscription);
}
}
public void setPayload(FhirContext theCtx, IBaseResource thePayload) {
@ -132,6 +123,10 @@ public class ResourceDeliveryMessage extends BaseResourceMessage implements IRes
* Helper method to fetch the subscription ID
*/
public String getSubscriptionId(FhirContext theFhirContext) {
return getSubscription().getIdElement(theFhirContext).getValue();
String retVal = null;
if (getSubscription() != null) {
retVal = getSubscription().getIdElement(theFhirContext).getValue();
}
return retVal;
}
}

View File

@ -1,19 +1,22 @@
package ca.uhn.fhir.jpa.subscription.module;
import ca.uhn.fhir.jpa.subscription.module.subscriber.ResourceDeliveryJsonMessage;
import ca.uhn.fhir.jpa.subscription.module.subscriber.ResourceDeliveryMessage;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.assertj.core.util.Lists;
import org.hamcrest.Matchers;
import org.junit.Test;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.contains;
import static org.junit.Assert.assertThat;
public class CanonicalSubscriptionTest {
@Test
public void testGetChannelExtension() {
public void testGetChannelExtension() throws IOException {
HashMap<String, List<String>> inMap = new HashMap<>();
inMap.put("key1", Lists.newArrayList("VALUE1"));
@ -22,13 +25,15 @@ public class CanonicalSubscriptionTest {
CanonicalSubscription s = new CanonicalSubscription();
s.setChannelExtensions(inMap);
s = serializeAndDeserialize(s);
assertThat(s.getChannelExtension("key1"), Matchers.equalTo("VALUE1"));
assertThat(s.getChannelExtension("key2"), Matchers.equalTo("VALUE2a"));
assertThat(s.getChannelExtension("key3"), Matchers.nullValue());
}
@Test
public void testGetChannelExtensions() {
public void testGetChannelExtensions() throws IOException {
HashMap<String, List<String>> inMap = new HashMap<>();
inMap.put("key1", Lists.newArrayList("VALUE1"));
@ -37,8 +42,24 @@ public class CanonicalSubscriptionTest {
CanonicalSubscription s = new CanonicalSubscription();
s.setChannelExtensions(inMap);
s = serializeAndDeserialize(s);
assertThat(s.getChannelExtensions("key1"), Matchers.contains("VALUE1"));
assertThat(s.getChannelExtensions("key2"), Matchers.contains("VALUE2a", "VALUE2b"));
assertThat(s.getChannelExtensions("key3"), Matchers.empty());
}
private CanonicalSubscription serializeAndDeserialize(CanonicalSubscription theSubscription) throws IOException {
ResourceDeliveryJsonMessage resourceDeliveryMessage = new ResourceDeliveryJsonMessage();
resourceDeliveryMessage.setPayload(new ResourceDeliveryMessage());
resourceDeliveryMessage.getPayload().setSubscription(theSubscription);
ObjectMapper mapper = new ObjectMapper();
String serialized = mapper.writeValueAsString(resourceDeliveryMessage);
resourceDeliveryMessage = mapper.readValue(serialized, ResourceDeliveryJsonMessage.class);
ResourceDeliveryMessage payload = resourceDeliveryMessage.getPayload();
return payload.getSubscription();
}
}