public void UnitTestDoWork_With_Now_In_The_Scheduled_Time_And_Check_Exists_Throws_Error()
        {
            MockRepository mockRepository = new MockRepository();

            // Set start time to 1 hour before now.
            TimeSpan dailyStartTime = (DateTime.Now - DateTime.Today).Subtract(this.oneHour);

            // Set end time to 1 hour after now.
            TimeSpan dailyEndTime = (DateTime.Now - DateTime.Today).Add(this.oneHour);
            const int PollingIntervalInMinutes = 60;

            // Arrange
            IDeployment mockDeployment = mockRepository.StrictMock<IDeployment>();
            IOperation mockOperation = mockRepository.StrictMock<IOperation>();
            mockDeployment
                .Expect(d => d.CheckExists(this.subscriptionId, CertificateThumbprint, ServiceName, DeploymentSlot))
                .Repeat.Once()
                .Throw(new Exception("Error."));

            // Act
            mockRepository.ReplayAll();
            DeploymentCreateForecastWorker deploymentCreateForecastWorker = new DeploymentCreateForecastWorker(
                mockDeployment,
                mockOperation,
                this.subscriptionId,
                CertificateThumbprint,
                ServiceName,
                DeploymentSlot,
                new[] { new ScheduleDay { DayOfWeek = DateTime.Now.DayOfWeek, EndTime = dailyEndTime, StartTime = dailyStartTime } },
                DeploymentName,
                this.packageUrl,
                Label,
                ConfigurationFilePath,
                StartDeployment,
                TreatWarningsAsError,
                PollingIntervalInMinutes);
            deploymentCreateForecastWorker.DoWork();

            // Assert
            mockRepository.VerifyAll();
        }
        public void UnitTestDoWork_With_Now_In_The_Scheduled_Time_But_Not_On_A_Scheduled_Day()
        {
            MockRepository mockRepository = new MockRepository();

            // Set start time to 1 hour before now.
            TimeSpan dailyStartTime = (DateTime.Now - DateTime.Today).Subtract(this.oneHour);

            // Set end time to 1 hour after now.
            TimeSpan dailyEndTime = (DateTime.Now - DateTime.Today).Add(this.oneHour);

            // Get a day that is not today.
            DayOfWeek notToday = DayOfWeek.Sunday;
            if (DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
            {
                notToday = DayOfWeek.Monday;
            }

            const int PollingIntervalInMinutes = 60;

            // Arrange
            IDeployment mockDeployment = mockRepository.StrictMock<IDeployment>();
            IOperation mockOperation = mockRepository.StrictMock<IOperation>();

            // Act
            mockRepository.ReplayAll();
            DeploymentCreateForecastWorker deploymentCreateForecastWorker = new DeploymentCreateForecastWorker(
                mockDeployment,
                mockOperation,
                this.subscriptionId,
                CertificateThumbprint,
                ServiceName,
                DeploymentSlot,
                new[] { new ScheduleDay { DayOfWeek = notToday, EndTime = dailyEndTime, StartTime = dailyStartTime } },
                DeploymentName,
                this.packageUrl,
                Label,
                ConfigurationFilePath,
                StartDeployment,
                TreatWarningsAsError,
                PollingIntervalInMinutes);
            deploymentCreateForecastWorker.DoWork();

            // Assert
            mockRepository.VerifyAll();
        }
        public void UnitTestDoWork_With_Now_In_The_Scheduled_Time_And_Deployment_Exists_And_Second_Call_Within_Polling_Interval()
        {
            MockRepository mockRepository = new MockRepository();

            // Set start time to 1 hour before now.
            TimeSpan dailyStartTime = (DateTime.Now - DateTime.Today).Subtract(this.oneHour);

            // Set end time to 1 hour after now.
            TimeSpan dailyEndTime = (DateTime.Now - DateTime.Today).Add(this.oneHour);
            const int PollingIntervalInMinutes = 60;
            OperationResult operationResult = new OperationResult
                {
                    Code = "Test",
                    HttpStatusCode = HttpStatusCode.OK,
                    Id = Guid.NewGuid(),
                    Message = string.Empty,
                    Status = OperationStatus.Succeeded
                };

            // Arrange
            IDeployment mockDeployment = mockRepository.StrictMock<IDeployment>();
            IOperation mockOperation = mockRepository.StrictMock<IOperation>();
            mockDeployment
                .Expect(d => d.CheckExists(this.subscriptionId, CertificateThumbprint, ServiceName, DeploymentSlot))
                .Repeat.Once()
                .Return(false);
            mockDeployment
                .Expect(d => d.CreateRequest(this.subscriptionId, CertificateThumbprint, ServiceName, DeploymentSlot, DeploymentName, this.packageUrl, Label, ConfigurationFilePath, StartDeployment, TreatWarningsAsError))
                .Repeat.Once()
                .Return(RequestId);
            mockOperation
                .Expect(o => o.StatusCheck(this.subscriptionId, CertificateThumbprint, RequestId))
                .Repeat.Once()
                .Return(operationResult);

            // Act
            mockRepository.ReplayAll();
            DeploymentCreateForecastWorker deploymentCreateForecastWorker = new DeploymentCreateForecastWorker(
                mockDeployment,
                mockOperation,
                this.subscriptionId,
                CertificateThumbprint,
                ServiceName,
                DeploymentSlot,
                new[] { new ScheduleDay { DayOfWeek = DateTime.Now.DayOfWeek, EndTime = dailyEndTime, StartTime = dailyStartTime } },
                DeploymentName,
                this.packageUrl,
                Label,
                ConfigurationFilePath,
                StartDeployment,
                TreatWarningsAsError,
                PollingIntervalInMinutes);
            deploymentCreateForecastWorker.DoWork();
            deploymentCreateForecastWorker.DoWork(); // Call DoWork twice to check the polling window works.

            // Assert
            mockRepository.VerifyAll();
        }