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 }; EntityInstanceContext context = new EntityInstanceContext(serializerContext, customerType.AsReference(), new Customer { CustomerId = 1 }); IEdmFunction rewardFunction = Assert.Single(model.SchemaElements.OfType <IEdmFunction>()); // Guard FunctionLinkBuilder functionLinkBuilder = model.GetAnnotationValue <FunctionLinkBuilder>(rewardFunction); //Assert Assert.Equal(expectedUri, reward.GetFunctionLink()(context)); Assert.NotNull(functionLinkBuilder); Assert.Equal(expectedUri, functionLinkBuilder.BuildFunctionLink(context)); }
public void BuildFunctionLink_ForFeed_ReturnsLink() { // Arrange FunctionLinkBuilder builder = new FunctionLinkBuilder((FeedContext a) => new Uri("http://localhost:456"), followsConventions: true); EntityInstanceContext entityContext = new EntityInstanceContext(); FeedContext feedContext = new FeedContext(); // Act Uri link = builder.BuildFunctionLink(entityContext); Uri feedLink = builder.BuildFunctionLink(feedContext); // Assert Assert.Null(link); Assert.NotNull(feedLink); Assert.Equal("http://localhost:456/", feedLink.AbsoluteUri); }
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.ODataProperties().RouteName = 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 }; EntityInstanceContext context = new EntityInstanceContext(serializerContext, movieType.AsReference(), new Movie { ID = 1, Name = "Avatar" }); FunctionLinkBuilder functionLinkBuilder = model.GetAnnotationValue <FunctionLinkBuilder>(watchFunction); //Assert Assert.Equal(expectedUri, watch.GetFunctionLink()(context)); Assert.NotNull(functionLinkBuilder); Assert.Equal(expectedUri, functionLinkBuilder.BuildFunctionLink(context)); }
public void WhenFeedActionLinksNotManuallyConfigured_ConventionBasedBuilderUsesConventions() { // Arrange Uri expectedUri = new Uri("http://server/Movies/Default.Watch(param=@param)"); ODataModelBuilder builder = new ODataConventionModelBuilder(); EntityTypeConfiguration <Movie> movie = builder.EntitySet <Movie>("Movies").EntityType; FunctionConfiguration watch = movie.Collection.Function("Watch").Returns <int>(); // function bound to collection 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.ODataProperties().RouteName = routeName; UrlHelper urlHelper = new UrlHelper(request); // Act IEdmEntityContainer container = model.SchemaElements.OfType <IEdmEntityContainer>().SingleOrDefault(); IEdmFunction watchFunction = Assert.Single(model.SchemaElements.OfType <IEdmFunction>()); // Guard IEdmEntitySet entitySet = container.EntitySets().SingleOrDefault(); FeedContext context = new FeedContext { EntitySetBase = entitySet, Url = urlHelper, Request = request }; FunctionLinkBuilder functionLinkBuilder = model.GetAnnotationValue <FunctionLinkBuilder>(watchFunction); //Assert Assert.Equal(expectedUri, watch.GetFeedFunctionLink()(context)); Assert.NotNull(functionLinkBuilder); Assert.Equal(expectedUri, functionLinkBuilder.BuildFunctionLink(context)); }
public void CanManuallyConfigureFeedFunctionLinkFactory() { // Arrange Uri expectedUri = new Uri("http://localhost/service/Customers/Reward"); ODataModelBuilder builder = new ODataModelBuilder(); EntityTypeConfiguration <Customer> customer = builder.EntitySet <Customer>("Customers").EntityType; customer.HasKey(c => c.CustomerId); customer.Property(c => c.Name); FunctionConfiguration reward = customer.Collection.Function("Reward").Returns <int>(); reward.HasFeedFunctionLink(ctx => expectedUri, followsConventions: false); IEdmModel model = builder.GetEdmModel(); // Act IEdmFunction rewardFuntion = Assert.Single(model.SchemaElements.OfType <IEdmFunction>()); // Guard FunctionLinkBuilder functionLinkBuilder = model.GetAnnotationValue <FunctionLinkBuilder>(rewardFuntion); FeedContext context = new FeedContext(); //Assert Assert.Equal(expectedUri, reward.GetFeedFunctionLink()(context)); Assert.NotNull(functionLinkBuilder); Assert.Equal(expectedUri, functionLinkBuilder.BuildFunctionLink(context)); }