public void Test_CreateDeployemntTask_RunsProperly_WhenAllIsWell()
        {
            var objectFactory = new Mock<IObjectFactory>(MockBehavior.Strict);
              var envInfoRepository = new Mock<IEnvironmentInfoRepository>(MockBehavior.Loose);
              var artifactsRepository = new Mock<IArtifactsRepository>(MockBehavior.Loose);
              var taskScheduler = new Mock<ITaskScheduler>(MockBehavior.Loose);
              var passwordCollector = new Mock<IPasswordCollector>(MockBehavior.Loose);

              var schedulerAppProjectInfo =
            new SchedulerAppProjectInfo(
              _ProjectName,
              _ArtifactsRepositoryName,
              _ArtifactsRepositoryDirName,
              _ArtifactsAreNotEnvirionmentSpecific,
              _SchedulerAppName,
              _SchedulerAppDirName,
              _SchedulerAppExeName,
              _SchedulerAppUserId,
              _ScheduledHour,
              _ScheduledMinute,
              _ExecutionTimeLimitInMinutes);

              objectFactory.Setup(o => o.CreateEnvironmentInfoRepository()).Returns(envInfoRepository.Object);
              objectFactory.Setup(o => o.CreateArtifactsRepository()).Returns(artifactsRepository.Object);
              objectFactory.Setup(o => o.CreateTaskScheduler()).Returns(taskScheduler.Object);
              objectFactory.Setup(o => o.CreatePasswordCollector()).Returns(passwordCollector.Object);

              schedulerAppProjectInfo.CreateDeploymentTask(
            objectFactory.Object, "configName", "buildID", "targetEnvironmentName");
        }
        // TODO IMM HI: can we update scheduler app without user name and password?
        public UpdateAppScheduleDeploymentStep(
            ITaskScheduler taskScheduler,
            string machineName,
            SchedulerAppProjectInfo schedulerAppProjectInfo,
            string executablePath,
            string userName,
            string password)
        {
            if (taskScheduler == null)
              {
            throw new ArgumentNullException("taskScheduler");
              }

              if (string.IsNullOrEmpty(machineName))
              {
            throw new ArgumentException("Argument can't be null nor empty.", "machineName");
              }

              if (schedulerAppProjectInfo == null)
              {
            throw new ArgumentNullException("schedulerAppProjectInfo");
              }

              if (string.IsNullOrEmpty(executablePath))
              {
            throw new ArgumentException("Argument can't be null nor empty.", "executablePath");
              }

              if (!Path.IsPathRooted(executablePath))
              {
            throw new ArgumentException(string.Format("Executable path ('{0}') is not an absolute path.", executablePath), "executablePath");
              }

              if (string.IsNullOrEmpty(userName))
              {
            throw new ArgumentException("Argument can't be null nor empty.", "userName");
              }

              if (string.IsNullOrEmpty(password))
              {
            throw new ArgumentException("Argument can't be null nor empty.", "password");
              }

              _taskScheduler = taskScheduler;
              _machineName = machineName;
              _schedulerAppProjectInfo = schedulerAppProjectInfo;
              _executablePath = executablePath;
              _userName = userName;
              _password = password;
        }
        public DeploySchedulerAppDeploymentTask(
      IEnvironmentInfoRepository environmentInfoRepository,
      IArtifactsRepository artifactsRepository,
      ITaskScheduler taskScheduler,
      IPasswordCollector passwordCollector,
      SchedulerAppProjectInfo projectInfo,
      string projectConfigurationName,
      string projectConfigurationBuildId,
      string targetEnvironmentName)
            : base(environmentInfoRepository, targetEnvironmentName)
        {
            if (artifactsRepository == null)
              {
            throw new ArgumentNullException("artifactsRepository");
              }

              if (taskScheduler == null)
              {
            throw new ArgumentNullException("taskScheduler");
              }

              if (passwordCollector == null)
              {
            throw new ArgumentNullException("passwordCollector");
              }

              if (projectInfo == null)
              {
            throw new ArgumentNullException("projectInfo");
              }

              if (string.IsNullOrEmpty(projectConfigurationName))
              {
            throw new ArgumentException("Argument can't be null nor empty.", "projectConfigurationName");
              }

              if (string.IsNullOrEmpty(projectConfigurationBuildId))
              {
            throw new ArgumentException("Argument can't be null nor empty.", "projectConfigurationBuildId");
              }

              _artifactsRepository = artifactsRepository;
              _taskScheduler = taskScheduler;
              _passwordCollector = passwordCollector;
              _projectInfo = projectInfo;
              _projectConfigurationName = projectConfigurationName;
              _projectConfigurationBuildId = projectConfigurationBuildId;
        }
        public void Test_CreateDeployemntTask_RunsProperly_WhenAllIsWell()
        {
            var objectFactory = new Mock<IObjectFactory>(MockBehavior.Strict);
              var prjInfoRepository = new Mock<IProjectInfoRepository>(MockBehavior.Loose);
              var envInfoRepository = new Mock<IEnvironmentInfoRepository>(MockBehavior.Loose);
              var artifactsRepository = new Mock<IArtifactsRepository>(MockBehavior.Loose);
              var taskScheduler = new Mock<ITaskScheduler>(MockBehavior.Loose);
              var passwordCollector = new Mock<IPasswordCollector>(MockBehavior.Loose);
              var directoryAdapter = new Mock<IDirectoryAdapter>(MockBehavior.Loose);
              var fileAdapter = new Mock<IFileAdapter>(MockBehavior.Loose);
              var zipFileAdapter = new Mock<IZipFileAdapter>(MockBehavior.Loose);

              var schedulerAppProjectInfo =
            new SchedulerAppProjectInfo(
              _ProjectName,
              _ArtifactsRepositoryName,
              _AllowedEnvironmentNames,
              _ArtifactsRepositoryDirName,
              _ArtifactsAreNotEnvirionmentSpecific,
              _SchedulerAppDirName,
              _SchedulerAppExeName,
              new List<SchedulerAppTask>
              {
            new SchedulerAppTask(
              _SchedulerAppName,
              _SchedulerAppName,
              _SchedulerAppUserId,
              _ScheduledHour,
              _ScheduledMinute,
              _ExecutionTimeLimitInMinutes,
              _Repetition)
              });

              objectFactory.Setup(o => o.CreateProjectInfoRepository()).Returns(prjInfoRepository.Object);
              objectFactory.Setup(o => o.CreateEnvironmentInfoRepository()).Returns(envInfoRepository.Object);
              objectFactory.Setup(o => o.CreateArtifactsRepository()).Returns(artifactsRepository.Object);
              objectFactory.Setup(o => o.CreateTaskScheduler()).Returns(taskScheduler.Object);
              objectFactory.Setup(o => o.CreatePasswordCollector()).Returns(passwordCollector.Object);
              objectFactory.Setup(o => o.CreateDirectoryAdapter()).Returns(directoryAdapter.Object);
              objectFactory.Setup(o => o.CreateFileAdapter()).Returns(fileAdapter.Object);
              objectFactory.Setup(o => o.CreateZipFileAdapter()).Returns(zipFileAdapter.Object);

              schedulerAppProjectInfo.CreateDeploymentTask(objectFactory.Object);
        }
        public void Test_GetTargetFolde_RunsProperly_WhenAllIsWell()
        {
            string machine = Environment.MachineName;
              const string baseDirPath = "c:\\basedir";

              var envInfo =
            new EnvironmentInfo(
              "name",
              true,
              "templates",
              "appservermachine",
              "failover",
              new[] { "webmachine" },
              "terminalmachine",
              new[] { machine },
              new[] { machine },
              baseDirPath,
              "webbasedir",
              "c:\\scheduler",
              "terminal",
              false,
              TestData.EnvironmentUsers,
              TestData.AppPoolInfos,
              TestData.DatabaseServers,
              TestData.ProjectToFailoverClusterGroupMappings,
              TestData.WebAppProjectConfigurationOverrides,
              TestData.DbProjectConfigurationOverrides,
              "terminalAppsShortcutFolder",
              "artifactsDeploymentDirPath",
              "domain-name",
              TestData.CustomEnvMachines);

              var schedulerAppProjectInfo =
            new SchedulerAppProjectInfo(
              _ProjectName,
              _ArtifactsRepositoryName,
              _AllowedEnvironmentNames,
              _ArtifactsRepositoryDirName,
              _ArtifactsAreNotEnvirionmentSpecific,
              _SchedulerAppDirName,
              _SchedulerAppExeName,
              new List<SchedulerAppTask>
              {
            new SchedulerAppTask(
              _SchedulerAppName,
              _SchedulerAppName,
              _SchedulerAppUserId,
              _ScheduledHour,
              _ScheduledMinute,
              _ExecutionTimeLimitInMinutes,
              _Repetition)
              });

              List<string> targetFolders =
            schedulerAppProjectInfo.GetTargetFolders(_objectFactoryFake.Object, envInfo)
              .ToList();

              Assert.IsNotNull(targetFolders);
              Assert.AreEqual(1, targetFolders.Count);
              Assert.AreEqual("\\\\" + machine + "\\c$\\scheduler\\" + _SchedulerAppDirName, targetFolders[0]);
        }
        public void SetUp()
        {
            _projectInfoRepositoryFake = new Mock<IProjectInfoRepository>();
              _environmentInfoRepositoryFake = new Mock<IEnvironmentInfoRepository>();
              _artifactsRepositoryFake = new Mock<IArtifactsRepository>();
              _taskSchedulerFake = new Mock<ITaskScheduler>();
              _passwordCollectorFake = new Mock<IPasswordCollector>();
              _directoryAdapterFake = new Mock<IDirectoryAdapter>();
              _fileAdapterFake = new Mock<IFileAdapter>();
              _zipFileAdapterFake = new Mock<IZipFileAdapter>();

              _projectInfo = ProjectInfoGenerator.GetSchedulerAppProjectInfo();

              SchedulerAppTask schedulerAppTask1 = _projectInfo.SchedulerAppTasks.First();
              SchedulerAppTask schedulerAppTask2 = _projectInfo.SchedulerAppTasks.Second();

              _environmentInfo =
            DeploymentDataGenerator.GetEnvironmentInfo(
              new[]
              {
            new EnvironmentUser(schedulerAppTask1.UserId, "user_name_1"),
            new EnvironmentUser(schedulerAppTask2.UserId, "user_name_2"),
              });

              _projectInfoRepositoryFake.Setup(pir => pir.FindByName(It.IsAny<string>()))
            .Returns(_projectInfo);

              string exeAbsolutePath1 =
            Path.Combine(
              _environmentInfo.SchedulerAppsBaseDirPath,
              _projectInfo.SchedulerAppDirName,
              schedulerAppTask1.ExecutableName);

              var scheduledTaskRepetition1 =
            new ScheduledTaskRepetition(
              schedulerAppTask1.Repetition.Interval,
              schedulerAppTask1.Repetition.Duration,
              schedulerAppTask1.Repetition.StopAtDurationEnd);

              ScheduledTaskDetails taskDetails1 =
            GetTaskDetails(schedulerAppTask1, exeAbsolutePath1, true, false, scheduledTaskRepetition1);

              ScheduledTaskDetails taskDetailsDisabled1 =
            GetTaskDetails(schedulerAppTask1, exeAbsolutePath1, false, false, scheduledTaskRepetition1);

              int timesCalled11 = 0;

              _taskSchedulerFake
            .Setup(x => x.GetScheduledTaskDetails(_environmentInfo.SchedulerServerTasksMachineNames.First(), schedulerAppTask1.Name))
            .Returns(() =>
            {
              timesCalled11++;

              if (timesCalled11 == 1)
              {
            return taskDetails1;
              }

              if (timesCalled11 == 2)
              {
            return taskDetailsDisabled1;
              }

              throw new Exception("Unexpected number of calls!");
            });

              int timesCalled21 = 0;

              _taskSchedulerFake
            .Setup(x => x.GetScheduledTaskDetails(_environmentInfo.SchedulerServerTasksMachineNames.Second(), schedulerAppTask1.Name))
            .Returns(() =>
            {
              timesCalled21++;

              if (timesCalled21 == 1)
              {
            return taskDetails1;
              }

              if (timesCalled21 == 2)
              {
            return taskDetailsDisabled1;
              }

              throw new Exception("Unexpected number of calls!");
            });

              string exeAbsolutePath2 =
            Path.Combine(
              _environmentInfo.SchedulerAppsBaseDirPath,
              _projectInfo.SchedulerAppDirName,
              schedulerAppTask2.ExecutableName);

              var scheduledTaskRepetition2 =
            new ScheduledTaskRepetition(
              schedulerAppTask2.Repetition.Interval,
              schedulerAppTask2.Repetition.Duration,
              schedulerAppTask2.Repetition.StopAtDurationEnd);

              ScheduledTaskDetails taskDetails2 =
            GetTaskDetails(schedulerAppTask2, exeAbsolutePath2, true, false, scheduledTaskRepetition2);

              ScheduledTaskDetails taskDetailsDisabled2 =
            GetTaskDetails(schedulerAppTask2, exeAbsolutePath2, false, false, scheduledTaskRepetition2);

              int timesCalled12 = 0;

              _taskSchedulerFake
            .Setup(x => x.GetScheduledTaskDetails(_environmentInfo.SchedulerServerTasksMachineNames.First(), schedulerAppTask2.Name))
            .Returns(() =>
            {
              timesCalled12++;

              if (timesCalled12 == 1)
              {
            return taskDetails2;
              }

              if (timesCalled12 == 2)
              {
            return taskDetailsDisabled2;
              }

              throw new Exception("Unexpected number of calls!");
            });

              int timesCalled22 = 0;

              _taskSchedulerFake
            .Setup(x => x.GetScheduledTaskDetails(_environmentInfo.SchedulerServerTasksMachineNames.Second(), schedulerAppTask2.Name))
            .Returns(() =>
            {
              timesCalled22++;

              if (timesCalled22 == 1)
              {
            return taskDetails2;
              }

              if (timesCalled22 == 2)
              {
            return taskDetailsDisabled2;
              }

              throw new Exception("Unexpected number of calls!");
            });

              _environmentInfoRepositoryFake
            .Setup(x => x.FindByName(It.IsAny<string>()))
            .Returns(_environmentInfo);

              _directoryAdapterFake
            .Setup(x => x.Exists(It.IsAny<string>()))
            .Returns(true);

              _deployTask =
            new DeploySchedulerAppDeploymentTask(
              _projectInfoRepositoryFake.Object,
              _environmentInfoRepositoryFake.Object,
              _artifactsRepositoryFake.Object,
              _taskSchedulerFake.Object,
              _passwordCollectorFake.Object,
              _directoryAdapterFake.Object,
              _fileAdapterFake.Object,
              _zipFileAdapterFake.Object);

              _deployTask.Initialize(DeploymentInfoGenerator.GetSchedulerAppDeploymentInfo());
        }
        protected override void DoPrepare()
        {
            EnvironmentInfo environmentInfo = GetEnvironmentInfo();

              _projectInfo = GetProjectInfo<SchedulerAppProjectInfo>();
              _collectedPasswordsByUserName = new Dictionary<string, string>();
              _existingTaskDetailsByMachineNameAndTaskName = new Dictionary<Tuple<string, string>, ScheduledTaskDetails>();

              foreach (string tmpSchedulerServerTasksMachineName in environmentInfo.SchedulerServerTasksMachineNames)
              {
            string schedulerServerTasksMachineName = tmpSchedulerServerTasksMachineName;

            _projectInfo.SchedulerAppTasks
              .ForEach(
            schedulerAppTask =>
            {
              ScheduledTaskDetails taskDetails =
                _taskScheduler.GetScheduledTaskDetails(schedulerServerTasksMachineName, schedulerAppTask.Name);

              _existingTaskDetailsByMachineNameAndTaskName.Add(
                Tuple.Create(schedulerServerTasksMachineName, schedulerAppTask.Name),
                taskDetails);

              EnsureTaskIsNotRunning(taskDetails, schedulerServerTasksMachineName);

              // create a step to disable scheduler task
              if (taskDetails != null && taskDetails.IsEnabled)
              {
                AddToggleSchedulerAppTaskEnabledStep(schedulerServerTasksMachineName, taskDetails.Name, false);
              }
            });
              }

              Lazy<string> binariesDirPathProvider =
            AddStepsToObtainBinaries(environmentInfo);

            /* // TODO IMM HI: xxx we don't need this for now - should we parameterize this somehow?
              if (_directoryAdapter.Exists(targetDirNetworkPath))
              {
            AddSubTask(
              new BackupFilesDeploymentStep(
            targetDirNetworkPath));
              }
            */

              // create steps for copying the binaries to target binaries machines
              foreach (string schedulerServerBinariesMachineName in environmentInfo.SchedulerServerBinariesMachineNames)
              {
            string targetDirPath = GetTargetDirPath(environmentInfo);
            string targetDirNetworkPath = environmentInfo.GetSchedulerServerNetworkPath(schedulerServerBinariesMachineName, targetDirPath);

            AddSubTask(
              new CopyFilesDeploymentStep(
            _directoryAdapter,
            _fileAdapter,
            binariesDirPathProvider,
            new Lazy<string>(() => targetDirNetworkPath)));
              }

              foreach (string tmpSchedulerServerTasksMachineName in environmentInfo.SchedulerServerTasksMachineNames)
              {
            string schedulerServerTasksMachineName = tmpSchedulerServerTasksMachineName;

            _projectInfo.SchedulerAppTasks
              .ForEach(
            schedulerAppTask =>
            {
              string taskName = schedulerAppTask.Name;

              Tuple<string, string> machineNameAndTaskName =
                Tuple.Create(schedulerServerTasksMachineName, taskName);

              ScheduledTaskDetails existingTaskDetails =
                _existingTaskDetailsByMachineNameAndTaskName[machineNameAndTaskName];

              AddTaskConfigurationSteps(
                environmentInfo,
                schedulerServerTasksMachineName,
                schedulerAppTask,
                existingTaskDetails);

              // create a step to toggle scheduler task enabled
              if (existingTaskDetails == null || existingTaskDetails.IsEnabled)
              {
                AddToggleSchedulerAppTaskEnabledStep(
                  schedulerServerTasksMachineName,
                  taskName,
                  true);
              }
            });
              }
        }
        public void Test_CreateDeployemntTask_Throws_When_ObjectFactory_null()
        {
            var schedulerAppProjectInfo =
            new SchedulerAppProjectInfo(
              _ProjectName,
              _ArtifactsRepositoryName,
              _ArtifactsRepositoryDirName,
              _ArtifactsAreNotEnvirionmentSpecific,
              _SchedulerAppName,
              _SchedulerAppDirName,
              _SchedulerAppExeName,
              _SchedulerAppUserId,
              _ScheduledHour,
              _ScheduledMinute,
              _ExecutionTimeLimitInMinutes);

              Assert.Throws<ArgumentNullException>(
            () => schedulerAppProjectInfo.CreateDeploymentTask(
              null, "configName", "buildID", "targetEnvironmentName"));
        }
        public void Test_GetTargetFolde_Throws_EnvInfo_null()
        {
            var schedulerAppProjectInfo =
            new SchedulerAppProjectInfo(
              _ProjectName,
              _ArtifactsRepositoryName,
              _ArtifactsRepositoryDirName,
              _ArtifactsAreNotEnvirionmentSpecific,
              _SchedulerAppName,
              _SchedulerAppDirName,
              _SchedulerAppExeName,
              _SchedulerAppUserId,
              _ScheduledHour,
              _ScheduledMinute,
              _ExecutionTimeLimitInMinutes);

              Assert.Throws<ArgumentNullException>(() => schedulerAppProjectInfo.GetTargetFolders(null));
        }
        public void Test_GetTargetFolde_RunsProperly_WhenAllIsWell()
        {
            string machine = Environment.MachineName;
              const string baseDirPath = "c:\\basedir";

              var envInfo =
            new EnvironmentInfo(
              "name",
              "templates",
              machine,
              "failover",
              new[] { "webmachine" },
              "terminalmachine",
              "databasemachine",
              baseDirPath,
              "webbasedir",
              "c:\\scheduler",
              "terminal",
              false,
              _EnvironmentUsers,
              _ProjectToFailoverClusterGroupMappings);

              var schedulerAppProjectInfo =
            new SchedulerAppProjectInfo(
              _ProjectName,
              _ArtifactsRepositoryName,
              _ArtifactsRepositoryDirName,
              _ArtifactsAreNotEnvirionmentSpecific,
              _SchedulerAppName,
              _SchedulerAppDirName,
              _SchedulerAppExeName,
              _SchedulerAppUserId,
              _ScheduledHour,
              _ScheduledMinute,
              _ExecutionTimeLimitInMinutes);

              List<string> targetFolders =
            schedulerAppProjectInfo.GetTargetFolders(envInfo)
              .ToList();

              Assert.IsNotNull(targetFolders);
              Assert.AreEqual(1, targetFolders.Count);
              Assert.AreEqual("\\\\" + machine + "\\c$\\scheduler\\" + _SchedulerAppDirName, targetFolders[0]);
        }