-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a test case for IntegrationEventHandler
- Loading branch information
1 parent
c7fa4a6
commit 0bdda4e
Showing
14 changed files
with
167 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 9 additions & 9 deletions
18
...gs.Architecture.IntegrationTestProject/Cnblogs.Architecture.IntegrationTestProject.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Cnblogs.Architecture.Ddd.Cqrs.AspNetCore\Cnblogs.Architecture.Ddd.Cqrs.AspNetCore.csproj" /> | ||
<ProjectReference Include="..\..\src\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr.csproj" /> | ||
<ProjectReference Include="..\Cnblogs.Architecture.TestIntegrationEvents\Cnblogs.Architecture.TestIntegrationEvents.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Cnblogs.Architecture.Ddd.Cqrs.AspNetCore\Cnblogs.Architecture.Ddd.Cqrs.AspNetCore.csproj" /> | ||
<ProjectReference Include="..\..\src\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr.csproj" /> | ||
<ProjectReference Include="..\Cnblogs.Architecture.TestIntegrationEvents\Cnblogs.Architecture.TestIntegrationEvents.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
7 changes: 7 additions & 0 deletions
7
test/Cnblogs.Architecture.IntegrationTestProject/Constants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Cnblogs.Architecture.IntegrationTestProject; | ||
|
||
public static class Constants | ||
{ | ||
public const string AppName = "test-web"; | ||
public const string IntegrationEventIdHeaderName = "X-IntegrationEvent-Id"; | ||
} |
30 changes: 30 additions & 0 deletions
30
.../Cnblogs.Architecture.IntegrationTestProject/EventHandlers/TestIntegrationEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Cnblogs.Architecture.Ddd.EventBus.Abstractions; | ||
using Cnblogs.Architecture.TestIntegrationEvents; | ||
using MediatR; | ||
using System.Diagnostics; | ||
|
||
namespace Cnblogs.Architecture.IntegrationTestProject.EventHandlers; | ||
|
||
public class TestIntegrationEventHandler : IIntegrationEventHandler<TestIntegrationEvent> | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
||
public TestIntegrationEventHandler(IHttpContextAccessor httpContextAccessor) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
public Task Handle(TestIntegrationEvent notification, CancellationToken cancellationToken) | ||
{ | ||
var context = _httpContextAccessor.HttpContext; | ||
context?.Response.OnStarting(() => | ||
{ | ||
context.Response.Headers.Add(Constants.IntegrationEventIdHeaderName, notification.Id.ToString()); | ||
return Task.CompletedTask; | ||
}); | ||
|
||
Debug.WriteLine($"notification message: " + notification.Message); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
11 changes: 7 additions & 4 deletions
11
test/Cnblogs.Architecture.IntegrationTestProject/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
test/Cnblogs.Architecture.IntegrationTests/Cnblogs.Architecture.IntegrationTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
test/Cnblogs.Architecture.IntegrationTests/DddWebTestCollection.cs
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
test/Cnblogs.Architecture.IntegrationTests/IntegrationEventHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Diagnostics; | ||
using System.IO.Pipes; | ||
using System.Net; | ||
using System.Net.Http.Json; | ||
using Cnblogs.Architecture.IntegrationTestProject; | ||
using Cnblogs.Architecture.IntegrationTestProject.EventHandlers; | ||
using Cnblogs.Architecture.TestIntegrationEvents; | ||
using FluentAssertions; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Cnblogs.Architecture.IntegrationTests; | ||
|
||
[Collection(IntegrationTestCollection.Name)] | ||
public class IntegrationEventHandlerTests | ||
{ | ||
private readonly IntegrationTestFactory _factory; | ||
|
||
public IntegrationEventHandlerTests(IntegrationTestFactory factory) | ||
{ | ||
_factory = factory; | ||
} | ||
|
||
[Fact] | ||
public async Task IntegrationEventHandler_TestIntegrationEvent_SuccessAsync() | ||
{ | ||
// Arrange | ||
var client = _factory.CreateClient(); | ||
|
||
// Act | ||
var subscriptions = await client.GetFromJsonAsync<Subscription[]>("/dapr/subscribe"); | ||
|
||
// Assert | ||
subscriptions.Should().NotBeNullOrEmpty(); | ||
|
||
// Act | ||
var sub = subscriptions.FirstOrDefault(s => s.Route.Contains(nameof(TestIntegrationEvent))); | ||
|
||
// Assert | ||
sub.Should().NotBeNull(); | ||
|
||
Debug.WriteLine("Subscription Route: " + sub.Route); | ||
|
||
// Act | ||
var @event = new TestIntegrationEvent(Guid.NewGuid(), DateTimeOffset.Now, "Hello World!"); | ||
var response = await client.PostAsJsonAsync(sub.Route, @event); | ||
|
||
// Assert | ||
response.Should().BeSuccessful(); | ||
Assert.True(response.Headers.TryGetValues(Constants.IntegrationEventIdHeaderName, out var values)); | ||
values.First().Should().Be(@event.Id.ToString()); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
test/Cnblogs.Architecture.IntegrationTests/IntegrationTestCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Cnblogs.Architecture.IntegrationTests; | ||
|
||
[CollectionDefinition(Name)] | ||
public class IntegrationTestCollection : ICollectionFixture<IntegrationTestFactory> | ||
{ | ||
public const string Name = nameof(IntegrationTestCollection); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
test/Cnblogs.Architecture.IntegrationTests/IntegrationTestFramework.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Diagnostics; | ||
using System.Text; | ||
using Cnblogs.Architecture.IntegrationTests; | ||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
[assembly: TestFramework($"Cnblogs.Architecture.IntegrationTests.{nameof(IntegrationTestFramework)}", "Cnblogs.Architecture.IntegrationTests")] | ||
|
||
namespace Cnblogs.Architecture.IntegrationTests; | ||
|
||
public class IntegrationTestFramework : XunitTestFramework | ||
{ | ||
public IntegrationTestFramework(IMessageSink messageSink) | ||
: base(messageSink) | ||
{ | ||
Console.OutputEncoding = Encoding.UTF8; | ||
Trace.Listeners.Add(new ConsoleTraceListener()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
test/Cnblogs.Architecture.IntegrationTests/Subscription.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Cnblogs.Architecture.IntegrationTests; | ||
|
||
/// <summary> | ||
/// This class defines subscribe endpoint response for dapr | ||
/// </summary> | ||
internal class Subscription | ||
{ | ||
/// <summary> | ||
/// Gets or sets the topic name. | ||
/// </summary> | ||
public string Topic { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets or sets the pubsub name | ||
/// </summary> | ||
public string PubsubName { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets or sets the route | ||
/// </summary> | ||
public string Route { get; set; } = string.Empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters