public static CosmosClient CreateMockCosmosClient(CosmosRequestHandler preProcessingHandler = null, CosmosConfiguration configuration = null)
        {
            DocumentClient      documentClient      = new MockDocumentClient();
            CosmosConfiguration cosmosConfiguration =
                configuration?.AddCustomHandlers(preProcessingHandler)
                ?? new CosmosConfiguration("http://localhost", Guid.NewGuid().ToString())
                .AddCustomHandlers(preProcessingHandler);

            return(new CosmosClient(
                       cosmosConfiguration,
                       documentClient));
        }
Exemplo n.º 2
0
        public static CosmosClient CreateMockCosmosClient(Action <CosmosClientBuilder> customizeClientBuilder = null)
        {
            DocumentClient documentClient = new MockDocumentClient();

            CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder("http://localhost", Guid.NewGuid().ToString());

            if (customizeClientBuilder != null)
            {
                customizeClientBuilder(cosmosClientBuilder);
            }

            return(cosmosClientBuilder.Build(documentClient));
        }
        public static CosmosClient CreateMockCosmosClient(Action <CosmosClientBuilder> customizeClientBuilder = null)
        {
            DocumentClient documentClient = new MockDocumentClient();

            CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder("http://localhost", MockAccountKey);

            if (customizeClientBuilder != null)
            {
                customizeClientBuilder(cosmosClientBuilder);
            }

            return(cosmosClientBuilder.Build(documentClient));
        }
        public static CosmosClient CreateMockCosmosClient(
            Action <CosmosClientBuilder> customizeClientBuilder = null,
            Cosmos.ConsistencyLevel?accountConsistencyLevel     = null)
        {
            DocumentClient documentClient;

            if (accountConsistencyLevel.HasValue)
            {
                documentClient = new MockDocumentClient(accountConsistencyLevel.Value);
            }
            else
            {
                documentClient = new MockDocumentClient();
            }

            CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder("http://localhost", Guid.NewGuid().ToString());

            if (customizeClientBuilder != null)
            {
                customizeClientBuilder(cosmosClientBuilder);
            }

            return(cosmosClientBuilder.Build(documentClient));
        }
        public async Task VerifySendUsesOringianlContinuationOnDocumentClientExceptionAfterRetry()
        {
            Mock <PartitionRoutingHelper> partitionRoutingHelperMock = this.GetPartitionRoutingHelperMock();

            //throw a DocumentClientException
            partitionRoutingHelperMock.Setup(m => m.TryAddPartitionKeyRangeToContinuationTokenAsync(
                                                 It.IsAny <INameValueCollection>(),
                                                 It.IsAny <List <Range <string> > >(),
                                                 It.IsAny <IRoutingMapProvider>(),
                                                 It.Is <string>(x => x == CollectionId),
                                                 It.IsAny <ResolvedRangeInfo>(),
                                                 It.IsAny <RntbdConstants.RntdbEnumerationDirection>()
                                                 )).ThrowsAsync(new DocumentClientException("error", HttpStatusCode.ServiceUnavailable, SubStatusCodes.Unknown));

            PartitionKeyRangeHandler partitionKeyRangeHandler = new PartitionKeyRangeHandler(MockDocumentClient.CreateMockCosmosClient(), partitionRoutingHelperMock.Object);

            TestHandler testHandler = new TestHandler(async(request, cancellationToken) => {
                CosmosResponseMessage successResponse = await TestHandler.ReturnSuccess();
                successResponse.Headers.Remove(HttpConstants.HttpHeaders.Continuation); //Clobber original continuation
                return(successResponse);
            });

            partitionKeyRangeHandler.InnerHandler = testHandler;

            //Pass valid collections path because it is required by DocumentServiceRequest's constructor. This can't be mocked because ToDocumentServiceRequest() is an extension method
            CosmosRequestMessage initialRequest = new CosmosRequestMessage(HttpMethod.Get, new Uri($"{Paths.DatabasesPathSegment}/test/{Paths.CollectionsPathSegment}/test", UriKind.Relative));

            initialRequest.OperationType = OperationType.ReadFeed;
            initialRequest.Headers.Add(HttpConstants.HttpHeaders.Continuation, Continuation);
            CosmosResponseMessage response = await partitionKeyRangeHandler.SendAsync(initialRequest, CancellationToken.None);

            Assert.IsFalse(response.IsSuccessStatusCode);
            Assert.AreEqual(System.Net.HttpStatusCode.ServiceUnavailable, response.StatusCode);
            //Check if original continuation was restored
            Assert.AreEqual(Continuation, response.Headers.GetValues(HttpConstants.HttpHeaders.Continuation).First());
            Assert.AreEqual(Continuation, initialRequest.Headers.GetValues(HttpConstants.HttpHeaders.Continuation).First());
        }
        public async Task VerifySendUsesOringianlContinuationOnNonSuccessfulResponse()
        {
            Mock <PartitionRoutingHelper> partitionRoutingHelperMock = this.GetPartitionRoutingHelperMock();
            PartitionKeyRangeHandler      partitionKeyRangeHandler   = new PartitionKeyRangeHandler(MockDocumentClient.CreateMockCosmosClient(), partitionRoutingHelperMock.Object);

            TestHandler testHandler = new TestHandler(async(request, cancellationToken) => {
                CosmosResponseMessage errorResponse = await TestHandler.ReturnStatusCode(HttpStatusCode.Gone);
                errorResponse.Headers.Remove(HttpConstants.HttpHeaders.Continuation); //Clobber original continuation
                return(errorResponse);
            });

            partitionKeyRangeHandler.InnerHandler = testHandler;

            //Pass valid collections path because it is required by DocumentServiceRequest's constructor. This can't be mocked because ToDocumentServiceRequest() is an extension method
            CosmosRequestMessage initialRequest = new CosmosRequestMessage(HttpMethod.Get, new Uri($"{Paths.DatabasesPathSegment}/test/{Paths.CollectionsPathSegment}/test", UriKind.Relative));

            initialRequest.OperationType = OperationType.ReadFeed;
            initialRequest.Headers.Add(HttpConstants.HttpHeaders.Continuation, Continuation);
            CosmosResponseMessage response = await partitionKeyRangeHandler.SendAsync(initialRequest, CancellationToken.None);

            Assert.IsFalse(response.IsSuccessStatusCode);
            Assert.AreEqual(System.Net.HttpStatusCode.Gone, response.StatusCode);
            //Check if original continuation was restored
            Assert.AreEqual(Continuation, response.Headers.GetValues(HttpConstants.HttpHeaders.Continuation).First());
            Assert.AreEqual(Continuation, initialRequest.Headers.GetValues(HttpConstants.HttpHeaders.Continuation).First());
        }