public async Task Stations_Change()
        {
            // Arrange
            var service    = new StationService();
            var controller = new StationsController(service);
            var nowObject  = new Station
            {
                CallSign = "2DayFM",
                Code     = "2DayFM",
                City     = "Sydney",
                State    = "NSW"
            };

            // Act
            var result = await controller.Put(20, nowObject);

            // Assert
            var okResult = result.Should().BeOfType <NoContentResult>().Subject;

            var station = service.Get(20);

            station.Id.Should().Be(20);
            station.CallSign.Should().Be("2DayFM");
            station.Code.Should().Be("2DayFM");
            station.City.Should().Be("Sydney");
            station.State.Should().Be("NSW");
        }
Exemple #2
0
        public void Can_Send_Pagination_View_Model()
        {
            Mock <IStationRepository> mock = new Mock <IStationRepository>();

            mock.Setup(m => m.Stations).Returns(new Station[] {
                new Station {
                    Id = "1", Name = "S1"
                },
                new Station {
                    Id = "2", Name = "S2"
                },
                new Station {
                    Id = "3", Name = "S3"
                },
                new Station {
                    Id = "4", Name = "S4"
                },
                new Station {
                    Id = "5", Name = "S5"
                }
            }.AsQueryable());
            StationsController stationController = new StationsController(mock.Object);

            stationController.PageSize = 3;

            StationListViewModel result     = (StationListViewModel)stationController.Index(2).Model;
            PagingInfo           pagingInfo = result.PagingInfo;

            Assert.AreEqual(pagingInfo.CurrentPage, 2);
            Assert.AreEqual(pagingInfo.ItemsPerPage, 3);
            Assert.AreEqual(pagingInfo.TotalItems, 5);
            Assert.AreEqual(pagingInfo.TotalPages, 2);
        }
Exemple #3
0
        public void Index()
        {
            var controller  = new StationsController(new StationBLL(new StationRepositoryStab()));
            var sessionMock = new TestControllerBuilder();

            sessionMock.InitializeController(controller);
            controller.Session["AuthenticatedUser"] = new DbUser
            {
                Username = "******",
                Password = null,
                Salt     = null
            };
            var stations = new List <Station>();
            var oslos    = new Station {
                StationId = 0, Name = "Oslo S", LineStations = null
            };

            stations.Add(oslos);
            stations.Add(oslos);
            stations.Add(oslos);

            var actionResult = (ViewResult)controller.Index();
            var result       = (List <Station>)actionResult.Model;

            Assert.AreEqual(actionResult.ViewName, "");

            for (var i = 0; i < result.Count; i++)
            {
                Assert.AreEqual(stations[i].StationId, result[i].StationId);
                Assert.AreEqual(stations[i].Name, result[i].Name);
                Assert.AreEqual(stations[i].LineStations, result[i].LineStations);
            }
        }
