Skip to content

Commit 77abeb4

Browse files
authored
Add tests (#4)
* Add middleware support (#1) Add functionality for configuring and executing custom middlewares before or after commands. * update example * update example * Add tests and fix Command with response Command with response when middleware configured was not working properly * Tests done
1 parent 810e841 commit 77abeb4

21 files changed

+413
-40
lines changed

.github/workflows/open-mediator-nuget-package.yml

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,32 @@ jobs:
3737
name: solution-build
3838
path: OpenMediator.*.nupkg
3939

40-
# tests:
41-
# name: Run tests
42-
# runs-on: ubuntu-latest
43-
# needs: [build]
44-
# steps:
45-
# - uses: actions/checkout@v4
46-
# # - name: Test
47-
# # run: dotnet test OpenMediator.sln
48-
# - name: Test
49-
# run: dotnet test --collect:"XPlat Code Coverage" --results-directory:./CoverageResults/ OpenMediator.sln
50-
# - name: Move coverage result files
51-
# run: |
52-
# echo MOVER COVERAGE RESULTS
53-
# cp CoverageResults/*/*.xml CoverageResults/
54-
# - name: Codecov
55-
# uses: codecov/[email protected]
56-
# with:
57-
# name: Code coverage report
58-
# files: CoverageResults/coverage.cobertura.xml
59-
# flags: unittests
60-
# verbose: false
40+
tests:
41+
name: Run tests
42+
runs-on: ubuntu-latest
43+
needs: [build]
44+
steps:
45+
- uses: actions/checkout@v4
46+
- name: Test
47+
run: dotnet test OpenMediator.sln
48+
- name: Test
49+
run: dotnet test --collect:"XPlat Code Coverage" --results-directory:./CoverageResults/ OpenMediator.sln
50+
- name: Move coverage result files
51+
run: |
52+
echo MOVER COVERAGE RESULTS
53+
cp CoverageResults/*/*.xml CoverageResults/
54+
- name: Codecov
55+
uses: codecov/[email protected]
56+
with:
57+
name: Code coverage report
58+
files: CoverageResults/coverage.cobertura.xml
59+
flags: unittests
60+
verbose: false
6161

6262
publish-github-package:
6363
name: Publish GitHub package
6464
runs-on: ubuntu-latest
65-
# needs: [build, tests]
66-
needs: [build]
65+
needs: [build, tests]
6766
steps:
6867
- uses: actions/download-artifact@v4
6968
with:
@@ -76,8 +75,7 @@ jobs:
7675
name: Publish GitHub release
7776
if: ${{ false }} # step disabled with false
7877
runs-on: ubuntu-latest
79-
# needs: [build, tests]
80-
needs: [build]
78+
needs: [build, tests]
8179
steps:
8280
- uses: actions/download-artifact@v4
8381
with:
@@ -98,8 +96,7 @@ jobs:
9896
environment:
9997
name: production
10098
url: https://www.nuget.org/packages/OpenMediator/
101-
# needs: [build, tests]
102-
needs: [build]
99+
needs: [build, tests]
103100
steps:
104101
- uses: actions/download-artifact@v4
105102
with:

OpenMediator.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{18DC
1616
EndProject
1717
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{00CDF08F-6557-44E0-9B54-D0FC0CB1879A}"
1818
EndProject
19-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenMediator.Integration.Test", "test\OpenMediator.Integration.Test\OpenMediator.Integration.Test.csproj", "{D06610C3-70D7-48FD-9075-A40CA685B58A}"
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenMediator.Unit.Test", "test\OpenMediator.Unit.Test\OpenMediator.Unit.Test.csproj", "{D06610C3-70D7-48FD-9075-A40CA685B58A}"
2020
EndProject
21-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenMediator.Shared.Integration.Test", "test\OpenMediator.Shared.Integration.Test\OpenMediator.Shared.Integration.Test.csproj", "{2436B77B-FA64-4DF2-A93D-DA175B411C44}"
21+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenMediator.Shared.Test", "test\OpenMediator.Shared.Test\OpenMediator.Shared.Test.csproj", "{2436B77B-FA64-4DF2-A93D-DA175B411C44}"
2222
EndProject
2323
Global
2424
GlobalSection(SolutionConfigurationPlatforms) = preSolution

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,32 @@ Also you can configure and execute custom middlewares before or after the comman
5555
1. Define your middleware by implementing the `IMiddleware` interface:
5656

5757
```csharp
58-
public class CustomMediatorMiddleware(ILogger<CustomMediatorMiddleware> _logger) : IMediatorMiddleware
58+
public class CustomMediatorMiddleware() : IMediatorMiddleware
5959
{
6060
public async Task ExecuteAsync<TCommand>(TCommand command, Func<Task> next)
6161
where TCommand : ICommand
6262
{
6363
// Do something before the command
64-
Task.Delay(1000).Wait();
64+
await Task.Delay(500);
6565

6666
await next();
6767

6868
// Do something after the command
69-
Task.Delay(1000).Wait();
69+
await Task.Delay(500);
70+
}
71+
72+
public async Task<TResponse> ExecuteAsync<TCommand, TResponse>(TCommand command, Func<Task<TResponse>> next)
73+
where TCommand : ICommand<TResponse>
74+
{
75+
// Do something before the command
76+
await Task.Delay(500);
77+
78+
var result = await next();
79+
80+
// Do something after the command
81+
await Task.Delay(500);
82+
83+
return result;
7084
}
7185
}
7286
```

examples/WebApplicationAPI/MediatorMiddleware/CustomMediatorMiddleware.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,27 @@ public async Task ExecuteAsync<TCommand>(TCommand command, Func<Task> next)
1010
{
1111
// Do something before the command
1212
_logger.LogInformation("=========> Before command: {Command}", command.GetType().Name);
13-
Task.Delay(1000).Wait();
13+
await Task.Delay(1000);
1414

1515
await next();
1616

1717
// Do something after the command
1818
_logger.LogInformation("=========> After command: {Command}", command.GetType().Name);
19-
Task.Delay(1000).Wait();
19+
await Task.Delay(1000);
20+
}
21+
22+
public Task ExecuteAsync<TCommand, TResponse>(TCommand command, Func<Task<TResponse>> next) where TCommand : ICommand<TResponse>
23+
{
24+
// Do something before the command
25+
_logger.LogInformation("=========> Before command: {Command}", command.GetType().Name);
26+
//await Task.Delay(1000);
27+
28+
var result = next();
29+
30+
// Do something after the command
31+
_logger.LogInformation("=========> After command: {Command}", command.GetType().Name);
32+
//await Task.Delay(1000);
33+
34+
return result;
2035
}
2136
}

examples/WebApplicationAPI/WebApplicationAPI.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.1" />
11-
<PackageReference Include="OpenMediator" Version="0.0.5" />
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.4" />
11+
<PackageReference Include="OpenMediator" Version="1.1.0" />
1212
</ItemGroup>
1313

1414
</Project>

examples/WebApplicationAPI/WebApplicationAPI.http

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ POST {{WebApplicationAPI_HostAddress}}/api/user
88
GET {{WebApplicationAPI_HostAddress}}/api/user/{{id}}
99

1010
###
11+
12+
GET {{WebApplicationAPI_HostAddress}}/api/user/{{id}}
13+
14+
###

src/Buses/DefaultMediatorBus.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public async Task<TResponse> SendAsync<TCommand, TResponse>(TCommand request)
1919
}
2020

2121
private async Task<TResponse> ExecuteMiddlewares<TCommand, TResponse>(TCommand command, Func<Task<TResponse>> next)
22-
where TCommand : ICommand<TResponse>
22+
where TCommand : ICommand<TResponse>
2323
{
24-
var middlewareTask = _middlewares.Aggregate(() => next(), (nextMiddleware, middleware) => () => (Task<TResponse>)middleware.ExecuteAsync(command, nextMiddleware));
24+
var middlewareTask = _middlewares.Aggregate(() => next(), (nextMiddleware, middleware) => async () => await middleware.ExecuteAsync(command, nextMiddleware));
2525
return await middlewareTask();
2626
}
2727

src/Middlewares/IMediatorMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ public interface IMediatorMiddleware
44
{
55
Task ExecuteAsync<TCommand>(TCommand command, Func<Task> next) where TCommand : ICommand;
66

7-
Task ExecuteAsync<TCommand, TResponse>(TCommand command, Func<Task<TResponse>> next) where TCommand : ICommand<TResponse>;
7+
Task<TResponse> ExecuteAsync<TCommand, TResponse>(TCommand command, Func<Task<TResponse>> next) where TCommand : ICommand<TResponse>;
88
}

src/OpenMediator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageTags>mediator;mediator-pattern</PackageTags>
1212
<Description>Alternative for those who do not want to pay for a mediator implementation.</Description>
1313
<Authors>Sergio Martin</Authors>
14-
<Version>1.1.0</Version>
14+
<Version>1.1.1</Version>
1515
<RepositoryUrl>https://github.com/Sergi0Martin/OpenMediator</RepositoryUrl>
1616
<PackageProjectUrl>https://sergi0martin.github.io/OpenMediator</PackageProjectUrl>
1717
<PackageReadmeFile>README.md</PackageReadmeFile>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace OpenMediator.Shared.Test.Commands;
2+
3+
public record CreateUserCommand(int Id, string Name) : ICommand;
4+
5+
public record CreateUserCommandHandler(TestDependency testDependency) : ICommandHandler<CreateUserCommand>
6+
{
7+
public async Task HandleAsync(CreateUserCommand command)
8+
{
9+
await Task.Delay(500);
10+
testDependency.Call();
11+
}
12+
}

0 commit comments

Comments
 (0)