diff --git a/.editorconfig b/.editorconfig index 18b259f..59e3b23 100644 --- a/.editorconfig +++ b/.editorconfig @@ -245,7 +245,7 @@ csharp_preserve_single_line_statements = false csharp_preserve_single_line_blocks = true # Namespace options # https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/formatting-rules#namespace-options -csharp_style_namespace_declarations = block_scoped:silent +csharp_style_namespace_declarations = file_scoped:warning ########################################## # .NET Naming Rules diff --git a/src/BuildingBlocks/Utils/ServiceLocator.cs b/src/BuildingBlocks/Utils/ServiceLocator.cs new file mode 100644 index 0000000..c8d3ce8 --- /dev/null +++ b/src/BuildingBlocks/Utils/ServiceLocator.cs @@ -0,0 +1,38 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace BuildingBlocks.Utils; + +//ref: https://dotnetcoretutorials.com/2018/05/06/servicelocator-shim-for-net-core/ +public class ServiceLocator +{ + private IServiceProvider _currentServiceProvider; + private static IServiceProvider _serviceProvider; + + public ServiceLocator(IServiceProvider currentServiceProvider) + { + _currentServiceProvider = currentServiceProvider; + } + + public static ServiceLocator Current + { + get + { + return new ServiceLocator(_serviceProvider); + } + } + + public static void SetLocatorProvider(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public object GetInstance(Type serviceType) + { + return _currentServiceProvider.GetService(serviceType); + } + + public TService GetInstance() + { + return _currentServiceProvider.GetService(); + } +} diff --git a/src/Services/Flight/tests/UnitTest/Fakes/FakeFlightCreate.cs b/src/Services/Flight/tests/UnitTest/Fakes/FakeFlightCreate.cs new file mode 100644 index 0000000..5d4660b --- /dev/null +++ b/src/Services/Flight/tests/UnitTest/Fakes/FakeFlightCreate.cs @@ -0,0 +1,14 @@ +namespace Unit.Test.Fakes; + +public static class FakeFlightCreate +{ + public static global::Flight.Flights.Models.Flight Generate() + { + var command = new FakeCreateFlightCommand().Generate(); + + return 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); + } +} diff --git a/src/Services/Flight/tests/UnitTest/Fakes/FakeFlightUpdate.cs b/src/Services/Flight/tests/UnitTest/Fakes/FakeFlightUpdate.cs new file mode 100644 index 0000000..b1e5087 --- /dev/null +++ b/src/Services/Flight/tests/UnitTest/Fakes/FakeFlightUpdate.cs @@ -0,0 +1,12 @@ +namespace Unit.Test.Fakes; + +using global::Flight.Flights.Models; + +public static class FakeFlightUpdate +{ + public static void Generate(Flight flight) + { + flight.Update(flight.Id, flight.FlightNumber, 3, flight.DepartureAirportId, flight.DepartureDate, + flight.ArriveDate, 3, flight.DurationMinutes, flight.FlightDate, flight.Status, flight.Price, flight.IsDeleted);; + } +} diff --git a/src/Services/Flight/tests/UnitTest/Flight/Features/CreateFlight/CreateFlightCommandHandlerTests.cs b/src/Services/Flight/tests/UnitTest/Flight/Features/Commands/CreateFlight/CreateFlightCommandHandlerTests.cs similarity index 86% rename from src/Services/Flight/tests/UnitTest/Flight/Features/CreateFlight/CreateFlightCommandHandlerTests.cs rename to src/Services/Flight/tests/UnitTest/Flight/Features/Commands/CreateFlight/CreateFlightCommandHandlerTests.cs index b78db65..90e10bf 100644 --- a/src/Services/Flight/tests/UnitTest/Flight/Features/CreateFlight/CreateFlightCommandHandlerTests.cs +++ b/src/Services/Flight/tests/UnitTest/Flight/Features/Commands/CreateFlight/CreateFlightCommandHandlerTests.cs @@ -1,18 +1,15 @@ -using System; +namespace Unit.Test.Flight.Features.Commands.CreateFlight; + +using System; using System.Threading; using System.Threading.Tasks; -using Flight.Flights.Dtos; -using Flight.Flights.Features.CreateFlight; -using Flight.Flights.Features.CreateFlight.Commands.V1; using FluentAssertions; -using Microsoft.AspNetCore.Http; -using NSubstitute; +using global::Flight.Flights.Dtos; +using global::Flight.Flights.Features.CreateFlight.Commands.V1; using Unit.Test.Common; using Unit.Test.Fakes; using Xunit; -namespace Unit.Test.Flight.Features.CreateFlight; - [Collection(nameof(UnitTestFixture))] public class CreateFlightCommandHandlerTests { diff --git a/src/Services/Flight/tests/UnitTest/Flight/Features/CreateFlight/CreateFlightCommandValidatorTests.cs b/src/Services/Flight/tests/UnitTest/Flight/Features/Commands/CreateFlight/CreateFlightCommandValidatorTests.cs similarity index 87% rename from src/Services/Flight/tests/UnitTest/Flight/Features/CreateFlight/CreateFlightCommandValidatorTests.cs rename to src/Services/Flight/tests/UnitTest/Flight/Features/Commands/CreateFlight/CreateFlightCommandValidatorTests.cs index d126811..043f0ca 100644 --- a/src/Services/Flight/tests/UnitTest/Flight/Features/CreateFlight/CreateFlightCommandValidatorTests.cs +++ b/src/Services/Flight/tests/UnitTest/Flight/Features/Commands/CreateFlight/CreateFlightCommandValidatorTests.cs @@ -1,13 +1,12 @@ -using Flight.Flights.Features.CreateFlight; -using Flight.Flights.Features.CreateFlight.Commands.V1; +namespace Unit.Test.Flight.Features.Commands.CreateFlight; + using FluentAssertions; using FluentValidation.TestHelper; +using global::Flight.Flights.Features.CreateFlight.Commands.V1; using Unit.Test.Common; using Unit.Test.Fakes; using Xunit; -namespace Unit.Test.Flight.Features.CreateFlight; - [Collection(nameof(UnitTestFixture))] public class CreateFlightCommandValidatorTests { diff --git a/src/Services/Flight/tests/UnitTest/Flight/Features/Domain/CreateFlightTests.cs b/src/Services/Flight/tests/UnitTest/Flight/Features/Domain/CreateFlightTests.cs new file mode 100644 index 0000000..2dd003e --- /dev/null +++ b/src/Services/Flight/tests/UnitTest/Flight/Features/Domain/CreateFlightTests.cs @@ -0,0 +1,34 @@ +namespace Unit.Test.Flight.Features.Domain +{ + using System.Linq; + using FluentAssertions; + using global::Flight.Flights.Features.CreateFlight.Events.Domain.V1; + using Unit.Test.Common; + using Unit.Test.Fakes; + using Xunit; + + [Collection(nameof(UnitTestFixture))] + public class CreateFlightTests + { + [Fact] + public void can_create_valid_flight() + { + // Arrange + Act + var fakeFlight = FakeFlightCreate.Generate(); + + // Assert + fakeFlight.Should().NotBeNull(); + } + + [Fact] + public void queue_domain_event_on_create() + { + // Arrange + Act + var fakeFlight = FakeFlightCreate.Generate(); + + // Assert + fakeFlight.DomainEvents.Count.Should().Be(1); + fakeFlight.DomainEvents.FirstOrDefault().Should().BeOfType(typeof(FlightCreatedDomainEvent)); + } + } +} diff --git a/src/Services/Flight/tests/UnitTest/Flight/Features/Domain/UpdateFlightTests.cs b/src/Services/Flight/tests/UnitTest/Flight/Features/Domain/UpdateFlightTests.cs new file mode 100644 index 0000000..9b79329 --- /dev/null +++ b/src/Services/Flight/tests/UnitTest/Flight/Features/Domain/UpdateFlightTests.cs @@ -0,0 +1,41 @@ +namespace Unit.Test.Flight.Features.Domain; + +using System.Linq; +using FluentAssertions; +using global::Flight.Flights.Features.UpdateFlight.Events.V1; +using Unit.Test.Common; +using Unit.Test.Fakes; +using Xunit; + +[Collection(nameof(UnitTestFixture))] +public class UpdateFlightTests +{ + [Fact] + public void can_update_valid_flight() + { + // Arrange + var fakeFlight = FakeFlightCreate.Generate(); + + // Act + FakeFlightUpdate.Generate(fakeFlight); + + // Assert + fakeFlight.ArriveAirportId.Should().Be(3); + fakeFlight.AircraftId.Should().Be(3); + } + + [Fact] + public void queue_domain_event_on_update() + { + // Arrange + var fakeFlight = FakeFlightCreate.Generate(); + fakeFlight.ClearDomainEvents(); + + // Act + FakeFlightUpdate.Generate(fakeFlight); + + // Assert + fakeFlight.DomainEvents.Count.Should().Be(1); + fakeFlight.DomainEvents.FirstOrDefault().Should().BeOfType(typeof(FlightUpdatedDomainEvent)); + } +} diff --git a/src/Services/Flight/tests/UnitTest/Unit.Test.csproj b/src/Services/Flight/tests/UnitTest/Unit.Test.csproj index 1807978..144c97b 100644 --- a/src/Services/Flight/tests/UnitTest/Unit.Test.csproj +++ b/src/Services/Flight/tests/UnitTest/Unit.Test.csproj @@ -21,4 +21,8 @@ + + + +