-
Notifications
You must be signed in to change notification settings - Fork 4
test: add a test case for IntegrationEventHandler #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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"; | ||
| } |
32 changes: 32 additions & 0 deletions
32
.../Cnblogs.Architecture.IntegrationTestProject/EventHandlers/TestIntegrationEventHandler.cs
This file contains hidden or 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,32 @@ | ||
| using System.Diagnostics; | ||
| using Cnblogs.Architecture.Ddd.EventBus.Abstractions; | ||
| using Cnblogs.Architecture.TestIntegrationEvents; | ||
| using MediatR; | ||
|
|
||
| namespace Cnblogs.Architecture.IntegrationTestProject.EventHandlers; | ||
|
|
||
| public class TestIntegrationEventHandler : IIntegrationEventHandler<TestIntegrationEvent> | ||
| { | ||
| private readonly IHttpContextAccessor _httpContextAccessor; | ||
| private readonly ILogger _logger; | ||
|
|
||
| public TestIntegrationEventHandler(IHttpContextAccessor httpContextAccessor, ILogger<TestIntegrationEventHandler> logger) | ||
| { | ||
| _httpContextAccessor = httpContextAccessor; | ||
| _logger = logger; | ||
| } | ||
|
|
||
| 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; | ||
| }); | ||
|
|
||
| _logger.LogInformation("Handled integration event {event}.", notification); | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
| } |
11 changes: 7 additions & 4 deletions
11
test/Cnblogs.Architecture.IntegrationTestProject/Program.cs
This file contains hidden or 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
3 changes: 1 addition & 2 deletions
3
test/Cnblogs.Architecture.IntegrationTests/Cnblogs.Architecture.IntegrationTests.csproj
This file contains hidden or 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 hidden or 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.
39 changes: 39 additions & 0 deletions
39
test/Cnblogs.Architecture.IntegrationTests/IntegrationEventHandlerTests.cs
This file contains hidden or 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,39 @@ | ||
| using System.Net.Http.Json; | ||
| using Cnblogs.Architecture.IntegrationTestProject; | ||
| using Cnblogs.Architecture.TestIntegrationEvents; | ||
| using FluentAssertions; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Cnblogs.Architecture.IntegrationTests; | ||
|
|
||
| [Collection(IntegrationTestCollection.Name)] | ||
| public class IntegrationEventHandlerTests | ||
| { | ||
| private readonly IntegrationTestFactory _factory; | ||
| private readonly ITestOutputHelper _testOutputHelper; | ||
|
|
||
| public IntegrationEventHandlerTests(IntegrationTestFactory factory, ITestOutputHelper testOutputHelper) | ||
| { | ||
| _factory = factory; | ||
| _testOutputHelper = testOutputHelper; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task IntegrationEventHandler_TestIntegrationEvent_SuccessAsync() | ||
| { | ||
| // Arrange | ||
| var client = _factory.CreateClient(); | ||
| var @event = new TestIntegrationEvent(Guid.NewGuid(), DateTimeOffset.Now, "Hello World!"); | ||
|
|
||
| // Act | ||
| var subscriptions = await client.GetFromJsonAsync<Subscription[]>("/dapr/subscribe"); | ||
| var sub = subscriptions!.First(x => x.Route.Contains(nameof(TestIntegrationEvent))); | ||
| var response = await client.PostAsJsonAsync(sub.Route, @event); | ||
| _testOutputHelper.WriteLine("Subscription Route: " + sub.Route); | ||
|
|
||
| // Assert | ||
| response.Should().BeSuccessful(); | ||
| response.Headers.Should().ContainKey(Constants.IntegrationEventIdHeaderName) | ||
| .WhoseValue.First().Should().Be(@event.Id.ToString()); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
test/Cnblogs.Architecture.IntegrationTests/IntegrationTestCollection.cs
This file contains hidden or 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 hidden or 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 hidden or 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 | ||
ikesnowy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.