Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ indent_style = space
indent_size = 4
tab_width = 4

[*.{xml,csproj}]
indent_size = 2
tab_width = 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,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 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,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<Project Sdk="Microsoft.NET.Sdk">
<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,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());
}
}
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);