diff --git a/src/BuildingBlocks/Domain/BusPublisher.cs b/src/BuildingBlocks/Domain/BusPublisher.cs index d764e71..45f4835 100644 --- a/src/BuildingBlocks/Domain/BusPublisher.cs +++ b/src/BuildingBlocks/Domain/BusPublisher.cs @@ -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 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> MapDomainEventToIntegrationEventAsync( diff --git a/src/BuildingBlocks/Web/CorrelationExtensions.cs b/src/BuildingBlocks/Web/CorrelationExtensions.cs index 64c5347..7c43a07 100644 --- a/src/BuildingBlocks/Web/CorrelationExtensions.cs +++ b/src/BuildingBlocks/Web/CorrelationExtensions.cs @@ -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()!); } } diff --git a/src/Services/Flight/tests/DeleteTests.cs b/src/Services/Flight/tests/DeleteTests.cs deleted file mode 100644 index 6ee4486..0000000 --- a/src/Services/Flight/tests/DeleteTests.cs +++ /dev/null @@ -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(1); - } -} diff --git a/src/Services/Flight/tests/Fakes/FakeCreateFlightCommand.cs b/src/Services/Flight/tests/Fakes/FakeCreateFlightCommand.cs new file mode 100644 index 0000000..de0f7fb --- /dev/null +++ b/src/Services/Flight/tests/Fakes/FakeCreateFlightCommand.cs @@ -0,0 +1,14 @@ +using AutoBogus; +using Flight.Flights.Features.CreateFlight; + +namespace Integration.Test.Fakes; + +public sealed class FakeCreateFlightCommand : AutoFaker +{ + public FakeCreateFlightCommand() + { + RuleFor(r => r.DepartureAirportId, _ => 1); + RuleFor(r => r.ArriveAirportId, _ => 2); + RuleFor(r => r.AircraftId, _ => 1); + } +} diff --git a/src/Services/Flight/tests/Flight/CreateFlightTest.cs b/src/Services/Flight/tests/Flight/CreateFlightTest.cs new file mode 100644 index 0000000..c9ddb79 --- /dev/null +++ b/src/Services/Flight/tests/Flight/CreateFlightTest.cs @@ -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); + } +}