PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

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