Example #1
0
        public void GetUZContextAsyncExpectTokenAndCookiesPresent()
        {
            UzContext context = _uzClient.GetUZContextAsync().Result;

            Assert.IsFalse(string.IsNullOrWhiteSpace(context.Token));
            Assert.IsNotNull(context.Cookie["_gv_sessid"]);
        }
Example #2
0
        public async void Check()
        {
            UzContext uzContext = await _uzClient.GetUZContextAsync();

            Request[] requests = GetRequests();
            Console.WriteLine($"Checking requests: {requests.Length}");
            foreach (Request request in requests)
            {
                TicketCheckResult checkResult = await CheckTicket(uzContext, request);

                switch (checkResult.RequestState)
                {
                case RequestState.Expired: {
                    NotifyExpired(request);
                    break;
                }

                case RequestState.Found: {
                    BookPlacesResponse bookingResult = await _ticketBooker.BookPlacesAsync(uzContext, checkResult, request);

                    if (!bookingResult.IsError)
                    {
                        NotifyFoundAsync(bookingResult, request);
                    }
                    Console.WriteLine(checkResult);
                    break;
                }
                }
            }
        }
Example #3
0
        private async Task <TicketCheckResult> CheckTicket(UzContext uzContext, Request request)
        {
            if (IsRequestExpired(request))
            {
                return(new TicketCheckResult {
                    RequestState = RequestState.Expired
                });
            }

            var resultedTrains = new List <Train>();

            string wagonTypeCode = UzFormatConverter.Get(request.WagonType);
            ICollection <TrainsResponse.Train> trains = await GetTrains(uzContext, request, wagonTypeCode);

            foreach (TrainsResponse.Train train in trains)
            {
                ICollection <ResponseWagonWithPlaceInfo> wagons = await GetWagons(uzContext, train, wagonTypeCode, request);

                if (!wagons.Any())
                {
                    continue;
                }
                Train resultedTrain = ConvertToResultedTrain(train, wagons, wagonTypeCode);
                resultedTrains.Add(resultedTrain);
            }
            return(new TicketCheckResult {
                Trains = resultedTrains,
                RequestState = resultedTrains.Any(train => train.Wagons.Any()) ? RequestState.Found : RequestState.Requested
            });
        }
Example #4
0
        public void BookPlaceAsyncWithValidParametersExpectPlaceBooked()
        {
            UzContext      context        = _uzClient.GetUZContextAsync().Result;
            TrainsResponse trainsResponse = GetTrains(context, KievStationId, LvovStatinId, DateTime.Today.AddDays(DaysOffset));

            TrainsResponse.Train train          = trainsResponse.Trains.First();
            WagonsResponse       wagonsResponse = _uzClient.GetWagonsAsync(context, KievStationId, LvovStatinId, train.StationFrom.DateTime, train.TrainNumber, train.TrainType, train.Wagons.First().TypeCode).Result;

            WagonsResponse.Wagon wagon          = wagonsResponse.Wagons.First();
            PlacesResponse       placesResponse = GetPlaces(context, KievStationId, LvovStatinId, train.StationFrom.DateTime, train.TrainNumber, wagon.Number, wagon.CoachClass, wagon.CoachType);

            BookPlacesResponse bookPlaceResult = BookPlace(context,
                                                           KievStationId,
                                                           LvovStatinId,
                                                           DateTime.Today.AddDays(DaysOffset),
                                                           train.TrainNumber,
                                                           wagon.Number,
                                                           wagon.CoachClass,
                                                           wagon.TypeCode,
                                                           placesResponse.Places.First(),
                                                           placesResponse.PlaceType,
                                                           "Валера",
                                                           "Топор");

            Assert.IsFalse(bookPlaceResult.IsError);
        }
Example #5
0
        public void GetStationsAsyncWithInvalidTermExpectNoStations()
        {
            UzContext context = _uzClient.GetUZContextAsync().Result;

            StationsResonse stationsResonse = _uzClient.GetStationsAsync(context, "Зажопинск").Result;

            CollectionAssert.IsEmpty(stationsResonse.Stations);
        }
