public void EnableProtectionTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"];
                string resourceGroupName = ConfigurationManager.AppSettings["RsVaultRgNameRP"];
                string resourceName = ConfigurationManager.AppSettings["RsVaultNameRP"];
                string location = ConfigurationManager.AppSettings["vaultLocationRP"];
                // TODO: Create VM instead of taking these parameters from config
                string containerUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMContainerUniqueNameRP"];
                string itemUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMItemUniqueNameRP"];
                string containeType = ConfigurationManager.AppSettings["IaaSVMContainerType"];
                string itemType = ConfigurationManager.AppSettings["IaaSVMItemType"];
                string containerUri = containeType + ";" + containerUniqueName;
                string itemUri = itemType + ";" + itemUniqueName;

                var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace);

                // 1. Create vault
                VaultTestHelpers vaultTestHelper = new VaultTestHelpers(client);
                vaultTestHelper.CreateVault(resourceGroupName, resourceName, location);

                // 2. Get default policy
                PolicyTestHelpers policyTestHelper = new PolicyTestHelpers(client);
                string policyId = policyTestHelper.GetDefaultPolicyId(resourceGroupName, resourceName);

                // ACTION: Enable protection and wait for completion
                ProtectedItemTestHelpers protectedItemTestHelper = new ProtectedItemTestHelpers(client);
                protectedItemTestHelper.EnableProtection(resourceGroupName, resourceName, policyId, containerUri, itemUri);
            }
        }
        public void ListProtectionPolicyTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"];
                string resourceGroupName = ConfigurationManager.AppSettings["RsVaultRgNameRP"];
                string resourceName = ConfigurationManager.AppSettings["RsVaultNameRP"];
                string location = ConfigurationManager.AppSettings["vaultLocationRP"];

                var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace);

                // 1. Create vault
                VaultTestHelpers vaultTestHelper = new VaultTestHelpers(client);
                vaultTestHelper.CreateVault(resourceGroupName, resourceName, location);

                // ACTION: List policies
                PolicyTestHelpers policyTestHelper = new PolicyTestHelpers(client);
                ProtectionPolicyQueryParameters queryParams = new ProtectionPolicyQueryParameters();
                ProtectionPolicyListResponse response = policyTestHelper.ListProtectionPolicy(resourceGroupName, resourceName, queryParams);

                // VALIDATION: At least default policy is expected
                Assert.NotNull(response.ItemList);
                Assert.NotNull(response.ItemList.Value);
                Assert.NotEmpty(response.ItemList.Value);
                foreach (ProtectionPolicyResource resource in response.ItemList.Value)
                {
                    Assert.NotNull(resource.Id);
                    Assert.NotNull(resource.Name);
                    Assert.NotNull(resource.Type);
                    Assert.NotNull(resource.Properties);
                }
            }
        }
        public void ListProtectionPolicyTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"];
                string rsVaultRgName = CommonTestHelper.GetSetting(TestConstants.RsVaultRgName);
                string rsVaultName = CommonTestHelper.GetSetting(TestConstants.RsVaultName);

                var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace);
                PolicyTestHelpers policyTestHelper = new PolicyTestHelpers(client);
                ProtectionPolicyQueryParameters queryParams = new ProtectionPolicyQueryParameters();
                queryParams.BackupManagementType = ConfigurationManager.AppSettings["ProviderTypeAzureSql"];

                ProtectionPolicyListResponse response = policyTestHelper.ListProtectionPolicy(rsVaultRgName, rsVaultName, queryParams);

                // atleast one default policy is expected
                Assert.NotNull(response.ItemList);
                Assert.NotNull(response.ItemList.Value);

                IList<ProtectionPolicyResource> policyList = response.ItemList.Value;

                // atleast one default policy should be there
                Assert.NotEmpty(policyList);

                foreach (ProtectionPolicyResource resource in policyList)
                {
                    Assert.NotNull(resource.Id);
                    Assert.NotNull(resource.Name);
                    Assert.NotNull(resource.Type);
                    Assert.NotNull(resource.Properties);
                }
            }
        }
        public void RestoreDiskTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"];
                string resourceGroupName = ConfigurationManager.AppSettings["RsVaultRgNameRP"];
                string resourceName = ConfigurationManager.AppSettings["RsVaultNameRP"];
                string location = ConfigurationManager.AppSettings["vaultLocationRP"];
                // TODO: Create VM instead of taking these parameters from config
                string containerUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMContainerUniqueNameRP"];
                string itemUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMItemUniqueNameRP"];
                string containeType = ConfigurationManager.AppSettings["IaaSVMContainerType"];
                string itemType = ConfigurationManager.AppSettings["IaaSVMItemType"];
                string containerUri = containeType + ";" + containerUniqueName;
                string itemUri = itemType + ";" + itemUniqueName;
                string storageAccountId = ConfigurationManager.AppSettings["StorageAccountId"];

                var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace);

                // 1. Create vault
                VaultTestHelpers vaultTestHelper = new VaultTestHelpers(client);
                vaultTestHelper.CreateVault(resourceGroupName, resourceName, location);

                // 2. Get default policy
                PolicyTestHelpers policyTestHelper = new PolicyTestHelpers(client);
                string policyId = policyTestHelper.GetDefaultPolicyId(resourceGroupName, resourceName);

                // 3. Enable protection
                ProtectedItemTestHelpers protectedItemTestHelper = new ProtectedItemTestHelpers(client);
                protectedItemTestHelper.EnableProtection(resourceGroupName, resourceName, policyId, containerUri, itemUri);

                // 4. Get protected item's source resource ID
                var protectedItemResponse = protectedItemTestHelper.GetProtectedItem(resourceGroupName, resourceName, containerUri, itemUri);
                string sourceResourceId = ((AzureIaaSVMProtectedItem)protectedItemResponse.Item.Properties).VirtualMachineId;

                // 5. Trigger backup and wait for completion
                BackupTestHelpers backupTestHelper = new BackupTestHelpers(client);
                DateTime backupStartTime = DateTime.UtcNow;
                string backupJobId = backupTestHelper.BackupProtectedItem(resourceGroupName, resourceName, containerUri, itemUri);
                JobTestHelpers jobTestHelper = new JobTestHelpers(client);
                jobTestHelper.WaitForJob(resourceGroupName, resourceName, backupJobId);
                DateTime backupEndTime = DateTime.UtcNow;

                // 6. Get latest RP
                RecoveryPointTestHelpers recoveryPointTestHelper = new RecoveryPointTestHelpers(client);
                var recoveryPoints = recoveryPointTestHelper.ListRecoveryPoints(
                    resourceGroupName, resourceName, containerUri, itemUri, backupStartTime, backupEndTime);
                var recoveryPointResource = recoveryPointTestHelper.GetRecoveryPointDetails(
                    resourceGroupName, resourceName, containerUri, itemUri, recoveryPoints[0].Name);

                // ACTION: Trigger disk restore on the latest RP and wait for completion
                RestoreTestHelpers restoreTestHelper = new RestoreTestHelpers(client);
                string restoreJobId = restoreTestHelper.RestoreProtectedItem(
                    resourceGroupName, resourceName, containerUri, itemUri, sourceResourceId, storageAccountId, recoveryPointResource);
                jobTestHelper.WaitForJob(resourceGroupName, resourceName, restoreJobId);
            }
        }
        public void ListJobsAndGetJobTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"];
                string resourceGroupName = ConfigurationManager.AppSettings["RsVaultRgNameRP"];
                string resourceName = ConfigurationManager.AppSettings["RsVaultNameRP"];
                string location = ConfigurationManager.AppSettings["vaultLocationRP"];
                // TODO: Create VM instead of taking these parameters from config
                string containerUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMContainerUniqueNameRP"];
                string itemUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMItemUniqueNameRP"];
                string containeType = ConfigurationManager.AppSettings["IaaSVMContainerType"];
                string itemType = ConfigurationManager.AppSettings["IaaSVMItemType"];
                string containerUri = containeType + ";" + containerUniqueName;
                string itemUri = itemType + ";" + itemUniqueName;
                string utcDateTimeFormat = ConfigurationManager.AppSettings["UTCDateTimeFormat"];

                var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace);

                // 1. Create vault
                VaultTestHelpers vaultTestHelper = new VaultTestHelpers(client);
                vaultTestHelper.CreateVault(resourceGroupName, resourceName, location);

                // 2. Get default policy
                PolicyTestHelpers policyTestHelper = new PolicyTestHelpers(client);
                string policyId = policyTestHelper.GetDefaultPolicyId(resourceGroupName, resourceName);

                // 3. Enable protection
                ProtectedItemTestHelpers protectedItemTestHelper = new ProtectedItemTestHelpers(client);
                DateTime protectionStartTime = DateTime.UtcNow;
                protectedItemTestHelper.EnableProtection(resourceGroupName, resourceName, policyId, containerUri, itemUri);
                DateTime protectionEndTime = DateTime.UtcNow;

                // ACTION: List jobs
                CommonJobQueryFilters commonFilters = new CommonJobQueryFilters();
                commonFilters.BackupManagementType = BackupManagementType.AzureIaasVM.ToString();
                commonFilters.StartTime = protectionStartTime.ToString(utcDateTimeFormat);
                commonFilters.EndTime = protectionEndTime.ToString(utcDateTimeFormat);
                JobTestHelpers helper = new JobTestHelpers(client);
                var jobList = helper.ListJobs(resourceGroupName, resourceName, commonFilters, null);

                // VALIDATION
                foreach (var job in jobList.ItemList.Value)
                {
                    Assert.NotNull(job.Id);
                    Assert.NotNull(job.Name);
                    helper.ValidateJobResponse(job.Properties, commonFilters);

                    // validating getjob
                    var jobDetails = helper.GetJob(resourceGroupName, resourceName, job.Name);
                    Assert.NotNull(jobDetails);
                    Assert.NotNull(jobDetails.Item);
                    helper.ValidateJobResponse(jobDetails.Item.Properties, null);
                }
            }
        }
        public void ListRecoveryPointTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"];
                string resourceGroupName = ConfigurationManager.AppSettings["RsVaultRgNameRP"];
                string resourceName = ConfigurationManager.AppSettings["RsVaultNameRP"];
                string location = ConfigurationManager.AppSettings["vaultLocationRP"];
                // TODO: Create VM instead of taking these parameters from config
                string containerUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMContainerUniqueNameRP"];
                string itemUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMItemUniqueNameRP"];
                string containeType = ConfigurationManager.AppSettings["IaaSVMContainerType"];
                string itemType = ConfigurationManager.AppSettings["IaaSVMItemType"];
                string containerUri = containeType + ";" + containerUniqueName;
                string itemUri = itemType + ";" + itemUniqueName;
                string fabricName = ConfigurationManager.AppSettings["AzureBackupFabricName"];
                string utcDateTimeFormat = ConfigurationManager.AppSettings["UTCDateTimeFormat"];
                
                var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace);

                // 1. Create vault
                VaultTestHelpers vaultTestHelper = new VaultTestHelpers(client);
                vaultTestHelper.CreateVault(resourceGroupName, resourceName, location);

                // 2. Get default policy
                PolicyTestHelpers policyTestHelper = new PolicyTestHelpers(client);
                string policyId = policyTestHelper.GetDefaultPolicyId(resourceGroupName, resourceName);

                // 3. Enable protection
                ProtectedItemTestHelpers protectedItemTestHelper = new ProtectedItemTestHelpers(client);
                protectedItemTestHelper.EnableProtection(resourceGroupName, resourceName, policyId, containerUri, itemUri);

                // 4. Trigger backup and wait for completion
                BackupTestHelpers backupTestHelper = new BackupTestHelpers(client);
                DateTime backupStartTime = DateTime.UtcNow;
                string jobId = backupTestHelper.BackupProtectedItem(resourceGroupName, resourceName, containerUri, itemUri);
                JobTestHelpers jobTestHelper = new JobTestHelpers(client);
                jobTestHelper.WaitForJob(resourceGroupName, resourceName, jobId);
                DateTime backupEndTime = DateTime.UtcNow;

                // ACTION: Fetch RP
                RecoveryPointQueryParameters queryFilter = new RecoveryPointQueryParameters();
                queryFilter.StartDate = backupStartTime.ToString(utcDateTimeFormat);
                queryFilter.EndDate = backupEndTime.ToString(utcDateTimeFormat);
                var response = client.RecoveryPoints.List(
                    resourceGroupName, resourceName, CommonTestHelper.GetCustomRequestHeaders(), fabricName, containerUri, itemUri, queryFilter);

                // VALIDATION: Should be only one RP
                Assert.NotNull(response.RecoveryPointList);
                Assert.NotNull(response.RecoveryPointList.RecoveryPoints);
                Assert.Equal(1, response.RecoveryPointList.RecoveryPoints.Count);
            }
        }
        public void ListProtectableObjectTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"];
                string resourceGroupName = ConfigurationManager.AppSettings["RsVaultRgNameRP"];
                string resourceName = ConfigurationManager.AppSettings["RsVaultNameRP"];
                string location = ConfigurationManager.AppSettings["vaultLocationRP"];
                // TODO: Create VM instead of taking these parameters from config
                string containerUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMContainerUniqueNameRP"];
                string itemUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMItemUniqueNameRP"];
                string containeType = ConfigurationManager.AppSettings["IaaSVMContainerType"];
                string itemType = ConfigurationManager.AppSettings["IaaSVMItemType"];
                string containerUri = containeType + ";" + containerUniqueName;
                string itemUri = itemType + ";" + itemUniqueName;

                var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace);

                // 1. Create vault
                VaultTestHelpers vaultTestHelper = new VaultTestHelpers(client);
                vaultTestHelper.CreateVault(resourceGroupName, resourceName, location);

                // 2. Get default policy
                PolicyTestHelpers policyTestHelper = new PolicyTestHelpers(client);
                string policyId = policyTestHelper.GetDefaultPolicyId(resourceGroupName, resourceName);

                // 3. Enable protection
                ProtectedItemTestHelpers protectedItemTestHelper = new ProtectedItemTestHelpers(client);
                protectedItemTestHelper.EnableProtection(resourceGroupName, resourceName, policyId, containerUri, itemUri);

                // ACTION: List protectable objects
                ProtectableObjectListQueryParameters queryParams = new ProtectableObjectListQueryParameters();
                queryParams.BackupManagementType = BackupManagementType.AzureIaasVM.ToString();
                ProtectableObjectTestHelper poTestHelper = new ProtectableObjectTestHelper(client);
                ProtectableObjectListResponse response = poTestHelper.ListProtectableObjects(
                    resourceGroupName, resourceName, queryParams, new PaginationRequest());

                // VALIDATION: VM should not be found in the list
                Assert.False(response.ItemList.ProtectableObjects.Any(
                    protectableObject =>
                    {
                        return protectableObject.Properties.GetType().IsSubclassOf(typeof(AzureIaaSVMProtectableItem)) &&
                            protectableObject.Name.Contains(containerUniqueName);
                    }),
                    "Retrieved list of containers doesn't contain AzureIaaSVMProtectableItem Item");
            }
        }
        public void CancelJobTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"];
                string resourceGroupName = ConfigurationManager.AppSettings["RsVaultRgNameRP"];
                string resourceName = ConfigurationManager.AppSettings["RsVaultNameRP"];
                string location = ConfigurationManager.AppSettings["vaultLocationRP"];
                // TODO: Create VM instead of taking these parameters from config
                string containerUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMContainerUniqueNameRP"];
                string itemUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMItemUniqueNameRP"];
                string containeType = ConfigurationManager.AppSettings["IaaSVMContainerType"];
                string itemType = ConfigurationManager.AppSettings["IaaSVMItemType"];
                string containerUri = containeType + ";" + containerUniqueName;
                string itemUri = itemType + ";" + itemUniqueName;

                var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace);

                // 1. Create vault
                VaultTestHelpers vaultTestHelper = new VaultTestHelpers(client);
                vaultTestHelper.CreateVault(resourceGroupName, resourceName, location);

                // 2. Get default policy
                PolicyTestHelpers policyTestHelper = new PolicyTestHelpers(client);
                string policyId = policyTestHelper.GetDefaultPolicyId(resourceGroupName, resourceName);

                // 3. Enable protection
                ProtectedItemTestHelpers protectedItemTestHelper = new ProtectedItemTestHelpers(client);
                DateTime protectionStartTime = DateTime.UtcNow;
                protectedItemTestHelper.EnableProtection(resourceGroupName, resourceName, policyId, containerUri, itemUri);
                DateTime protectionEndTime = DateTime.UtcNow;

                // 4. Trigger backup and get the job
                BackupTestHelpers backupTestHelper = new BackupTestHelpers(client);
                string jobId = backupTestHelper.BackupProtectedItem(resourceGroupName, resourceName, containerUri, itemUri);
                CommonJobQueryFilters commonFilters = new CommonJobQueryFilters();
                commonFilters.Status = JobStatus.InProgress.ToString();
                commonFilters.Operation = JobOperation.Backup.ToString();
                JobTestHelpers helper = new JobTestHelpers(client);
                var job = helper.GetJob(resourceGroupName, resourceName, jobId);

                // ACTION: Cancel the job
                var cancelResponse = helper.CancelJob(resourceGroupName, resourceName, jobId);
                var opId = helper.GetOpId(cancelResponse.Location);
                var opStatus = helper.GetJobOperationStatus(resourceGroupName, resourceName, jobId, opId);
                TestUtilities.RetryActionWithTimeout(
                    () => opStatus = helper.GetJobOperationStatus(resourceGroupName, resourceName, jobId, opId),
                    () => opStatus.StatusCode != HttpStatusCode.Accepted,
                    TimeSpan.FromMinutes(30),
                    statusCode =>
                    {
                        if (HttpMockServer.Mode == HttpRecorderMode.Record)
                        {
                            Thread.Sleep(15 * 1000);
                        }
                        return true;
                    });
            }
        }
        public void GetRecoveryPointDetailTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"];
                var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace);

                // 1. Create vault
                string resourceGroupName = ConfigurationManager.AppSettings["RsVaultRgNameRP"];
                string resourceName = ConfigurationManager.AppSettings["RsVaultNameRP"];
                string location = ConfigurationManager.AppSettings["vaultLocationRP"];
                VaultTestHelpers vaultTestHelper = new VaultTestHelpers(client);
                vaultTestHelper.CreateVault(resourceGroupName, resourceName, location);

                // 2. Get default policy
                PolicyTestHelpers policyTestHelper = new PolicyTestHelpers(client);
                string policyId = policyTestHelper.GetDefaultPolicyId(resourceGroupName, resourceName);

                // 3. Enable protection
                // TODO: Create VM instead of taking these parameters from config
                string containerUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMContainerUniqueNameRP"];
                string itemUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMItemUniqueNameRP"];
                string containeType = ConfigurationManager.AppSettings["IaaSVMContainerType"];
                string itemType = ConfigurationManager.AppSettings["IaaSVMItemType"];
                string containerUri = containeType + ";" + containerUniqueName;
                string itemUri = itemType + ";" + itemUniqueName;
                ProtectedItemTestHelpers protectedItemTestHelper = new ProtectedItemTestHelpers(client);
                protectedItemTestHelper.EnableProtection(resourceGroupName, resourceName, policyId, containerUri, itemUri);

                // 4. Trigger backup and wait for completion
                BackupTestHelpers backupTestHelper = new BackupTestHelpers(client);
                DateTime backupStartTime = DateTime.UtcNow;
                string jobId = backupTestHelper.BackupProtectedItem(resourceGroupName, resourceName, containerUri, itemUri);
                JobTestHelpers jobTestHelper = new JobTestHelpers(client);
                jobTestHelper.WaitForJob(resourceGroupName, resourceName, jobId);
                DateTime backupEndTime = DateTime.UtcNow;

                // 5. Get latest RP
                RecoveryPointTestHelpers recoveryPointTestHelper = new RecoveryPointTestHelpers(client);
                var recoveryPoints = recoveryPointTestHelper.ListRecoveryPoints(
                    resourceGroupName, resourceName, containerUri, itemUri, backupStartTime, backupEndTime);
                var recoveryPointResource = recoveryPoints[0];
                var recoveryPoint = (RecoveryPoint)recoveryPointResource.Properties;

                // ACTION: Get RP details
                string fabricName = ConfigurationManager.AppSettings["AzureBackupFabricName"];
                var response = client.RecoveryPoints.Get(
                    resourceGroupName, resourceName, CommonTestHelper.GetCustomRequestHeaders(),
                    fabricName, containerUri, itemUri, recoveryPointResource.Name);

                // VALIDATION: Should be valid RP
                Assert.NotNull(response);
                Assert.NotNull(response.RecPoint);
                Assert.True(!string.IsNullOrEmpty(response.RecPoint.Name), "RP Id cant be null");
                Assert.True(!string.IsNullOrEmpty(((RecoveryPoint)response.RecPoint.Properties).RecoveryPointTime),
                    "RecoveryPointTime can't be null or empty");
                Assert.True(!string.IsNullOrEmpty(((RecoveryPoint)response.RecPoint.Properties).SourceVMStorageType),
                    "SourceVMStorageType can't be null or empty");
            }
        }
        public void PolicyCrudTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"];
                string resourceGroupName = ConfigurationManager.AppSettings["RsVaultRgNameRP"];
                string resourceName = ConfigurationManager.AppSettings["RsVaultNameRP"];
                string location = ConfigurationManager.AppSettings["vaultLocationRP"];
                string defaultPolicyName = ConfigurationManager.AppSettings["DefaultPolicyName"];

                var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace);

                // 1. Create vault
                VaultTestHelpers vaultTestHelper = new VaultTestHelpers(client);
                vaultTestHelper.CreateVault(resourceGroupName, resourceName, location);

                PolicyTestHelpers policyTestHelper = new PolicyTestHelpers(client);

                // ACTION: Get default policy
                ProtectionPolicyResponse response = policyTestHelper.GetProtectionPolicy(resourceGroupName, resourceName, defaultPolicyName);

                // VALIDATION: Name should match
                Assert.NotNull(response.Item.Name);
                Assert.Equal(response.Item.Name, defaultPolicyName);
                Assert.NotNull(response.Item.Id);
                Assert.NotNull(response.Item.Type);
                Assert.NotNull(response.Item.Properties);

                // ACTION: Add new policy
                ProtectionPolicyRequest request = new ProtectionPolicyRequest();
                request.Item = new ProtectionPolicyResource();
                request.Item.Properties = response.Item.Properties;
                string newPolicyName = defaultPolicyName + "_updated";
                response = policyTestHelper.AddOrUpdateProtectionPolicy(resourceGroupName, resourceName, newPolicyName, request);

                // ACTION: Update the policy
                response = policyTestHelper.AddOrUpdateProtectionPolicy(resourceGroupName, resourceName, newPolicyName, request);

                // VALIDATION: Name should match
                Assert.NotNull(response.Item.Name);
                Assert.Equal(response.Item.Name, newPolicyName);
                Assert.NotNull(response.Item.Id);
                Assert.NotNull(response.Item.Type);
                Assert.NotNull(response.Item.Properties);

                // ACTION: Delete the policy
                AzureOperationResponse deleteResponse = policyTestHelper.DeleteProtectionPolicy(resourceGroupName, resourceName, newPolicyName);
                Assert.Equal(deleteResponse.StatusCode, HttpStatusCode.OK);
            }
        }
        public void ListProtectedItemsTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"];
                string resourceGroupName = ConfigurationManager.AppSettings["RsVaultRgNameRP"];
                string resourceName = ConfigurationManager.AppSettings["RsVaultNameRP"];
                string location = ConfigurationManager.AppSettings["vaultLocationRP"];
                // TODO: Create VM instead of taking these parameters from config
                string containerUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMContainerUniqueNameRP"];
                string itemUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMItemUniqueNameRP"];
                string containeType = ConfigurationManager.AppSettings["IaaSVMContainerType"];
                string itemType = ConfigurationManager.AppSettings["IaaSVMItemType"];
                string containerUri = containeType + ";" + containerUniqueName;
                string itemUri = itemType + ";" + itemUniqueName;

                var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace);

                // 1. Create vault
                VaultTestHelpers vaultTestHelper = new VaultTestHelpers(client);
                vaultTestHelper.CreateVault(resourceGroupName, resourceName, location);

                // 2. Get default policy
                PolicyTestHelpers policyTestHelper = new PolicyTestHelpers(client);
                string policyId = policyTestHelper.GetDefaultPolicyId(resourceGroupName, resourceName);

                // 3. Enable protection
                ProtectedItemTestHelpers protectedItemTestHelper = new ProtectedItemTestHelpers(client);
                protectedItemTestHelper.EnableProtection(resourceGroupName, resourceName, policyId, containerUri, itemUri);

                // ACTION: List protected items
                ProtectedItemListQueryParam queryParams = new ProtectedItemListQueryParam();
                queryParams.BackupManagementType = CommonTestHelper.GetSetting(TestConstants.ProviderTypeAzureIaasVM);
                queryParams.DatasourceType = CommonTestHelper.GetSetting(TestConstants.WorkloadTypeVM);
                var response = protectedItemTestHelper.ListProtectedItems(resourceGroupName, resourceName, queryParams);

                // VALIDATION: VM should be found in the list
                Assert.True(response.ItemList.Value.Any(item =>
                    {
                        return item.Properties.GetType().IsSubclassOf(typeof(AzureIaaSVMProtectedItem)) &&
                               item.Name.Contains(itemUniqueName);
                    }),
                    "Retrieved list of items doesn't contain AzureIaaSVMProtectedItem test item");
            }
        }
        public void PolicyCrudTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"];
                string rsVaultRgName = CommonTestHelper.GetSetting(TestConstants.RsVaultRgName);
                string rsVaultName = CommonTestHelper.GetSetting(TestConstants.RsVaultName);

                var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace);
                PolicyTestHelpers policyTestHelper = new PolicyTestHelpers(client);
                string policyName = ConfigurationManager.AppSettings["AzureSqlPolicyName"];

                //create policy request
                SimpleRetentionPolicy retPolicy = new SimpleRetentionPolicy();
                retPolicy.RetentionDuration = new RetentionDuration()
                {
                    DurationType = "Weeks",
                    Count = 6
                };

                AzureSqlProtectionPolicy sqlPolicy = new AzureSqlProtectionPolicy()
                {
                    RetentionPolicy = retPolicy
                };

                ProtectionPolicyRequest policyRequest = new ProtectionPolicyRequest()
                {
                    Item = new ProtectionPolicyResource()
                    {
                        Properties = sqlPolicy
                    }
                };

                //create policy
                ProtectionPolicyResponse response = policyTestHelper.AddOrUpdateProtectionPolicy(rsVaultRgName, rsVaultName, policyName, policyRequest);

                // get policy
                response = policyTestHelper.GetProtectionPolicy(rsVaultRgName, rsVaultName, policyName);
                Assert.NotNull(response.Item.Name);
                Assert.Equal(response.Item.Name, policyName);
                Assert.NotNull(response.Item.Id);
                Assert.NotNull(response.Item.Type);
                Assert.NotNull(response.Item.Properties);
                Assert.NotNull(response.Item.Properties as AzureSqlProtectionPolicy);
                AzureSqlProtectionPolicy resultPolicy = response.Item.Properties as AzureSqlProtectionPolicy;
                SimpleRetentionPolicy resultRetetion = resultPolicy.RetentionPolicy as SimpleRetentionPolicy;
                Assert.Equal(resultRetetion.RetentionDuration.DurationType, "Weeks");
                Assert.Equal(resultRetetion.RetentionDuration.Count, 6);

                //update policy request
                retPolicy.RetentionDuration = new RetentionDuration()
                {
                    DurationType = "Months",
                    Count = 2
                };

                sqlPolicy = new AzureSqlProtectionPolicy()
                {
                    RetentionPolicy = retPolicy
                };

                policyRequest = new ProtectionPolicyRequest()
                {
                    Item = new ProtectionPolicyResource()
                    {
                        Properties = sqlPolicy
                    }
                };


                // update policy
                response = policyTestHelper.AddOrUpdateProtectionPolicy(rsVaultRgName, rsVaultName, policyName, policyRequest);
                // validations
                Assert.NotNull(response.Item.Name);
                Assert.Equal(response.Item.Name, policyName);
                Assert.NotNull(response.Item.Id);
                Assert.NotNull(response.Item.Type);
                Assert.NotNull(response.Item.Properties);
                Assert.NotNull(response.Item.Properties as AzureSqlProtectionPolicy);
                resultPolicy = response.Item.Properties as AzureSqlProtectionPolicy;
                resultRetetion = resultPolicy.RetentionPolicy as SimpleRetentionPolicy;
                Assert.Equal(resultRetetion.RetentionDuration.DurationType, "Months");
                Assert.Equal(resultRetetion.RetentionDuration.Count, 2);


                // delete the policy
                AzureOperationResponse deleteResponse = policyTestHelper.DeleteProtectionPolicy(rsVaultRgName, rsVaultName, policyName);
                Assert.Equal(deleteResponse.StatusCode, HttpStatusCode.OK);
            }
        }