Ejemplo n.º 1
0
        public async Task FlightControllerTests_Update_ShouldReturnBadRequest_WhenAircraftIdIsZero()
        {
            // Arrange
            var newFlight = new CreateFlightArgs
            {
                FlightNumber         = string.Empty,
                AircraftId           = 0,
                DepartureAirportId   = 1,
                DestinationAirportId = 2,
                FlightDate           = DateTime.Now,
                FlightTime           = DateTime.Now
            };

            var flightController = new FlightsController(_flightCoreServicesMock.Object, _airportCoreServicesMock.Object, _aircraftCoreServicesMock.Object);

            flightController.ModelState.AddModelError("AircraftId", "Aircraft Id is not Valid.");

            // Act
            var result = await flightController.CreateFlightPost(newFlight);

            // Assert
            Check.That(result).IsInstanceOfType(typeof(BadRequestObjectResult));
            var viewResult = result as BadRequestObjectResult;

            Check.That(viewResult).IsNotNull();
            Check.That(flightController.ModelState.IsValid).IsFalse();
            Check.That(viewResult.StatusCode).IsEqualTo(400);

            _flightCoreServicesMock.Verify(service => service.CreateFlight(It.IsAny <CreateFlightArgs>()), Times.Never);
        }
Ejemplo n.º 2
0
        public async Task FlightControllerTests_Update_ShouldCallFlightService_WhenModelIsValid()
        {
            // Arrange
            var newFlight = new CreateFlightArgs
            {
                FlightNumber         = "NewFlight",
                AircraftId           = 1,
                DepartureAirportId   = 1,
                DestinationAirportId = 2,
                FlightDate           = DateTime.Now,
                FlightTime           = DateTime.Now
            };

            var flightController = new FlightsController(_flightCoreServicesMock.Object, _airportCoreServicesMock.Object, _aircraftCoreServicesMock.Object);

            // Act
            var result = await flightController.CreateFlightPost(newFlight);

            // Assert
            Check.That(result).IsInstanceOfType(typeof(RedirectToActionResult));
            var viewResult = result as RedirectToActionResult;

            Check.That(viewResult).IsNotNull();
            Check.That(flightController.ModelState.IsValid).IsTrue();
            _flightCoreServicesMock.Verify(service => service.CreateFlight(It.IsAny <CreateFlightArgs>()), Times.Once);
        }
Ejemplo n.º 3
0
        public async Task <FlightDto> CreateFlight(CreateFlightArgs args)
        {
            CheckFlightUnicity(null, args.FlightNumber);

            var flightRelatedData = GetFlightRelatedData(args.DepartureAirportId, args.DestinationAirportId, args.AircraftId);

            var departure   = flightRelatedData.Departure;
            var destination = flightRelatedData.Destination;
            var aircraft    = flightRelatedData.Aircraft;

            var distance          = _flightHelper.CalculateDistance(new GpsCoordinates(departure.Latitude, departure.Longitude), new GpsCoordinates(departure.Latitude, destination.Longitude));
            var estimatedDuration = _flightHelper.CalculateDuration(distance, aircraft.MilesPerHour);

            var newFlight = new Flight
            {
                FlightNumber            = args.FlightNumber,
                FlightDate              = args.FlightDate,
                FlightTime              = args.FlightTime,
                AirportDepartureId      = args.DepartureAirportId,
                AirportDestinationId    = args.DestinationAirportId,
                AircraftId              = args.AircraftId,
                Distance                = distance,
                EstimatedFlightDuration = estimatedDuration,
                EstimatedFuelNeeded     = _flightHelper.CalculateFuelConsumption(estimatedDuration, aircraft.ConsumptionKgPerH, aircraft.TakeOffEffort),
                CreationDate            = DateTime.UtcNow,
                LastUpdateDate          = DateTime.UtcNow
            };

            var createdFlight = await _flightsRepository.AddAsync(newFlight);

            return(_mapper.MapFromFlight(createdFlight));
        }
