コード例 #1
0
        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
            };

            EntityInstanceContext context = new EntityInstanceContext(serializerContext, customerType.AsReference(), new Customer {
                CustomerId = 1
            });
            IEdmAction        rewardAction      = Assert.Single(model.SchemaElements.OfType <IEdmAction>()); // Guard
            ActionLinkBuilder actionLinkBuilder = model.GetAnnotationValue <ActionLinkBuilder>(rewardAction);

            //Assert
            Assert.Equal(expectedUri, reward.GetActionLink()(context));
            Assert.NotNull(actionLinkBuilder);
            Assert.Equal(expectedUri, actionLinkBuilder.BuildActionLink(context));
        }
コード例 #2
0
        public void ActionLink_PreservesFollowsConventions(bool value)
        {
            // Arrange
            ODataModelBuilder            builder                  = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();
            ActionConfiguration          configuration            = new ActionConfiguration(builder, "IgnoreAction");
            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.HasActionLink((a) => { throw new NotImplementedException(); }, followsConventions: value);
            builder.AddOperation(configuration);
            builder.AddEntityType(entityType);

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            var action = Assert.Single(model.SchemaElements.OfType <IEdmAction>());
            OperationLinkBuilder actionLinkBuilder = model.GetOperationLinkBuilder(action);

            Assert.NotNull(actionLinkBuilder);
            Assert.Equal(value, actionLinkBuilder.FollowsConventions);
        }
コード例 #3
0
        public void HasActionLink_ThrowsException_OnNonBindableActions()
        {
            // Arrange
            ODataModelBuilder   builder = new ODataModelBuilder();
            ActionConfiguration action  = builder.Action("NoBindableAction");

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => action.HasActionLink(ctx => new Uri("http://any"), followsConventions: false),
                "To register an action link factory, actions must be bindable to a single entity. " +
                "Action 'NoBindableAction' does not meet this requirement.");
        }
コード例 #4
0
        public void HasActionLink_SetsFollowsConventions(bool value)
        {
            // Arrange
            ODataModelBuilder            builder = new ODataModelBuilder();
            ActionConfiguration          action  = new ActionConfiguration(builder, "IgnoreAction");
            Mock <IEdmTypeConfiguration> bindingParameterTypeMock = new Mock <IEdmTypeConfiguration>();

            bindingParameterTypeMock.Setup(o => o.Kind).Returns(EdmTypeKind.Entity);
            IEdmTypeConfiguration bindingParameterType = bindingParameterTypeMock.Object;

            action.SetBindingParameter("IgnoreParameter", bindingParameterType, alwaysBindable: false);

            // Act
            action.HasActionLink((a) => { throw new NotImplementedException(); }, followsConventions: value);

            // Assert
            Assert.Equal(value, action.FollowsConventions);
        }