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

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

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

            // Assert
            Assert.True(action.FollowsConventions);
        }
        public void Apply_SetsOperationLinkBuilder_OnlyIfActionIsBindable()
        {
            // Arrange
            ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create();

            builder.EntitySet <Customer>("Customers");
            var paintAction = builder.Action("Paint");
            ActionLinkGenerationConvention convention = new ActionLinkGenerationConvention();

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

            // Assert
            IEdmModel model          = builder.GetEdmModel();
            var       paintEdmAction = model.EntityContainer.Elements.OfType <IEdmActionImport>().Single();

            OperationLinkBuilder actionLinkBuilder = model.GetAnnotationValue <OperationLinkBuilder>(paintEdmAction);

            Assert.Null(actionLinkBuilder);
        }
        public void Apply_SetsOperationLinkBuilder_OnlyIfFunctionIsBindable()
        {
            // Arrange
            ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create();
            var function = builder.Function("MyFunction").Returns <int>();
            ActionLinkGenerationConvention convention = new ActionLinkGenerationConvention();

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

            // Assert
            IEdmModel model       = builder.GetEdmModel();
            var       edmFunction = model.EntityContainer.Elements.OfType <IEdmFunctionImport>().Single();

            Assert.NotNull(edmFunction);

            OperationLinkBuilder linkBuilder = model.GetAnnotationValue <OperationLinkBuilder>(edmFunction);

            Assert.Null(linkBuilder);
        }
        public void Apply_SetOperationLinkBuilder_ForActionBoundToEntity()
        {
            // Arrange
            ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create();
            var action = builder.EntityType <Customer>().Action("MyAction");

            // Act
            Assert.Null(action.GetActionLink());     // Guard
            Assert.Null(action.GetFeedActionLink()); // Guard

            ActionLinkGenerationConvention convention = new ActionLinkGenerationConvention();

            convention.Apply(action, builder);

            // Assert
            var actionLink = action.GetActionLink();

            Assert.NotNull(actionLink);
            Assert.IsType <Func <ResourceContext, Uri> >(actionLink);

            Assert.Null(action.GetFeedActionLink());
        }
        public void Apply_Doesnot_Override_UserConfiguration()
        {
            // Arrange
            ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create();
            var customers   = builder.EntitySet <Customer>("Customers");
            var paintAction = customers.EntityType.Action("Paint");

            paintAction.HasActionLink(ctxt => new Uri("http://localhost/ActionTestWorks"), followsConventions: false);
            ActionLinkGenerationConvention convention = new ActionLinkGenerationConvention();

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

            IEdmModel model        = builder.GetEdmModel();
            var       edmCustomers = model.EntityContainer.FindEntitySet("Customers");
            var       edmCustomer  = model.SchemaElements.OfType <IEdmEntityType>().First(c => c.Name == "Customer");
            var       edmAction    = model.SchemaElements.OfType <IEdmAction>().First(a => a.Name == "Paint");

            Assert.NotNull(edmAction);

            string routeName     = "OData";
            var    configuration = RoutingConfigurationFactory.CreateWithRootContainer(routeName);

            configuration.MapODataServiceRoute(routeName, null, model);

            var request = RequestFactory.Create(HttpMethod.Get, "http://localhost", configuration, routeName);

            OperationLinkBuilder actionLinkBuilder = model.GetOperationLinkBuilder(edmAction);

            var serializerContext = ODataSerializerContextFactory.Create(model, edmCustomers, request);
            var entityContext     = new ResourceContext(serializerContext, edmCustomer.AsReference(), new Customer {
                Id = 2009
            });

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

            Assert.Equal("http://localhost/ActionTestWorks", link.AbsoluteUri);
        }
        public void Apply_WorksFor_ActionBoundToCollectionOfEntity()
        {
            // Arrange
            ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create();
            var action = builder.EntityType <Customer>().Collection.Action("MyFunction").Returns <int>();

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

            // Act
            Assert.Null(action.GetActionLink());     // Guard
            Assert.Null(action.GetFeedActionLink()); // Guard

            ActionLinkGenerationConvention convention = new ActionLinkGenerationConvention();

            convention.Apply(action, builder);

            // Assert
            var actionLink = action.GetFeedActionLink();

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

            Assert.Null(action.GetActionLink());
        }