From b757d95d6418517a36fbab33d8137f930f7372f4 Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Mon, 30 Sep 2019 21:04:29 -0400 Subject: [PATCH] added tests --- .../module/cache/SubscriptionRegistry.java | 2 +- .../channel/SubscriptionChannelRegistry.java | 2 + .../cache/ActiveSubscriptionCacheTest.java | 64 +++++++++++-------- .../cache/BaseSubscriptionRegistryTest.java | 53 +++++++++++++++ .../cache/SubscriptionRegistrySharedTest.java | 48 ++++++++++++++ .../cache/SubscriptionRegistryTest.java | 47 +++----------- .../SubscriptionChannelRegistryTest.java | 3 + 7 files changed, 153 insertions(+), 66 deletions(-) create mode 100644 hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/BaseSubscriptionRegistryTest.java create mode 100644 hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/SubscriptionRegistrySharedTest.java diff --git a/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/cache/SubscriptionRegistry.java b/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/cache/SubscriptionRegistry.java index 12b28fffc41..9a7ceccc58d 100644 --- a/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/cache/SubscriptionRegistry.java +++ b/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/cache/SubscriptionRegistry.java @@ -94,7 +94,7 @@ public class SubscriptionRegistry { String channelName = mySubscriptionDeliveryChannelNamer.nameFromSubscription(canonicalized); - ourLog.info("Registering active subscription {}", theSubscription.getIdElement().toUnqualified().getValue()); + ourLog.info("Registering active subscription {}", subscriptionId); ActiveSubscription activeSubscription = new ActiveSubscription(canonicalized, channelName); mySubscriptionChannelRegistry.add(activeSubscription); myActiveSubscriptionCache.put(subscriptionId, activeSubscription); diff --git a/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistry.java b/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistry.java index 0a299e0d029..8fbf0bfdbb5 100644 --- a/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistry.java +++ b/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistry.java @@ -35,9 +35,11 @@ public class SubscriptionChannelRegistry { return; } String channelName = theActiveSubscription.getChannelName(); + ourLog.info("Adding subscription {} to channel {}", theActiveSubscription.getId(), channelName); myActiveSubscriptionByChannelName.put(channelName, theActiveSubscription.getId()); if (mySubscriptionChannelCache.containsKey(channelName)) { + ourLog.info("Channel {} already exists. Not creating.", channelName); return; } diff --git a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/ActiveSubscriptionCacheTest.java b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/ActiveSubscriptionCacheTest.java index f9fc7af1a40..52fe9fab7c1 100644 --- a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/ActiveSubscriptionCacheTest.java +++ b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/ActiveSubscriptionCacheTest.java @@ -1,5 +1,7 @@ package ca.uhn.fhir.jpa.subscription.module.cache; +import ca.uhn.fhir.jpa.subscription.module.CanonicalSubscription; +import ca.uhn.fhir.model.primitive.IdDt; import org.junit.Test; import java.util.ArrayList; @@ -9,40 +11,47 @@ import static org.hamcrest.Matchers.containsInAnyOrder; import static org.junit.Assert.*; public class ActiveSubscriptionCacheTest { + static final String ID1 = "id1"; + static final String ID2 = "id2"; + @Test public void twoPhaseDelete() { ActiveSubscriptionCache activeSubscriptionCache = new ActiveSubscriptionCache(); - ActiveSubscription activeSub1 = new ActiveSubscription(null, null); - String id1 = "id1"; - activeSubscriptionCache.put(id1, activeSub1); + ActiveSubscription activeSub1 = buildActiveSubscription(ID1); + activeSubscriptionCache.put(ID1, activeSub1); assertFalse(activeSub1.isFlagForDeletion()); List saveIds = new ArrayList<>(); List idsToDelete = activeSubscriptionCache.markAllSubscriptionsNotInCollectionForDeletionAndReturnIdsToDelete(saveIds); assertTrue(activeSub1.isFlagForDeletion()); - assertNotNull(activeSubscriptionCache.get(id1)); + assertNotNull(activeSubscriptionCache.get(ID1)); assertEquals(0, idsToDelete.size()); idsToDelete = activeSubscriptionCache.markAllSubscriptionsNotInCollectionForDeletionAndReturnIdsToDelete(saveIds); - assertThat(idsToDelete, containsInAnyOrder(id1)); + assertThat(idsToDelete, containsInAnyOrder(ID1)); + } + + private ActiveSubscription buildActiveSubscription(String theId) { + CanonicalSubscription canonicalSubscription = new CanonicalSubscription(); + canonicalSubscription.setIdElement(new IdDt(theId)); + return new ActiveSubscription(canonicalSubscription, null); } @Test public void secondPassUnflags() { ActiveSubscriptionCache activeSubscriptionCache = new ActiveSubscriptionCache(); - ActiveSubscription activeSub1 = new ActiveSubscription(null, null); - String id1 = "id1"; + ActiveSubscription activeSub1 = buildActiveSubscription(ID1); List saveIds = new ArrayList<>(); - activeSubscriptionCache.put(id1, activeSub1); + activeSubscriptionCache.put(ID1, activeSub1); assertFalse(activeSub1.isFlagForDeletion()); List idsToDelete = activeSubscriptionCache.markAllSubscriptionsNotInCollectionForDeletionAndReturnIdsToDelete(saveIds); assertTrue(activeSub1.isFlagForDeletion()); - assertNotNull(activeSubscriptionCache.get(id1)); + assertNotNull(activeSubscriptionCache.get(ID1)); assertEquals(0, idsToDelete.size()); - saveIds.add(id1); + saveIds.add(ID1); idsToDelete = activeSubscriptionCache.markAllSubscriptionsNotInCollectionForDeletionAndReturnIdsToDelete(saveIds); assertFalse(activeSub1.isFlagForDeletion()); assertEquals(0, idsToDelete.size()); @@ -51,43 +60,42 @@ public class ActiveSubscriptionCacheTest { @Test public void onlyFlaggedDeleted() { ActiveSubscriptionCache activeSubscriptionCache = new ActiveSubscriptionCache(); - ActiveSubscription activeSub1 = new ActiveSubscription(null, null); - String id1 = "id1"; - ActiveSubscription activeSub2 = new ActiveSubscription(null, null); - String id2 = "id2"; - activeSubscriptionCache.put(id1, activeSub1); - activeSubscriptionCache.put(id2, activeSub2); + + ActiveSubscription activeSub1 = buildActiveSubscription(ID1); + ActiveSubscription activeSub2 = buildActiveSubscription(ID2); + activeSubscriptionCache.put(activeSub1.getId(), activeSub1); + activeSubscriptionCache.put(activeSub2.getId(), activeSub2); activeSub1.setFlagForDeletion(true); List saveIds = new ArrayList<>(); List idsToDelete = activeSubscriptionCache.markAllSubscriptionsNotInCollectionForDeletionAndReturnIdsToDelete(saveIds); - assertThat(idsToDelete, containsInAnyOrder(id1)); - assertNotNull(activeSubscriptionCache.get(id2)); + assertThat(idsToDelete, containsInAnyOrder(ID1)); + assertNotNull(activeSubscriptionCache.get(ID2)); assertTrue(activeSub2.isFlagForDeletion()); } @Test public void onListSavesAndUnmarksFlag() { ActiveSubscriptionCache activeSubscriptionCache = new ActiveSubscriptionCache(); - ActiveSubscription activeSub1 = new ActiveSubscription(null, null); - String id1 = "id1"; - ActiveSubscription activeSub2 = new ActiveSubscription(null, null); - String id2 = "id2"; - activeSubscriptionCache.put(id1, activeSub1); - activeSubscriptionCache.put(id2, activeSub2); + ActiveSubscription activeSub1 = buildActiveSubscription(ID1); + + ActiveSubscription activeSub2 = buildActiveSubscription(ID2); + + activeSubscriptionCache.put(ID1, activeSub1); + activeSubscriptionCache.put(ID2, activeSub2); activeSub1.setFlagForDeletion(true); List saveIds = new ArrayList<>(); - saveIds.add(id1); - saveIds.add(id2); + saveIds.add(ID1); + saveIds.add(ID2); activeSubscriptionCache.markAllSubscriptionsNotInCollectionForDeletionAndReturnIdsToDelete(saveIds); - assertNotNull(activeSubscriptionCache.get(id1)); + assertNotNull(activeSubscriptionCache.get(ID1)); assertFalse(activeSub1.isFlagForDeletion()); - assertNotNull(activeSubscriptionCache.get(id2)); + assertNotNull(activeSubscriptionCache.get(ID2)); assertFalse(activeSub2.isFlagForDeletion()); } diff --git a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/BaseSubscriptionRegistryTest.java b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/BaseSubscriptionRegistryTest.java new file mode 100644 index 00000000000..79d613fda21 --- /dev/null +++ b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/BaseSubscriptionRegistryTest.java @@ -0,0 +1,53 @@ +package ca.uhn.fhir.jpa.subscription.module.cache; + +import ca.uhn.fhir.jpa.subscription.module.BaseSubscriptionDstu3Test; +import ca.uhn.fhir.jpa.subscription.module.channel.SubscriptionChannelRegistry; +import org.hl7.fhir.dstu3.model.Subscription; +import org.junit.After; +import org.springframework.beans.factory.annotation.Autowired; + +import static org.junit.Assert.assertEquals; + +public abstract class BaseSubscriptionRegistryTest extends BaseSubscriptionDstu3Test { + public static final String SUBSCRIPTION_ID = "1"; + public static final String ORIG_CRITERIA = "Patient?"; + public static final String NEW_CRITERIA = "Observation?"; + + @Autowired + SubscriptionRegistry mySubscriptionRegistry; + @Autowired + SubscriptionChannelRegistry mySubscriptionChannelRegistry; + + @After + public void clearRegistryAfter() { + mySubscriptionRegistry.unregisterAllSubscriptions(); + assertRegistrySize(0); + } + + protected Subscription createSubscription() { + Subscription subscription = new Subscription(); + subscription.setId(SUBSCRIPTION_ID); + subscription.setCriteria(ORIG_CRITERIA); + subscription.setStatus(Subscription.SubscriptionStatus.ACTIVE); + setChannel(subscription, Subscription.SubscriptionChannelType.RESTHOOK); + return subscription; + } + + protected void setChannel(Subscription theSubscription, Subscription.SubscriptionChannelType theResthook) { + Subscription.SubscriptionChannelComponent channel = new Subscription.SubscriptionChannelComponent(); + channel.setType(theResthook); + channel.setPayload("application/json"); + channel.setEndpoint("http://unused.test.endpoint/"); + theSubscription.setChannel(channel); + } + + protected void assertRegistrySize(int theSize) { + assertRegistrySize(theSize, theSize); + } + + protected void assertRegistrySize(int theSubscriptionRegistrySize, int theSubscriptionChannelRegistrySize) { + assertEquals(theSubscriptionRegistrySize, mySubscriptionRegistry.size()); + assertEquals(theSubscriptionChannelRegistrySize, mySubscriptionChannelRegistry.size()); + + } +} diff --git a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/SubscriptionRegistrySharedTest.java b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/SubscriptionRegistrySharedTest.java new file mode 100644 index 00000000000..dbdfe3f7ee8 --- /dev/null +++ b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/SubscriptionRegistrySharedTest.java @@ -0,0 +1,48 @@ +package ca.uhn.fhir.jpa.subscription.module.cache; + +import ca.uhn.fhir.jpa.subscription.module.CanonicalSubscription; +import ca.uhn.fhir.jpa.subscription.module.channel.ISubscriptionDeliveryChannelNamer; +import ca.uhn.fhir.jpa.subscription.module.channel.SubscriptionDeliveryChannelNamer; +import org.hl7.fhir.dstu3.model.Subscription; +import org.junit.After; +import org.junit.Test; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.test.annotation.DirtiesContext; + +import static org.junit.Assert.*; + +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) +public class SubscriptionRegistrySharedTest extends BaseSubscriptionRegistryTest { + + private static final String OTHER_ID = "OTHER_ID"; + + @Configuration + public static class SpringConfig { + @Primary + @Bean + ISubscriptionDeliveryChannelNamer subscriptionDeliveryChannelNamer() { + return new SharedNamer(); + } + + private class SharedNamer implements ISubscriptionDeliveryChannelNamer { + @Override + public String nameFromSubscription(CanonicalSubscription theCanonicalSubscription) { + return "shared"; + } + } + } + + @Test + public void testTwoSubscriptionsOneChannel() { + Subscription subscription = createSubscription(); + assertRegistrySize(0); + mySubscriptionRegistry.registerSubscriptionUnlessAlreadyRegistered(subscription); + assertRegistrySize(1); + Subscription otherSubscription = createSubscription(); + otherSubscription.setId(OTHER_ID); + mySubscriptionRegistry.registerSubscriptionUnlessAlreadyRegistered(otherSubscription); + assertRegistrySize(2, 1); + } +} diff --git a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/SubscriptionRegistryTest.java b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/SubscriptionRegistryTest.java index aa51e2fbf80..79dd2c74b84 100644 --- a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/SubscriptionRegistryTest.java +++ b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/cache/SubscriptionRegistryTest.java @@ -1,45 +1,32 @@ package ca.uhn.fhir.jpa.subscription.module.cache; - -import ca.uhn.fhir.jpa.subscription.module.BaseSubscriptionDstu3Test; import org.hl7.fhir.dstu3.model.Subscription; import org.junit.After; -import org.junit.Before; import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; import static org.junit.Assert.*; -public class SubscriptionRegistryTest extends BaseSubscriptionDstu3Test { - public static final String SUBSCRIPTION_ID = "1"; - public static final String ORIG_CRITERIA = "Patient?"; - public static final String NEW_CRITERIA = "Observation?"; - @Autowired - SubscriptionRegistry mySubscriptionRegistry; - - @Before - public void clearRegistryBefore() { - mySubscriptionRegistry.unregisterAllSubscriptions(); - } +public class SubscriptionRegistryTest extends BaseSubscriptionRegistryTest { @After public void clearRegistryAfter() { mySubscriptionRegistry.unregisterAllSubscriptions(); + assertRegistrySize(0); } @Test public void updateSubscriptionReusesActiveSubscription() { Subscription subscription = createSubscription(); - assertEquals(0, mySubscriptionRegistry.size()); + assertRegistrySize(0); mySubscriptionRegistry.registerSubscriptionUnlessAlreadyRegistered(subscription); - assertEquals(1, mySubscriptionRegistry.size()); + assertRegistrySize(1); ActiveSubscription origActiveSubscription = mySubscriptionRegistry.get(SUBSCRIPTION_ID); assertEquals(ORIG_CRITERIA, origActiveSubscription.getCriteriaString()); subscription.setCriteria(NEW_CRITERIA); assertEquals(ORIG_CRITERIA, origActiveSubscription.getCriteriaString()); mySubscriptionRegistry.registerSubscriptionUnlessAlreadyRegistered(subscription); - assertEquals(1, mySubscriptionRegistry.size()); + assertRegistrySize(1); ActiveSubscription newActiveSubscription = mySubscriptionRegistry.get(SUBSCRIPTION_ID); assertEquals(NEW_CRITERIA, newActiveSubscription.getCriteriaString()); // The same object @@ -49,9 +36,10 @@ public class SubscriptionRegistryTest extends BaseSubscriptionDstu3Test { @Test public void updateSubscriptionDoesntReusesActiveSubscriptionWhenChannelChanges() { Subscription subscription = createSubscription(); - assertEquals(0, mySubscriptionRegistry.size()); + assertRegistrySize(0); mySubscriptionRegistry.registerSubscriptionUnlessAlreadyRegistered(subscription); - assertEquals(1, mySubscriptionRegistry.size()); + assertRegistrySize(1); + ActiveSubscription origActiveSubscription = mySubscriptionRegistry.get(SUBSCRIPTION_ID); assertEquals(ORIG_CRITERIA, origActiveSubscription.getCriteriaString()); @@ -59,26 +47,11 @@ public class SubscriptionRegistryTest extends BaseSubscriptionDstu3Test { assertEquals(ORIG_CRITERIA, origActiveSubscription.getCriteriaString()); mySubscriptionRegistry.registerSubscriptionUnlessAlreadyRegistered(subscription); - assertEquals(1, mySubscriptionRegistry.size()); + assertRegistrySize(1); + ActiveSubscription newActiveSubscription = mySubscriptionRegistry.get(SUBSCRIPTION_ID); // A new object assertFalse(newActiveSubscription == origActiveSubscription); } - private Subscription createSubscription() { - Subscription subscription = new Subscription(); - subscription.setId(SUBSCRIPTION_ID); - subscription.setCriteria(ORIG_CRITERIA); - subscription.setStatus(Subscription.SubscriptionStatus.ACTIVE); - setChannel(subscription, Subscription.SubscriptionChannelType.RESTHOOK); - return subscription; - } - - private void setChannel(Subscription theSubscription, Subscription.SubscriptionChannelType theResthook) { - Subscription.SubscriptionChannelComponent channel = new Subscription.SubscriptionChannelComponent(); - channel.setType(theResthook); - channel.setPayload("application/json"); - channel.setEndpoint("http://unused.test.endpoint/"); - theSubscription.setChannel(channel); - } } diff --git a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistryTest.java b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistryTest.java index 32b7802431d..3871d77ef3f 100644 --- a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistryTest.java +++ b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistryTest.java @@ -3,6 +3,7 @@ package ca.uhn.fhir.jpa.subscription.module.channel; import ca.uhn.fhir.jpa.model.entity.ModelConfig; import ca.uhn.fhir.jpa.subscription.module.CanonicalSubscription; import ca.uhn.fhir.jpa.subscription.module.cache.ActiveSubscription; +import ca.uhn.fhir.model.primitive.IdDt; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -41,8 +42,10 @@ public class SubscriptionChannelRegistryTest { when(myModelConfig.isSubscriptionMatchingEnabled()).thenReturn(true); CanonicalSubscription cansubA = new CanonicalSubscription(); + cansubA.setIdElement(new IdDt("A")); ActiveSubscription activeSubscriptionA = new ActiveSubscription(cansubA, TEST_CHANNEL_NAME); CanonicalSubscription cansubB = new CanonicalSubscription(); + cansubB.setIdElement(new IdDt("B")); ActiveSubscription activeSubscriptionB = new ActiveSubscription(cansubB, TEST_CHANNEL_NAME); assertNull(mySubscriptionChannelRegistry.get(TEST_CHANNEL_NAME));