PageRenderTime 21ms CodeModel.GetById 15ms RepoModel.GetById 8ms app.codeStats 0ms

/Framework/src/Ncqrs.Tests/Commanding/CommandExecution/Mapping/Fluent/FluentCommandMappingTests.cs

https://github.com/elfrostie/ncqrs
C# | 231 lines | 184 code | 47 blank | 0 comment | 4 complexity | b596507ddf539da8cd675fbbf748d60a MD5 | raw file
  1. using System;
  2. using FluentAssertions;
  3. using NUnit.Framework;
  4. using Ncqrs.Commanding;
  5. using Ncqrs.Commanding.CommandExecution;
  6. using Ncqrs.Commanding.CommandExecution.Mapping.Fluent;
  7. using Ncqrs.Commanding.ServiceModel;
  8. using Ncqrs.Domain;
  9. namespace Ncqrs.Tests.Commanding.CommandExecution.Mapping.Fluent
  10. {
  11. [TestFixture]
  12. public class FluentCommandMappingTests
  13. {
  14. private ICommandService TheService
  15. { get; set; }
  16. public static AggregateRootTarget AggRoot
  17. { get; set; }
  18. private static AggregateRootTarget GetAggregateRoot()
  19. {
  20. return AggRoot ?? (AggRoot = new AggregateRootTarget("from GetAggregateRoot()"));
  21. }
  22. private static AggregateRootTarget GetAggregateRoot(Guid ID)
  23. {
  24. if (AggRoot == null)
  25. {
  26. return new AggregateRootTarget("new Agg Root");
  27. }
  28. if (AggRoot.ArId == ID)
  29. {
  30. return AggRoot;
  31. }
  32. else
  33. {
  34. return new AggregateRootTarget("new and Different Agg Root");
  35. }
  36. }
  37. public class AggregateRootTargetUpdateTitleCommand : CommandBase
  38. {
  39. public string Title
  40. { get; set; }
  41. public Guid Id
  42. { get; set; }
  43. }
  44. public class AggregateRootTargetCreateOrUpdateTitleCommand : CommandBase
  45. {
  46. public string Title
  47. { get; set; }
  48. public Guid Id
  49. { get; set; }
  50. }
  51. public class AggregateRootTargetNotAMappedCommand : CommandBase
  52. {
  53. public string Title
  54. { get; set; }
  55. public Guid Id
  56. { get; set; }
  57. }
  58. public class AggregateRootTargetCreateNewCommand : CommandBase
  59. {
  60. public string Title
  61. { get; set; }
  62. public Guid Id
  63. { get; set; }
  64. }
  65. public class AggregateRootTargetStaticCreateCommand : CommandBase
  66. {
  67. public string Title
  68. { get; set; }
  69. public Guid Id
  70. { get; set; }
  71. }
  72. public class AggregateRootTargetTitleUpdatedEvent
  73. {
  74. public string Title
  75. { get; set; }
  76. }
  77. public class AggregateRootTargetCreatedNewEvent
  78. {
  79. public string Title
  80. { get; set; }
  81. }
  82. public class AggregateRootTarget : AggregateRootMappedWithExpressions
  83. {
  84. public string Title
  85. { get; private set; }
  86. public Guid ArId
  87. { get; private set; }
  88. public long created = DateTime.Now.Ticks;
  89. public AggregateRootTarget(string title)
  90. {
  91. ArId = Guid.NewGuid();
  92. var eventargs = new AggregateRootTargetCreatedNewEvent { Title = title };
  93. ApplyEvent(eventargs);
  94. }
  95. public AggregateRootTarget(Guid id, string title)
  96. {
  97. ArId = id;
  98. var eventargs = new AggregateRootTargetCreatedNewEvent { Title = title };
  99. ApplyEvent(eventargs);
  100. }
  101. public void UpdateTitle(string title)
  102. {
  103. var e = new AggregateRootTargetTitleUpdatedEvent { Title = title };
  104. ApplyEvent(e);
  105. }
  106. public void TitleUpdated(AggregateRootTargetTitleUpdatedEvent ev)
  107. {
  108. this.Title = ev.Title;
  109. AggRoot = this;
  110. }
  111. private void NewTestAggregateRootCreated(AggregateRootTargetCreatedNewEvent ev)
  112. {
  113. Title = ev.Title;
  114. AggRoot = this;
  115. }
  116. public override void InitializeEventHandlers()
  117. {
  118. Map<AggregateRootTargetTitleUpdatedEvent>().ToHandler(eventargs => TitleUpdated(eventargs));
  119. Map<AggregateRootTargetCreatedNewEvent>().ToHandler(eventargs => NewTestAggregateRootCreated(eventargs));
  120. }
  121. public static AggregateRootTarget CreateNew(string title)
  122. {
  123. return new AggregateRootTarget(title);
  124. }
  125. }
  126. [SetUp]
  127. public void Setup()
  128. {
  129. var service = new CommandService();
  130. Map.Command<AggregateRootTargetStaticCreateCommand>().ToAggregateRoot<AggregateRootTarget>().CreateNew((cmd) => AggregateRootTarget.CreateNew(cmd.Title)).StoreIn((cmd, aggroot) => AggRoot = aggroot).RegisterWith(service);
  131. Map.Command<AggregateRootTargetUpdateTitleCommand>().ToAggregateRoot<AggregateRootTarget>().WithId(cmd => cmd.Id, (guid, knownVersion) => GetAggregateRoot()).ToCallOn((cmd, aggroot) => aggroot.UpdateTitle(cmd.Title)).RegisterWith(service);
  132. Map.Command<AggregateRootTargetCreateNewCommand>().ToAggregateRoot<AggregateRootTarget>().CreateNew((cmd) => new AggregateRootTarget(cmd.Title)).StoreIn((cmd, aggroot) => AggRoot = aggroot).RegisterWith(service);
  133. Map.Command<AggregateRootTargetCreateOrUpdateTitleCommand>().ToAggregateRoot<AggregateRootTarget>().UseExistingOrCreateNew(cmd => cmd.Id, (guid, knownVersion) => GetAggregateRoot(guid), (cmd) => new AggregateRootTarget(cmd.Id, cmd.Title)).ToCallOn((cmd, aggroot) => aggroot.UpdateTitle(cmd.Title)).RegisterWith(service);
  134. TheService = service;
  135. }
  136. [Test]
  137. public void Command_should_update_the_title_of_the_aggregate_root()
  138. {
  139. var command = new AggregateRootTargetUpdateTitleCommand { Title = "AggregateRootTargetUpdateTitleCommand" };
  140. TheService.Execute(command);
  141. AggRoot.Title.Should().Be("AggregateRootTargetUpdateTitleCommand");
  142. }
  143. [Test]
  144. public void Command_should_throw_an_exception_when_the_command_is_not_mapped()
  145. {
  146. var command = new AggregateRootTargetNotAMappedCommand { Title = "AggregateRootTargetNotAMappedCommand" };
  147. Action act = () => TheService.Execute(command);
  148. act.ShouldThrow<ExecutorForCommandNotFoundException>();
  149. }
  150. [Test]
  151. public void Command_should_create_new_aggregate_root()
  152. {
  153. var command = new AggregateRootTargetCreateNewCommand { Title = "AggregateRootTargetCreateNewCommand" };
  154. TheService.Execute(command);
  155. AggRoot.Title.Should().Be("AggregateRootTargetCreateNewCommand");
  156. }
  157. [Test]
  158. public void Command_should_create_new_aggregate_root_with_static_method()
  159. {
  160. var command = new AggregateRootTargetStaticCreateCommand { Title = "AggregateRootTargetStaticCreateCommand" };
  161. TheService.Execute(command);
  162. AggRoot.Title.Should().Be("AggregateRootTargetStaticCreateCommand");
  163. }
  164. [Test]
  165. public void Command_should_create_and_then_use_existing()
  166. {
  167. var command = new AggregateRootTargetCreateOrUpdateTitleCommand { Title = "AggregateRootCreateNewCommand", Id = Guid.NewGuid() };
  168. TheService.Execute(command);
  169. AggRoot.Title.Should().Be("AggregateRootCreateNewCommand");
  170. var arId = AggRoot.ArId;
  171. command = new AggregateRootTargetCreateOrUpdateTitleCommand { Title = "AggregateRootCreateNewCommand2", Id = Guid.NewGuid() };
  172. TheService.Execute(command);
  173. AggRoot.Title.Should().Be("AggregateRootCreateNewCommand2");
  174. Assert.AreNotEqual(arId, AggRoot.ArId, "Id's should be different.");
  175. var createTicks = AggRoot.created;
  176. arId = AggRoot.ArId;
  177. command = new AggregateRootTargetCreateOrUpdateTitleCommand { Title = "AggregateRootUpdatedCommand", Id = arId };
  178. TheService.Execute(command);
  179. AggRoot.Title.Should().Be("AggregateRootUpdatedCommand");
  180. AggRoot.ArId.Should().Be(arId);
  181. AggRoot.created.Should().Be(createTicks);
  182. }
  183. }
  184. }