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(); ResourceDeliveryMessage msg = (ResourceDeliveryMessage) theMessage.getPayload();
String subscriptionId = msg.getSubscriptionId(myFhirContext); 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()); ActiveSubscription updatedSubscription = mySubscriptionRegistry.get(msg.getSubscription().getIdElement(myFhirContext).getIdPart());
if (updatedSubscription != null) { 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.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.Gson;
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType; 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) @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 { public class ResourceDeliveryMessage extends BaseResourceMessage implements IResourceMessage {
@JsonIgnore @JsonProperty("canonicalSubscription")
private transient CanonicalSubscription mySubscription; private CanonicalSubscription mySubscription;
@JsonProperty("subscription")
private String mySubscriptionString;
@JsonProperty("payload") @JsonProperty("payload")
private String myPayloadString; private String myPayloadString;
@JsonIgnore @JsonIgnore
@ -85,17 +82,11 @@ public class ResourceDeliveryMessage extends BaseResourceMessage implements IRes
} }
public CanonicalSubscription getSubscription() { public CanonicalSubscription getSubscription() {
if (mySubscription == null && mySubscriptionString != null) {
mySubscription = new Gson().fromJson(mySubscriptionString, CanonicalSubscription.class);
}
return mySubscription; return mySubscription;
} }
public void setSubscription(CanonicalSubscription theSubscription) { public void setSubscription(CanonicalSubscription theSubscription) {
mySubscription = theSubscription; mySubscription = theSubscription;
if (mySubscription != null) {
mySubscriptionString = new Gson().toJson(mySubscription);
}
} }
public void setPayload(FhirContext theCtx, IBaseResource thePayload) { 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 * Helper method to fetch the subscription ID
*/ */
public String getSubscriptionId(FhirContext theFhirContext) { 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; 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.assertj.core.util.Lists;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import static org.junit.Assert.*; import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.contains;
public class CanonicalSubscriptionTest { public class CanonicalSubscriptionTest {
@Test @Test
public void testGetChannelExtension() { public void testGetChannelExtension() throws IOException {
HashMap<String, List<String>> inMap = new HashMap<>(); HashMap<String, List<String>> inMap = new HashMap<>();
inMap.put("key1", Lists.newArrayList("VALUE1")); inMap.put("key1", Lists.newArrayList("VALUE1"));
@ -22,13 +25,15 @@ public class CanonicalSubscriptionTest {
CanonicalSubscription s = new CanonicalSubscription(); CanonicalSubscription s = new CanonicalSubscription();
s.setChannelExtensions(inMap); s.setChannelExtensions(inMap);
s = serializeAndDeserialize(s);
assertThat(s.getChannelExtension("key1"), Matchers.equalTo("VALUE1")); assertThat(s.getChannelExtension("key1"), Matchers.equalTo("VALUE1"));
assertThat(s.getChannelExtension("key2"), Matchers.equalTo("VALUE2a")); assertThat(s.getChannelExtension("key2"), Matchers.equalTo("VALUE2a"));
assertThat(s.getChannelExtension("key3"), Matchers.nullValue()); assertThat(s.getChannelExtension("key3"), Matchers.nullValue());
} }
@Test @Test
public void testGetChannelExtensions() { public void testGetChannelExtensions() throws IOException {
HashMap<String, List<String>> inMap = new HashMap<>(); HashMap<String, List<String>> inMap = new HashMap<>();
inMap.put("key1", Lists.newArrayList("VALUE1")); inMap.put("key1", Lists.newArrayList("VALUE1"));
@ -37,8 +42,24 @@ public class CanonicalSubscriptionTest {
CanonicalSubscription s = new CanonicalSubscription(); CanonicalSubscription s = new CanonicalSubscription();
s.setChannelExtensions(inMap); s.setChannelExtensions(inMap);
s = serializeAndDeserialize(s);
assertThat(s.getChannelExtensions("key1"), Matchers.contains("VALUE1")); assertThat(s.getChannelExtensions("key1"), Matchers.contains("VALUE1"));
assertThat(s.getChannelExtensions("key2"), Matchers.contains("VALUE2a", "VALUE2b")); assertThat(s.getChannelExtensions("key2"), Matchers.contains("VALUE2a", "VALUE2b"));
assertThat(s.getChannelExtensions("key3"), Matchers.empty()); 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();
}
} }