Line data Source code
1 : using System;
2 : using System.Collections.Generic;
3 : using System.Linq;
4 : using Cqrs.Authentication;
5 : using Cqrs.Domain;
6 :
7 : namespace Cqrs.Tests.Substitutes
8 : {
9 : public class TestSagaUnitOfWork
10 : : ISagaUnitOfWork<ISingleSignOnToken>
11 : , ISagaUnitOfWork<Guid>
12 0 : {
13 : public static IDictionary<Guid, Tuple<ISaga<Guid>, int, int, int>> TestGuidSagasWithAddCountGetCountAndCommitCount { get; set; }
14 :
15 : public static IDictionary<Guid, Tuple<ISaga<ISingleSignOnToken>, int, int, int>> TestSagasWithAddCountGetCountAndCommitCount { get; set; }
16 :
17 : static TestSagaUnitOfWork()
18 : {
19 : TestGuidSagasWithAddCountGetCountAndCommitCount = new Dictionary<Guid, Tuple<ISaga<Guid>, int, int, int>>();
20 : TestSagasWithAddCountGetCountAndCommitCount = new Dictionary<Guid, Tuple<ISaga<ISingleSignOnToken>, int, int, int>>();
21 : }
22 :
23 : #region Implementation of ISagaUnitOfWork<Guid>
24 :
25 : /// <summary>
26 : /// Add an item into the <see cref="ISagaUnitOfWork{TAuthenticationToken}"/> ready to be committed.
27 : /// </summary>
28 1 : public void Add<TSaga>(TSaga saga)
29 : where TSaga : ISaga<Guid>
30 : {
31 : Tuple<ISaga<Guid>, int, int, int> testSaga;
32 : if (!TestGuidSagasWithAddCountGetCountAndCommitCount.TryGetValue(saga.Id, out testSaga))
33 : testSaga = new Tuple<ISaga<Guid>, int, int, int>(saga, 1, 0, 0);
34 : else
35 : testSaga = new Tuple<ISaga<Guid>, int, int, int>(saga, testSaga.Item2 + 1, testSaga.Item3, testSaga.Item4);
36 : TestGuidSagasWithAddCountGetCountAndCommitCount[saga.Id] = testSaga;
37 : }
38 :
39 : /// <summary>
40 : /// Get an item from the <see cref="ISagaUnitOfWork{TAuthenticationToken}"/> if it has already been loaded.
41 : /// </summary>
42 1 : public TSaga Get<TSaga>(Guid id, int? expectedVersion = null)
43 : where TSaga : ISaga<Guid>
44 : {
45 : Tuple<ISaga<Guid>, int, int, int> testSaga = TestGuidSagasWithAddCountGetCountAndCommitCount[id];
46 : testSaga = new Tuple<ISaga<Guid>, int, int, int>(testSaga.Item1, testSaga.Item2, testSaga.Item3 + 1, testSaga.Item4);
47 : TestGuidSagasWithAddCountGetCountAndCommitCount[id] = testSaga;
48 :
49 : return (TSaga)testSaga.Item1;
50 : }
51 :
52 : /// <summary>
53 : /// Commit any changed <see cref="Saga{TAuthenticationToken}"/> added to this <see cref="ISagaUnitOfWork{TAuthenticationToken}"/> via <see cref="ISagaUnitOfWork{TAuthenticationToken}.Add{TSaga}"/>
54 : /// </summary>
55 1 : public void Commit()
56 : {
57 : IList<Tuple<ISaga<Guid>, int, int, int>> trackList1 = TestGuidSagasWithAddCountGetCountAndCommitCount.Values.ToList();
58 : foreach (Tuple<ISaga<Guid>, int, int, int> tracked in trackList1)
59 : {
60 : var testSaga = new Tuple<ISaga<Guid>, int, int, int>(tracked.Item1, tracked.Item2, tracked.Item3, tracked.Item4 + 1);
61 : TestGuidSagasWithAddCountGetCountAndCommitCount[testSaga.Item1.Id] = testSaga;
62 : }
63 : IList<Tuple<ISaga<ISingleSignOnToken>, int, int, int>> trackList2 = TestSagasWithAddCountGetCountAndCommitCount.Values.ToList();
64 : foreach (Tuple<ISaga<ISingleSignOnToken>, int, int, int> tracked in trackList2)
65 : {
66 : var testSaga = new Tuple<ISaga<ISingleSignOnToken>, int, int, int>(tracked.Item1, tracked.Item2, tracked.Item3, tracked.Item4 + 1);
67 : TestSagasWithAddCountGetCountAndCommitCount[testSaga.Item1.Id] = testSaga;
68 : }
69 : }
70 :
71 : #endregion
72 :
73 : #region Implementation of ISagaUnitOfWork<ISingleSignOnToken>
74 :
75 : /// <summary>
76 : /// Add an item into the <see cref="ISagaUnitOfWork{TAuthenticationToken}"/> ready to be committed.
77 : /// </summary>
78 : void ISagaUnitOfWork<ISingleSignOnToken>.Add<TSaga>(TSaga saga)
79 : {
80 : Tuple<ISaga<ISingleSignOnToken>, int, int, int> testSaga;
81 : if (!TestSagasWithAddCountGetCountAndCommitCount.TryGetValue(saga.Id, out testSaga))
82 : testSaga = new Tuple<ISaga<ISingleSignOnToken>, int, int, int>(saga, 1, 0, 0);
83 : else
84 : testSaga = new Tuple<ISaga<ISingleSignOnToken>, int, int, int>(saga, testSaga.Item2 + 1, testSaga.Item3, testSaga.Item4);
85 : TestSagasWithAddCountGetCountAndCommitCount[saga.Id] = testSaga;
86 : }
87 :
88 : /// <summary>
89 : /// Get an item from the <see cref="ISagaUnitOfWork{TAuthenticationToken}"/> if it has already been loaded.
90 : /// </summary>
91 : TSaga ISagaUnitOfWork<ISingleSignOnToken>.Get<TSaga>(Guid id, int? expectedVersion)
92 : {
93 : Tuple<ISaga<ISingleSignOnToken>, int, int, int> testSaga = TestSagasWithAddCountGetCountAndCommitCount[id];
94 : testSaga = new Tuple<ISaga<ISingleSignOnToken>, int, int, int>(testSaga.Item1, testSaga.Item2, testSaga.Item3 + 1, testSaga.Item4);
95 : TestSagasWithAddCountGetCountAndCommitCount[id] = testSaga;
96 :
97 : return (TSaga)testSaga.Item1;
98 : }
99 :
100 : #endregion
101 : }
102 : }
|