Exemplo n.º 1
0
 public void RegisterRoute(RouteCriteria routeCriteria, string name = null)
 {
     if (routeCriteria == null)
     {
         throw new ArgumentException("At least one route criterion is required.");
     }
     this.routeList.Add(new RpcRoute(new List <RouteCriteria> {
         routeCriteria
     }, name));
 }
Exemplo n.º 2
0
 public void MatchesRpcRoute_DifferentRoutes_Valid(string requestUrl, string availableRouteName, bool shouldMatch)
 {
     IRpcRouteProvider routeProvider = new FakeRouteProvider();
     RouteCriteria routeCriteria = new RouteCriteria(typeof(ParserTests));
     routeProvider.RegisterRoute(routeCriteria, availableRouteName);
     DefaultRpcParser parser = new DefaultRpcParser(null);
     RpcRoute matchedRoute;
     bool isMatch = parser.MatchesRpcRoute(routeProvider, requestUrl, out matchedRoute);
     Assert.Equal(isMatch, shouldMatch);
     Assert.Equal(matchedRoute != null, shouldMatch);
 }
Exemplo n.º 3
0
        public void MatchesRpcRoute_DifferentRoutes_Valid(string requestUrl, string availableRouteName, bool shouldMatch)
        {
            IRpcRouteProvider routeProvider = new FakeRouteProvider();
            RouteCriteria     routeCriteria = new RouteCriteria(typeof(ParserTests));

            routeProvider.RegisterRoute(routeCriteria, availableRouteName);
            DefaultRpcParser parser = new DefaultRpcParser(null);
            RpcRoute         matchedRoute;
            bool             isMatch = parser.MatchesRpcRoute(routeProvider, requestUrl, out matchedRoute);

            Assert.Equal(isMatch, shouldMatch);
            Assert.Equal(matchedRoute != null, shouldMatch);
        }
Exemplo n.º 4
0
        public IEnumerable <PlaceDto> GetBestRoute(string originName, string destinationName, string criteria)
        {
            if (string.IsNullOrEmpty(originName) && string.IsNullOrEmpty(destinationName))
            {
                throw new ArgumentException("The Origin and Destination are required fields.");
            }

            var origin      = _readUnit.ReadRepository <Place>().Get(x => x.Description.Equals(originName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
            var destination = _readUnit.ReadRepository <Place>().Get(x => x.Description.Equals(destinationName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();

            if (origin == null)
            {
                throw new InvalidOperationException("The Origin provided does not exist.");
            }

            if (destination == null)
            {
                throw new InvalidOperationException("The Destination provided does not exist.");
            }

            //Retrieve filter criteria
            //if the passed criteria is null, the default value is Cost
            //if find a match will return a RouteCriteria type
            //if the passed value doesn't exist, will throw an exception
            RouteCriteria criteriaRoute = string.IsNullOrEmpty(criteria) ? RouteCriteria.Cost :
                                          RouteCriteria.FromDisplayName <RouteCriteria>(criteria);

            //retrieve routes and places from DB
            var routes = _readRepository.GetAllIncluding(x => x.Origin, x => x.Destination).ToList();
            var places = _readUnit.ReadRepository <Place>().GetAll().ToList();

            //create the graph network
            RoutingGraph <Place> graph = new RoutingGraph <Place>();

            places.ForEach(x => graph.AddNode(x));
            routes.ForEach(r => {
                int cost = RouteCriteria.Equals(criteriaRoute, RouteCriteria.Time) ? r.Time : r.Cost;
                graph.Connect(r.Origin, r.Destination, cost);
            });

            //processa o resultado
            var         processor = new RouteProcessor <Place>(graph);
            IPathResult result    = processor.Process(origin, destination);

            //build the result
            List <Place> resultados = new List <Place>();

            result.GetPath().ToList().ForEach(x => resultados.Add(graph[x].Item));
            return(resultados.EnumerableTo <PlaceDto>());
        }
Exemplo n.º 5
0
        public void RpcRouteWithBaseRoute_NoBaseRoute_Failure()
        {
            IRpcRouteProvider routeProvider = new FakeRouteProvider();

            routeProvider.BaseRequestPath = "Base";
            RouteCriteria routeCriteria = new RouteCriteria(typeof(ParserTests));

            routeProvider.RegisterRoute(routeCriteria, "Test");
            DefaultRpcParser parser = new DefaultRpcParser(null);
            RpcRoute         matchedRoute;
            bool             isMatch = parser.MatchesRpcRoute(routeProvider, "/Test", out matchedRoute);

            Assert.False(isMatch);
            Assert.Null(matchedRoute);
        }