public void GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var cars        = builder.EntitySet <Car>("cars");
            var paintAction = cars.EntityType.Action("Paint");

            IEdmModel model      = builder.GetEdmModel();
            var       carsEdmSet = model.EntityContainers().Single().FindEntitySet("cars");

            HttpConfiguration configuration = new HttpConfiguration();

            configuration.EnableOData(model);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");

            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
            request.Properties[HttpPropertyKeys.HttpRouteDataKey]     = new HttpRouteData(new HttpRoute());

            Uri link = ActionLinkGenerationConvention.GenerateActionLink(
                new EntityInstanceContext()
            {
                EdmModel       = model,
                EntitySet      = carsEdmSet,
                EntityType     = carsEdmSet.ElementType,
                UrlHelper      = request.GetUrlHelper(),
                PathHandler    = new DefaultODataPathHandler(model),
                EntityInstance = new Car {
                    Model = 2009, Name = "Accord"
                }
            },
                paintAction);

            Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')/Paint", link.AbsoluteUri);
        }
        public void GenerateActionLink_GeneratesLinkWithCast_IfEntitySetTypeDoesnotMatchActionEntityType()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var vehicles    = builder.EntitySet <Vehicle>("vehicles");
            var car         = builder.Entity <Car>();
            var paintAction = car.Action("Paint");

            IEdmModel model          = builder.GetEdmModel();
            var       vehiclesEdmSet = model.EntityContainers().Single().FindEntitySet("vehicles");
            var       carEdmType     = model.FindDeclaredType("System.Web.Http.OData.Builder.TestModels.Car") as IEdmEntityType;

            HttpConfiguration configuration = new HttpConfiguration();

            configuration.Routes.MapHttpRoute(ODataRouteNames.InvokeBoundActionWithCast, "{controller}({boundId})/{entityType}/{odataAction}");

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");

            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
            request.Properties[HttpPropertyKeys.HttpRouteDataKey]     = new HttpRouteData(new HttpRoute());

            Uri link = ActionLinkGenerationConvention.GenerateActionLink(
                new EntityInstanceContext(model, vehiclesEdmSet, carEdmType, request.GetUrlHelper(), new Car {
                Model = 2009, Name = "Accord"
            }),
                paintAction);

            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/System.Web.Http.OData.Builder.TestModels.Car/Paint", link.AbsoluteUri);
        }
        public void GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var cars        = builder.EntitySet <Car>("cars");
            var paintAction = cars.EntityType.Action("Paint");

            IEdmModel model      = builder.GetEdmModel();
            var       carsEdmSet = model.EntityContainers().Single().FindEntitySet("cars");

            HttpConfiguration configuration = new HttpConfiguration();

            configuration.Routes.MapHttpRoute(ODataRouteNames.InvokeBoundAction, "{controller}({boundId})/{odataAction}");

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");

            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
            request.Properties[HttpPropertyKeys.HttpRouteDataKey]     = new HttpRouteData(new HttpRoute());

            Uri link = ActionLinkGenerationConvention.GenerateActionLink(
                new EntityInstanceContext(model, carsEdmSet, carsEdmSet.ElementType, request.GetUrlHelper(), new Car {
                Model = 2009, Name = "Accord"
            }),
                paintAction);

            Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')/Paint", link.AbsoluteUri);
        }
Ejemplo n.º 4
0
        public void GenerateActionLink_GeneratesLinkWithoutCast_IfEntitySetTypeDerivesFromActionEntityType()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var cars        = builder.EntitySet <Car>("cars");
            var paintAction = builder.Entity <Vehicle>().Action("Paint");

            IEdmModel model      = builder.GetEdmModel();
            var       carsEdmSet = model.EntityContainers().Single().FindEntitySet("cars");

            HttpConfiguration configuration = new HttpConfiguration();
            string            routeName     = "Route";

            configuration.Routes.MapODataRoute(routeName, null, model);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");

            request.SetConfiguration(configuration);
            request.SetODataRouteName(routeName);

            Uri link = ActionLinkGenerationConvention.GenerateActionLink(
                new EntityInstanceContext()
            {
                EdmModel       = model,
                EntitySet      = carsEdmSet,
                EntityType     = carsEdmSet.ElementType,
                Url            = request.GetUrlHelper(),
                EntityInstance = new Car {
                    Model = 2009, Name = "Accord"
                }
            },
                paintAction);

            Assert.Equal("http://localhost/cars(Model=2009,Name='Accord')/Paint", link.AbsoluteUri);
        }
Ejemplo n.º 5
0
        public void GenerateActionLink_GeneratesLinkWithCast_IfEntitySetTypeDoesnotMatchActionEntityType()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var vehicles    = builder.EntitySet <Vehicle>("vehicles");
            var car         = builder.Entity <Car>();
            var paintAction = car.Action("Paint");

            IEdmModel model          = builder.GetEdmModel();
            var       vehiclesEdmSet = model.EntityContainers().Single().FindEntitySet("vehicles");
            var       carEdmType     = model.FindDeclaredType("System.Web.Http.OData.Builder.TestModels.Car") as IEdmEntityType;

            HttpConfiguration configuration = new HttpConfiguration();
            string            routeName     = "Route";

            configuration.Routes.MapODataRoute(routeName, null, model);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");

            request.SetConfiguration(configuration);
            request.SetODataRouteName(routeName);

            Uri link = ActionLinkGenerationConvention.GenerateActionLink(
                new EntityInstanceContext()
            {
                EdmModel       = model,
                EntitySet      = vehiclesEdmSet,
                EntityType     = carEdmType,
                Url            = request.GetUrlHelper(),
                EntityInstance = new Car {
                    Model = 2009, Name = "Accord"
                }
            },
                paintAction);

            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/System.Web.Http.OData.Builder.TestModels.Car/Paint", link.AbsoluteUri);
        }