Ejemplo n.º 1
0
        public async Task FlightCreatedEventHandler_ValidEvent_Success()
        {
            // Arrange
            var expectedEvent  = _fixture.Create <FlightCreatedEvent>();
            var expectedFlight = DomainHelpers.CreateValidFlight();

            _flightRepositoryMock
            .Setup(x => x.GetByIdAsync(expectedEvent.Id))
            .ReturnsAsync(expectedFlight);

            _airportRepositoryMock
            .Setup(x => x.GetByIdAsync(expectedFlight.DepartureAirportId))
            .ReturnsAsync(DomainHelpers.CreateValidAirport());

            _airportRepositoryMock
            .Setup(x => x.GetByIdAsync(expectedFlight.DestinationAirportId))
            .ReturnsAsync(DomainHelpers.CreateValidAirport());

            var target = new FlightCreatedEventHandler(
                _reportItemRepositoryMock.Object,
                _flightRepositoryMock.Object,
                _airportRepositoryMock.Object);

            // Act
            await target.Handle(expectedEvent, CancellationToken.None);

            // Assert
            _reportItemRepositoryMock.Verify(
                x => x.AddAsync(It.IsAny <ReportItem>()),
                Times.Once);

            _flightRepositoryMock.Verify();
            _airportRepositoryMock.Verify();
        }
Ejemplo n.º 2
0
        public async Task FlightUpdate_ValidCommand_CommandHandledWithSuccess()
        {
            // Arrange
            var expectedFlight = DomainHelpers.CreateValidFlight();

            var command = new FlightUpdate.Command(
                expectedFlight.Id,
                Guid.NewGuid(),
                Guid.NewGuid());

            var handler = new FlightUpdate.Handler(_mediatorMock.Object, _flightRepository.Object);

            _flightRepository
            .Setup(x => x.GetByIdAsync(command.Id))
            .ReturnsAsync(expectedFlight);

            // Act
            await handler.Handle(command, CancellationToken.None);

            // Assert
            _flightRepository.Verify(x => x.UpdateAsync(command.Id, expectedFlight), Times.Once);

            _mediatorMock.Verify(x => x.Publish <INotification>(
                                     It.IsAny <FlightDepartureAirportUpdatedEvent>(),
                                     It.IsAny <CancellationToken>()));

            _mediatorMock.Verify(x => x.Publish <INotification>(
                                     It.IsAny <FlightDestinationAirportUpdatedEvent>(),
                                     It.IsAny <CancellationToken>()));
        }
Ejemplo n.º 3
0
        public AirportTests()
        {
            _fixture = new Fixture();

            _validLatitude  = DomainHelpers.GetValidLatitude();
            _validLongitude = DomainHelpers.GetValidLongitude();
        }
Ejemplo n.º 4
0
        public void ClearDomainEvents_RemoveEvents_Success()
        {
            // Arrange
            var flight = DomainHelpers.CreateValidFlight();

            // Act
            flight.ClearDomainEvents();

            // Assert
            flight.DomainEvents.Should().BeEmpty();
        }
Ejemplo n.º 5
0
        public void AddDomainEvent_AddFirstEvent_CreatesDomainEventsList()
        {
            // Arrange
            var flight = DomainHelpers.CreateValidFlight();

            // Act & Assert
            flight.DomainEvents.Should().NotBeNull();
            flight.DomainEvents.Should()
            .Contain(x => x.GetType() == typeof(FlightCreatedEvent))
            .And.HaveCount(1);
        }
Ejemplo n.º 6
0
        public void ToDto_ValidFlightAndDistance_Success()
        {
            // Arrange
            var flight   = DomainHelpers.CreateValidFlight();
            var distance = _fixture.Create <Distance>();

            // Act
            var flightDto = flight.ToDto(distance);

            // Assert
            flightDto.Id.Should().Be(flight.Id);
            flightDto.DepartureAirportId.Should().Be(flight.DepartureAirportId);
            flightDto.DestinationAirportId.Should().Be(flight.DestinationAirportId);
            flightDto.FlightDuration.Should().Be(flight.GetFlightDuration(distance));
            flightDto.FuelRequiredInLitres.Should().Be(flight.GetFuelRequired(distance).Litres);
            flightDto.DistanceInKilometers.Should().Be(distance.Kilometers);
            flightDto.CreatedAt.Should().Be(flight.CreatedAt);
        }
