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_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()); }
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 WhenActionLinksNotManuallyConfigured_ConventionBasedBuilderUsesConventions() { // Arrange string uriTemplate = "http://server/Movies({0})/Default.Watch"; Uri expectedUri = new Uri(string.Format(uriTemplate, 1)); ODataModelBuilder builder = ODataConventionModelBuilderFactory.Create(); EntityTypeConfiguration <Movie> movie = builder.EntitySet <Movie>("Movies").EntityType; ActionConfiguration watch = movie.Action("Watch"); IEdmModel model = builder.GetEdmModel(); var configuration = RoutingConfigurationFactory.Create(); string routeName = "Route"; configuration.MapODataServiceRoute(routeName, null, model); var request = RequestFactory.Create(HttpMethod.Get, "http://server/Movies", configuration, routeName); // Act IEdmEntityType movieType = model.SchemaElements.OfType <IEdmEntityType>().SingleOrDefault(); IEdmEntityContainer container = model.SchemaElements.OfType <IEdmEntityContainer>().SingleOrDefault(); IEdmAction watchAction = Assert.Single(model.SchemaElements.OfType <IEdmAction>()); // Guard IEdmEntitySet entitySet = container.EntitySets().SingleOrDefault(); ODataSerializerContext serializerContext = ODataSerializerContextFactory.Create(model, entitySet, request); ResourceContext context = new ResourceContext(serializerContext, movieType.AsReference(), new Movie { ID = 1, Name = "Avatar" }); OperationLinkBuilder actionLinkBuilder = model.GetAnnotationValue <OperationLinkBuilder>(watchAction); //Assert Assert.Equal(expectedUri, watch.GetActionLink()(context)); Assert.NotNull(actionLinkBuilder); Assert.Equal(expectedUri, actionLinkBuilder.BuildLink(context)); }
public void NavigationLinksGenerationConvention_GeneratesLinksWithoutCast_ForBaseProperties() { ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet <SportBike>("vehicles"); builder.EntitySet <Manufacturer>("manufacturers"); IEdmModel model = builder.GetEdmModel(); IEdmEntitySet vehiclesEdmEntitySet = model.EntityContainers().Single().FindEntitySet("vehicles"); IEdmEntityType sportbikeType = model.AssertHasEntityType(typeof(SportBike)); IEdmNavigationProperty motorcycleManufacturerProperty = sportbikeType.AssertHasNavigationProperty(model, "Manufacturer", typeof(MotorcycleManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne); 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); var serializerContext = new ODataSerializerContext { Model = model, EntitySet = vehiclesEdmEntitySet, Url = request.GetUrlHelper() }; var entityContext = new EntityInstanceContext(serializerContext, sportbikeType.AsReference(), new SportBike { Model = 2009, Name = "Ninja" }); Uri uri = linkBuilder.BuildNavigationLink(entityContext, motorcycleManufacturerProperty, ODataMetadataLevel.Default); Assert.Equal("http://localhost/vehicles(Model=2009,Name='Ninja')/Manufacturer", uri.AbsoluteUri); }
public void NavigationLinksGenerationConvention_GeneratesLinksWithCast_ForDerivedProperties() { ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet <Vehicle>("vehicles"); builder.EntitySet <Manufacturer>("manufacturers"); IEdmModel model = builder.GetEdmModel(); IEdmEntitySet vehiclesEdmEntitySet = model.EntityContainer.FindEntitySet("vehicles"); IEdmEntityType carType = model.AssertHasEntityType(typeof(Car)); IEdmNavigationProperty carManufacturerProperty = carType.AssertHasNavigationProperty(model, "Manufacturer", typeof(CarManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne); HttpConfiguration configuration = new HttpConfiguration(); string routeName = "Route"; configuration.MapODataServiceRoute(routeName, null, model); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost"); request.SetConfiguration(configuration); request.ODataProperties().RouteName = routeName; NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehiclesEdmEntitySet); var serializerContext = new ODataSerializerContext { Model = model, NavigationSource = vehiclesEdmEntitySet, Url = request.GetUrlHelper() }; var entityContext = new EntityInstanceContext(serializerContext, carType.AsReference(), new Car { Model = 2009, Name = "Accord" }); Uri uri = linkBuilder.BuildNavigationLink(entityContext, carManufacturerProperty, ODataMetadataLevel.FullMetadata); Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/System.Web.OData.Builder.TestModels.Car/Manufacturer", uri.AbsoluteUri); }
public void NavigationLinksGenerationConvention_GeneratesLinksWithCast_ForDerivedProperties() { ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create(); builder.EntitySet <Vehicle>("vehicles"); builder.EntitySet <Manufacturer>("manufacturers"); IEdmModel model = builder.GetEdmModel(); IEdmEntitySet vehiclesEdmEntitySet = model.EntityContainer.FindEntitySet("vehicles"); IEdmEntityType carType = model.AssertHasEntityType(typeof(Car)); IEdmNavigationProperty carManufacturerProperty = carType.AssertHasNavigationProperty(model, "Manufacturer", typeof(CarManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne); var configuration = RoutingConfigurationFactory.Create(); string routeName = "Route"; configuration.MapODataServiceRoute(routeName, null, model); var request = RequestFactory.Create(HttpMethod.Get, "http://localhost", configuration, routeName); NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehiclesEdmEntitySet); linkBuilder.AddNavigationPropertyLinkBuilder(carManufacturerProperty, new NavigationLinkBuilder((context, property) => context.GenerateNavigationPropertyLink(property, includeCast: true), false)); var serializerContext = ODataSerializerContextFactory.Create(model, vehiclesEdmEntitySet, request); var entityContext = new ResourceContext(serializerContext, carType.AsReference(), new Car { Model = 2009, Name = "Accord" }); Uri uri = linkBuilder.BuildNavigationLink(entityContext, carManufacturerProperty, ODataMetadataLevel.MinimalMetadata); Assert.Equal("http://localhost/vehicles(Model=2009,Name='Accord')/Microsoft.AspNet.OData.Test.Builder.TestModels.Car/Manufacturer", uri.AbsoluteUri); }
public void CanManuallyConfigureFunctionLinkFactory() { // Arrange string uriTemplate = "http://server/service/Customers({0})/Reward"; Uri expectedUri = new Uri(string.Format(uriTemplate, 1)); ODataModelBuilder builder = new ODataModelBuilder(); EntityTypeConfiguration <Customer> customer = builder.EntitySet <Customer>("Customers").EntityType; customer.HasKey(c => c.CustomerId); customer.Property(c => c.Name); // Act FunctionConfiguration reward = customer.Function("Reward"); reward.HasFunctionLink(ctx => new Uri(string.Format(uriTemplate, ctx.GetPropertyValue("CustomerId"))), followsConventions: false); reward.Returns <bool>(); IEdmModel model = builder.GetEdmModel(); IEdmEntityType customerType = model.SchemaElements.OfType <IEdmEntityType>().SingleOrDefault(); ODataSerializerContext serializerContext = new ODataSerializerContext { Model = model }; ResourceContext context = new ResourceContext(serializerContext, customerType.AsReference(), new Customer { CustomerId = 1 }); IEdmFunction rewardFunction = Assert.Single(model.SchemaElements.OfType <IEdmFunction>()); // Guard OperationLinkBuilder functionLinkBuilder = model.GetAnnotationValue <OperationLinkBuilder>(rewardFunction); //Assert Assert.Equal(expectedUri, reward.GetFunctionLink()(context)); Assert.NotNull(functionLinkBuilder); Assert.Equal(expectedUri, functionLinkBuilder.BuildLink(context)); }
public void GenerateNavigationLink_WorksToGenerateExpectedNavigationLink_ForContainedNavigation( bool includeCast, string expectedNavigationLink) { // NOTE: This test is generating a link that does not technically correspond to a valid model (specifically // the extra OrderLines navigation), but it allows us to validate the nested navigation scenario // without twisting the model unnecessarily. // Arrange IEdmEntityType myOrder = (IEdmEntityType)_myOrderModel.FindDeclaredType("NS.MyOrder"); IEdmEntityType orderLine = (IEdmEntityType)_myOrderModel.FindDeclaredType("NS.OrderLine"); IEdmNavigationProperty orderLinesProperty = myOrder.NavigationProperties().Single(x => x.ContainsTarget); IEdmEntitySet entitySet = _myOrderModel.FindDeclaredEntitySet("MyOrders"); IDictionary <string, object> parameters = new Dictionary <string, object> { { "ID", 42 } }; IDictionary <string, object> parameters2 = new Dictionary <string, object> { { "ID", 21 } }; // containment IEdmContainedEntitySet orderLines = (IEdmContainedEntitySet)entitySet.FindNavigationTarget(orderLinesProperty); ODataPath path = new ODataPath( new EntitySetSegment(entitySet), new KeySegment(parameters.ToArray(), myOrder, entitySet), new NavigationPropertySegment(orderLinesProperty, orderLines), new KeySegment(parameters2.ToArray(), orderLine, orderLines)); var request = RequestFactory.Create(_myOrderModel); var serializerContext = ODataSerializerContextFactory.Create(_myOrderModel, orderLines, path, request); var entityContext = new ResourceContext(serializerContext, orderLine.AsReference(), new { ID = 21 }); // Act Uri uri = entityContext.GenerateNavigationPropertyLink(orderLinesProperty, includeCast); // Assert Assert.Equal(expectedNavigationLink, uri.AbsoluteUri); }
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 WhenFunctionLinksNotManuallyConfigured_ConventionBasedBuilderUsesConventions() { // Arrange string uriTemplate = "http://server/Movies({0})/Default.Watch(param=@param)"; Uri expectedUri = new Uri(string.Format(uriTemplate, 1)); ODataModelBuilder builder = new ODataConventionModelBuilder(); EntityTypeConfiguration <Movie> movie = builder.EntitySet <Movie>("Movies").EntityType; FunctionConfiguration watch = movie.Function("Watch").Returns <int>(); watch.Parameter <string>("param"); IEdmModel model = builder.GetEdmModel(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://server/Movies"); HttpConfiguration configuration = new HttpConfiguration(); string routeName = "Route"; configuration.MapODataServiceRoute(routeName, null, model); request.SetConfiguration(configuration); request.EnableODataDependencyInjectionSupport(routeName); UrlHelper urlHelper = new UrlHelper(request); // Act IEdmEntityType movieType = model.SchemaElements.OfType <IEdmEntityType>().SingleOrDefault(); IEdmEntityContainer container = model.SchemaElements.OfType <IEdmEntityContainer>().SingleOrDefault(); IEdmFunction watchFunction = Assert.Single(model.SchemaElements.OfType <IEdmFunction>()); // Guard IEdmEntitySet entitySet = container.EntitySets().SingleOrDefault(); ODataSerializerContext serializerContext = new ODataSerializerContext { Model = model, NavigationSource = entitySet, Url = urlHelper }; ResourceContext context = new ResourceContext(serializerContext, movieType.AsReference(), new Movie { ID = 1, Name = "Avatar" }); OperationLinkBuilder functionLinkBuilder = model.GetAnnotationValue <OperationLinkBuilder>(watchFunction); //Assert Assert.Equal(expectedUri, watch.GetFunctionLink()(context)); Assert.NotNull(functionLinkBuilder); Assert.Equal(expectedUri, functionLinkBuilder.BuildLink(context)); }
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_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()); }
public void GenerateNavigationLink_WorksToGenerateExpectedNavigationLink_ForNonContainedNavigation() { // Arrange IEdmEntityType myOrder = (IEdmEntityType)_myOrderModel.FindDeclaredType("NS.MyOrder"); IEdmEntityType orderLine = (IEdmEntityType)_myOrderModel.FindDeclaredType("NS.OrderLine"); IEdmNavigationProperty nonOrderLinesProperty = myOrder.NavigationProperties().Single(x => x.Name.Equals("NonContainedOrderLines")); IEdmEntitySet entitySet = _myOrderModel.FindDeclaredEntitySet("MyOrders"); IDictionary <string, object> parameters = new Dictionary <string, object> { { "ID", 42 } }; IDictionary <string, object> parameters2 = new Dictionary <string, object> { { "ID", 21 } }; IEdmNavigationSource nonContainedOrderLines = entitySet.FindNavigationTarget(nonOrderLinesProperty); ODataPath path = new ODataPath( new EntitySetSegment(entitySet), new KeySegment(parameters.ToArray(), myOrder, entitySet), new NavigationPropertySegment(nonOrderLinesProperty, nonContainedOrderLines), new KeySegment(parameters2.ToArray(), orderLine, nonContainedOrderLines)); IEdmNavigationProperty orderLinesProperty = myOrder.NavigationProperties().Single(x => x.ContainsTarget); IEdmContainedEntitySet orderLines = (IEdmContainedEntitySet)entitySet.FindNavigationTarget(orderLinesProperty); var request = RequestFactory.Create(_myOrderModel); var serializerContext = ODataSerializerContextFactory.Create(_myOrderModel, orderLines, path, request); var entityContext = new ResourceContext(serializerContext, orderLine.AsReference(), new { ID = 21 }); // Act Uri uri = entityContext.GenerateSelfLink(false); // Assert Assert.Equal("http://localhost/OrderLines(21)", uri.AbsoluteUri); }
public void NavigationLinksGenerationConvention_GeneratesLinksWithoutCast_ForBaseProperties() { ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create(); builder.EntitySet <SportBike>("vehicles"); builder.EntitySet <Manufacturer>("manufacturers"); IEdmModel model = builder.GetEdmModel(); IEdmEntitySet vehiclesEdmEntitySet = model.EntityContainer.FindEntitySet("vehicles"); IEdmEntityType sportbikeType = model.AssertHasEntityType(typeof(SportBike)); IEdmNavigationProperty motorcycleManufacturerProperty = sportbikeType.AssertHasNavigationProperty(model, "Manufacturer", typeof(MotorcycleManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne); var configuration = RoutingConfigurationFactory.Create(); string routeName = "Route"; configuration.MapODataServiceRoute(routeName, null, model); var request = RequestFactory.Create(HttpMethod.Get, "http://localhost", configuration, routeName); NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehiclesEdmEntitySet); linkBuilder.AddNavigationPropertyLinkBuilder(motorcycleManufacturerProperty, new NavigationLinkBuilder((context, property) => context.GenerateNavigationPropertyLink(property, includeCast: false), false)); var serializerContext = ODataSerializerContextFactory.Create(model, vehiclesEdmEntitySet, request); var entityContext = new ResourceContext(serializerContext, sportbikeType.AsReference(), new SportBike { Model = 2009, Name = "Ninja" }); // We might get one of these: // http://localhost/vehicles(Model=2009,Name='Ninja')/Manufacturer // http://localhost/vehicles(Name='Ninja',Model=2009)/Manufacturer Uri uri = linkBuilder.BuildNavigationLink(entityContext, motorcycleManufacturerProperty, ODataMetadataLevel.MinimalMetadata); string aboluteUri = uri.AbsoluteUri; Assert.Contains("http://localhost/vehicles(", aboluteUri); Assert.Contains("Model=2009", aboluteUri); Assert.Contains("Name='Ninja'", aboluteUri); Assert.Contains(")/Manufacturer", aboluteUri); }
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()); }
public void NavigationLinksGenerationConvention_GeneratesLinksWithoutCast_ForDeclaredProperties_OnSingleton() { ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.Singleton <Car>("Contoso"); builder.EntitySet <Manufacturer>("manufacturers"); IEdmModel model = builder.GetEdmModel(); IEdmSingleton vehicleEdmSingleton = model.EntityContainer.FindSingleton("Contoso"); IEdmEntityType carType = model.AssertHasEntityType(typeof(Car)); IEdmNavigationProperty carManufacturerProperty = carType.AssertHasNavigationProperty(model, "Manufacturer", typeof(CarManufacturer), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne); HttpConfiguration configuration = new HttpConfiguration(); string routeName = "Route"; configuration.MapODataServiceRoute(routeName, null, model); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost"); request.SetConfiguration(configuration); request.EnableODataDependencyInjectionSupport(routeName); NavigationSourceLinkBuilderAnnotation linkBuilder = model.GetNavigationSourceLinkBuilder(vehicleEdmSingleton); linkBuilder.AddNavigationPropertyLinkBuilder(carManufacturerProperty, new NavigationLinkBuilder((context, property) => context.GenerateNavigationPropertyLink(property, includeCast: false), false)); var serializerContext = new ODataSerializerContext { Model = model, NavigationSource = vehicleEdmSingleton, Url = request.GetUrlHelper() }; var entityContext = new ResourceContext(serializerContext, carType.AsReference(), new Car { Model = 2014, Name = "Contoso2014" }); Uri uri = linkBuilder.BuildNavigationLink(entityContext, carManufacturerProperty, ODataMetadataLevel.MinimalMetadata); Assert.Equal("http://localhost/Contoso/Manufacturer", uri.AbsoluteUri); }
public void WhenActionLinksNotManuallyConfigured_ConventionBasedBuilderUsesConventions() { // Arrange string uriTemplate = "http://server/Movies({0})/Watch"; Uri expectedUri = new Uri(string.Format(uriTemplate, 1)); ODataModelBuilder builder = new ODataConventionModelBuilder(); EntityTypeConfiguration <Movie> movie = builder.EntitySet <Movie>("Movies").EntityType; ActionConfiguration watch = movie.Action("Watch"); IEdmModel model = builder.GetEdmModel(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://server/Movies"); HttpConfiguration configuration = new HttpConfiguration(); string routeName = "Route"; configuration.Routes.MapODataServiceRoute(routeName, null, model); request.SetConfiguration(configuration); request.ODataProperties().RouteName = routeName; UrlHelper urlHelper = new UrlHelper(request); // Act IEdmEntityType movieType = model.SchemaElements.OfType <IEdmEntityType>().SingleOrDefault(); IEdmEntityContainer container = model.SchemaElements.OfType <IEdmEntityContainer>().SingleOrDefault(); IEdmFunctionImport watchAction = container.FunctionImports().SingleOrDefault(); IEdmEntitySet entitySet = container.EntitySets().SingleOrDefault(); ODataSerializerContext serializerContext = new ODataSerializerContext { Model = model, EntitySet = entitySet, Url = urlHelper }; EntityInstanceContext context = new EntityInstanceContext(serializerContext, movieType.AsReference(), new Movie { ID = 1, Name = "Avatar" }); ActionLinkBuilder actionLinkBuilder = model.GetAnnotationValue <ActionLinkBuilder>(watchAction); //Assert Assert.Equal(expectedUri, watch.GetActionLink()(context)); Assert.NotNull(actionLinkBuilder); Assert.Equal(expectedUri, actionLinkBuilder.BuildActionLink(context)); }