added tests
This commit is contained in:
parent
4c8e330669
commit
b757d95d64
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<String> saveIds = new ArrayList<>();
|
||||
|
||||
List<String> 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<String> saveIds = new ArrayList<>();
|
||||
activeSubscriptionCache.put(id1, activeSub1);
|
||||
activeSubscriptionCache.put(ID1, activeSub1);
|
||||
|
||||
assertFalse(activeSub1.isFlagForDeletion());
|
||||
|
||||
List<String> 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<String> saveIds = new ArrayList<>();
|
||||
|
||||
List<String> 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<String> 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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
|
|
Loading…
Reference in New Issue