public Result <AddResultStatus> AddFlight(FlightToDisplay flightInput) { if (flightInput == null) { throw new ArgumentNullException("flightInput"); } if (flightInput.DepartureAirportID == flightInput.DestinationAirportID) { ///The airports chosen are the same return(new Result <AddResultStatus>(AddResultStatus.SameAirportsChosen)); } else { var resCalculation = CalculateFlightInfo(flightInput, out Flight flight) ?? throw new ArgumentNullException("resCalculation"); if (!resCalculation.Status) { return(new Result <AddResultStatus>(AddResultStatus.DataError, resCalculation.Info)); } if (flightRepository.CheckFlightEntriesAlreadyExist(flightInput)) { ///Flight exists in the DB return(new Result <AddResultStatus>(AddResultStatus.AlreadyExists)); } else { ///Flight does not exist in the DB, add it flightRepository.AddFlight(flight); return(new Result <AddResultStatus>(AddResultStatus.Success)); } } }
public void Setup() { var flight = new FlightToDisplay() { DepartureAirportID = 1, DestinationAirportID = 2, AircraftID = 1 }; var listFlights = new List <FlightToDisplay>() { flight }; fakeService = new Services.Fakes.StubIFlightService() { DisposeOfStorage = () => { }, AddFlightFlightToDisplay = (f) => { return(new Result <AddResultStatus>(AddResultStatus.Success)); }, RemoveFlightInt32 = (f) => { return(new Result <RemoveResultStatus>(RemoveResultStatus.Success)); }, EditFlightFlightToDisplay = (f) => { return(new Result <EditResultStatus>(EditResultStatus.Success)); }, GetFlightsToDisplayListOfFlightToDisplayOut = (out List <FlightToDisplay> f) => { f = listFlights; return(new Result <bool>(true)); }, GetFlightToDisplayInt32FlightToDisplayOut = (int id, out FlightToDisplay f) => { f = flight; return(new Result <bool>(true)); }, }; sut = new FlightsController(fakeService); }
public ActionResult Create([Bind(Include = "DepartureAirportID,DestinationAirportID,AircraftID")] FlightToDisplay flightInput) { if (ModelState.IsValid) { var res = flightService.AddFlight(flightInput) ?? throw new ArgumentNullException("res"); switch (res.Status) { case AddResultStatus.SameAirportsChosen: //The airports chosen are the same, retry ModelState.AddModelError("", "Please choose different airports"); break; case AddResultStatus.AlreadyExists: //Flight exists in the DB, retry ModelState.AddModelError("", "The entered flight already exists, please choose another one"); break; case AddResultStatus.Success: //New entry successfully added return(RedirectToAction("Index")); default: //Internal Error return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Unexpected Internal Error: " + res.Info)); } } ViewBag.SelectableAirports = flightService.Airports; ViewBag.SelectableAircrafts = flightService.Aircrafts; return(View(flightInput)); }
/// This test expects Edit to redirect to view if successful public void Edit_Should_Redirect_To_Index_If_Successful() { //Arrange var flight = new FlightToDisplay(); //Act var result = sut.Edit(flight) as RedirectToRouteResult; // Assert Assert.IsNotNull(result); Assert.AreEqual("Index", result.RouteValues["action"]); }
public bool CheckFlightEntriesAlreadyExist(FlightToDisplay flight) { if (flight == null) { throw new ArgumentNullException("flight"); } return(db.Flights.Any(f => ( f.AircraftID == flight.AircraftID && f.DepartureAirportID == flight.DepartureAirportID && f.DestinationAirportID == flight.DestinationAirportID) )); }
/// This test expects Edit show error page if failure public void Edit_Should_Show_Eror_Page_If_Failure() { //Arrange var flight = new FlightToDisplay(); fakeService.EditFlightFlightToDisplay = (f) => { return(new Result <EditResultStatus>(EditResultStatus.Failure)); }; //Act var result = sut.Edit(flight) as HttpStatusCodeResult; // Assert Assert.IsNotNull(result); }
/// This test expects Edit to redirect to view if successful public void Edit_Should_Redirect_To_Index_If_Entries_Not_Changed() { //Arrange var flight = new FlightToDisplay(); fakeService.EditFlightFlightToDisplay = (f) => { return(new Result <EditResultStatus>(EditResultStatus.EntriesNotChanged)); }; //Act var result = sut.Edit(flight) as RedirectToRouteResult; // Assert Assert.IsNotNull(result); Assert.AreEqual("Index", result.RouteValues["action"]); }
//This test expects EditFlight to succeed for default fake conditions public void EditFlight_Should_Succeed() { //Arrange var input = new FlightToDisplay() { DepartureAirportID = 2, DestinationAirportID = 3 }; //Act var res = sut.EditFlight(input); //Assert Assert.AreEqual(EditResultStatus.Success, res.Status); }
//This test expects EditFlight to fail if same airports are chosen public void EditFlight_Should_Fail_If_Same_Airports_Chosen() { //Arrange var input = new FlightToDisplay() { DepartureAirportID = 2, DestinationAirportID = 2 }; //Act var oldFlightsCount = flightsCount; var res = sut.EditFlight(input); //Assert Assert.AreEqual(EditResultStatus.SameAirportsChosen, res.Status); Assert.AreEqual(oldFlightsCount, flightsCount); }
//This test expects AddFlight to succeed for default fake conditions public void AddFlight_Should_Succeed() { //Arrange var input = new FlightToDisplay() { DepartureAirportID = 1, DestinationAirportID = 2 }; //Act var oldFlightsCount = flightsCount; var res = sut.AddFlight(input); //Assert Assert.AreEqual(AddResultStatus.Success, res.Status); Assert.AreEqual(oldFlightsCount + 1, flightsCount); }
//This test expects EditFlight to fail if the flight entries have not changed public void EditFlight_Should_Fail_If_Entries_Not_Changed() { //Arrange var input = new FlightToDisplay() { DepartureAirportID = 1, DestinationAirportID = 2 }; //Act var oldFlightsCount = flightsCount; var res = sut.EditFlight(input); //Assert Assert.AreEqual(EditResultStatus.EntriesNotChanged, res.Status); Assert.AreEqual(oldFlightsCount, flightsCount); }
public Result <EditResultStatus> EditFlight(FlightToDisplay flightInput) { if (flightInput == null) { throw new ArgumentNullException("flightInput"); } if (flightInput.DepartureAirportID == flightInput.DestinationAirportID) { ///The airports chosen are the same return(new Result <EditResultStatus>(EditResultStatus.SameAirportsChosen)); } var resCheckEntries = CheckFlightEntriesChanged(flightInput, out bool entriesChanged) ?? throw new ArgumentNullException("resCheckEntries"); if (!resCheckEntries.Status) { return(new Result <EditResultStatus>(EditResultStatus.Failure, resCheckEntries.Info)); } if (!entriesChanged) { ///Flight entries have not changed return(new Result <EditResultStatus>(EditResultStatus.EntriesNotChanged)); } if (flightRepository.CheckFlightEntriesAlreadyExist(flightInput)) { ///Flight exists in the DB return(new Result <EditResultStatus>(EditResultStatus.AlreadyExists)); } var resCalculation = CalculateFlightInfo(flightInput, out Flight flight) ?? throw new ArgumentNullException("resCalculation"); if (!resCalculation.Status) { return(new Result <EditResultStatus>(EditResultStatus.DataError, resCalculation.Info)); } if (flight == null) { throw new ArgumentNullException("flight"); } flightRepository.EditFlight(flight); return(new Result <EditResultStatus>(EditResultStatus.Success)); }
/// This test expects Edit to show model error if flight already exists public void Edit_Should_Show_Model_Error_If_Flight_Already_Exists() { //Arrange var flight = new FlightToDisplay(); fakeService.EditFlightFlightToDisplay = (f) => { return(new Result <EditResultStatus>(EditResultStatus.AlreadyExists)); }; //Act var result = sut.Edit(flight) as ViewResult; // Assert Assert.IsNotNull(result); Assert.IsNotNull(result.Model as FlightToDisplay); Assert.IsNotNull(result.ViewData.ModelState[""]); Assert.IsNotNull(result.ViewData.ModelState[""].Errors); Assert.IsTrue(result.ViewData.ModelState[""].Errors.Count == 1); }
/// This test expects Create to show model error if same airports chosen public void Create_Should_Show_Model_Error_If_Same_Airports_Chosen() { //Arrange var flight = new FlightToDisplay(); fakeService.AddFlightFlightToDisplay = (f) => { return(new Result <AddResultStatus>(AddResultStatus.SameAirportsChosen)); }; //Act var result = sut.Create(flight) as ViewResult; // Assert Assert.IsNotNull(result); Assert.IsNotNull(result.Model as FlightToDisplay); Assert.IsNotNull(result.ViewData.ModelState[""]); Assert.IsNotNull(result.ViewData.ModelState[""].Errors); Assert.IsTrue(result.ViewData.ModelState[""].Errors.Count == 1); }
//This test expects EditFlight to fail if flight already exists public void EditFlight_Should_Fail_If_Flight_Already_Exists() { //Arrange var input = new FlightToDisplay() { DepartureAirportID = 2, DestinationAirportID = 3 }; fakeFlightRepository.CheckFlightEntriesAlreadyExistFlightToDisplay = (f) => { return(true); }; //Act var oldFlightsCount = flightsCount; var res = sut.EditFlight(input); //Assert Assert.AreEqual(EditResultStatus.AlreadyExists, res.Status); Assert.AreEqual(oldFlightsCount, flightsCount); }
//This test expects AddFlight to fail if flight distance calculation fails public void AddFlight_Should_Fail_If_Distance_Calculation_Faults() { //Arrange var input = new FlightToDisplay() { DepartureAirportID = 1, DestinationAirportID = 2 }; fakeFlightCalculator.CalculateDistanceBetweenCoordinatesDoubleDoubleDoubleDouble = (a, b, c, d) => { throw new ArgumentException(); }; //Act var oldFlightsCount = flightsCount; var res = sut.AddFlight(input); //Assert Assert.AreEqual(AddResultStatus.DataError, res.Status); Assert.AreEqual(oldFlightsCount, flightsCount); }
//This test expects EditFlight to fail if flight's fuel consumption calculation fails public void EditFlight_Should_Fail_If_Fuel_Calculation_Faults() { //Arrange var input = new FlightToDisplay() { DepartureAirportID = 2, DestinationAirportID = 3 }; fakeFlightCalculator.CalculateFlightFuelConsumptionAircraftDouble = (a, b) => { throw new ArgumentException(); }; //Act var oldFlightsCount = flightsCount; var res = sut.EditFlight(input); //Assert Assert.AreEqual(EditResultStatus.DataError, res.Status); Assert.AreEqual(oldFlightsCount, flightsCount); }
//This test expects EditFlight to fail if finding the flight to check whether entries have changed fails public void EditFlight_Should_Fail_If_Find_Flight_To_Check_Entries_Fails() { //Arrange var input = new FlightToDisplay() { DepartureAirportID = 1, DestinationAirportID = 2 }; fakeFlightRepository.FindFlightInt32BooleanFlightOut = (int id, bool track, out Flight f) => { f = null; return(new Result <bool>(false)); }; //Act var oldFlightsCount = flightsCount; var res = sut.EditFlight(input); //Assert Assert.AreEqual(EditResultStatus.Failure, res.Status); Assert.AreEqual(oldFlightsCount, flightsCount); }
/// <summary> /// Get flight info from the DB. /// </summary> /// <param name="flightInput"> The chosen flight to display </param> /// <param name="flight"> Contains the flight retrieved from the Data Base, if retrieving successful, otherwise null </param> /// <returns> true if flight successfully retrieved, otherwise false </returns> private Result <bool> CalculateFlightInfo(FlightToDisplay flightInput, out Flight flight) { flight = null; var resDepAirport = flightRepository.FindAirport(flightInput.DepartureAirportID, out Airport departureAirport) ?? throw new ArgumentNullException("resDepAirport"); if (!resDepAirport.Status) { return(new Result <bool>(false, resDepAirport.Info)); } if (departureAirport == null) { throw new ArgumentNullException("departureAirport"); } var resDestAirport = flightRepository.FindAirport(flightInput.DestinationAirportID, out Airport destinationAirport) ?? throw new ArgumentNullException("resDestAirport"); if (!resDestAirport.Status) { return(new Result <bool>(false, resDestAirport.Info)); } if (destinationAirport == null) { throw new ArgumentNullException("destinationAirport"); } var resAircraft = flightRepository.FindAircraft(flightInput.AircraftID, out Aircraft aircraft) ?? throw new ArgumentNullException("resAircraft"); if (!resAircraft.Status) { return(new Result <bool>(false, resAircraft.Info)); } if (aircraft == null) { throw new ArgumentNullException("aircraft"); } //Calculate distance and fuel consumption double distance = 0; double fuelConsumption = 0; try { distance = flightCalculator.CalculateDistanceBetweenCoordinates(departureAirport.Latitude, departureAirport.Longitude, destinationAirport.Latitude, destinationAirport.Longitude); fuelConsumption = flightCalculator.CalculateFlightFuelConsumption(aircraft, distance); } catch (ArgumentException e) { //At least one of the supplied arguments is out of range return(new Result <bool>(false, "Error while calculating flight info: " + e.Message)); } flight = new Flight() { FlightID = flightInput.FlightID, Distance = distance.ToString("0.00"), FuelConsumption = fuelConsumption.ToString("0.00"), DepartureAirportID = departureAirport.AirportID, DestinationAirportID = destinationAirport.AirportID, AircraftID = aircraft.AircraftID, }; return(new Result <bool>(true)); }
public Result <bool> GetFlightToDisplay(int flightID, out FlightToDisplay flightToDisplay) { flightToDisplay = null; //Get Flight Info var resFlight = flightRepository.FindFlight(flightID, false, out Flight flight) ?? throw new ArgumentNullException("resFlight"); if (!resFlight.Status) { return(new Result <bool>(false, resFlight.Info)); } if (flight == null) { throw new ArgumentNullException("flight"); } //Get departure Airport name var resDepAirport = flightRepository.FindAirport(flight.DepartureAirportID, out Airport departureAirport) ?? throw new ArgumentNullException("resDepAirport"); if (!resDepAirport.Status) { return(new Result <bool>(false, resDepAirport.Info)); } if (departureAirport == null) { throw new ArgumentNullException("departureAirport"); } //Get arrival Airport name var resDestAirport = flightRepository.FindAirport(flight.DestinationAirportID, out Airport destinationAirport) ?? throw new ArgumentNullException("resDestAirport"); if (!resDestAirport.Status) { return(new Result <bool>(false, resDestAirport.Info)); } if (destinationAirport == null) { throw new ArgumentNullException("destinationAirport"); } //Get Aircraft name var resAircraft = flightRepository.FindAircraft(flight.AircraftID, out Aircraft aircraft) ?? throw new ArgumentNullException("resAircraft"); if (!resAircraft.Status) { return(new Result <bool>(false, resAircraft.Info)); } if (aircraft == null) { throw new ArgumentNullException("aircraft"); } flightToDisplay = new FlightToDisplay() { FlightID = flight.FlightID, AircraftID = flight.AircraftID, DepartureAirportID = flight.DepartureAirportID, DestinationAirportID = flight.DestinationAirportID, Distance = flight.Distance, FuelConsumption = flight.FuelConsumption, DepartureAirportName = departureAirport.AirportName, DestinationAirportName = destinationAirport.AirportName, AircraftName = aircraft.AircraftName, }; return(new Result <bool>(true)); }