-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathIntegrationEventBusTest.cs
143 lines (129 loc) · 6.28 KB
/
IntegrationEventBusTest.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
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
#pragma warning disable CS0618
namespace Masa.Contrib.Dispatcher.IntegrationEvents.Dapr.Tests;
[TestClass]
public class IntegrationEventBusTest
{
private Mock<IDispatcherOptions> _options;
private Mock<IOptions<DispatcherOptions>> _dispatcherOptions;
private Mock<IIntegrationEventLogService> _eventLog;
private Mock<IOptionsMonitor<MasaAppConfigureOptions>> _masaAppConfigureOptions;
private Mock<IUnitOfWork> _uoW;
[TestInitialize]
public void Initialize()
{
_options = new();
_options.Setup(option => option.Services).Returns(new ServiceCollection()).Verifiable();
_dispatcherOptions = new();
_dispatcherOptions
.Setup(option => option.Value)
.Returns(() => new DispatcherOptions(_options.Object.Services, AppDomain.CurrentDomain.GetAssemblies()));
_eventLog = new();
_eventLog.Setup(eventLog => eventLog.SaveEventAsync(It.IsAny<IIntegrationEvent>(), null!)).Verifiable();
_eventLog.Setup(eventLog => eventLog.MarkEventAsInProgressAsync(It.IsAny<Guid>())).Verifiable();
_eventLog.Setup(eventLog => eventLog.MarkEventAsPublishedAsync(It.IsAny<Guid>())).Verifiable();
_eventLog.Setup(eventLog => eventLog.MarkEventAsFailedAsync(It.IsAny<Guid>())).Verifiable();
_masaAppConfigureOptions = new();
_masaAppConfigureOptions.Setup(appConfig => appConfig.CurrentValue).Returns(() => new MasaAppConfigureOptions
{
AppId = "Test"
});
_uoW = new();
_uoW.Setup(uoW => uoW.CommitAsync(default)).Verifiable();
_uoW.Setup(uoW => uoW.Transaction).Returns(() => null!);
_uoW.Setup(uoW => uoW.UseTransaction).Returns(true);
}
[TestMethod]
public void TestDispatcherOption()
{
var services = new ServiceCollection();
DispatcherOptions options;
Assert.ThrowsException<ArgumentException>(() =>
{
options = new DispatcherOptions(services, null!);
});
Assert.ThrowsException<ArgumentException>(() =>
{
options = new DispatcherOptions(services, Array.Empty<Assembly>());
});
options = new DispatcherOptions(services, new[] { typeof(IntegrationEventBusTest).Assembly });
Assert.IsTrue(options.Services.Equals(services));
var allEventTypes = new[] { typeof(IntegrationEventBusTest).Assembly }.SelectMany(assembly => assembly.GetTypes())
.Where(type => type.IsClass && type != typeof(IntegrationEvent) && typeof(IEvent).IsAssignableFrom(type)).ToList();
Assert.IsTrue(options.AllEventTypes.Count == allEventTypes.Count());
}
[TestMethod]
public void TestAddMultDaprEventBus()
{
var services = new ServiceCollection();
Mock<IDistributedDispatcherOptions> distributedDispatcherOptions = new();
distributedDispatcherOptions.Setup(option => option.Services).Returns(services).Verifiable();
distributedDispatcherOptions.Setup(option => option.Assemblies).Returns(AppDomain.CurrentDomain.GetAssemblies()).Verifiable();
distributedDispatcherOptions.Object
.UseDaprEventBus<CustomIntegrationEventLogService>()
.UseDaprEventBus<CustomIntegrationEventLogService>();
var serviceProvider = services.BuildServiceProvider();
Assert.IsTrue(serviceProvider.GetServices<IIntegrationEventBus>().Count() == 1);
}
[TestMethod]
public void TestAddDaprEventBus()
{
IServiceCollection services = new ServiceCollection();
services.AddDaprEventBus<CustomIntegrationEventLogService>();
var serviceProvider = services.BuildServiceProvider();
var integrationEventBus = serviceProvider.GetRequiredService<IIntegrationEventBus>();
Assert.IsNotNull(integrationEventBus);
}
[TestMethod]
public void TestNotUseLoggerAndUoW()
{
IServiceCollection services = new ServiceCollection();
services.AddLogging();
services
.AddDaprEventBus<
CustomIntegrationEventLogService>(); //The logger cannot be mocked and cannot verify that the logger is executed only once
var serviceProvider = services.BuildServiceProvider();
var integrationEventBus = serviceProvider.GetRequiredService<IIntegrationEventBus>();
Assert.IsNotNull(integrationEventBus);
}
[TestMethod]
public void TestUseLogger()
{
IServiceCollection services = new ServiceCollection();
services.AddDaprEventBus<CustomIntegrationEventLogService>(AppDomain.CurrentDomain.GetAssemblies(), option =>
{
option.PubSubName = "pubsub";
});
var serviceProvider = services.BuildServiceProvider();
var integrationEventBus = serviceProvider.GetRequiredService<IIntegrationEventBus>();
Assert.IsNotNull(integrationEventBus);
}
[TestMethod]
public void TestAddDaprEventBusAndNullServicesAsync()
{
IServiceCollection services = null!;
Mock<IDistributedDispatcherOptions> distributedDispatcherOptions = new();
distributedDispatcherOptions.Setup(option => option.Services).Returns(services).Verifiable();
distributedDispatcherOptions.Setup(option => option.Assemblies).Returns(AppDomain.CurrentDomain.GetAssemblies()).Verifiable();
Assert.ThrowsException<ArgumentNullException>(() =>
distributedDispatcherOptions.Object.UseDaprEventBus<CustomIntegrationEventLogService>(),
$"Value cannot be null. (Parameter '{nameof(_options.Object.Services)}')");
}
[TestMethod]
public void TestUseDaprReturnNotNull()
{
var services = new ServiceCollection();
services.AddIntegrationEventBus<CustomIntegrationEventLogService>(opt =>
{
opt.UseDapr();
});
Mock<IIntegrationEventLogService> eventLogService = new();
services.AddScoped(_ => eventLogService.Object);
var serviceProvider = services.BuildServiceProvider();
var publisher = serviceProvider.GetService<IPublisher>();
Assert.IsNotNull(publisher);
var integrationEventBus = serviceProvider.GetService<IIntegrationEventBus>();
Assert.IsNotNull(integrationEventBus);
}
}