예제 #1
0
        public void Validate_Throw_WithInvalidMaxExpansionDepth()
        {
            int maxExpansionDepth = -1;
            // Arrange
            string expand  = "Parent($levels=1)";
            var    builder = ODataConventionModelBuilderFactory.Create();

            builder.EntitySet <ODataLevelsTest.LevelsEntity>("Entities");
            IEdmModel model   = builder.GetEdmModel();
            var       context = new ODataQueryContext(model, typeof(ODataLevelsTest.LevelsEntity));

            context.RequestContainer = new MockContainer();
            var validator = SelectExpandQueryValidator.GetSelectExpandQueryValidator(context);
            var selectExpandQueryOption = new SelectExpandQueryOption(null, expand, context);

            // Act & Assert
#if NETCOREAPP3_1
            ExceptionAssert.Throws <ArgumentOutOfRangeException>(
                () => validator.Validate(
                    selectExpandQueryOption,
                    new ODataValidationSettings {
                MaxExpansionDepth = maxExpansionDepth
            }),
                "Value must be greater than or equal to 0. (Parameter 'value')\r\nActual value was -1.");
#else
            ExceptionAssert.Throws <ArgumentOutOfRangeException>(
                () => validator.Validate(
                    selectExpandQueryOption,
                    new ODataValidationSettings {
                MaxExpansionDepth = maxExpansionDepth
            }),
                "Value must be greater than or equal to 0.\r\nParameter name: value\r\nActual value was -1.");
#endif
        }
예제 #2
0
        public void ValidateThrowException_IfNotExpandable_QuerySettings()
        {
            // Arrange
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();

            model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(Customer)));
            ODataQueryContext queryContext = new ODataQueryContext(model.Model, typeof(Customer));

            queryContext.RequestContainer = new MockContainer();
            SelectExpandQueryValidator validator = SelectExpandQueryValidator.GetSelectExpandQueryValidator(queryContext);
            SelectExpandQueryOption    selectExpandQueryOption = new SelectExpandQueryOption(null, "Orders", queryContext);
            IEdmStructuredType         customerType            =
                model.Model.SchemaElements.First(e => e.Name.Equals("Customer")) as IEdmStructuredType;
            ModelBoundQuerySettings querySettings = new ModelBoundQuerySettings();

            querySettings.ExpandConfigurations.Add("Orders", new ExpandConfiguration
            {
                ExpandType = SelectExpandType.Disabled,
                MaxDepth   = 0
            });
            model.Model.SetAnnotationValue(customerType, querySettings);

            // Act & Assert
            ExceptionAssert.Throws <ODataException>(
                () => validator.Validate(selectExpandQueryOption, new ODataValidationSettings()),
                "The property 'Orders' cannot be used in the $expand query option.");
        }
예제 #3
0
        // This constructor is intended for unit testing only.
        internal SelectExpandQueryOption(string select, string expand, ODataQueryContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            if (String.IsNullOrEmpty(select) && String.IsNullOrEmpty(expand))
            {
                throw Error.Argument(SRResources.SelectExpandEmptyOrNull);
            }

            if (!(context.ElementType is IEdmStructuredType))
            {
                throw Error.Argument("context", SRResources.SelectNonStructured, context.ElementType.ToTraceString());
            }

            Context            = context;
            RawSelect          = select;
            RawExpand          = expand;
            Validator          = SelectExpandQueryValidator.GetSelectExpandQueryValidator(context);
            _queryOptionParser = new ODataQueryOptionParser(
                context.Model,
                context.ElementType,
                context.NavigationSource,
                new Dictionary <string, string> {
                { "$select", select }, { "$expand", expand }
            },
                context.RequestContainer);
        }
