private async Task ItemOperations(CosmosContainer container, bool containerNotExist) { if (containerNotExist) { dynamic randomItem = new { id = "test", pk = "doesnotexist" }; Stream create = jsonSerializer.ToStream <dynamic>(randomItem); this.VerifyNotFoundResponse(await container.CreateItemStreamAsync(new PartitionKey(randomItem.pk), create)); var queryIterator = container.CreateItemQueryStream("select * from t where true", maxConcurrency: 2); this.VerifyQueryNotFoundResponse(await queryIterator.FetchNextSetAsync()); var feedIterator = container.GetItemsStreamIterator(); this.VerifyNotFoundResponse(await feedIterator.FetchNextSetAsync()); dynamic randomUpsertItem = new { id = DoesNotExist, pk = DoesNotExist, status = 42 }; Stream upsert = jsonSerializer.ToStream <dynamic>(randomUpsertItem); this.VerifyNotFoundResponse(await container.UpsertItemStreamAsync( partitionKey: new Cosmos.PartitionKey(randomUpsertItem.pk), streamPayload: upsert)); } this.VerifyNotFoundResponse(await container.ReadItemStreamAsync(partitionKey: new Cosmos.PartitionKey(DoesNotExist), id: DoesNotExist)); this.VerifyNotFoundResponse(await container.DeleteItemStreamAsync(partitionKey: new Cosmos.PartitionKey(DoesNotExist), id: DoesNotExist)); dynamic randomReplaceItem = new { id = "test", pk = "doesnotexist", status = 42 }; Stream replace = jsonSerializer.ToStream <dynamic>(randomReplaceItem); this.VerifyNotFoundResponse(await container.ReplaceItemStreamAsync( partitionKey: new Cosmos.PartitionKey(randomReplaceItem.pk), id: randomReplaceItem.id, streamPayload: replace)); }
private async Task VerifyItemOperations( Cosmos.PartitionKey partitionKey, string partitionKeySerialized, dynamic testItem, ItemRequestOptions requestOptions = null) { CosmosResponseMessage response = null; HttpStatusCode httpStatusCode = HttpStatusCode.OK; int testHandlerHitCount = 0; TestHandler testHandler = new TestHandler((request, cancellationToken) => { Assert.IsTrue(request.RequestUri.OriginalString.StartsWith(@"dbs/testdb/colls/testcontainer")); Assert.AreEqual(requestOptions, request.RequestOptions); Assert.AreEqual(ResourceType.Document, request.ResourceType); Assert.IsNotNull(request.Headers.PartitionKey); Assert.AreEqual(partitionKeySerialized, request.Headers.PartitionKey); testHandlerHitCount++; response = new CosmosResponseMessage(httpStatusCode, request, errorMessage: null); response.Content = request.Content; return(Task.FromResult(response)); }); CosmosClient client = MockCosmosUtil.CreateMockCosmosClient( (builder) => builder.AddCustomHandlers(testHandler)); CosmosContainer container = client.GetDatabase("testdb") .GetContainer("testcontainer"); ItemResponse <dynamic> itemResponse = await container.CreateItemAsync <dynamic>( item : testItem, requestOptions : requestOptions); Assert.IsNotNull(itemResponse); Assert.AreEqual(httpStatusCode, itemResponse.StatusCode); itemResponse = await container.ReadItemAsync <dynamic>( partitionKey : partitionKey, id : testItem.id, requestOptions : requestOptions); Assert.IsNotNull(itemResponse); Assert.AreEqual(httpStatusCode, itemResponse.StatusCode); itemResponse = await container.UpsertItemAsync <dynamic>( item : testItem, requestOptions : requestOptions); Assert.IsNotNull(itemResponse); Assert.AreEqual(httpStatusCode, itemResponse.StatusCode); itemResponse = await container.ReplaceItemAsync <dynamic>( id : testItem.id, item : testItem, requestOptions : requestOptions); Assert.IsNotNull(itemResponse); Assert.AreEqual(httpStatusCode, itemResponse.StatusCode); itemResponse = await container.DeleteItemAsync <dynamic>( partitionKey : partitionKey, id : testItem.id, requestOptions : requestOptions); Assert.IsNotNull(itemResponse); Assert.AreEqual(httpStatusCode, itemResponse.StatusCode); Assert.AreEqual(5, testHandlerHitCount, "An operation did not make it to the handler"); CosmosJsonSerializerCore jsonSerializer = new CosmosJsonSerializerCore(); using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem)) { using (CosmosResponseMessage streamResponse = await container.CreateItemStreamAsync( partitionKey: partitionKey, streamPayload: itemStream, requestOptions: requestOptions)) { Assert.IsNotNull(streamResponse); Assert.AreEqual(httpStatusCode, streamResponse.StatusCode); } } using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem)) { using (CosmosResponseMessage streamResponse = await container.ReadItemStreamAsync( partitionKey: partitionKey, id: testItem.id, requestOptions: requestOptions)) { Assert.IsNotNull(streamResponse); Assert.AreEqual(httpStatusCode, streamResponse.StatusCode); } } using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem)) { using (CosmosResponseMessage streamResponse = await container.UpsertItemStreamAsync( partitionKey: partitionKey, streamPayload: itemStream, requestOptions: requestOptions)) { Assert.IsNotNull(streamResponse); Assert.AreEqual(httpStatusCode, streamResponse.StatusCode); } } using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem)) { using (CosmosResponseMessage streamResponse = await container.ReplaceItemStreamAsync( partitionKey: partitionKey, id: testItem.id, streamPayload: itemStream, requestOptions: requestOptions)) { Assert.IsNotNull(streamResponse); Assert.AreEqual(httpStatusCode, streamResponse.StatusCode); } } using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem)) { using (CosmosResponseMessage streamResponse = await container.DeleteItemStreamAsync( partitionKey: partitionKey, id: testItem.id, requestOptions: requestOptions)) { Assert.IsNotNull(streamResponse); Assert.AreEqual(httpStatusCode, streamResponse.StatusCode); } } Assert.AreEqual(10, testHandlerHitCount, "A stream operation did not make it to the handler"); }
private async Task VerifyItemNullPartitionKeyExpectations( dynamic testItem, ItemRequestOptions requestOptions = null) { TestHandler testHandler = new TestHandler((request, cancellationToken) => { Assert.IsNotNull(request.Headers.PartitionKey); Assert.AreEqual(Documents.Routing.PartitionKeyInternal.Undefined.ToString(), request.Headers.PartitionKey.ToString()); return(Task.FromResult(new CosmosResponseMessage(HttpStatusCode.OK))); }); CosmosClient client = MockCosmosUtil.CreateMockCosmosClient( (cosmosClientBuilder) => cosmosClientBuilder.AddCustomHandlers(testHandler)); CosmosContainer container = client.GetDatabase("testdb") .GetContainer("testcontainer"); await container.CreateItemAsync <dynamic>( item : testItem, requestOptions : requestOptions); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => { await container.ReadItemAsync <dynamic>( partitionKey: null, id: testItem.id, requestOptions: requestOptions); }, "ReadItemAsync should throw ArgumentNullException without the correct request option set."); await container.UpsertItemAsync <dynamic>( item : testItem, requestOptions : requestOptions); await container.ReplaceItemAsync <dynamic>( id : testItem.id, item : testItem, requestOptions : requestOptions); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => { await container.DeleteItemAsync <dynamic>( partitionKey: null, id: testItem.id, requestOptions: requestOptions); }, "DeleteItemAsync should throw ArgumentNullException without the correct request option set."); CosmosJsonSerializerCore jsonSerializer = new CosmosJsonSerializerCore(); using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem)) { await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => { await container.CreateItemStreamAsync( partitionKey: null, streamPayload: itemStream, requestOptions: requestOptions); }, "CreateItemAsync should throw ArgumentNullException without the correct request option set."); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => { await container.ReadItemStreamAsync( partitionKey: null, id: testItem.id, requestOptions: requestOptions); }, "ReadItemAsync should throw ArgumentNullException without the correct request option set."); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => { await container.UpsertItemStreamAsync( partitionKey: null, streamPayload: itemStream, requestOptions: requestOptions); }, "UpsertItemAsync should throw ArgumentNullException without the correct request option set."); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => { await container.ReplaceItemStreamAsync( partitionKey: null, id: testItem.id, streamPayload: itemStream, requestOptions: requestOptions); }, "ReplaceItemAsync should throw ArgumentNullException without the correct request option set."); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => { await container.DeleteItemStreamAsync( partitionKey: null, id: testItem.id, requestOptions: requestOptions); }, "DeleteItemAsync should throw ArgumentNullException without the correct request option set."); } }