Ejemplo n.º 7
0
        public async Task AirportAll_ValidQuery_Success()
        {
            // Arrange
            var query   = new AirportAll.Query();
            var handler = new AirportAll.Handler(_mapper, _airportRepositoryMock.Object);

            var expectedAirports = DomainHelpers.CreateManyValidAirports().ToList();

            _airportRepositoryMock
            .Setup(x => x.GetAllAsync())
            .ReturnsAsync(expectedAirports);

            // Act
            var result = await handler.Handle(query, CancellationToken.None);

            // Assert
            result.Count().Should().Be(expectedAirports.Count);
        }
Ejemplo n.º 8
0
        public async Task FlightDelete_ValidCommand_CommandHandledWithSuccess()
        {
            // Arrange
            var command = _fixture.Create <FlightDelete.Command>();
            var handler = new FlightDelete.Handler(_mediatorMock.Object, _flightRepository.Object);

            _flightRepository
            .Setup(x => x.GetByIdAsync(command.Id))
            .ReturnsAsync(DomainHelpers.CreateValidFlight());

            // Act
            await handler.Handle(command, CancellationToken.None);

            // Assert
            _flightRepository.Verify(x => x.DeleteAsync(command.Id), Times.Once);

            _mediatorMock.Verify(x => x.Publish <INotification>(
                                     It.IsAny <FlightDeletedEvent>(),
                                     It.IsAny <CancellationToken>()));
        }
Ejemplo n.º 9
0
        public async Task GetDistanceBetweenAirportsAsync_ValidFlight_ReturnsDistance()
        {
            // Arrange
            var flight = DomainHelpers.CreateValidFlight();

            _airportRepositoryMock
            .Setup(x => x.GetByIdAsync(flight.DepartureAirportId))
            .ReturnsAsync(DomainHelpers.CreateValidAirport());

            _airportRepositoryMock
            .Setup(x => x.GetByIdAsync(flight.DestinationAirportId))
            .ReturnsAsync(DomainHelpers.CreateValidAirport());

            var target = new FlightDomainService(_airportRepositoryMock.Object);

            // Act
            var distance = await target.GetDistanceBetweenAirportsAsync(flight);

            // Assert
            distance.Should().NotBeNull();
        }
Ejemplo n.º 10
0
        public async Task FlightById_ValidQuery_Success()
        {
            // Arrange
            var expectedFlight = DomainHelpers.CreateValidFlight();

            var query   = new FlightById.Query(expectedFlight.Id);
            var handler = new FlightById.Handler(_flightRepositoryMock.Object, _flightDomainServiceMock.Object);

            _flightRepositoryMock
            .Setup(x => x.GetByIdAsync(expectedFlight.Id))
            .ReturnsAsync(expectedFlight);

            _flightDomainServiceMock
            .Setup(x => x.GetDistanceBetweenAirportsAsync(It.IsAny <Flight>()))
            .ReturnsAsync(_fixture.Create <Distance>());

            // Act
            var result = await handler.Handle(query, CancellationToken.None);

            // Assert
            result.Id.Should().Be(expectedFlight.Id);
        }
Ejemplo n.º 11
0
        public async Task DispatchDomainEventsAsync_EventsCreated_PublishEventsAndEmptyList()
        {
            // Arrange
            var flight = DomainHelpers.CreateValidFlight();

            flight.ChangeDestinationAirport(Guid.NewGuid());

            // Act
            await _mediatorMock.Object.DispatchDomainEventsAsync(flight);

            // Assert
            _mediatorMock.Verify(x => x.Publish <INotification>(
                                     It.IsAny <FlightCreatedEvent>(),
                                     It.IsAny <CancellationToken>()),
                                 Times.Once);

            _mediatorMock.Verify(x => x.Publish <INotification>(
                                     It.IsAny <FlightDestinationAirportUpdatedEvent>(),
                                     It.IsAny <CancellationToken>()),
                                 Times.Once);

            flight.DomainEvents.Should().BeEmpty();
        }