Line data Source code
1 : using System;
2 : using System.Collections.Generic;
3 : using System.Threading;
4 : using cdmdotnet.Logging;
5 : using cdmdotnet.Logging.Configuration;
6 : using cdmdotnet.StateManagement;
7 : using cdmdotnet.StateManagement.Web;
8 : using Cqrs.Akka.Commands;
9 : using Cqrs.Akka.Domain;
10 : using Cqrs.Akka.Events;
11 : using Cqrs.Akka.Tests.Unit.Commands;
12 : using Cqrs.Akka.Tests.Unit.Commands.Handlers;
13 : using Cqrs.Akka.Tests.Unit.Events;
14 : using Cqrs.Akka.Tests.Unit.Events.Handlers;
15 : using Cqrs.Authentication;
16 : using Cqrs.Bus;
17 : using Cqrs.Commands;
18 : using Cqrs.Configuration;
19 : using Cqrs.Domain;
20 : using Cqrs.Domain.Factories;
21 : using Cqrs.Events;
22 : using Cqrs.Ninject.Akka;
23 : using Cqrs.Ninject.Configuration;
24 : using Microsoft.VisualStudio.TestTools.UnitTesting;
25 : using Ninject;
26 :
27 : namespace Cqrs.Akka.Tests.Unit
28 : {
29 : [TestClass]
30 : public class AkkaUnitTests
31 0 : {
32 : internal static IDictionary<Guid, bool> Step1Reached = new Dictionary<Guid, bool>();
33 : internal static IDictionary<Guid, bool> Step2Reached = new Dictionary<Guid, bool>();
34 : internal static IDictionary<Guid, bool> Step3Reached = new Dictionary<Guid, bool>();
35 : internal static IDictionary<Guid, bool> Step4Reached = new Dictionary<Guid, bool>();
36 :
37 : [TestMethod]
38 0 : public void SendingCommandsAndEvents_AcrossBusesInMultipleWays_AllWork()
39 : {
40 : // Arrange
41 : var command = new SayHelloWorldCommand();
42 : Guid correlationId = Guid.NewGuid();
43 : ICorrelationIdHelper correlationIdHelper = new WebCorrelationIdHelper(new WebContextItemCollectionFactory());
44 : correlationIdHelper.SetCorrelationId(correlationId);
45 : ILogger logger = new ConsoleLogger(new LoggerSettings(), correlationIdHelper);
46 : IConfigurationManager configurationManager = new ConfigurationManager();
47 : IBusHelper busHelper = new BusHelper(configurationManager);
48 :
49 : var kernel = new StandardKernel();
50 : kernel.Bind<ILogger>().ToConstant(logger);
51 : kernel.Bind<IAggregateFactory>().To<AggregateFactory>().InSingletonScope();
52 : kernel.Bind<IUnitOfWork<Guid>>().To<UnitOfWork<Guid>>().InSingletonScope();
53 : kernel.Bind<IRepository<Guid>>().To<AkkaRepository<Guid>>().InSingletonScope();
54 : kernel.Bind<IAkkaRepository<Guid>>().To<AkkaRepository<Guid>>().InSingletonScope();
55 : kernel.Bind<IEventStore<Guid>>().To<MemoryCacheEventStore<Guid>>().InSingletonScope();
56 : kernel.Bind<IEventBuilder<Guid>>().To<DefaultEventBuilder<Guid>>().InSingletonScope();
57 : kernel.Bind<IEventDeserialiser<Guid>>().To<EventDeserialiser<Guid>>().InSingletonScope();
58 : kernel.Bind<IEventPublisher<Guid>>().To<InProcessBus<Guid>>().InSingletonScope();
59 : kernel.Bind<IEventReceiver<Guid>>().To<InProcessBus<Guid>>().InSingletonScope();
60 : kernel.Bind<ICorrelationIdHelper>().ToConstant(correlationIdHelper).InSingletonScope();
61 : kernel.Bind<IAkkaEventPublisher<Guid>>().To<AkkaEventBus<Guid>>().InSingletonScope();
62 : kernel.Bind<IAkkaEventPublisherProxy<Guid>>().To<AkkaEventBusProxy<Guid>>().InSingletonScope();
63 : kernel.Bind<IAkkaCommandSender<Guid>>().To<AkkaCommandBus<Guid>>().InSingletonScope();
64 : kernel.Bind<ICommandHandlerRegistrar>().To<AkkaCommandBus<Guid>>().InSingletonScope();
65 : kernel.Bind<IEventHandlerRegistrar>().To<AkkaEventBus<Guid>>().InSingletonScope();
66 : kernel.Bind<ICommandSender<Guid>>().To<InProcessBus<Guid>>().InSingletonScope();
67 : kernel.Bind<ICommandPublisher<Guid>>().To<InProcessBus<Guid>>().InSingletonScope();
68 : kernel.Bind<ICommandReceiver<Guid>>().To<InProcessBus<Guid>>().InSingletonScope();
69 : kernel.Bind<IConfigurationManager>().ToConstant(configurationManager).InSingletonScope();
70 : kernel.Bind<IBusHelper>().ToConstant(busHelper).InSingletonScope();
71 : kernel.Bind<IAuthenticationTokenHelper<Guid>>().To<AuthenticationTokenHelper<Guid>>().InSingletonScope();
72 : kernel.Bind<IContextItemCollectionFactory>().To<WebContextItemCollectionFactory>().InSingletonScope();
73 :
74 : AkkaNinjectDependencyResolver.Start(kernel);
75 : var dependencyResolver = (AkkaNinjectDependencyResolver)NinjectDependencyResolver.Current;
76 :
77 : var commandBus = dependencyResolver.Resolve<ICommandHandlerRegistrar>();
78 : var eventBus = dependencyResolver.Resolve<IEventHandlerRegistrar>();
79 : var inProcessBus = dependencyResolver.Resolve<InProcessBus<Guid>>();
80 :
81 : var commandBusProxy = new AkkaCommandBusProxy<Guid>(dependencyResolver, correlationIdHelper, dependencyResolver.Resolve<IAuthenticationTokenHelper<Guid>>());
82 : // Commands handled by Akka.net
83 : commandBus.RegisterHandler<SayHelloWorldCommand>(new SayHelloWorldCommandHandler(dependencyResolver).Handle);
84 : commandBus.RegisterHandler<ReplyToHelloWorldCommand>(new ReplyToHelloWorldCommandHandler(dependencyResolver).Handle);
85 : commandBus.RegisterHandler<EndConversationCommand>(new EndConversationCommandHandler(dependencyResolver).Handle);
86 :
87 : // Events in process
88 : inProcessBus.RegisterHandler<HelloWorldSaid>(new HelloWorldSaidEventHandler(dependencyResolver.Resolve<IAkkaCommandSender<Guid>>()).Handle);
89 : inProcessBus.RegisterHandler<ConversationEnded>(new ConversationEndedEventHandler(dependencyResolver.Resolve<IAkkaCommandSender<Guid>>()).Handle);
90 :
91 : // events handled by Akka.net
92 : eventBus.RegisterHandler<HelloWorldRepliedTo>(new HelloWorldRepliedToEventHandler(dependencyResolver).Handle);
93 : eventBus.RegisterHandler<HelloWorldRepliedTo>(new HelloWorldRepliedToSendEndConversationCommandEventHandler(dependencyResolver).Handle);
94 :
95 : Step1Reached.Add(correlationId, false);
96 : Step2Reached.Add(correlationId, false);
97 : Step3Reached.Add(correlationId, false);
98 : Step4Reached.Add(correlationId, false);
99 :
100 : // Act
101 : commandBusProxy.Send(command);
102 :
103 : // Assert
104 : SpinWait.SpinUntil(() => Step1Reached[correlationId] && Step2Reached[correlationId] && Step3Reached[correlationId] && Step4Reached[correlationId]);
105 :
106 : AkkaNinjectDependencyResolver.Stop();
107 : }
108 : }
109 : }
|