PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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