public void Apply_FollowsConventions()
        {
            // Arrange
            ODataModelBuilder            builder  = new ODataModelBuilder();
            FunctionConfiguration        function = new FunctionConfiguration(builder, "IgnoreAction");
            Mock <IEdmTypeConfiguration> mockBindingParameterType = new Mock <IEdmTypeConfiguration>();

            mockBindingParameterType.Setup(o => o.Kind).Returns(EdmTypeKind.Entity);
            mockBindingParameterType.Setup(o => o.ClrType).Returns(typeof(int));
            function.SetBindingParameter("IgnoreParameter", mockBindingParameterType.Object);
            FunctionLinkGenerationConvention convention = new FunctionLinkGenerationConvention();

            // Act
            convention.Apply(function, builder);

            // Assert
            Assert.True(function.FollowsConventions);
        }
        public void Apply_Doesnot_Override_UserConfiguration()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var customers = builder.EntitySet <Customer>("Customers");
            var function  = customers.EntityType.Function("MyFunction").Returns <int>();

            function.HasFunctionLink(ctx => new Uri("http://localhost/FunctionTestWorks"), followsConventions: false);
            FunctionLinkGenerationConvention convention = new FunctionLinkGenerationConvention();

            convention.Apply(function, builder);

            // Act
            IEdmModel model        = builder.GetEdmModel();
            var       edmCustomers = model.EntityContainer.FindEntitySet("Customers");
            var       edmType      = model.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer");
            var       edmFunction  = model.SchemaElements.OfType <IEdmFunction>().Single(f => f.Name == "MyFunction");

            Assert.NotNull(edmFunction);

            HttpConfiguration configuration = new HttpConfiguration();

            configuration.MapODataServiceRoute(model);

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

            request.SetConfiguration(configuration);

            OperationLinkBuilder fuinctionLinkBuilder = model.GetOperationLinkBuilder(edmFunction);

            var serializerContext = new ODataSerializerContext {
                Model = model, NavigationSource = edmCustomers, Url = request.GetUrlHelper()
            };
            var entityContext = new ResourceContext(serializerContext, edmType.AsReference(), new Customer {
                Id = 109
            });

            // Assert
            Uri link = fuinctionLinkBuilder.BuildLink(entityContext);

            Assert.Equal("http://localhost/FunctionTestWorks", link.AbsoluteUri);
        }
        public void Apply_WorksFor_FunctionBoundToCollectionOfEntity()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var function = builder.EntityType<Customer>().Collection.Function("MyFunction").Returns<int>();
            function.Parameter<string>("param");

            // Act
            Assert.Null(function.GetFunctionLink()); // Guard
            Assert.Null(function.GetFeedFunctionLink()); // Guard

            FunctionLinkGenerationConvention convention = new FunctionLinkGenerationConvention();
            convention.Apply(function, builder);

            // Assert
            var functionLink = function.GetFeedFunctionLink();
            Assert.NotNull(functionLink);
            Assert.IsType<Func<FeedContext, Uri>>(functionLink);

            Assert.Null(function.GetFunctionLink());
        }
        public void Apply_WorksFor_FunctionBoundToCollectionOfEntity()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var function = builder.EntityType <Customer>().Collection.Function("MyFunction").Returns <int>();

            function.Parameter <string>("param");

            // Act
            Assert.Null(function.GetFunctionLink());     // Guard
            Assert.Null(function.GetFeedFunctionLink()); // Guard

            FunctionLinkGenerationConvention convention = new FunctionLinkGenerationConvention();

            convention.Apply(function, builder);

            // Assert
            var functionLink = function.GetFeedFunctionLink();

            Assert.NotNull(functionLink);
            Assert.IsType <Func <ResourceSetContext, Uri> >(functionLink);

            Assert.Null(function.GetFunctionLink());
        }
        public void Apply_FollowsConventions()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            FunctionConfiguration function = new FunctionConfiguration(builder, "IgnoreAction");
            Mock<IEdmTypeConfiguration> mockBindingParameterType = new Mock<IEdmTypeConfiguration>();
            mockBindingParameterType.Setup(o => o.Kind).Returns(EdmTypeKind.Entity);
            mockBindingParameterType.Setup(o => o.ClrType).Returns(typeof(int));
            function.SetBindingParameter("IgnoreParameter", mockBindingParameterType.Object);
            FunctionLinkGenerationConvention convention = new FunctionLinkGenerationConvention();

            // Act
            convention.Apply(function, builder);

            // Assert
            Assert.True(function.FollowsConventions);
        }
        public void Apply_Doesnot_Override_UserConfiguration()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            var customers = builder.EntitySet<Customer>("Customers");
            var function = customers.EntityType.Function("MyFunction").Returns<int>();
            function.HasFunctionLink(ctx => new Uri("http://localhost/FunctionTestWorks"), followsConventions: false);
            FunctionLinkGenerationConvention convention = new FunctionLinkGenerationConvention();
            convention.Apply(function, builder);

            // Act
            IEdmModel model = builder.GetEdmModel();
            var edmCustomers = model.EntityContainer.FindEntitySet("Customers");
            var edmType = model.SchemaElements.OfType<IEdmEntityType>().First(c => c.Name == "Customer");
            var edmFunction = model.SchemaElements.OfType<IEdmFunction>().Single(f => f.Name == "MyFunction");
            Assert.NotNull(edmFunction);

            HttpConfiguration configuration = new HttpConfiguration();
            configuration.MapODataServiceRoute(model);

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

            FunctionLinkBuilder fuinctionLinkBuilder = model.GetFunctionLinkBuilder(edmFunction);

            var serializerContext = new ODataSerializerContext { Model = model, NavigationSource = edmCustomers, Url = request.GetUrlHelper() };
            var entityContext = new EntityInstanceContext(serializerContext, edmType.AsReference(), new Customer { Id = 109 });

            // Assert
            Uri link = fuinctionLinkBuilder.BuildFunctionLink(entityContext);
            Assert.Equal("http://localhost/FunctionTestWorks", link.AbsoluteUri);
        }