public void Same_Start_And_End_Input_Should_Return_Exception()
        {
            // Arrange
            var routes = new List <Path>
            {
                new Path {
                    Id = 1, PointOne = this.pointA, PointTwo = this.pointB, Distance = 1
                },
                new Path {
                    Id = 2, PointOne = this.pointB, PointTwo = this.pointC, Distance = 4
                },
                new Path {
                    Id = 3, PointOne = this.pointC, PointTwo = this.pointD, Distance = 3
                },
                new Path {
                    Id = 4, PointOne = this.pointA, PointTwo = this.pointD, Distance = 20
                }
            };
            var routeFinder = new RouteFinder(new RouteExplorer(), new ShortestRouteFinder(), new DatabaseReader(), new RouteValidator());

            var startPoint = this.pointA;
            var endPoint   = this.pointA;

            // Act
            var result = routeFinder.CalculateShortestRoute(startPoint, endPoint);

            // Assert
            Assert.Throws <InputValidationException>(() => result);
        }
        public void RouteFinder_Should_Call_RouteExplorer()
        {
            // Arrange
            var startPoint = this.pointA;
            var endPoint   = this.pointD;

            var routes = new List <Path>
            {
                new Path {
                    Id = 1, PointOne = this.pointA, PointTwo = this.pointB, Distance = 1
                },
                new Path {
                    Id = 2, PointOne = this.pointB, PointTwo = this.pointC, Distance = 4
                },
                new Path {
                    Id = 3, PointOne = this.pointC, PointTwo = this.pointD, Distance = 3
                },
                new Path {
                    Id = 4, PointOne = this.pointA, PointTwo = this.pointD, Distance = 20
                }
            };

            var expected = new List <Route>
            {
                new Route()
                {
                    Paths = new List <Path>()
                }
            };


            var mockRouteExplorer = new Mock <IRouteExplorer>();

            mockRouteExplorer
            .Setup(x => x.GetAllPossibleRoutes(It.IsAny <List <Path> >(), It.IsAny <Point>(), It.IsAny <Point>()))
            .Returns(expected);

            var mockShortestRouteFinder = new Mock <IShortestRouteFinder>();

            var mockShortestRouteFinderResult = new Route();

            var mockRouteValidator = new Mock <IRouteValidator>();

            mockShortestRouteFinder
            .Setup(x => x.GetShortestRoute(expected))
            .Returns(mockShortestRouteFinderResult);

            var routeFinder = new RouteFinder(mockRouteExplorer.Object, mockShortestRouteFinder.Object, new DatabaseReader(), mockRouteValidator.Object);

            // Act
            var result = routeFinder.CalculateShortestRoute(startPoint, endPoint);

            // Assert
            mockRouteExplorer.Verify(x => x.GetAllPossibleRoutes(routes, startPoint, endPoint), Times.Once);
            mockShortestRouteFinder.Verify(x => x.GetShortestRoute(expected), Times.Once);
            mockShortestRouteFinderResult.Should().Be(result);
        }
