Example #1
0
        internal static Uri GenerateFunctionLink(this ResourceSetContext feedContext, string bindingParameterType,
                                                 string functionName, IEnumerable <string> parameterNames)
        {
            Contract.Assert(feedContext != null);

            if (feedContext.EntitySetBase is IEdmContainedEntitySet)
            {
                return(null);
            }

            if (feedContext.EdmModel == null)
            {
                return(null);
            }

            IEdmModel model = feedContext.EdmModel;

            string elementType = DeserializationHelpers.GetCollectionElementTypeName(bindingParameterType,
                                                                                     isNested: false);

            Contract.Assert(elementType != null);

            IEdmTypeReference typeReference = model.FindDeclaredType(elementType).ToEdmTypeReference(true);
            IEdmTypeReference collection    = new EdmCollectionTypeReference(new EdmCollectionType(typeReference));
            IEdmOperation     operation     = model.FindDeclaredOperations(functionName).First();

            return(feedContext.GenerateFunctionLink(collection, operation, parameterNames));
        }
Example #2
0
        internal static Uri GenerateActionLink(this ResourceSetContext resourceSetContext, IEdmTypeReference bindingParameterType,
                                               IEdmOperation action)
        {
            Contract.Assert(resourceSetContext != null);

            if (resourceSetContext.EntitySetBase is IEdmContainedEntitySet)
            {
                return(null);
            }

            IList <ODataPathSegment> actionPathSegments = new List <ODataPathSegment>();

            resourceSetContext.GenerateBaseODataPathSegmentsForFeed(actionPathSegments);

            // generate link with cast if the navigation source doesn't match the type the action is bound to.
            if (resourceSetContext.EntitySetBase.Type.FullTypeName() != bindingParameterType.FullName())
            {
                actionPathSegments.Add(new TypeSegment(bindingParameterType.Definition, resourceSetContext.EntitySetBase));
            }

            OperationSegment operationSegment = new OperationSegment(action, entitySet: null);

            actionPathSegments.Add(operationSegment);

            string actionLink = resourceSetContext.InternalUrlHelper.CreateODataLink(actionPathSegments);

            return(actionLink == null ? null : new Uri(actionLink));
        }
Example #3
0
        public void WhenFeedActionLinksNotManuallyConfigured_ConventionBasedBuilderUsesConventions()
        {
            // Arrange
            Uri expectedUri                       = new Uri("http://server/Movies/Default.Watch(param=@param)");
            ODataModelBuilder builder             = ODataConventionModelBuilderFactory.Create();
            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();

            var    configuration = RoutingConfigurationFactory.Create();
            string routeName     = "Route";

            configuration.MapODataServiceRoute(routeName, null, model);

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

            // Act
            IEdmEntityContainer container     = model.SchemaElements.OfType <IEdmEntityContainer>().SingleOrDefault();
            IEdmFunction        watchFunction = Assert.Single(model.SchemaElements.OfType <IEdmFunction>()); // Guard
            IEdmEntitySet       entitySet     = container.EntitySets().SingleOrDefault();

            ODataSerializerContextFactory.Create(model, entitySet, request);
            ResourceSetContext context = ResourceSetContextFactory.Create(entitySet, request);

            OperationLinkBuilder functionLinkBuilder = model.GetAnnotationValue <OperationLinkBuilder>(watchFunction);

            //Assert
            Assert.Equal(expectedUri, watch.GetFeedFunctionLink()(context));
            Assert.NotNull(functionLinkBuilder);
            Assert.Equal(expectedUri, functionLinkBuilder.BuildLink(context));
        }
