Example #1
0
        private async Task TestPreferredRegionOrderAsync()
        {
            Uri    globalEndpointUri = new Uri(ConfigurationManager.AppSettings["GatewayEndpoint"]);
            string authKey           = ConfigurationManager.AppSettings["MasterKey"];

            ConnectionPolicy connectionPolicy = new ConnectionPolicy();

            connectionPolicy.PreferredLocations.Add(ConfigurationManager.AppSettings["Location"]);  //write region
            connectionPolicy.PreferredLocations.Add(ConfigurationManager.AppSettings["Location2"]); // read region

            DocumentClient client = new DocumentClient(globalEndpointUri, authKey, connectionPolicy);

            CosmosDatabaseSettings database = await client.CreateDatabaseAsync(new CosmosDatabaseSettings { Id = Guid.NewGuid().ToString() });

            CosmosContainerSettings collection = await client.CreateDocumentCollectionAsync(database.SelfLink, new CosmosContainerSettings { Id = Guid.NewGuid().ToString() });

            // todo: SessionToken container has a bug which prevent the session consistency read. So we sleep to make sure it is replicated.
            await Task.Delay(GlobalDatabaseAccountTests.WaitDurationForAsyncReplication);

            Document document =
                await client.CreateDocumentAsync(collection.SelfLink, new Document { Id = Guid.NewGuid().ToString() });

            Assert.AreEqual(client.WriteEndpoint, DNSHelper.GetResolvedUri(ConfigurationManager.AppSettings["GatewayEndpoint"]));
            // Ensure that the ReadEndpoint gets set to whatever is the first region in PreferredLocations irrespective whether it's read or write region
            Assert.AreEqual(client.ReadEndpoint, DNSHelper.GetResolvedUri(ConfigurationManager.AppSettings["GatewayEndpoint"]));
        }
        public async Task DatabaseContractTest()
        {
            DatabaseResponse response = await this.CreateDatabaseHelper();

            Assert.IsNotNull(response);
            Assert.IsTrue(response.RequestCharge > 0);
            Assert.IsNotNull(response.Headers);
            Assert.IsNotNull(response.Headers.ActivityId);

            CosmosDatabaseSettings databaseSettings = response.Resource;

            Assert.IsNotNull(databaseSettings.Id);
            Assert.IsNotNull(databaseSettings.ResourceId);
            Assert.IsNotNull(databaseSettings.ETag);
            Assert.IsTrue(databaseSettings.LastModified.HasValue);
            Assert.IsTrue(databaseSettings.LastModified.Value > new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), databaseSettings.LastModified.Value.ToString());

            CosmosDatabaseCore databaseCore = response.Database as CosmosDatabaseCore;

            Assert.IsNotNull(databaseCore);
            Assert.IsNotNull(databaseCore.LinkUri);
            Assert.IsFalse(databaseCore.LinkUri.ToString().StartsWith("/"));

            response = await response.Database.DeleteAsync(cancellationToken : this.cancellationToken);

            Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
        }
Example #3
0
        public void ResourceResponseStreamingTest()
        {
            using (DocumentClient client = TestCommon.CreateClient(true))
            {
                CosmosDatabaseSettings db = client.CreateDatabaseAsync(new CosmosDatabaseSettings()
                {
                    Id = Guid.NewGuid().ToString()
                }).Result.Resource;
                CosmosContainerSettings coll = TestCommon.CreateCollectionAsync(client, db, new CosmosContainerSettings()
                {
                    Id = Guid.NewGuid().ToString()
                }).Result;
                ResourceResponse <Document> doc = client.CreateDocumentAsync(coll.SelfLink, new Document()
                {
                    Id = Guid.NewGuid().ToString()
                }).Result;

                Assert.AreEqual(doc.ResponseStream.Position, 0);

                StreamReader streamReader = new StreamReader(doc.ResponseStream);
                string       text         = streamReader.ReadToEnd();

                Assert.AreEqual(doc.ResponseStream.Position, doc.ResponseStream.Length);

                try
                {
                    doc.Resource.ToString();
                    Assert.Fail("Deserializing Resource here should throw exception since the stream was already read");
                }
                catch (JsonReaderException ex)
                {
                    Console.WriteLine("Expected exception while deserializing Resource: " + ex.Message);
                }
            }
        }
        public async Task ImplicitConversion()
        {
            string databaseName = Guid.NewGuid().ToString();

            CosmosDatabaseResponse cosmosDatabaseResponse = await this.cosmosClient.Databases[databaseName].ReadAsync(cancellationToken: this.cancellationToken);
            CosmosDatabase         cosmosDatabase         = cosmosDatabaseResponse;
            CosmosDatabaseSettings cosmosDatabaseSettings = cosmosDatabaseResponse;

            Assert.IsNotNull(cosmosDatabase);
            Assert.IsNull(cosmosDatabaseSettings);

            cosmosDatabaseResponse = await this.CreateDatabaseHelper();

            cosmosDatabase         = cosmosDatabaseResponse;
            cosmosDatabaseSettings = cosmosDatabaseResponse;
            Assert.IsNotNull(cosmosDatabase);
            Assert.IsNotNull(cosmosDatabaseSettings);

            cosmosDatabaseResponse = await cosmosDatabase.DeleteAsync(cancellationToken : this.cancellationToken);

            cosmosDatabase         = cosmosDatabaseResponse;
            cosmosDatabaseSettings = cosmosDatabaseResponse;
            Assert.IsNotNull(cosmosDatabase);
            Assert.IsNull(cosmosDatabaseSettings);
        }
        public void ValidateCurrentWriteQuorumAndReplicaSetHeader()
        {
            DocumentClient         client = TestCommon.CreateClient(false);
            CosmosDatabaseSettings db     = null;

            try
            {
                var dbResource = client.CreateDatabaseAsync(new CosmosDatabaseSettings()
                {
                    Id = Guid.NewGuid().ToString()
                }).Result;
                db = dbResource.Resource;
                var coll = client.CreateDocumentCollectionAsync(db, new CosmosContainerSettings()
                {
                    Id = Guid.NewGuid().ToString()
                }).Result.Resource;
                var docResult = client.CreateDocumentAsync(coll, new Document()
                {
                    Id = Guid.NewGuid().ToString()
                }).Result;
                Assert.IsTrue(int.Parse(docResult.ResponseHeaders[WFConstants.BackendHeaders.CurrentWriteQuorum], CultureInfo.InvariantCulture) > 0);
                Assert.IsTrue(int.Parse(docResult.ResponseHeaders[WFConstants.BackendHeaders.CurrentReplicaSetSize], CultureInfo.InvariantCulture) > 0);
            }
            finally
            {
                client.DeleteDatabaseAsync(db).Wait();
            }
        }
