public void SelfLinksGenerationConvention_Uses_GetByIdWithoutCast_IfDerivedTypeDoesnotHaveNavigationProperty()
        {
            ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create();
            var motorcycles = builder.EntitySet <Motorcycle>("motorcycles");

            IEdmModel      model = builder.GetEdmModel();
            IEdmEntitySet  vehiclesEdmEntitySet = model.EntityContainer.EntitySets().Single();
            IEdmEntityType sportbikeType        = model.AssertHasEntityType(typeof(SportBike));

            var request = RequestFactory.CreateFromModel(model);
            NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehiclesEdmEntitySet);
            var serializerContext = ODataSerializerContextFactory.Create(model, vehiclesEdmEntitySet, request);
            var entityContext     = new ResourceContext(serializerContext, sportbikeType.AsReference(), new SportBike {
                Model = 2009, Name = "Ninja"
            });

            EntitySelfLinks selfLinks = linkBuilder.BuildEntitySelfLinks(entityContext, ODataMetadataLevel.FullMetadata);

            // This test sometimes writes one of these two:
            //Assert.Equal("http://localhost/motorcycles(Model=2009,Name='Ninja')", );
            //Assert.Equal("http://localhost/motorcycles(Name='Ninja',Model=2009)", );
            var link = selfLinks.IdLink.ToString();

            Assert.Contains("http://localhost/motorcycles", link);
            Assert.Contains("Model=2009", link);
            Assert.Contains("Name='Ninja'", link);
        }
        public void SelfLinksGenerationConvention_Uses_WithCast_IfDerivedTypeHasNavigationProperty_ForSingleton()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var myVehicle = builder.Singleton <Vehicle>("MyVehicle");

            IEdmModel      model = builder.GetEdmModel();
            IEdmSingleton  vehicleEdmSingleton = model.EntityContainer.FindSingleton("MyVehicle");
            IEdmEntityType carType             = model.AssertHasEntityType(typeof(Car));

            HttpRequestMessage request = GetODataRequest(model);
            NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehicleEdmSingleton);
            var serializerContext = new ODataSerializerContext {
                Model = model, NavigationSource = vehicleEdmSingleton, Url = request.GetUrlHelper()
            };
            var entityContext = new EntityInstanceContext(serializerContext, carType.AsReference(), new Car {
                Model = 2014, Name = "Contoso"
            });

            // Act
            EntitySelfLinks selfLinks = linkBuilder.BuildEntitySelfLinks(entityContext, ODataMetadataLevel.FullMetadata);

            // Assert
            Assert.Equal("http://localhost/MyVehicle", selfLinks.IdLink.ToString());
            Assert.Equal("http://localhost/MyVehicle/System.Web.OData.Builder.TestModels.Car", selfLinks.EditLink.ToString());
        }
        public void SelfLinksGenerationConvention_Uses_WithoutCast_IfDerivedTypeDoesnotHaveNavigationProperty_ForSingleton()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var myMotorcycle = builder.Singleton <Motorcycle>("MyMotor");

            IEdmModel      model = builder.GetEdmModel();
            IEdmSingleton  vehicleEdmSingleton = model.EntityContainer.FindSingleton("MyMotor");
            IEdmEntityType sportbikeType       = model.AssertHasEntityType(typeof(SportBike));

            HttpRequestMessage request = GetODataRequest(model);
            NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehicleEdmSingleton);
            var serializerContext = new ODataSerializerContext {
                Model = model, NavigationSource = vehicleEdmSingleton, Url = request.GetUrlHelper()
            };
            var entityContext = new EntityInstanceContext(serializerContext, sportbikeType.AsReference(), new SportBike {
                Model = 2014, Name = "Ninja"
            });

            // Act
            EntitySelfLinks selfLinks = linkBuilder.BuildEntitySelfLinks(entityContext, ODataMetadataLevel.FullMetadata);

            // Assert
            Assert.Equal("http://localhost/MyMotor", selfLinks.IdLink.ToString());
        }
        /// <summary>
        /// Constructs the <see cref="EntitySelfLinks" /> for a particular <see cref="EntityInstanceContext" /> and <see cref="ODataMetadataLevel" />.
        /// </summary>
        public virtual EntitySelfLinks BuildEntitySelfLinks(EntityInstanceContext instanceContext, ODataMetadataLevel metadataLevel)
        {
            EntitySelfLinks selfLinks = new EntitySelfLinks();

            selfLinks.IdLink   = BuildIdLink(instanceContext, metadataLevel);
            selfLinks.EditLink = BuildEditLink(instanceContext, metadataLevel, selfLinks.IdLink);
            selfLinks.ReadLink = BuildReadLink(instanceContext, metadataLevel, selfLinks.EditLink);
            return(selfLinks);
        }
        public void SelfLinksGenerationConvention_Uses_GetByIdWithCast_IfDerivedTypeHasNavigationProperty()
        {
            ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create();
            var vehicles = builder.EntitySet <Vehicle>("vehicles");

            IEdmModel      model = builder.GetEdmModel();
            IEdmEntitySet  vehiclesEdmEntitySet = model.EntityContainer.EntitySets().Single();
            IEdmEntityType carType = model.AssertHasEntityType(typeof(Car));

            var request = RequestFactory.CreateFromModel(model);
            NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehiclesEdmEntitySet);
            var serializerContext = ODataSerializerContextFactory.Create(model, vehiclesEdmEntitySet, request);
            var entityContext     = new ResourceContext(serializerContext, carType.AsReference(), new Car {
                Model = 2009, Name = "Contoso"
            });

            EntitySelfLinks selfLinks = linkBuilder.BuildEntitySelfLinks(entityContext, ODataMetadataLevel.FullMetadata);

            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Contoso')", selfLinks.IdLink.ToString());
            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Contoso')/Microsoft.Test.AspNet.OData.Builder.TestModels.Car", selfLinks.EditLink.ToString());
        }
        public void SelfLinksGenerationConvention_Uses_GetByIdWithoutCast_IfDerivedTypeDoesnotHaveNavigationProperty()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var motorcycles = builder.EntitySet <Motorcycle>("motorcycles");

            IEdmModel      model = builder.GetEdmModel();
            IEdmEntitySet  vehiclesEdmEntitySet = model.EntityContainer.EntitySets().Single();
            IEdmEntityType sportbikeType        = model.AssertHasEntityType(typeof(SportBike));

            HttpRequestMessage request = GetODataRequest(model);
            NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehiclesEdmEntitySet);
            var serializerContext = new ODataSerializerContext {
                Model = model, NavigationSource = vehiclesEdmEntitySet, Url = request.GetUrlHelper()
            };
            var entityContext = new EntityInstanceContext(serializerContext, sportbikeType.AsReference(), new SportBike {
                Model = 2009, Name = "Ninja"
            });

            EntitySelfLinks selfLinks = linkBuilder.BuildEntitySelfLinks(entityContext, ODataMetadataLevel.FullMetadata);

            Assert.Equal("http://localhost/motorcycles(Model=2009,Name='Ninja')", selfLinks.IdLink.ToString());
        }
