Ejemplo n.º 1
0
        public static object ShouldBeConstantParameterWithValueType <TValue>(this OperationSegmentParameter parameter, string name, TValue value)
        {
            Assert.Equal(name, parameter.Name);
            ConstantNode constantNode = Assert.IsType <ConstantNode>(parameter.Value);

            if (value == null)
            {
                Assert.Null(constantNode.Value);
            }
            else
            {
                if (typeof(TValue).IsPrimitive() || typeof(TValue) == typeof(decimal))
                {
                    // for int value --> long TValue
                    TValue tmp = (TValue)Convert.ChangeType(constantNode.Value, typeof(TValue));
                    Assert.NotNull(tmp);
                    Assert.Equal(value, tmp);
                }
                else if (typeof(TValue) == typeof(UriTemplateExpression))
                {
                    UriTemplateExpression actual = Assert.IsType <UriTemplateExpression>(constantNode.Value);
                    UriTemplateExpression expect = Assert.IsType <UriTemplateExpression>(value);
                    Assert.Equal(expect.LiteralText, actual.LiteralText);
                    Assert.Equal(expect.ExpectedType.FullName(), expect.ExpectedType.FullName());
                }
                else
                {
                    constantNode.Value.GetType().IsAssignableFrom(typeof(TValue));
                    Assert.Equal(value, constantNode.Value);
                }
            }

            return(constantNode.Value);
        }
Ejemplo n.º 2
0
        public void BuildKeyMappingsKeySegmentTemplate_ReturnsKeyMapping_UriTemplateExpression()
        {
            // Arrange
            EdmEntityType         customerType = new EdmEntityType("NS", "Customer");
            EdmStructuralProperty idProperty   = customerType.AddStructuralProperty("customerId", EdmPrimitiveTypeKind.Int32);

            customerType.AddKeys(idProperty);

            UriTemplateExpression tempateExpression = BuildExpression("{yourId}", idProperty.Type);

            IDictionary <string, object> keys = new Dictionary <string, object>
            {
                { "customerId", tempateExpression }
            };

            // Act
            IDictionary <string, string> mapped = KeySegmentTemplate.BuildKeyMappings(keys, customerType);

            // Assert
            Assert.NotNull(mapped);
            KeyValuePair <string, string> actual = Assert.Single(mapped);

            Assert.Equal("customerId", actual.Key);
            Assert.Equal("yourId", actual.Value);
        }
Ejemplo n.º 3
0
        // Translate the object of key in ODL path to string literal.
        private static string TranslateKeySegmentValue(object value, bool enableUriTemplateParsing)
        {
            if (value == null)
            {
                throw Error.ArgumentNull("value");
            }

            if (enableUriTemplateParsing)
            {
                UriTemplateExpression uriTemplateExpression = value as UriTemplateExpression;
                if (uriTemplateExpression != null)
                {
                    return(uriTemplateExpression.LiteralText);
                }
            }

            ConstantNode constantNode = value as ConstantNode;

            if (constantNode != null)
            {
                ODataEnumValue enumValue = constantNode.Value as ODataEnumValue;
                if (enumValue != null)
                {
                    return(ODataUriUtils.ConvertToUriLiteral(enumValue, ODataVersion.V4));
                }
            }

            return(ODataUriUtils.ConvertToUriLiteral(value, ODataVersion.V4));
        }
