-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEventExpecationsConfig.cs
More file actions
470 lines (406 loc) · 27.7 KB
/
Copy pathEventExpecationsConfig.cs
File metadata and controls
470 lines (406 loc) · 27.7 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
namespace CoreEx.UnitTesting.Events;
/// <summary>
/// Provides the <see cref="EventExpectations{TTester}"/> configuration for a specific <see cref="IEventPublisher"/> service key, enabling the configuration of expected events and their assertion during testing.
/// </summary>
/// <remarks>Where expected events have been specified they will be matched, in the sequence specified, against the actual events published during the test execution.</remarks>
public sealed class EventExpectationsConfig
{
private bool _expectNoEvents;
private readonly List<EventExpectationAssertor> _assertors = [];
private readonly List<string> _pathsToIgnore = [.. DefaultPathsToIgnore];
private Action<AssertArgs, DestinationEvent[]>? _assertAllEvents;
/// <summary>
/// Gets or sets the default <see cref="CloudEvent"/> metadata paths to ignore for comparisons.
/// </summary>
public static List<string> DefaultMetadataPathsToIgnore { get; set => field = value.ThrowIfNull(); } = ["id", "time", "subject", "partitionkey", "authtype", "authid", "traceparent", "tracestate", "baggage"];
/// <summary>
/// Gets or sets the default <see cref="CloudEvent"/> data paths to ignore for comparisons.
/// </summary>
public static List<string> DefaultDataPathsToIgnore { get; set => field = value.ThrowIfNull(); } = ["data.id", "data.changelog", "data.etag"];
/// <summary>
/// Gets the default JSON <see cref="CloudEvent"/> paths to ignore for comparisons, which is a combination of the <see cref="DefaultMetadataPathsToIgnore"/> and <see cref="DefaultDataPathsToIgnore"/>.
/// </summary>
public static List<string> DefaultPathsToIgnore => [.. DefaultMetadataPathsToIgnore, .. DefaultDataPathsToIgnore];
/// <summary>
/// Initializes a new instance of the <see cref="EventExpectationsConfig"/> class.
/// </summary>
/// <param name="tester">The owning <see cref="TesterBase"/>.</param>
/// <param name="serviceKey">The registered service key.</param>
/// <param name="requestId">The request identifier.</param>
/// <param name="assembly">The assembly to use for resource resolution.</param>
internal EventExpectationsConfig(TesterBase tester, string serviceKey, string? requestId, Assembly assembly)
{
Tester = tester;
ServiceKey = serviceKey;
RequestId = requestId;
ResourceAssembly = assembly;
}
/// <summary>
/// Gets the owning <see cref="TesterBase"/>.
/// </summary>
internal TesterBase Tester { get; }
/// <summary>
/// Gets the registered service key for the underlying <see cref="IEventPublisher"/>.
/// </summary>
internal string ServiceKey { get; }
/// <summary>
/// Gets the request identifier.
/// </summary>
internal string? RequestId { get; }
/// <summary>
/// Gets the assembly to use for resource resolution.
/// </summary>
internal Assembly ResourceAssembly { get; }
/// <summary>
/// Gets the current list of JSON paths to ignore for all event comparisons.
/// </summary>
/// <remarks><para>Defaults to <see cref="DefaultPathsToIgnore"/>.</para>
/// Note: this list is copied and concatenated with the path(s) specified for the individual event expectations. Therefore, any changes after an event expectation is made, will not be included; i.e. only
/// applies to subsequent event expectations. This is to ensure consistency for the individual event expectation configurations, and to avoid any unintended consequences of changes to the default paths
/// after preceding event expectations have been configured.</remarks>
public List<string> PathsToIgnore => _pathsToIgnore;
/// <summary>
/// Indicates that no events should have been published.
/// </summary>
internal void ExpectNoEvents()
{
if (_assertors.Count != 0)
throw new InvalidOperationException("Cannot set to expect no events when event expectations have already been configured.");
_expectNoEvents = true;
}
/// <summary>
/// Indicates that events should have been published.
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
internal void ExpectEvents()
{
if (_expectNoEvents)
throw new InvalidOperationException("Cannot set to expect events when already configured to expect no events.");
if (_assertAllEvents is not null)
throw new InvalidOperationException($"Cannot set to expect events when already configured using {nameof(AssertAllFromJsonResource)}, {nameof(AssertCount)}, or {nameof(Assert)}.");
}
/// <summary>
/// Indicates that events should have been published and asserted, in aggregate, using <see cref="AssertAllFromJsonResource(string, Assembly?, IEnumerable{string})"/>, <see cref="AssertCount(int)"/>, or <see cref="Assert(Action{DestinationEvent[]})"/>.
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
private void ExpectAllEvents()
{
ExpectEvents();
if (_assertors.Count > 0)
throw new InvalidOperationException($"Cannot set to expect all events using {nameof(AssertAllFromJsonResource)}, {nameof(AssertCount)}, or {nameof(Assert)} when individual event expectations have already been configured.");
}
/// <summary>
/// Gets or sets the factory function used to create new <see cref="ExecutionContext"/> instance.
/// </summary>
public Func<IServiceProvider, ExecutionContext> ExecutionContextFactory { get; set; } = sp => new ExecutionContext();
/// <summary>
/// Gets or sets the <see cref="JsonSerializerOptions"/>; where <see langword="null"/> then the registered version from the <see cref="TesterBase.Services"/> will be used.
/// </summary>
public JsonSerializerOptions? JsonSerializerOptions { get; set; }
/// <summary>
/// Gets or sets the <see cref="IHostSettings"/>; where <see langword="null"/> then the registered host version from the <see cref="TesterBase.Services"/> will be used.
/// </summary>
public IHostSettings? HostSettings { get; set; }
/// <summary>
/// Gets or sets the <see cref="IEventFormatter"/>; where <see langword="null"/> then the registered version from the <see cref="TesterBase.Services"/> will be used.
/// </summary>
public IEventFormatter? EventFormatter { get; set; }
/// <summary>
/// Adds an expectation assertion that the consumer of the <paramref name="events"/> action is responsible for asserting as required.
/// </summary>
/// <param name="events">The actual <see cref="DestinationEvent"/> array.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
public EventExpectationsConfig Assert(Action<DestinationEvent[]> events)
{
ExpectAllEvents();
events.ThrowIfNull();
_assertAllEvents = (_, actual) =>
{
events(actual);
Tester.Implementor.WriteLine($" > Expected one or more event(s) with a custom Assert; and that assertion was met.");
};
return this;
}
/// <summary>
/// Adds am expectation assertion that asserts that the number of published events matches the specified <paramref name="count"/>..
/// </summary>
/// <param name="count">The expected number of events to be published.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
public EventExpectationsConfig AssertCount(int count)
{
ExpectAllEvents();
count.ThrowIfLessThanOrEqualToZero();
_assertAllEvents = (_, actual) =>
{
if (count == actual.Length)
Tester.Implementor.WriteLine($" > Expected {count} event(s); and that number was found to be published.");
else
Tester.Implementor.AssertFail($"Expected {count} '{ServiceKey}' events; however, {actual.Length} were found to be published.");
};
return this;
}
/// <summary>
/// Adds an expectation assertion that one or more events were published matching the <see cref="DestinationEvent"/> array from the specified JSON resource (with any specified JSON paths to ignore).
/// </summary>
/// <typeparam name="TAssembly">The <see cref="Type"/> to infer the <see cref="Assembly"/> for the embedded resource.</typeparam>
/// <param name="resourceName">The embedded resource name (matches to the end of the fully qualified resource name).</param>
/// <param name="pathsToIgnore">Any additional JSON paths to ignore from the underlying <see cref="CloudEvent"/> comparison.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
/// <remarks>The <paramref name="pathsToIgnore"/> should be from an individual <see cref="CloudEvent"/> perspective from a consistency perspective.</remarks>
public EventExpectationsConfig AssertAllFromJsonResource<TAssembly>(string resourceName, params IEnumerable<string> pathsToIgnore)
=> AssertAllFromJsonResource(resourceName, typeof(TAssembly).Assembly, pathsToIgnore);
/// <summary>
/// Adds an expectation assertion that one or more events were published matching the <see cref="DestinationEvent"/> array from the specified JSON resource (with any specified JSON paths to ignore).
/// </summary>
/// <param name="resourceName">The embedded resource name (matches to the end of the fully qualified resource name).</param>
/// <param name="pathsToIgnore">Any additional JSON paths to ignore from the underlying <see cref="CloudEvent"/> comparison.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
/// <remarks>The <paramref name="pathsToIgnore"/> should be from an individual <see cref="CloudEvent"/> perspective from a consistency perspective.</remarks>
public EventExpectationsConfig AssertAllFromJsonResource(string resourceName, params IEnumerable<string> pathsToIgnore)
=> AssertAllFromJsonResource(resourceName, null, pathsToIgnore);
/// <summary>
/// Adds an expectation assertion that one or more events were published matching the <see cref="DestinationEvent"/> array from the specified JSON resource (with any specified JSON paths to ignore).
/// </summary>
/// <param name="resourceName">The embedded resource name (matches to the end of the fully qualified resource name).</param>
/// <param name="assembly">The <see cref="ResourceAssembly"/> that contains the resource; defaults to <see cref="Assembly.GetCallingAssembly"/>.</param>
/// <param name="pathsToIgnore">Any additional JSON paths to ignore from the underlying <see cref="CloudEvent"/> comparison.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
/// <remarks>The <paramref name="pathsToIgnore"/> should be from an individual <see cref="CloudEvent"/> perspective from a consistency perspective.</remarks>
public EventExpectationsConfig AssertAllFromJsonResource(string resourceName, Assembly? assembly = null, params IEnumerable<string> pathsToIgnore)
{
ExpectAllEvents();
assembly ??= ResourceAssembly;
var capturedPaths = CombinePaths(pathsToIgnore).Select(x => $"event.{(x.StartsWith("$.") ? x[2..] : x)}").ToArray();
_assertAllEvents = (args, events) =>
{
var ej = Resource.GetJson(resourceName, assembly);
var aj = JsonSerializer.Serialize(events.Select(kvp => new { destination = kvp.Destination, @event = kvp.Event.EncodeToJsonElement() }));
ObjectComparer.AssertJson(new UnitTestEx.Json.JsonElementComparerOptions { PreambleText = $"'{ServiceKey}' event(s) comparison." }, ej, aj, capturedPaths);
Tester.Implementor.WriteLine(" > All event expectations met successfully.");
};
return this;
}
/// <summary>
/// Adds an expectation assertion that an event was published where the returned value will be used as the source of the <see cref="EventData.Data"/>.
/// </summary>
/// <param name="destination">The expected destination (i.e. topic) name.</param>
/// <param name="title">The expected <see cref="EventData.Title"/> (.</param>
/// <param name="messageType">The expected <see cref="EventData.MessageType"/>.</param>
/// <param name="updater">An optional action to further update the expected <see cref="EventData"/> prior to assertion.</param>
/// <param name="pathsToIgnore">Any additional JSON paths to ignore from the underlying <see cref="CloudEvent"/> comparison.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
/// <remarks>Internally this constructs the <see cref="EventData"/> and converts it to a <see cref="CloudEvent"/> for comparison.</remarks>
public EventExpectationsConfig AssertWithValue(string destination, string title, CoreEx.Events.MessageType messageType = CoreEx.Events.MessageType.Event, Action<EventData>? updater = null, params IEnumerable<string> pathsToIgnore)
{
ExpectEvents();
_assertors.Add(new EventExpectationAssertor(this, destination, (assertor, args, _) =>
{
var ed = new EventData() { Title = title, MessageType = messageType }.WithValue(args.Value, null, assertor.JsonSerializerOptions);
updater?.Invoke(ed);
return assertor.EventFormatter.ConvertToCloudEvent(assertor.EventFormatter.Format(ed));
}, CombinePaths(pathsToIgnore)));
return this;
}
/// <summary>
/// Adds an expectation assertion that an event was published where the returned value from the <paramref name="valueFactory"/> will be used as the source of the <see cref="EventData.Data"/>.
/// </summary>
/// <typeparam name="TValue">The value <see cref="Type"/>.</typeparam>
/// <param name="valueFactory">A function to create the value.</param>
/// <param name="destination">The expected destination (i.e. topic) name.</param>
/// <param name="title">The expected <see cref="EventData.Title"/>.</param>
/// <param name="messageType">The expected <see cref="EventData.MessageType"/>.</param>
/// <param name="updater">An optional action to further update the expected <see cref="EventData"/> prior to assertion.</param>
/// <param name="pathsToIgnore">Any additional JSON paths to ignore from the underlying <see cref="CloudEvent"/> comparison.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
/// <remarks>Internally this constructs the <see cref="EventData"/> and converts it to a <see cref="CloudEvent"/> for comparison.</remarks>
public EventExpectationsConfig AssertWithValue<TValue>(Func<TValue> valueFactory, string destination, string title, CoreEx.Events.MessageType messageType = CoreEx.Events.MessageType.Event, Action<EventData>? updater = null, params IEnumerable<string> pathsToIgnore)
{
valueFactory.ThrowIfNull();
ExpectEvents();
_assertors.Add(new EventExpectationAssertor(this, destination, (assertor, args, _) =>
{
var value = valueFactory();
var ed = new EventData() { Title = title, MessageType = messageType }.WithValue(value, null, assertor.JsonSerializerOptions);
updater?.Invoke(ed);
return assertor.EventFormatter.ConvertToCloudEvent(assertor.EventFormatter.Format(ed));
}, CombinePaths(pathsToIgnore)));
return this;
}
/// <summary>
/// Adds an expectation assertion that an event was published matching the specified <see cref="EventData"/> converted as a <see cref="CloudEvent"/> (with any specified JSON paths to ignore).
/// </summary>
/// <param name="destination">The expected destination (i.e. topic) name.</param>
/// <param name="eventData">The <see cref="EventData"/>.</param>
/// <param name="pathsToIgnore">Any additional JSON paths to ignore from the underlying <see cref="CloudEvent"/> comparison.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
public EventExpectationsConfig AssertEventData(string destination, EventData eventData, params IEnumerable<string> pathsToIgnore)
{
ExpectEvents();
eventData.ThrowIfNull();
_assertors.Add(new EventExpectationAssertor(this, destination, (assertor, args, _) =>
{
return assertor.EventFormatter.ConvertToCloudEvent(assertor.EventFormatter.Format(eventData));
}, CombinePaths(pathsToIgnore)));
return this;
}
/// <summary>
/// Adds an expectation assertion that an event was published matching the primary metadata (where specified) only.
/// </summary>
/// <param name="destination">The expected destination (i.e. topic) name.</param>
/// <param name="title">The expected <see cref="EventData.Title"/> (<see cref="CloudEvent.Type"/>) glob-like matching pattern that will represent the underlying title <see cref="Regex"/>.</param>
/// <param name="key">The expected <see cref="EventData.Key"/> (<see cref="CloudEvent.Subject"/>) value.</param>
/// <param name="source">The expected <see cref="EventData.Source"/> (<see cref="CloudEvent.Source"/>) glob-like matching pattern that will represent the underlying source <see cref="Regex"/>.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
/// <remarks>The <paramref name="title"/> and <paramref name="source"/> both support glob-like matching patterns.</remarks>
public EventExpectationsConfig AssertMetadata(string destination, string? title = null, string? key = null, string? source = null)
{
ExpectEvents();
_assertors.Add(new EventExpectationAssertor(this, destination, (assertor, args, actual) =>
{
assertor.AssertDestination(actual.Destination);
var sa = new SubscribeAttribute(title, source);
if (sa.IsMatch(actual.Event?.Type, actual.Event?.Source))
{
if (key != null)
assertor.Tester.Implementor.AssertAreEqual(key, actual.Event?.Subject, $"Expected key '{key}'; but found '{actual.Event?.Subject}'.");
}
else
assertor.Tester.Implementor.AssertFail($"Expected event metadata did not match; Title expected '{title}' vs actual '{actual.Event?.Type}', Source expected '{source}' vs actual '{actual.Event?.Source}'");
}, []));
return this;
}
/// <summary>
/// Adds an expectation assertion that an event was published matching the specified <see cref="CloudEvent"/> (with any specified JSON paths to ignore).
/// </summary>
/// <param name="destination">The expected destination (i.e. topic) name.</param>
/// <param name="cloudEvent">The expected <see cref="CloudEvent"/>. </param>
/// <param name="pathsToIgnore">Any additional JSON paths to ignore from the underlying <see cref="CloudEvent"/> comparison.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
public EventExpectationsConfig AssertCloudEvent(string destination, CloudEvent cloudEvent, params IEnumerable<string> pathsToIgnore)
{
ExpectEvents();
_assertors.Add(new EventExpectationAssertor(this, destination, (_, _, _) => cloudEvent, CombinePaths(pathsToIgnore)));
return this;
}
/// <summary>
/// Adds an expectation assertion that an event was published matching the <see cref="CloudEvent"/> from the specified JSON resource (with any specified JSON paths to ignore).
/// </summary>
/// <typeparam name="TAssembly">The <see cref="Type"/> to infer the <see cref="Assembly"/> for the embedded resource.</typeparam>
/// <param name="destination">The expected destination (i.e. topic) name.</param>
/// <param name="resourceName">The embedded resource name (matches to the end of the fully qualified resource name).</param>
/// <param name="pathsToIgnore">Any additional JSON paths to ignore from the underlying <see cref="CloudEvent"/> comparison.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
public EventExpectationsConfig AssertCloudEventFromJsonResource<TAssembly>(string destination, string resourceName, params IEnumerable<string> pathsToIgnore)
=> AssertCloudEventFromJsonResource<TAssembly>(destination, resourceName, null, pathsToIgnore);
/// <summary>
/// Adds an expectation assertion that an event was published matching the <see cref="CloudEvent"/> from the specified JSON resource (with any specified JSON paths to ignore).
/// </summary>
/// <typeparam name="TAssembly">The <see cref="Type"/> to infer the <see cref="Assembly"/> for the embedded resource.</typeparam>
/// <param name="destination">The expected destination (i.e. topic) name.</param>
/// <param name="resourceName">The embedded resource name (matches to the end of the fully qualified resource name).</param>
/// <param name="updater">An optional action to update the <see cref="CloudEvent"/> before use.</param>
/// <param name="pathsToIgnore">Any additional JSON paths to ignore from the underlying <see cref="CloudEvent"/> comparison.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
public EventExpectationsConfig AssertCloudEventFromJsonResource<TAssembly>(string destination, string resourceName, Action<CloudEvent>? updater = null, params IEnumerable<string> pathsToIgnore)
{
ExpectEvents();
_assertors.Add(new EventExpectationAssertor(this, destination, (assertor, args, _) =>
{
return assertor.Tester.CreateCloudEventFromJsonResource<TAssembly>(resourceName, updater);
}, CombinePaths(pathsToIgnore)));
return this;
}
/// <summary>
/// Adds an expectation assertion that an event was published matching the <see cref="CloudEvent"/> from the specified JSON resource (with any specified JSON paths to ignore).
/// </summary>
/// <param name="destination">The expected destination (i.e. topic) name.</param>
/// <param name="resourceName">The embedded resource name (matches to the end of the fully qualified resource name).</param>
/// <param name="pathsToIgnore">Any additional JSON paths to ignore from the underlying <see cref="CloudEvent"/> comparison.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
public EventExpectationsConfig AssertCloudEventFromJsonResource(string destination, string resourceName, params IEnumerable<string> pathsToIgnore)
=> AssertCloudEventFromJsonResource(destination, resourceName, null, null, pathsToIgnore);
/// <summary>
/// Adds an expectation assertion that an event was published matching the <see cref="CloudEvent"/> from the specified JSON resource (with any specified JSON paths to ignore).
/// </summary>
/// <param name="destination">The expected destination (i.e. topic) name.</param>
/// <param name="resourceName">The embedded resource name (matches to the end of the fully qualified resource name).</param>
/// <param name="updater">An optional action to update the <see cref="CloudEvent"/> before use.</param>
/// <param name="assembly">The <see cref="ResourceAssembly"/> that contains the resource; defaults to <see cref="Assembly.GetCallingAssembly"/>.</param>
/// <param name="pathsToIgnore">Any additional JSON paths to ignore from the underlying <see cref="CloudEvent"/> comparison.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
public EventExpectationsConfig AssertCloudEventFromJsonResource(string destination, string resourceName, Action<CloudEvent>? updater = null, Assembly? assembly = null, params IEnumerable<string> pathsToIgnore)
{
ExpectEvents();
_assertors.Add(new EventExpectationAssertor(this, destination, (assertor, args, _) =>
{
return assertor.Tester.CreateCloudEventFromJsonResource(resourceName, updater, assembly ?? ResourceAssembly);
}, CombinePaths(pathsToIgnore)));
return this;
}
/// <summary>
/// Adds a custom expectation assertion that an event was published where the provided <paramref name="customAssert"/> will be invoked to perform the assertion against the actual <see cref="DestinationEvent"/> (with any specified JSON paths to ignore).
/// </summary>
/// <param name="destination">The expected destination (i.e. topic) name.</param>
/// <param name="customAssert">The custom assert action.</param>
/// <param name="pathsToIgnore">Any additional JSON paths to ignore from the underlying <see cref="CloudEvent"/> comparison.</param>
/// <returns>The <see cref="EventExpectationsConfig"/> to support fluent-style method-chaining.</returns>
/// <remarks>The <see cref="EventExpectationAssertor.AssertDestination(string)"/> and <see cref="EventExpectationAssertor.AssertCloudEvent(CloudEvent, CloudEvent)"/> methods can be used within the custom assert action for consistency.</remarks>
public EventExpectationsConfig AssertCustom(string destination, Action<EventExpectationAssertor, AssertArgs, DestinationEvent> customAssert, params IEnumerable<string> pathsToIgnore)
{
ExpectEvents();
_assertors.Add(new EventExpectationAssertor(this, destination, customAssert, CombinePaths(pathsToIgnore)));
return this;
}
/// <summary>
/// Combines the specified array of paths to ignore with the existing set of ignored paths.
/// </summary>
private string[] CombinePaths(IEnumerable<string> pathsToIgnore) => _pathsToIgnore.Count == 0 ? [.. pathsToIgnore] : [.. _pathsToIgnore, .. pathsToIgnore];
/// <summary>
/// Performs the assertion of the expected events against those that were published, throwing an exception if any expectations were not met.
/// </summary>
/// <param name="args">The <see cref="AssertArgs"/>.</param>
internal void Assert(AssertArgs args)
{
Tester.Implementor.WriteLine($" > '{ServiceKey}' events.");
// High-level checks.
if (!Tester.SharedState.RequestStateData(RequestId).TryGetValue(ServiceKey, out var obj) || obj is not DestinationEvent[] events || events.Length == 0)
{
if (_expectNoEvents)
{
Tester.Implementor.WriteLine(" > Expected no events and there were none.");
return;
}
args.Tester.Implementor.AssertFail($"Expected '{ServiceKey}' events; however, no events were found to be published.");
return;
}
if (_expectNoEvents)
{
Tester.Implementor.AssertFail($"Expected no '{ServiceKey}'events; however, {events.Length} found to be published.");
return;
}
if (_assertAllEvents is not null)
{
_assertAllEvents(args, events);
return;
}
if (_assertors.Count == 0)
{
Tester.Implementor.WriteLine($" > Expected events; and {events.Length} found to be published.");
return;
}
if (_assertors.Count != events.Length)
{
Tester.Implementor.AssertFail($"Expected {_assertors.Count} '{ServiceKey}' events; however, {events.Length} were found to be published.");
return;
}
// Iterate and check each.
var index = 0;
foreach (var assertor in _assertors)
{
Tester.Implementor.WriteLine($" > Event {index + 1} of {_assertors.Count}.");
assertor.Assert(args, events[index]);
index++;
}
Tester.Implementor.WriteLine(" > Event expectations met successfully.");
}
}