public void CanManuallyConfigureActionLinkFactory() { // 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 ActionConfiguration reward = customer.Action("Reward"); reward.HasActionLink(ctx => new Uri(string.Format(uriTemplate, ctx.GetPropertyValue("CustomerId"))), followsConventions: false); 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 }); IEdmAction rewardAction = Assert.Single(model.SchemaElements.OfType <IEdmAction>()); // Guard OperationLinkBuilder actionLinkBuilder = model.GetAnnotationValue <OperationLinkBuilder>(rewardAction); //Assert Assert.Equal(expectedUri, reward.GetActionLink()(context)); Assert.NotNull(actionLinkBuilder); Assert.Equal(expectedUri, actionLinkBuilder.BuildLink(context)); }
public void FunctionLink_PreservesFollowsConventions(bool value) { // Arrange ODataModelBuilder builder = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>(); FunctionConfiguration configuration = new FunctionConfiguration(builder, "IgnoreFunction"); configuration.Returns <int>(); Mock <IEdmTypeConfiguration> bindingParameterTypeMock = new Mock <IEdmTypeConfiguration>(); bindingParameterTypeMock.Setup(o => o.Kind).Returns(EdmTypeKind.Entity); Type entityType = typeof(object); bindingParameterTypeMock.Setup(o => o.ClrType).Returns(entityType); configuration.SetBindingParameter("IgnoreParameter", bindingParameterTypeMock.Object); configuration.HasFunctionLink((a) => { throw new NotImplementedException(); }, followsConventions: value); builder.AddOperation(configuration); builder.AddEntityType(entityType); // Act IEdmModel model = builder.GetEdmModel(); // Assert var function = Assert.Single(model.SchemaElements.OfType <IEdmFunction>()); OperationLinkBuilder functionLinkBuilder = model.GetOperationLinkBuilder(function); Assert.NotNull(functionLinkBuilder); Assert.Equal(value, functionLinkBuilder.FollowsConventions); }
public void FollowsConventions_IsSpecifiedValue(bool value) { // Arrange OperationLinkBuilder builder = new OperationLinkBuilder((ResourceContext a) => { throw new NotImplementedException(); }, followsConventions: value); // Act bool followsConventions = builder.FollowsConventions; // Assert Assert.Equal(value, followsConventions); }
/// <summary> /// Register a factory that creates actions links. /// </summary> public ActionConfiguration HasActionLink(Func <ResourceContext, Uri> actionLinkFactory, bool followsConventions) { if (actionLinkFactory == null) { throw new ArgumentNullException("actionLinkFactory"); } if (!IsBindable || BindingParameter.TypeConfiguration.Kind != EdmTypeKind.Entity) { throw Error.InvalidOperation(SRResources.HasActionLinkRequiresBindToEntity, Name); } OperationLinkBuilder = new OperationLinkBuilder(actionLinkFactory, followsConventions); FollowsConventions = followsConventions; return(this); }
public void BuildOperationLink_ForFeed_ReturnsLink() { // Arrange OperationLinkBuilder builder = new OperationLinkBuilder((ResourceSetContext a) => new Uri("http://localhost:456"), followsConventions: true); ResourceContext entityContext = new ResourceContext(); ResourceSetContext feedContext = new ResourceSetContext(); // Act Uri link = builder.BuildLink(entityContext); Uri feedLink = builder.BuildLink(feedContext); // Assert Assert.Null(link); Assert.NotNull(feedLink); Assert.Equal("http://localhost:456/", feedLink.AbsoluteUri); }
/// <summary> /// Register a factory that creates feed functions links. /// </summary> public FunctionConfiguration HasFeedFunctionLink(Func <ResourceSetContext, Uri> functionLinkFactory, bool followsConventions) { if (functionLinkFactory == null) { throw new ArgumentNullException("functionLinkFactory"); } if (!IsBindable || BindingParameter.TypeConfiguration.Kind != EdmTypeKind.Collection || ((CollectionTypeConfiguration)BindingParameter.TypeConfiguration).ElementType.Kind != EdmTypeKind.Entity) { throw Error.InvalidOperation(SRResources.HasFunctionLinkRequiresBindToCollectionOfEntity, Name); } OperationLinkBuilder = new OperationLinkBuilder(functionLinkFactory, followsConventions); FollowsConventions = followsConventions; return(this); }
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.EnableODataDependencyInjectionSupport(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(); ResourceSetContext context = new ResourceSetContext { EntitySetBase = entitySet, Url = urlHelper, Request = request }; OperationLinkBuilder functionLinkBuilder = model.GetAnnotationValue <OperationLinkBuilder>(watchFunction); //Assert Assert.Equal(expectedUri, watch.GetFeedFunctionLink()(context)); Assert.NotNull(functionLinkBuilder); Assert.Equal(expectedUri, functionLinkBuilder.BuildLink(context)); }
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 CanManuallyConfigureFeedActionLinkFactory() { // 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); ActionConfiguration reward = customer.Collection.Action("Reward"); reward.HasFeedActionLink(ctx => expectedUri, followsConventions: false); IEdmModel model = builder.GetEdmModel(); // Act IEdmAction rewardAction = Assert.Single(model.SchemaElements.OfType <IEdmAction>()); // Guard OperationLinkBuilder actionLinkBuilder = model.GetAnnotationValue <OperationLinkBuilder>(rewardAction); ResourceSetContext context = new ResourceSetContext(); //Assert Assert.Equal(expectedUri, reward.GetFeedActionLink()(context)); Assert.NotNull(actionLinkBuilder); Assert.Equal(expectedUri, actionLinkBuilder.BuildLink(context)); }