Example #4
0
 private static void GenerateBaseODataPathSegmentsForFeed(
     this ResourceSetContext feedContext,
     IList <ODataPathSegment> odataPath)
 {
     GenerateBaseODataPathSegmentsForNonSingletons(feedContext.InternalRequest.Context.Path,
                                                   feedContext.EntitySetBase,
                                                   odataPath);
 }
        public void GenerateFunctionLinkForFeed_ThrowsArgumentNull_Function()
        {
            // Arrange
            ResourceSetContext feedContext = new ResourceSetContext();

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(() => feedContext.GenerateFunctionLink(function: null), "function");
        }
        public void GenerateActionLinkForFeed_ThrowsArgumentNull_Action()
        {
            // Arrange
            ResourceSetContext resourceSetContext = new ResourceSetContext();

            // Act & Assert
            Assert.ThrowsArgumentNull(() => resourceSetContext.GenerateActionLink(action: null), "action");
        }
        public void GenerateFunctionLinkForFeed_ThrowsArgumentNull_ResourceSetContext()
        {
            // Arrange
            ResourceSetContext resourceSetContext = null;
            IEdmFunction       function           = new Mock <IEdmFunction>().Object;

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(() => resourceSetContext.GenerateFunctionLink(function), "resourceSetContext");
        }
        public void GenerateActionLinkForFeed_ThrowsArgumentNull_FeedContext()
        {
            // Arrange
            ResourceSetContext feedContext = null;
            IEdmAction         action      = new Mock <IEdmAction>().Object;

            // Act & Assert
            Assert.ThrowsArgumentNull(() => feedContext.GenerateActionLink(action), "resourceSetContext");
        }
        /// <summary>
        /// Builds the operation link for the given feed.
        /// </summary>
        /// <param name="context">An feed context wrapping the feed instance.</param>
        /// <returns>The generated link.</returns>
        public virtual Uri BuildLink(ResourceSetContext context)
        {
            if (_feedLinkFactory == null)
            {
                return(null);
            }

            return(_feedLinkFactory(context));
        }
        public void CreateODataOperations_CreateOperations(bool followConventions)
        {
            // Arrange
            string expectedTarget = "aa://Target";
            Mock <ODataSerializerProvider> serializerProvider = new Mock <ODataSerializerProvider>();
            ODataResourceSetSerializer     serializer         = new ODataResourceSetSerializer(serializerProvider.Object);
            var builder = new ODataConventionModelBuilder();

            builder.EntitySet <FeedCustomer>("Customers");
            var       function = builder.EntityType <FeedCustomer>().Collection.Function("MyFunction").Returns <int>();
            IEdmModel model    = builder.GetEdmModel();

            IEdmEntitySet customers   = model.EntityContainer.FindEntitySet("Customers");
            IEdmFunction  edmFunction = model.SchemaElements.OfType <IEdmFunction>().First(f => f.Name == "MyFunction");

            Func <ResourceSetContext, Uri> functionLinkFactory = a => new Uri(expectedTarget);
            var operationLinkBuilder = new OperationLinkBuilder(functionLinkFactory, followConventions);

            model.SetOperationLinkBuilder(edmFunction, operationLinkBuilder);

            var request = RequestFactory.Create(method: "get", uri: "http://any", opt => opt.AddModel(model));
            ResourceSetContext resourceSetContext = new ResourceSetContext
            {
                EntitySetBase = customers,
                Request       = request,
            };

            ODataSerializerContext serializerContext = new ODataSerializerContext
            {
                NavigationSource = customers,
                Request          = request,
                Model            = model,
                MetadataLevel    = ODataMetadataLevel.Full,
            };
            string expectedMetadataPrefix = "http://any/$metadata";

            // Act
            ODataOperation actualOperation = serializer.CreateODataOperation(edmFunction, resourceSetContext, serializerContext);

            // Assert
            Assert.NotNull(actualOperation);
            string         expectedMetadata = expectedMetadataPrefix + "#Default.MyFunction";
            ODataOperation expectedFunction = new ODataFunction
            {
                Metadata = new Uri(expectedMetadata),
                Target   = new Uri(expectedTarget),
                Title    = "MyFunction"
            };

            AssertEqual(expectedFunction, actualOperation);
        }
        public void CreateODataOperations_CreateOperations(bool followConventions)
        {
            // Arrange
            // Arrange
            string expectedTarget = "aa://Target";
            ODataResourceSetSerializer serializer = new ODataResourceSetSerializer(_serializerProvider);
            var builder = new ODataConventionModelBuilder();

            builder.EntitySet <FeedCustomer>("Customers");
            var function = builder.EntityType <FeedCustomer>().Collection.Function("MyFunction").Returns <int>();

            function.HasFeedFunctionLink(a => new Uri(expectedTarget), followConventions);
            IEdmModel model = builder.GetEdmModel();

            IEdmEntitySet customers              = model.EntityContainer.FindEntitySet("Customers");
            IEdmFunction  edmFunction            = model.SchemaElements.OfType <IEdmFunction>().First(f => f.Name == "MyFunction");
            string        expectedMetadataPrefix = "http://Metadata";

            UrlHelper          url                = CreateMetadataLinkFactory(expectedMetadataPrefix);
            HttpRequestMessage request            = new HttpRequestMessage();
            ResourceSetContext resourceSetContext = new ResourceSetContext
            {
                EntitySetBase = customers,
                Request       = request,
                Url           = url
            };

            ODataSerializerContext serializerContext = new ODataSerializerContext
            {
                NavigationSource = customers,
                Request          = request,
                Model            = model,
                MetadataLevel    = ODataMetadataLevel.FullMetadata,
                Url = url
            };

            // Act
            ODataOperation actualOperation = serializer.CreateODataOperation(edmFunction, resourceSetContext, serializerContext);

            // Assert
            Assert.NotNull(actualOperation);
            string         expectedMetadata = expectedMetadataPrefix + "#Default.MyFunction";
            ODataOperation expectedFunction = new ODataFunction
            {
                Metadata = new Uri(expectedMetadata),
                Target   = new Uri(expectedTarget),
                Title    = "MyFunction"
            };

            AssertEqual(expectedFunction, actualOperation);
        }
        public void GenerateFunctionLinkForFeed_ThrowsFunctionNotBoundToCollectionOfEntity_IfFunctionHasNoParameters()
        {
            // Arrange
            ResourceSetContext  context  = new ResourceSetContext();
            Mock <IEdmFunction> function = new Mock <IEdmFunction>();

            function.Setup(a => a.Parameters).Returns(Enumerable.Empty <IEdmOperationParameter>());
            function.Setup(a => a.Name).Returns("SomeFunction");

            // Act & Assert
            ExceptionAssert.ThrowsArgument(
                () => context.GenerateFunctionLink(function.Object),
                "function",
                "The function 'SomeFunction' is not bound to the collection of entity. Only functions that are bound to entities can have function links.");
        }
