-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlightStateMachineTests.cs
172 lines (143 loc) · 5.71 KB
/
FlightStateMachineTests.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
namespace NFB.UI.DiscordBot.Tests.StateMachines
{
using System;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using MassTransit;
using MassTransit.Testing;
using Microsoft.Extensions.DependencyInjection;
using NFB.Domain.Bus.DTOs;
using NFB.Domain.Bus.Events;
using NFB.UI.DiscordBot.StateMachines;
using NFB.UI.DiscordBot.StateMachines.Activities;
using NFB.UI.DiscordBot.States;
using Xunit;
/// <summary>
/// The flight state machine tests.
/// </summary>
public class FlightStateMachineTests : IDisposable
{
#region Private Fields
/// <summary>
/// The client.
/// </summary>
private readonly DiscordSocketClient client;
/// <summary>
/// The harness.
/// </summary>
private readonly InMemoryTestHarness harness;
/// <summary>
/// The saga harness.
/// </summary>
private readonly IStateMachineSagaTestHarness<FlightState, FlightStateMachine> sagaHarness;
/// <summary>
/// The state machine.
/// </summary>
private readonly FlightStateMachine stateMachine;
#endregion Private Fields
#region Public Constructors
/// <summary>
/// Initializes a new instance of the <see cref="FlightStateMachineTests"/> class.
/// </summary>
public FlightStateMachineTests()
{
// Get the token.
var token = Environment.GetEnvironmentVariable("NFBTestToken", EnvironmentVariableTarget.Machine);
if (string.IsNullOrEmpty(token))
throw new Exception("Token empty or not found");
// Client
this.client = new DiscordSocketClient(new DiscordSocketConfig { LogLevel = LogSeverity.Debug, DefaultRetryMode = RetryMode.AlwaysRetry });
this.client.LoginAsync(TokenType.Bot, token).Wait();
this.client.StartAsync().Wait();
// Services
var services = new ServiceCollection();
services.AddMassTransitInMemoryTestHarness(
cfg =>
{
cfg.AddSagaStateMachine<FlightStateMachine, FlightState>().InMemoryRepository();
cfg.AddSagaStateMachineTestHarness<FlightStateMachine, FlightState>();
});
services.AddSingleton(this.client);
services.AddScoped<CheckFlightCompletedActivity>();
services.AddScoped<CreateAnnouncementMessageActivity>();
services.AddScoped<CreateVoiceChannelActivity>();
services.AddScoped<UpdateActiveFlightMessageActivity>();
services.AddScoped<UpdateVatsimPilotDataActivity>();
var provider = services.BuildServiceProvider(true);
// Harness
this.harness = provider.GetRequiredService<InMemoryTestHarness>();
this.sagaHarness = provider.GetRequiredService<IStateMachineSagaTestHarness<FlightState, FlightStateMachine>>();
this.stateMachine = new FlightStateMachine();
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// The dispose.
/// </summary>
public void Dispose()
{
this.client?.LogoutAsync().Wait();
this.client?.Dispose();
}
/// <summary>
/// Receive flight created event creates new saga.
/// </summary>
/// <returns>
/// The <see cref="Task"/>.
/// </returns>
[Fact]
public async Task ReceiveFlightCreatedEventCreatesNewSaga()
{
// Arrange
await this.harness.Start();
var flightCreatedEvent = new FlightCreatedEvent
{
Id = Guid.NewGuid(),
Destination = new AirportEntityDto { ICAO = "EGCC" },
Origin = new AirportEntityDto { ICAO = "EGLL" },
StartTime = DateTime.UtcNow.AddHours(3)
};
// Act
await this.harness.Bus.Publish(flightCreatedEvent);
// Assert
var sagaExists = await this.sagaHarness.Exists(flightCreatedEvent.Id, this.stateMachine.Created, TimeSpan.FromSeconds(5));
Assert.Equal(flightCreatedEvent.Id, sagaExists.Value);
await this.harness.Stop();
}
/// <summary>
/// Receive out of order flight created event creates new saga.
/// </summary>
/// <returns>
/// The <see cref="Task"/>.
/// </returns>
[Fact]
public async Task ReceiveOutOfOrderFlightCreatedEventCreatesNewSaga()
{
// Arrange
await this.harness.Start();
var flightStartingEvent = new FlightStartingEvent
{
Id = Guid.NewGuid(),
Destination = "EGCC",
Origin = "EGLL",
StartTime = DateTime.UtcNow.AddHours(3)
};
var flightCreatedEvent = new FlightCreatedEvent
{
Id = flightStartingEvent.Id,
Destination = new AirportEntityDto { ICAO = "EGCC" },
Origin = new AirportEntityDto { ICAO = "EGLL" },
StartTime = DateTime.UtcNow.AddHours(3)
};
// Act
await this.harness.Bus.Publish(flightStartingEvent);
await this.harness.Bus.Publish(flightCreatedEvent);
// Assert
var sagaExists = await this.sagaHarness.Exists(flightCreatedEvent.Id, this.stateMachine.Active, TimeSpan.FromSeconds(5));
Assert.Equal(flightCreatedEvent.Id, sagaExists.Value);
await this.harness.Stop();
}
#endregion Public Methods
}
}