internal static bool ResourceDoesnotExists <T>(string resourceId, DocumentClient client, string ownerId = null) where T : Resource, new() { T nonExistingResource; try { if (typeof(T) != typeof(Attachment)) { INameValueCollection responseHeaders; nonExistingResource = TestCommon.ReadWithRetry <T>(client, resourceId, out responseHeaders); } else { Attachment nonExisitingAttachment = client.Read <Attachment>(resourceId); } return(false); } catch (DocumentClientException clientException) { TestCommon.AssertException(clientException, HttpStatusCode.NotFound); return(true); } }
private async Task ValidatePartitionedCollectionCRUDAsync() { using (DocumentClient writeRegionClient = TestCommon.CreateClient(true, createForGeoRegion: false)) { using (DocumentClient readRegionClient = TestCommon.CreateClient(true, defaultConsistencyLevel: ConsistencyLevel.Session, createForGeoRegion: true, enableEndpointDiscovery: false)) { string databaseName = "geocollcruddb-" + Guid.NewGuid(); CosmosDatabaseSettings db = new CosmosDatabaseSettings { Id = databaseName, }; ResourceResponse <CosmosDatabaseSettings> dbResponse = await writeRegionClient.CreateDatabaseAsync(db); Assert.AreEqual(HttpStatusCode.Created, dbResponse.StatusCode, "Status code should be Created (201)"); Util.ValidateCommonCustomHeaders(dbResponse.Headers); this.ValidateDatabaseResponseBody(dbResponse.Resource, databaseName); string databaseSelfLink = dbResponse.Resource.SelfLink; // Add some delay since we do async replication between geo-regions await Task.Delay(GlobalDatabaseAccountTests.WaitDurationForAsyncReplication); ResourceResponse <CosmosDatabaseSettings> readRegionDbResponse = await readRegionClient.ReadDatabaseAsync(databaseSelfLink); this.ValidateDatabaseResponseBody(readRegionDbResponse.Resource, databaseName); Assert.AreEqual(dbResponse.Resource.ETag, readRegionDbResponse.Resource.ETag); Assert.AreEqual(dbResponse.Resource.ResourceId, readRegionDbResponse.Resource.ResourceId); Assert.AreEqual(dbResponse.Resource.SelfLink, readRegionDbResponse.Resource.SelfLink); string collectionName = "geocollcrudcoll-" + Guid.NewGuid(); CosmosContainerSettings collection = new CosmosContainerSettings() { Id = collectionName, PartitionKey = new PartitionKeyDefinition { Paths = new Collection <string> { "/id" } } }; ResourceResponse <CosmosContainerSettings> collResponse = await writeRegionClient.CreateDocumentCollectionAsync(databaseSelfLink, collection, new RequestOptions { OfferThroughput = 12000 }); Assert.AreEqual(HttpStatusCode.Created, collResponse.StatusCode, "Status code should be Created (201)"); string collectionSelfLink = collResponse.Resource.SelfLink; // Add some delay since we do async replication between geo-regions await Task.Delay(GlobalDatabaseAccountTests.WaitDurationForAsyncReplication); ResourceResponse <CosmosContainerSettings> readRegionCollResponse = await readRegionClient.ReadDocumentCollectionAsync(collectionSelfLink, new RequestOptions { SessionToken = collResponse.SessionToken }); Assert.AreEqual(collectionName, readRegionCollResponse.Resource.Id); Assert.AreEqual(collResponse.Resource.ETag, readRegionCollResponse.Resource.ETag); Assert.AreEqual(collResponse.Resource.ResourceId, readRegionCollResponse.Resource.ResourceId); Assert.AreEqual(collResponse.Resource.SelfLink, readRegionCollResponse.Resource.SelfLink); // Create a document string documentName = "geocollcruddoc-" + Guid.NewGuid(); Document document = new Document() { Id = documentName, }; ResourceResponse <Document> docResponse = await writeRegionClient.CreateDocumentAsync(collectionSelfLink, document); Assert.AreEqual(HttpStatusCode.Created, docResponse.StatusCode, "Status code should be Created (201)"); Util.ValidateCommonCustomHeaders(docResponse.Headers); string documentSelfLink = docResponse.Resource.SelfLink; // Add some delay since we do async replication between geo-regions await Task.Delay(GlobalDatabaseAccountTests.WaitDurationForAsyncReplication); ResourceResponse <Document> readRegionDocResponse = await readRegionClient.ReadDocumentAsync(documentSelfLink, new RequestOptions { PartitionKey = new PartitionKey(document.Id), SessionToken = docResponse.SessionToken }); Assert.AreEqual(documentName, readRegionDocResponse.Resource.Id); Assert.AreEqual(docResponse.Resource.ETag, readRegionDocResponse.Resource.ETag); Assert.AreEqual(docResponse.Resource.ResourceId, readRegionDocResponse.Resource.ResourceId); Assert.AreEqual(docResponse.Resource.SelfLink, readRegionDocResponse.Resource.SelfLink); // Delete document from write region await writeRegionClient.DeleteDocumentAsync(documentSelfLink, new RequestOptions { PartitionKey = new PartitionKey(document.Id) }); await Task.Delay(GlobalDatabaseAccountTests.WaitDurationForAsyncReplication); // Try reading document from read region try { await readRegionClient.ReadDocumentAsync(documentSelfLink, new RequestOptions { PartitionKey = new PartitionKey(document.Id), SessionToken = docResponse.SessionToken }); Assert.Fail("Expected exception when reading deleted document from read region"); } catch (DocumentClientException clientException) { TestCommon.AssertException(clientException, HttpStatusCode.NotFound); } // Create another document documentName = "geocolldeltestdoc-" + Guid.NewGuid(); document = new Document() { Id = documentName, }; docResponse = await writeRegionClient.CreateDocumentAsync(collectionSelfLink, document); Assert.AreEqual(HttpStatusCode.Created, docResponse.StatusCode, "Status code should be Created (201)"); Util.ValidateCommonCustomHeaders(docResponse.Headers); documentSelfLink = docResponse.Resource.SelfLink; // Add some delay since we do async replication between geo-regions await Task.Delay(GlobalDatabaseAccountTests.WaitDurationForAsyncReplication); readRegionDocResponse = await readRegionClient.ReadDocumentAsync(documentSelfLink, new RequestOptions { PartitionKey = new PartitionKey(document.Id), SessionToken = docResponse.SessionToken }); Assert.AreEqual(documentName, readRegionDocResponse.Resource.Id); Assert.AreEqual(docResponse.Resource.ETag, readRegionDocResponse.Resource.ETag); Assert.AreEqual(docResponse.Resource.ResourceId, readRegionDocResponse.Resource.ResourceId); Assert.AreEqual(docResponse.Resource.SelfLink, readRegionDocResponse.Resource.SelfLink); ResourceResponse <CosmosContainerSettings> replaceCollResponse = await writeRegionClient.ReplaceDocumentCollectionAsync(collResponse.Resource); // TODO: Collection delete is not working, and that is why if you have this test as the last test, it will cause problem to other tests // Delete collection from write region await writeRegionClient.DeleteDocumentCollectionAsync(collectionSelfLink); await Task.Delay(GlobalDatabaseAccountTests.WaitDurationForAsyncReplication); // Try reading collection from write region try { await writeRegionClient.ReadDocumentCollectionAsync(collectionSelfLink, new RequestOptions { SessionToken = replaceCollResponse.SessionToken }); Assert.Fail("Expected exception when reading deleted collection from write region"); } catch (DocumentClientException clientException) { TestCommon.AssertException(clientException, HttpStatusCode.NotFound); } //TODO: Aditya, Investigate // Try reading collection from read region //try //{ // await readRegionClient.ReadDocumentCollectionAsync(collectionSelfLink, new RequestOptions { SessionToken = replaceCollResponse.SessionToken }); // Assert.Fail("Expected exception when reading deleted collection from read region"); //} //catch (DocumentClientException clientException) //{ // TestCommon.AssertException(clientException, HttpStatusCode.NotFound); //} // Now try reading the document from both the regions try { await writeRegionClient.ReadDocumentAsync(documentSelfLink, new RequestOptions { PartitionKey = new PartitionKey(document.Id), SessionToken = docResponse.SessionToken }); Assert.Fail("Expected exception when reading document of a deleted collection from write region"); } catch (DocumentClientException clientException) { TestCommon.AssertException(clientException, HttpStatusCode.NotFound); } //TODO: Aditya, Investigate //try //{ // await readRegionClient.ReadDocumentAsync(documentSelfLink, new RequestOptions { PartitionKey = new PartitionKey(document.Id), SessionToken = docResponse.SessionToken }); // Assert.Fail("Expected exception when reading document of a deleted collection from read region"); //} //catch (DocumentClientException clientException) //{ // TestCommon.AssertException(clientException, HttpStatusCode.NotFound); //} // Delete the database await writeRegionClient.DeleteDatabaseAsync(databaseSelfLink); // Add some delay since we do async replication between geo-regions await Task.Delay(GlobalDatabaseAccountTests.WaitDurationForAsyncReplication); // Try reading from read region try { await readRegionClient.ReadDatabaseAsync(databaseSelfLink); Assert.Fail("Expected exception when reading deleted database from read region"); } catch (DocumentClientException clientException) { TestCommon.AssertException(clientException, HttpStatusCode.NotFound); } } } }