Example #13
0
        public void GenerateFunctionLinkForFeed_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType()
        {
            // Arrange
            var          request  = RequestFactory.Create(_model);
            IEdmFunction function = _model.SchemaElements.OfType <IEdmFunction>().First(a => a.Name == "IsAllUpgraded");

            Assert.NotNull(function); // Guard
            ResourceSetContext context = new ResourceSetContext {
                EntitySetBase = _customers, Request = request
            };

            // Act
            Uri link = context.GenerateFunctionLink(function);

            Assert.Equal("http://localhost/Customers/NS.IsAllUpgraded(param=@param)", link.AbsoluteUri);
        }
Example #14
0
        public void GenerateActionLinkForFeed_GeneratesLinkWithCast_IfEntitySetTypeDoesnotMatchActionEntityType()
        {
            // Arrange
            var        request = RequestFactory.Create(_model);
            IEdmAction action  = _model.SchemaElements.OfType <IEdmAction>().First(a => a.Name == "UpgradeSpecialAll");

            Assert.NotNull(action); // Guard
            ResourceSetContext context = new ResourceSetContext {
                EntitySetBase = _customers, Request = request
            };

            // Act
            Uri link = context.GenerateActionLink(action);

            // Assert
            Assert.Equal("http://localhost/Customers/NS.SpecialCustomer/NS.UpgradeSpecialAll", link.AbsoluteUri);
        }
        public void BuildLinkOperationLinkBuilder_Works_ResourceSetContext()
        {
            // Arrange
            Uri uri = new Uri("http://any");
            Func <ResourceSetContext, Uri> linkFactory = r => uri;
            OperationLinkBuilder           linkBuilder = new OperationLinkBuilder(linkFactory, false);

            // Act & Assert
            ResourceContext context = new ResourceContext();

            Assert.Null(linkBuilder.BuildLink(context));

            // Act & Assert
            ResourceSetContext setContext = new ResourceSetContext();

            Assert.Same(uri, linkBuilder.BuildLink(setContext));
        }
Example #16
0
        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);
        }
