/// <summary>
        /// Helper function to convert ps backup policy model for AzureSql from service response.
        /// </summary>
        public static PolicyBase GetPolicyModelForAzureSql(ServiceClientModel.ProtectionPolicyResource serviceClientResponse,
                                                           PolicyBase policyModel)
        {
            ServiceClientModel.AzureSqlProtectionPolicy azureSqlPolicy =
                (ServiceClientModel.AzureSqlProtectionPolicy)serviceClientResponse.Properties;

            if (azureSqlPolicy.RetentionPolicy.GetType() !=
                typeof(ServiceClientModel.SimpleRetentionPolicy))
            {
                Logger.Instance.WriteDebug("Unknown RetentionPolicy object received: " +
                                           azureSqlPolicy.RetentionPolicy.GetType());
                Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
                return(null);
            }

            policyModel = new AzureSqlPolicy();
            AzureSqlPolicy sqlPolicyModel = policyModel as AzureSqlPolicy;

            sqlPolicyModel.WorkloadType         = WorkloadType.AzureSQLDatabase;
            sqlPolicyModel.BackupManagementType = BackupManagementType.AzureSQL;

            ServiceClientModel.SimpleRetentionPolicy azureSqlRetentionPolicy =
                (ServiceClientModel.SimpleRetentionPolicy)azureSqlPolicy.RetentionPolicy;
            sqlPolicyModel.RetentionPolicy =
                PolicyHelpers.GetPSSimpleRetentionPolicy(azureSqlRetentionPolicy, null, "AzureSql");
            return(policyModel);
        }
        /// <summary>
        /// Helper function to convert ps backup policy model from service response.
        /// </summary>
        public static PolicyBase GetPolicyModel(ServiceClientModel.ProtectionPolicyResource serviceClientResponse)
        {
            PolicyBase policyModel = null;

            if (serviceClientResponse == null || serviceClientResponse.Properties == null)
            {
                Logger.Instance.WriteDebug("Policy Service Client response is Null/Empty");
                throw new ArgumentException(Resources.EmptyServiceClientResponseException);
            }

            if (serviceClientResponse.Properties.GetType() == typeof(ServiceClientModel.AzureIaaSVMProtectionPolicy))
            {
                if (((ServiceClientModel.AzureIaaSVMProtectionPolicy)serviceClientResponse.Properties).RetentionPolicy.GetType() !=
                    typeof(ServiceClientModel.LongTermRetentionPolicy))
                {
                    Logger.Instance.WriteDebug("Unknown RetentionPolicy object received: " +
                                               ((ServiceClientModel.AzureIaaSVMProtectionPolicy)serviceClientResponse.Properties).RetentionPolicy.GetType());
                    Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
                    return(null);
                }

                if (((ServiceClientModel.AzureIaaSVMProtectionPolicy)serviceClientResponse.Properties).SchedulePolicy.GetType() !=
                    typeof(ServiceClientModel.SimpleSchedulePolicy))
                {
                    Logger.Instance.WriteDebug("Unknown SchedulePolicy object received: " +
                                               ((ServiceClientModel.AzureIaaSVMProtectionPolicy)serviceClientResponse.Properties).SchedulePolicy.GetType());
                    Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
                    return(null);
                }

                policyModel = new AzureVmPolicy();
                AzureVmPolicy iaasPolicyModel = policyModel as AzureVmPolicy;
                iaasPolicyModel.WorkloadType         = WorkloadType.AzureVM;
                iaasPolicyModel.BackupManagementType = BackupManagementType.AzureVM;
                iaasPolicyModel.RetentionPolicy      = PolicyHelpers.GetPSLongTermRetentionPolicy((ServiceClientModel.LongTermRetentionPolicy)
                                                                                                      ((ServiceClientModel.AzureIaaSVMProtectionPolicy)serviceClientResponse.Properties).RetentionPolicy,
                                                                                                  ((ServiceClientModel.AzureIaaSVMProtectionPolicy)serviceClientResponse.Properties).TimeZone);
                iaasPolicyModel.SchedulePolicy = PolicyHelpers.GetPSSimpleSchedulePolicy((ServiceClientModel.SimpleSchedulePolicy)
                                                                                             ((ServiceClientModel.AzureIaaSVMProtectionPolicy)serviceClientResponse.Properties).SchedulePolicy,
                                                                                         ((ServiceClientModel.AzureIaaSVMProtectionPolicy)serviceClientResponse.Properties).TimeZone);
            }
            else if (serviceClientResponse.Properties.GetType() ==
                     typeof(ServiceClientModel.AzureSqlProtectionPolicy))
            {
                ServiceClientModel.AzureSqlProtectionPolicy azureSqlPolicy =
                    (ServiceClientModel.AzureSqlProtectionPolicy)serviceClientResponse.Properties;

                if (azureSqlPolicy.RetentionPolicy.GetType() !=
                    typeof(ServiceClientModel.SimpleRetentionPolicy))
                {
                    Logger.Instance.WriteDebug("Unknown RetentionPolicy object received: " +
                                               azureSqlPolicy.RetentionPolicy.GetType());
                    Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
                    return(null);
                }

                policyModel = new AzureSqlPolicy();
                AzureSqlPolicy sqlPolicyModel = policyModel as AzureSqlPolicy;
                sqlPolicyModel.WorkloadType         = WorkloadType.AzureSQLDatabase;
                sqlPolicyModel.BackupManagementType = BackupManagementType.AzureSQL;

                ServiceClientModel.SimpleRetentionPolicy azureSqlRetentionPolicy =
                    (ServiceClientModel.SimpleRetentionPolicy)azureSqlPolicy.RetentionPolicy;
                sqlPolicyModel.RetentionPolicy =
                    PolicyHelpers.GetPSSimpleRetentionPolicy(azureSqlRetentionPolicy, null);
            }
            else
            {
                // we will enter this case when service supports new workload and customer
                // still using old version of azure powershell. Trace warning message, ignore and return
                Logger.Instance.WriteDebug("Unknown Policy object received: " +
                                           serviceClientResponse.Properties.GetType());
                Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
                return(null);
            }

            policyModel.Name = serviceClientResponse.Name;
            policyModel.Id   = serviceClientResponse.Id;

            return(policyModel);
        }
        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);
            }
        }