Example #3
0
        static void Main(string[] args)
        {
            var paths = new List <Path>
            {
                new Path {
                    Id = 1, PointOne = Points.A, PointTwo = Points.D, Distance = 20
                },
                new Path {
                    Id = 2, PointOne = Points.D, PointTwo = Points.A, Distance = 14
                },
                new Path {
                    Id = 3, PointOne = Points.A, PointTwo = Points.C, Distance = 15
                },
                new Path {
                    Id = 4, PointOne = Points.A, PointTwo = Points.B, Distance = 5
                },
                new Path {
                    Id = 5, PointOne = Points.B, PointTwo = Points.C, Distance = 5
                }
            };

            var userStartPoint = args[0].ToUpper();
            var userEndPoint   = args[1].ToUpper();

            var startPointEnum = (Core.Points)Enum.Parse(typeof(Core.Points), userStartPoint);
            var endPointEnum   = (Core.Points)Enum.Parse(typeof(Core.Points), userEndPoint);

            var routeFinder = new RouteFinder(
                new RouteExplorer(),
                new ShortestRouteFinder(),
                paths,
                new RouteValidator());

            var shortestRouteList = routeFinder.CalculateShortestRoute(startPointEnum, endPointEnum);

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"The shortest route for {userStartPoint} to {userEndPoint} is: ");

            foreach (var path in shortestRouteList.Paths)
            {
                Console.WriteLine($"Path {path.Id}: {path.PointOne} to {path.PointTwo} - Distance {path.Distance}");
            }
        }
        public void Given_Two_Possible_Routes_RouteFinder_Should_Find_The_Shortest_One()
        {
            // Arrange
            var routes = new List <Path>
            {
                new Path {
                    Id = 1, PointOne = this.pointA, PointTwo = this.pointB, Distance = 1
                },
                new Path {
                    Id = 2, PointOne = this.pointB, PointTwo = this.pointC, Distance = 4
                },
                new Path {
                    Id = 3, PointOne = this.pointC, PointTwo = this.pointD, Distance = 3
                },
                new Path {
                    Id = 4, PointOne = this.pointA, PointTwo = this.pointD, Distance = 20
                }
            };
            var routeFinder = new RouteFinder(new RouteExplorer(), new ShortestRouteFinder(), new DatabaseReader(), new RouteValidator());

            var startPoint = this.pointA;
            var endPoint   = this.pointD;

            // Act
            var result = routeFinder.CalculateShortestRoute(startPoint, endPoint);

            // Assert
            result.Paths.Should().BeEquivalentTo(
                new List <Path>
            {
                new Path {
                    Id = 1, PointOne = this.pointA, PointTwo = this.pointB, Distance = 1
                },
                new Path {
                    Id = 2, PointOne = this.pointB, PointTwo = this.pointC, Distance = 4
                },
                new Path {
                    Id = 3, PointOne = this.pointC, PointTwo = this.pointD, Distance = 3
                },
            }
                );
        }
        public void RouteFinder_Should_Call_RouteValidator()
        {
            // Arrange
            var startPoint = this.pointA;
            var endPoint   = this.pointA;

            var routes = new List <Path>
            {
                new Path {
                    Id = 1, PointOne = this.pointA, PointTwo = this.pointB, Distance = 1
                },
                new Path {
                    Id = 2, PointOne = this.pointB, PointTwo = this.pointC, Distance = 4
                },
                new Path {
                    Id = 3, PointOne = this.pointC, PointTwo = this.pointD, Distance = 3
                },
                new Path {
                    Id = 4, PointOne = this.pointA, PointTwo = this.pointD, Distance = 20
                }
            };

            var expected = new List <Route>
            {
                new Route()
                {
                    Paths = new List <Path>()
                }
            };

            var mockRouteExplorer = new Mock <IRouteExplorer>();

            mockRouteExplorer
            .Setup(x => x.GetAllPossibleRoutes(It.IsAny <List <Path> >(), It.IsAny <Point>(), It.IsAny <Point>()))
            .Returns(expected);

            var mockShortestRouteFinder = new Mock <IShortestRouteFinder>();

            var mockShortestRouteFinderResult = new Route();

            mockShortestRouteFinder
            .Setup(x => x.GetShortestRoute(expected))
            .Returns(mockShortestRouteFinderResult);

            var mockRouteValidator = new Mock <IRouteValidator>();

            var mockRouteValidatorResult = new InputValidationException("The start and end point cannot be the same");

            mockRouteValidator
            .Setup(x => x.ValidateInput(startPoint, endPoint))
            .Throws(mockRouteValidatorResult);

            var routeFinder = new RouteFinder(mockRouteExplorer.Object, mockShortestRouteFinder.Object, new DatabaseReader(), mockRouteValidator.Object);

            // Act
            Action result = () => routeFinder.CalculateShortestRoute(startPoint, endPoint);

            // Assert
            result.Should().Throw <InputValidationException>()
            .WithMessage("The start and end point cannot be the same");
        }