public void TryMatch_ReturnTrue_IfSameFunction()
        {
            // Arrange
            IEdmEntityType returnType = new Mock<IEdmEntityType>().Object;
            EdmFunction function = new EdmFunction("NS", "Function", new EdmEntityTypeReference(returnType, isNullable: false));

            Dictionary<string, string> parameterValues = new Dictionary<string, string>()
            {
                { "Parameter1", "1" },
                { "Parameter2", "2" }
            };

            Dictionary<string, string> parameterMappings = new Dictionary<string, string>()
            {
                { "Parameter1", "{param1}" },
                { "Parameter2", "{param2}" }
            };

            BoundFunctionPathSegment segment = new BoundFunctionPathSegment(function, model: null, parameterValues: parameterValues);
            BoundFunctionPathSegmentTemplate template = new BoundFunctionPathSegmentTemplate(
                new BoundFunctionPathSegment(function, model: null, parameterValues: parameterMappings));

            // Act
            Dictionary<string, object> values = new Dictionary<string,object>();
            bool result = template.TryMatch(segment, values);

            // Assert
            Assert.True(result);
            Assert.Equal(2, values.Count);
            Assert.Equal("1", values["param1"]);
            Assert.Equal("2", values["param2"]);
        }
        public void Property_SegmentKind_IsEntitySet()
        {
            // Arrange
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            BoundFunctionPathSegment segment = new BoundFunctionPathSegment("function", parameters);

            // Act & Assert
            Assert.Equal(ODataSegmentKinds.Function, segment.SegmentKind);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="BoundFunctionPathSegmentTemplate"/> class.
        /// </summary>
        /// <param name="function">The function segment to be templatized</param>
        public BoundFunctionPathSegmentTemplate(BoundFunctionPathSegment function)
        {
            if (function == null)
            {
                throw Error.ArgumentNull("function");
            }

            FunctionName = function.FunctionName;
            ParameterMappings = KeyValuePathSegmentTemplate.BuildParameterMappings(function.Values, function.ToString());
        }
        public void GetEdmType_Returns_FunctionReturnType()
        {
            // Arrange
            IEdmEntityType returnType = new Mock<IEdmEntityType>().Object;
            EdmFunction function = new EdmFunction("NS", "Function", new EdmEntityTypeReference(returnType, isNullable: false));
            BoundFunctionPathSegment segment = new BoundFunctionPathSegment(function, model: null, parameterValues: null);

            // Act
            var result = segment.GetEdmType(returnType);

            // Assert
            Assert.Same(returnType, result);
        }
예제 #5
0
        public void GetEdmType_Returns_FunctionReturnType()
        {
            // Arrange
            IEdmEntityType           returnType = new Mock <IEdmEntityType>().Object;
            EdmFunction              function   = new EdmFunction("NS", "Function", new EdmEntityTypeReference(returnType, isNullable: false));
            BoundFunctionPathSegment segment    = new BoundFunctionPathSegment(function, model: null, parameterValues: null);

            // Act
            var result = segment.GetEdmType(returnType);

            // Assert
            Assert.Same(returnType, result);
        }
        public void Ctor_TakingFunction_InitializesFunctionNameProperty()
        {
            // Arrange
            IEdmModel model = new EdmModel();
            Mock<IEdmFunction> edmFunction = new Mock<IEdmFunction>();
            edmFunction.Setup(a => a.Namespace).Returns("NS");
            edmFunction.Setup(a => a.Name).Returns("Function");

            // Act
            BoundFunctionPathSegment functionPathSegment = new BoundFunctionPathSegment(edmFunction.Object, model, null);

            // Assert
            Assert.Equal("NS.Function", functionPathSegment.FunctionName);
        }
        /// <inheritdoc />
        public override bool TryMatch(ODataPathSegment pathSegment, IDictionary <string, object> values)
        {
            if (pathSegment.SegmentKind == ODataSegmentKinds.Function)
            {
                BoundFunctionPathSegment functionSegment = (BoundFunctionPathSegment)pathSegment;
                if (FunctionName == functionSegment.FunctionName)
                {
                    var enumNames = functionSegment.Function.Parameters.Where(p => p.Type.IsEnum()).Select(p => p.Name);
                    return(KeyValuePathSegmentTemplate.TryMatch(ParameterMappings, functionSegment.Values, values, enumNames));
                }
            }

            return(false);
        }
예제 #8
0
        public void GetNavigationSource_Returns_FunctionTargetNavigationSource_EntitySetPathExpression()
        {
            // Arrange
            IEdmModel     model                    = GetEdmModel();
            IEdmFunction  function                 = model.SchemaElements.OfType <IEdmFunction>().First(c => c.Name == "GetMyOrders1");
            IEdmEntitySet previouseEntitySet       = model.EntityContainer.FindEntitySet("MyCustomers");
            IEdmEntitySet expectedEntitySet        = model.EntityContainer.FindEntitySet("MyOrders");
            Dictionary <string, string> parameters = new Dictionary <string, string>();

            // Act
            BoundFunctionPathSegment segment = new BoundFunctionPathSegment(function, model, parameters);

            // Assert
            Assert.Same(expectedEntitySet, segment.GetNavigationSource(previouseEntitySet));
        }
        public void GetNavigationSource_Returns_FunctionTargetNavigationSource()
        {
            // Arrange
            Mock <IEdmNavigationSource> targetNavigationSource = new Mock <IEdmNavigationSource>();
            Mock <IEdmFunction>         edmFuncton             = new Mock <IEdmFunction>();

            edmFuncton.Setup(a => a.Namespace).Returns("NS");
            edmFuncton.Setup(a => a.Name).Returns("Funtion");

            // Act
            BoundFunctionPathSegment segment = new BoundFunctionPathSegment(edmFuncton.Object, null, null);

            // Assert
            Assert.Same(targetNavigationSource.Object, segment.GetNavigationSource(targetNavigationSource.Object));
        }
        public void Ctor_TakingFunction_InitializesFunctionNameProperty()
        {
            // Arrange
            IEdmModel           model       = new EdmModel();
            Mock <IEdmFunction> edmFunction = new Mock <IEdmFunction>();

            edmFunction.Setup(a => a.Namespace).Returns("NS");
            edmFunction.Setup(a => a.Name).Returns("Function");

            // Act
            BoundFunctionPathSegment functionPathSegment = new BoundFunctionPathSegment(edmFunction.Object, model, null);

            // Assert
            Assert.Equal("NS.Function", functionPathSegment.FunctionName);
        }
        public void ToString_ReturnsSameString_Function()
        {
            // Arrange
            Dictionary <string, string> parameters = new Dictionary <string, string>();

            parameters.Add("Id", "123");
            parameters.Add("Name", "John");
            BoundFunctionPathSegment segment = new BoundFunctionPathSegment("function", parameters);

            // Act
            string actual = segment.ToString();

            // Assert
            Assert.Equal("function(Id=123,Name=John)", actual);
        }
        public void TryMatch_ReturnsTrue_IfSameFunction()
        {
            // Arrange
            IEdmEntityType returnType = new Mock <IEdmEntityType>().Object;
            EdmFunction    function   = new EdmFunction("NS", "Function", new EdmEntityTypeReference(returnType, isNullable: false));

            BoundFunctionPathSegment template = new BoundFunctionPathSegment(function, model: null, parameterValues: null);
            BoundFunctionPathSegment segment  = new BoundFunctionPathSegment(function, model: null, parameterValues: null);

            // Act
            Dictionary <string, object> values = new Dictionary <string, object>();
            bool result = template.TryMatch(segment, values);

            // Assert
            Assert.True(result);
            Assert.Empty(values);
        }
예제 #13
0
        private static BoundFunctionPathSegment TryMatchBoundFunctionCall(string segment, Queue <string> segments, IEdmModel model,
                                                                          IEdmType bindingType)
        {
            Contract.Assert(model != null);
            Contract.Assert(bindingType != null);

            string nextSegment = segments.Count > 0 ? segments.Peek() : null;
            IEnumerable <IEdmOperation> matchedOperations = model.FindMatchedOperations(segment, bindingType);
            IEnumerable <IEdmFunction>  possibleFunctions = matchedOperations.OfType <IEdmFunction>();

            if (possibleFunctions.Count() == 0)
            {
                return(null);
            }

            IEdmEntityType currentBindingType;

            if (bindingType.TypeKind == EdmTypeKind.Collection)
            {
                currentBindingType = (IEdmEntityType)(((IEdmCollectionType)bindingType).ElementType.Definition);
            }
            else
            {
                currentBindingType = (IEdmEntityType)bindingType;
            }

            while (currentBindingType != null)
            {
                IEnumerable <IEdmFunction> matchedFunctions = possibleFunctions.Where(f => FunctionResolver.IsBoundTo(f, currentBindingType));
                BoundFunctionPathSegment   functionSegment  = FunctionResolver.TryResolveBound(matchedFunctions, model, nextSegment);
                if (functionSegment != null)
                {
                    if (FunctionResolver.IsEnclosedInParentheses(nextSegment))
                    {
                        segments.Dequeue();
                    }

                    return(functionSegment);
                }

                currentBindingType = currentBindingType.BaseEntityType();
            }

            return(null);
        }
예제 #14
0
        public void TryMatch_ReturnTrue_IfSameFunction()
        {
            // Arrange
            IEdmModel      model      = new Mock <IEdmModel>().Object;
            IEdmEntityType returnType = new Mock <IEdmEntityType>().Object;
            EdmFunction    function   = new EdmFunction("NS", "Function", new EdmEntityTypeReference(returnType, isNullable: false));

            function.AddParameter("Parameter1", EdmCoreModel.Instance.GetInt32(isNullable: false));
            function.AddParameter("Parameter2", EdmCoreModel.Instance.GetInt32(isNullable: false));

            Dictionary <string, string> parameterValues = new Dictionary <string, string>()
            {
                { "Parameter1", "1" },
                { "Parameter2", "2" }
            };

            Dictionary <string, string> parameterMappings = new Dictionary <string, string>()
            {
                { "Parameter1", "{param1}" },
                { "Parameter2", "{param2}" }
            };

            BoundFunctionPathSegment         segment  = new BoundFunctionPathSegment(function, model, parameterValues: parameterValues);
            BoundFunctionPathSegmentTemplate template = new BoundFunctionPathSegmentTemplate(
                new BoundFunctionPathSegment(function, model, parameterValues: parameterMappings));

            // Act
            Dictionary <string, object> values = new Dictionary <string, object>();
            bool result = template.TryMatch(segment, values);

            // Assert
            Assert.True(result);
            Assert.Equal(4, values.Count);
            Assert.Equal("1", values["param1"]);
            Assert.Equal("2", values["param2"]);

            Assert.Equal(1, (values[ODataParameterValue.ParameterValuePrefix + "param1"] as ODataParameterValue).Value);
            Assert.Equal(2, (values[ODataParameterValue.ParameterValuePrefix + "param2"] as ODataParameterValue).Value);
        }
        public void TryMatch_ReturnTrue_IfSameFunction()
        {
            // Arrange
            IEdmModel model = new Mock<IEdmModel>().Object;
            IEdmEntityType returnType = new Mock<IEdmEntityType>().Object;
            EdmFunction function = new EdmFunction("NS", "Function", new EdmEntityTypeReference(returnType, isNullable: false));
            function.AddParameter("Parameter1", EdmCoreModel.Instance.GetInt32(isNullable: false));
            function.AddParameter("Parameter2", EdmCoreModel.Instance.GetInt32(isNullable: false));

            Dictionary<string, string> parameterValues = new Dictionary<string, string>()
            {
                { "Parameter1", "1" },
                { "Parameter2", "2" }
            };

            Dictionary<string, string> parameterMappings = new Dictionary<string, string>()
            {
                { "Parameter1", "{param1}" },
                { "Parameter2", "{param2}" }
            };

            BoundFunctionPathSegment segment = new BoundFunctionPathSegment(function, model, parameterValues: parameterValues);
            BoundFunctionPathSegmentTemplate template = new BoundFunctionPathSegmentTemplate(
                new BoundFunctionPathSegment(function, model, parameterValues: parameterMappings));

            // Act
            Dictionary<string, object> values = new Dictionary<string,object>();
            bool result = template.TryMatch(segment, values);

            // Assert
            Assert.True(result);
            Assert.Equal(4, values.Count);
            Assert.Equal("1", values["param1"]);
            Assert.Equal("2", values["param2"]);

            Assert.Equal(1, (values[ODataParameterValue.ParameterValuePrefix + "param1"] as ODataParameterValue).Value);
            Assert.Equal(2, (values[ODataParameterValue.ParameterValuePrefix + "param2"] as ODataParameterValue).Value);
        }
예제 #16
0
        /// <summary>
        /// Parses the next OData path segment following an entity.
        /// </summary>
        /// <param name="model">The model to use for path parsing.</param>
        /// <param name="previous">The previous path segment.</param>
        /// <param name="previousEdmType">The EDM type of the OData path up to the previous segment.</param>
        /// <param name="segment">The value of the segment to parse.</param>
        /// <param name="segments">The queue of pending segments.</param>
        /// <returns>A parsed representation of the segment.</returns>
        protected virtual ODataPathSegment ParseAtEntity(IEdmModel model, ODataPathSegment previous,
                                                         IEdmType previousEdmType, string segment, Queue <string> segments)
        {
            if (previous == null)
            {
                throw Error.ArgumentNull("previous");
            }
            if (segments == null)
            {
                throw Error.ArgumentNull("segments");
            }
            if (String.IsNullOrEmpty(segment))
            {
                throw Error.Argument(SRResources.SegmentNullOrEmpty);
            }
            IEdmEntityType previousType = previousEdmType as IEdmEntityType;

            if (previousType == null)
            {
                throw Error.Argument(SRResources.PreviousSegmentMustBeEntityType, previousEdmType);
            }

            // first look for navigation properties
            IEdmNavigationProperty navigation = previousType.NavigationProperties().SingleOrDefault(np => np.Name == segment);

            if (navigation != null)
            {
                return(new NavigationPathSegment(navigation));
            }

            // next look for properties
            IEdmProperty property = previousType.Properties().SingleOrDefault(p => p.Name == segment);

            if (property != null)
            {
                return(new PropertyAccessPathSegment(property));
            }

            // next look for type casts
            IEdmEntityType castType = model.FindDeclaredType(segment) as IEdmEntityType;

            if (castType != null)
            {
                if (!castType.IsOrInheritsFrom(previousType) && !previousType.IsOrInheritsFrom(castType))
                {
                    throw new ODataException(Error.Format(SRResources.InvalidCastInPath, castType, previousType));
                }
                return(new CastPathSegment(castType));
            }

            // look for $ref
            if (segment == ODataSegmentKinds.Ref)
            {
                return(new RefPathSegment());
            }

            // finally look for bindable procedures
            IEdmAction action = model.FindAction(segment, previousType);

            if (action != null)
            {
                return(new BoundActionPathSegment(action));
            }

            // Try to match this to a function call
            BoundFunctionPathSegment pathSegment = TryMatchBoundFunctionCall(segment, segments, model, bindingType: previousType);

            if (pathSegment != null)
            {
                return(pathSegment);
            }

            // Treating as an open property
            return(new UnresolvedPathSegment(segment));
        }
        public void GetNavigationSource_Returns_FunctionTargetNavigationSource()
        {
            // Arrange
            Mock<IEdmNavigationSource> targetNavigationSource = new Mock<IEdmNavigationSource>();
            Mock<IEdmFunction> edmFuncton = new Mock<IEdmFunction>();
            edmFuncton.Setup(a => a.Namespace).Returns("NS");
            edmFuncton.Setup(a => a.Name).Returns("Funtion");

            // Act
            BoundFunctionPathSegment segment = new BoundFunctionPathSegment(edmFuncton.Object, null, null);

            // Assert
            Assert.Same(targetNavigationSource.Object, segment.GetNavigationSource(targetNavigationSource.Object));
        }
        public void GetNavigationSource_Returns_FunctionTargetNavigationSource_EntitySetPathExpression()
        {
            // Arrange
            IEdmModel model = GetEdmModel();
            IEdmFunction function = model.SchemaElements.OfType<IEdmFunction>().First(c => c.Name == "GetMyOrders1");
            IEdmEntitySet previouseEntitySet = model.EntityContainer.FindEntitySet("MyCustomers");
            IEdmEntitySet expectedEntitySet = model.EntityContainer.FindEntitySet("MyOrders");
            Dictionary<string, string> parameters = new Dictionary<string, string>();

            // Act
            BoundFunctionPathSegment segment = new BoundFunctionPathSegment(function, model, parameters);

            // Assert
            Assert.Same(expectedEntitySet, segment.GetNavigationSource(previouseEntitySet));
        }
예제 #19
0
        /// <summary>
        /// Parses the next OData path segment following an entity collection.
        /// </summary>
        /// <param name="model">The model to use for path parsing.</param>
        /// <param name="previous">The previous path segment.</param>
        /// <param name="previousEdmType">The EDM type of the OData path up to the previous segment.</param>
        /// <param name="segment">The value of the segment to parse.</param>
        /// <param name="segments">The queue of pending segments.</param>
        /// <returns>A parsed representation of the segment.</returns>
        protected virtual ODataPathSegment ParseAtEntityCollection(IEdmModel model, ODataPathSegment previous,
                                                                   IEdmType previousEdmType, string segment, Queue <string> segments)
        {
            if (previous == null)
            {
                throw Error.ArgumentNull("previous");
            }
            if (segments == null)
            {
                throw Error.ArgumentNull("segments");
            }
            if (String.IsNullOrEmpty(segment))
            {
                throw Error.Argument(SRResources.SegmentNullOrEmpty);
            }

            if (previousEdmType == null)
            {
                throw Error.InvalidOperation(SRResources.PreviousSegmentEdmTypeCannotBeNull);
            }
            IEdmCollectionType collectionType = previousEdmType as IEdmCollectionType;

            if (collectionType == null)
            {
                throw Error.Argument(SRResources.PreviousSegmentMustBeEntityCollectionType, previousEdmType);
            }
            IEdmEntityType elementType = collectionType.ElementType.Definition as IEdmEntityType;

            if (elementType == null)
            {
                throw Error.Argument(SRResources.PreviousSegmentMustBeEntityCollectionType, previousEdmType);
            }

            // look for keys first.
            if (segment.StartsWith("(", StringComparison.Ordinal) && segment.EndsWith(")", StringComparison.Ordinal))
            {
                Contract.Assert(segment.Length >= 2);
                string value = segment.Substring(1, segment.Length - 2);
                return(new KeyValuePathSegment(value));
            }

            // next look for casts
            IEdmEntityType castType = model.FindDeclaredType(segment) as IEdmEntityType;

            if (castType != null)
            {
                IEdmType previousElementType = collectionType.ElementType.Definition;
                if (!castType.IsOrInheritsFrom(previousElementType) && !previousElementType.IsOrInheritsFrom(castType))
                {
                    throw new ODataException(Error.Format(SRResources.InvalidCastInPath, castType, previousElementType));
                }
                return(new CastPathSegment(castType));
            }

            // look for $ref
            if (segment == ODataSegmentKinds.Ref)
            {
                return(new RefPathSegment());
            }

            // now look for bindable actions
            IEdmAction action = model.FindAction(segment, collectionType);

            if (action != null)
            {
                return(new BoundActionPathSegment(action));
            }

            // Try to match this to a function call
            BoundFunctionPathSegment pathSegment = TryMatchBoundFunctionCall(segment, segments, model, bindingType: collectionType);

            if (pathSegment != null)
            {
                return(pathSegment);
            }

            throw new ODataException(Error.Format(SRResources.NoActionFoundForCollection, segment, collectionType.ElementType));
        }
        public void GetEntitySet_Returns_FunctionTargetEntitySet()
        {
            // Arrange
            Mock<IEdmEntitySet> targetEntitySet = new Mock<IEdmEntitySet>();
            Mock<IEdmFunction> edmFuncton = new Mock<IEdmFunction>();
            edmFuncton.Setup(a => a.Namespace).Returns("NS");
            edmFuncton.Setup(a => a.Name).Returns("Funtion");

            // Act
            BoundFunctionPathSegment segment = new BoundFunctionPathSegment(edmFuncton.Object, null, null);

            // Assert
            Assert.Same(targetEntitySet.Object, segment.GetEntitySet(targetEntitySet.Object));
        }
        public void TryMatch_ReturnsTrue_IfSameFunction()
        {
            // Arrange
            IEdmEntityType returnType = new Mock<IEdmEntityType>().Object;
            EdmFunction function = new EdmFunction("NS", "Function", new EdmEntityTypeReference(returnType, isNullable: false));

            BoundFunctionPathSegment template = new BoundFunctionPathSegment(function, model: null, parameterValues: null);
            BoundFunctionPathSegment segment = new BoundFunctionPathSegment(function, model: null, parameterValues: null);

            // Act
            Dictionary<string, object> values = new Dictionary<string,object>();
            bool result = template.TryMatch(segment, values);

            // Assert
            Assert.True(result);
            Assert.Empty(values);
        }
        public void GetParameterValue_Returns_FunctionParameterValue()
        {
            // Arrange
            string parameterName = "Parameter";
            EdmModel model = new EdmModel();
            var entityType = new EdmEntityType("NS", "Customer");
            model.AddElement(entityType);

            IEdmTypeReference returnType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Boolean, isNullable: false);
            IEdmTypeReference parameterType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false);
            IEdmTypeReference bindingParamterType = new EdmEntityTypeReference(entityType, isNullable: false);
            EdmFunction function = new EdmFunction("NS", "Function", returnType);
            function.AddParameter("bindingParameter", bindingParamterType);
            function.AddParameter(parameterName, parameterType);
            model.AddElement(function);

            IDictionary<string, string> parameterValues = new Dictionary<string, string>();
            parameterValues.Add(parameterName, "101");

            // Act
            BoundFunctionPathSegment segment = new BoundFunctionPathSegment(function, model, parameterValues);
            var result = segment.GetParameterValue(parameterName);

            // Assert
            Assert.Equal("System.Int32", result.GetType().FullName);
            Assert.Equal("101", result.ToString());
        }
        public void ToString_ReturnsSameString_Function()
        {
            // Arrange
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("Id", "123");
            parameters.Add("Name", "John");
            BoundFunctionPathSegment segment = new BoundFunctionPathSegment("function", parameters);

            // Act
            string actual = segment.ToString();

            // Assert
            Assert.Equal("function(Id=123,Name=John)", actual);
        }