diff --git a/src/Services/Flight/src/Flight/Flights/Features/CreateFlight/CreateFlightCommandHandler.cs b/src/Services/Flight/src/Flight/Flights/Features/CreateFlight/CreateFlightCommandHandler.cs index a441d1f..a2dca53 100644 --- a/src/Services/Flight/src/Flight/Flights/Features/CreateFlight/CreateFlightCommandHandler.cs +++ b/src/Services/Flight/src/Flight/Flights/Features/CreateFlight/CreateFlightCommandHandler.cs @@ -26,7 +26,7 @@ public class CreateFlightCommandHandler : IRequestHandler x.FlightNumber == command.FlightNumber && !x.IsDeleted, + var flight = await _flightDbContext.Flights.SingleOrDefaultAsync(x => x.Id == command.Id && !x.IsDeleted, cancellationToken); if (flight is not null) diff --git a/src/Services/Flight/src/Flight/Flights/Features/UpdateFlight/UpdateFlightCommandHandler.cs b/src/Services/Flight/src/Flight/Flights/Features/UpdateFlight/UpdateFlightCommandHandler.cs index 2276cb6..3758afd 100644 --- a/src/Services/Flight/src/Flight/Flights/Features/UpdateFlight/UpdateFlightCommandHandler.cs +++ b/src/Services/Flight/src/Flight/Flights/Features/UpdateFlight/UpdateFlightCommandHandler.cs @@ -27,7 +27,7 @@ public class UpdateFlightCommandHandler : IRequestHandler x.FlightNumber == command.FlightNumber && !x.IsDeleted, + var flight = await _flightDbContext.Flights.SingleOrDefaultAsync(x => x.Id == command.Id && !x.IsDeleted, cancellationToken); if (flight is null) diff --git a/src/Services/Flight/src/Flight/Flights/Models/Flight.cs b/src/Services/Flight/src/Flight/Flights/Models/Flight.cs index 7765cec..985b41a 100644 --- a/src/Services/Flight/src/Flight/Flights/Models/Flight.cs +++ b/src/Services/Flight/src/Flight/Flights/Models/Flight.cs @@ -54,6 +54,18 @@ public class Flight : Aggregate long arriveAirportId, decimal durationMinutes, DateTime flightDate, FlightStatus status, decimal price, bool isDeleted = false) { + FlightNumber = flightNumber; + AircraftId = aircraftId; + DepartureAirportId = departureAirportId; + DepartureDate = departureDate; + arriveDate = ArriveDate; + ArriveAirportId = arriveAirportId; + DurationMinutes = durationMinutes; + FlightDate = flightDate; + Status = status; + Price = price; + IsDeleted = isDeleted; + var @event = new FlightUpdatedDomainEvent(id, flightNumber, aircraftId, departureDate, departureAirportId, arriveDate, arriveAirportId, durationMinutes, flightDate, status, price, isDeleted); diff --git a/src/Services/Flight/tests/Fakes/FakeCreateFlightCommand.cs b/src/Services/Flight/tests/Fakes/FakeCreateFlightCommand.cs index de0f7fb..6951855 100644 --- a/src/Services/Flight/tests/Fakes/FakeCreateFlightCommand.cs +++ b/src/Services/Flight/tests/Fakes/FakeCreateFlightCommand.cs @@ -7,6 +7,8 @@ public sealed class FakeCreateFlightCommand : AutoFaker { public FakeCreateFlightCommand() { + RuleFor(r => r.Id, r => r.Random.Number(50, 100000)); + RuleFor(r => r.FlightNumber, r => r.Random.String()); RuleFor(r => r.DepartureAirportId, _ => 1); RuleFor(r => r.ArriveAirportId, _ => 2); RuleFor(r => r.AircraftId, _ => 1); diff --git a/src/Services/Flight/tests/Fakes/FakeUpdateFlightCommand.cs b/src/Services/Flight/tests/Fakes/FakeUpdateFlightCommand.cs new file mode 100644 index 0000000..e0f8395 --- /dev/null +++ b/src/Services/Flight/tests/Fakes/FakeUpdateFlightCommand.cs @@ -0,0 +1,17 @@ +using AutoBogus; +using Flight.Flights.Features.UpdateFlight; + +namespace Integration.Test.Fakes; + +public class FakeUpdateFlightCommand : AutoFaker +{ + public FakeUpdateFlightCommand(long id) + { + RuleFor(r => r.Id, _ => id); + RuleFor(r => r.DepartureAirportId, _ => 2); + RuleFor(r => r.ArriveAirportId, _ => 1); + RuleFor(r => r.AircraftId, _ => 2); + RuleFor(r => r.FlightNumber, _ => "12BB"); + RuleFor(r => r.Price, _ => 800); + } +} diff --git a/src/Services/Flight/tests/Flight/CreateFlightTest.cs b/src/Services/Flight/tests/Flight/CreateFlightTests.cs similarity index 93% rename from src/Services/Flight/tests/Flight/CreateFlightTest.cs rename to src/Services/Flight/tests/Flight/CreateFlightTests.cs index a96fbe6..25e4c9c 100644 --- a/src/Services/Flight/tests/Flight/CreateFlightTest.cs +++ b/src/Services/Flight/tests/Flight/CreateFlightTests.cs @@ -8,11 +8,11 @@ using Xunit; namespace Integration.Test.Flight; [Collection(nameof(TestFixture))] -public class CreateFlightTest +public class CreateFlightTests { private readonly TestFixture _fixture; - public CreateFlightTest(TestFixture fixture) + public CreateFlightTests(TestFixture fixture) { _fixture = fixture; } diff --git a/src/Services/Flight/tests/Flight/GetAvailableFlightsTests.cs b/src/Services/Flight/tests/Flight/GetAvailableFlightsTests.cs new file mode 100644 index 0000000..83e2be8 --- /dev/null +++ b/src/Services/Flight/tests/Flight/GetAvailableFlightsTests.cs @@ -0,0 +1,51 @@ +using System.Linq; +using System.Threading.Tasks; +using AutoBogus; +using Bogus; +using BuildingBlocks.IdsGenerator; +using Flight.Flights.Features.CreateFlight; +using Flight.Flights.Features.GetAvailableFlights; +using Flight.Flights.Features.GetFlightById; +using FluentAssertions; +using Integration.Test.Fakes; +using Xunit; + +namespace Integration.Test.Flight; + +[Collection(nameof(TestFixture))] +public class GetAvailableFlightsTests +{ + private readonly TestFixture _fixture; + + public GetAvailableFlightsTests(TestFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public async Task should_return_available_flights() + { + // Arrange + var flightCommand1 = new FakeCreateFlightCommand().Generate(); + var flightCommand2 = new FakeCreateFlightCommand().Generate(); + + var flightEntity1 = global::Flight.Flights.Models.Flight.Create( + flightCommand1.Id, flightCommand1.FlightNumber, flightCommand1.AircraftId, flightCommand1.DepartureAirportId, flightCommand1.DepartureDate, + flightCommand1.ArriveDate, flightCommand1.ArriveAirportId, flightCommand1.DurationMinutes, flightCommand1.FlightDate, flightCommand1.Status, flightCommand1.Price); + + var flightEntity2 = global::Flight.Flights.Models.Flight.Create( + flightCommand2.Id, flightCommand2.FlightNumber, flightCommand2.AircraftId, flightCommand2.DepartureAirportId, flightCommand2.DepartureDate, + flightCommand2.ArriveDate, flightCommand2.ArriveAirportId, flightCommand2.DurationMinutes, flightCommand2.FlightDate, flightCommand2.Status, flightCommand2.Price); + + await _fixture.InsertAsync(flightEntity1, flightEntity2); + + var query = new GetAvailableFlightsQuery(); + + // Act + var flightResponse = await _fixture.SendAsync(query); + + // Assert + flightResponse?.Should().NotBeNull(); + flightResponse?.Count().Should().BeGreaterOrEqualTo(2); + } +} diff --git a/src/Services/Flight/tests/Flight/GetFlightByIdTests.cs b/src/Services/Flight/tests/Flight/GetFlightByIdTests.cs new file mode 100644 index 0000000..39e394e --- /dev/null +++ b/src/Services/Flight/tests/Flight/GetFlightByIdTests.cs @@ -0,0 +1,40 @@ +using System.Threading.Tasks; +using BuildingBlocks.Contracts.EventBus.Messages; +using Flight.Flights.Features.CreateFlight; +using Flight.Flights.Features.GetFlightById; +using FluentAssertions; +using Integration.Test.Fakes; +using Xunit; + +namespace Integration.Test.Flight; + +[Collection(nameof(TestFixture))] +public class GetFlightByIdTests +{ + private readonly TestFixture _fixture; + + public GetFlightByIdTests(TestFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public async Task should_retrive_a_flight_by_id_currectly() + { + // Arrange + var command = new FakeCreateFlightCommand().Generate(); + var flightEntity = global::Flight.Flights.Models.Flight.Create( + command.Id, command.FlightNumber, command.AircraftId, command.DepartureAirportId, command.DepartureDate, + command.ArriveDate, command.ArriveAirportId, command.DurationMinutes, command.FlightDate, command.Status, command.Price); + await _fixture.InsertAsync(flightEntity); + + var query = new GetFlightByIdQuery(flightEntity.Id); + + // Act + var flightResponse = await _fixture.SendAsync(query); + + // Assert + flightResponse.Should().NotBeNull(); + flightResponse?.Id.Should().Be(flightEntity.Id); + } +} diff --git a/src/Services/Flight/tests/Flight/UpdateFlightTests.cs b/src/Services/Flight/tests/Flight/UpdateFlightTests.cs new file mode 100644 index 0000000..1458c7b --- /dev/null +++ b/src/Services/Flight/tests/Flight/UpdateFlightTests.cs @@ -0,0 +1,45 @@ +using System.Linq; +using System.Threading.Tasks; +using BuildingBlocks.Contracts.EventBus.Messages; +using Flight.Flights.Features.CreateFlight; +using FluentAssertions; +using Integration.Test.Fakes; +using Microsoft.EntityFrameworkCore; +using Xunit; + +namespace Integration.Test.Flight; + +[Collection(nameof(TestFixture))] +public class UpdateFlightTests +{ + private readonly TestFixture _fixture; + + public UpdateFlightTests(TestFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public async Task should_update_flight_to_db_and_publish_message_to_broker() + { + // Arrange + var fakeCreateCommandFlight = new FakeCreateFlightCommand().Generate(); + var flightEntity = global::Flight.Flights.Models.Flight.Create(fakeCreateCommandFlight.Id, fakeCreateCommandFlight.FlightNumber, + fakeCreateCommandFlight.AircraftId, fakeCreateCommandFlight.DepartureAirportId, fakeCreateCommandFlight.DepartureDate, + fakeCreateCommandFlight.ArriveDate, fakeCreateCommandFlight.ArriveAirportId, fakeCreateCommandFlight.DurationMinutes, + fakeCreateCommandFlight.FlightDate, fakeCreateCommandFlight.Status, fakeCreateCommandFlight.Price); + await _fixture.InsertAsync(flightEntity); + + var command = new FakeUpdateFlightCommand(flightEntity.Id).Generate(); + + // Act + var flightResponse = await _fixture.SendAsync(command); + + // Assert + flightResponse.Should().NotBeNull(); + flightResponse?.Id.Should().Be(flightEntity?.Id); + flightResponse?.Price.Should().NotBe(flightEntity?.Price); + (await _fixture.IsFaultyPublished()).Should().BeFalse(); + (await _fixture.IsPublished()).Should().BeTrue(); + } +}