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.Akka.Tests.Unit.Sagas;
16 : using Cqrs.Authentication;
17 : using Cqrs.Bus;
18 : using Cqrs.Commands;
19 : using Cqrs.Configuration;
20 : using Cqrs.Domain;
21 : using Cqrs.Domain.Factories;
22 : using Cqrs.Events;
23 : using Cqrs.Ninject.Akka;
24 : using Cqrs.Ninject.Configuration;
25 : using Microsoft.VisualStudio.TestTools.UnitTesting;
26 : using Ninject;
27 :
28 : namespace Cqrs.Akka.Tests.Unit
29 : {
30 : [TestClass]
31 : public class AkkaUnitTests
32 0 : {
33 : internal static IDictionary<Guid, bool> Step1Reached = new Dictionary<Guid, bool>();
34 : internal static IDictionary<Guid, bool> Step2Reached = new Dictionary<Guid, bool>();
35 : internal static IDictionary<Guid, bool> Step3Reached = new Dictionary<Guid, bool>();
36 : internal static IDictionary<Guid, bool> Step4Reached = new Dictionary<Guid, bool>();
37 : internal static IDictionary<Guid, bool> FinalCommandReached = new Dictionary<Guid, bool>();
38 :
39 : [TestMethod]
40 0 : public void SendingCommandsAndEvents_AcrossBusesInMultipleWays_AllWork()
41 : {
42 : // Arrange
43 : var command = new SayHelloWorldCommand();
44 : Guid correlationId = Guid.NewGuid();
45 : ICorrelationIdHelper correlationIdHelper = new WebCorrelationIdHelper(new WebContextItemCollectionFactory());
46 : correlationIdHelper.SetCorrelationId(correlationId);
47 : ILogger logger = new ConsoleLogger(new LoggerSettings(), correlationIdHelper);
48 : IConfigurationManager configurationManager = new ConfigurationManager();
49 : IBusHelper busHelper = new BusHelper(configurationManager);
50 :
51 : var kernel = new StandardKernel();
52 : kernel.Bind<ILogger>().ToConstant(logger);
53 : kernel.Bind<IAggregateFactory>().To<AggregateFactory>().InSingletonScope();
54 : kernel.Bind<IUnitOfWork<Guid>>().To<UnitOfWork<Guid>>().InSingletonScope();
55 : kernel.Bind<ISagaUnitOfWork<Guid>>().To<SagaUnitOfWork<Guid>>().InSingletonScope();
56 : kernel.Bind<IAggregateRepository<Guid>>().To<AkkaAggregateRepository<Guid>>().InSingletonScope();
57 : kernel.Bind<IAkkaAggregateRepository<Guid>>().To<AkkaAggregateRepository<Guid>>().InSingletonScope();
58 : kernel.Bind<ISagaRepository<Guid>>().To<AkkaSagaRepository<Guid>>().InSingletonScope();
59 : kernel.Bind<IAkkaSagaRepository<Guid>>().To<AkkaSagaRepository<Guid>>().InSingletonScope();
60 : kernel.Bind<IEventStore<Guid>>().To<MemoryCacheEventStore<Guid>>().InSingletonScope();
61 : kernel.Bind<IEventBuilder<Guid>>().To<DefaultEventBuilder<Guid>>().InSingletonScope();
62 : kernel.Bind<IEventDeserialiser<Guid>>().To<EventDeserialiser<Guid>>().InSingletonScope();
63 : kernel.Bind<IEventPublisher<Guid>>().To<InProcessBus<Guid>>().InSingletonScope();
64 : kernel.Bind<IEventReceiver<Guid>>().To<InProcessBus<Guid>>().InSingletonScope();
65 : kernel.Bind<ICorrelationIdHelper>().ToConstant(correlationIdHelper).InSingletonScope();
66 : kernel.Bind<IAkkaEventPublisher<Guid>>().To<AkkaEventBus<Guid>>().InSingletonScope();
67 : kernel.Bind<IAkkaEventPublisherProxy<Guid>>().To<AkkaEventBusProxy<Guid>>().InSingletonScope();
68 : kernel.Bind<IAkkaCommandSender<Guid>>().To<AkkaCommandBus<Guid>>().InSingletonScope();
69 : kernel.Bind<ICommandHandlerRegistrar>().To<AkkaCommandBus<Guid>>().InSingletonScope();
70 : kernel.Bind<IEventHandlerRegistrar>().To<AkkaEventBus<Guid>>().InSingletonScope();
71 : kernel.Bind<ICommandSender<Guid>>().To<InProcessBus<Guid>>().InSingletonScope();
72 : kernel.Bind<ICommandPublisher<Guid>>().To<InProcessBus<Guid>>().InSingletonScope();
73 : kernel.Bind<ICommandReceiver<Guid>>().To<InProcessBus<Guid>>().InSingletonScope();
74 : kernel.Bind<IConfigurationManager>().ToConstant(configurationManager).InSingletonScope();
75 : kernel.Bind<IBusHelper>().ToConstant(busHelper).InSingletonScope();
76 : kernel.Bind<IAuthenticationTokenHelper<Guid>>().To<AuthenticationTokenHelper<Guid>>().InSingletonScope();
77 : kernel.Bind<IContextItemCollectionFactory>().To<WebContextItemCollectionFactory>().InSingletonScope();
78 :
79 : AkkaNinjectDependencyResolver.Start(kernel);
80 : var dependencyResolver = (AkkaNinjectDependencyResolver)NinjectDependencyResolver.Current;
81 :
82 : var commandBus = dependencyResolver.Resolve<ICommandHandlerRegistrar>();
83 : var eventBus = dependencyResolver.Resolve<IEventHandlerRegistrar>();
84 : var inProcessBus = dependencyResolver.Resolve<InProcessBus<Guid>>();
85 :
86 : var commandBusProxy = new AkkaCommandBusProxy<Guid>(dependencyResolver, correlationIdHelper, dependencyResolver.Resolve<IAuthenticationTokenHelper<Guid>>());
87 : // Commands handled by Akka.net
88 : commandBus.RegisterHandler<SayHelloWorldCommand>(new SayHelloWorldCommandHandler(dependencyResolver).Handle);
89 : commandBus.RegisterHandler<ReplyToHelloWorldCommand>(new ReplyToHelloWorldCommandHandler(dependencyResolver).Handle);
90 : commandBus.RegisterHandler<EndConversationCommand>(new EndConversationCommandHandler(dependencyResolver).Handle);
91 :
92 : // Commands handled in process
93 : inProcessBus.RegisterHandler<UpdateCompletedConversationReportCommand>(new UpdateCompletedConversationReportCommandHandler(dependencyResolver).Handle);
94 :
95 : // Events in process
96 : inProcessBus.RegisterHandler<HelloWorldSaid>(new HelloWorldSaidEventHandler(dependencyResolver.Resolve<IAkkaCommandSender<Guid>>()).Handle);
97 : inProcessBus.RegisterHandler<ConversationEnded>(new ConversationEndedEventHandler(dependencyResolver.Resolve<IAkkaCommandSender<Guid>>()).Handle);
98 :
99 : // events handled by Akka.net
100 : eventBus.RegisterHandler<HelloWorldRepliedTo>(new HelloWorldRepliedToEventHandler(dependencyResolver).Handle);
101 : eventBus.RegisterHandler<HelloWorldRepliedTo>(new HelloWorldRepliedToSendEndConversationCommandEventHandler(dependencyResolver).Handle);
102 :
103 : var handler = new ConversationReportProcessManagerEventHandlers(dependencyResolver);
104 : eventBus.RegisterHandler<HelloWorldSaid>(handler.Handle);
105 : eventBus.RegisterHandler<ConversationEnded>(handler.Handle);
106 : eventBus.RegisterHandler<HelloWorldRepliedTo>(handler.Handle);
107 :
108 : Step1Reached.Add(correlationId, false);
109 : Step2Reached.Add(correlationId, false);
110 : Step3Reached.Add(correlationId, false);
111 : Step4Reached.Add(correlationId, false);
112 : FinalCommandReached.Add(correlationId, false);
113 :
114 : // Act
115 : commandBusProxy.Send(command);
116 :
117 : // Assert
118 : SpinWait.SpinUntil
119 : (
120 : () =>
121 : Step1Reached[correlationId] &&
122 : Step2Reached[correlationId] &&
123 : Step3Reached[correlationId] &&
124 : Step4Reached[correlationId] &&
125 : FinalCommandReached[correlationId]
126 : );
127 :
128 : AkkaNinjectDependencyResolver.Stop();
129 : }
130 : }
131 : }
|