public void UnitTestDoWork_With_One_Role_Having_Instance_Count_Too_High_And_Second_Role_Having_Instance_Size_Too_Large()
        {
            MockRepository mockRepository = new MockRepository();
            WhiteListService whiteListService = new WhiteListService { Name = "wls1" };
            WhiteListRole whiteListRole = new WhiteListRole { Name = "wlr1", MaxInstanceSize = InstanceSize.ExtraSmall, MaxInstanceCount = 1 };
            whiteListService.Roles.Add(whiteListRole);
            WhiteListService[] whiteListServices = new[] { whiteListService };

            // Arrange
            ISubscription mockSubscription = mockRepository.StrictMock<ISubscription>();
            mockSubscription
                .Expect(s => s.ListHostedServices())
                .Repeat.Once()
                .Return(new[] { "bls1", "wls1" });
            mockSubscription
                .Expect(s => s.SubscriptionId)
                .Repeat.Any()
                .Return(SubscriptionId);
            mockSubscription
                .Expect(s => s.CertificateThumbprint)
                .Repeat.Any()
                .Return(CertificateThumbprint);
            ISubscription[] subscriptions = new[] { mockSubscription };
            IDeployment mockDeployment = mockRepository.StrictMock<IDeployment>();
            IOperation mockOperation = mockRepository.StrictMock<IOperation>();

            // Delete the black listed service from Staging and Production
            SetupDeleteRequestWithStatusCheck(mockDeployment, mockOperation, "bls1", DeploymentSlot.Staging, "deleteRequest1");
            SetupDeleteRequestWithStatusCheck(mockDeployment, mockOperation, "bls1", DeploymentSlot.Production, "deleteRequest2");

            // Get the instance size of White List Role 1 from Staging, all okay.
            mockDeployment
                .Expect(d => d.GetInstanceSize(SubscriptionId, CertificateThumbprint, "wls1", DeploymentSlot.Staging, "wlr1"))
                .Repeat.Once()
                .Return(InstanceSize.ExtraSmall.ToString());

            // Get the instance count of White List Role 1 from Staging, horizontally scale as a result.
            mockDeployment
                .Expect(d => d.GetInstanceCount(SubscriptionId, CertificateThumbprint, "wls1", DeploymentSlot.Staging, "wlr1"))
                .Repeat.Once()
                .Return(2);
            SetupHorizontallyScaleWithStatusCheck(mockDeployment, mockOperation, "wls1", DeploymentSlot.Staging, new[] { new HorizontalScale { RoleName = "wlr1", InstanceCount = 1 } }, "scaleRequest1");

            // Get the instance size of the White List Role 1 from Production, delete as a result.
            mockDeployment
                .Expect(d => d.GetInstanceSize(SubscriptionId, CertificateThumbprint, "wls1", DeploymentSlot.Production, "wlr1"))
                .Repeat.Once()
                .Return(InstanceSize.ExtraLarge.ToString());
            SetupDeleteRequestWithStatusCheck(mockDeployment, mockOperation, "wls1", DeploymentSlot.Production, "deleteRequest3");

            // Act
            mockRepository.ReplayAll();
            WhiteListForecastWorker whiteListForecastWorker = new WhiteListForecastWorker(subscriptions, mockDeployment, mockOperation, whiteListServices, 1);
            whiteListForecastWorker.DoWork();
            whiteListForecastWorker.DoWork(); // Call twice to test the polling interval (nothing should be invoked the second time).

            // Assert
            mockRepository.VerifyAll();
        }
        public WhiteListConfiguration GetWindowsAzureHostedServiceWhiteListConfiguration()
        {
            // The "StealFocusForecastConfiguration.Instance.WindowsAzure.HostedService.WhiteList" property is
            // never null because it's a "Configuration Element Collection". So the best way to test if the White
            // List has been explicitly configured is if the "PollingIntervalInMinutes" is set (i.e. not "0").
            if (StealFocusForecastConfiguration.Instance.WindowsAzure.HostedService.WhiteList.PollingIntervalInMinutes > 0)
            {
                WhiteListConfiguration whiteListConfiguration = new WhiteListConfiguration();
                whiteListConfiguration.IncludeDeploymentCreateServices = StealFocusForecastConfiguration.Instance.WindowsAzure.HostedService.WhiteList.IncludeDeploymentCreateServices;
                whiteListConfiguration.IncludeDeploymentDeleteServices = StealFocusForecastConfiguration.Instance.WindowsAzure.HostedService.WhiteList.IncludeDeploymentDeleteServices;
                whiteListConfiguration.IncludeHorizontalScaleServices = StealFocusForecastConfiguration.Instance.WindowsAzure.HostedService.WhiteList.IncludeHorizontalScaleServices;
                whiteListConfiguration.PollingIntervalInMinutes = StealFocusForecastConfiguration.Instance.WindowsAzure.HostedService.WhiteList.PollingIntervalInMinutes;
                foreach (ServiceConfigurationElement serviceConfigurationElement in StealFocusForecastConfiguration.Instance.WindowsAzure.HostedService.WhiteList)
                {
                    WhiteListService whiteListService = new WhiteListService { Name = serviceConfigurationElement.Name };
                    foreach (RoleConfigurationElement roleConfigurationElement in serviceConfigurationElement.Roles)
                    {
                        WhiteListRole whiteListRole = new WhiteListRole { Name = roleConfigurationElement.Name };
                        if (!string.IsNullOrEmpty(roleConfigurationElement.MaxInstanceSize))
                        {
                            InstanceSize instanceSize;
                            bool parsed = System.Enum.TryParse(roleConfigurationElement.MaxInstanceSize, true, out instanceSize);
                            if (!parsed)
                            {
                                string exceptionMessage = string.Format(CultureInfo.CurrentCulture, "The configured instance size of '{0}' could not be parsed as a valid Azure instance size. Valid values are '{1}', '{2}', '{3}', '{4}' or '{5}'.", roleConfigurationElement.MaxInstanceSize, InstanceSize.ExtraSmall, InstanceSize.Small, InstanceSize.Medium, InstanceSize.Large, InstanceSize.ExtraLarge);
                                throw new ForecastException(exceptionMessage);
                            }

                            whiteListRole.MaxInstanceSize = instanceSize;
                        }

                        if (roleConfigurationElement.MaxInstanceCount > 0)
                        {
                            whiteListRole.MaxInstanceCount = roleConfigurationElement.MaxInstanceCount;
                        }

                        whiteListService.Roles.Add(whiteListRole);
                    }

                    whiteListConfiguration.Services.Add(whiteListService);
                }

                return whiteListConfiguration;
            }

            return null;
        }