Skip to content

Commit 5583eab

Browse files
authored
Merge pull request #15 from cnblogs/add-test-case
test: add a test case for IntegrationEventHandler
2 parents fcf3070 + 5c96567 commit 5583eab

File tree

14 files changed

+151
-25
lines changed

14 files changed

+151
-25
lines changed

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ indent_style = space
7979
indent_size = 4
8080
tab_width = 4
8181

82+
[*.{xml,csproj}]
83+
indent_size = 2
84+
tab_width = 2
85+
8286
#### C# Coding Conventions ####
8387
[*.cs]
8488

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

3-
<ItemGroup>
4-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
5-
</ItemGroup>
3+
<ItemGroup>
4+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
5+
</ItemGroup>
66

7-
<ItemGroup>
8-
<ProjectReference Include="..\..\src\Cnblogs.Architecture.Ddd.Cqrs.AspNetCore\Cnblogs.Architecture.Ddd.Cqrs.AspNetCore.csproj" />
9-
<ProjectReference Include="..\..\src\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr.csproj" />
10-
<ProjectReference Include="..\Cnblogs.Architecture.TestIntegrationEvents\Cnblogs.Architecture.TestIntegrationEvents.csproj" />
11-
</ItemGroup>
7+
<ItemGroup>
8+
<ProjectReference Include="..\..\src\Cnblogs.Architecture.Ddd.Cqrs.AspNetCore\Cnblogs.Architecture.Ddd.Cqrs.AspNetCore.csproj" />
9+
<ProjectReference Include="..\..\src\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr.csproj" />
10+
<ProjectReference Include="..\Cnblogs.Architecture.TestIntegrationEvents\Cnblogs.Architecture.TestIntegrationEvents.csproj" />
11+
</ItemGroup>
1212

1313
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Cnblogs.Architecture.IntegrationTestProject;
2+
3+
public static class Constants
4+
{
5+
public const string AppName = "test-web";
6+
public const string IntegrationEventIdHeaderName = "X-IntegrationEvent-Id";
7+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Diagnostics;
2+
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
3+
using Cnblogs.Architecture.TestIntegrationEvents;
4+
using MediatR;
5+
6+
namespace Cnblogs.Architecture.IntegrationTestProject.EventHandlers;
7+
8+
public class TestIntegrationEventHandler : IIntegrationEventHandler<TestIntegrationEvent>
9+
{
10+
private readonly IHttpContextAccessor _httpContextAccessor;
11+
private readonly ILogger _logger;
12+
13+
public TestIntegrationEventHandler(IHttpContextAccessor httpContextAccessor, ILogger<TestIntegrationEventHandler> logger)
14+
{
15+
_httpContextAccessor = httpContextAccessor;
16+
_logger = logger;
17+
}
18+
19+
public Task Handle(TestIntegrationEvent notification, CancellationToken cancellationToken)
20+
{
21+
var context = _httpContextAccessor.HttpContext;
22+
context?.Response.OnStarting(() =>
23+
{
24+
context.Response.Headers.Add(Constants.IntegrationEventIdHeaderName, notification.Id.ToString());
25+
return Task.CompletedTask;
26+
});
27+
28+
_logger.LogInformation("Handled integration event {event}.", notification);
29+
30+
return Task.CompletedTask;
31+
}
32+
}

test/Cnblogs.Architecture.IntegrationTestProject/Program.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1+
using System.Reflection;
12
using Cnblogs.Architecture.Ddd.Cqrs.AspNetCore;
23
using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr;
4+
using Cnblogs.Architecture.IntegrationTestProject;
35
using Cnblogs.Architecture.IntegrationTestProject.Application.Commands;
46
using Cnblogs.Architecture.IntegrationTestProject.Application.Queries;
57
using Cnblogs.Architecture.IntegrationTestProject.Payloads;
68
using Cnblogs.Architecture.TestIntegrationEvents;
79

8-
const string appName = "test-web";
9-
1010
var builder = WebApplication.CreateBuilder(args);
1111

12-
builder.Services.AddCqrs(typeof(Cnblogs.Architecture.IntegrationTestProject.Program).Assembly)
12+
builder.Services.AddCqrs(
13+
Assembly.GetExecutingAssembly(),
14+
typeof(TestIntegrationEvent).Assembly)
1315
.AddDefaultDateTimeAndRandomProvider();
14-
builder.Services.AddDaprEventBus(appName);
16+
builder.Services.AddDaprEventBus(Constants.AppName);
1517
builder.Services.AddControllers().AddCqrsModelBinderProvider();
1618

1719
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
1820
builder.Services.AddCnblogsApiVersioning();
1921
builder.Services.AddSwaggerGen();
22+
builder.Services.AddHttpContextAccessor();
2023

2124
var app = builder.Build();
2225

test/Cnblogs.Architecture.IntegrationTests/Cnblogs.Architecture.IntegrationTests.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
1+
<Project Sdk="Microsoft.NET.Sdk">
32
<ItemGroup>
43
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.2" />
54
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />

test/Cnblogs.Architecture.IntegrationTests/DaprTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public async Task Dapr_SubscribeEndpoint_OkAsync()
3232
var response = await httpClient.GetAsync("/dapr/subscribe");
3333

3434
// Assert
35-
response.StatusCode.Should().Be(HttpStatusCode.OK);
35+
response.Should().BeSuccessful();
3636
var responseText = await response.Content.ReadAsStringAsync();
37+
Debug.WriteLine(responseText);
3738
responseText.Should().Contain(nameof(TestIntegrationEvent));
3839
}
3940

