Line data Source code
1 : using System;
2 : using cdmdotnet.Logging;
3 : using Cqrs.Domain;
4 : using Cqrs.Authentication;
5 : using Cqrs.Configuration;
6 : using Cqrs.Events;
7 :
8 : namespace Cqrs.Tests.Substitutes
9 : {
10 : public class TestSagaEventHandlers
11 : : SagaEventHandler<ISingleSignOnToken, TestSaga>
12 : , IEventHandler<ISingleSignOnToken, TestAggregateDidSomething>
13 : , IEventHandler<ISingleSignOnToken, TestAggregateDidSomethingElse>
14 : , IEventHandler<ISingleSignOnToken, TestAggregateDidSomethingElse2>
15 0 : {
16 : public int TimesRun { get; set; }
17 :
18 0 : public TestSagaEventHandlers(IDependencyResolver dependencyResolver, ILogger logger, ISagaUnitOfWork<ISingleSignOnToken> sagaUnitOfWork) : base(dependencyResolver, logger, sagaUnitOfWork)
19 : {
20 : }
21 :
22 : /// <summary>
23 : /// A constructor for the <see cref="Cqrs.Domain.Factories.IAggregateFactory"/>
24 : /// </summary>
25 1 : public TestSagaEventHandlers(IDependencyResolver dependencyResolver, ILogger logger) : base(dependencyResolver, logger)
26 : {
27 : }
28 :
29 : #region Implementation of IMessageHandler<in TestAggregateDidSomething>
30 :
31 0 : public void Handle(TestAggregateDidSomething message)
32 : {
33 : // There are two ways for this to pan out.
34 : // 1) Events WILL arrive in order in which case this handler would ADD TO and all others would GET FROM the UOW
35 : // 2) Events may not arrive in order in which case all handlers should try to GET FROM and if it fails ADD TO the UOW
36 : // Given this is a test, we'll code for the first.
37 :
38 : var saga = new TestSaga(DependencyResolver, message.Id == Guid.Empty ? Guid.NewGuid() : message.Id);
39 : saga.Handle(message);
40 : SagaUnitOfWork.Add(saga);
41 : SagaUnitOfWork.Commit();
42 :
43 : TimesRun++;
44 : }
45 :
46 : #endregion
47 :
48 : #region Implementation of IMessageHandler<in TestAggregateDidSomethingElse>
49 :
50 0 : public void Handle(TestAggregateDidSomethingElse message)
51 : {
52 : TestSaga saga = GetSaga(message.Id);
53 : saga.Handle(message);
54 : SagaUnitOfWork.Add(saga);
55 : SagaUnitOfWork.Commit();
56 :
57 : TimesRun++;
58 : }
59 :
60 : #endregion
61 :
62 : #region Implementation of IMessageHandler<in TestAggregateDidSomethingElse2>
63 :
64 0 : public void Handle(TestAggregateDidSomethingElse2 message)
65 : {
66 : TestSaga saga = GetSaga(message.Id);
67 : saga.Handle(message);
68 : SagaUnitOfWork.Add(saga);
69 : SagaUnitOfWork.Commit();
70 :
71 : TimesRun++;
72 : }
73 :
74 : #endregion
75 : }
76 :
77 : public class TestSaga : Saga<ISingleSignOnToken>
78 :
79 0 : {
80 : public int DidSomethingCount;
81 :
82 : public bool DidSomething;
83 :
84 : public bool DidSomethingElse;
85 :
86 : public bool DidSomethingElse2;
87 :
88 : public bool Responded;
89 :
90 : private TestSaga(IDependencyResolver dependencyResolver)
91 : : base(dependencyResolver, dependencyResolver.Resolve<ILogger>())
92 : {
93 : ApplyChange(new SagaEvent<ISingleSignOnToken>(new TestAggregateCreated()));
94 : Version++;
95 : }
96 :
97 0 : public TestSaga(IDependencyResolver dependencyResolver, Guid id)
98 : : base(dependencyResolver, dependencyResolver.Resolve<ILogger>())
99 : {
100 : Id = id;
101 : ApplyChange(new SagaEvent<ISingleSignOnToken>(new TestAggregateCreated()));
102 : Version++;
103 : }
104 :
105 : #region Implementation of IMessageHandler<in TestAggregateDidSomething>
106 :
107 0 : public void Handle(TestAggregateDidSomething message)
108 : {
109 : // This could happen out of order in this test
110 : if (DidSomethingElse2)
111 : {
112 : CommandPublisher.Publish(new TestAggregateDoSomething3());
113 : // This is a testing variable
114 : Responded = true;
115 : }
116 : ApplyChange(message);
117 : }
118 :
119 : #endregion
120 :
121 : #region Implementation of IMessageHandler<in TestAggregateDidSomethingElse>
122 :
123 0 : public void Handle(TestAggregateDidSomethingElse message)
124 : {
125 : ApplyChange(message);
126 : }
127 :
128 : #endregion
129 :
130 : #region Implementation of IMessageHandler<in TestAggregateDidSomethingElse>
131 :
132 0 : public void Handle(TestAggregateDidSomethingElse2 message)
133 : {
134 : // This could happen out of order in this test
135 : if (DidSomething)
136 : {
137 : CommandPublisher.Publish(new TestAggregateDoSomething3());
138 : // This is a testing variable
139 : Responded = true;
140 : }
141 : ApplyChange(message);
142 : }
143 :
144 : #endregion
145 :
146 0 : public void Apply(TestAggregateDidSomething e)
147 : {
148 : DidSomethingCount++;
149 : DidSomething = true;
150 : }
151 :
152 0 : public void Apply(TestAggregateDidSomethingElse e)
153 : {
154 : DidSomethingElse = true;
155 : }
156 : }
157 : }
|