Example #6
0
        // POST api/wagons
        public async Task <IHttpActionResult> Post([FromBody] BookPlacesDto bookPlacesDto)
        {
            UzContext context = await UZ.GetUZContextAsync();

            BookPlacesResponse places = await UZ.BookPlaceAsync(context, bookPlacesDto.StationFromId, bookPlacesDto.StationToId, bookPlacesDto.DateTime, bookPlacesDto.TrainNumber, bookPlacesDto.WagonNumber, bookPlacesDto.CoachClass, bookPlacesDto.WagonTypeCode, bookPlacesDto.Places[0], bookPlacesDto.PlaceType, "Serhii", "Kharchenko");

            return(Json(places));
        }
Example #7
0
        // POST api/stations
        public async Task <IHttpActionResult> Post([FromBody] StationsRequestDto requestDto)
        {
            UzContext context = await UZ.GetUZContextAsync();

            StationsResonse stations = await UZ.GetStationsAsync(context, requestDto.Term);

            return(Json(stations));
        }
Example #8
0
        // POST api/wagons
        public async Task <IHttpActionResult> Post([FromBody] PlacesRequestDto requestDto)
        {
            UzContext context = await UZ.GetUZContextAsync();

            PlacesResponse places = await UZ.GetPlacesAsync(context, requestDto.StationFromId, requestDto.StationToId, requestDto.DateTime, requestDto.TrainNumber, requestDto.WagonNumber, requestDto.CoachClass, requestDto.CoachType);

            return(Json(places));
        }
Example #9
0
        // POST api/trains
        public async Task <IHttpActionResult> Post([FromBody] TrainsRequestDto requestDto)
        {
            UzContext context = await UZ.GetUZContextAsync();

            TrainsResponse trains = await UZ.GetTrainsAsync(context, requestDto.StationFromId, requestDto.StationToId, requestDto.DateTime);

            return(Json(trains));
        }
Example #10
0
        public void GetTrainsAsyncWithValidParametersExpectTrains()
        {
            UzContext context = _uzClient.GetUZContextAsync().Result;

            TrainsResponse trainsResponse = GetTrains(context, KievStationId, LvovStatinId, DateTime.Today.AddDays(DaysOffset));

            CollectionAssert.IsNotEmpty(trainsResponse.Trains);
        }
Example #11
0
        public void GetTrainsAsyncWithInvalidDateExpectNoTrains()
        {
            UzContext context = _uzClient.GetUZContextAsync().Result;

            TrainsResponse trainsResponse = GetTrains(context, KievStationId, LvovStatinId, DateTime.Today.AddDays(-1));

            CollectionAssert.IsEmpty(trainsResponse.Trains);
        }
Example #12
0
        // POST api/wagons
        public async Task <IHttpActionResult> Post([FromBody] WagonsRequestDto requestDto)
        {
            UzContext context = await UZ.GetUZContextAsync();

            WagonsResponse wagons = await UZ.GetWagonsAsync(context, requestDto.StationFromId, requestDto.StationToId, requestDto.DateTime,
                                                            requestDto.TrainNumber, requestDto.TrainType, requestDto.WagonType);

            return(Json(wagons));
        }
Example #13
0
        private async Task <ICollection <TrainsResponse.Train> > GetTrains(UzContext uzContext, Request request, string wagonTypeId)
        {
            TrainsResponse trainsResponse = await _uzClient.GetTrainsAsync(uzContext, request.StationFromId, request.StationToId,
                                                                           request.DateTime);

            return((from train in trainsResponse.Trains
                    from wagon in train.Wagons
                    where wagonTypeId == wagon.TypeCode
                    select train).ToList());
        }
Example #14
0
        private async Task <ICollection <ResponseWagonWithPlaceInfo> > GetWagons(UzContext uzContext, TrainsResponse.Train train, string wagonTypeId, Request request)
        {
            WagonsResponse wagonsResponse =
                await _uzClient.GetWagonsAsync(uzContext, train.StationFrom.Id,
                                               train.StationTo.Id,
                                               train.StationFrom.DateTime,
                                               train.TrainNumber, train.TrainType, wagonTypeId);

            return(await FilterWagonsByAvailablePlaces(uzContext, train, wagonsResponse, request));
        }