Exemplo n.º 7
0
        public void SelfLinksGenerationConvention_Uses_GetByIdWithCast_IfDerivedTypeHasNavigationProperty()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var vehicles = builder.EntitySet <Vehicle>("vehicles");

            IEdmModel      model = builder.GetEdmModel();
            IEdmEntitySet  vehiclesEdmEntitySet = model.EntityContainers().Single().EntitySets().Single();
            IEdmEntityType carType = model.AssertHasEntityType(typeof(Car));

            HttpRequestMessage             request     = GetODataRequest(model);
            EntitySetLinkBuilderAnnotation linkBuilder = model.GetEntitySetLinkBuilder(vehiclesEdmEntitySet);
            var serializerContext = new ODataSerializerContext {
                Model = model, EntitySet = vehiclesEdmEntitySet, Url = request.GetUrlHelper()
            };
            var entityContext = new EntityInstanceContext(serializerContext, carType.AsReference(), new Car {
                Model = 2009, Name = "Accord"
            });

            EntitySelfLinks selfLinks = linkBuilder.BuildEntitySelfLinks(entityContext, ODataMetadataLevel.Default);

            Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/System.Web.Http.OData.Builder.TestModels.Car", selfLinks.IdLink);
        }
        public void SelfLinksGenerationConvention_Uses_WithCast_IfDerivedTypeHasNavigationProperty_ForSingleton()
        {
            // Arrange
            ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create();
            var myVehicle = builder.Singleton <Vehicle>("MyVehicle");

            IEdmModel      model = builder.GetEdmModel();
            IEdmSingleton  vehicleEdmSingleton = model.EntityContainer.FindSingleton("MyVehicle");
            IEdmEntityType carType             = model.AssertHasEntityType(typeof(Car));

            var request = RequestFactory.CreateFromModel(model);
            NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehicleEdmSingleton);
            var serializerContext = ODataSerializerContextFactory.Create(model, vehicleEdmSingleton, request);
            var entityContext     = new ResourceContext(serializerContext, carType.AsReference(), new Car {
                Model = 2014, Name = "Contoso"
            });

            // Act
            EntitySelfLinks selfLinks = linkBuilder.BuildEntitySelfLinks(entityContext, ODataMetadataLevel.FullMetadata);

            // Assert
            Assert.Equal("http://localhost/MyVehicle", selfLinks.IdLink.ToString());
            Assert.Equal("http://localhost/MyVehicle/Microsoft.Test.AspNet.OData.Builder.TestModels.Car", selfLinks.EditLink.ToString());
        }