Ejemplo n.º 4
0
        public void FlightServicesTests_CreateNewFlight_ShouldThrowDuplicateFlightException()
        {
            // Arrange
            _flightRepositoryMock.Setup(repo => repo.Search(It.IsAny <Expression <Func <Flight, bool> > >()))
            .Returns(new List <Flight>
            {
                _flightsRecordsMock.First()
            }.AsQueryable());

            var newFlight = new CreateFlightArgs
            {
                FlightNumber         = "FakeFlight_1",
                DepartureAirportId   = 1,
                DestinationAirportId = 2,
                AircraftId           = 1,
                FlightDate           = DateTime.Now,
                FlightTime           = DateTime.Now
            };

            var flightServices = new FlightServices(_flightRepositoryMock.Object,
                                                    _airportRepositoryMock.Object,
                                                    _aircraftRepositoryMock.Object,
                                                    _mapperWrapper);

            // Act
            Check.ThatCode(async() => await flightServices.CreateFlight(newFlight))
            .Throws <DuplicateFlightException>()
            .WithMessage(ErrorMessages.DUPLICATED_FLIGHT_NUMBER);
        }
Ejemplo n.º 5
0
        public async Task FlightServicesTests_CreateNewFlight_ShouldAddSuccessfulyNewFlight()
        {
            // Arrange
            _flightRepositoryMock.Setup(repo => repo.Search(It.IsAny <Expression <Func <Flight, bool> > >()))
            .Returns(new List <Flight>().AsQueryable());

            var newFlight = new CreateFlightArgs
            {
                FlightNumber         = "FakeFlight_" + 50,
                DepartureAirportId   = 1,
                DestinationAirportId = 2,
                AircraftId           = 1,
                FlightDate           = DateTime.Now,
                FlightTime           = DateTime.Now
            };

            var flightServices = new FlightServices(_flightRepositoryMock.Object,
                                                    _airportRepositoryMock.Object,
                                                    _aircraftRepositoryMock.Object,
                                                    _mapperWrapper);

            // Act
            var createdFlight = await flightServices.CreateFlight(newFlight);

            // Assert
            Check.That(createdFlight).IsNotNull();
            _flightRepositoryMock.Verify(repo => repo.AddAsync(It.IsAny <Flight>()), Times.Once);
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> CreateFlightPost(CreateFlightArgs newFlight)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var createdFlight = await _flightServices.CreateFlight(newFlight);

            return(RedirectToAction("index"));
        }
Ejemplo n.º 7
0
        public void FlightServicesTests_CreateNewFlight_ShouldThrowAircraftNotFoundException()
        {
            // Arrange
            var newFlight = new CreateFlightArgs
            {
                FlightNumber         = "FakeFlight_30",
                DepartureAirportId   = 1,
                DestinationAirportId = 2,
                AircraftId           = 134343,
                FlightDate           = DateTime.Now,
                FlightTime           = DateTime.Now
            };

            var flightServices = new FlightServices(_flightRepositoryMock.Object,
                                                    _airportRepositoryMock.Object,
                                                    _aircraftRepositoryMock.Object,
                                                    _mapperWrapper);

            // Act
            Check.ThatCode(async() => await flightServices.CreateFlight(newFlight))
            .Throws <AircraftNotFoundException>()
            .WithMessage(string.Format(ErrorMessages.AIRCRAFT_NOT_FOUND, newFlight.AircraftId));
        }
Ejemplo n.º 8
0
        public void FlightServicesTests_CreateNewFlight_ShouldThrowSameDepartureAndDestinationAirportsException()
        {
            // Arrange
            var newFlight = new CreateFlightArgs
            {
                FlightNumber         = "FakeFlight_30",
                DepartureAirportId   = 1,
                DestinationAirportId = 1,
                AircraftId           = 1,
                FlightDate           = DateTime.Now,
                FlightTime           = DateTime.Now
            };

            var flightServices = new FlightServices(_flightRepositoryMock.Object,
                                                    _airportRepositoryMock.Object,
                                                    _aircraftRepositoryMock.Object,
                                                    _mapperWrapper);

            // Act
            Check.ThatCode(async() => await flightServices.CreateFlight(newFlight))
            .Throws <SameDepartureAndDestinationAirportsException>()
            .WithMessage(ErrorMessages.SAME_DEPARTURE_AND_DESTINATION);
        }