Example #15
0
        public void GetTrainsAsyncWithInvalidContextExpectNoTrains()
        {
            var context = new UzContext("token", new CookieCollection());

            TrainsResponse trainsResponse = _uzClient.GetTrainsAsync(context,
                                                                     KievStationId, // Киев
                                                                     LvovStatinId,  // Львов
                                                                     DateTime.Today.AddDays(DaysOffset)
                                                                     ).Result;

            CollectionAssert.IsEmpty(trainsResponse.Trains);
        }
Example #16
0
        public void GetWagonsAsyncWithInvalidTrainNumberExpectNoWagons()
        {
            UzContext      context        = _uzClient.GetUZContextAsync().Result;
            TrainsResponse trainsResponse = GetTrains(context, KievStationId, LvovStatinId, DateTime.Today.AddDays(DaysOffset));

            TrainsResponse.Train train = trainsResponse.Trains.First();

            WagonsResponse wagonsResponse = GetWagons(context,
                                                      KievStationId,
                                                      LvovStatinId,
                                                      train.StationFrom.DateTime,
                                                      "invalid_train",
                                                      train.TrainType,
                                                      train.Wagons.First().TypeCode);

            CollectionAssert.IsEmpty(wagonsResponse.Wagons);
        }
Example #17
0
        public async Task <BookPlacesResponse> BookPlacesAsync(UzContext uzContext, TicketCheckResult checkResult, Request request)
        {
            var uzClient = new UzClient();

            Tuple <Train, Wagon> cheapestTrainAndWagon = GetCheapestTrainAndWagon(checkResult);
            Train train = cheapestTrainAndWagon.Item1;
            Wagon wagon = cheapestTrainAndWagon.Item2;

            BookPlacesResponse bookingResult =
                await uzClient.BookPlaceAsync(uzContext, train.StationFrom.Id, train.StationTo.Id,
                                              train.StationFrom.DateTime,
                                              train.TrainNumber, wagon.Number, wagon.CoachClass, wagon.TypeCode,
                                              wagon.Places.First(), wagon.PlaceType, request.PassangerName,
                                              request.PassangerSurname);

            return(bookingResult);
        }
Example #18
0
        public void GetPlacesAsyncWithInvalidCoachTypeExpectNoPlaces()
        {
            UzContext      context        = _uzClient.GetUZContextAsync().Result;
            TrainsResponse trainsResponse = GetTrains(context, KievStationId, LvovStatinId, DateTime.Today.AddDays(DaysOffset));

            TrainsResponse.Train train          = trainsResponse.Trains.First();
            WagonsResponse       wagonsResponse = _uzClient.GetWagonsAsync(context, KievStationId, LvovStatinId, train.StationFrom.DateTime, train.TrainNumber, train.TrainType, train.Wagons.First().TypeCode).Result;

            WagonsResponse.Wagon wagon = wagonsResponse.Wagons.First();

            PlacesResponse placesResponse = GetPlaces(context,
                                                      KievStationId,
                                                      LvovStatinId,
                                                      DateTime.Now.AddDays(-1),
                                                      train.TrainNumber,
                                                      wagon.Number,
                                                      wagon.CoachClass,
                                                      int.MaxValue);

            CollectionAssert.IsEmpty(placesResponse.Places);
        }
Example #19
0
        public void GetStationsAsyncWithValidTermExpectStationsPresent()
        {
            UzContext context = _uzClient.GetUZContextAsync().Result;

            StationsResonse stationsResonse = _uzClient.GetStationsAsync(context, "Киев").Result;

            CollectionAssert.AreEquivalent(new List <StationsResonse.Station> {
                new StationsResonse.Station {
                    Id = KievStationId, Title = "Киев"
                },
                new StationsResonse.Station {
                    Id = 2201180, Title = "Киевская Русановка"
                },
                new StationsResonse.Station {
                    Id = 2058041, Title = "Киевская"
                },
                new StationsResonse.Station {
                    Id = 2064627, Title = "Киевский"
                },
            }, stationsResonse.Stations);
        }
