Inheritance: Dev2.Settings.Scheduler.SchedulerViewModel
 public void SchedulerViewModel_SaveCommand_UserNamePasswordSet_CallsIsAuthorised()
 {
     //------------Setup for test--------------------------
     var resources = new ObservableCollection<IScheduledResource>();
     var scheduledResourceForTest = new ScheduledResourceForTest { IsDirty = true };
     resources.Add(scheduledResourceForTest);
     scheduledResourceForTest.UserName = "******";
     scheduledResourceForTest.Password = "******";
     var schedulerViewModel = new SchedulerViewModelForTest();
     var env = new Mock<IEnvironmentModel>();
     var auth = new Mock<IAuthorizationService>();
     env.Setup(a => a.IsConnected).Returns(true);
     env.Setup(a => a.AuthorizationService).Returns(auth.Object);
     auth.Setup(a => a.IsAuthorized(AuthorizationContext.Administrator, null)).Returns(true).Verifiable();
     schedulerViewModel.CurrentEnvironment = env.Object;
     var mockScheduledResourceModel = new Mock<IScheduledResourceModel>();
     mockScheduledResourceModel.Setup(model => model.ScheduledResources).Returns(resources);
     mockScheduledResourceModel.Setup(model => model.Save(It.IsAny<IScheduledResource>(), It.IsAny<string>(), It.IsAny<string>())).Verifiable();
     schedulerViewModel.ScheduledResourceModel = mockScheduledResourceModel.Object;
     schedulerViewModel.SelectedTask = schedulerViewModel.TaskList[0];
     //------------Execute Test---------------------------
     schedulerViewModel.SaveCommand.Execute(null);
     //------------Assert Results-------------------------
     Assert.IsTrue(schedulerViewModel.ShowSaveErrorDialogCalled);
     Assert.IsFalse(schedulerViewModel.GetCredentialsCalled);
     string errorMessage;
     mockScheduledResourceModel.Verify(model => model.Save(It.IsAny<IScheduledResource>(), out errorMessage), Times.Once());
 }
 public void SchedulerViewModel_SaveCommand_WithNewNameDiffToOldNameCancelDialogResponse_IsNewFalse_ShouldShowDialog()
 {
     //------------Setup for test--------------------------
     var resources = new ObservableCollection<IScheduledResource>();
     var scheduledResourceForTest = new ScheduledResource("Task2", SchedulerStatus.Enabled, DateTime.MaxValue, new Mock<IScheduleTrigger>().Object, "TestFlow1") { OldName = "Task1", IsDirty = true };
     scheduledResourceForTest.IsDirty = true;
     resources.Add(scheduledResourceForTest);
     resources.Add(new ScheduledResource("Task3", SchedulerStatus.Enabled, DateTime.MaxValue, new Mock<IScheduleTrigger>().Object, "TestFlow1") { OldName = "Task3", IsDirty = true });
     Mock<IPopupController> mockPopupController = new Mock<IPopupController>();
     mockPopupController.Setup(c => c.ShowNameChangedConflict(It.IsAny<string>(), It.IsAny<string>())).Returns(MessageBoxResult.Cancel).Verifiable();
     var schedulerViewModel = new SchedulerViewModelForTest(new Mock<IEventAggregator>().Object, new Mock<DirectoryObjectPickerDialog>().Object, mockPopupController.Object, new TestAsyncWorker());
     var env = new Mock<IEnvironmentModel>();
     var auth = new Mock<IAuthorizationService>();
     env.Setup(a => a.IsConnected).Returns(true);
     env.Setup(a => a.AuthorizationService).Returns(auth.Object);
     auth.Setup(a => a.IsAuthorized(AuthorizationContext.Administrator, null)).Returns(true).Verifiable();
     schedulerViewModel.CurrentEnvironment = env.Object;
     var mockScheduledResourceModel = new Mock<IScheduledResourceModel>();
     mockScheduledResourceModel.Setup(model => model.ScheduledResources).Returns(resources);
     mockScheduledResourceModel.Setup(model => model.Save(It.IsAny<IScheduledResource>(), It.IsAny<string>(), It.IsAny<string>())).Verifiable();
     schedulerViewModel.ScheduledResourceModel = mockScheduledResourceModel.Object;
     schedulerViewModel.SelectedTask = schedulerViewModel.TaskList[0];
     schedulerViewModel.SelectedTask.IsNew = false;
     //------------Execute Test---------------------------
     schedulerViewModel.SaveCommand.Execute(null);
     //------------Assert Results-------------------------
     Assert.IsFalse(schedulerViewModel.GetCredentialsCalled);
     string errorMessage;
     mockScheduledResourceModel.Verify(model => model.Save(It.IsAny<IScheduledResource>(), out errorMessage), Times.Never());
     Assert.AreEqual("Task2", schedulerViewModel.TaskList[0].Name);
     mockPopupController.Verify(c => c.ShowNameChangedConflict(It.IsAny<string>(), It.IsAny<string>()), Times.Once());
 }
        public void SchedulerViewModel_SaveCommand_WithNewNameDiffToOldNameYesDialogResponse_OldNameChanges()
        {
            //------------Setup for test--------------------------
            var resources = new ObservableCollection<IScheduledResource>();
            var scheduledResourceForTest = new ScheduledResource("Task2", SchedulerStatus.Enabled, DateTime.MaxValue, new Mock<IScheduleTrigger>().Object, "TestFlow1") { OldName = "Task1", IsDirty = true };
            scheduledResourceForTest.IsDirty = true;
            resources.Add(scheduledResourceForTest);
            resources.Add(new ScheduledResource("Task3", SchedulerStatus.Enabled, DateTime.MaxValue, new Mock<IScheduleTrigger>().Object, "TestFlow1") { OldName = "Task3", IsDirty = true });
            Mock<IPopupController> mockPopupController = new Mock<IPopupController>();
            mockPopupController.Setup(c => c.ShowNameChangedConflict(It.IsAny<string>(), It.IsAny<string>())).Returns(MessageBoxResult.Yes).Verifiable();
            var schedulerViewModel = new SchedulerViewModelForTest(new Mock<IEventAggregator>().Object, new Mock<DirectoryObjectPickerDialog>().Object, mockPopupController.Object, new TestAsyncWorker());
            var env = new Mock<IEnvironmentModel>();
            var auth = new Mock<IAuthorizationService>();
            env.Setup(a => a.AuthorizationService).Returns(auth.Object);
            env.Setup(a => a.IsConnected).Returns(true);
            auth.Setup(a => a.IsAuthorized(AuthorizationContext.Administrator, null)).Returns(true).Verifiable();
            schedulerViewModel.CurrentEnvironment = env.Object;
            var mockScheduledResourceModel = new Mock<IScheduledResourceModel>();
            mockScheduledResourceModel.Setup(model => model.ScheduledResources).Returns(resources);
            string test;
            mockScheduledResourceModel.Setup(model => model.Save(It.IsAny<IScheduledResource>(), out test)).Returns(true).Verifiable();
            schedulerViewModel.ScheduledResourceModel = mockScheduledResourceModel.Object;
            schedulerViewModel.SelectedTask = schedulerViewModel.TaskList[0];
            schedulerViewModel.ShowError("Error while saving: test error");
            Assert.IsTrue(schedulerViewModel.HasErrors);

            var _errorsChanged = false;
            var _errorChanged = false;
            var _taskListChanged = false;


            schedulerViewModel.PropertyChanged += delegate(object sender, PropertyChangedEventArgs args)
            {
                switch(args.PropertyName)
                {
                    case "Errors":
                        _errorsChanged = true;
                        break;
                    case "Error":
                        _errorChanged = true;
                        break;
                    case "TaskList":
                        _taskListChanged = true;
                        break;
                }
            };

            //------------Execute Test---------------------------
            schedulerViewModel.SaveCommand.Execute(null);
            //------------Assert Results-------------------------
            Assert.IsTrue(_errorsChanged);
            Assert.IsTrue(_errorChanged);
            Assert.IsTrue(_taskListChanged);
            Assert.IsFalse(schedulerViewModel.HasErrors);
            Assert.IsTrue(schedulerViewModel.GetCredentialsCalled);
            string errorMessage;
            mockScheduledResourceModel.Verify(model => model.Save(It.IsAny<IScheduledResource>(), out errorMessage), Times.Once());
            Assert.AreEqual("Task2", schedulerViewModel.TaskList[0].Name);
            Assert.AreEqual("Task2", schedulerViewModel.TaskList[0].OldName);
            mockPopupController.Verify(c => c.ShowNameChangedConflict(It.IsAny<string>(), It.IsAny<string>()), Times.Once());
        }
        public void SchedulerViewModel_SaveCommand_AuthorizationFails()
        {
            //------------Setup for test--------------------------
            var resources = new ObservableCollection<IScheduledResource>();
            var scheduledResourceForTest = new ScheduledResource("Task2", SchedulerStatus.Enabled, DateTime.MaxValue, new Mock<IScheduleTrigger>().Object, "TestFlow1") { OldName = "New Task 1", IsDirty = true };
            scheduledResourceForTest.IsDirty = true;
            resources.Add(scheduledResourceForTest);
            resources.Add(new ScheduledResource("Task3", SchedulerStatus.Enabled, DateTime.MaxValue, new Mock<IScheduleTrigger>().Object, "TestFlow1") { OldName = "Task3", IsDirty = true });
            Mock<IPopupController> mockPopupController = new Mock<IPopupController>();
            mockPopupController.Setup(c => c.ShowNameChangedConflict(It.IsAny<string>(), It.IsAny<string>())).Returns(MessageBoxResult.Yes).Verifiable();
            var schedulerViewModel = new SchedulerViewModelForTest(new Mock<IEventAggregator>().Object, new Mock<DirectoryObjectPickerDialog>().Object, mockPopupController.Object, new TestAsyncWorker());
            var env = new Mock<IEnvironmentModel>();
            var auth = new Mock<IAuthorizationService>();
            env.Setup(a => a.IsConnected).Returns(true);
            env.Setup(a => a.AuthorizationService).Returns(auth.Object);
            auth.Setup(a => a.IsAuthorized(AuthorizationContext.Administrator, null)).Returns(false).Verifiable();
            schedulerViewModel.CurrentEnvironment = env.Object;
            var mockScheduledResourceModel = new Mock<IScheduledResourceModel>();
            mockScheduledResourceModel.Setup(model => model.ScheduledResources).Returns(resources);

            schedulerViewModel.ScheduledResourceModel = mockScheduledResourceModel.Object;
            schedulerViewModel.SelectedTask = schedulerViewModel.TaskList[0];
            //------------Execute Test---------------------------
            schedulerViewModel.SaveCommand.Execute(null);
            //------------Assert Results-------------------------
            auth.Verify(a => a.IsAuthorized(AuthorizationContext.Administrator, null));
            Assert.AreEqual(schedulerViewModel.Errors.FetchErrors().Count, 1);
            Assert.AreEqual(@"Error while saving: You don't have permission to schedule on this server.
You need Administrator permission.", schedulerViewModel.Errors.FetchErrors().FirstOrDefault());
        }
        public void SchedulerViewModel_EditTrigger_ShouldEditTheTrigger()
        {
            //------------Setup for test--------------------------

            var schedulerViewModel = new SchedulerViewModelForTest();
            var resources = new ObservableCollection<IScheduledResource>();
            Mock<IScheduleTrigger> mockScheduleTrigger = new Mock<IScheduleTrigger>();
            mockScheduleTrigger.Setup(c => c.Trigger.Instance).Returns(new DailyTrigger());
            resources.Add(new ScheduledResource("bob", SchedulerStatus.Enabled, DateTime.MaxValue, mockScheduleTrigger.Object, "c") { NumberOfHistoryToKeep = 1 });
            resources.Add(new ScheduledResource("dave", SchedulerStatus.Enabled, DateTime.MaxValue, mockScheduleTrigger.Object, "c"));

            var resourceModel = new Mock<IScheduledResourceModel>();
            resourceModel.Setup(c => c.ScheduledResources).Returns(resources);
            schedulerViewModel.ScheduledResourceModel = resourceModel.Object;
            //------------Execute Test---------------------------
            Assert.AreEqual(2, schedulerViewModel.TaskList.Count);
            schedulerViewModel.SelectedTask = schedulerViewModel.TaskList[0];

            var _triggerTextChange = false;

            schedulerViewModel.PropertyChanged += delegate(object sender, PropertyChangedEventArgs args)
            {
                switch(args.PropertyName)
                {
                    case "TriggerText":
                        _triggerTextChange = true;
                        break;
                }
            };

            schedulerViewModel.EditTriggerCommand.Execute(null);
            //------------Assert Results-------------------------
            Assert.IsTrue(_triggerTextChange);
            Assert.AreEqual("bob", schedulerViewModel.SelectedTask.Name);
            Assert.AreEqual(2013, schedulerViewModel.SelectedTask.NextRunDate.Year);
            Assert.AreEqual(02, schedulerViewModel.SelectedTask.NextRunDate.Hour);
            Assert.AreEqual(21, schedulerViewModel.SelectedTask.NextRunDate.Minute);
            Assert.IsTrue(schedulerViewModel.TriggerText.StartsWith("At"));
            Assert.IsTrue(schedulerViewModel.TriggerText.EndsWith("AM every day"));
        }