Skip to content

Commit 0bdda4e

Browse files
committed
add a test case for IntegrationEventHandler
1 parent c7fa4a6 commit 0bdda4e

File tree

14 files changed

+167
-25
lines changed

14 files changed

+167
-25
lines changed

.editorconfig

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

82+
[*.xml]
83+
indent_size = 2
84+
8285
#### C# Coding Conventions ####
8386
[*.cs]
8487

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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
2+
using Cnblogs.Architecture.TestIntegrationEvents;
3+
using MediatR;
4+
using System.Diagnostics;
5+
6+
namespace Cnblogs.Architecture.IntegrationTestProject.EventHandlers;
7+
8+
public class TestIntegrationEventHandler : IIntegrationEventHandler<TestIntegrationEvent>
9+
{
10+
private readonly IHttpContextAccessor _httpContextAccessor;
11+
12+
public TestIntegrationEventHandler(IHttpContextAccessor httpContextAccessor)
13+
{
14+
_httpContextAccessor = httpContextAccessor;
15+
}
16+
17+
public Task Handle(TestIntegrationEvent notification, CancellationToken cancellationToken)
18+
{
19+
var context = _httpContextAccessor.HttpContext;
20+
context?.Response.OnStarting(() =>
21+
{
22+
context.Response.Headers.Add(Constants.IntegrationEventIdHeaderName, notification.Id.ToString());
23+
return Task.CompletedTask;
24+
});
25+
26+
Debug.WriteLine($"notification message: " + notification.Message);
27+
28+
return Task.CompletedTask;
29+
}
30+
}

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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Nullable>disable</Nullable>
4+
</PropertyGroup>
35
<ItemGroup>
46
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.2" />
57
<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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Diagnostics;
2+
using System.IO.Pipes;
3+
using System.Net;
4+
using System.Net.Http.Json;
5+
using Cnblogs.Architecture.IntegrationTestProject;
6+
using Cnblogs.Architecture.IntegrationTestProject.EventHandlers;
7+
using Cnblogs.Architecture.TestIntegrationEvents;
8+
using FluentAssertions;
9+
using MediatR;
10+
using Microsoft.AspNetCore.Http;
11+
using Microsoft.AspNetCore.Mvc.Testing;
12+
using Microsoft.Extensions.DependencyInjection;
13+
14+
namespace Cnblogs.Architecture.IntegrationTests;
15+
16+
[Collection(IntegrationTestCollection.Name)]
17+
public class IntegrationEventHandlerTests
18+
{
19+
private readonly IntegrationTestFactory _factory;
20+
21+
public IntegrationEventHandlerTests(IntegrationTestFactory factory)
22+
{
23+
_factory = factory;
24+
}
25+
26+
[Fact]
27+
public async Task IntegrationEventHandler_TestIntegrationEvent_SuccessAsync()
28+
{
29+
// Arrange
30+
var client = _factory.CreateClient();
31+
32+
// Act
33+
var subscriptions = await client.GetFromJsonAsync<Subscription[]>("/dapr/subscribe");
34+
35+
// Assert
36+
subscriptions.Should().NotBeNullOrEmpty();
37+
38+
// Act
39+
var sub = subscriptions.FirstOrDefault(s => s.Route.Contains(nameof(TestIntegrationEvent)));
40+
41+
// Assert
42+
sub.Should().NotBeNull();
43+
44+
Debug.WriteLine("Subscription Route: " + sub.Route);
45+
46+
// Act
47+
var @event = new TestIntegrationEvent(Guid.NewGuid(), DateTimeOffset.Now, "Hello World!");
48+
var response = await client.PostAsJsonAsync(sub.Route, @event);
49+
50+
// Assert
51+
response.Should().BeSuccessful();
52+
Assert.True(response.Headers.TryGetValues(Constants.IntegrationEventIdHeaderName, out var values));
53+
values.First().Should().Be(@event.Id.ToString());
54+
}
55+
}
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)