public async Task Stations_Get_ReturnsOk_AndStations()
        {
            // arrange
            var fixture = new Fixture();

            var loggerFactory = A.Fake <ILoggerFactory>();
            var logger        = A.Fake <ILogger>();

            A.CallTo(() => loggerFactory.For <StationsController>())
            .Returns(logger);

            Logger.LoggerFactory = loggerFactory;

            var service = A.Fake <IStationService>();
            var query   = fixture.Create <string>();

            var stations = fixture.Create <(List <Station> stations, List <char> nextCharacters)>();

            A.CallTo(() => service.Search(query))
            .Returns(stations);

            // act
            var sut = new StationsController(service);

            var result = await sut.Get(query)
                         .ConfigureAwait(true);

            // assert
            result.Should().BeOfType <OkObjectResult>()
            .Which.Value.Should().BeOfType <StationsResponse>()
            .Which.Should().BeEquivalentTo(new StationsResponse {
                Stations = stations.stations, NextCharacters = stations.nextCharacters
            });
        }
        public async Task GET_Returns_Ordered_List()
        {
            var controller = new StationsController(Context);

            base.ConfigureRequest(controller);

            Context.Stations.Add(new Station
            {
                Id       = Guid.NewGuid(),
                Name     = "My Custom Station",
                RegionId = DefaultRegionId
            });

            Context.Stations.Add(new Station
            {
                Id       = Guid.NewGuid(),
                Name     = "A different name",
                RegionId = DefaultRegionId
            });
            Context.SaveChanges();

            var orderedList = await GetData <List <StationModel> >(controller.Get(true));

            Assert.AreEqual(2, orderedList.Count());
            Assert.AreNotEqual(Guid.Empty, orderedList.First().Id);
            Assert.AreNotEqual(Guid.Empty, orderedList.Last().Id);
            Assert.AreEqual("A different name", orderedList.First().Name);
            Assert.AreEqual("My Custom Station", orderedList.Last().Name);
        }
        public async Task Stations_Get_ReturnsUnprocessableEntity_AndLogs_WhenServiceThrows()
        {
            // arrange
            var fixture = new Fixture();

            var loggerFactory = A.Fake <ILoggerFactory>();
            var logger        = A.Fake <ILogger>();

            A.CallTo(() => loggerFactory.For <StationsController>())
            .Returns(logger);

            Logger.LoggerFactory = loggerFactory;

            var service = A.Fake <IStationService>();
            var query   = fixture.Create <string>();
            var error   = fixture.Create <Exception>();

            A.CallTo(() => service.Search(query))
            .Throws(error);

            // act
            var sut = new StationsController(service);

            var result = await sut.Get(query)
                         .ConfigureAwait(true);

            // assert
            result.Should().BeOfType <UnprocessableEntityObjectResult>()
            .Which.Value.Should().BeOfType <string>()
            .Which.Should().BeEquivalentTo(error.Message);

            A.CallTo(() => logger.Error(error))
            .MustHaveHappened();
        }
        public async Task GET_Is_OK()
        {
            var controller = new StationsController(Context);

            ConfigureRequest(controller);

            var result = await GetResponse(controller.Get(true));

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
        }
        public async Task GET_Returns_List()
        {
            var controller = new StationsController(Context);

            base.ConfigureRequest(controller);

            // Act
            var result = await GetData <List <StationModel> >(controller.Get(true));

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count);
        }
        public async Task Stations_Get_Specific()
        {
            // Arrange
            var controller = new StationsController(new StationService());

            // Act
            var result = await controller.Get(16);

            // Assert
            var okResult = result.Should().BeOfType <OkObjectResult>().Subject;
            var station  = okResult.Value.Should().BeAssignableTo <Station>().Subject;

            station.Id.Should().Be(16);
        }
        public async Task Stations_Get_All()
        {
            // Arrange
            var controller = new StationsController(new StationService());

            // Act
            var result = await controller.Get();

            // Assert
            var okResult = result.Should().BeOfType <OkObjectResult>().Subject;
            var stations = okResult.Value.Should().BeAssignableTo <IEnumerable <Station> >().Subject;

            stations.Count().Should().Be(50);
        }
        public async Task GET_Returns_Ordered_List_Excluding_Deleted()
        {
            var controller = new StationsController(Context);

            base.ConfigureRequest(controller);

            var region = Context.Regions.First();

            Context.Stations.Add(new Station
            {
                Id       = Guid.NewGuid(),
                Name     = "My Custom Station",
                RegionId = region.Id
            });

            Context.Stations.Add(new Station
            {
                Id       = Guid.NewGuid(),
                Name     = "A different name",
                RegionId = region.Id
            });

            Context.Stations.Add(new Station
            {
                Id                = Guid.NewGuid(),
                Name              = "A hidden name",
                RegionId          = region.Id,
                IsMarkedAsDeleted = true
            });
            Context.SaveChanges();

            var orderedList = await GetData <List <StationModel> >(controller.Get(false));

            Assert.AreEqual(2, orderedList.Count());
            Assert.AreNotEqual(Guid.Empty, orderedList.First().Id);
            Assert.AreNotEqual(Guid.Empty, orderedList.Last().Id);
            Assert.AreEqual("A different name", orderedList.First().Name);
            Assert.AreEqual("My Custom Station", orderedList.Last().Name);
        }