Exemplo n.º 9
0
        public void SelfLinksGenerationConvention_Uses_GetByIdWithoutCast_IfDerivedTypeDoesnotHaveNavigationProperty()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var motorcycles = builder.EntitySet <Motorcycle>("motorcycles");

            IEdmModel      model = builder.GetEdmModel();
            IEdmEntitySet  vehiclesEdmEntitySet = model.EntityContainers().Single().EntitySets().Single();
            IEdmEntityType sportbikeType        = model.AssertHasEntityType(typeof(SportBike));

            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);

            EntitySetLinkBuilderAnnotation linkBuilder = model.GetEntitySetLinkBuilder(vehiclesEdmEntitySet);

            EntitySelfLinks selfLinks = linkBuilder.BuildEntitySelfLinks(
                new EntityInstanceContext()
            {
                EdmModel       = model,
                EntitySet      = vehiclesEdmEntitySet,
                EntityType     = sportbikeType,
                Url            = request.GetUrlHelper(),
                EntityInstance = new Car {
                    Model = 2009, Name = "Ninja"
                }
            },
                ODataMetadataLevel.Default);

            Assert.Equal("http://localhost/motorcycles(Model=2009,Name='Ninja')", selfLinks.IdLink);
        }
Exemplo n.º 10
0
        public void SelfLinksGenerationConvention_Uses_GetByIdWithCast_IfDerivedTypeHasNavigationProperty()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var vehicles = builder.EntitySet <Vehicle>("vehicles");

            IEdmModel      model = builder.GetEdmModel();
            IEdmEntitySet  vehiclesEdmEntitySet = model.EntityContainers().Single().EntitySets().Single();
            IEdmEntityType carType = model.AssertHasEntityType(typeof(Car));

            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);

            EntitySetLinkBuilderAnnotation linkBuilder = model.GetEntitySetLinkBuilder(vehiclesEdmEntitySet);

            EntitySelfLinks selfLinks = linkBuilder.BuildEntitySelfLinks(
                new EntityInstanceContext()
            {
                EdmModel       = model,
                EntitySet      = vehiclesEdmEntitySet,
                EntityType     = carType,
                Url            = request.GetUrlHelper(),
                EntityInstance = new Car {
                    Model = 2009, Name = "Accord"
                }
            },
                ODataMetadataLevel.Default);

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