Example #6
0
        public void DatabaseStreamDeserialzieTest()
        {
            string dbId = "946ad017-14d9-4cee-8619-0cbc62414157";
            string rid  = "vu9cAA==";
            string self = "dbs\\/vu9cAA==\\/";
            string etag = "00000000-0000-0000-f8ea-31d6e5f701d4";
            double ts   = 1555923784;

            DateTime UnixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            DateTime expected      = UnixStartTime.AddSeconds(ts);

            string testPyaload = "{\"id\":\"" + dbId
                                 + "\",\"_rid\":\"" + rid
                                 + "\",\"_self\":\"" + self
                                 + "\",\"_etag\":\"" + etag
                                 + "\",\"_colls\":\"colls\\/\",\"_users\":\"users\\/\",\"_ts\":" + ts + "}";

            CosmosDatabaseSettings deserializedPayload =
                JsonConvert.DeserializeObject <CosmosDatabaseSettings>(testPyaload);

            Assert.IsTrue(deserializedPayload.LastModified.HasValue);
            Assert.AreEqual(expected, deserializedPayload.LastModified.Value);
            Assert.AreEqual(dbId, deserializedPayload.Id);
            Assert.AreEqual(rid, deserializedPayload.ResourceId);
            Assert.AreEqual(etag, deserializedPayload.ETag);
        }
Example #7
0
        public void DatabaseSettingsDeSerializeTest()
        {
            string dbResponsePayload = @"{
                _colls : 'dbs/6GoAAA==/colls/',
                _users: 'dbs/6GoAAA==/users/',
                 id: 'QuickStarts',
                _rid: '6GoAAA==',
                _self: 'dbs/6GoAAA==/',
                _ts: 1530581163,
                _etag: '00002000-0000-0000-0000-5b3ad0ab0000'
                }";

            CosmosDatabaseSettings databaseSettings = SettingsContractTests.CosmosDeserialize <CosmosDatabaseSettings>(dbResponsePayload);
            Database db = SettingsContractTests.DirectDeSerialize <Database>(dbResponsePayload);

            // Not all are exposed in CosmosDatabaseSettings
            // so lets only validate relevant parts
            Assert.AreEqual(db.Id, databaseSettings.Id);
            Assert.AreEqual(db.ETag, databaseSettings.ETag);
            Assert.AreEqual(db.ResourceId, databaseSettings.ResourceId);

            Assert.AreEqual("QuickStarts", databaseSettings.Id);
            Assert.AreEqual("00002000-0000-0000-0000-5b3ad0ab0000", databaseSettings.ETag);
            Assert.AreEqual("6GoAAA==", databaseSettings.ResourceId);
        }