Example #17
0
        public void CreateODataOperation_ThrowsArgumentNull_ForInputParameters()
        {
            // Arrange & Act & Assert
            Mock <ODataSerializerProvider> serializerProvider = new Mock <ODataSerializerProvider>();
            ODataResourceSetSerializer     serializer         = new ODataResourceSetSerializer(serializerProvider.Object);

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(() => serializer.CreateODataOperation(null, null, null), "operation");

            // Act & Assert
            IEdmOperation operation = new Mock <IEdmOperation>().Object;

            ExceptionAssert.ThrowsArgumentNull(() => serializer.CreateODataOperation(operation, null, null), "resourceSetContext");

            // Act & Assert
            ResourceSetContext context = new ResourceSetContext();

            ExceptionAssert.ThrowsArgumentNull(() => serializer.CreateODataOperation(operation, context, null), "writeContext");
        }
Example #18
0
        public void GenerateFunctionLinkForFeed_GeneratesLinkWithDownCast_IfElementTypeDerivesFromBindingParameterType()
        {
            // Arrange
            var          request  = RequestFactory.Create(_model);
            IEdmFunction function = _model.SchemaElements.OfType <IEdmFunction>().First(a => a.Name == "IsAllUpgraded");

            Assert.NotNull(function); // Guard
            IEdmEntitySet specialCustomers = new EdmEntitySet(_model.EntityContainer, "SpecialCustomers", _specialCustomer);

            ResourceSetContext context = new ResourceSetContext {
                EntitySetBase = specialCustomers, Request = request
            };

            // Act
            Uri link = context.GenerateFunctionLink(function);

            // Assert
            Assert.Equal("http://localhost/SpecialCustomers/NS.Customer/NS.IsAllUpgraded(param=@param)", link.AbsoluteUri);
        }
        public void GenerateFunctionLinkForFeed_GeneratesLinkWithCast_IfEntitySetTypeDoesnotMatchActionEntityType()
        {
            // Arrange
            HttpRequestMessage request  = GetODataRequest(_model.Model);
            IEdmFunction       function = _model.Model.SchemaElements.OfType <IEdmFunction>().First(a => a.Name == "IsSpecialAllUpgraded");

            Assert.NotNull(function); // Guard
            var context = new ResourceSetContext
            {
                Request       = request,
                EntitySetBase = _model.Customers,
                Url           = request.GetUrlHelper(),
            };

            // Act
            Uri link = context.GenerateFunctionLink(function);

            // Assert
            Assert.Equal("http://localhost/Customers/NS.SpecialCustomer/NS.IsSpecialAllUpgraded(param=@param)", link.AbsoluteUri);
        }
        public void GenerateActionLinkForFeed_GeneratesLinkWithoutCast_IfEntitySetTypeMatchesActionEntityType()
        {
            // Arrange
            HttpRequestMessage request = GetODataRequest(_model.Model);
            IEdmAction         action  = _model.Model.SchemaElements.OfType <IEdmAction>().First(a => a.Name == "UpgradeAll");

            Assert.NotNull(action); // Guard

            var context = new ResourceSetContext
            {
                Request       = request,
                EntitySetBase = _model.Customers,
                Url           = request.GetUrlHelper(),
            };

            // Act
            Uri link = context.GenerateActionLink(action);

            Assert.Equal("http://localhost/Customers/NS.UpgradeAll", link.AbsoluteUri);
        }
