/// <summary>
        /// Validates a <see cref="CountQueryOption" />.
        /// </summary>
        /// <param name="countQueryOption">The $count query.</param>
        /// <param name="validationSettings">The validation settings.</param>
        public virtual void Validate(CountQueryOption countQueryOption, ODataValidationSettings validationSettings)
        {
            if (countQueryOption == null)
            {
                throw Error.ArgumentNull("countQueryOption");
            }

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

            ODataPath path = countQueryOption.Context.Path;

            if (path != null && path.Segments.Count > 0)
            {
                ODataPathSegment lastSegment = path.Segments.Last();

                if (lastSegment is CountPathSegment && path.Segments.Count > 1)
                {
                    ValidateCount(path.Segments[path.Segments.Count - 2], countQueryOption.Context.Model);
                }
                else
                {
                    ValidateCount(lastSegment, countQueryOption.Context.Model);
                }
            }
        }
        public void Validate_Throws_NullSettings()
        {
            // Arrange
            ODataQueryContext context = ValidationTestHelper.CreateCustomerContext();
            var option = new CountQueryOption("Name eq 'abc'", context);

            // Act & Assert
            Assert.Throws<ArgumentNullException>(() => _validator.Validate(option, null));
        }
        public void Validate_Throws_DollarCountAppliedOnNotCountableCollection(string uri, string message)
        {
            // Arrange
            IEdmModel model = GetEdmModel();
            var pathHandler = new DefaultODataPathHandler();
            string serviceRoot = "http://localhost/";
            ODataPath path = pathHandler.Parse(model, serviceRoot, uri);
            var context = new ODataQueryContext(
                model,
                EdmCoreModel.Instance.GetInt32(false).Definition,
                path);
            var option = new CountQueryOption("true", context);
            var settings = new ODataValidationSettings();

            // Act & Assert
            Assert.Throws<InvalidOperationException>(() => _validator.Validate(option, settings), message);
        }
        public void Validate_Throws_DollarCountAppliedOnNotCountableCollection(string uri, string message)
        {
            // Arrange
            IEdmModel model       = GetEdmModel();
            var       pathHandler = new DefaultODataPathHandler();
            string    serviceRoot = "http://localhost/";
            ODataPath path        = pathHandler.Parse(model, serviceRoot, uri);
            var       context     = new ODataQueryContext(
                model,
                EdmCoreModel.Instance.GetInt32(false).Definition,
                path);
            var option   = new CountQueryOption("true", context);
            var settings = new ODataValidationSettings();

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() => _validator.Validate(option, settings), message);
        }
Example #5
0
        /// <summary>
        /// Validates a <see cref="CountQueryOption" />.
        /// </summary>
        /// <param name="countQueryOption">The $count query.</param>
        /// <param name="validationSettings">The validation settings.</param>
        public virtual void Validate(CountQueryOption countQueryOption, ODataValidationSettings validationSettings)
        {
            if (countQueryOption == null)
            {
                throw Error.ArgumentNull(nameof(countQueryOption));
            }

            if (validationSettings == null)
            {
                throw Error.ArgumentNull(nameof(validationSettings));
            }

            ODataPath path = countQueryOption.Context.Path;

            if (path != null && path.Count > 0)
            {
                IEdmProperty       property       = countQueryOption.Context.TargetProperty;
                IEdmStructuredType structuredType = countQueryOption.Context.TargetStructuredType;
                string             name           = countQueryOption.Context.TargetName;
                if (EdmHelpers.IsNotCountable(property, structuredType,
                                              countQueryOption.Context.Model,
                                              countQueryOption.Context.DefaultQuerySettings.EnableCount))
                {
                    if (property == null)
                    {
                        throw new InvalidOperationException(Error.Format(
                                                                SRResources.NotCountableEntitySetUsedForCount,
                                                                name));
                    }
                    else
                    {
                        throw new InvalidOperationException(Error.Format(
                                                                SRResources.NotCountablePropertyUsedForCount,
                                                                name));
                    }
                }
            }
        }