public async Task TestCreateDatabase()
        {
            IDatabaseService provider = CreateProvider();

            using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(600))))
            {
                ReadOnlyCollection <DatabaseFlavor> flavors = await provider.ListFlavorsAsync(cancellationTokenSource.Token);

                if (flavors.Count == 0)
                {
                    Assert.Inconclusive("The service did not report any flavors.");
                }

                DatabaseFlavor smallestFlavor = flavors.Where(i => i.Memory.HasValue).OrderBy(i => i.Memory).First();
                string         instanceName   = CreateRandomDatabaseInstanceName();
                DatabaseInstanceConfiguration configuration = new DatabaseInstanceConfiguration(smallestFlavor.Href, new DatabaseVolumeConfiguration(1), instanceName);
                DatabaseInstance instance = await provider.CreateDatabaseInstanceAsync(configuration, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);

                DatabaseName          databaseName          = CreateRandomDatabaseName();
                DatabaseConfiguration databaseConfiguration = new DatabaseConfiguration(databaseName, null, null);
                await provider.CreateDatabaseAsync(instance.Id, databaseConfiguration, cancellationTokenSource.Token);

                Console.WriteLine("Databases in instance '{0}':", instance.Name);
                foreach (var database in await ListAllDatabasesAsync(provider, instance.Id, null, cancellationTokenSource.Token))
                {
                    Console.WriteLine("    {0}", database.Name);
                }

                await provider.RemoveDatabaseAsync(instance.Id, databaseName, cancellationTokenSource.Token);

                /* Cleanup
                 */
                await provider.RemoveDatabaseInstanceAsync(instance.Id, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);
            }
        }
        public async Task TestResizeInstance()
        {
            IDatabaseService provider = CreateProvider();

            using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(600))))
            {
                ReadOnlyCollection <DatabaseFlavor> flavors = await provider.ListFlavorsAsync(cancellationTokenSource.Token);

                if (flavors.Count == 0)
                {
                    Assert.Inconclusive("The service did not report any flavors.");
                }

                DatabaseFlavor smallestFlavor = flavors.Where(i => i.Memory.HasValue).OrderBy(i => i.Memory).First();
                string         instanceName   = CreateRandomDatabaseInstanceName();
                DatabaseInstanceConfiguration configuration = new DatabaseInstanceConfiguration(smallestFlavor.Href, new DatabaseVolumeConfiguration(1), instanceName);
                DatabaseInstance instance = await provider.CreateDatabaseInstanceAsync(configuration, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);

                DatabaseFlavor nextSmallestFlavor = flavors.Where(i => i.Memory.HasValue).OrderBy(i => i.Memory).Skip(1).First();
                await provider.ResizeDatabaseInstanceAsync(instance.Id, nextSmallestFlavor.Href, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);

                /* Cleanup
                 */
                await provider.RemoveDatabaseInstanceAsync(instance.Id, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);
            }
        }
        public static void ClassInitialize(TestContext testContext)
        {
            IDatabaseService provider = UserDatabaseTests.CreateProvider();

            using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(600))))
            {
                ReadOnlyCollection <DatabaseFlavor> flavors = provider.ListFlavorsAsync(cancellationTokenSource.Token).Result;
                if (flavors.Count == 0)
                {
                    Assert.Inconclusive("The service did not report any flavors.");
                }

                DatabaseFlavor smallestFlavor = flavors.Where(i => i.Memory.HasValue).OrderBy(i => i.Memory).First();
                string         instanceName   = UserDatabaseTests.CreateRandomDatabaseInstanceName();
                DatabaseInstanceConfiguration configuration = new DatabaseInstanceConfiguration(smallestFlavor.Href, new DatabaseVolumeConfiguration(1), instanceName);
                _instance = provider.CreateDatabaseInstanceAsync(configuration, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null).Result;

                _databaseName = UserDatabaseTests.CreateRandomDatabaseName();
                DatabaseConfiguration databaseConfiguration = new DatabaseConfiguration(_databaseName, null, null);
                provider.CreateDatabaseAsync(_instance.Id, databaseConfiguration, cancellationTokenSource.Token).Wait();

                _databaseName2 = UserDatabaseTests.CreateRandomDatabaseName();
                DatabaseConfiguration databaseConfiguration2 = new DatabaseConfiguration(_databaseName2, null, null);
                provider.CreateDatabaseAsync(_instance.Id, databaseConfiguration2, cancellationTokenSource.Token).Wait();
            }
        }
        public async Task TestEnableRootUser()
        {
            IDatabaseService provider = CreateProvider();

            using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(600))))
            {
                ReadOnlyCollection <DatabaseFlavor> flavors = await provider.ListFlavorsAsync(cancellationTokenSource.Token);

                if (flavors.Count == 0)
                {
                    Assert.Inconclusive("The service did not report any flavors.");
                }

                DatabaseFlavor smallestFlavor = flavors.Where(i => i.Memory.HasValue).OrderBy(i => i.Memory).First();
                string         instanceName   = CreateRandomDatabaseInstanceName();
                DatabaseInstanceConfiguration configuration = new DatabaseInstanceConfiguration(smallestFlavor.Href, new DatabaseVolumeConfiguration(1), instanceName);
                DatabaseInstance instance = await provider.CreateDatabaseInstanceAsync(configuration, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);

                bool?enabled = await provider.CheckRootEnabledStatusAsync(instance.Id, cancellationTokenSource.Token);

                Assert.IsNotNull(enabled);
                Assert.IsFalse(enabled.Value);

                RootUser rootUser = await provider.EnableRootUserAsync(instance.Id, cancellationTokenSource.Token);

                Assert.IsNotNull(rootUser);
                Assert.IsFalse(string.IsNullOrEmpty(rootUser.Name));
                Assert.IsFalse(string.IsNullOrEmpty(rootUser.Password));

                enabled = await provider.CheckRootEnabledStatusAsync(instance.Id, cancellationTokenSource.Token);

                RootUser anotherRootUser = await provider.EnableRootUserAsync(instance.Id, cancellationTokenSource.Token);

                Assert.IsNotNull(anotherRootUser);
                Assert.IsFalse(string.IsNullOrEmpty(rootUser.Name));
                Assert.IsFalse(string.IsNullOrEmpty(rootUser.Password));

                /* Cleanup
                 */
                await provider.RemoveDatabaseInstanceAsync(instance.Id, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);
            }
        }
        public async Task TestGetFlavor()
        {
            IDatabaseService provider = CreateProvider();

            using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(60))))
            {
                ReadOnlyCollection <DatabaseFlavor> flavors = await provider.ListFlavorsAsync(cancellationTokenSource.Token);

                if (flavors.Count == 0)
                {
                    Assert.Inconclusive("The service did not report any flavors.");
                }

                Task <DatabaseFlavor>[] flavorTasks = Array.ConvertAll(flavors.ToArray(), flavor => provider.GetFlavorAsync(flavor.Id, cancellationTokenSource.Token));
                await Task.WhenAll(flavorTasks);

                for (int i = 0; i < flavors.Count; i++)
                {
                    DatabaseFlavor referenceFlavor = flavors[i];
                    Assert.IsNotNull(referenceFlavor);

                    DatabaseFlavor individualFlavor = flavorTasks[i].Result;
                    Assert.IsNotNull(individualFlavor);

                    Assert.AreEqual(referenceFlavor.Id, individualFlavor.Id);
                    Assert.AreEqual(referenceFlavor.Name, individualFlavor.Name);
                    Assert.AreEqual(referenceFlavor.Href, individualFlavor.Href);
                    Assert.AreEqual(referenceFlavor.Memory, individualFlavor.Memory);
                    Assert.AreEqual(referenceFlavor.Links.Count, individualFlavor.Links.Count);
                    for (int j = 0; j < referenceFlavor.Links.Count; j++)
                    {
                        Assert.AreEqual(referenceFlavor.Links[j].Href, individualFlavor.Links[j].Href);
                        Assert.AreEqual(referenceFlavor.Links[j].Rel, individualFlavor.Links[j].Rel);
                    }
                }
            }
        }
        public async Task TestCreateBackup()
        {
            IDatabaseService provider = CreateProvider();

            using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(600))))
            {
                ReadOnlyCollection <DatabaseFlavor> flavors = await provider.ListFlavorsAsync(cancellationTokenSource.Token);

                if (flavors.Count == 0)
                {
                    Assert.Inconclusive("The service did not report any flavors.");
                }

                DatabaseFlavor smallestFlavor = flavors.Where(i => i.Memory.HasValue).OrderBy(i => i.Memory).First();
                string         instanceName   = CreateRandomDatabaseInstanceName();
                DatabaseInstanceConfiguration configuration = new DatabaseInstanceConfiguration(smallestFlavor.Href, new DatabaseVolumeConfiguration(1), instanceName);
                DatabaseInstance instance = await provider.CreateDatabaseInstanceAsync(configuration, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);

                ReadOnlyCollection <Backup> initialBackups = await provider.ListBackupsForInstanceAsync(instance.Id, cancellationTokenSource.Token);

                Assert.IsNotNull(initialBackups);
                Assert.AreEqual(0, initialBackups.Count);

                string backupName        = CreateRandomBackupName();
                string backupDescription = "My backup";
                BackupConfiguration backupConfiguration = new BackupConfiguration(instance.Id, backupName, backupDescription);
                Backup backup = await provider.CreateBackupAsync(backupConfiguration, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);

                Assert.AreEqual(BackupStatus.Completed, backup.Status);

                Backup backupCopy = await provider.GetBackupAsync(backup.Id, cancellationTokenSource.Token);

                Assert.AreEqual(backup.Id, backupCopy.Id);

                ReadOnlyCollection <Backup> allBackups = await provider.ListBackupsAsync(cancellationTokenSource.Token);

                ReadOnlyCollection <Backup> instanceBackups = await provider.ListBackupsForInstanceAsync(instance.Id, cancellationTokenSource.Token);

                Assert.IsTrue(allBackups.Count >= instanceBackups.Count);
                Assert.AreEqual(1, instanceBackups.Count);
                Assert.AreEqual(backupName, instanceBackups[0].Name);
                Assert.AreEqual(backupDescription, instanceBackups[0].Description);

                await provider.EnableRootUserAsync(instance.Id, cancellationTokenSource.Token);

                Assert.AreEqual(true, await provider.CheckRootEnabledStatusAsync(instance.Id, cancellationTokenSource.Token));

                await provider.RemoveDatabaseInstanceAsync(instance.Id, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);

                ReadOnlyCollection <Backup> instanceBackupsAfterRemove = await provider.ListBackupsForInstanceAsync(instance.Id, cancellationTokenSource.Token);

                Assert.AreEqual(instanceBackups.Count, instanceBackupsAfterRemove.Count);

                configuration = new DatabaseInstanceConfiguration(smallestFlavor.Href, new DatabaseVolumeConfiguration(1), instanceName, new RestorePoint(backup.Id));
                instance      = await provider.CreateDatabaseInstanceAsync(configuration, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);

                Assert.AreEqual(false, await provider.CheckRootEnabledStatusAsync(instance.Id, cancellationTokenSource.Token));

                await provider.RemoveBackupAsync(backup.Id, cancellationTokenSource.Token);

                /* Cleanup
                 */
                await provider.RemoveDatabaseInstanceAsync(instance.Id, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);
            }
        }