Example #8
0
        public static void Initialize(TestContext textContext)
        {
            var authKey          = ConfigurationManager.AppSettings["MasterKey"];
            var uri              = new Uri(ConfigurationManager.AppSettings["GatewayEndpoint"]);
            var connectionPolicy = new ConnectionPolicy
            {
                ConnectionMode          = ConnectionMode.Gateway,
                EnableEndpointDiscovery = true,
            };
            ConsistencyLevel?defaultConsistencyLevel = null;

            client = new DocumentClient(
                uri,
                authKey,
                new JsonSerializerSettings()
            {
                ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
                // We want to simulate the property not exist so ignoring the null value
                NullValueHandling = NullValueHandling.Ignore
            },
                connectionPolicy,
                defaultConsistencyLevel);
            var db = new CosmosDatabaseSettings()
            {
                Id = nameof(LinqTranslationBaselineTests)
            };

            try
            {
                var response = client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(db.Id)).Result;
            }
            catch { }
            testDb = client.CreateDatabaseAsync(db).Result;
        }
        public void TestInitialize()
        {
            this.client     = TestCommon.CreateClient(true);
            this.database   = TestCommon.CreateOrGetDatabase(this.client);
            this.collection = new CosmosContainerSettings()
            {
                Id = Guid.NewGuid().ToString()
            };
            this.collection.IndexingPolicy.IndexingMode = IndexingMode.Lazy;

            try
            {
                this.collection = this.client.CreateDocumentCollectionAsync(this.database, this.collection).Result;
            }
            catch (DocumentClientException exception)
            {
                Assert.Fail(exception.InnerException.Message);
            }

            this.triggerName = "uniqueConstraint_" + Guid.NewGuid().ToString("N");
            string triggerContent = File.ReadAllText("ScriptSampleTests_UniqueConstraint.js");
            CosmosTriggerSettings triggerResource = new CosmosTriggerSettings
            {
                Id               = this.triggerName,
                Body             = triggerContent,
                TriggerOperation = TriggerOperation.All,
                TriggerType      = TriggerType.Pre
            };
            CosmosTriggerSettings trigger = this.client.CreateTriggerAsync(this.collection.SelfLink, triggerResource).Result;
        }
        public async Task StreamCreateConflictTestAsync()
        {
            CosmosDatabaseSettings databaseSettings = new CosmosDatabaseSettings()
            {
                Id = Guid.NewGuid().ToString()
            };

            using (CosmosResponseMessage response = await this.cosmosClient.Databases.CreateDatabaseStreamAsync(CosmosResource.ToStream(databaseSettings)))
            {
                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
                Assert.IsNotNull(response.Headers);
                Assert.IsTrue(response.Headers.RequestCharge > 0);
            }

            // Stream operations do not throw exceptions.
            using (CosmosResponseMessage response = await this.cosmosClient.Databases.CreateDatabaseStreamAsync(CosmosResource.ToStream(databaseSettings)))
            {
                Assert.AreEqual(HttpStatusCode.Conflict, response.StatusCode);
                Assert.IsNotNull(response.Headers);
                Assert.IsTrue(response.Headers.RequestCharge > 0);
            }

            using (CosmosResponseMessage response = await this.cosmosClient.Databases[databaseSettings.Id].DeleteStreamAsync())
            {
                Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode);
                Assert.IsNotNull(response.Headers);
                Assert.IsTrue(response.Headers.RequestCharge > 0);
            }
        }
        private async Task <CosmosDatabase> CreateDatabaseStreamHelper(
            string databaseId   = null,
            int?throughput      = null,
            bool databaseExists = false)
        {
            if (string.IsNullOrEmpty(databaseId))
            {
                databaseId = Guid.NewGuid().ToString();
            }

            CosmosDatabaseSettings databaseSettings = new CosmosDatabaseSettings()
            {
                Id = databaseId
            };

            using (Stream streamPayload = CosmosResource.ToStream(databaseSettings))
            {
                CosmosResponseMessage response = await this.cosmosClient.Databases.CreateDatabaseStreamAsync(
                    streamPayload,
                    throughput : 400);

                Assert.IsNotNull(response);
                Assert.IsNotNull(response.Headers.RequestCharge);
                Assert.IsNotNull(response.Headers.ActivityId);

                Assert.IsTrue(response.StatusCode == HttpStatusCode.OK || (response.StatusCode == HttpStatusCode.Created && !databaseExists));

                return(this.cosmosClient.Databases[databaseId]);
            }
        }
Example #12
0
        private void CreateDatabaseIfNotExists()
        {
            string databaseId         = Guid.NewGuid().ToString();
            CosmosDatabaseSettings db = new CosmosDatabaseSettings {
                Id = databaseId
            };

            // Create the database with this unique id
            CosmosDatabaseSettings createdDatabase = this.client.CreateDatabaseIfNotExistsAsync(db).Result;

            // CreateDatabaseIfNotExistsAsync should create the new database
            Assert.AreEqual(databaseId, createdDatabase.Id);

            string databaseId2 = Guid.NewGuid().ToString();

            db = new CosmosDatabaseSettings {
                Id = databaseId2
            };

            // Pre-create the database with this unique id
            createdDatabase = this.client.CreateDatabaseAsync(db).Result;

            CosmosDatabaseSettings readDatabase = this.client.CreateDatabaseIfNotExistsAsync(db).Result;

            // CreateDatabaseIfNotExistsAsync should return the same database
            Assert.AreEqual(createdDatabase.SelfLink, readDatabase.SelfLink);

            // cleanup created databases
            this.client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId)).Wait();
            this.client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId2)).Wait();
        }
        private async Task ValidateBasicQueryAsync()
        {
            DocumentClient client = TestCommon.CreateClient(true);

            string databaseName             = Guid.NewGuid().ToString("n"); //Make sure local site variable are evaluated inline.
            CosmosDatabaseSettings database = await client.CreateDatabaseAsync(
                new CosmosDatabaseSettings { Id = databaseName });

            List <CosmosDatabaseSettings> queryResults = new List <CosmosDatabaseSettings>();
            //Simple Equality
            IQueryable <CosmosDatabaseSettings> dbQuery = from db in client.CreateDatabaseQuery()
                                                          where db.Id == databaseName
                                                          select db;
            IDocumentQuery <CosmosDatabaseSettings> documentQuery = dbQuery.AsDocumentQuery();

            while (documentQuery.HasMoreResults)
            {
                FeedResponse <CosmosDatabaseSettings> pagedResponse = await documentQuery.ExecuteNextAsync <CosmosDatabaseSettings>();

                Assert.IsNotNull(pagedResponse.ResponseHeaders, "ResponseHeaders is null");
                Assert.IsNotNull(pagedResponse.ActivityId, "Query ActivityId is null");
                queryResults.AddRange(pagedResponse);
            }

            Assert.AreEqual(1, queryResults.Count);
            Assert.AreEqual(databaseName, queryResults[0].Id);

            //Logical Or
            dbQuery = from db in client.CreateDatabaseQuery()
                      where db.Id == databaseName || db.ResourceId == database.ResourceId
                      select db;

            documentQuery = dbQuery.AsDocumentQuery();

            while (documentQuery.HasMoreResults)
            {
                queryResults.AddRange(await documentQuery.ExecuteNextAsync <CosmosDatabaseSettings>());
            }

            Assert.AreEqual(2, queryResults.Count);
            Assert.AreEqual(databaseName, queryResults[0].Id);

            //Select Property
            IQueryable <string> idQuery = from db in client.CreateDatabaseQuery()
                                          where db.Id == databaseName
                                          select db.ResourceId;
            IDocumentQuery <string> documentIdQuery = idQuery.AsDocumentQuery();

            List <string> idResults = new List <string>();

            while (documentIdQuery.HasMoreResults)
            {
                idResults.AddRange(await documentIdQuery.ExecuteNextAsync <string>());
            }

            Assert.AreEqual(1, idResults.Count);
            Assert.AreEqual(database.ResourceId, idResults[0]);
        }
