public void Copy_Schedule()
        {
            // Arange
            int scheduleId = 1;
            string scheduleName = "ScheduleName1";

            int pierId = 1;
            string pierName = "PierName1";
            Pier pier = new Pier { Id = pierId, Name = pierName };

            int routeId = 1;
            string routeCode = "RouteCode1";
            string routeName = "RouteName1";
            Route route = new Route { Id = routeId, Code = routeCode, Name = routeName };

            TimeSpan time = new TimeSpan(0, 10, 0);

            Schedule oldSchedule = new Schedule { Id = scheduleId, Name = scheduleName, Pier = pier, Route = route, Time = time };

            // Act
            Schedule newSchedule = oldSchedule.Copy();

            // Assert
            Assert.AreNotSame(newSchedule, oldSchedule); // Not the same object

            // Check properties
            Assert.AreEqual(newSchedule.Id, scheduleId);
            Assert.AreEqual(newSchedule.Name, scheduleName);
            Assert.AreEqual(newSchedule.Route, route);
            Assert.AreEqual(newSchedule.Pier, pier);
            Assert.AreEqual(newSchedule.Time, time);

        }
        public void Delete_Schedule_By_Schedule()
        {
            Pier pier = new Pier { Name = "Pier Test" };
            Route route = new Route { Name = "Route Test", TimeTable = new TimeTable { Name = "TimeTable" } };

            // Add to database
            using (ThamesClipperContext ctx = new ThamesClipperContext())
            {
                ScheduleRepository scheduleRepository = new ScheduleRepository(ctx);
                scheduleRepository.Add(new Schedule { Pier = pier, Route = route, Time = new TimeSpan(1, 0, 0) });
                scheduleRepository.SaveChanges();
            }

            // Find and delete it
            using (ThamesClipperContext ctx = new ThamesClipperContext())
            {
                List<Schedule> schedules = ctx.Schedules.Where(schedule => schedule.Pier.Id == pier.Id && schedule.Route.Id == route.Id).ToList();
                Assert.AreEqual(1, schedules.Count());

                ScheduleRepository scheduleRepository = new ScheduleRepository(ctx);
                scheduleRepository.Delete(schedules[0]);
                scheduleRepository.SaveChanges();
            }

            // Check that nothing is there
            using (ThamesClipperContext ctx = new ThamesClipperContext())
            {
                List<Schedule> schedules = ctx.Schedules.Where(schedule => schedule.Pier.Id == pier.Id && schedule.Route.Id == route.Id).ToList();
                Assert.AreEqual(0, schedules.Count());
            }
        }
        public void Copy_Route()
        {
            // Arange
            int routeId = 1;
            string routeCode = "RouteCode1";
            string routeName = "RouteName1";
            string newRouteName = "00:10 Pier 1 to Pier 5";
            DateTime startDate = new DateTime(2013, 06, 01);
            DateTime endDate = new DateTime(2013, 07, 01);

            int timeTableId = 1;
            string timeTableCode = "TimeTableCode1";
            string timeTableName = "TimeTableName1";
            TimeTable TimeTable = new TimeTable { Id = timeTableId, Code = timeTableCode, Name = timeTableName };

            List<Schedule> schedules = new List<Schedule>();

            Route oldRoute = new Route() 
                { 
                    Id = routeId ,
                    Code = routeCode,
                    Name = routeName,
                    TimeTableId = timeTableId,
                    TimeTable = TimeTable
                };

            string[] pierNames = { "Pier 1", "Pier 2", "Pier 3", "Pier 4", "Pier 5" };
            int[] pierIds = { 1, 2, 3, 4, 5 };

            for (int i = 0; i < 5; i++)
            {
                schedules.Add(new Schedule
                {
                    Id = i,
                    Name = "Schedule " + i.ToString(),
                    Pier = new Pier { Id = pierIds[i], Name = pierNames[i] },
                    Route = oldRoute,
                    Time = new TimeSpan(i, 0, 0)
                });
            }

            oldRoute.Schedules = schedules;

            TimeSpan increment = new TimeSpan(0, 10, 0);

            // Act
            Route newRoute = oldRoute.Copy(increment);
            var timeTableIsCorrect = oldRoute.Schedules.Zip(newRoute.Schedules, (s1, s2) => s1.Time == (s2.Time - increment));

            // Assert
            Assert.AreNotSame(newRoute, oldRoute); // Not the same object
            Assert.IsTrue(timeTableIsCorrect.All(timeTable => timeTable)); // Increment has been applied

            // Check properties
            Assert.AreEqual(newRoute.Id, routeId);
            Assert.AreEqual(newRoute.Code, routeCode);
            Assert.AreEqual(newRoute.Name, newRouteName);
            Assert.AreEqual(newRoute.TimeTableId, timeTableId);
        }
        public ActionResult Create(Route route)
        {
            if (ModelState.IsValid)
            {
                routeRepository.Add(route);
                routeRepository.SaveChanges();
                return RedirectToAction("Index");
            }

            return View("Create", route);
        }
        public Route Copy(TimeSpan increment, string name = "")
        {
            Route copy = new Route();

            foreach (PropertyInfo property in typeof(Route).GetProperties())
            {
                if (property.PropertyType == typeof(List<Schedule>))
                {
                    var schedules = (List<Schedule>)property.GetValue(this);

                    if (schedules != null)
                    {
                        List<Schedule> newSchedules = new List<Schedule>();

                        foreach (Schedule s in schedules)
                        {
                            Schedule newSchedule = s.Copy();
                            newSchedule.Route = copy;
                            newSchedules.Add(newSchedule);
                        }

                        property.SetValue(copy, newSchedules);
                    }
                }
                else
                {
                    property.SetValue(copy, property.GetValue(this));
                }
            }

            if (copy.Schedules != null)
                copy.Schedules.ForEach(schedule => {
                    schedule.Time += increment;
                    schedule.Time = new TimeSpan(schedule.Time.Value.Hours, schedule.Time.Value.Minutes, 0);
                });

            if (name != "")
                copy.Name = name;
            else
                copy.UpdateName();

            return copy;
        }
        public void Delete_Route_By_Route()
        {
            string routeName = "Test Route to Delete";

            // Add to database
            using (ThamesClipperContext ctx = new ThamesClipperContext())
            {
                RouteRepository routeRepository = new RouteRepository(ctx);
                Route route = new Route { 
                    Name = routeName, 
                    TimeTable = new TimeTable { Name = "TimeTable" }
                };

                route.Schedules = new List<Schedule>() { new Schedule { 
                    Route = route, 
                    Pier = new Pier { Name = "Pier" },
                    Time = new TimeSpan(1, 0, 0)
                    }
                };

                routeRepository.Add(route);
                routeRepository.SaveChanges();
            }

            // Find and delete it
            using (ThamesClipperContext ctx = new ThamesClipperContext())
            {
                List<Route> routes = ctx.Routes.Where(route => route.Name == routeName).ToList();
                Assert.AreEqual(1, routes.Count());

                RouteRepository routeRepository = new RouteRepository(ctx);
                routeRepository.Delete(routes[0]);
                routeRepository.SaveChanges();
            }

            // Check that nothing is there
            using (ThamesClipperContext ctx = new ThamesClipperContext())
            {
                List<Route> routes = ctx.Routes.Where(route => route.Name == routeName).ToList();
                Assert.AreEqual(0, routes.Count());
            }
        }
        public void Add_Or_Update_Schedule()
        {
            Pier pier = new Pier { Name = "Pier" };
            TimeTable timeTable = new TimeTable { Name = "TimeTable" };
            Route routeExisting = new Route { Name = "Route1", TimeTable = timeTable };
            Route routeAdd = new Route { Name = "Route2", TimeTable = timeTable };

            TimeSpan timeExisting = new TimeSpan(13, 0, 0);
            TimeSpan timeAdd = new TimeSpan(14, 0, 0);
            TimeSpan timeUpdated = new TimeSpan(15, 0, 0);

            Schedule existing = new Schedule { Pier = pier, Route = routeExisting, Time = timeExisting };
            Schedule add = new Schedule { Pier = pier, Route = routeAdd, Time = timeAdd };

            // Add the existing one to database
            using (ThamesClipperContext ctx = new ThamesClipperContext())
            {
                ScheduleRepository scheduleRepository = new ScheduleRepository(ctx);
                scheduleRepository.Add(existing);
                scheduleRepository.SaveChanges();
            }

            // Find and update it
            using (ThamesClipperContext ctx = new ThamesClipperContext())
            {
                ScheduleRepository scheduleRepository = new ScheduleRepository(ctx);
                existing.Time = timeUpdated;
                scheduleRepository.AddOrUpate(existing);
                scheduleRepository.AddOrUpate(add);
                scheduleRepository.SaveChanges();
            }

            // Check that the new name is there
            using (ThamesClipperContext ctx = new ThamesClipperContext())
            {
                var schedulesExisting = ctx.Schedules.Where(schedule => schedule.Pier.Id == pier.Id && schedule.Route.Id == routeExisting.Id).ToList();
                Assert.AreEqual(1, schedulesExisting.Count());
                Assert.AreEqual(timeUpdated, schedulesExisting[0].Time);

                var schedulesAdd = ctx.Schedules.Where(schedule => schedule.Pier.Id == pier.Id && schedule.Route.Id == routeAdd.Id).ToList();
                Assert.AreEqual(1, schedulesAdd.Count());
            }
        }
 public ActionResult Edit(Route route)
 {
     if (ModelState.IsValid)
     {
         routeRepository.Update(route);
         routeRepository.SaveChanges();
         return RedirectToAction("Index");
     }
     return View("Edit", route);
 }
        public void Edit_Does_Not_Calls_Save_When_InValid()
        {
            // Arange
            Route validRouteToEdit = new Route { Id = 1, Name = "Route 1" };

            RouteController pierController = unityContainer.Resolve<RouteController>();
            pierController.ModelState.AddModelError("Test Model Error", "An Error to test an invalid Model");

            // Act
            var result = pierController.Edit(validRouteToEdit) as ViewResult;

            // Asert
            mockRouteRepository.Verify(x => x.Update(validRouteToEdit), Times.Never());
            mockRouteRepository.Verify(x => x.SaveChanges(), Times.Never());
        }
        public void Edit_Calls_Edit_When_Valid()
        {
            // Arange
            Route validRouteToEdit = new Route { Id = 1, Name = "Route 1" };
            
            RouteController pierController = unityContainer.Resolve<RouteController>();

            // Act
            var result = pierController.Edit(validRouteToEdit) as RedirectToRouteResult;

            // Asert
            mockRouteRepository.Verify(x => x.Update(validRouteToEdit), Times.Once());
            mockRouteRepository.Verify(x => x.SaveChanges());
        }
        public void Create_Calls_Save_When_Valid()
        {
            // Arange
            Route validRouteToAdd = new Route();

            RouteController pierController = unityContainer.Resolve<RouteController>();

            // Act
            var result = pierController.Create(validRouteToAdd) as RedirectToRouteResult;

            // Asert
            mockRouteRepository.Verify(x => x.Add(validRouteToAdd), Times.Once());
            mockRouteRepository.Verify(x => x.SaveChanges());
        }
        public void Create_Returns_To_Create_When_InValid()
        {
            // Arange
            Route validRouteToAdd = new Route();

            string expectedViewName = "Create";
            RouteController pierController = unityContainer.Resolve<RouteController>();
            pierController.ModelState.AddModelError("Test Model Error", "An Error to test an invalid Model");

            // Act
            var result = pierController.Create(validRouteToAdd) as ViewResult;

            // Asert
            Assert.IsNotNull(result, "Expected ViewResult to be returned");
            Assert.AreEqual(expectedViewName, result.ViewName);
        }
        public void Create_Returns_To_Index_When_Valid()
        {
            // Arange
            Route validRouteToAdd = new Route();
            string expectedAction = "Index";

            RouteController pierController = unityContainer.Resolve<RouteController>();

            // Act
            var result = pierController.Create(validRouteToAdd) as RedirectToRouteResult;

            // Asert
            Assert.IsNotNull(result, "Expected RedirectToRouteResult to be returned");
            Assert.AreEqual(expectedAction, result.RouteValues["Action"]);
        }
        public void Update_Route_Name_For_Empty()
        {
            // Arange
            List<Schedule> schedules = new List<Schedule>();
            string oldName = "RouteName";

            Route route = new Route { Name = oldName, Schedules = schedules };

            // Act
            route.UpdateName();

            // Assert
            Assert.AreEqual(route.Name, "Empty");
        }
        public void Copy_Route_Over_24h()
        {
            // Arange
            TimeSpan currentTimeSpan = new TimeSpan(23, 50, 00);
            TimeSpan newTimeSpan = new TimeSpan(00, 10, 00);
            TimeSpan increment = new TimeSpan(0, 20, 0);
            Route route = new Route { Name = "Route 1", Schedules = new List<Schedule> { new Schedule { Time = currentTimeSpan, Pier = new Pier { Name = "Pier" } } } };

            // Act
            Route newRoute = route.Copy(increment);

            // Assert
            Assert.AreEqual(newTimeSpan, newRoute.Schedules[0].Time.Value);
        }
        public void Update_Route_Name()
        {
            // Arange
            List<Schedule> schedules = new List<Schedule>();
            int numberOfSchedules = 10;
            string oldName = "RouteName";

            for (int i = 0; i < numberOfSchedules; i++)
            {
                Pier pier = new Pier { Id = i, Name = "Pier " + i };
                schedules.Add(new Schedule { Time = new TimeSpan(i, 0, 0), Pier = pier });
            }

            Route route = new Route { Name = oldName, Schedules = schedules };

            // Act
            route.UpdateName();

            // Assert
            Assert.AreEqual(route.Name, "00:00 Pier 0 to Pier 9");
        }