-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessorTests.cs
More file actions
204 lines (170 loc) · 6.58 KB
/
Copy pathProcessorTests.cs
File metadata and controls
204 lines (170 loc) · 6.58 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using FluentAssertions;
using Mediator.Behaviors;
using Mediator.Tests.TestData;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Mediator.Tests;
/// <summary>
/// Tests for Pre and Post Processors.
/// </summary>
public class ProcessorTests : IDisposable
{
private ServiceProvider? _serviceProvider;
public void Dispose()
{
_serviceProvider?.Dispose();
TrackingPreProcessor<PingRequest>.Reset();
TrackingPostProcessor<PingRequest, PongResponse>.Reset();
PingPreProcessor.Reset();
PingPostProcessor.Reset();
}
private IMediator CreateMediatorWithProcessors()
{
var services = new ServiceCollection();
services.AddMediator(config =>
{
config.AddAssembly(typeof(ProcessorTests).Assembly);
config.AddBehavior(typeof(RequestPreProcessorBehavior<,>));
config.AddBehavior(typeof(RequestPostProcessorBehavior<,>));
});
_serviceProvider = services.BuildServiceProvider();
return _serviceProvider.GetRequiredService<IMediator>();
}
[Fact]
public async Task PreProcessor_ExecutesBeforeHandler()
{
// Arrange
var services = new ServiceCollection();
services.AddMediator(config =>
{
config.AddAssembly(typeof(ProcessorTests).Assembly);
config.AddBehavior(typeof(RequestPreProcessorBehavior<,>));
});
_serviceProvider = services.BuildServiceProvider();
var mediator = _serviceProvider.GetRequiredService<IMediator>();
PingPreProcessor.Reset();
// Act
await mediator.Send(new PingRequest("PreProcess Test"));
// Assert
PingPreProcessor.ExecutionCount.Should().Be(1);
PingPreProcessor.LastMessage.Should().Be("PreProcess Test");
}
[Fact]
public async Task PostProcessor_ExecutesAfterHandler()
{
// Arrange
var services = new ServiceCollection();
services.AddMediator(config =>
{
config.AddAssembly(typeof(ProcessorTests).Assembly);
config.AddBehavior(typeof(RequestPostProcessorBehavior<,>));
});
_serviceProvider = services.BuildServiceProvider();
var mediator = _serviceProvider.GetRequiredService<IMediator>();
PingPostProcessor.Reset();
// Act
await mediator.Send(new PingRequest("PostProcess Test"));
// Assert
PingPostProcessor.ExecutionCount.Should().Be(1);
PingPostProcessor.LastResponse.Should().NotBeNull();
PingPostProcessor.LastResponse!.Reply.Should().Be("Pong: PostProcess Test");
}
[Fact]
public async Task PreAndPostProcessors_ExecuteInCorrectOrder()
{
// Arrange
var executionOrder = new List<string>();
var services = new ServiceCollection();
services.AddMediator(config =>
{
config.AddAssembly(typeof(ProcessorTests).Assembly);
config.AddBehavior(typeof(RequestPreProcessorBehavior<,>));
config.AddBehavior(typeof(RequestPostProcessorBehavior<,>));
});
// Custom processors that track order (not auto-registered because they are nested types)
services.AddTransient<IRequestPreProcessor<PingRequest>>(sp =>
new OrderTrackingPreProcessor(executionOrder));
services.AddTransient<IRequestPostProcessor<PingRequest, PongResponse>>(sp =>
new OrderTrackingPostProcessor(executionOrder));
_serviceProvider = services.BuildServiceProvider();
var mediator = _serviceProvider.GetRequiredService<IMediator>();
// Act
await mediator.Send(new PingRequest("Order Test"));
// Assert
executionOrder.Should().HaveCount(2);
executionOrder[0].Should().Be("PreProcessor");
executionOrder[1].Should().Be("PostProcessor");
}
[Fact]
public async Task MultiplePreProcessors_AllExecute()
{
// Arrange
var services = new ServiceCollection();
services.AddMediator(config =>
{
config.AddAssembly(typeof(ProcessorTests).Assembly);
config.AddBehavior(typeof(RequestPreProcessorBehavior<,>));
});
_serviceProvider = services.BuildServiceProvider();
var mediator = _serviceProvider.GetRequiredService<IMediator>();
PingPreProcessor.Reset();
AnotherPingPreProcessor.Reset();
// Act
await mediator.Send(new PingRequest("Multiple Test"));
// Assert
PingPreProcessor.ExecutionCount.Should().Be(1);
AnotherPingPreProcessor.ExecutionCount.Should().Be(1);
}
[Fact]
public async Task NoProcessors_HandlerStillExecutes()
{
// Arrange
var services = new ServiceCollection();
services.AddMediator(config =>
{
config.AddAssembly(typeof(ProcessorTests).Assembly);
config.AddBehavior(typeof(RequestPreProcessorBehavior<,>));
config.AddBehavior(typeof(RequestPostProcessorBehavior<,>));
});
_serviceProvider = services.BuildServiceProvider();
var mediator = _serviceProvider.GetRequiredService<IMediator>();
// Act
var response = await mediator.Send(new PingRequest("No processors"));
// Assert
response.Reply.Should().Be("Pong: No processors");
}
// Helper classes for order tracking
private class OrderTrackingPreProcessor : IRequestPreProcessor<PingRequest>
{
private readonly List<string> _order;
public OrderTrackingPreProcessor(List<string> order) => _order = order;
public Task Process(PingRequest request, CancellationToken cancellationToken)
{
_order.Add("PreProcessor");
return Task.CompletedTask;
}
}
private class OrderTrackingPostProcessor : IRequestPostProcessor<PingRequest, PongResponse>
{
private readonly List<string> _order;
public OrderTrackingPostProcessor(List<string> order) => _order = order;
public Task Process(PingRequest request, PongResponse response, CancellationToken cancellationToken)
{
_order.Add("PostProcessor");
return Task.CompletedTask;
}
}
}
/// <summary>
/// Another pre-processor for testing multiple processors.
/// </summary>
public class AnotherPingPreProcessor : IRequestPreProcessor<PingRequest>
{
public static int ExecutionCount { get; private set; }
public Task Process(PingRequest request, CancellationToken cancellationToken)
{
ExecutionCount++;
return Task.CompletedTask;
}
public static void Reset() => ExecutionCount = 0;
}