Example #14
0
        private async Task TestClientWithPreferredRegionAsync()
        {
            Uri    writeRegionUri = new Uri(ConfigurationManager.AppSettings["GatewayEndpoint"]);
            Uri    readRegionUri  = new Uri(ConfigurationManager.AppSettings["GatewayEndpoint"]);
            string authKey        = ConfigurationManager.AppSettings["MasterKey"];

            // #1. Enable failover on client side, verify write succeed on readRegion.
            ConnectionPolicy failoverPolicy = new ConnectionPolicy();

            failoverPolicy.PreferredLocations.Add("West US");

            DocumentClient client3 = new DocumentClient(
                readRegionUri,
                authKey,
                failoverPolicy);

            // write should succeed as it will automatic endpoint discovery
            CosmosDatabaseSettings database = await client3.CreateDatabaseAsync(new CosmosDatabaseSettings { Id = Guid.NewGuid().ToString() });

            CosmosContainerSettings collection = await client3.CreateDocumentCollectionAsync(database.SelfLink, new CosmosContainerSettings { Id = Guid.NewGuid().ToString() });

            // make sure it is replicated
            await Task.Delay(GlobalDatabaseAccountTests.WaitDurationForAsyncReplication);

            CosmosContainerSettings collection1 = await client3.ReadDocumentCollectionAsync(collection.SelfLink);

            Document document1 = await client3.CreateDocumentAsync(collection.AltLink, new Document { Id = Guid.NewGuid().ToString() });

            // #2. Add the preferred read region. Read should go to read region.
            // make sure it is replicated
            await Task.Delay(GlobalDatabaseAccountTests.WaitDurationForAsyncReplication);

            ResourceResponse <Document> response1 = await client3.ReadDocumentAsync(document1);

            Uri uri = new Uri(response1.ResponseHeaders[HttpConstants.HttpHeaders.ContentLocation]);

            Assert.AreEqual(8081, uri.Port, "Read should go to port 8081");

            failoverPolicy.PreferredLocations.Clear();
            failoverPolicy.PreferredLocations.Add("South Central US");

            ResourceResponse <Document> response2 = await client3.ReadDocumentAsync(document1);

            Uri uri2 = new Uri(response2.ResponseHeaders[HttpConstants.HttpHeaders.ContentLocation]);

            Assert.AreEqual(8081, uri2.Port, "Read should go to port 8081");

            // #3. No preferred read region. Read should go to the write region.
            failoverPolicy.PreferredLocations.Clear();

            ResourceResponse <Document> response3 = await client3.ReadDocumentAsync(document1);

            Uri uri3 = new Uri(response3.ResponseHeaders[HttpConstants.HttpHeaders.ContentLocation]);

            Assert.AreEqual(8081, uri3.Port, "Read should go to port 8081");
        }
Example #15
0
        public void DatabaseSettingsDefaults()
        {
            CosmosDatabaseSettings dbSettings = new CosmosDatabaseSettings();

            Assert.IsNull(dbSettings.LastModified);
            Assert.IsNull(dbSettings.ResourceId);
            Assert.IsNull(dbSettings.Id);
            Assert.IsNull(dbSettings.ETag);

            SettingsContractTests.TypeAccessorGuard(typeof(CosmosDatabaseSettings), "Id");
        }
Example #16
0
        private async Task ReadDocumentFromReadRegionWithRetryAsync()
        {
            Uri    writeRegionUri = new Uri(ConfigurationManager.AppSettings["GatewayEndpoint"]);
            string authKey        = ConfigurationManager.AppSettings["MasterKey"];

            // #1. Enable failover on client side, verify write succeed on readRegion.
            ConnectionPolicy connectionPolicy = new ConnectionPolicy();

            connectionPolicy.PreferredLocations.Add("West US");

            DocumentClient client = new DocumentClient(
                writeRegionUri,
                authKey,
                connectionPolicy,
                ConsistencyLevel.Session);

            string databaseId   = "GlobalDB_SessionRetry";
            string collectionId = "GlobalDB_SessionRetry_Col1";

            CosmosDatabaseSettings database =
                client.ReadDatabaseFeedAsync(new FeedOptions())
                .Result.FirstOrDefault(database1 => database1.Id.Equals(databaseId, StringComparison.OrdinalIgnoreCase));

            if (database == null)
            {
                database = await client.CreateDatabaseAsync(new CosmosDatabaseSettings { Id = databaseId });
            }

            await Task.Delay(GlobalDatabaseAccountTests.WaitDurationForAsyncReplication);

            CosmosContainerSettings collection =
                client.ReadDocumentCollectionFeedAsync(database.SelfLink)
                .Result.FirstOrDefault(
                    documentCollection => documentCollection.Id.Equals(collectionId, StringComparison.OrdinalIgnoreCase));

            if (collection == null)
            {
                collection = await client.CreateDocumentCollectionAsync(database.SelfLink, new CosmosContainerSettings { Id = collectionId });
            }

            await Task.Delay(GlobalDatabaseAccountTests.WaitDurationForAsyncReplication);

            for (int i = 0; i < 10; i++)
            {
                Document document = await client.CreateDocumentAsync(collection.AltLink, new Document { Id = Guid.NewGuid().ToString() });

                ResourceResponse <Document> response = await client.ReadDocumentAsync(document);

                Document createdDocument = response.Resource;
                Assert.IsNotNull(createdDocument);
            }
        }
