Example #1
0
        public async Task <FavoriteStation> GetFavoriteStation(string stationName)
        {
            var stations = await _bikeShareClient.GetStationsAsync();

            StationIdentity    stationIdentifier;
            StationCoordinates stationCoordinates;

            try
            {
                stationIdentifier = new StationIdentity(
                    stations.First(s => s.Name.ToLower().Equals(stationName.ToLower())).Name,
                    stations.First(s => s.Name.ToLower().Equals(stationName.ToLower())).Id
                    );
                stationCoordinates = new StationCoordinates
                {
                    Latitude  = stations.First(s => s.Name.ToLower().Equals(stationName.ToLower())).Latitude,
                    Longitude = stations.First(s => s.Name.ToLower().Equals(stationName.ToLower())).Longitude
                };
            }
            catch (InvalidOperationException noElementException)
            {
                if (!stationName.Equals(_defaultStationName))
                {
                    return(await GetFavoriteStation(_defaultStationName));
                }
                var bikeSystemInfo = await _bikeShareClient.GetSystemInformationAsync();

                throw new ArgumentException($"Can't find {_defaultStationName} station in bikeshare-provider {bikeSystemInfo.Name} ({bikeSystemInfo.OperatorName})");
            }
            var stationsStatuses = await _bikeShareClient.GetStationsStatusAsync();

            var stationStatus = stationsStatuses.First(s => s.Id.Equals(stationIdentifier.Id));

            return(new FavoriteStation(stationIdentifier.Name, stationStatus.BikesAvailable, stationStatus.DocksAvailable, stationCoordinates));
        }
        public async Task GetClosestAvailableStaiton_GivenSystemWithSingleEmptyStation_ReturnsNoAvailableStation()
        {
            var emptyStationIdentity     = new StationIdentity("mockStation", "mockStation");
            var singleEmptyStationStatus = new List <StationStatus> {
                new StationStatus(emptyStationIdentity.Name, 0, 0, 1, true, true, true, 0, DateTime.Now)
            };
            var singleEmptyStation = new List <Station> {
                new Station(emptyStationIdentity.Id, emptyStationIdentity.Name, "Empty Address", 12, 12, 1)
            };
            var singleStationSystemMock = new Mock <IBikeshareClient>();

            singleStationSystemMock.Setup(s => s.GetStationsStatusAsync()).Returns(Task.FromResult <IEnumerable <StationStatus> >(singleEmptyStationStatus));
            singleStationSystemMock.Setup(s => s.GetStationsAsync()).Returns(Task.FromResult <IEnumerable <Station> >(singleEmptyStation));
            var cut = new StationService(singleStationSystemMock.Object, emptyStationIdentity.Name);

            var emptyStation = await cut.GetClosestAvailableStation(await cut.GetFavoriteStation());

            Assert.Equal("No Available Bikes found from bikeshare provider.", emptyStation.Name);
        }