public async Task BuildPassesEnableCrossPartitionQuery_WhenLeaseCollectionIsPartitionedById()
        {
            var leaseCollection = MockHelpers.CreateCollection(
                "collectionId",
                "collectionRid",
                new PartitionKeyDefinition {
                Paths = { "/id" }
            },
                collectionLink);

            var leaseClient = this.CreateMockDocumentClient(collection);

            Mock.Get(leaseClient)
            .Setup(c => c.ReadDocumentCollectionAsync(
                       It.IsAny <Uri>(),
                       It.IsAny <RequestOptions>()))
            .ReturnsAsync(new ResourceResponse <DocumentCollection>(leaseCollection));

            this.builder
            .WithFeedDocumentClient(this.CreateMockDocumentClient())
            .WithLeaseDocumentClient(leaseClient)
            .WithObserverFactory(Mock.Of <IChangeFeedObserverFactory>());
            await this.builder.BuildAsync();

            await this.builder.LeaseStoreManager.GetAllLeasesAsync();

            Mock.Get(leaseClient)
            .Verify(c => c.CreateDocumentQuery <Document>(
                        It.IsAny <string>(),
                        It.IsAny <SqlQuerySpec>(),
                        It.Is <FeedOptions>(opt => opt.EnableCrossPartitionQuery == true)));
        }
        private void SetupBuilderForPartitionedLeaseCollection(string partitionKey)
        {
            var partitionedCollection = MockHelpers.CreateCollection(
                collection.Id,
                collection.ResourceId,
                new PartitionKeyDefinition {
                Paths = { partitionKey }
            });

            this.builder
            .WithFeedDocumentClient(this.CreateMockDocumentClient())
            .WithLeaseDocumentClient(this.CreateMockDocumentClient(partitionedCollection))
            .WithObserverFactory(Mock.Of <IChangeFeedObserverFactory>());
        }
        public async Task BuildPassesPartitionKey_WhenLeaseCollectionIsPartitionedById()
        {
            var leaseCollection = MockHelpers.CreateCollection(
                "collectionId",
                "collectionRid",
                new PartitionKeyDefinition {
                Paths = { "/id" }
            },
                collectionLink);

            var lease = Mock.Of <ILease>();

            Mock.Get(lease)
            .SetupGet(l => l.Id)
            .Returns("leaseId");

            var leaseClient = this.CreateMockDocumentClient(collection);

            Mock.Get(leaseClient)
            .Setup(c => c.ReadDocumentCollectionAsync(
                       It.IsAny <Uri>(),
                       It.IsAny <RequestOptions>()))
            .ReturnsAsync(new ResourceResponse <DocumentCollection>(leaseCollection));
            Mock.Get(leaseClient)
            .Setup(c => c.ReadDocumentAsync(
                       It.IsAny <Uri>(),
                       It.IsAny <RequestOptions>(),
                       It.IsAny <CancellationToken>()))
            .Callback((Uri uri, RequestOptions options, CancellationToken token) =>
            {
                if (new PartitionKey(lease.Id).Equals(options.PartitionKey))
                {
                    throw DocumentExceptionHelpers.CreateNotFoundException();       // Success code path: cause lease lost.
                }
                throw new Exception("Failure");
            });

            this.builder
            .WithFeedDocumentClient(this.CreateMockDocumentClient())
            .WithLeaseDocumentClient(leaseClient)
            .WithObserverFactory(Mock.Of <IChangeFeedObserverFactory>());
            await this.builder.BuildAsync();

            Exception exception = await Record.ExceptionAsync(() => this.builder.LeaseStoreManager.ReleaseAsync(lease));

            Assert.Equal(typeof(LeaseLostException), exception.GetType());
        }