Skip to content
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

Refactoring towards Ogooreck #136

Merged
merged 14 commits into from
Jun 16, 2022
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
111 changes: 0 additions & 111 deletions Core.Api.Testing/ApiFixture.cs

This file was deleted.

21 changes: 0 additions & 21 deletions Core.Api.Testing/Core.Api.Testing.csproj

This file was deleted.

17 changes: 0 additions & 17 deletions Core.Api.Testing/ResponseExtensions.cs

This file was deleted.

72 changes: 0 additions & 72 deletions Core.Api.Testing/TestContext.cs

This file was deleted.

25 changes: 0 additions & 25 deletions Core.Api.Testing/TestWebHostBuilder.cs

This file was deleted.

5 changes: 3 additions & 2 deletions Core.Testing/Core.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="FluentAssertions" Version="6.7.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.5" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.6" />
<PackageReference Include="Ogooreck" Version="0.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Core.Api.Testing\Core.Api.Testing.csproj" />
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>
</Project>
15 changes: 8 additions & 7 deletions Core.Testing/EventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ public class EventsLog
public List<object> PublishedEvents { get; } = new();
}

public class EventListener<TEvent>: IEventHandler<TEvent>
where TEvent : notnull
public class EventListener: IEventBus
{
private readonly IEventBus eventBus;
private readonly EventsLog eventsLog;

public EventListener(EventsLog eventsLog)
public EventListener(EventsLog eventsLog, IEventBus eventBus)
{
this.eventBus = eventBus;
this.eventsLog = eventsLog;
}

public Task Handle(TEvent @event, CancellationToken cancellationToken)
public async Task Publish(IEventEnvelope eventEnvelope, CancellationToken ct)
{
eventsLog.PublishedEvents.Add(@event);

return Task.CompletedTask;
eventsLog.PublishedEvents.Add(eventEnvelope);
await eventBus.Publish(eventEnvelope, ct);
}
}

Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
using System.Linq.Expressions;
using Core.Api.Testing;
using Core.Commands;
using Core.Events;
using Core.Events.External;
using Core.Requests;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Core.Testing;

public abstract class ApiWithEventsFixture<TStartup>: ApiFixture<TStartup> where TStartup : class
public class TestWebApplicationFactory<TProject>: WebApplicationFactory<TProject> where TProject : class
{
private readonly EventsLog eventsLog = new();
private readonly DummyExternalEventProducer externalEventProducer = new();
private readonly DummyExternalCommandBus externalCommandBus = new();

public override TestContext CreateTestContext() =>
new TestContext<TStartup>(GetConfiguration, services =>
{
SetupServices?.Invoke(services);
services.AddSingleton(eventsLog);
services.AddSingleton(typeof(IEventHandler<>), typeof(EventListener<>));
services.AddSingleton<IExternalEventProducer>(externalEventProducer);
services.AddSingleton<IEventBus>(sp =>
new EventBusDecoratorWithExternalProducer(sp.GetRequiredService<EventBus>(),
sp.GetRequiredService<IExternalEventProducer>()));
services.AddSingleton<IExternalCommandBus>(externalCommandBus);
services.AddSingleton<IExternalEventConsumer, DummyExternalEventConsumer>();
}, SetupWebHostBuilder);


public IReadOnlyCollection<TEvent> PublishedExternalEventsOfType<TEvent>() where TEvent : IExternalEvent
{
return externalEventProducer.PublishedEvents.OfType<TEvent>().ToList();
}
private readonly string schemaName = $"test{Guid.NewGuid().ToString("N").ToLower()}";

public IReadOnlyCollection<TCommand> PublishedExternalCommandOfType<TCommand>() where TCommand : ICommand
protected override IHost CreateHost(IHostBuilder builder)
{
return externalCommandBus.SentCommands.OfType<TCommand>().ToList();
builder.ConfigureServices(services =>
{
services.AddSingleton(eventsLog)
.AddSingleton<IExternalEventProducer>(externalEventProducer)
.AddSingleton<IEventBus>(sp =>
new EventListener(eventsLog,
new EventBusDecoratorWithExternalProducer(sp.GetRequiredService<EventBus>(),
sp.GetRequiredService<IExternalEventProducer>())
)
)
.AddSingleton<IExternalCommandBus>(externalCommandBus)
.AddSingleton<IExternalEventConsumer, DummyExternalEventConsumer>();
});


Environment.SetEnvironmentVariable("SchemaName", schemaName);

return base.CreateHost(builder);
}

public void PublishedExternalEventsOfType<TEvent>() where TEvent : IExternalEvent =>
externalEventProducer.PublishedEvents.OfType<TEvent>().ToList().Should().NotBeEmpty();

public Task PublishInternalEvent<TEvent>(TEvent @event, CancellationToken ct = default) where TEvent : notnull =>
PublishInternalEvent(
new EventEnvelope<TEvent>(@event, new EventMetadata(Guid.NewGuid().ToString(), 0, 0, null)), ct);

public async Task PublishInternalEvent<TEvent>(EventEnvelope<TEvent> eventEnvelope, CancellationToken ct = default)
where TEvent : notnull
{
using var scope = Server.Host.Services.CreateScope();
using var scope = Services.CreateScope();
var eventBus = scope.ServiceProvider.GetRequiredService<IEventBus>();

await eventBus.Publish(eventEnvelope, ct);
Expand All @@ -56,7 +58,6 @@ public async Task PublishInternalEvent<TEvent>(EventEnvelope<TEvent> eventEnvelo
public IReadOnlyCollection<TEvent> PublishedInternalEventsOfType<TEvent>() =>
eventsLog.PublishedEvents.OfType<TEvent>().ToList();

// TODO: Add Poly here
public async Task ShouldPublishInternalEventOfType<TEvent>(
Expression<Func<TEvent, bool>> predicate,
int maxNumberOfRetries = 5,
Expand Down Expand Up @@ -86,7 +87,3 @@ public async Task PublishInternalEvent<TEvent>(EventEnvelope<TEvent> eventEnvelo
} while (!finished);
}
}

public abstract class ApiWithEventsFixture: ApiFixture
{
}
Loading