예제 #1
0
        public void Execute_WithSortOptionsAndManualSortOptions_ThrowsInvalidOperationException()
        {
            //prepare
            var sort = new SortingOptions();

            sort.AddSort("test", true);
            _options.Setup(o => o.SortingOptions)
            .Returns(sort);
            _options.Setup(o => o.Controls)
            .Returns(new[] { new SortRequestControl() });
            var command = new MockStandardQueryCommand(_options.Object, _mapping.Object)
                          .DisablePagedRequest()
                          .DisableStandardRequest();

            //assert
            Executing.This(() => command.Execute(null, SearchScope.OneLevel, 1, true))
            .Should().Throw <InvalidOperationException>()
            .And.Exception.Message.Should().Be.EqualTo("Only one sort request control can be sent to the server");
        }
예제 #2
0
        public void Execute_ReturnsRequest()
        {
            //prepare
            var sortingOptions = new SortingOptions();

            sortingOptions.AddSort("att1", true);
            var options = new Mock <IQueryCommandOptions>();

            options.SetupGet(o => o.Filter)
            .Returns("x=1");
            options.SetupGet(o => o.AttributesToLoad)
            .Returns(new Dictionary <string, string> {
                { "Property1", "prop1" }, { "Property2", "prop2" }
            });
            options.SetupGet(o => o.SortingOptions)
            .Returns(sortingOptions);
            options.SetupGet(o => o.Controls)
            .Returns(new DirectoryControl[] { new AsqRequestControl("att") });
            options.SetupGet(o => o.TakeSize)
            .Returns(2);
            var connection = new MockLdapConnection();
            var mapping    = new Mock <IObjectMapping>();

            mapping.Setup(m => m.NamingContext)
            .Returns("nm");
            var command = new GetRequestCommand(options.Object, mapping.Object);

            //act
            var result = command.Execute(connection, SearchScope.Subtree, 1, true).As <SearchRequest>();

            //assert
            result.Filter.Should().Be.EqualTo("x=1");
            result.Controls[0].As <AsqRequestControl>().AttributeName.Should().Be.EqualTo("att");
            result.Controls[1].As <SortRequestControl>().SortKeys[0].AttributeName.Should().Be.EqualTo("att1");
            result.Controls[1].As <SortRequestControl>().SortKeys[0].ReverseOrder.Should().Be.True();
            result.Controls[2].As <PageResultRequestControl>().PageSize.Should().Be.EqualTo(2);
            result.Attributes[0].Should().Be.EqualTo("prop1");
            result.Attributes[1].Should().Be.EqualTo("prop2");
            result.Scope.Should().Be.EqualTo(SearchScope.Subtree);

            result.DistinguishedName.Should().Be.EqualTo("nm");
        }
예제 #3
0
        public void Execute_WithSortOptionsAndNoPagingOptions_CallsHandleStandardRequest()
        {
            //prepare
            var sort = new SortingOptions();

            sort.AddSort("test", true);
            _options.Setup(o => o.SortingOptions)
            .Returns(sort);
            var command = new MockStandardQueryCommand(_options.Object, _mapping.Object)
                          .DisablePagedRequest()
                          .DisableStandardRequest();

            //act
            command.Execute(null, SearchScope.OneLevel, 1, true);

            //assert
            command.GetRequest().Controls.OfType <SortRequestControl>().Should().Have.Count.EqualTo(1);
            command.HandleStandardRequestCalled.Should().Be.True();
            command.HandlePagedRequestCalled.Should().Be.False();
        }