Example #17
0
        /// <summary>
        /// Run basic database meta data operations as a console application.
        /// </summary>
        /// <returns></returns>
        private static async Task RunDatabaseDemo(CosmosClient client)
        {
            // An object containing relevant information about the response
            DatabaseResponse databaseResponse = await client.CreateDatabaseIfNotExistsAsync(databaseId, 10000);

            // A client side reference object that allows additional operations like ReadAsync
            CosmosDatabase database = databaseResponse;

            // The response from Azure Cosmos
            CosmosDatabaseSettings settings = databaseResponse;

            Console.WriteLine($"\n1. Create a database resource with id: {settings.Id} and last modified time stamp: {settings.LastModified}");
            Console.WriteLine($"\n2. Create a database resource request charge: {databaseResponse.RequestCharge} and Activity Id: {databaseResponse.ActivityId}");

            // Read the database from Azure Cosmos
            DatabaseResponse readResponse = await database.ReadAsync();

            Console.WriteLine($"\n3. Read a database: {readResponse.Resource.Id}");

            await readResponse.Database.CreateContainerAsync("testContainer", "/pk");

            // Get the current throughput for the database
            int?throughput = await database.ReadProvisionedThroughputAsync();

            if (throughput.HasValue)
            {
                Console.WriteLine($"\n4. Read a database throughput: {throughput.Value}");

                // Update the current throughput for the database
                await database.ReplaceProvisionedThroughputAsync(11000);
            }

            Console.WriteLine("\n5. Reading all databases resources for an account");
            FeedIterator <CosmosDatabaseSettings> iterator = client.GetDatabasesIterator();

            do
            {
                foreach (CosmosDatabaseSettings db in await iterator.FetchNextSetAsync())
                {
                    Console.WriteLine(db.Id);
                }
            } while (iterator.HasMoreResults);

            // Delete the database from Azure Cosmos.
            await database.DeleteAsync();

            Console.WriteLine($"\n6. Database {database.Id} deleted.");
        }
Example #18
0
        public static void Initialize(TestContext textContext)
        {
            client = TestCommon.CreateClient(true);

            var db = new CosmosDatabaseSettings()
            {
                Id = nameof(LinqTranslationBaselineTests)
            };

            try
            {
                var response = client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(db.Id)).Result;
            }
            catch { }
            testDb = client.CreateDatabaseAsync(db).Result;
        }
Example #19
0
 private void ValidateDatabaseResponseBody(CosmosDatabaseSettings database, string databaseName = null)
 {
     Assert.IsNotNull(database.Id, "Id cannot be null");
     Assert.IsNotNull(database.ResourceId, "Resource Id (Rid) cannot be null");
     Assert.IsNotNull(database.SelfLink, "Self link cannot be null");
     Assert.IsNotNull(database.CollectionsLink, "Collections link cannot be null");
     Assert.IsNotNull(database.UsersLink, "Users link cannot be null");
     Assert.IsNotNull(database.ETag, "Etag cannot be null");
     Assert.IsNotNull(database.Timestamp, "Timestamp cannot be null");
     Assert.IsTrue(database.UsersLink.Contains(database.ResourceId), string.Format(CultureInfo.InvariantCulture, "Users link {0} should contain resource id {1}", database.UsersLink, database.ResourceId));
     Assert.IsTrue(database.CollectionsLink.Contains(database.ResourceId), string.Format(CultureInfo.InvariantCulture, "Collections link {0} should contain resource id {1}", database.CollectionsLink, database.ResourceId));
     Assert.IsTrue(database.SelfLink.Contains(database.ResourceId), string.Format(CultureInfo.InvariantCulture, "Self link {0} should contain resource id {1}", database.SelfLink, database.ResourceId));
     if (databaseName != null)
     {
         Assert.AreEqual(databaseName, database.Id, "Id should be match the name");
     }
 }
