-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEventExpectationAssertor.cs
More file actions
110 lines (96 loc) · 6.05 KB
/
Copy pathEventExpectationAssertor.cs
File metadata and controls
110 lines (96 loc) · 6.05 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
namespace CoreEx.UnitTesting.Events;
/// <summary>
/// Provides assertion capabilities for an expected event.
/// </summary>
public sealed class EventExpectationAssertor
{
private readonly EventExpectationsConfig _config;
private readonly string? _expectedDestination;
private readonly string[] _pathsToIgnore;
private readonly Func<EventExpectationAssertor, AssertArgs, DestinationEvent, CloudEvent>? _expectedEventFactory;
private readonly Action<EventExpectationAssertor, AssertArgs, DestinationEvent>? _customAssert;
private JsonSerializerOptions? _jsonSerializerOptions;
private IHostSettings? _hostSettings;
private IEventFormatter? _eventFormatter;
/// <summary>
/// Initializes a new instance of the <see cref="EventExpectationAssertor"/> class with an <paramref name="expectedEventFactory"/>.
/// </summary>
/// <param name="config">The owning <see cref="EventExpectationsConfig"/>.</param>
/// <param name="expectedDestination">The expected destination.</param>
/// <param name="expectedEventFactory">The function to create the expected <see cref="CloudEvent"/>.</param>
/// <param name="pathsToIgnore">The JSON paths to ignore.</param>
internal EventExpectationAssertor(EventExpectationsConfig config, string? expectedDestination, Func<EventExpectationAssertor, AssertArgs, DestinationEvent, CloudEvent> expectedEventFactory, IEnumerable<string> pathsToIgnore)
{
_config = config;
_expectedDestination = expectedDestination;
_expectedEventFactory = expectedEventFactory;
_pathsToIgnore = [.. pathsToIgnore];
// Copy out key attributes from the config for use in the assertion - need the now version.
_jsonSerializerOptions = _config.JsonSerializerOptions;
_hostSettings = _config.HostSettings;
_eventFormatter = _config.EventFormatter;
}
/// <summary>
/// Initializes a new instance of the <see cref="EventExpectationAssertor"/> class with a <paramref name="customAssert"/>.
/// </summary>
/// <param name="config">The owning <see cref="EventExpectationsConfig"/>.</param>
/// <param name="expectedDestination">The expected destination.</param>
/// <param name="customAssert">The custom assert action.</param>
/// <param name="pathsToIgnore">The JSON paths to ignore.</param>
internal EventExpectationAssertor(EventExpectationsConfig config, string? expectedDestination, Action<EventExpectationAssertor, AssertArgs, DestinationEvent> customAssert, IEnumerable<string> pathsToIgnore)
{
_config = config;
_expectedDestination = expectedDestination;
_customAssert = customAssert;
_pathsToIgnore = [.. pathsToIgnore];
// Copy out key attributes from the config for use in the assertion - need the now version.
_jsonSerializerOptions = _config.JsonSerializerOptions;
_hostSettings = _config.HostSettings;
_eventFormatter = _config.EventFormatter;
}
/// <summary>
/// Gets the owning <see cref="TesterBase"/>.
/// </summary>
public TesterBase Tester => _config.Tester;
/// <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 => _jsonSerializerOptions ??= _config.Tester.Services.GetService<JsonSerializerOptions>() ?? CoreEx.Json.JsonDefaults.SerializerOptions;
/// <summary>
/// Gets 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 => _hostSettings ??= _config.Tester.Services.GetService<IHostSettings>() ?? throw new InvalidOperationException($"A {nameof(IHostSettings)} instance is required to perform the event assertion; either set on the config or ensure it is registered in the services.");
/// <summary>
/// Gets 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 => _eventFormatter ??= _config.Tester.Services.GetService<IEventFormatter>() ?? new EventFormatter(HostSettings);
/// <summary>
/// Asserts that the expected and actual <see cref="DestinationEvent"/> are equal, ignoring any specified paths.
/// </summary>
/// <param name="args">The <see cref="AssertArgs"/>.</param>
/// <param name="actual">The actual <see cref="DestinationEvent"/>.</param>
internal void Assert(AssertArgs args, DestinationEvent actual)
{
AssertDestination(actual.Destination);
if (_expectedEventFactory is not null)
AssertCloudEvent(_expectedEventFactory(this, args, actual), actual.Event);
else
_customAssert?.Invoke(this, args, actual);
}
/// <summary>
/// Asserts that the previously configured expected destination and <paramref name="actual"/> destination are equal.
/// </summary>
/// <param name="actual">The actual destination.</param>
public void AssertDestination(string actual) => Tester.Implementor.AssertAreEqual(_expectedDestination, actual, $"Expected '{_config.ServiceKey}' event destination '{_expectedDestination}'; but found '{actual}'.");
/// <summary>
/// Asserts that the expected and actual <see cref="CloudEvent"/> are equal, ignoring any previously configured JSON paths.
/// </summary>
/// <param name="expected">The expected <see cref="CloudEvent"/>.</param>
/// <param name="actual">The actual <see cref="CloudEvent"/>.</param>
public void AssertCloudEvent(CloudEvent expected, CloudEvent actual)
{
var ej = expected?.EncodeToJsonElement(JsonSerializerOptions) ?? null;
var aj = actual?.EncodeToJsonElement(JsonSerializerOptions) ?? null;
ObjectComparer.Assert(new UnitTestEx.Json.JsonElementComparerOptions { PreambleText = $"'{_config.ServiceKey}' event comparison." }, ej, aj, _pathsToIgnore);
}
}