mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-15 13:51:09 +08:00
add a sample for integration test
This commit is contained in:
parent
b62d53e1ee
commit
6c4ad7094b
@ -42,19 +42,7 @@ public sealed class BusPublisher : IBusPublisher
|
||||
|
||||
if (!integrationEvents.Any()) return;
|
||||
|
||||
foreach (var integrationEvent in integrationEvents)
|
||||
{
|
||||
await _publishEndpoint.Publish((object)integrationEvent, context =>
|
||||
{
|
||||
context.CorrelationId = new Guid(_httpContextAccessor.HttpContext.GetCorrelationId());
|
||||
context.Headers.Set("UserId",
|
||||
_httpContextAccessor?.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier));
|
||||
context.Headers.Set("UserName",
|
||||
_httpContextAccessor?.HttpContext?.User?.FindFirstValue(ClaimTypes.Name));
|
||||
}, cancellationToken);
|
||||
|
||||
_logger.LogTrace("Publish a message with ID: {Id}", integrationEvent?.EventId);
|
||||
}
|
||||
await PublishMessageToBroker(integrationEvents, cancellationToken);
|
||||
|
||||
_logger.LogTrace("Processing integration events done...");
|
||||
}
|
||||
@ -70,11 +58,18 @@ public sealed class BusPublisher : IBusPublisher
|
||||
|
||||
_logger.LogTrace("Processing integration events start...");
|
||||
|
||||
await PublishMessageToBroker(integrationEvents, cancellationToken);
|
||||
|
||||
_logger.LogTrace("Processing integration events done...");
|
||||
}
|
||||
|
||||
private async Task PublishMessageToBroker(IReadOnlyList<IIntegrationEvent> integrationEvents, CancellationToken cancellationToken)
|
||||
{
|
||||
foreach (var integrationEvent in integrationEvents)
|
||||
{
|
||||
await _publishEndpoint.Publish((object)integrationEvent, context =>
|
||||
await _publishEndpoint.Publish((object) integrationEvent, context =>
|
||||
{
|
||||
context.CorrelationId = new Guid(_httpContextAccessor.HttpContext.GetCorrelationId());
|
||||
context.CorrelationId = _httpContextAccessor?.HttpContext?.GetCorrelationId();
|
||||
context.Headers.Set("UserId",
|
||||
_httpContextAccessor?.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier));
|
||||
context.Headers.Set("UserName",
|
||||
@ -83,8 +78,6 @@ public sealed class BusPublisher : IBusPublisher
|
||||
|
||||
_logger.LogTrace("Publish a message with ID: {Id}", integrationEvent?.EventId);
|
||||
}
|
||||
|
||||
_logger.LogTrace("Processing integration events done...");
|
||||
}
|
||||
|
||||
private Task<IReadOnlyList<IIntegrationEvent>> MapDomainEventToIntegrationEventAsync(
|
||||
|
||||
@ -19,8 +19,9 @@ public static class CorrelationExtensions
|
||||
});
|
||||
}
|
||||
|
||||
public static string GetCorrelationId(this HttpContext context)
|
||||
public static Guid GetCorrelationId(this HttpContext context)
|
||||
{
|
||||
return context.Items.TryGetValue(CorrelationId, out var correlationId) ? correlationId as string : null;
|
||||
context.Items.TryGetValue(CorrelationId, out var correlationId);
|
||||
return string.IsNullOrEmpty(correlationId?.ToString()) ? Guid.NewGuid() : new Guid(correlationId.ToString()!);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,27 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using BuildingBlocks.Contracts.EventBus.Messages;
|
||||
using Flight.Airports.Models;
|
||||
using Flight.Flights.Features.GetFlightById;
|
||||
using MassTransit.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Integration.Test;
|
||||
|
||||
[Collection(nameof(TestFixture))]
|
||||
public class DeleteTests
|
||||
{
|
||||
private readonly TestFixture _fixture;
|
||||
|
||||
public DeleteTests(TestFixture fixture)
|
||||
{
|
||||
_fixture = fixture;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_get_flight()
|
||||
{
|
||||
var query = new GetFlightByIdQuery(1);
|
||||
var flight = await _fixture.SendAsync(query);
|
||||
var airport = await _fixture.FindAsync<Airport>(1);
|
||||
}
|
||||
}
|
||||
14
src/Services/Flight/tests/Fakes/FakeCreateFlightCommand.cs
Normal file
14
src/Services/Flight/tests/Fakes/FakeCreateFlightCommand.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using AutoBogus;
|
||||
using Flight.Flights.Features.CreateFlight;
|
||||
|
||||
namespace Integration.Test.Fakes;
|
||||
|
||||
public sealed class FakeCreateFlightCommand : AutoFaker<CreateFlightCommand>
|
||||
{
|
||||
public FakeCreateFlightCommand()
|
||||
{
|
||||
RuleFor(r => r.DepartureAirportId, _ => 1);
|
||||
RuleFor(r => r.ArriveAirportId, _ => 2);
|
||||
RuleFor(r => r.AircraftId, _ => 1);
|
||||
}
|
||||
}
|
||||
36
src/Services/Flight/tests/Flight/CreateFlightTest.cs
Normal file
36
src/Services/Flight/tests/Flight/CreateFlightTest.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System.Threading.Tasks;
|
||||
using Flight.Flights.Features.CreateFlight;
|
||||
using FluentAssertions;
|
||||
using Integration.Test.Fakes;
|
||||
using Xunit;
|
||||
|
||||
namespace Integration.Test.Flight;
|
||||
|
||||
[Collection(nameof(TestFixture))]
|
||||
public class CreateFlightTest
|
||||
{
|
||||
private readonly TestFixture _fixture;
|
||||
|
||||
public CreateFlightTest(TestFixture fixture)
|
||||
{
|
||||
_fixture = fixture;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task should_create_new_flight_to_db()
|
||||
{
|
||||
// Arrange
|
||||
var fakeFlight = new FakeCreateFlightCommand().Generate();
|
||||
var command = new CreateFlightCommand(fakeFlight.FlightNumber, fakeFlight.AircraftId,
|
||||
fakeFlight.DepartureAirportId, fakeFlight.DepartureDate,
|
||||
fakeFlight.ArriveDate, fakeFlight.ArriveAirportId, fakeFlight.DurationMinutes, fakeFlight.FlightDate,
|
||||
fakeFlight.Status, fakeFlight.Price);
|
||||
|
||||
// Act
|
||||
var flightResponse = await _fixture.SendAsync(command);
|
||||
|
||||
// Assert
|
||||
flightResponse.Should().NotBeNull();
|
||||
flightResponse?.FlightNumber.Should().Be(command.FlightNumber);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user