Exemple #4
0
        public void Can_Get_Stations_By_Route()
        {
            //Setup
            Mock <IStationRepository> mock = new Mock <IStationRepository>();

            mock.Setup(m => m.Stations).Returns(new Station[] {
                new Station {
                    Id = "1", Name = "S1", Route = "Troncal"
                },
                new Station {
                    Id = "2", Name = "S2", Route = "Troncal"
                },
                new Station {
                    Id = "3", Name = "S3", Route = "Troncal"
                },
                new Station {
                    Id = "4", Name = "S4", Route = "Different"
                },
                new Station {
                    Id = "5", Name = "S5", Route = "Different"
                }
            }.AsQueryable());
            StationsController stationController = new StationsController(mock.Object);

            stationController.PageSize = 5;

            //Act
            StationListViewModel result = (StationListViewModel)stationController.ByRoute("Troncal").Model;

            //Assert
            Station[] stationArray = result.Stations.ToArray();
            Assert.IsTrue(stationArray.Length == 3);
            Assert.AreEqual(stationArray[0].Name, "S1");
            Assert.AreEqual(stationArray[1].Name, "S2");
        }
        public async Task PUT_Updates_Data()
        {
            var controller = new StationsController(Context);

            ConfigureRequest(controller);

            var model = new StationModel
            {
                Id       = Guid.Parse("{69EA67A4-C575-472B-B463-C156E5BA61F3}"),
                Name     = "Test No Id",
                RegionId = DefaultRegionId
            };

            //setup database record
            Context.Stations.Add(new Station
            {
                Id       = model.Id,
                Name     = model.Name,
                RegionId = DefaultRegionId
            });
            Context.SaveChanges();

            model.Name = "My New Name";

            var result = await GetData <StationModel>(controller.Put(model));

            Assert.AreEqual(model.Name, result.Name);
        }
        public void TestGetIndex()
        {
            // Arrange
            var SessionMock = new TestControllerBuilder();

            var controller = new StationsController(new StationLogic(new StationRepositoryStub()));

            SessionMock.InitializeController(controller);
            controller.Session["LoggedIn"] = true;
            var StationList = new List <Station>
            {
                new Station {
                    StationID = 1, StationName = "Oslo S"
                },
                new Station {
                    StationID = 2, StationName = "Nationaltheatret"
                },
                new Station {
                    StationID = 3, StationName = "Lysaker"
                }
            };

            // ACt
            var result     = (ViewResult)controller.Index();
            var resultList = (List <Station>)result.Model;

            // Assert
            Assert.AreEqual("", result.ViewName);
            for (int i = 0; i < resultList.Count(); i++)
            {
                Assert.AreEqual(StationList[i].StationID, resultList[i].StationID);
                Assert.AreEqual(StationList[i].StationName, resultList[i].StationName);
            }
        }
        public async Task POST_Inserts_Different_Region_Records()
        {
            var controller = new StationsController(Context);

            ConfigureRequest(controller);

            var newRegionId = Guid.NewGuid();

            var model = new StationModel
            {
                Name     = "Test No Id",
                RegionId = newRegionId
            };

            Context.Regions.Add(new Region
            {
                Id   = newRegionId,
                Name = "My New Region"
            });

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

            var result = await GetResponse(controller.Post(model));

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
        }
        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_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_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
            });
        }
Exemple #11
0
 public override void _Ready()
 {
     if (instance == null)
     {
         instance = this;
     }
     SetProcess(false);
 }
        public async Task PUT_Is_Bad_Request_Null_Data()
        {
            var controller = new StationsController(Context);

            ConfigureRequest(controller);

            var result = await GetResponse(controller.Put(null));

            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }
        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 void TestGetAdd()
        {
            // Arrange
            var controller = new StationsController(new StationLogic(new StationRepositoryStub()));

            // ACt
            var result = (ViewResult)controller.Add();

            // Assert
            Assert.AreEqual("", result.ViewName);
        }
        public void TestPostDeleteStationNotExsist()
        {
            // Arrange
            var controller = new StationsController(new StationLogic(new StationRepositoryStub()));

            // ACt
            var result = controller.Delete(100) as HttpNotFoundResult;

            // Assert
            Assert.AreEqual(404, result.StatusCode);
        }
