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

/src/NEventStore.Tests/ConversionTests/EventUpconverterPipelineHookTests.cs

http://github.com/joliver/EventStore
C# | 249 lines | 210 code | 34 blank | 5 comment | 1 complexity | fda949cb291f82080d9bc5c721c4d05f MD5 | raw file
  1. namespace NEventStore.ConversionTests
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using NEventStore.Conversion;
  8. using NEventStore.Persistence;
  9. using NEventStore.Persistence.AcceptanceTests.BDD;
  10. using FluentAssertions;
  11. #if MSTEST
  12. using Microsoft.VisualStudio.TestTools.UnitTesting;
  13. #endif
  14. #if NUNIT
  15. using NUnit.Framework;
  16. #endif
  17. #if XUNIT
  18. using Xunit;
  19. using Xunit.Should;
  20. #endif
  21. #if MSTEST
  22. [TestClass]
  23. #endif
  24. public class when_opening_a_commit_that_does_not_have_convertible_events : using_event_converter
  25. {
  26. private ICommit _commit;
  27. private ICommit _converted;
  28. protected override void Context()
  29. {
  30. _commit = CreateCommit(new EventMessage { Body = new NonConvertingEvent() });
  31. }
  32. protected override void Because()
  33. {
  34. _converted = EventUpconverter.Select(_commit);
  35. }
  36. [Fact]
  37. public void should_not_be_converted()
  38. {
  39. _converted.Should().BeSameAs(_commit);
  40. }
  41. [Fact]
  42. public void should_have_the_same_instance_of_the_event()
  43. {
  44. _converted.Events.Single().Should().Be(_commit.Events.Single());
  45. }
  46. }
  47. #if MSTEST
  48. [TestClass]
  49. #endif
  50. public class when_opening_a_commit_that_has_convertible_events : using_event_converter
  51. {
  52. private ICommit _commit;
  53. private readonly Guid _id = Guid.NewGuid();
  54. private ICommit _converted;
  55. protected override void Context()
  56. {
  57. _commit = CreateCommit(new EventMessage { Body = new ConvertingEvent(_id) });
  58. }
  59. protected override void Because()
  60. {
  61. _converted = EventUpconverter.Select(_commit);
  62. }
  63. [Fact]
  64. public void should_be_of_the_converted_type()
  65. {
  66. _converted.Events.Single().Body.GetType().Should().Be(typeof(ConvertingEvent3));
  67. }
  68. [Fact]
  69. public void should_have_the_same_id_of_the_commited_event()
  70. {
  71. ((ConvertingEvent3)_converted.Events.Single().Body).Id.Should().Be(_id);
  72. }
  73. }
  74. // ReSharper disable InconsistentNaming
  75. #if MSTEST
  76. [TestClass]
  77. #endif
  78. public class when_an_event_converter_implements_the_IConvertEvents_interface_explicitly : using_event_converter
  79. // ReSharper restore InconsistentNaming
  80. {
  81. private ICommit _commit;
  82. private readonly Guid _id = Guid.NewGuid();
  83. private ICommit _converted;
  84. private EventMessage _eventMessage;
  85. protected override void Context()
  86. {
  87. _eventMessage = new EventMessage { Body = new ConvertingEvent2(_id, "FooEvent") };
  88. _commit = CreateCommit(_eventMessage);
  89. }
  90. protected override void Because()
  91. {
  92. _converted = EventUpconverter.Select(_commit);
  93. }
  94. [Fact]
  95. public void should_be_of_the_converted_type()
  96. {
  97. _converted.Events.Single().Body.GetType().Should().Be(typeof(ConvertingEvent3));
  98. }
  99. [Fact]
  100. public void should_have_the_same_id_of_the_commited_event()
  101. {
  102. ((ConvertingEvent3)_converted.Events.Single().Body).Id.Should().Be(_id);
  103. }
  104. }
  105. public abstract class using_event_converter : SpecificationBase
  106. {
  107. private IEnumerable<Assembly> _assemblies;
  108. private Dictionary<Type, Func<object, object>> _converters;
  109. private EventUpconverterPipelineHook _eventUpconverter;
  110. protected EventUpconverterPipelineHook EventUpconverter
  111. {
  112. get { return _eventUpconverter ?? (_eventUpconverter = CreateUpConverterHook()); }
  113. }
  114. private EventUpconverterPipelineHook CreateUpConverterHook()
  115. {
  116. _assemblies = GetAllAssemblies();
  117. _converters = GetConverters(_assemblies);
  118. return new EventUpconverterPipelineHook(_converters);
  119. }
  120. private Dictionary<Type, Func<object, object>> GetConverters(IEnumerable<Assembly> toScan)
  121. {
  122. IEnumerable<KeyValuePair<Type, Func<object, object>>> c = from a in toScan
  123. from t in a.GetTypes()
  124. #if !NETSTANDARD1_6
  125. let i = t.GetInterface(typeof (IUpconvertEvents<,>).FullName)
  126. #else
  127. let i = t.GetTypeInfo().GetInterface(typeof(IUpconvertEvents<,>).FullName)
  128. #endif
  129. where i != null
  130. let sourceType = i.GetGenericArguments().First()
  131. let convertMethod = i.GetMethods(BindingFlags.Public | BindingFlags.Instance).First()
  132. let instance = Activator.CreateInstance(t)
  133. select new KeyValuePair<Type, Func<object, object>>(sourceType, e => convertMethod.Invoke(instance, new[] { e }));
  134. try
  135. {
  136. return c.ToDictionary(x => x.Key, x => x.Value);
  137. }
  138. catch (ArgumentException ex)
  139. {
  140. throw new MultipleConvertersFoundException(ex.Message, ex);
  141. }
  142. }
  143. private IEnumerable<Assembly> GetAllAssemblies()
  144. {
  145. #if !NETSTANDARD1_6
  146. return
  147. Assembly.GetCallingAssembly().GetReferencedAssemblies().Select(Assembly.Load).Concat(new[] {Assembly.GetCallingAssembly()});
  148. #else
  149. // in .netcore we return the current assembly, instead of looking at all the assemblies in the folder
  150. // GetCallingAssembly is not supported
  151. return new[] { typeof(ConvertingEventConverter).GetTypeInfo().Assembly };
  152. #endif
  153. }
  154. protected static ICommit CreateCommit(EventMessage eventMessage)
  155. {
  156. return new Commit(Bucket.Default,
  157. Guid.NewGuid().ToString(),
  158. 1,
  159. Guid.NewGuid(),
  160. 1,
  161. DateTime.MinValue,
  162. 1,
  163. null,
  164. new[] { eventMessage });
  165. }
  166. }
  167. public class ConvertingEventConverter : IUpconvertEvents<ConvertingEvent, ConvertingEvent2>
  168. {
  169. public ConvertingEvent2 Convert(ConvertingEvent sourceEvent)
  170. {
  171. return new ConvertingEvent2(sourceEvent.Id, "Temp");
  172. }
  173. }
  174. public class ExplicitConvertingEventConverter : IUpconvertEvents<ConvertingEvent2, ConvertingEvent3>
  175. {
  176. ConvertingEvent3 IUpconvertEvents<ConvertingEvent2, ConvertingEvent3>.Convert(ConvertingEvent2 sourceEvent)
  177. {
  178. return new ConvertingEvent3(sourceEvent.Id, "Temp", true);
  179. }
  180. }
  181. public class NonConvertingEvent
  182. { }
  183. public class ConvertingEvent
  184. {
  185. public ConvertingEvent(Guid id)
  186. {
  187. Id = id;
  188. }
  189. public Guid Id { get; set; }
  190. }
  191. public class ConvertingEvent2
  192. {
  193. public ConvertingEvent2(Guid id, string name)
  194. {
  195. Id = id;
  196. Name = name;
  197. }
  198. public Guid Id { get; set; }
  199. public string Name { get; set; }
  200. }
  201. public class ConvertingEvent3
  202. {
  203. public ConvertingEvent3(Guid id, string name, bool imExplicit)
  204. {
  205. Id = id;
  206. Name = name;
  207. ImExplicit = imExplicit;
  208. }
  209. public Guid Id { get; set; }
  210. public string Name { get; set; }
  211. public bool ImExplicit { get; set; }
  212. }
  213. }
  214. // ReSharper enable InconsistentNaming
  215. #pragma warning restore 169