mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-11 02:20:20 +08:00
test: Update test-container to v3.0.0 for support event-store
This commit is contained in:
parent
e25c5f5dff
commit
db0da365b0
@ -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" />
|
||||
|
||||
@ -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 =>
|
||||
{
|
||||
|
||||
@ -5,6 +5,11 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
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)
|
||||
@ -33,4 +38,39 @@ public static class Extensions
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,8 +30,12 @@ using WebMotions.Fake.Authentication.JwtBearer;
|
||||
|
||||
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
|
||||
@ -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
|
||||
{
|
||||
@ -151,7 +156,6 @@ public class TestFixture<TEntryPoint> : IAsyncLifetime
|
||||
return ExecuteScopeAsync(sp =>
|
||||
{
|
||||
var mediator = sp.GetRequiredService<IMediator>();
|
||||
|
||||
return mediator.Send(request);
|
||||
});
|
||||
}
|
||||
@ -238,15 +242,17 @@ public class TestFixture<TEntryPoint> : IAsyncLifetime
|
||||
|
||||
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()
|
||||
@ -255,20 +261,24 @@ public class TestFixture<TEntryPoint> : IAsyncLifetime
|
||||
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())
|
||||
});
|
||||
}
|
||||
|
||||
@ -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;
|
||||
@ -474,8 +485,8 @@ public class TestFixtureCore<TEntryPoint> : IAsyncLifetime
|
||||
|
||||
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))
|
||||
{
|
||||
@ -514,24 +525,26 @@ public class TestFixtureCore<TEntryPoint> : IAsyncLifetime
|
||||
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));
|
||||
|
||||
@ -1,62 +1,149 @@
|
||||
using System;
|
||||
using DotNet.Testcontainers.Builders;
|
||||
using DotNet.Testcontainers.Configurations;
|
||||
using DotNet.Testcontainers.Containers;
|
||||
|
||||
namespace BuildingBlocks.TestBase;
|
||||
namespace BuildingBlocks.TestBase;
|
||||
using Testcontainers.EventStoreDb;
|
||||
using Testcontainers.MongoDb;
|
||||
using Testcontainers.PostgreSql;
|
||||
using Testcontainers.RabbitMq;
|
||||
using Web;
|
||||
|
||||
public static class TestContainers
|
||||
{
|
||||
public static PostgreSqlTestcontainer PostgresTestContainer => new TestcontainersBuilder<PostgreSqlTestcontainer>()
|
||||
.WithDatabase(
|
||||
new PostgreSqlTestcontainerConfiguration
|
||||
{
|
||||
Database = Guid.NewGuid().ToString("D"),
|
||||
Password = Guid.NewGuid().ToString("D"),
|
||||
Username = Guid.NewGuid().ToString("D")
|
||||
})
|
||||
.WithImage("postgres:latest")
|
||||
.WithPortBinding(5432, true)
|
||||
.WithCleanUp(true)
|
||||
.Build();
|
||||
public static RabbitMqContainerOptions RabbitMqContainerConfiguration { get;}
|
||||
public static PostgresContainerOptions PostgresContainerConfiguration { get;}
|
||||
public static PostgresPersistContainerOptions PostgresPersistContainerConfiguration { get;}
|
||||
public static MongoContainerOptions MongoContainerConfiguration { get;}
|
||||
public static EventStoreContainerOptions EventStoreContainerConfiguration { get;}
|
||||
|
||||
public static PostgreSqlTestcontainer PostgresPersistTestContainer => new TestcontainersBuilder<PostgreSqlTestcontainer>()
|
||||
.WithDatabase(
|
||||
new PostgreSqlTestcontainerConfiguration
|
||||
{
|
||||
Database = Guid.NewGuid().ToString("D"),
|
||||
Password = Guid.NewGuid().ToString("D"),
|
||||
Username = Guid.NewGuid().ToString("D")
|
||||
})
|
||||
.WithImage("postgres:latest")
|
||||
.WithPortBinding(5432, true)
|
||||
.WithCleanUp(true)
|
||||
.Build();
|
||||
static TestContainers()
|
||||
{
|
||||
var configuration = ConfigurationHelper.GetConfiguration();
|
||||
|
||||
RabbitMqContainerConfiguration = configuration.GetOptions<RabbitMqContainerOptions>(nameof(RabbitMqContainerOptions));
|
||||
PostgresContainerConfiguration = configuration.GetOptions<PostgresContainerOptions>(nameof(PostgresContainerOptions));
|
||||
PostgresPersistContainerConfiguration = configuration.GetOptions<PostgresPersistContainerOptions>(nameof(PostgresPersistContainerOptions));
|
||||
MongoContainerConfiguration = configuration.GetOptions<MongoContainerOptions>(nameof(MongoContainerOptions));
|
||||
EventStoreContainerConfiguration = configuration.GetOptions<EventStoreContainerOptions>(nameof(EventStoreContainerOptions));
|
||||
}
|
||||
|
||||
public static MongoDbTestcontainer MongoTestContainer => new TestcontainersBuilder<MongoDbTestcontainer>()
|
||||
.WithDatabase(new MongoDbTestcontainerConfiguration()
|
||||
{
|
||||
Database = Guid.NewGuid().ToString("D"),
|
||||
Username = Guid.NewGuid().ToString("D"),
|
||||
Password = Guid.NewGuid().ToString("D"),
|
||||
})
|
||||
.WithImage("mongo:5")
|
||||
.WithCleanUp(true)
|
||||
.Build();
|
||||
public static PostgreSqlContainer PostgresTestContainer()
|
||||
{
|
||||
var baseBuilder = new PostgreSqlBuilder()
|
||||
.WithUsername(PostgresContainerConfiguration.UserName)
|
||||
.WithPassword(PostgresContainerConfiguration.Password)
|
||||
.WithLabel("Key", "Value");
|
||||
|
||||
var builder = baseBuilder
|
||||
.WithImage(PostgresContainerConfiguration.ImageName)
|
||||
.WithName(PostgresContainerConfiguration.Name)
|
||||
.WithPortBinding(PostgresContainerConfiguration.Port, true)
|
||||
.Build();
|
||||
|
||||
public static RabbitMqTestcontainer RabbitMqTestContainer => new TestcontainersBuilder<RabbitMqTestcontainer>()
|
||||
.WithMessageBroker(new RabbitMqTestcontainerConfiguration()
|
||||
{
|
||||
Password = "guest",
|
||||
Username = "guest"
|
||||
})
|
||||
.WithImage("rabbitmq:3-management")
|
||||
.WithPortBinding(15672, true)
|
||||
.WithPortBinding(5672, true)
|
||||
.WithCleanUp(true)
|
||||
.Build();
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static PostgreSqlContainer PostgresPersistTestContainer()
|
||||
{
|
||||
var baseBuilder = new PostgreSqlBuilder()
|
||||
.WithUsername(PostgresPersistContainerConfiguration.UserName)
|
||||
.WithPassword(PostgresPersistContainerConfiguration.Password)
|
||||
.WithLabel("Key", "Value");
|
||||
|
||||
var builder = baseBuilder
|
||||
.WithImage(PostgresPersistContainerConfiguration.ImageName)
|
||||
.WithName(PostgresPersistContainerConfiguration.Name)
|
||||
.WithPortBinding(PostgresPersistContainerConfiguration.Port, true)
|
||||
.Build();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static MongoDbContainer MongoTestContainer()
|
||||
{
|
||||
var baseBuilder = new MongoDbBuilder()
|
||||
.WithUsername(MongoContainerConfiguration.UserName)
|
||||
.WithPassword(MongoContainerConfiguration.Password)
|
||||
.WithLabel("Key", "Value");
|
||||
|
||||
var builder = baseBuilder
|
||||
.WithImage(MongoContainerConfiguration.ImageName)
|
||||
.WithName(MongoContainerConfiguration.Name)
|
||||
.WithPortBinding(MongoContainerConfiguration.Port, true)
|
||||
.Build();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static RabbitMqContainer RabbitMqTestContainer()
|
||||
{
|
||||
var baseBuilder = new RabbitMqBuilder()
|
||||
.WithUsername(RabbitMqContainerConfiguration.UserName)
|
||||
.WithPassword(RabbitMqContainerConfiguration.Password)
|
||||
.WithLabel("Key", "Value");
|
||||
|
||||
var builder = baseBuilder
|
||||
.WithImage(RabbitMqContainerConfiguration.ImageName)
|
||||
.WithName(RabbitMqContainerConfiguration.Name)
|
||||
.WithPortBinding(RabbitMqContainerConfiguration.ApiPort, true)
|
||||
.WithPortBinding(RabbitMqContainerConfiguration.Port, true)
|
||||
.Build();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static EventStoreDbContainer EventStoreTestContainer()
|
||||
{
|
||||
var baseBuilder = new EventStoreDbBuilder()
|
||||
.WithLabel("Key", "Value");
|
||||
|
||||
var builder = baseBuilder
|
||||
.WithImage(EventStoreContainerConfiguration.ImageName)
|
||||
.WithName(EventStoreContainerConfiguration.Name)
|
||||
.WithPortBinding(EventStoreContainerConfiguration.Port, true)
|
||||
.Build();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public sealed class RabbitMqContainerOptions
|
||||
{
|
||||
public string Name { get; set; } = "rabbitmq_" + Guid.NewGuid();
|
||||
public int Port { get; set; } = 5672;
|
||||
public int ApiPort { get; set; } = 15672;
|
||||
public string ImageName { get; set; } = "rabbitmq:3-management";
|
||||
public string UserName { get; set; } = "guest";
|
||||
public string Password { get; set; } = "guest";
|
||||
}
|
||||
|
||||
public sealed class PostgresContainerOptions
|
||||
{
|
||||
public string Name { get; set; } = "postgreSql_" + Guid.NewGuid().ToString("D");
|
||||
public int Port { get; set; } = 5432;
|
||||
public string ImageName { get; set; } = "postgres:latest";
|
||||
public string UserName { get; set; } = Guid.NewGuid().ToString("D");
|
||||
public string Password { get; set; } = Guid.NewGuid().ToString("D");
|
||||
}
|
||||
|
||||
public sealed class PostgresPersistContainerOptions
|
||||
{
|
||||
public string Name { get; set; } = "postgreSql_" + Guid.NewGuid().ToString("D");
|
||||
public int Port { get; set; } = 5432;
|
||||
public string ImageName { get; set; } = "postgres:latest";
|
||||
public string UserName { get; set; } = Guid.NewGuid().ToString("D");
|
||||
public string Password { get; set; } = Guid.NewGuid().ToString("D");
|
||||
}
|
||||
|
||||
public sealed class MongoContainerOptions
|
||||
{
|
||||
public string Name { get; set; } = "mongo_" + Guid.NewGuid().ToString("D");
|
||||
public int Port { get; set; } = 27017;
|
||||
public string ImageName { get; set; } = "mongo:5";
|
||||
public string UserName { get; set; } = Guid.NewGuid().ToString("D");
|
||||
public string Password { get; set; } = Guid.NewGuid().ToString("D");
|
||||
}
|
||||
|
||||
public sealed class EventStoreContainerOptions
|
||||
{
|
||||
public string Name { get; set; } = "event_store_" + Guid.NewGuid().ToString("D");
|
||||
public int Port { get; set; } = 2113;
|
||||
public string ImageName { get; set; } = "eventstore/eventstore:21.2.0-buster-slim";
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
using System.Threading.RateLimiting;
|
||||
using Booking.Data;
|
||||
using BuildingBlocks.Core;
|
||||
using BuildingBlocks.EFCore;
|
||||
using BuildingBlocks.EventStoreDB;
|
||||
using BuildingBlocks.HealthCheck;
|
||||
using BuildingBlocks.IdsGenerator;
|
||||
@ -27,6 +26,8 @@ using Serilog;
|
||||
|
||||
namespace Booking.Extensions.Infrastructure;
|
||||
|
||||
using BuildingBlocks.PersistMessageProcessor.Data;
|
||||
|
||||
public static class InfrastructureExtensions
|
||||
{
|
||||
public static WebApplicationBuilder AddInfrastructure(this WebApplicationBuilder builder)
|
||||
@ -99,6 +100,7 @@ public static class InfrastructureExtensions
|
||||
});
|
||||
app.UseCorrelationId();
|
||||
app.UseHttpMetrics();
|
||||
app.UseMigration<PersistMessageDbContext>(env);
|
||||
app.UseCustomHealthCheck();
|
||||
app.MapMetrics();
|
||||
app.MapGet("/", x => x.Response.WriteAsync(appOptions.Name));
|
||||
|
||||
@ -1,11 +1,8 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Booking.Api;
|
||||
using Booking.Data;
|
||||
using BuildingBlocks.Contracts.EventBus.Messages;
|
||||
using BuildingBlocks.EFCore;
|
||||
using BuildingBlocks.PersistMessageProcessor.Data;
|
||||
using BuildingBlocks.TestBase;
|
||||
using Flight;
|
||||
using FluentAssertions;
|
||||
@ -21,72 +18,69 @@ using GetByIdRequest = Flight.GetByIdRequest;
|
||||
|
||||
namespace Integration.Test.Booking.Features
|
||||
{
|
||||
// todo: uncomment after event-store test-container is published.
|
||||
// public class CreateBookingTests : BookingIntegrationTestBase
|
||||
// {
|
||||
// public CreateBookingTests(TestReadFixture<Program, BookingReadDbContext> integrationTestFixture) : base(
|
||||
// integrationTestFixture)
|
||||
// {
|
||||
// }
|
||||
//
|
||||
// protected override void RegisterTestsServices(IServiceCollection services)
|
||||
// {
|
||||
// MockFlightGrpcServices(services);
|
||||
// MockPassengerGrpcServices(services);
|
||||
// }
|
||||
//
|
||||
// // todo: add support test for event-store
|
||||
// [Fact]
|
||||
// public async Task should_create_booking_to_event_store_currectly()
|
||||
// {
|
||||
// // Arrange
|
||||
// var command = new FakeCreateBookingCommand().Generate();
|
||||
//
|
||||
// // Act
|
||||
//
|
||||
// var response = await Fixture.SendAsync(command);
|
||||
//
|
||||
// // Assert
|
||||
// response.Should().BeGreaterOrEqualTo(0);
|
||||
//
|
||||
// (await Fixture.WaitForPublishing<BookingCreated>()).Should().Be(true);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// private void MockPassengerGrpcServices(IServiceCollection services)
|
||||
// {
|
||||
// services.Replace(ServiceDescriptor.Singleton(x =>
|
||||
// {
|
||||
// var mockPassenger = Substitute.For<PassengerGrpcService.PassengerGrpcServiceClient>();
|
||||
//
|
||||
// mockPassenger.GetByIdAsync(Arg.Any<Passenger.GetByIdRequest>())
|
||||
// .Returns(TestCalls.AsyncUnaryCall(Task.FromResult(new FakePassengerResponse().Generate()),
|
||||
// Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }));
|
||||
//
|
||||
// return mockPassenger;
|
||||
// }));
|
||||
// }
|
||||
//
|
||||
// private void MockFlightGrpcServices(IServiceCollection services)
|
||||
// {
|
||||
// services.Replace(ServiceDescriptor.Singleton(x =>
|
||||
// {
|
||||
// var mockFlight = Substitute.For<FlightGrpcService.FlightGrpcServiceClient>();
|
||||
//
|
||||
// mockFlight.GetByIdAsync(Arg.Any<GetByIdRequest>())
|
||||
// .Returns(TestCalls.AsyncUnaryCall(Task.FromResult(new FakeFlightResponse().Generate()),
|
||||
// Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }));
|
||||
//
|
||||
// mockFlight.GetAvailableSeatsAsync(Arg.Any<GetAvailableSeatsRequest>())
|
||||
// .Returns(TestCalls.AsyncUnaryCall(Task.FromResult(FakeSeatsResponse.Generate()),
|
||||
// Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }));
|
||||
//
|
||||
// mockFlight.ReserveSeatAsync(Arg.Any<ReserveSeatRequest>())
|
||||
// .Returns(TestCalls.AsyncUnaryCall(Task.FromResult(FakeSeatsResponse.Generate()?.Items?.First()),
|
||||
// Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }));
|
||||
//
|
||||
// return mockFlight;
|
||||
// }));
|
||||
// }
|
||||
// }
|
||||
public class CreateBookingTests : BookingIntegrationTestBase
|
||||
{
|
||||
public CreateBookingTests(TestReadFixture<Program, BookingReadDbContext> integrationTestFixture) : base(
|
||||
integrationTestFixture)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void RegisterTestsServices(IServiceCollection services)
|
||||
{
|
||||
MockFlightGrpcServices(services);
|
||||
MockPassengerGrpcServices(services);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task should_create_booking_to_event_store_currectly()
|
||||
{
|
||||
// Arrange
|
||||
var command = new FakeCreateBookingCommand().Generate();
|
||||
|
||||
// Act
|
||||
var response = await Fixture.SendAsync(command);
|
||||
|
||||
// Assert
|
||||
response.Should().BeGreaterOrEqualTo(0);
|
||||
|
||||
(await Fixture.WaitForPublishing<BookingCreated>()).Should().Be(true);
|
||||
}
|
||||
|
||||
|
||||
private void MockPassengerGrpcServices(IServiceCollection services)
|
||||
{
|
||||
services.Replace(ServiceDescriptor.Singleton(x =>
|
||||
{
|
||||
var mockPassenger = Substitute.For<PassengerGrpcService.PassengerGrpcServiceClient>();
|
||||
|
||||
mockPassenger.GetByIdAsync(Arg.Any<Passenger.GetByIdRequest>())
|
||||
.Returns(TestCalls.AsyncUnaryCall(Task.FromResult(new FakePassengerResponse().Generate()),
|
||||
Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }));
|
||||
|
||||
return mockPassenger;
|
||||
}));
|
||||
}
|
||||
|
||||
private void MockFlightGrpcServices(IServiceCollection services)
|
||||
{
|
||||
services.Replace(ServiceDescriptor.Singleton(x =>
|
||||
{
|
||||
var mockFlight = Substitute.For<FlightGrpcService.FlightGrpcServiceClient>();
|
||||
|
||||
mockFlight.GetByIdAsync(Arg.Any<GetByIdRequest>())
|
||||
.Returns(TestCalls.AsyncUnaryCall(Task.FromResult(new FakeFlightResponse().Generate()),
|
||||
Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }));
|
||||
|
||||
mockFlight.GetAvailableSeatsAsync(Arg.Any<GetAvailableSeatsRequest>())
|
||||
.Returns(TestCalls.AsyncUnaryCall(Task.FromResult(FakeSeatsResponse.Generate()),
|
||||
Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }));
|
||||
|
||||
mockFlight.ReserveSeatAsync(Arg.Any<ReserveSeatRequest>())
|
||||
.Returns(TestCalls.AsyncUnaryCall(Task.FromResult(FakeSeatsResponse.Generate()?.Items?.First()),
|
||||
Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }));
|
||||
|
||||
return mockFlight;
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user