Example #20
0
        internal static Func <bool, IQueryable <Data> > GenerateSimpleData(
            DocumentClient client,
            CosmosDatabaseSettings testDb,
            out CosmosContainerSettings testCollection)
        {
            const int DocumentCount = 10;

            testCollection = client.CreateDocumentCollectionAsync(
                testDb.GetLink(),
                new CosmosContainerSettings()
            {
                Id = Guid.NewGuid().ToString()
            }).Result;

            Random      random   = new Random();
            List <Data> testData = new List <Data>();

            for (int index = 0; index < DocumentCount; index++)
            {
                Data dataEntry = new Data()
                {
                    Number    = random.Next(-10000, 10000),
                    Flag      = index % 2 == 0 ? true : false,
                    Multiples = new int[] { index, index * 2, index * 3, index * 4 }
                };

                client.CreateDocumentAsync(testCollection.GetLink(), dataEntry).Wait();
                testData.Add(dataEntry);
            }

            FeedOptions feedOptions = new FeedOptions()
            {
                EnableScanInQuery = true, EnableCrossPartitionQuery = true
            };
            var query = client.CreateDocumentQuery <Data>(testCollection.GetLink()).AsQueryable();

            // To cover both query against backend and queries on the original data using LINQ nicely,
            // the LINQ expression should be written once and they should be compiled and executed against the two sources.
            // That is done by using Func that take a boolean Func. The parameter of the Func indicate whether the Cosmos DB query
            // or the data list should be used. When a test is executed, the compiled LINQ expression would pass different values
            // to this getQuery method.
            Func <bool, IQueryable <Data> > getQuery = useQuery => useQuery ? query : testData.AsQueryable();

            return(getQuery);
        }
        public void RetryExceedingMaxTimeLimit()
        {
            Mock <IStoreModel> mockStoreModel = new Mock <IStoreModel>();

            mockStoreModel.Setup(model => model.ProcessMessageAsync(It.IsAny <DocumentServiceRequest>(), default(CancellationToken)))
            .Throws(this.CreateTooManyRequestException(100));

            ConnectionPolicy connectionPolicy = new ConnectionPolicy()
            {
                EnableEndpointDiscovery = false,
                RetryOptions            = new RetryOptions {
                    MaxRetryAttemptsOnThrottledRequests = 100, MaxRetryWaitTimeInSeconds = 1
                }
            };

            DocumentClient client = new DocumentClient(
                new Uri(ConfigurationManager.AppSettings["GatewayEndpoint"]),
                ConfigurationManager.AppSettings["MasterKey"],
                connectionPolicy);

            client.GetDatabaseAccountAsync().Wait();

            int expectedExecutionTimes = 11;

            client.StoreModel        = mockStoreModel.Object;
            client.GatewayStoreModel = mockStoreModel.Object;
            bool throttled = false;

            try
            {
                CosmosDatabaseSettings db = new CosmosDatabaseSettings {
                    Id = "test db 1"
                };
                client.CreateDatabaseAsync(db).Wait();
            }
            catch (Exception exp)
            {
                DocumentClientException docExp = exp.InnerException as DocumentClientException;
                Assert.AreEqual((HttpStatusCode)429, docExp.StatusCode);
                throttled = true;
            }

            mockStoreModel.Verify(model => model.ProcessMessageAsync(It.IsAny <DocumentServiceRequest>(), default(CancellationToken)), Times.Exactly(expectedExecutionTimes));
            Assert.IsTrue(throttled);
        }
Example #22
0
        private async Task <CosmosContainerSettings> SetupSingleCollectionScenario()
        {
            DocumentClient client = TestCommon.CreateClient(true);

            TestCommon.DeleteAllDatabasesAsync(client).Wait();

            CosmosDatabaseSettings database    = (await client.CreateDatabaseAsync(new CosmosDatabaseSettings {
                Id = this.DatabaseName
            })).Resource;
            CosmosContainerSettings collection = (await client.CreateDocumentCollectionIfNotExistsAsync(database.SelfLink, new CosmosContainerSettings {
                Id = this.CollectionName
            }, new RequestOptions {
                OfferThroughput = 10000
            })).Resource;

            //   await Task.Delay(30000);

            return(collection);
        }
Example #23
0
        internal void TestEtagOnUpsertOperation(DocumentClient client)
        {
            CosmosDatabaseSettings db = client.CreateDatabaseAsync(new CosmosDatabaseSettings()
            {
                Id = Guid.NewGuid().ToString()
            }).Result.Resource;
            CosmosContainerSettings coll = TestCommon.CreateCollectionAsync(client, db, new CosmosContainerSettings()
            {
                Id = Guid.NewGuid().ToString()
            }).Result;

            LinqGeneralBaselineTests.Book myBook = new LinqGeneralBaselineTests.Book();
            myBook.Id    = Guid.NewGuid().ToString();
            myBook.Title = "Azure DocumentDB 101";

            Document doc = client.CreateDocumentAsync(coll, myBook).Result.Resource;

            myBook.Title = "Azure DocumentDB 201";
            client.ReplaceDocumentAsync(doc.SelfLink, myBook).Wait();

            AccessCondition condition = new AccessCondition();

            condition.Type      = AccessConditionType.IfMatch;
            condition.Condition = doc.ETag;

            RequestOptions requestOptions = new RequestOptions();

            requestOptions.AccessCondition = condition;

            myBook.Title = "Azure DocumentDB 301";

            try
            {
                client.UpsertDocumentAsync(coll.SelfLink, myBook, requestOptions).Wait();
                Assert.Fail("Upsert Document should fail since the Etag is not matching.");
            }
            catch (Exception ex)
            {
                var innerException = ex.InnerException as DocumentClientException;
                Assert.IsTrue(innerException.StatusCode == HttpStatusCode.PreconditionFailed, "Invalid status code");
            }
        }
        public static void Initialize(TestContext textContext)
        {
            client = TestCommon.CreateClient(false, defaultConsistencyLevel: ConsistencyLevel.Session);

            // Set a callback to get the handle of the last executed query to do the verification
            // This is neede because aggregate queries return type is a scalar so it can't be used
            // to verify the translated LINQ directly as other queries type.
            client.OnExecuteScalarQueryCallback = q => LinqAggregateFunctionBaselineTests.lastExecutedScalarQuery = q;

            string databaseName = $"{nameof(LinqAggregateFunctionBaselineTests)}-{Guid.NewGuid().ToString("N")}";

            databaseUri = UriFactory.CreateDatabaseUri(databaseName);
            CosmosDatabaseSettings testDb = client.CreateDatabaseAsync(new CosmosDatabaseSettings()
            {
                Id = databaseName
            }).Result;

            CosmosContainerSettings collection;

            getQuery       = LinqTestsCommon.GenerateSimpleData(client, testDb, out collection);
            getQueryFamily = LinqTestsCommon.GenerateFamilyData(client, testDb, out collection);
        }
