PageRenderTime 36ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/Framework/src/Ncqrs.Tests/Commanding/CommandExecution/Mapping/Attributes/AttributeCommandMappingTests.cs

https://github.com/elfrostie/ncqrs
C# | 389 lines | 312 code | 77 blank | 0 comment | 4 complexity | ccb50c2e888ea4b7d9502a8529830e87 MD5 | raw file
  1. using System;
  2. using System.Transactions;
  3. using FluentAssertions;
  4. using Ncqrs.Commanding;
  5. using Ncqrs.Commanding.CommandExecution.Mapping;
  6. using Ncqrs.Commanding.CommandExecution.Mapping.Attributes;
  7. using Ncqrs.Domain;
  8. using NUnit.Framework;
  9. namespace Ncqrs.Tests.Commanding.CommandExecution.Mapping.Attributes
  10. {
  11. [TestFixture]
  12. public class AttributeCommandMappingTests
  13. {
  14. [MapsToAggregateRootMethod("Ncqrs.Tests.Commanding.CommandExecution.Mapping.Attributes.AttributeCommandMappingTests+AggregateRootTarget, Ncqrs.Tests", "UpdateTitle")]
  15. public class AggregateRootTargetUpdateTitleCommand : CommandBase
  16. {
  17. public string Title
  18. { get; set; }
  19. [AggregateRootId]
  20. public Guid Id
  21. { get; set; }
  22. }
  23. [MapsToAggregateRootMethodOrConstructor("Ncqrs.Tests.Commanding.CommandExecution.Mapping.Attributes.AttributeCommandMappingTests+AggregateRootTarget, Ncqrs.Tests", "UpdateTitle")]
  24. public class AggregateRootTargetCreateOrUpdateTitleCommand : CommandBase
  25. {
  26. public string Title
  27. { get; set; }
  28. [AggregateRootId]
  29. public Guid Id
  30. { get; set; }
  31. }
  32. [Transactional]
  33. [MapsToAggregateRootMethod("Ncqrs.Tests.Commanding.CommandExecution.Mapping.Attributes.AttributeCommandMappingTests+AggregateRootTarget, Ncqrs.Tests", "UpdateTitle")]
  34. public class TransactionalAggregateRootTargetUpdateTitleCommand : CommandBase
  35. {
  36. public string Title
  37. { get; set; }
  38. [AggregateRootId]
  39. public Guid Id
  40. { get; set; }
  41. }
  42. [Transactional]
  43. [MapsToAggregateRootMethodOrConstructor("Ncqrs.Tests.Commanding.CommandExecution.Mapping.Attributes.AttributeCommandMappingTests+AggregateRootTarget, Ncqrs.Tests", "UpdateTitle")]
  44. public class TransactionalAggregateRootTargetCreateOrUpdateTitleCommand : CommandBase
  45. {
  46. public string Title
  47. { get; set; }
  48. [AggregateRootId]
  49. public Guid Id
  50. { get; set; }
  51. }
  52. public class AggregateRootTargetNotAMappedCommand : CommandBase
  53. {
  54. public string Title
  55. { get; set; }
  56. [AggregateRootId]
  57. public Guid Id
  58. { get; set; }
  59. }
  60. [MapsToAggregateRootConstructor("Ncqrs.Tests.Commanding.CommandExecution.Mapping.Attributes.AttributeCommandMappingTests+AggregateRootTarget, Ncqrs.Tests")]
  61. public class AggregateRootTargetCreateNewCommand : CommandBase
  62. {
  63. public string Title
  64. { get; set; }
  65. [ExcludeInMapping]
  66. public Guid Id
  67. { get; set; }
  68. }
  69. [Transactional]
  70. [MapsToAggregateRootConstructor("Ncqrs.Tests.Commanding.CommandExecution.Mapping.Attributes.AttributeCommandMappingTests+AggregateRootTarget, Ncqrs.Tests")]
  71. public class TransactionalAggregateRootTargetCreateNewCommand : CommandBase
  72. {
  73. public string Title
  74. { get; set; }
  75. [ExcludeInMapping]
  76. public Guid Id
  77. { get; set; }
  78. }
  79. public class AggregateRootTargetTitleUpdatedEvent
  80. {
  81. public string Title
  82. { get; set; }
  83. }
  84. public class AggregateRootTargetCreatedNewEvent
  85. {
  86. public string Title
  87. { get; set; }
  88. }
  89. public class AggregateRootTarget : AggregateRootMappedWithExpressions
  90. {
  91. public string Title
  92. { get; private set; }
  93. public AggregateRootTarget(string title)
  94. {
  95. var eventargs = new AggregateRootTargetCreatedNewEvent { Title = title };
  96. ApplyEvent(eventargs);
  97. }
  98. private AggregateRootTarget()
  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. }
  109. private void NewTestAggregateRootCreated(AggregateRootTargetCreatedNewEvent ev)
  110. {
  111. Title = ev.Title;
  112. }
  113. public override void InitializeEventHandlers()
  114. {
  115. Map<AggregateRootTargetTitleUpdatedEvent>().ToHandler(eventargs => TitleUpdated(eventargs));
  116. Map<AggregateRootTargetCreatedNewEvent>().ToHandler(eventargs => NewTestAggregateRootCreated(eventargs));
  117. }
  118. }
  119. [MapsToAggregateRootConstructor("Ncqrs.Tests.Commanding.CommandExecution.Mapping.Attributes.AttributeCommandMappingTests+ComplexAggregateRootTarget, Ncqrs.Tests")]
  120. public class ComplexAggregateRootTargetCreateNewCommand1 : CommandBase
  121. {
  122. [Parameter(1)]
  123. public string Title
  124. { get; set; }
  125. [Parameter(2)]
  126. public int Quantity
  127. { get; set; }
  128. }
  129. [MapsToAggregateRootConstructor("Ncqrs.Tests.Commanding.CommandExecution.Mapping.Attributes.AttributeCommandMappingTests+ComplexAggregateRootTarget, Ncqrs.Tests")]
  130. public class ComplexAggregateRootTargetCreateNewCommand2 : CommandBase
  131. {
  132. [Parameter("title")]
  133. public string Title
  134. { get; set; }
  135. [Parameter("quantity")]
  136. public int Quantity
  137. { get; set; }
  138. }
  139. [MapsToAggregateRootConstructor("Ncqrs.Tests.Commanding.CommandExecution.Mapping.Attributes.AttributeCommandMappingTests+ComplexAggregateRootTarget, Ncqrs.Tests")]
  140. public class ComplexAggregateRootTargetCreateNewCommand3 : CommandBase
  141. {
  142. [Parameter("title")]
  143. public string Title
  144. { get; set; }
  145. [Parameter(2)]
  146. public int Quantity
  147. { get; set; }
  148. }
  149. [MapsToAggregateRootConstructor("Ncqrs.Tests.Commanding.CommandExecution.Mapping.Attributes.AttributeCommandMappingTests+ComplexAggregateRootTarget, Ncqrs.Tests")]
  150. public class ComplexAggregateRootTargetCreateNewCommand4 : CommandBase
  151. {
  152. [Parameter]
  153. public string Title
  154. { get; set; }
  155. [Parameter]
  156. public int Quantity
  157. { get; set; }
  158. }
  159. public class ComplexAggregateRootTargetCreatedNewEvent
  160. {
  161. public string Title
  162. { get; set; }
  163. public int Quantity
  164. { get; set; }
  165. }
  166. public class ComplexAggregateRootTarget : AggregateRootMappedWithExpressions
  167. {
  168. public string Title
  169. { get; private set; }
  170. public int Quantity
  171. { get; private set; }
  172. public ComplexAggregateRootTarget(string title, int quantity)
  173. {
  174. var eventargs = new ComplexAggregateRootTargetCreatedNewEvent { Title = title, Quantity = quantity };
  175. ApplyEvent(eventargs);
  176. }
  177. private ComplexAggregateRootTarget()
  178. { }
  179. private void NewTestComplexAggregateRootCreated(ComplexAggregateRootTargetCreatedNewEvent ev)
  180. {
  181. Title = ev.Title;
  182. Quantity = ev.Quantity;
  183. }
  184. public override void InitializeEventHandlers()
  185. {
  186. Map<ComplexAggregateRootTargetCreatedNewEvent>().ToHandler(NewTestComplexAggregateRootCreated);
  187. }
  188. }
  189. [Test]
  190. public void Command_should_update_the_title_of_the_aggregate_root()
  191. {
  192. var instance = new AggregateRootTarget("TitleSetInConstructor");
  193. var command = new AggregateRootTargetUpdateTitleCommand { Title = "AggregateRootTargetUpdateTitleCommand" };
  194. var executor = new TestAttributeMappedCommandExecutor<AggregateRootTarget>(command, instance);
  195. executor.Execute();
  196. executor.Instance.Title.Should().Be("AggregateRootTargetUpdateTitleCommand");
  197. }
  198. [Test]
  199. public void Command_should_create_and_update_the_title_of_the_aggregate_root()
  200. {
  201. AggregateRootTarget instance = null;
  202. var command = new AggregateRootTargetCreateOrUpdateTitleCommand { Title = "AggregateRootTargetUpdateTitleCommand" };
  203. var executor = new TestAttributeMappedCommandExecutor<AggregateRootTarget>(command, instance);
  204. executor.Execute();
  205. executor.Instance.Title.Should().Be("AggregateRootTargetUpdateTitleCommand");
  206. }
  207. [Test]
  208. public void Command_should_update_the_title_of_the_existing_aggregate_root()
  209. {
  210. var instance = new AggregateRootTarget("TitleSetInConstructor");
  211. var command = new AggregateRootTargetCreateOrUpdateTitleCommand { Title = "AggregateRootTargetUpdateTitleCommand" };
  212. var executor = new TestAttributeMappedCommandExecutor<AggregateRootTarget>(command, instance);
  213. executor.Execute();
  214. executor.Instance.Title.Should().Be("AggregateRootTargetUpdateTitleCommand");
  215. }
  216. [Test]
  217. public void Command_decorated_with_Transactional_attribute_mapped_to_methodOrCreator_should_be_create_and_be_executed_in_context_of_transaction()
  218. {
  219. bool executedInTransaction = false;
  220. AggregateRootTarget instance = null;
  221. var command = new TransactionalAggregateRootTargetCreateOrUpdateTitleCommand { Title = "TransactionalAggregateRootTargetUpdateTitleCommand" };
  222. var executor = new TestAttributeMappedCommandExecutor<AggregateRootTarget>(command, instance);
  223. executor.VerificationAction = () => executedInTransaction = Transaction.Current != null;
  224. executor.Execute();
  225. Assert.IsTrue(executedInTransaction);
  226. }
  227. [Test]
  228. public void Command_decorated_with_Transactional_attribute_mapped_to_methodOrCreator_should_be_executed_in_context_of_transaction()
  229. {
  230. bool executedInTransaction = false;
  231. var instance = new AggregateRootTarget("TitleSetInConstructor");
  232. var command = new TransactionalAggregateRootTargetCreateOrUpdateTitleCommand { Title = "TransactionalAggregateRootTargetUpdateTitleCommand" };
  233. var executor = new TestAttributeMappedCommandExecutor<AggregateRootTarget>(command, instance);
  234. executor.VerificationAction = () => executedInTransaction = Transaction.Current != null;
  235. executor.Execute();
  236. Assert.IsTrue(executedInTransaction);
  237. }
  238. [Test]
  239. public void Command_decorated_with_Transactional_attribute_mapped_to_method_should_be_executed_in_context_of_transaction()
  240. {
  241. bool executedInTransaction = false;
  242. var instance = new AggregateRootTarget("TitleSetInConstructor");
  243. var command = new TransactionalAggregateRootTargetUpdateTitleCommand { Title = "TransactionalAggregateRootTargetUpdateTitleCommand" };
  244. var executor = new TestAttributeMappedCommandExecutor<AggregateRootTarget>(command, instance);
  245. executor.VerificationAction = () => executedInTransaction = Transaction.Current != null;
  246. executor.Execute();
  247. Assert.IsTrue(executedInTransaction);
  248. }
  249. [Test]
  250. public void Command_decorated_with_Transactional_attribute_mapped_to_constructor_should_be_executed_in_context_of_transaction()
  251. {
  252. bool executedInTransaction = false;
  253. var command = new TransactionalAggregateRootTargetCreateNewCommand { Title = "TransactionalAggregateRootTargetCreateNewCommand" };
  254. var executor = new TestAttributeMappedCommandExecutor<AggregateRootTarget>(command);
  255. executor.VerificationAction = () => executedInTransaction = Transaction.Current != null;
  256. executor.Execute();
  257. Assert.IsTrue(executedInTransaction);
  258. }
  259. [Test]
  260. public void Command_should_throw_an_exception_when_the_command_is_not_mapped()
  261. {
  262. var command = new AggregateRootTargetNotAMappedCommand { Title = "AggregateRootTargetNotAMappedCommand" };
  263. var executor = new TestAttributeMappedCommandExecutor<AggregateRoot>(command);
  264. Action act = executor.Execute;
  265. act.ShouldThrow<CommandMappingException>();
  266. }
  267. [Test]
  268. public void Command_should_create_new_aggregate_root()
  269. {
  270. var command = new AggregateRootTargetCreateNewCommand { Title = "AggregateRootTargetCreateNewCommand" };
  271. var executor = new TestAttributeMappedCommandExecutor<AggregateRootTarget>(command);
  272. executor.Execute();
  273. executor.Instance.Title.Should().Be("AggregateRootTargetCreateNewCommand");
  274. }
  275. [Test]
  276. public void Command_should_create_new_complex_aggregate_root_using_ordinal_parameter_mappings()
  277. {
  278. var command = new ComplexAggregateRootTargetCreateNewCommand1 { Title = "ComplexAggregateRootTargetCreateNewCommand1", Quantity = 10 };
  279. var executor = new TestAttributeMappedCommandExecutor<ComplexAggregateRootTarget>(command);
  280. executor.Execute();
  281. executor.Instance.Title.Should().Be("ComplexAggregateRootTargetCreateNewCommand1");
  282. executor.Instance.Quantity.Should().Be(10);
  283. }
  284. [Test]
  285. public void Command_should_create_new_complex_aggregate_root_using_name_parameter_mappings()
  286. {
  287. var command = new ComplexAggregateRootTargetCreateNewCommand2 { Title = "ComplexAggregateRootTargetCreateNewCommand2", Quantity = 20 };
  288. var executor = new TestAttributeMappedCommandExecutor<ComplexAggregateRootTarget>(command);
  289. executor.Execute();
  290. executor.Instance.Title.Should().Be("ComplexAggregateRootTargetCreateNewCommand2");
  291. executor.Instance.Quantity.Should().Be(20);
  292. }
  293. [Test]
  294. public void Command_should_create_new_complex_aggregate_root_using_mixed_parameter_mappings()
  295. {
  296. var command = new ComplexAggregateRootTargetCreateNewCommand3 { Title = "ComplexAggregateRootTargetCreateNewCommand3", Quantity = 30 };
  297. var executor = new TestAttributeMappedCommandExecutor<ComplexAggregateRootTarget>(command);
  298. executor.Execute();
  299. executor.Instance.Title.Should().Be("ComplexAggregateRootTargetCreateNewCommand3");
  300. executor.Instance.Quantity.Should().Be(30);
  301. }
  302. [Test]
  303. public void Command_should_create_new_complex_aggregate_root_using_implicit_parameter_mappings()
  304. {
  305. var command = new ComplexAggregateRootTargetCreateNewCommand4 { Title = "ComplexAggregateRootTargetCreateNewCommand4", Quantity = 40 };
  306. var executor = new TestAttributeMappedCommandExecutor<ComplexAggregateRootTarget>(command);
  307. executor.Execute();
  308. executor.Instance.Title.Should().Be("ComplexAggregateRootTargetCreateNewCommand4");
  309. executor.Instance.Quantity.Should().Be(40);
  310. }
  311. }
  312. }