Skip to content

Commit

Permalink
add a test case for IntegrationEventHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
cnblogs-dudu committed Feb 8, 2023
1 parent c7fa4a6 commit 0bdda4e
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ indent_style = space
indent_size = 4
tab_width = 4

[*.xml]
indent_size = 2

#### C# Coding Conventions ####
[*.cs]

Expand Down
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 test/Cnblogs.Architecture.IntegrationTestProject/Constants.cs
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";
}
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 test/Cnblogs.Architecture.IntegrationTestProject/Program.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
using System.Reflection;
using Cnblogs.Architecture.Ddd.Cqrs.AspNetCore;
using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr;
using Cnblogs.Architecture.IntegrationTestProject;
using Cnblogs.Architecture.IntegrationTestProject.Application.Commands;
using Cnblogs.Architecture.IntegrationTestProject.Application.Queries;
using Cnblogs.Architecture.IntegrationTestProject.Payloads;
using Cnblogs.Architecture.TestIntegrationEvents;

const string appName = "test-web";

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddCqrs(typeof(Cnblogs.Architecture.IntegrationTestProject.Program).Assembly)
builder.Services.AddCqrs(
Assembly.GetExecutingAssembly(),
typeof(TestIntegrationEvent).Assembly)
.AddDefaultDateTimeAndRandomProvider();
builder.Services.AddDaprEventBus(appName);
builder.Services.AddDaprEventBus(Constants.AppName);
builder.Services.AddControllers().AddCqrsModelBinderProvider();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddCnblogsApiVersioning();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpContextAccessor();

var app = builder.Build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
Expand Down
3 changes: 2 additions & 1 deletion test/Cnblogs.Architecture.IntegrationTests/DaprTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public async Task Dapr_SubscribeEndpoint_OkAsync()
var response = await httpClient.GetAsync("/dapr/subscribe");

// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
response.Should().BeSuccessful();
var responseText = await response.Content.ReadAsStringAsync();
Debug.WriteLine(responseText);
responseText.Should().Contain(nameof(TestIntegrationEvent));
}

Expand Down

This file was deleted.

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());
}
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

namespace Cnblogs.Architecture.IntegrationTests;

public class DddWebTestFactory : WebApplicationFactory<Program>
public class IntegrationTestFactory : WebApplicationFactory<Program>
{
}
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 test/Cnblogs.Architecture.IntegrationTests/Subscription.cs
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

namespace Cnblogs.Architecture.TestIntegrationEvents;

public record TestIntegrationEvent(Guid Id, DateTimeOffset CreatedTime) : IntegrationEvent(Id, CreatedTime);
public record TestIntegrationEvent(Guid Id, DateTimeOffset CreatedTime, string Message) : IntegrationEvent(Id, CreatedTime);

0 comments on commit 0bdda4e

Please sign in to comment.