Example #21
0
        public void CreateODataOperation_OmitsOperations_WhenNonFullMetadata(ODataMetadataLevel metadataLevel)
        {
            // Arrange
            ODataResourceSetSerializer serializer = new ODataResourceSetSerializer(_serializerProvider);

            IEdmTypeReference returnType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Boolean, isNullable: false);
            IEdmFunction      function   = new EdmFunction("NS", "Function", returnType, isBound: true, entitySetPathExpression: null, isComposable: false);

            ResourceSetContext     resourceSetContext = new ResourceSetContext();
            ODataSerializerContext serializerContext  = new ODataSerializerContext
            {
                MetadataLevel = metadataLevel
            };
            // Act

            ODataOperation operation = serializer.CreateODataOperation(function, resourceSetContext, serializerContext);

            // Assert
            Assert.Null(operation);
        }
        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 GenerateFunctionLinkForFeed_GeneratesLinkWithDownCast_IfElementTypeDerivesFromBindingParameterType()
        {
            // Arrange
            HttpRequestMessage request  = GetODataRequest(_model.Model);
            IEdmFunction       function = _model.Model.SchemaElements.OfType <IEdmFunction>().First(a => a.Name == "IsAllUpgraded");

            Assert.NotNull(function); // Guard
            IEdmEntitySet specialCustomers = new EdmEntitySet(_model.Container, "SpecialCustomers", _model.SpecialCustomer);

            var context = new ResourceSetContext
            {
                Request       = request,
                EntitySetBase = specialCustomers,
                Url           = request.GetUrlHelper(),
            };

            // Act
            Uri link = context.GenerateFunctionLink(function);

            // Assert
            Assert.Equal("http://localhost/SpecialCustomers/NS.Customer/NS.IsAllUpgraded(param=@param)", link.AbsoluteUri);
        }
Example #24
0
        /// <summary>
        /// Generates an action link following the OData URL conventions for the action <paramref name="action"/> and bound to the
        /// collection of entity represented by <paramref name="resourceSetContext"/>.
        /// </summary>
        /// <param name="resourceSetContext">The <see cref="ResourceSetContext"/> representing the feed for which the action link needs to be generated.</param>
        /// <param name="action">The action for which the action link needs to be generated.</param>
        /// <returns>The generated action link following OData URL conventions.</returns>
        public static Uri GenerateActionLink(this ResourceSetContext resourceSetContext, IEdmOperation action)
        {
            if (resourceSetContext == null)
            {
                throw Error.ArgumentNull("resourceSetContext");
            }

            if (action == null)
            {
                throw Error.ArgumentNull("action");
            }

            IEdmOperationParameter bindingParameter = action.Parameters.FirstOrDefault();

            if (bindingParameter == null ||
                !bindingParameter.Type.IsCollection() ||
                !((IEdmCollectionType)bindingParameter.Type.Definition).ElementType.IsEntity())
            {
                throw Error.Argument("action", SRResources.ActionNotBoundToCollectionOfEntity, action.Name);
            }

            return(GenerateActionLink(resourceSetContext, bindingParameter.Type, action));
        }
Example #25
0
        internal static Uri GenerateFunctionLink(this ResourceSetContext resourceSetContext, IEdmTypeReference bindingParameterType,
                                                 IEdmOperation functionImport, IEnumerable <string> parameterNames)
        {
            Contract.Assert(resourceSetContext != null);

            if (resourceSetContext.EntitySetBase is IEdmContainedEntitySet)
            {
                return(null);
            }

            IList <ODataPathSegment> functionPathSegments = new List <ODataPathSegment>();

            resourceSetContext.GenerateBaseODataPathSegmentsForFeed(functionPathSegments);

            // generate link with cast if the navigation source type doesn't match the entity type the function is bound to.
            if (resourceSetContext.EntitySetBase.Type.FullTypeName() != bindingParameterType.Definition.FullTypeName())
            {
                functionPathSegments.Add(new TypeSegment(bindingParameterType.Definition, null));
            }

            IList <OperationSegmentParameter> parameters = new List <OperationSegmentParameter>();

            // skip the binding parameter
            foreach (string param in parameterNames.Skip(1))
            {
                string value = "@" + param;
                parameters.Add(new OperationSegmentParameter(param, new ConstantNode(value, value)));
            }

            OperationSegment segment = new OperationSegment(new[] { functionImport }, parameters, null);

            functionPathSegments.Add(segment);

            string functionLink = resourceSetContext.InternalUrlHelper.CreateODataLink(functionPathSegments);

            return(functionLink == null ? null : new Uri(functionLink));
        }
        public void Convention_GeneratesUri_ForFunctionBoundToCollectionOfEntity()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet <Customer>("Customers");
            var function = builder.EntityType <Customer>().Collection.Function("MyFunction").Returns <int>();

            function.Parameter <string>("param");
            IEdmModel model = builder.GetEdmModel();

            // Act
            HttpConfiguration configuration = new HttpConfiguration();

            configuration.MapODataServiceRoute("odata", "odata", model);

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://*****:*****@param)",
                         link.AbsoluteUri);
        }
        /// <summary>
        /// Generates a function link following the OData URL conventions for the function <paramref name="function"/> and bound to the
        /// collection of entity represented by <paramref name="resourceSetContext"/>.
        /// </summary>
        /// <param name="resourceSetContext">The <see cref="ResourceSetContext"/> representing the feed for which the function link needs to be generated.</param>
        /// <param name="function">The function for which the function link needs to be generated.</param>
        /// <returns>The generated function link following OData URL conventions.</returns>
        public static Uri GenerateFunctionLink(this ResourceSetContext resourceSetContext, IEdmOperation function)
        {
            if (resourceSetContext == null)
            {
                throw Error.ArgumentNull("resourceSetContext");
            }

            if (function == null)
            {
                throw Error.ArgumentNull("function");
            }

            IEdmOperationParameter bindingParameter = function.Parameters.FirstOrDefault();

            if (bindingParameter == null ||
                !bindingParameter.Type.IsCollection() ||
                !((IEdmCollectionType)bindingParameter.Type.Definition).ElementType.IsEntity())
            {
                throw Error.Argument("function", "TODO: " /*SRResources.FunctionNotBoundToCollectionOfEntity, function.Name*/);
            }

            return(GenerateFunctionLink(resourceSetContext, bindingParameter.Type, function,
                                        function.Parameters.Select(p => p.Name)));
        }