Example #20
0
        public void GetPlacesAsyncWithValidParametersExpectPlaces()
        {
            UzContext      context        = _uzClient.GetUZContextAsync().Result;
            TrainsResponse trainsResponse = GetTrains(context, KievStationId, LvovStatinId, DateTime.Today.AddDays(DaysOffset));

            TrainsResponse.Train train          = trainsResponse.Trains.First();
            WagonsResponse       wagonsResponse = _uzClient.GetWagonsAsync(context, KievStationId, LvovStatinId, train.StationFrom.DateTime, train.TrainNumber, train.TrainType, train.Wagons.First().TypeCode).Result;

            WagonsResponse.Wagon wagon = wagonsResponse.Wagons.First();

            PlacesResponse placesResponse = GetPlaces(context,
                                                      KievStationId,
                                                      LvovStatinId,
                                                      train.StationFrom.DateTime,
                                                      train.TrainNumber,
                                                      wagon.Number,
                                                      wagon.CoachClass,
                                                      wagon.CoachType);

            Assert.IsFalse(string.IsNullOrWhiteSpace(placesResponse.PlaceType));
            CollectionAssert.IsNotEmpty(placesResponse.Places);
        }
Example #21
0
        private async Task <ICollection <ResponseWagonWithPlaceInfo> > FilterWagonsByAvailablePlaces(UzContext uzContext, TrainsResponse.Train train, WagonsResponse wagonsResponse, Request request)
        {
            var wagons = new List <ResponseWagonWithPlaceInfo>();

            foreach (WagonsResponse.Wagon wagon in wagonsResponse.Wagons)
            {
                PlacesResponse placesResponse = await GetPlacesAsync(uzContext, train, wagon, request);

                bool isPlaceAvailable = IsPlaceAvailable(placesResponse, request);
                if (isPlaceAvailable)
                {
                    ResponseWagonWithPlaceInfo wagonWithPlaceInfo = ExtendResponseWagonWithPlaceInfo(wagon, placesResponse, request);
                    wagons.Add(wagonWithPlaceInfo);
                }
            }
            return(wagons);
        }
Example #22
0
 private async Task <PlacesResponse> GetPlacesAsync(UzContext uzContext, TrainsResponse.Train train, WagonsResponse.Wagon wagon, Request request)
 {
     return(await _uzClient.GetPlacesAsync(uzContext, train.StationFrom.Id, train.StationTo.Id,
                                           train.StationFrom.DateTime,
                                           train.TrainNumber, wagon.Number, wagon.CoachClass, wagon.CoachType));
 }
Example #23
0
 private BookPlacesResponse BookPlace(UzContext context, int stationFromId, int stationToId, DateTime dateTime, string trainNumber, int wagonNumber, string coachClass, string wagonTypeCode, int place, string placeType, string firstName, string lastName)
 {
     return(_uzClient.BookPlaceAsync(context, stationFromId, stationToId, dateTime, trainNumber, wagonNumber, coachClass, wagonTypeCode, place, placeType, firstName, lastName).Result);
 }
Example #24
0
 private PlacesResponse GetPlaces(UzContext context, int stationIdFrom, int stationIdTo, DateTime dateTime, string trainNumber, int wagonNumber, string coachClass, int coachType)
 {
     return(_uzClient.GetPlacesAsync(context, stationIdFrom, stationIdTo, dateTime, trainNumber, wagonNumber, coachClass, coachType).Result);
 }
Example #25
0
 private WagonsResponse GetWagons(UzContext context, int stationIdFrom, int stationIdTo, DateTime dateTime, string trainNumber, int trainType, string wagonType)
 {
     return(_uzClient.GetWagonsAsync(context, stationIdFrom, stationIdTo, dateTime, trainNumber, trainType, wagonType).Result);
 }
Example #26
0
 private TrainsResponse GetTrains(UzContext context, int stationIdFrom, int stationIdTo, DateTime dateTime)
 {
     return(_uzClient.GetTrainsAsync(context, stationIdFrom, stationIdTo, dateTime).Result);
 }