public async Task ArrivalsNowReturnsServicesWithStops(bool includeStops, Type expected)
        {
            var data       = CreateStubDataWithFindArrivals();
            var controller = new ArrivalsController(data, FilterFactory, _config.CreateMapper(), Substitute.For <ILogger>());
            var response   = await controller.Arrivals("CLJ", includeStops : includeStops) as ObjectResult;

            Assert.IsType(expected, response.Value);
        }
        public async Task ArrivalsReturnsNotFoundWithReason(FindStatus status, string expectedReason)
        {
            var data       = CreateStubDataWithFindArrivals(returnedStatus: status, returnedStops:  new ResolvedServiceStop[0]);
            var controller = new ArrivalsController(data, FilterFactory, _config.CreateMapper(), Substitute.For <ILogger>());
            var response   = await controller.Arrivals("CLJ", Aug12AtTenFifteen) as ObjectResult;

            Assert.Equal(404, response.StatusCode);

            var notFound = response.Value as Model.NotFoundResponse;

            Assert.Equal(expectedReason, notFound.Reason);
            AssertRequestSetInResponse(notFound);
        }
        public async Task ArrivalsReturnsServices(string clapham)
        {
            var data       = CreateStubDataWithFindArrivals("CLJ", Aug12AtTenFifteen);
            var controller = new ArrivalsController(data, FilterFactory, _config.CreateMapper(), Substitute.For <ILogger>());
            var response   = await controller.Arrivals(clapham, Aug12AtTenFifteen) as ObjectResult;;

            Assert.Equal(200, response.StatusCode);

            var services = response.Value as Model.FoundSummaryResponse;

            AssertRequestSetInResponse(services);
            Assert.NotEmpty(services.Services);
        }
        public async Task FullRailDayArrivalsReturnsServices()
        {
            var data       = CreateStubDataWithAllArrivals("CLJ", Aug12, boundary: Time.StartRailDay);
            var controller = new ArrivalsController(data, FilterFactory, _config.CreateMapper(), Substitute.For <ILogger>());
            var response   = await controller.Arrivals("CLJ", Aug12, fullDay : true, dayBoundary : "02:30") as ObjectResult;;

            Assert.Equal(200, response.StatusCode);

            var services = response.Value as Model.FoundSummaryResponse;

            AssertRequestSetInResponse(services);
            Assert.NotEmpty(services.Services);
        }
        public async Task ArrivalsNowSetsReturnCancelledFlag()
        {
            var returnedStop = CreateClaphamResolvedStop(true);
            var data         = CreateStubDataWithFindArrivals(returnedStops:  new [] { returnedStop });
            var controller   = new ArrivalsController(data, FilterFactory, _config.CreateMapper(), Substitute.For <ILogger>());
            var response     = await controller.Arrivals("CLJ", returnCancelledServices : true) as ObjectResult;

            Assert.Equal(200, response.StatusCode);
            var services = response.Value as Model.FoundSummaryResponse;

            Assert.Single(services.Services);
            Assert.True(services.Services[0].Service.IsCancelled);
        }
        public async Task ArrivalsNowSetsTocFilter()
        {
            var data          = CreateStubDataWithFindArrivals();
            var filter        = Substitute.For <GatherConfiguration.GatherFilter>();
            var filterFactory = Substitute.For <IFilterFactory>();

            filterFactory.NoFilter.Returns(GatherFilterFactory.NoFilter);
            filterFactory.ProvidedByToc(Arg.Any <TocFilter>(), GatherFilterFactory.NoFilter).Returns(filter);

            var controller = new ArrivalsController(data, filterFactory, _config.CreateMapper(), Substitute.For <ILogger>());
            var response   = await controller.Arrivals("CLJ", toc : new [] { "VT" }) as ObjectResult;

            filterFactory.Received().ProvidedByToc(Arg.Any <TocFilter>(), GatherFilterFactory.NoFilter);
        }
        public async Task ArrivalsReturnsError()
        {
            var data = Substitute.For <ILocationData>();

            data.FindArrivals(Arg.Any <string>(), Arg.Any <DateTime>(), Arg.Any <GatherConfiguration>())
            .Throws(new Exception("Something went wrong"));

            var controller = new ArrivalsController(data, FilterFactory, _config.CreateMapper(), Substitute.For <ILogger>());
            var response   = await controller.Arrivals("CLJ", Aug12AtTenFifteen) as ObjectResult;

            Assert.Equal(500, response.StatusCode);

            var notFound = response.Value as Model.NotFoundResponse;

            Assert.Equal("Error while finding services for CLJ@2019-08-12T10:15:00", notFound.Reason);
            AssertRequestSetInResponse(notFound);
        }
        public async Task ArrivalsReturns400WithInvalidTocs()
        {
            var data          = CreateStubDataWithFindArrivals();
            var filter        = Substitute.For <GatherConfiguration.GatherFilter>();
            var filterFactory = Substitute.For <IFilterFactory>();

            filterFactory.NoFilter.Returns(GatherFilterFactory.NoFilter);
            filterFactory.ProvidedByToc(Arg.Any <TocFilter>(), GatherFilterFactory.NoFilter).Returns(filter);

            var controller = new ArrivalsController(data, filterFactory, _config.CreateMapper(), Substitute.For <ILogger>());
            var response   = await controller.Arrivals("CLJ", toc : new [] { "VT", "SWR" }) as ObjectResult;

            Assert.Equal(400, response.StatusCode);
            var error = response.Value as BadRequestResponse;

            Assert.Equal("Invalid tocs provided in request VT|SWR", error.Reason);
        }
        public async Task SetsFromFilter(string location, bool hasFilter)
        {
            var data = CreateStubDataWithFindArrivals();

            data.TryGetStation("SUR", out Arg.Any <Station>()).Returns(true);

            var filterFactory = Substitute.For <IFilterFactory>();

            filterFactory.NoFilter.Returns(GatherFilterFactory.NoFilter);
            filterFactory.ArrivalsComeFrom(Arg.Any <Station>()).Returns(GatherFilterFactory.NoFilter);

            var controller = new ArrivalsController(data, filterFactory, _config.CreateMapper(), Substitute.For <ILogger>());
            var response   = await controller.Arrivals("CLJ", Aug12AtTenFifteen, location) as ObjectResult;;

            if (hasFilter)
            {
                filterFactory.Received().ArrivalsComeFrom(Arg.Any <Station>());
            }
            else
            {
                filterFactory.DidNotReceive().ArrivalsComeFrom(Arg.Any <Station>());
            }
        }