예제 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SelectExpandQueryOption"/> class.
        /// </summary>
        /// <param name="select">The $select query parameter value.</param>
        /// <param name="expand">The $expand query parameter value.</param>
        /// <param name="context">The <see cref="ODataQueryContext"/> which contains the <see cref="IEdmModel"/> and some type information.</param>
        /// <param name="queryOptionParser">The <see cref="ODataQueryOptionParser"/> which is used to parse the query option.</param>
        public SelectExpandQueryOption(string select, string expand, ODataQueryContext context,
                                       ODataQueryOptionParser queryOptionParser)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            if (String.IsNullOrEmpty(select) && String.IsNullOrEmpty(expand))
            {
                throw Error.Argument(SRResources.SelectExpandEmptyOrNull);
            }

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

            if (!(context.ElementType is IEdmStructuredType))
            {
                throw Error.Argument(SRResources.SelectNonStructured, context.ElementType);
            }

            Context            = context;
            RawSelect          = select;
            RawExpand          = expand;
            Validator          = SelectExpandQueryValidator.GetSelectExpandQueryValidator(context);
            _queryOptionParser = queryOptionParser;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SelectExpandQueryOption"/> class.
        /// </summary>
        /// <param name="select">The $select query parameter value.</param>
        /// <param name="expand">The $expand query parameter value.</param>
        /// <param name="context">The <see cref="ODataQueryContext"/> which contains the <see cref="IEdmModel"/> and some type information.</param>
        /// <param name="queryOptionParser">The <see cref="ODataQueryOptionParser"/> which is used to parse the query option.</param>
        public SelectExpandQueryOption(string select, string expand, ODataQueryContext context,
                                       ODataQueryOptionParser queryOptionParser)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            if (String.IsNullOrEmpty(select) && String.IsNullOrEmpty(expand))
            {
                throw Error.Argument(SRResources.SelectExpandEmptyOrNull);
            }

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

            IEdmEntityType entityType = context.ElementType as IEdmEntityType;

            if (entityType == null)
            {
                throw Error.Argument("context", SRResources.SelectNonEntity, context.ElementType.ToTraceString());
            }

            Context            = context;
            RawSelect          = select;
            RawExpand          = expand;
            Validator          = SelectExpandQueryValidator.GetSelectExpandQueryValidator(context);
            _queryOptionParser = queryOptionParser;
        }
예제 #6
0
        public void GetSelectExpandQueryValidator_Returns_Validator()
        {
            // Arrange & Act & Assert
            Assert.NotNull(SelectExpandQueryValidator.GetSelectExpandQueryValidator(null));

            // Arrange & Act & Assert
            ODataQueryContext context = new ODataQueryContext(EdmCoreModel.Instance, typeof(int));

            Assert.NotNull(SelectExpandQueryValidator.GetSelectExpandQueryValidator(context));

            // Arrange & Act & Assert
            IServiceProvider services = new ServiceCollection()
                                        .AddSingleton <SelectExpandQueryValidator>()
                                        .AddSingleton <DefaultQuerySettings>().BuildServiceProvider();

            context.RequestContainer = services;
            Assert.NotNull(SelectExpandQueryValidator.GetSelectExpandQueryValidator(context));
        }
예제 #7
0
        public void ValidateThrowException_IfNotExpandable()
        {
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();

            model.Model.SetAnnotationValue(model.Customer, new ClrTypeAnnotation(typeof(Customer)));
            ODataQueryContext queryContext = new ODataQueryContext(model.Model, typeof(Customer));

            queryContext.RequestContainer = new MockContainer();
            model.Model.SetAnnotationValue(model.Customer.FindProperty("Orders"), new QueryableRestrictionsAnnotation(new QueryableRestrictions {
                NotExpandable = true
            }));

            string expand = "Orders";
            SelectExpandQueryValidator validator = SelectExpandQueryValidator.GetSelectExpandQueryValidator(queryContext);
            SelectExpandQueryOption    selectExpandQueryOption = new SelectExpandQueryOption(null, expand, queryContext);

            ExceptionAssert.Throws <ODataException>(
                () => validator.Validate(selectExpandQueryOption, new ODataValidationSettings()),
                "The property 'Orders' cannot be used in the $expand query option.");
        }
예제 #8
0
        public void ValidateThrowException_IfBaseOrDerivedClassPropertyNotExpandable(string className, string propertyName)
        {
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();

            model.Model.SetAnnotationValue(model.SpecialCustomer, new ClrTypeAnnotation(typeof(Customer)));
            ODataQueryContext queryContext = new ODataQueryContext(model.Model, typeof(Customer));

            queryContext.RequestContainer = new MockContainer();
            EdmEntityType classType = (className == "Customer") ? model.Customer : model.SpecialCustomer;

            model.Model.SetAnnotationValue(classType.FindProperty(propertyName), new QueryableRestrictionsAnnotation(new QueryableRestrictions {
                NotExpandable = true
            }));

            string expand = "NS.SpecialCustomer/" + propertyName;
            SelectExpandQueryValidator validator = SelectExpandQueryValidator.GetSelectExpandQueryValidator(queryContext);
            SelectExpandQueryOption    selectExpandQueryOption = new SelectExpandQueryOption(null, expand, queryContext);

            ExceptionAssert.Throws <ODataException>(
                () => validator.Validate(selectExpandQueryOption, new ODataValidationSettings()),
                String.Format(CultureInfo.InvariantCulture, "The property '{0}' cannot be used in the $expand query option.", propertyName));
        }