PageRenderTime 167ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FubuMVC.Tests/ServiceBus/Subscriptions/SubscriptionRepositoryTester.cs

http://github.com/DarthFubuMVC/fubumvc
C# | 299 lines | 228 code | 71 blank | 0 comment | 0 complexity | 1b7ece88e94993885bf18eeaa31e9785 MD5 | raw file
Possible License(s): JSON, 0BSD, Apache-2.0, WTFPL, BSD-3-Clause, MIT, BSD-2-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using FubuMVC.Core.ServiceBus;
  5. using FubuMVC.Core.ServiceBus.Configuration;
  6. using FubuMVC.Core.ServiceBus.Subscriptions;
  7. using NUnit.Framework;
  8. using Shouldly;
  9. namespace FubuMVC.Tests.ServiceBus.Subscriptions
  10. {
  11. [TestFixture]
  12. public class SubscriptionRepositoryTester
  13. {
  14. private InMemorySubscriptionPersistence persistence;
  15. private SubscriptionRepository theRepository;
  16. private string TheNodeName = "TheNode";
  17. private ChannelGraph channelGraph;
  18. [SetUp]
  19. public void SetUp()
  20. {
  21. persistence = new InMemorySubscriptionPersistence();
  22. channelGraph = new ChannelGraph{Name = TheNodeName};
  23. channelGraph.AddReplyChannel("foo", "foo://replies".ToUri());
  24. channelGraph.AddReplyChannel("bar", "bar://replies".ToUri());
  25. theRepository = new SubscriptionRepository(channelGraph, persistence);
  26. }
  27. [Test]
  28. public void save_the_first_subscriptions()
  29. {
  30. var subscription = ObjectMother.NewSubscription();
  31. theRepository.PersistSubscriptions(subscription);
  32. var requirements = theRepository.LoadSubscriptions(SubscriptionRole.Subscribes);
  33. requirements
  34. .ShouldHaveTheSameElementsAs(subscription);
  35. requirements.Single().Id.ShouldNotBe(Guid.Empty);
  36. }
  37. [Test]
  38. public void save_a_new_subscription_that_does_not_match_existing()
  39. {
  40. var existing = ObjectMother.ExistingSubscription();
  41. existing.NodeName = TheNodeName;
  42. persistence.Persist(existing);
  43. var subscription = ObjectMother.NewSubscription();
  44. subscription.NodeName = TheNodeName;
  45. theRepository.PersistSubscriptions(subscription);
  46. var requirements = theRepository.LoadSubscriptions(SubscriptionRole.Subscribes);
  47. requirements.Count().ShouldBe(2);
  48. requirements.ShouldContain(existing);
  49. requirements.ShouldContain(subscription);
  50. }
  51. [Test]
  52. public void save_a_new_subscription_with_a_mix_of_existing_subscriptions()
  53. {
  54. var existing = ObjectMother.ExistingSubscription();
  55. existing.NodeName = TheNodeName;
  56. persistence.Persist(existing);
  57. persistence.Persist(ObjectMother.ExistingSubscription("Different"));
  58. persistence.Persist(ObjectMother.ExistingSubscription("Different"));
  59. persistence.Persist(ObjectMother.ExistingSubscription("Different"));
  60. persistence.Persist(ObjectMother.ExistingSubscription("Different"));
  61. persistence.Persist(ObjectMother.ExistingSubscription("Different"));
  62. var subscription = ObjectMother.NewSubscription();
  63. subscription.NodeName = TheNodeName;
  64. theRepository.PersistSubscriptions(subscription);
  65. var requirements = theRepository.LoadSubscriptions(SubscriptionRole.Subscribes);
  66. requirements.Count().ShouldBe(2);
  67. requirements.ShouldContain(existing);
  68. requirements.ShouldContain(subscription);
  69. }
  70. [Test]
  71. public void save_a_subscription_that_already_exists()
  72. {
  73. var existing = ObjectMother.ExistingSubscription();
  74. existing.NodeName = TheNodeName;
  75. var subscription = existing.Clone();
  76. theRepository.PersistSubscriptions(subscription);
  77. theRepository.LoadSubscriptions(SubscriptionRole.Subscribes)
  78. .Single()
  79. .ShouldBe(existing);
  80. }
  81. [Test]
  82. public void save_a_mixed_bag_of_existing_and_new_subscriptions()
  83. {
  84. var existing = ObjectMother.ExistingSubscription(TheNodeName);
  85. var anotherExisting = ObjectMother.ExistingSubscription(TheNodeName);
  86. persistence.Persist(anotherExisting);
  87. persistence.Persist(existing);
  88. persistence.Persist(ObjectMother.ExistingSubscription("Different"));
  89. persistence.Persist(ObjectMother.ExistingSubscription("Different"));
  90. persistence.Persist(ObjectMother.ExistingSubscription("Different"));
  91. persistence.Persist(ObjectMother.ExistingSubscription("Different"));
  92. persistence.Persist(ObjectMother.ExistingSubscription("Different"));
  93. persistence.Persist(ObjectMother.ExistingSubscription("Different"));
  94. var old = existing.Clone();
  95. var newSubscription = ObjectMother.NewSubscription(TheNodeName);
  96. theRepository.PersistSubscriptions(old, newSubscription);
  97. var requirements = theRepository.LoadSubscriptions(SubscriptionRole.Subscribes);
  98. requirements.Count().ShouldBe(3);
  99. requirements.ShouldContain(existing);
  100. requirements.ShouldContain(newSubscription);
  101. requirements.ShouldContain(anotherExisting);
  102. }
  103. [Test]
  104. public void save_transport_node_for_the_first_time()
  105. {
  106. theRepository.Persist(new TransportNode(channelGraph));
  107. var node = persistence.NodesForGroup(channelGraph.Name)
  108. .Single();
  109. node.ShouldBe(new TransportNode(channelGraph));
  110. node.Id.ShouldNotBe(Guid.Empty.ToString());
  111. }
  112. [Test]
  113. public void saving_the_transport_node_is_idempotent()
  114. {
  115. theRepository.Persist(new TransportNode(channelGraph));
  116. var id = persistence.NodesForGroup(channelGraph.Name)
  117. .Single().Id;
  118. theRepository.Persist(new TransportNode(channelGraph));
  119. theRepository.Persist(new TransportNode(channelGraph));
  120. theRepository.Persist(new TransportNode(channelGraph));
  121. theRepository.Persist(new TransportNode(channelGraph));
  122. theRepository.Persist(new TransportNode(channelGraph));
  123. theRepository.Persist(new TransportNode(channelGraph));
  124. theRepository.Persist(new TransportNode(channelGraph));
  125. theRepository.Persist(new TransportNode(channelGraph));
  126. theRepository.Persist(new TransportNode(channelGraph));
  127. persistence.NodesForGroup(channelGraph.Name)
  128. .Single().Id.ShouldBe(id);
  129. }
  130. [Test]
  131. public void find_local()
  132. {
  133. var local = new TransportNode(channelGraph);
  134. theRepository.Persist(local, new TransportNode{Id="Foo"}, new TransportNode{Id = "Bar"});
  135. theRepository.FindLocal().ShouldBeTheSameAs(local);
  136. }
  137. [Test]
  138. public void find_peer()
  139. {
  140. var local = new TransportNode(channelGraph);
  141. var fooNode = new TransportNode { Id = "Foo" };
  142. theRepository.Persist(local, fooNode, new TransportNode { Id = "Bar" });
  143. theRepository.FindPeer("Foo")
  144. .ShouldBeTheSameAs(fooNode);
  145. }
  146. [Test]
  147. public void record_ownership_to_this_node_singular()
  148. {
  149. var local = new TransportNode(channelGraph);
  150. var fooNode = new TransportNode { Id = "Foo" };
  151. theRepository.Persist(local, fooNode, new TransportNode { Id = "Bar" });
  152. var subject = "foo://1".ToUri();
  153. theRepository.AddOwnershipToThisNode(subject);
  154. local.OwnedTasks.ShouldContain(subject);
  155. }
  156. [Test]
  157. public void record_multiple_ownerships_to_this_node()
  158. {
  159. var subjects = new Uri[] {"foo://1".ToUri(), "foo://2".ToUri(), "bar://1".ToUri()};
  160. var local = new TransportNode(channelGraph);
  161. var fooNode = new TransportNode { Id = "Foo" };
  162. theRepository.Persist(local, fooNode, new TransportNode { Id = "Bar" });
  163. theRepository.AddOwnershipToThisNode(subjects);
  164. local.OwnedTasks.ShouldHaveTheSameElementsAs(subjects);
  165. }
  166. [Test]
  167. public void remove_ownership_from_the_current_node()
  168. {
  169. var local = new TransportNode(channelGraph);
  170. var fooNode = new TransportNode { Id = "Foo" };
  171. theRepository.Persist(local, fooNode, new TransportNode { Id = "Bar" });
  172. var subject = "foo://1".ToUri();
  173. local.AddOwnership(subject);
  174. theRepository.RemoveOwnershipFromThisNode(subject);
  175. local.OwnedTasks.ShouldNotContain(subject);
  176. }
  177. [Test]
  178. public void remove_ownership_from_a_different_node()
  179. {
  180. var subject = "foo://1".ToUri();
  181. var local = new TransportNode(channelGraph);
  182. var fooNode = new TransportNode { Id = "Foo" };
  183. fooNode.AddOwnership(subject);
  184. theRepository.Persist(local, fooNode, new TransportNode { Id = "Bar" });
  185. theRepository.RemoveOwnershipFromNode(fooNode.Id, subject);
  186. fooNode.OwnedTasks.ShouldNotContain(subject);
  187. }
  188. [Test]
  189. public void remove_local_subscriptions()
  190. {
  191. var subscriptions = new[] { ObjectMother.NewSubscription(), ObjectMother.NewSubscription() };
  192. subscriptions.Each(x =>
  193. {
  194. x.Receiver = channelGraph.ReplyChannelFor("foo");
  195. x.Source = new Uri("foo://source");
  196. });
  197. theRepository.PersistSubscriptions(subscriptions);
  198. persistence.LoadSubscriptions(TheNodeName, SubscriptionRole.Subscribes)
  199. .ShouldHaveTheSameElementsAs(subscriptions);
  200. var removed = theRepository.RemoveLocalSubscriptions();
  201. removed.ShouldHaveTheSameElementsAs(subscriptions);
  202. persistence.LoadSubscriptions(TheNodeName, SubscriptionRole.Subscribes)
  203. .ShouldHaveCount(0);
  204. }
  205. [Test]
  206. public void remove_subscriptions_for_receiver()
  207. {
  208. var differentNode = ObjectMother.ExistingSubscription("DifferentNode");
  209. var differentReceiver = ObjectMother.ExistingSubscription();
  210. differentReceiver.Receiver = new Uri("memory://other_receiver");
  211. var subscriptions = new[]
  212. {
  213. ObjectMother.ExistingSubscription(),
  214. ObjectMother.ExistingSubscription(),
  215. ObjectMother.ExistingSubscription(),
  216. differentNode,
  217. differentReceiver
  218. };
  219. subscriptions.Each(x => x.Role = SubscriptionRole.Publishes);
  220. persistence.Persist(subscriptions);
  221. theRepository.RemoveSubscriptionsForReceiver(subscriptions[0].Receiver);
  222. persistence.LoadSubscriptions(TheNodeName, SubscriptionRole.Publishes)
  223. .ShouldHaveTheSameElementsAs(differentReceiver);
  224. persistence.LoadSubscriptions("DifferentNode", SubscriptionRole.Publishes)
  225. .ShouldHaveTheSameElementsAs(differentNode);
  226. }
  227. }
  228. }