forked from oskardudycz/EventSourcing.NetCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassesWithMediatRTest.cs
112 lines (92 loc) · 3.51 KB
/
ClassesWithMediatRTest.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
using EventPipelines.MediatR;
using FluentAssertions;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace EventPipelines.Tests;
public class ClassesWithMediatRTest
{
public record UserAdded(
string FirstName,
string LastName,
bool IsAdmin
) : INotification;
public record AdminAdded(
string FirstName,
string LastName
) : INotification;
public record AdminGrantedInTenant(
string FirstName,
string LastName,
string TenantName
) : INotification;
public class AdminStorage
{
public static readonly string[] TenantNames = ["FB", "Google", "Twitter"];
public List<AdminGrantedInTenant> AdminsInTenants = [];
public List<AdminAdded> GlobalAdmins = [];
}
public class IsAdmin: IEventFilter<UserAdded>
{
public ValueTask<bool> Handle(UserAdded @event, CancellationToken ct) =>
ValueTask.FromResult(@event.IsAdmin);
}
public class ToAdminAdded: IEventTransformation<UserAdded, AdminAdded>
{
public ValueTask<AdminAdded> Handle(UserAdded @event, CancellationToken ct) =>
ValueTask.FromResult(new AdminAdded(@event.FirstName, @event.LastName));
}
public class HandleAdminAdded(AdminStorage adminStorage): IEventHandler<AdminAdded>
{
public ValueTask Handle(AdminAdded @event, CancellationToken ct)
{
adminStorage.GlobalAdmins.Add(@event);
return ValueTask.CompletedTask;
}
}
public class SendToTenants: IEventTransformation<AdminAdded, List<AdminGrantedInTenant>>
{
public ValueTask<List<AdminGrantedInTenant>> Handle(AdminAdded @event, CancellationToken ct) =>
ValueTask.FromResult(AdminStorage.TenantNames
.Select(tenantName =>
new AdminGrantedInTenant(@event.FirstName, @event.LastName, tenantName)
)
.ToList());
}
public class HandleAdminGrantedInTenant(AdminStorage adminStorage): IEventHandler<AdminGrantedInTenant>
{
public ValueTask Handle(AdminGrantedInTenant @event, CancellationToken ct)
{
adminStorage.AdminsInTenants.Add(@event);
return ValueTask.CompletedTask;
}
}
private readonly IServiceProvider sp;
public ClassesWithMediatRTest()
{
var serviceCollection = new ServiceCollection();
serviceCollection
.AddSingleton<AdminStorage>()
.AddEventBus()
.RouteEventsFromMediatR()
.AddScoped<IMediator, Mediator>()
.AddEventHandler<IsAdmin>()
.AddEventHandler<ToAdminAdded>()
.AddEventHandler<HandleAdminAdded>()
.AddEventHandler<SendToTenants>()
.AddEventHandler<HandleAdminGrantedInTenant>();
sp = serviceCollection.BuildServiceProvider();
}
[Fact]
public async Task ShouldWork()
{
var eventBus = sp.GetRequiredService<IMediator>();
await eventBus.Publish(new UserAdded("Oskar", "TheGrouch", false), CancellationToken.None);
await eventBus.Publish(new UserAdded("Big", "Bird", true), CancellationToken.None);
var adminStorage = sp.GetRequiredService<AdminStorage>();
adminStorage.AdminsInTenants.Should().HaveCount(3);
adminStorage.GlobalAdmins.Should().HaveCount(1);
adminStorage.GlobalAdmins.Single().FirstName.Should().Be("Big");
adminStorage.GlobalAdmins.Single().LastName.Should().Be("Bird");
}
}