예제 #4
0
        private void VisitQueryableMethods(MethodCallExpression m)
        {
            switch (m.Method.Name)
            {
            case "Where":
                _exclusiveWhereCount++;
                foreach (Expression t in m.Arguments)
                {
                    Visit(t);
                }
                _commandType = QueryCommandType.StandardCommand;
                break;

            case "WithoutPaging":
                foreach (Expression t in m.Arguments)
                {
                    Visit(t);
                }
                _commandType   = QueryCommandType.StandardCommand;
                _withoutPaging = true;
                break;

            case "GetRequest":
                foreach (Expression t in m.Arguments)
                {
                    Visit(t);
                }
                _commandType = QueryCommandType.GetRequestCommand;
                break;

            case "Any":
                if (m.Arguments.Count > 1)
                {
                    _exclusiveWhereCount++;
                }
                foreach (Expression t in m.Arguments)
                {
                    Visit(t);
                }
                _commandType = QueryCommandType.AnyCommand;
                break;

            case "FirstOrDefault":
            case "First":
                if (m.Arguments.Count > 1)
                {
                    _exclusiveWhereCount++;
                }
                foreach (Expression t in m.Arguments)
                {
                    Visit(t);
                }
                _commandType = QueryCommandType.FirstOrDefaultCommand;
                break;

            case "SingleOrDefault":
                if (m.Arguments.Count > 1)
                {
                    _exclusiveWhereCount++;
                }
                foreach (Expression t in m.Arguments)
                {
                    Visit(t);
                }
                _commandType = QueryCommandType.SingleOrDefaultCommand;
                break;

            case "Single":
                if (m.Arguments.Count > 1)
                {
                    _exclusiveWhereCount++;
                }
                foreach (Expression t in m.Arguments)
                {
                    Visit(t);
                }
                _commandType = QueryCommandType.SingleCommand;
                break;

            case "LongCount":
            case "Count":
                _isLongCount = m.Method.Name == "LongCount";
                if (m.Arguments.Count > 1)
                {
                    _exclusiveWhereCount++;
                }
                foreach (Expression t in m.Arguments)
                {
                    Visit(t);
                }
                _commandType = QueryCommandType.CountCommand;
                break;

            case "ListAttributes":
                Dictionary <string, string> attributes = null;
                foreach (Expression t in m.Arguments)
                {
                    if (t.Type == typeof(string[]))
                    {
                        attributes = (((ConstantExpression)t).Value as string[]).ToDictionary(s => s);
                    }
                    else
                    {
                        Visit(t);
                    }
                }
                _commandOptions = new ListAttributesQueryCommandOptions(attributes);
                _commandType    = QueryCommandType.StandardCommand;
                break;

            case "FilterWith":
                foreach (Expression t in m.Arguments)
                {
                    if (t.Type == typeof(string))
                    {
                        _sbCustomFilter.Append(((ConstantExpression)t).Value.ToString());
                        _customFiltersCount++;
                    }
                    else
                    {
                        Visit(t);
                    }
                }
                _commandType = QueryCommandType.StandardCommand;
                break;

            case "Select":
                foreach (Expression t in m.Arguments)
                {
                    var lambda = StripQuotes(t) as LambdaExpression;
                    if (lambda != null)
                    {
                        if (_commandOptions != null)
                        {
                            throw new FilterException("Cannot have multiple Select projections.");
                        }

                        if (IsDynamic)
                        {
                            var projection = new DynamicSelectProjector(_mapping.Properties)
                                             .ProjectProperties(lambda);

                            _commandOptions = new DynamicQueryCommandOptions(projection);
                        }
                        else
                        {
                            var projection = new SelectProjector(_mapping.Properties)
                                             .ProjectProperties(lambda);
                            _commandOptions = new ProjectionQueryCommandOptions(_mapping, projection);
                        }
                    }
                    else if (t.Type == typeof(string[]))
                    {
                        if (!IsDynamic)
                        {
                            throw new FilterException(
                                      "Cannot use a string attribute projection with a static type.");
                        }
                        if (_commandOptions != null)
                        {
                            throw new FilterException("Cannot have multiple Select projections.");
                        }
                        _commandOptions =
                            new DynamicQueryCommandOptions(((ConstantExpression)t).Value as string[]);
                    }
                    else
                    {
                        Visit(t);
                    }
                }
                _commandType = QueryCommandType.StandardCommand;
                break;

            case "ToPage":
                int    pageSize = 0;
                byte[] nextPage = null;
                foreach (Expression t in m.Arguments)
                {
                    if (t.Type == typeof(int))
                    {
                        pageSize = (int)((ConstantExpression)t).Value;
                    }
                    else if (t.Type == typeof(byte[]))
                    {
                        nextPage = (byte[])((ConstantExpression)t).Value;
                    }
                    else if (t.Type == typeof(string))
                    {
                        _pagingFilter = (string)((ConstantExpression)t).Value;
                    }
                    else
                    {
                        Visit(t);
                    }
                }
                _pagingOptions = new PagingOptions(pageSize, nextPage);
                break;

            case "InPagesOf":
                foreach (Expression t in m.Arguments)
                {
                    if (t.Type == typeof(int))
                    {
                        _toListPageSize = (int)((ConstantExpression)t).Value;
                    }
                    else
                    {
                        Visit(t);
                    }
                }
                _commandType = QueryCommandType.StandardCommand;
                break;

            case "ToList":
                foreach (Expression t in m.Arguments)
                {
                    Visit(t);
                }
                _commandType = QueryCommandType.StandardCommand;
                break;

            case "Take":
                int takeAmount = 0;
                foreach (Expression t in m.Arguments)
                {
                    if (t.Type == typeof(int))
                    {
                        takeAmount = (int)((ConstantExpression)t).Value;
                    }
                    else
                    {
                        Visit(t);
                    }
                }
                _takeSize = takeAmount;
                break;

            case "Skip":
                int skipAmount = 0;
                foreach (Expression t in m.Arguments)
                {
                    if (t.Type == typeof(int))
                    {
                        skipAmount = (int)((ConstantExpression)t).Value;
                    }
                    else
                    {
                        Visit(t);
                    }
                }
                if (skipAmount < 0)
                {
                    throw new ArgumentException("Skip value must be greater than zero.");
                }
                _skipSize = skipAmount;
                break;

            case "WithControls":
                if (_controls == null)
                {
                    _controls = new List <DirectoryControl>();
                }
                foreach (Expression t in m.Arguments)
                {
                    IEnumerable <DirectoryControl> controls;
                    if (t is ConstantExpression && (controls = ((ConstantExpression)t).Value as IEnumerable <DirectoryControl>) != null)
                    {
                        _controls.AddRange(controls);
                    }
                    else
                    {
                        Visit(t);
                    }
                }
                break;

            case "OrderByDescending":
            case "ThenByDescending":
                string descRule = null;
                if (_sortingOptions == null)
                {
                    _sortingOptions = new SortingOptions();
                }
                foreach (Expression t in m.Arguments)
                {
                    var lambda = StripQuotes(t) as LambdaExpression;
                    if (lambda != null)
                    {
                        var attribute = GetMemberName((MemberExpression)lambda.Body);
                        _sortingOptions.AddSort(attribute, true);
                    }
                    else if (t.Type == typeof(string))
                    {
                        if (m.Arguments.IndexOf(t) == 1)
                        {
                            _sortingOptions.AddSort(((ConstantExpression)t).Value.ToString(), true);
                        }
                        else
                        {
                            descRule = ((ConstantExpression)t).Value.ToString();
                        }
                    }
                    else
                    {
                        Visit(t);
                    }
                }
                _sortingOptions.SetMatchingRule(descRule);
                break;

            case "OrderBy":
            case "ThenBy":
                if (_sortingOptions == null)
                {
                    _sortingOptions = new SortingOptions();
                }
                string ascRule = null;
                foreach (Expression t in m.Arguments)
                {
                    var lambda = StripQuotes(t) as LambdaExpression;
                    if (lambda != null)
                    {
                        var attribute = GetMemberName((MemberExpression)lambda.Body);
                        _sortingOptions.AddSort(attribute, false);
                    }
                    else if (t.Type == typeof(string))
                    {
                        if (m.Arguments.IndexOf(t) == 1)
                        {
                            _sortingOptions.AddSort(((ConstantExpression)t).Value.ToString(), false);
                        }
                        else
                        {
                            ascRule = ((ConstantExpression)t).Value.ToString();
                        }
                    }
                    else
                    {
                        Visit(t);
                    }
                }
                _sortingOptions.SetMatchingRule(ascRule);
                break;

            case "IgnoreOC":
                foreach (Expression t in m.Arguments)
                {
                    if (t.Type == typeof(OC))
                    {
                        _ignoreOc = (OC)((ConstantExpression)t).Value;
                    }
                    else
                    {
                        Visit(t);
                    }
                }
                break;

            case "IncludeOC":
                foreach (Expression t in m.Arguments)
                {
                    if (t.Type == typeof(OC))
                    {
                        _includeOc = (OC)((ConstantExpression)t).Value;
                    }
                    else
                    {
                        Visit(t);
                    }
                }
                break;
            }
        }