-
Notifications
You must be signed in to change notification settings - Fork 1
/
SagaFlowTests.Success.cs
148 lines (118 loc) · 5.03 KB
/
SagaFlowTests.Success.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DistributedTransactions.Attributes;
using DistributedTransactions.Providers.Abstractions;
using DistributedTransactions.Saga.Models.Abstractions;
using DistributedTransactions.Tests.Base;
using DistributedTransactions.Tests.Mocks;
using DistributedTransactions.Tests.Mocks.Models;
using FluentAssertions;
using NUnit.Framework;
namespace DistributedTransactions.Tests.Saga
{
[TestFixture]
internal class SagaSuccessFlowTests : DistributedTransactionsTestsBase
{
private ITransactionProvider _transactionProvider;
private IOperationProvider _operationProvider;
private MockDatabase _mockDatabase;
[OneTimeSetUp]
public void SetupServiceProvider()
{
OneTimeSetup();
}
[SetUp]
public void Setup()
{
_transactionProvider = TransactionProvider;
_operationProvider = OperationProvider;
_mockDatabase = MockDatabase;
_mockDatabase.Clear();
}
[Test]
public async Task CreateManufacturer_CreateAutos_Success()
{
var manufacturer = new Manufacturer
{
Id = 1,
Name = "Audi"
};
var auto = new Auto
{
Id = 1,
ManufacturerId = 1,
Name = "A7"
};
var sagaExecutor = SagaExecutorBuilder.ValidateAndBuild();
var createManufacturer = new CreateManufacturer(TransactionContext)
{
Manufacturer = manufacturer
};
var createAuto = new CreateAuto(TransactionContext)
{
Auto = auto
};
sagaExecutor.RegisterOperation(createManufacturer);
sagaExecutor.RegisterOperation(createAuto);
await sagaExecutor.ExecuteTransactionAsync(CancellationToken.None);
// instance status
sagaExecutor.Status.Should().Be(TransactionStatus.FinishedCorrectly);
sagaExecutor.LastOccuredException.Should().BeNull();
// in database entities
var transactionId = sagaExecutor.TransactionId!.Value;
var transaction = await _transactionProvider.GetByTransactionIdAsync(transactionId, CancellationToken.None);
transaction.Status.Should().Be(TransactionStatus.FinishedCorrectly);
var operationEntities = await _operationProvider.GetByTransactionIdAsync(transactionId, CancellationToken.None);
operationEntities.First().Status.Should().Be(OperationStatus.Committed);
var manufacturersInDb = _mockDatabase.Manufacturers.Get(x => x.Id == manufacturer.Id);
manufacturersInDb.First().Should().Be(manufacturer);
var autosInDb = _mockDatabase.Autos.Get(x => x.Id == auto.Id);
autosInDb.First().Should().Be(auto);
}
[DistributedTransactionOperation(nameof(TransactionType.CreateManufacturerWithAuto), nameof(OperationType.CreateManufacturer))]
private class CreateManufacturer : SagaOperationBase<long>
{
public Manufacturer Manufacturer { get; init; }
private readonly MockDatabase _mockDatabase;
public CreateManufacturer(ITransactionContext transactionContext) : base(transactionContext)
{
_mockDatabase = transactionContext.GetRequiredService<MockDatabase>();
}
public override Task CommitAsync(CancellationToken cancellationToken)
{
_mockDatabase.Manufacturers.Add(Manufacturer);
// saving as rollback data
RollbackData = Manufacturer.Id;
return Task.CompletedTask;
}
public override Task RollbackAsync(CancellationToken cancellationToken)
{
_mockDatabase.Manufacturers.Remove(x => x.Id == RollbackData);
return Task.CompletedTask;
}
}
[DistributedTransactionOperation(nameof(TransactionType.CreateManufacturerWithAuto), nameof(OperationType.CreateAuto))]
private class CreateAuto : SagaOperationBase<long>
{
public Auto Auto { get; init; }
private readonly MockDatabase _mockDatabase;
public CreateAuto(ITransactionContext transactionContext) : base(transactionContext)
{
_mockDatabase = transactionContext.GetRequiredService<MockDatabase>();
}
public override Task CommitAsync(CancellationToken cancellationToken)
{
_mockDatabase.Autos.Add(Auto);
// saving as rollback data
RollbackData = Auto.Id;
return Task.CompletedTask;
}
public override Task RollbackAsync(CancellationToken cancellationToken)
{
_mockDatabase.Autos.Remove(x => x.Id == RollbackData);
return Task.CompletedTask;
}
}
}
}