Ejemplo n.º 4
0
        // Translate the object of key in ODL path to string literal.
        private static string TranslateKeySegmentValue(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            UriTemplateExpression uriTemplateExpression = value as UriTemplateExpression;

            if (uriTemplateExpression != null)
            {
                return(uriTemplateExpression.LiteralText);
            }

            ConstantNode constantNode = value as ConstantNode;

            if (constantNode != null)
            {
                ODataEnumValue enumValue = constantNode.Value as ODataEnumValue;
                if (enumValue != null)
                {
                    return(ODataUriUtils.ConvertToUriLiteral(enumValue, ODataVersion.V4));
                }
            }

            return(ODataUriUtils.ConvertToUriLiteral(value, ODataVersion.V4));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Build the key mappings
        /// </summary>
        /// <param name="keys">The Uri template parsing result. for example: SSN={ssnKey}</param>
        /// <param name="entityType">The Edm entity type.</param>
        /// <param name="keyProperties">The key properties.</param>
        /// <returns>The mapping</returns>
        internal static IDictionary <string, string> BuildKeyMappings(IEnumerable <KeyValuePair <string, object> > keys,
                                                                      IEdmEntityType entityType, IDictionary <string, IEdmProperty> keyProperties)
        {
            Contract.Assert(keys != null);
            Contract.Assert(entityType != null);
            Contract.Assert(keyProperties != null);

            Dictionary <string, string> parameterMappings = new Dictionary <string, string>();

            int count = keys.Count();

            if (count != keyProperties.Count)
            {
                throw new ODataException(Error.Format(SRResources.InputKeyNotMatchEntityTypeKey, count, keyProperties.Count, entityType.FullName()));
            }

            // keys have:  SSN={ssn},Name={name}
            // the key "SSN" or "Name" is the alias in alternate keys
            foreach (KeyValuePair <string, object> key in keys)
            {
                string keyName = key.Key;

                // key name is case-sensitive
                if (!keyProperties.ContainsKey(key.Key))
                {
                    throw new ODataException(Error.Format(SRResources.CannotFindKeyInEntityType, keyName, entityType.FullName()));
                }

                string nameInRouteData;

                UriTemplateExpression uriTemplateExpression = key.Value as UriTemplateExpression;
                if (uriTemplateExpression != null)
                {
                    nameInRouteData = uriTemplateExpression.LiteralText.Trim();
                }
                else
                {
                    // just for easy construct the key segment template
                    // it must start with "{" and end with "}"
                    nameInRouteData = key.Value as string;
                }

                if (nameInRouteData == null || !nameInRouteData.IsValidTemplateLiteral())
                {
                    throw new ODataException(Error.Format(SRResources.KeyTemplateMustBeInCurlyBraces, key.Value, key.Key));
                }

                nameInRouteData = nameInRouteData.Substring(1, nameInRouteData.Length - 2);
                if (string.IsNullOrEmpty(nameInRouteData))
                {
                    throw new ODataException(Error.Format(SRResources.EmptyKeyTemplate, key.Value, key.Key));
                }

                parameterMappings[key.Key] = nameInRouteData;
            }

            return(parameterMappings);
        }
Ejemplo n.º 6
0
        public static UriTemplateExpression ShouldBeUriTemplateExpression(this object node, string literalText, IEdmTypeReference expectedType)
        {
            Assert.NotNull(node);
            UriTemplateExpression uriTemplate = Assert.IsType <UriTemplateExpression>(node);

            Assert.Equal(literalText, uriTemplate.LiteralText);
            Assert.True(uriTemplate.ExpectedType.IsEquivalentTo(expectedType));
            return(uriTemplate);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Parse a literal as Uri template.
        /// </summary>
        /// <param name="literalText">The input text.</param>
        /// <param name="expectedType">The expected type of the object which the Uri template stands for.</param>
        /// <param name="expression">The <see cref="UriTemplateExpression"/> representing the Uri template.</param>
        /// <returns>True if successfully expression parsed, false otherwise. </returns>
        internal static bool TryParseLiteral(string literalText, IEdmTypeReference expectedType, out UriTemplateExpression expression)
        {
            if (IsValidTemplateLiteral(literalText))
            {
                expression = new UriTemplateExpression {
                    LiteralText = literalText, ExpectedType = expectedType
                };
                return(true);
            }

            expression = null;
            return(false);
        }
Ejemplo n.º 8
0
        private static string TranslateNode(object node, string functionName, string parameterName)
        {
            // If the function parameter is null, for example myFunction(param=null),
            // the input node here is not null, it is a contant node with a value as "null".
            // However, if a function call (or key) using parameter alias but without providing the parameter alias value,
            // the input node here is a null.
            if (node == null)
            {
                // We can't throw ODataException here because ODataException will be caught and return 404 response with empty message.
                throw new InvalidOperationException(Error.Format(SRResources.MissingConvertNode, parameterName, functionName));
            }

            ConstantNode constantNode = node as ConstantNode;

            if (constantNode != null)
            {
                UriTemplateExpression uriTemplateExpression = constantNode.Value as UriTemplateExpression;
                if (uriTemplateExpression != null)
                {
                    return(uriTemplateExpression.LiteralText);
                }

                // Make the enum prefix free to work.
                ODataEnumValue enumValue = constantNode.Value as ODataEnumValue;
                if (enumValue != null)
                {
                    return(ODataUriUtils.ConvertToUriLiteral(enumValue, ODataVersion.V4));
                }

                return(constantNode.LiteralText);
            }

            ConvertNode convertNode = node as ConvertNode;

            if (convertNode != null)
            {
                return(TranslateNode(convertNode.Source, functionName, parameterName));
            }

            ParameterAliasNode parameterAliasNode = node as ParameterAliasNode;

            if (parameterAliasNode != null)
            {
                return(parameterAliasNode.Alias);
            }

            //return node.ToString();
            throw Error.NotSupported(SRResources.CannotRecognizeNodeType, typeof(ODataPathSegmentHandler),
                                     node.GetType().FullName);
        }
        public static IDictionary <string, string> BuildParameterMappings(
            IEnumerable <OperationSegmentParameter> parameters, string segment)
        {
            Contract.Assert(parameters != null);

            Dictionary <string, string> parameterMappings = new Dictionary <string, string>();

            foreach (OperationSegmentParameter parameter in parameters)
            {
                string parameterName   = parameter.Name;
                string nameInRouteData = null;

                ConstantNode node = parameter.Value as ConstantNode;
                if (node != null)
                {
                    UriTemplateExpression uriTemplateExpression = node.Value as UriTemplateExpression;
                    if (uriTemplateExpression != null)
                    {
                        nameInRouteData = uriTemplateExpression.LiteralText.Trim();
                    }
                    else
                    {
                        nameInRouteData = node.Value as string;
                    }
                }
                else
                {
                    // Just for easy constructor the function parameters
                    nameInRouteData = parameter.Value as string;
                }

                if (nameInRouteData == null || !IsRouteParameter(nameInRouteData))
                {
                    throw new ODataException(
                              Error.Format(SRResources.ParameterAliasMustBeInCurlyBraces, parameter.Value, segment));
                }

                nameInRouteData = nameInRouteData.Substring(1, nameInRouteData.Length - 2);
                if (String.IsNullOrEmpty(nameInRouteData))
                {
                    throw new ODataException(
                              Error.Format(SRResources.EmptyParameterAlias, parameter.Value, segment));
                }

                parameterMappings[parameterName] = nameInRouteData;
            }

            return(parameterMappings);
        }
Ejemplo n.º 10
0
        // Translate the node in ODL path to string literal.
        private string TranslateNode(object node)
        {
            if (node == null)
            {
                throw Error.ArgumentNull("node");
            }

            ConstantNode constantNode = node as ConstantNode;

            if (constantNode != null)
            {
                if (_enableUriTemplateParsing)
                {
                    UriTemplateExpression uriTemplateExpression = constantNode.Value as UriTemplateExpression;
                    if (uriTemplateExpression != null)
                    {
                        return(uriTemplateExpression.LiteralText);
                    }
                }

                // Make the enum prefix free to work.
                ODataEnumValue enumValue = constantNode.Value as ODataEnumValue;
                if (enumValue != null)
                {
                    return(ODataUriUtils.ConvertToUriLiteral(enumValue, ODataVersion.V4));
                }

                return(constantNode.LiteralText);
            }

            ConvertNode convertNode = node as ConvertNode;

            if (convertNode != null)
            {
                return(TranslateNode(convertNode.Source));
            }

            ParameterAliasNode parameterAliasNode = node as ParameterAliasNode;

            if (parameterAliasNode != null)
            {
                return(TranslateParameterAlias(parameterAliasNode.Alias));
            }

            throw Error.NotSupported(
                      SRResources.CannotRecognizeNodeType,
                      typeof(ODataPathSegmentTranslator),
                      node.GetType().FullName);
        }
Ejemplo n.º 11
0
        public void TranslateNode_TranslatesValue()
        {
            // Arrange & Act & Assert
            UriTemplateExpression expression = KeySegmentTemplateTests.BuildExpression("{key}");
            ConstantNode          node       = new ConstantNode(expression);

            Assert.Equal("{key}", ODataPathSegmentHandler.TranslateNode(node));

            // Arrange & Act & Assert
            EdmEnumType enumType = new EdmEnumType("NS", "Color");

            enumType.AddMember(new EdmEnumMember(enumType, "Red", new EdmEnumMemberValue(1)));
            ODataEnumValue enumValue = new ODataEnumValue("Red", "NS.Color");

            node = new ConstantNode(enumValue);
            Assert.Equal("NS.Color'Red'", ODataPathSegmentHandler.TranslateNode(node));
        }
Ejemplo n.º 12
0
        internal static IDictionary <string, string> BuildKeyMappings(KeyValuePathSegment keyValuePathSegment)
        {
            Contract.Assert(keyValuePathSegment != null);

            if (keyValuePathSegment.Segment != null)
            {
                Dictionary <string, string> parameterMappings = new Dictionary <string, string>();
                foreach (var key in keyValuePathSegment.Segment.Keys)
                {
                    string parameterNameInRouteData             = null;
                    UriTemplateExpression uriTemplateExpression = key.Value as UriTemplateExpression;
                    if (uriTemplateExpression != null)
                    {
                        parameterNameInRouteData = uriTemplateExpression.LiteralText;
                    }

                    if (String.IsNullOrEmpty(parameterNameInRouteData))
                    {
                        parameterNameInRouteData = key.Key;
                    }
                    else if (IsRouteParameter(parameterNameInRouteData))
                    {
                        parameterNameInRouteData = parameterNameInRouteData.Substring(1, parameterNameInRouteData.Length - 2);
                        if (String.IsNullOrEmpty(parameterNameInRouteData))
                        {
                            throw new ODataException(
                                      Error.Format(SRResources.EmptyKeyTemplate, keyValuePathSegment.Value));
                        }
                    }
                    else
                    {
                        throw new ODataException(
                                  Error.Format(SRResources.KeyTemplateMustBeInCurlyBraces, keyValuePathSegment.Value));
                    }

                    parameterMappings[key.Key] = parameterNameInRouteData;
                }

                return(parameterMappings);
            }

            return(BuildParameterMappings(keyValuePathSegment.Values, keyValuePathSegment.Value));
        }
Ejemplo n.º 13
0
        private static string TranslateNode(object node)
        {
            Contract.Assert(node != null);

            ConstantNode constantNode = node as ConstantNode;

            if (constantNode != null)
            {
                UriTemplateExpression uriTemplateExpression = constantNode.Value as UriTemplateExpression;
                if (uriTemplateExpression != null)
                {
                    return(uriTemplateExpression.LiteralText);
                }

                // Make the enum prefix free to work.
                ODataEnumValue enumValue = constantNode.Value as ODataEnumValue;
                if (enumValue != null)
                {
                    return(ODataUriUtils.ConvertToUriLiteral(enumValue, ODataVersion.V4));
                }

                return(constantNode.LiteralText);
            }

            ConvertNode convertNode = node as ConvertNode;

            if (convertNode != null)
            {
                return(TranslateNode(convertNode.Source));
            }

            ParameterAliasNode parameterAliasNode = node as ParameterAliasNode;

            if (parameterAliasNode != null)
            {
                return(parameterAliasNode.Alias);
            }

            //return node.ToString();
            throw Error.NotSupported(SRResources.CannotRecognizeNodeType, typeof(ODataPathSegmentHandler),
                                     node.GetType().FullName);
        }
Ejemplo n.º 14
0
        private static string TranslateNode(object node)
        {
            Contract.Assert(node != null);

            ConstantNode constantNode = node as ConstantNode;

            if (constantNode != null)
            {
                UriTemplateExpression uriTemplateExpression = constantNode.Value as UriTemplateExpression;
                if (uriTemplateExpression != null)
                {
                    return(uriTemplateExpression.LiteralText);
                }

                // Make the enum prefix free to work.
                ODataEnumValue enumValue = constantNode.Value as ODataEnumValue;
                if (enumValue != null)
                {
                    return(ODataUriUtils.ConvertToUriLiteral(enumValue, ODataVersion.V4));
                }

                return(constantNode.LiteralText);
            }

            ConvertNode convertNode = node as ConvertNode;

            if (convertNode != null)
            {
                return(TranslateNode(convertNode.Source));
            }

            ParameterAliasNode parameterAliasNode = node as ParameterAliasNode;

            if (parameterAliasNode != null)
            {
                return(parameterAliasNode.Alias);
            }

            //return node.ToString();
            throw new NotSupportedException($"Cannot recongnize {node.GetType().FullName}");
        }
Ejemplo n.º 15
0
        internal static IDictionary <string, string> BuildKeyMappings(IEnumerable <KeyValuePair <string, object> > keys)
        {
            Contract.Assert(keys != null);

            Dictionary <string, string> parameterMappings = new Dictionary <string, string>();

            foreach (KeyValuePair <string, object> key in keys)
            {
                string nameInRouteData;

                UriTemplateExpression uriTemplateExpression = key.Value as UriTemplateExpression;
                if (uriTemplateExpression != null)
                {
                    nameInRouteData = uriTemplateExpression.LiteralText.Trim();
                }
                else
                {
                    // just for easy construct the key segment template
                    // it must start with "{" and end with "}"
                    nameInRouteData = key.Value as string;
                }

                if (nameInRouteData == null || !RoutingConventionHelpers.IsRouteParameter(nameInRouteData))
                {
                    throw new ODataException(
                              Error.Format(SRResources.KeyTemplateMustBeInCurlyBraces, key.Value, key.Key));
                }

                nameInRouteData = nameInRouteData.Substring(1, nameInRouteData.Length - 2);
                if (String.IsNullOrEmpty(nameInRouteData))
                {
                    throw new ODataException(
                              Error.Format(SRResources.EmptyKeyTemplate, key.Value, key.Key));
                }

                parameterMappings[key.Key] = nameInRouteData;
            }

            return(parameterMappings);
        }
Ejemplo n.º 16
0
        internal static string TranslateNode(object node)
        {
            ConstantNode constantNode = node as ConstantNode;

            if (constantNode != null)
            {
                UriTemplateExpression uriTemplateExpression = constantNode.Value as UriTemplateExpression;
                if (uriTemplateExpression != null)
                {
                    return(uriTemplateExpression.LiteralText);
                }

                // Make the enum prefix free to work.
                ODataEnumValue enumValue = constantNode.Value as ODataEnumValue;
                if (enumValue != null)
                {
                    return(ODataUriUtils.ConvertToUriLiteral(enumValue, ODataVersion.V4));
                }

                return(constantNode.LiteralText);
            }

            ConvertNode convertNode = node as ConvertNode;

            if (convertNode != null)
            {
                return(TranslateNode(convertNode.Source));
            }

            ParameterAliasNode parameterAliasNode = node as ParameterAliasNode;

            if (parameterAliasNode != null)
            {
                return(parameterAliasNode.Alias);
            }

            return(ODataUriUtils.ConvertToUriLiteral(node, ODataVersion.V4));
        }
        public void BuildKeyMappings_ReturnsKeyMapping_UriTemplateExpression()
        {
            // Arrange
            EdmEntityType         customerType = new EdmEntityType("NS", "Customer");
            EdmStructuralProperty idProperty   = customerType.AddStructuralProperty("customerId", EdmPrimitiveTypeKind.Int32);

            customerType.AddKeys(idProperty);

            // The properties of UriTemplateExpression are internal set.
            // We use the reflect to set the value.
            PropertyInfo literalText = typeof(UriTemplateExpression).GetProperty("LiteralText");

            Assert.NotNull(literalText);

            PropertyInfo expectedType = typeof(UriTemplateExpression).GetProperty("ExpectedType");

            Assert.NotNull(expectedType);

            UriTemplateExpression tempateExpression = new UriTemplateExpression();

            literalText.SetValue(tempateExpression, "{yourId}");
            expectedType.SetValue(tempateExpression, idProperty.Type);

            IDictionary <string, object> keys = new Dictionary <string, object>
            {
                { "customerId", tempateExpression }
            };

            // Arrange
            IDictionary <string, string> mapped = KeySegmentTemplate.BuildKeyMappings(keys, customerType);

            // Assert
            Assert.NotNull(mapped);
            KeyValuePair <string, string> actual = Assert.Single(mapped);

            Assert.Equal("customerId", actual.Key);
            Assert.Equal("yourId", actual.Value);
        }
Ejemplo n.º 18
0
        private static UriTemplateExpression BuildExpression(string value, object expectType = null)
        {
            // The properties of UriTemplateExpression are internal set.
            // We use the reflect to set the value.

            PropertyInfo literalText = typeof(UriTemplateExpression).GetProperty("LiteralText");

            Assert.NotNull(literalText);

            PropertyInfo expectedType = typeof(UriTemplateExpression).GetProperty("ExpectedType");

            Assert.NotNull(expectedType);

            UriTemplateExpression tempateExpression = new UriTemplateExpression();

            literalText.SetValue(tempateExpression, value);

            if (expectType != null)
            {
                expectedType.SetValue(tempateExpression, expectType);
            }

            return(tempateExpression);
        }