Skip to content

Commit

Permalink
Merge pull request #230 from meysamhadeli/test/add-event-store-test-c…
Browse files Browse the repository at this point in the history
…ontainer

Test/add event store test container
  • Loading branch information
meysamhadeli authored Mar 11, 2023
2 parents 7669ad4 + db0da36 commit eab6386
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 156 deletions.
6 changes: 5 additions & 1 deletion src/BuildingBlocks/BuildingBlocks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@
<PackageReference Include="Duende.IdentityServer.EntityFramework" Version="6.2.2" />
<PackageReference Include="Duende.IdentityServer.EntityFramework.Storage" Version="6.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Testcontainers" Version="2.3.0" />
<PackageReference Include="Testcontainers" Version="3.0.0" />
<PackageReference Include="Testcontainers.EventStoreDb" Version="3.0.0" />
<PackageReference Include="Testcontainers.MongoDb" Version="3.0.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="3.0.0" />
<PackageReference Include="Testcontainers.RabbitMq" Version="3.0.0" />
<PackageReference Include="Unchase.Swashbuckle.AspNetCore.Extensions" Version="2.7.1" />
<PackageReference Include="WebMotions.Fake.Authentication.JwtBearer" Version="7.0.0" />
<PackageReference Include="Yarp.ReverseProxy" Version="1.1.1" />
Expand Down
2 changes: 1 addition & 1 deletion src/BuildingBlocks/CAP/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class Extensions
public static IServiceCollection AddCustomCap<TDbContext>(this IServiceCollection services)
where TDbContext : DbContext
{
var rabbitMqOptions = services.GetOptions<RabbitMQOptions>("RabbitMq");
var rabbitMqOptions = services.GetOptions<RabbitMQOptions>(nameof(RabbitMQOptions));

services.AddCap(x =>
{
Expand Down
40 changes: 40 additions & 0 deletions src/BuildingBlocks/PersistMessageProcessor/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

namespace BuildingBlocks.PersistMessageProcessor;

using EFCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

public static class Extensions
{
public static IServiceCollection AddPersistMessageProcessor(this IServiceCollection services)
Expand Down Expand Up @@ -33,4 +38,39 @@ public static IServiceCollection AddPersistMessageProcessor(this IServiceCollect

return services;
}

public static IApplicationBuilder UseMigration<TContext>(this IApplicationBuilder app, IWebHostEnvironment env)
where TContext : DbContext, IPersistMessageDbContext
{
MigrateDatabaseAsync<TContext>(app.ApplicationServices).GetAwaiter().GetResult();

if (!env.IsEnvironment("test"))
{
SeedDataAsync(app.ApplicationServices).GetAwaiter().GetResult();
}

return app;
}

private static async Task MigrateDatabaseAsync<TContext>(IServiceProvider serviceProvider)
where TContext : DbContext, IPersistMessageDbContext
{
using var scope = serviceProvider.CreateScope();

var persistMessageContext = scope.ServiceProvider.GetRequiredService<PersistMessageDbContext>();
await persistMessageContext.Database.MigrateAsync();

var context = scope.ServiceProvider.GetRequiredService<TContext>();
await context.Database.MigrateAsync();
}

private static async Task SeedDataAsync(IServiceProvider serviceProvider)
{
using var scope = serviceProvider.CreateScope();
var seeders = scope.ServiceProvider.GetServices<IDataSeeder>();
foreach (var seeder in seeders)
{
await seeder.SeedAllAsync();
}
}
}
63 changes: 38 additions & 25 deletions src/BuildingBlocks/TestBase/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@

namespace BuildingBlocks.TestBase;

using System.Globalization;
using Npgsql;
using Exception = System.Exception;
using Testcontainers.EventStoreDb;
using Testcontainers.MongoDb;
using Testcontainers.PostgreSql;
using Testcontainers.RabbitMq;

public class TestFixture<TEntryPoint> : IAsyncLifetime
where TEntryPoint : class
Expand All @@ -40,10 +44,11 @@ public class TestFixture<TEntryPoint> : IAsyncLifetime
private int Timeout => 120; // Second
private ITestHarness TestHarness => ServiceProvider?.GetTestHarness();
private Action<IServiceCollection> TestRegistrationServices { get; set; }
private PostgreSqlTestcontainer PostgresTestcontainer;
private PostgreSqlTestcontainer PostgresPersistTestContainer;
public RabbitMqTestcontainer RabbitMqTestContainer;
public MongoDbTestcontainer MongoDbTestContainer;
private PostgreSqlContainer PostgresTestcontainer;
private PostgreSqlContainer PostgresPersistTestContainer;
public RabbitMqContainer RabbitMqTestContainer;
public MongoDbContainer MongoDbTestContainer;
public EventStoreDbContainer EventStoreDbTestContainer;

public HttpClient HttpClient
{
Expand Down Expand Up @@ -151,7 +156,6 @@ public Task SendAsync(IRequest request)
return ExecuteScopeAsync(sp =>
{
var mediator = sp.GetRequiredService<IMediator>();

return mediator.Send(request);
});
}
Expand Down Expand Up @@ -238,15 +242,17 @@ public async Task<bool> ShouldProcessedPersistInternalCommand<TInternalCommand>(

private async Task StartTestContainerAsync()
{
PostgresTestcontainer = TestContainers.PostgresTestContainer;
PostgresPersistTestContainer = TestContainers.PostgresPersistTestContainer;
RabbitMqTestContainer = TestContainers.RabbitMqTestContainer;
MongoDbTestContainer = TestContainers.MongoTestContainer;
PostgresTestcontainer = TestContainers.PostgresTestContainer();
PostgresPersistTestContainer = TestContainers.PostgresPersistTestContainer();
RabbitMqTestContainer = TestContainers.RabbitMqTestContainer();
MongoDbTestContainer = TestContainers.MongoTestContainer();
EventStoreDbTestContainer = TestContainers.EventStoreTestContainer();

await MongoDbTestContainer.StartAsync();
await PostgresTestcontainer.StartAsync();
await PostgresPersistTestContainer.StartAsync();
await RabbitMqTestContainer.StartAsync();
await EventStoreDbTestContainer.StartAsync();
}

private async Task StopTestContainerAsync()
Expand All @@ -255,20 +261,24 @@ private async Task StopTestContainerAsync()
await PostgresPersistTestContainer.StopAsync();
await RabbitMqTestContainer.StopAsync();
await MongoDbTestContainer.StopAsync();
await EventStoreDbTestContainer.StopAsync();
}

private void AddCustomAppSettings(IConfigurationBuilder configuration)
{
configuration.AddInMemoryCollection(new KeyValuePair<string, string>[]
{
new("PostgresOptions:ConnectionString", PostgresTestcontainer.ConnectionString),
new("PersistMessageOptions:ConnectionString", PostgresPersistTestContainer.ConnectionString),
new("PostgresOptions:ConnectionString", PostgresTestcontainer.GetConnectionString()),
new("PersistMessageOptions:ConnectionString", PostgresPersistTestContainer.GetConnectionString()),
new("RabbitMqOptions:HostName", RabbitMqTestContainer.Hostname),
new("RabbitMqOptions:UserName", RabbitMqTestContainer.Username),
new("RabbitMqOptions:Password", RabbitMqTestContainer.Password),
new("RabbitMqOptions:Port", RabbitMqTestContainer.Port.ToString()),
new("MongoOptions:ConnectionString", MongoDbTestContainer.ConnectionString),
new("MongoOptions:DatabaseName", MongoDbTestContainer.Database)
new("RabbitMqOptions:UserName", TestContainers.RabbitMqContainerConfiguration.UserName),
new("RabbitMqOptions:Password", TestContainers.RabbitMqContainerConfiguration.Password), new(
"RabbitMqOptions:Port",
RabbitMqTestContainer.GetMappedPublicPort(TestContainers.RabbitMqContainerConfiguration.Port)
.ToString(NumberFormatInfo.InvariantInfo)),
new("MongoOptions:ConnectionString", MongoDbTestContainer.GetConnectionString()),
new("MongoOptions:DatabaseName", TestContainers.MongoContainerConfiguration.Name),
new("EventStore:ConnectionString", EventStoreDbTestContainer.GetConnectionString())
});
}

Expand Down Expand Up @@ -450,6 +460,7 @@ public class TestFixtureCore<TEntryPoint> : IAsyncLifetime
private NpgsqlConnection DefaultDbConnection { get; set; }
private NpgsqlConnection PersistDbConnection { get; set; }


public TestFixtureCore(TestFixture<TEntryPoint> integrationTestFixture, ITestOutputHelper outputHelper)
{
Fixture = integrationTestFixture;
Expand All @@ -474,8 +485,8 @@ public async Task DisposeAsync()

private async Task InitPostgresAsync()
{
var postgresOptions = Fixture.ServiceProvider.GetRequiredService<PostgresOptions>();
var persistOptions = Fixture.ServiceProvider.GetRequiredService<PersistMessageOptions>();
var postgresOptions = Fixture.ServiceProvider.GetService<PostgresOptions>();
var persistOptions = Fixture.ServiceProvider.GetService<PersistMessageOptions>();

if (!string.IsNullOrEmpty(persistOptions?.ConnectionString))
{
Expand Down Expand Up @@ -514,24 +525,26 @@ private async Task ResetPostgresAsync()
private async Task ResetMongoAsync(CancellationToken cancellationToken = default)
{
//https://stackoverflow.com/questions/3366397/delete-everything-in-a-mongodb-database
var dbClient = new MongoClient(Fixture.MongoDbTestContainer?.ConnectionString);
var collections = await dbClient.GetDatabase(Fixture.MongoDbTestContainer?.Database)
var dbClient = new MongoClient(Fixture.MongoDbTestContainer?.GetConnectionString());
var collections = await dbClient.GetDatabase(TestContainers.MongoContainerConfiguration.Name)
.ListCollectionsAsync(cancellationToken: cancellationToken);

foreach (var collection in collections.ToList())
{
await dbClient.GetDatabase(Fixture.MongoDbTestContainer?.Database)
await dbClient.GetDatabase(TestContainers.MongoContainerConfiguration.Name)
.DropCollectionAsync(collection["name"].AsString, cancellationToken);
}
}

private async Task ResetRabbitMqAsync(CancellationToken cancellationToken = default)
{
var port = Fixture.RabbitMqTestContainer?.GetMappedPublicPort(15672) ?? 15672;
var port = Fixture.RabbitMqTestContainer?.GetMappedPublicPort(TestContainers.RabbitMqContainerConfiguration
.ApiPort)
?? TestContainers.RabbitMqContainerConfiguration.ApiPort;

var managementClient = new ManagementClient(Fixture.RabbitMqTestContainer?.Hostname,
Fixture.RabbitMqTestContainer?.Username,
Fixture.RabbitMqTestContainer?.Password, port);
TestContainers.RabbitMqContainerConfiguration?.UserName,
TestContainers.RabbitMqContainerConfiguration?.Password, port);

var bd = await managementClient.GetBindingsAsync(cancellationToken);
var bindings = bd.Where(x => !string.IsNullOrEmpty(x.Source) && !string.IsNullOrEmpty(x.Destination));
Expand Down
Loading

0 comments on commit eab6386

Please sign in to comment.