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