Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.Collections.Generic;
11 : using System.Threading;
12 : using cdmdotnet.Logging;
13 : using cdmdotnet.Logging.Configuration;
14 : using cdmdotnet.StateManagement;
15 : using cdmdotnet.StateManagement.Threaded;
16 : using cdmdotnet.StateManagement.Web;
17 : using Cqrs.Akka.Commands;
18 : using Cqrs.Akka.Domain;
19 : using Cqrs.Akka.Events;
20 : using Cqrs.Akka.Tests.Unit.Commands;
21 : using Cqrs.Akka.Tests.Unit.Commands.Handlers;
22 : using Cqrs.Akka.Tests.Unit.Events;
23 : using Cqrs.Akka.Tests.Unit.Events.Handlers;
24 : using Cqrs.Akka.Tests.Unit.Sagas;
25 : using Cqrs.Authentication;
26 : using Cqrs.Bus;
27 : using Cqrs.Commands;
28 : using Cqrs.Configuration;
29 : using Cqrs.Domain;
30 : using Cqrs.Domain.Factories;
31 : using Cqrs.Events;
32 : using Cqrs.Ninject.Akka;
33 : using Microsoft.VisualStudio.TestTools.UnitTesting;
34 : using Ninject;
35 :
36 : namespace Cqrs.Akka.Tests.Unit
37 : {
38 : /// <summary>
39 : /// A series of tests on Akka.net
40 : /// </summary>
41 : [TestClass]
42 : public class AkkaUnitTests
43 1 : {
44 : internal static IDictionary<Guid, bool> Step1Reached = new Dictionary<Guid, bool>();
45 : internal static IDictionary<Guid, bool> Step2Reached = new Dictionary<Guid, bool>();
46 : internal static IDictionary<Guid, bool> Step3Reached = new Dictionary<Guid, bool>();
47 : internal static IDictionary<Guid, bool> Step4Reached = new Dictionary<Guid, bool>();
48 : internal static IDictionary<Guid, bool> FinalCommandReached = new Dictionary<Guid, bool>();
49 :
50 : /// <summary>
51 : /// AkkaSystem_ATestSayHelloWorldCommand_5PointsAreReached
52 : /// </summary>
53 : [TestMethod]
54 1 : public void SendingCommandsAndEvents_AcrossBusesInMultipleWays_AllWork()
55 : {
56 : // Arrange
57 : var command = new SayHelloWorldCommand();
58 : Guid correlationId = Guid.NewGuid();
59 : ICorrelationIdHelper correlationIdHelper = new WebCorrelationIdHelper(new WebContextItemCollectionFactory());
60 : correlationIdHelper.SetCorrelationId(correlationId);
61 : ILogger logger = new ConsoleLogger(new LoggerSettings(), correlationIdHelper);
62 : IConfigurationManager configurationManager = new ConfigurationManager();
63 : IBusHelper busHelper = new BusHelper(configurationManager, new ThreadedContextItemCollectionFactory());
64 :
65 : var kernel = new StandardKernel();
66 : kernel.Bind<ILogger>().ToConstant(logger);
67 : kernel.Bind<IAggregateFactory>().To<AggregateFactory>().InSingletonScope();
68 : kernel.Bind<IUnitOfWork<Guid>>().To<UnitOfWork<Guid>>().InSingletonScope();
69 : kernel.Bind<ISagaUnitOfWork<Guid>>().To<SagaUnitOfWork<Guid>>().InSingletonScope();
70 : kernel.Bind<IAggregateRepository<Guid>>().To<AkkaAggregateRepository<Guid>>().InSingletonScope();
71 : kernel.Bind<IAkkaAggregateRepository<Guid>>().To<AkkaAggregateRepository<Guid>>().InSingletonScope();
72 : kernel.Bind<ISagaRepository<Guid>>().To<AkkaSagaRepository<Guid>>().InSingletonScope();
73 : kernel.Bind<IAkkaSagaRepository<Guid>>().To<AkkaSagaRepository<Guid>>().InSingletonScope();
74 : kernel.Bind<IEventStore<Guid>>().To<MemoryCacheEventStore<Guid>>().InSingletonScope();
75 : kernel.Bind<IEventBuilder<Guid>>().To<DefaultEventBuilder<Guid>>().InSingletonScope();
76 : kernel.Bind<IEventDeserialiser<Guid>>().To<EventDeserialiser<Guid>>().InSingletonScope();
77 : kernel.Bind<IEventPublisher<Guid>>().To<InProcessBus<Guid>>().InSingletonScope();
78 : kernel.Bind<IEventReceiver<Guid>>().To<InProcessBus<Guid>>().InSingletonScope();
79 : kernel.Bind<ICorrelationIdHelper>().ToConstant(correlationIdHelper).InSingletonScope();
80 : kernel.Bind<IAkkaEventPublisher<Guid>>().To<AkkaEventBus<Guid>>().InSingletonScope();
81 : kernel.Bind<IAkkaEventPublisherProxy<Guid>>().To<AkkaEventBusProxy<Guid>>().InSingletonScope();
82 : kernel.Bind<IAkkaCommandPublisher<Guid>>().To<AkkaCommandBus<Guid>>().InSingletonScope();
83 : kernel.Bind<ICommandHandlerRegistrar>().To<AkkaCommandBus<Guid>>().InSingletonScope();
84 : kernel.Bind<IEventHandlerRegistrar>().To<AkkaEventBus<Guid>>().InSingletonScope();
85 : kernel.Bind<ICommandPublisher<Guid>>().To<InProcessBus<Guid>>().InSingletonScope();
86 : kernel.Bind<ICommandReceiver<Guid>>().To<InProcessBus<Guid>>().InSingletonScope();
87 : kernel.Bind<IConfigurationManager>().ToConstant(configurationManager).InSingletonScope();
88 : kernel.Bind<IBusHelper>().ToConstant(busHelper).InSingletonScope();
89 : kernel.Bind<IAuthenticationTokenHelper<Guid>>().To<AuthenticationTokenHelper<Guid>>().InSingletonScope();
90 : kernel.Bind<IContextItemCollectionFactory>().To<WebContextItemCollectionFactory>().InSingletonScope();
91 :
92 : AkkaNinjectDependencyResolver.Start(kernel);
93 : var dependencyResolver = (AkkaNinjectDependencyResolver)DependencyResolver.Current;
94 :
95 : var commandBus = dependencyResolver.Resolve<ICommandHandlerRegistrar>();
96 : var eventBus = dependencyResolver.Resolve<IEventHandlerRegistrar>();
97 : var inProcessBus = dependencyResolver.Resolve<InProcessBus<Guid>>();
98 :
99 : var commandBusProxy = new AkkaCommandBusProxy<Guid>(dependencyResolver, correlationIdHelper, dependencyResolver.Resolve<IAuthenticationTokenHelper<Guid>>());
100 : // Commands handled by Akka.net
101 : commandBus.RegisterHandler<SayHelloWorldCommand>(new SayHelloWorldCommandHandler(dependencyResolver).Handle);
102 : commandBus.RegisterHandler<ReplyToHelloWorldCommand>(new ReplyToHelloWorldCommandHandler(dependencyResolver).Handle);
103 : commandBus.RegisterHandler<EndConversationCommand>(new EndConversationCommandHandler(dependencyResolver).Handle);
104 :
105 : // Commands handled in process
106 : inProcessBus.RegisterHandler<UpdateCompletedConversationReportCommand>(new UpdateCompletedConversationReportCommandHandler(dependencyResolver).Handle);
107 :
108 : // Events in process
109 : inProcessBus.RegisterHandler<HelloWorldSaid>(new HelloWorldSaidEventHandler(dependencyResolver.Resolve<IAkkaCommandPublisher<Guid>>()).Handle);
110 : inProcessBus.RegisterHandler<ConversationEnded>(new ConversationEndedEventHandler(dependencyResolver.Resolve<IAkkaCommandPublisher<Guid>>()).Handle);
111 :
112 : // events handled by Akka.net
113 : eventBus.RegisterHandler<HelloWorldRepliedTo>(new HelloWorldRepliedToEventHandler(dependencyResolver).Handle);
114 : eventBus.RegisterHandler<HelloWorldRepliedTo>(new HelloWorldRepliedToSendEndConversationCommandEventHandler(dependencyResolver).Handle);
115 :
116 : var handler = new ConversationReportProcessManagerEventHandlers(dependencyResolver);
117 : eventBus.RegisterHandler<HelloWorldSaid>(handler.Handle);
118 : eventBus.RegisterHandler<ConversationEnded>(handler.Handle);
119 : eventBus.RegisterHandler<HelloWorldRepliedTo>(handler.Handle);
120 :
121 : Step1Reached.Add(correlationId, false);
122 : Step2Reached.Add(correlationId, false);
123 : Step3Reached.Add(correlationId, false);
124 : Step4Reached.Add(correlationId, false);
125 : FinalCommandReached.Add(correlationId, false);
126 :
127 : // Act
128 : commandBusProxy.Publish(command);
129 :
130 : // Assert
131 : SpinWait.SpinUntil
132 : (
133 : () =>
134 : Step1Reached[correlationId] &&
135 : Step2Reached[correlationId] &&
136 : Step3Reached[correlationId] &&
137 : Step4Reached[correlationId] &&
138 : FinalCommandReached[correlationId]
139 : );
140 :
141 : AkkaNinjectDependencyResolver.Stop();
142 : }
143 : }
144 : }
|