Exemple #16
0
        private StationsController createStationsControllerFromMockRepository()
        {
            var stations = createStations();
            var mockStationRepository = new Mock <IStationRepository>();

            mockStationRepository.Setup(repository => repository.GetAllInOrder()).Returns(stations);
            var stationService     = new StationService(mockStationRepository.Object);
            var stationsController = new StationsController(stationService);

            return(stationsController);
        }
        public void TestPostDeleteConfirmed()
        {
            // Arrange
            var controller = new StationsController(new StationLogic(new StationRepositoryStub()));

            // ACt
            var result = (RedirectToRouteResult)controller.DeleteConfirmed(1);

            // Assert
            Assert.AreEqual("", result.RouteName);
            Assert.AreEqual("Index", result.RouteValues.Values.First());
        }
        public void Test_DeleteStation_ReturnsNotFoundResult()
        {
            // Arrange
            var mockRepo   = new Mock <IMongoDataRepository <Station> >();
            var mockHub    = new Mock <IHubContext <LiveStationHub> >();
            var controller = new StationsController(mockRepo.Object, mockHub.Object, Mapper);

            // Act
            var notFoundResult = controller.DeleteStation(ObjectId.GenerateNewId().ToString());

            // Assert
            Assert.IsType <NotFoundResult>(notFoundResult.Result);
        }
        public void Test_GetAllStations_ReturnsOkResult()
        {
            // Arrange
            var mockRepo   = new Mock <IMongoDataRepository <Station> >();
            var mockHub    = new Mock <IHubContext <LiveStationHub> >();
            var controller = new StationsController(mockRepo.Object, mockHub.Object, Mapper);

            // Act
            var okResult = controller.GetAllStations();

            // Assert
            Assert.IsType <OkObjectResult>(okResult.Result);
        }
        public void Test_CreateStation_ReturnsNotFoundResult()
        {
            // Arrange
            var mockRepo   = new Mock <IMongoDataRepository <Station> >();
            var mockHub    = new Mock <IHubContext <LiveStationHub> >();
            var controller = new StationsController(mockRepo.Object, mockHub.Object, Mapper);

            // Act
            var notFoundResult = controller.CreateStation((StationCreateDto)null);

            // Assert
            Assert.IsType <NotFoundResult>(notFoundResult.Result);
        }
        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 PUT_Is_Bad_Request_Missing_Id()
        {
            var controller = new StationsController(Context);

            ConfigureRequest(controller);

            var model = new StationModel {
                Name = "Test No Id"
            };

            var result = await GetResponse(controller.Put(model));

            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }
        public void TestPostAddDBError()
        {
            // Arrange
            var controller = new StationsController(new StationLogic(new StationRepositoryStub()));
            var NewStation = new Station {
                StationID = 0, StationName = ""
            };

            // ACt
            var result = (ViewResult)controller.Add(NewStation);

            // Assert
            Assert.AreEqual("", result.ViewName);
        }
        public void TestPostEditValidationError()
        {
            // Arrange
            var controller = new StationsController(new StationLogic(new StationRepositoryStub()));

            controller.ViewData.ModelState.AddModelError("StationName", "Station name should string!");
            var OneStation = new Station();

            // ACt
            var result = (ViewResult)controller.Edit(OneStation);

            // Assert
            Assert.AreEqual("", result.ViewName);
        }
        public void TestPostEditDBError()
        {
            // Arrange
            var controller = new StationsController(new StationLogic(new StationRepositoryStub()));
            var OneStation = new Station {
                StationID = 1, StationName = null
            };

            // ACt
            var result = (ViewResult)controller.Edit(OneStation);

            // Assert
            Assert.AreEqual("", result.ViewName);
        }
        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 void TestPostEdit()
        {
            // Arrange
            var controller = new StationsController(new StationLogic(new StationRepositoryStub()));
            var OneStation = new Station {
                StationID = 1, StationName = "Bergen"
            };

            // ACt
            var result = (RedirectToRouteResult)controller.Edit(OneStation);

            // Assert
            Assert.AreEqual("", result.RouteName);
            Assert.AreEqual("Index", result.RouteValues.Values.First());
        }
        public void TestPostAdd()
        {
            // Arrange
            var controller = new StationsController(new StationLogic(new StationRepositoryStub()));
            var NewStation = new Station {
                StationID = 4, StationName = "Sandvika"
            };

            // ACt
            var result = (RedirectToRouteResult)controller.Add(NewStation);

            // Assert
            Assert.AreEqual("", result.RouteName);
            Assert.AreEqual("Index", result.RouteValues.Values.First());
        }
        public async Task POST_Is_OK()
        {
            var controller = new StationsController(Context);

            ConfigureRequest(controller);

            var model = new StationModel
            {
                Name     = "Test No Id",
                RegionId = DefaultRegionId
            };

            var result = await GetResponse(controller.Post(model));

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
        }