public void SetUp() { _fakeTimeService = Substitute.For <ITimeService>(); _fakeTimeService.GetCurrentDate().Returns(new DateTime(2020, 1, 1)); _fakeTimeService.GetCurrentTime().Returns(new TimeSpan(0, 12, 0, 0)); _uut = new CreateRideRequest(_fakeTimeService); }
public async Task AddRideAsync_WhenRideIsASoloRide_ReturnsACreateRideResponseContainingTheCorrectPrice() { decimal distance = 10; decimal calculatedPrice = 100; _googleMapsApiService.GetDistanceInKmAsync(Arg.Any <string>(), Arg.Any <string>()) .ReturnsForAnyArgs(distance); _soloRidePriceStrategy.CalculatePrice(distance).Returns(calculatedPrice); //Stub the solo strategy var expectedResponse = new CreateRideResponse { Price = calculatedPrice }; _mapper.Map <CreateRideResponse>(null).ReturnsForAnyArgs(expectedResponse); _mapper.Map <SoloRide>(null) .ReturnsForAnyArgs(new SoloRide { StartDestination = _anAddress, EndDestination = _anAddress }); var request = new CreateRideRequest { StartDestination = _anAddress, EndDestination = _anAddress, RideType = RideType.SoloRide }; var response = await _rideService.AddRideAsync(request, "aCustomerId"); Assert.That(response.Price, Is.EqualTo(calculatedPrice)); }
/// <summary> /// Adds a ride to the database. /// </summary> /// <param name="request">The data used to create the ride.</param> /// <param name="customerId">The id of the customer that has requested to create the ride.</param> /// <returns>A response object containing information about the created ride.</returns> public Task <CreateRideResponse> AddRideAsync(CreateRideRequest request, string customerId) { if (request.RideType == RideType.SharedRide) { return(AddSharedRideAsync(request, customerId)); } return(AddSoloRideAsync(request, customerId)); }
[TestCase(null, 0)] //will default to false public void IsShared_WhenSet_ValidatesInput(RideType type, int numberOfErrors) { var request = new CreateRideRequest { RideType = type, DepartureTime = _validDepartureTime, ConfirmationDeadline = _validConfirmationTime, PassengerCount = _validPassengerCount, StartDestination = _validAddress, EndDestination = _validAddress }; Assert.That(ValidateModelHelper.ValidateModel(request).Count, Is.EqualTo(numberOfErrors)); }
public void EndDestination_WhenSetToNull_ValidationFails() { var request = new CreateRideRequest { RideType = _validIsShared, DepartureTime = _validDepartureTime, ConfirmationDeadline = _validConfirmationTime, PassengerCount = _validPassengerCount, StartDestination = _validAddress, EndDestination = new Address("City", 8210, "Street2", 1) }; Assert.That(ValidateModelHelper.ValidateModel(request).Count, Is.EqualTo(1)); }
public void ConfirmationTime_WhenSetToExcatlyNow_ValidationFails() { var expectedNumberOfErrors = 1; var request = new CreateRideRequest { RideType = _validIsShared, DepartureTime = _validDepartureTime, ConfirmationDeadline = DateTime.Now, PassengerCount = _validPassengerCount, StartDestination = _validAddress, EndDestination = _validAddress }; Assert.That(ValidateModelHelper.ValidateModel(request).Count, Is.EqualTo(expectedNumberOfErrors)); }
public async Task <IActionResult> Create([FromHeader] string authorization, [FromBody] CreateRideRequest request) { //Get the customerId that is stored as a claim in the token var customerId = User.Claims.FirstOrDefault(x => x.Type == Constants.UserIdClaim)?.Value; //This should never happen, but better safe than sorry if (string.IsNullOrEmpty(customerId)) { throw new UserIdInvalidException( $"The supplied JSON Web Token does not contain a valid value in the '{ Constants.UserIdClaim }' claim."); } var response = await _rideService.AddRideAsync(request, customerId); return(Ok(response)); }
public async Task AddRideAsync_WhenRideIsASharedRide_ReturnsACreateRideResponseWithWaitingForAccept() { decimal distance = 10; decimal calculatedPrice = 100; _googleMapsApiService.GetDistanceInKmAsync(Arg.Any <string>(), Arg.Any <string>()) .ReturnsForAnyArgs(distance); _sharedRidePriceStrategy.CalculatePrice(distance).Returns(calculatedPrice); var expectedResponse = new CreateRideResponse { Price = calculatedPrice }; _mapper.Map <CreateRideResponse>(null).ReturnsForAnyArgs(expectedResponse); _mapper.Map <SharedRide>(null) .ReturnsForAnyArgs(new SharedRide { StartDestination = _anAddress, EndDestination = _anAddress }); var request = new CreateRideRequest { StartDestination = _anAddress, EndDestination = _anAddress, RideType = RideType.SharedRide }; //Must have ID because otherwise both are null _unitOfWork.RideRepository.FindUnmatchedSharedRides().ReturnsForAnyArgs(new List <Ride>() { new Ride() { Id = 3, StartDestination = _anAddress, EndDestination = _anAddress, } }); _matchService.Match(null, null, 0).ReturnsForAnyArgs(true); var response = await _rideService.AddRideAsync(request, "aCustomerId"); //Validate through savechanges were called twice, //To acknowledge that the rides have matched. _unitOfWork.Received(2).SaveChangesAsync(); }
/// <summary> /// Initializes a new instance of the <see cref="CreateRideViewModel"/> class. /// </summary> /// <param name="navigationService">Navigation service.</param> /// <param name="pageDialogService">Page dialog service.</param> public CreateRideViewModel(INavigationService navigationService, IPageDialogService pageDialogService, IBackendApiService backEndApiService) : base(navigationService, pageDialogService) { Title = "Opret tur"; Request = new CreateRideRequest(new TimeService()); _backendApiService = backEndApiService; Price = "Beregn min pris"; //TEST /* * Request.OriginCityName = "Aarhus V"; * Request.OriginPostalCode = "8210"; * Request.OriginStreetName = "Bispehavevej"; * Request.OriginStreetNumber = "3"; * Request.DestinationCityName = "Aarhus C"; * Request.DestinationPostalCode = "8000"; * Request.DestinationStreetName = "Banegårdspladsen"; * Request.DestinationStreetNumber = "1"; */ }
/// <summary> /// Adds a solo ride to the database. /// </summary> /// <param name="request">The data used to create the ride.</param> /// <param name="customerId">The id of the customer that has requested to create the ride.</param> /// <returns>A response object containing information about the created ride.</returns> private async Task <CreateRideResponse> AddSoloRideAsync(CreateRideRequest request, string customerId) { var ride = _mapper.Map <SoloRide>(request); ride.Price = await CalculatePriceAsync(ride.StartDestination, ride.EndDestination, request.RideType); ride.CustomerId = customerId; //Reserve the money, create the order and return the created order await _unitOfWork.CustomerRepository.ReservePriceFromCustomerAsync(ride.CustomerId, ride.Price); ride = (SoloRide)_unitOfWork.RideRepository.Add(ride); var order = _unitOfWork.OrderRepository.Add(new Order()); await _unitOfWork.OrderRepository.AddRideToOrderAsync(ride, order); await _unitOfWork.SaveChangesAsync(); var response = _mapper.Map <CreateRideResponse>(ride); return(response); }
/// <summary> /// Adds a shared ride to the database. /// </summary> /// <param name="request">The data used to create the ride.</param> /// <param name="customerId">The id of the customer that has requested to create the ride.</param> /// <returns>A response object containing information about the created ride.</returns> private async Task <CreateRideResponse> AddSharedRideAsync(CreateRideRequest request, string customerId) { //Map request to ride var ride = _mapper.Map <SharedRide>(request); //Calculate price and attach customer ride.Price = await CalculatePriceAsync(ride.StartDestination, ride.EndDestination, request.RideType); ride.CustomerId = customerId; //Reserve the money and save the data to the database. await _unitOfWork.CustomerRepository.ReservePriceFromCustomerAsync(ride.CustomerId, ride.Price); _unitOfWork.RideRepository.AddSharedRide(ride); await _unitOfWork.SaveChangesAsync(); //Check for matches await CheckForMatches(ride); //No match var response = _mapper.Map <CreateRideResponse>(ride); return(response); }