Example #28
0
        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
            OperationLinkBuilder functionLinkBuilder = model.GetAnnotationValue <OperationLinkBuilder>(rewardFuntion);
            ResourceSetContext   context             = new ResourceSetContext();

            //Assert
            Assert.Equal(expectedUri, reward.GetFeedFunctionLink()(context));
            Assert.NotNull(functionLinkBuilder);
            Assert.Equal(expectedUri, functionLinkBuilder.BuildLink(context));
        }
Example #29
0
        private IEnumerable <ODataOperation> CreateODataOperations(IEnumerable <IEdmOperation> operations, ResourceSetContext resourceSetContext, ODataSerializerContext writeContext)
        {
            Contract.Assert(operations != null);
            Contract.Assert(resourceSetContext != null);
            Contract.Assert(writeContext != null);

            foreach (IEdmOperation operation in operations)
            {
                ODataOperation oDataOperation = CreateODataOperation(operation, resourceSetContext, writeContext);
                if (oDataOperation != null)
                {
                    yield return(oDataOperation);
                }
            }
        }
Example #30
0
        public virtual ODataOperation CreateODataOperation(IEdmOperation operation, ResourceSetContext resourceSetContext, ODataSerializerContext writeContext)
        {
            if (operation == null)
            {
                throw Error.ArgumentNull("operation");
            }

            if (resourceSetContext == null)
            {
                throw Error.ArgumentNull("resourceSetContext");
            }

            if (writeContext == null)
            {
                throw Error.ArgumentNull("writeContext");
            }

            ODataMetadataLevel metadataLevel = writeContext.MetadataLevel;
            IEdmModel          model         = writeContext.Model;

            if (metadataLevel != ODataMetadataLevel.FullMetadata)
            {
                return(null);
            }

            OperationLinkBuilder builder;

            builder = model.GetOperationLinkBuilder(operation);

            if (builder == null)
            {
                return(null);
            }

            Uri target = builder.BuildLink(resourceSetContext);

            if (target == null)
            {
                return(null);
            }

            Uri baseUri  = new Uri(writeContext.Url.CreateODataLink(MetadataSegment.Instance));
            Uri metadata = new Uri(baseUri, "#" + operation.FullName());

            ODataOperation odataOperation;
            IEdmAction     action = operation as IEdmAction;

            if (action != null)
            {
                odataOperation = new ODataAction();
            }
            else
            {
                odataOperation = new ODataFunction();
            }
            odataOperation.Metadata = metadata;

            // Always omit the title in minimal/no metadata modes.
            ODataResourceSerializer.EmitTitle(model, operation, odataOperation);

            // Omit the target in minimal/no metadata modes unless it doesn't follow conventions.
            if (metadataLevel == ODataMetadataLevel.FullMetadata || !builder.FollowsConventions)
            {
                odataOperation.Target = target;
            }

            return(odataOperation);
        }