test/Cnblogs.Architecture.IntegrationTests/DddWebTestCollection.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Net.Http.Json;
2+
using Cnblogs.Architecture.IntegrationTestProject;
3+
using Cnblogs.Architecture.TestIntegrationEvents;
4+
using FluentAssertions;
5+
using Xunit.Abstractions;
6+
7+
namespace Cnblogs.Architecture.IntegrationTests;
8+
9+
[Collection(IntegrationTestCollection.Name)]
10+
public class IntegrationEventHandlerTests
11+
{
12+
private readonly IntegrationTestFactory _factory;
13+
private readonly ITestOutputHelper _testOutputHelper;
14+
15+
public IntegrationEventHandlerTests(IntegrationTestFactory factory, ITestOutputHelper testOutputHelper)
16+
{
17+
_factory = factory;
18+
_testOutputHelper = testOutputHelper;
19+
}
20+
21+
[Fact]
22+
public async Task IntegrationEventHandler_TestIntegrationEvent_SuccessAsync()
23+
{
24+
// Arrange
25+
var client = _factory.CreateClient();
26+
var @event = new TestIntegrationEvent(Guid.NewGuid(), DateTimeOffset.Now, "Hello World!");
27+
28+
// Act
29+
var subscriptions = await client.GetFromJsonAsync<Subscription[]>("/dapr/subscribe");
30+
var sub = subscriptions!.First(x => x.Route.Contains(nameof(TestIntegrationEvent)));
31+
var response = await client.PostAsJsonAsync(sub.Route, @event);
32+
_testOutputHelper.WriteLine("Subscription Route: " + sub.Route);
33+
34+
// Assert
35+
response.Should().BeSuccessful();
36+
response.Headers.Should().ContainKey(Constants.IntegrationEventIdHeaderName)
37+
.WhoseValue.First().Should().Be(@event.Id.ToString());
38+
}
39+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Cnblogs.Architecture.IntegrationTests;
2+
3+
[CollectionDefinition(Name)]
4+
public class IntegrationTestCollection : ICollectionFixture<IntegrationTestFactory>
5+
{
6+
public const string Name = nameof(IntegrationTestCollection);
7+
}

0 commit comments

Comments
 (0)