Example #25
0
        private void CreateDocumentCollectionIfNotExists()
        {
            string databaseId         = Guid.NewGuid().ToString();
            CosmosDatabaseSettings db = new CosmosDatabaseSettings {
                Id = databaseId
            };

            // Create the database with this unique id
            CosmosDatabaseSettings createdDatabase = this.client.CreateDatabaseIfNotExistsAsync(db).Result;

            string collectionId = Guid.NewGuid().ToString();
            CosmosContainerSettings collection = new CosmosContainerSettings {
                Id = collectionId
            };

            CosmosContainerSettings createdCollection = this.client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(createdDatabase.Id), collection).Result;

            // CreateDocumentCollectionIfNotExistsAsync should create the new collection
            Assert.AreEqual(collectionId, createdCollection.Id);

            string collectionId2 = Guid.NewGuid().ToString();

            collection = new CosmosContainerSettings {
                Id = collectionId2
            };

            // Pre-create the collection with this unique id
            createdCollection = this.client.CreateDocumentCollectionIfNotExistsAsync(createdDatabase.SelfLink, collection).Result;

            CosmosContainerSettings readCollection = this.client.CreateDocumentCollectionIfNotExistsAsync(createdDatabase.SelfLink, collection).Result;

            // CreateDocumentCollectionIfNotExistsAsync should return the same collection
            Assert.AreEqual(createdCollection.SelfLink, readCollection.SelfLink);

            // cleanup created database
            this.client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId)).Wait();
        }
Example #26
0
        public void DatabaseSettingsSerializeTest()
        {
            string id = Guid.NewGuid().ToString();

            CosmosDatabaseSettings databaseSettings = new CosmosDatabaseSettings()
            {
                Id = id
            };

            Database db = new Database()
            {
                Id = id
            };

            string cosmosSerialized = SettingsContractTests.CosmosSerialize(databaseSettings);
            string directSerialized = SettingsContractTests.DirectSerialize(db);

            // Swap de-serialize and validate
            CosmosDatabaseSettings dbDeserSettings = SettingsContractTests.CosmosDeserialize <CosmosDatabaseSettings>(directSerialized);
            Database dbDeser = SettingsContractTests.DirectDeSerialize <Database>(cosmosSerialized);

            Assert.AreEqual(dbDeserSettings.Id, dbDeser.Id);
            Assert.AreEqual(dbDeserSettings.Id, db.Id);
        }
        private void TestRetryOnThrottled(int?numberOfRetries)
        {
            Mock <IStoreModel> mockStoreModel = new Mock <IStoreModel>();

            mockStoreModel.Setup(model => model.ProcessMessageAsync(It.IsAny <DocumentServiceRequest>(), default(CancellationToken)))
            .Throws(this.CreateTooManyRequestException(100));

            ConnectionPolicy connectionPolicy = new ConnectionPolicy()
            {
                EnableEndpointDiscovery = false,
            };

            if (numberOfRetries != null)
            {
                connectionPolicy.RetryOptions = new RetryOptions {
                    MaxRetryAttemptsOnThrottledRequests = numberOfRetries.Value
                };
            }

            DocumentClient client = new DocumentClient(
                new Uri(ConfigurationManager.AppSettings["GatewayEndpoint"]),
                ConfigurationManager.AppSettings["MasterKey"],
                connectionPolicy);

            client.GetDatabaseAccountAsync().Wait();

            int expectedExecutionTimes = numberOfRetries + 1 ?? 10;

            client.StoreModel        = mockStoreModel.Object;
            client.GatewayStoreModel = mockStoreModel.Object;
            bool throttled = false;

            try
            {
                CosmosDatabaseSettings db = new CosmosDatabaseSettings {
                    Id = "test db 1"
                };
                client.CreateDatabaseAsync(db).Wait();
            }
            catch (Exception exp)
            {
                DocumentClientException docExp = exp.InnerException as DocumentClientException;
                Assert.AreEqual((HttpStatusCode)429, docExp.StatusCode);
                throttled = true;
            }

            mockStoreModel.Verify(model => model.ProcessMessageAsync(It.IsAny <DocumentServiceRequest>(), default(CancellationToken)), Times.Exactly(expectedExecutionTimes));
            Assert.IsTrue(throttled);

            throttled = false;
            try
            {
                client.ReadDatabaseAsync("/dbs/id1").Wait();
            }
            catch (Exception exp)
            {
                DocumentClientException docExp = exp.InnerException as DocumentClientException;
                Assert.AreEqual((HttpStatusCode)429, docExp.StatusCode);
                throttled = true;
            }

            mockStoreModel.Verify(model => model.ProcessMessageAsync(It.IsAny <DocumentServiceRequest>(), default(CancellationToken)), Times.Exactly(2 * expectedExecutionTimes));
            Assert.IsTrue(throttled);

            throttled = false;
            try
            {
                client.DeleteDocumentCollectionAsync("dbs/db_rid/colls/col_rid/").Wait();
            }
            catch (Exception exp)
            {
                DocumentClientException docExp = exp.InnerException as DocumentClientException;
                Assert.AreEqual((HttpStatusCode)429, docExp.StatusCode);
                throttled = true;
            }

            mockStoreModel.Verify(model => model.ProcessMessageAsync(It.IsAny <DocumentServiceRequest>(), default(CancellationToken)), Times.Exactly(3 * expectedExecutionTimes));
            Assert.IsTrue(throttled);

            throttled = false;
            try
            {
                client.CreateDatabaseQuery().AsDocumentQuery().ExecuteNextAsync().Wait();
            }
            catch (Exception exp)
            {
                DocumentClientException docExp = exp.InnerException as DocumentClientException;
                Assert.AreEqual((HttpStatusCode)429, docExp.StatusCode);
                throttled = true;
            }

            mockStoreModel.Verify(model => model.ProcessMessageAsync(It.IsAny <DocumentServiceRequest>(), default(CancellationToken)), Times.Exactly(4 * expectedExecutionTimes));
            Assert.IsTrue(throttled);
        }
Example #28
0
        private async Task TestDistanceAndWithin(bool allowScan)
        {
            CosmosDatabaseSettings database = await this.client.CreateDatabaseAsync(new CosmosDatabaseSettings { Id = Guid.NewGuid().ToString("N") });

            CosmosContainerSettings collectionDefinition = new CosmosContainerSettings()
            {
                Id = Guid.NewGuid().ToString("N")
            };

            CosmosContainerSettings collection;

            if (allowScan)
            {
                collection = await this.client.CreateDocumentCollectionAsync(database.SelfLink, collectionDefinition);
            }
            else
            {
                collectionDefinition.IndexingPolicy = new IndexingPolicy()
                {
                    IncludedPaths = new Collection <IncludedPath>()
                    {
                        new IncludedPath()
                        {
                            Path = "/"
                        },
                        new IncludedPath()
                        {
                            Path    = "/Location/?",
                            Indexes = new Collection <Index>()
                            {
                                new SpatialIndex(DataType.Point)
                            }
                        }
                    }
                };

                collection = await this.client.CreateDocumentCollectionAsync(database.SelfLink, collectionDefinition);
            }

            var class1 = new SpatialSampleClass
            {
                Location = new Point(20, 20),
                Id       = Guid.NewGuid().ToString()
            };

            await this.client.CreateDocumentAsync(collection.SelfLink, class1);

            var class2 = new SpatialSampleClass
            {
                Location = new Point(100, 100),
                Id       = Guid.NewGuid().ToString()
            };

            await this.client.CreateDocumentAsync(collection.SelfLink, class2);

            IOrderedQueryable <SpatialSampleClass> sampleClasses =
                this.client.CreateDocumentQuery <SpatialSampleClass>(collection.DocumentsLink, new FeedOptions()
            {
                EnableScanInQuery = allowScan
            });

            SpatialSampleClass[] distanceQuery = sampleClasses
                                                 .Where(f => f.Location.Distance(new Point(20.1, 20)) < 20000)
                                                 .ToArray();

            Assert.AreEqual(1, distanceQuery.Count());

            Polygon polygon = new Polygon(
                new[]
            {
                new Position(10, 10),
                new Position(30, 10),
                new Position(30, 30),
                new Position(10, 30),
                new Position(10, 10),
            });

            SpatialSampleClass[] withinQuery = sampleClasses
                                               .Where(f => f.Location.Within(polygon))
                                               .ToArray();

            Assert.AreEqual(1, withinQuery.Count());
        }
Example #29
0
        public async Task TestIsValid()
        {
            CosmosDatabaseSettings database = await this.client.CreateDatabaseAsync(
                new CosmosDatabaseSettings { Id = Guid.NewGuid().ToString("N") });

            CosmosContainerSettings collectionDefinition = new CosmosContainerSettings()
            {
                Id = Guid.NewGuid().ToString("N")
            };

            collectionDefinition.IndexingPolicy = new IndexingPolicy()
            {
                IncludedPaths = new Collection <IncludedPath>()
                {
                    new IncludedPath()
                    {
                        Path = "/"
                    },
                    new IncludedPath()
                    {
                        Path    = "/Location/?",
                        Indexes = new Collection <Index>()
                        {
                            new SpatialIndex(DataType.Point)
                        }
                    }
                }
            };

            CosmosContainerSettings collection = await this.client.CreateDocumentCollectionAsync(
                database.SelfLink,
                collectionDefinition);

            var invalidGeometry = new SpatialSampleClass
            {
                Location = new Point(20, 180),
                Id       = Guid.NewGuid().ToString()
            };

            await this.client.CreateDocumentAsync(collection.SelfLink, invalidGeometry);

            var validGeometry = new SpatialSampleClass
            {
                Location = new Point(50, 50),
                Id       = Guid.NewGuid().ToString()
            };

            await this.client.CreateDocumentAsync(collection.SelfLink, validGeometry);

            IOrderedQueryable <SpatialSampleClass> sampleClasses =
                this.client.CreateDocumentQuery <SpatialSampleClass>(collection.DocumentsLink, new FeedOptions()
            {
            });

            SpatialSampleClass[] distanceQuery = sampleClasses
                                                 .Where(f => f.Location.Distance(new Point(20, 180)) < 20000)
                                                 .ToArray();

            Assert.AreEqual(0, distanceQuery.Count());

            SpatialSampleClass[] isNotValidQuery = sampleClasses
                                                   .Where(f => !f.Location.IsValid())
                                                   .ToArray();

            Assert.AreEqual(1, isNotValidQuery.Count());
            Assert.AreEqual(invalidGeometry.Location, isNotValidQuery[0].Location);

            IOrderedQueryable <SpatialSampleClass> invalidDetailed =
                this.client.CreateDocumentQuery <SpatialSampleClass>(collection.DocumentsLink, new FeedOptions()
            {
            });

            var query = invalidDetailed
                        .Where(f => !f.Location.IsValid()).Select(f => f.Location.IsValidDetailed());
            var isNotValidDetailedQuery = query.ToArray();

            Assert.AreEqual(1, isNotValidDetailedQuery.Count());
            Assert.AreEqual("Latitude values must be between -90 and 90 degrees.", isNotValidDetailedQuery[0].Reason);
            Assert.AreEqual(false, isNotValidDetailedQuery[0].IsValid);

            SpatialSampleClass[] isValidQuery = sampleClasses
                                                .Where(f => f.Location.IsValid())
                                                .ToArray();

            Assert.AreEqual(1, isValidQuery.Count());
            Assert.AreEqual(validGeometry.Location, isValidQuery[0].Location);
        }
 public void TestInitialize()
 {
     this.client   = TestCommon.CreateClient(true);
     this.database = TestCommon.CreateOrGetDatabase(this.client);
 }