public void InitializeTest()
        {
            // Arrange.
            var model = Mock.Create<ProcessDataTriggerEdit>(Behavior.Loose);
            var parentViewModel = Mock.Create<IProcessDataTriggersViewModel>(Behavior.Loose);
            var parentProcess = Mock.Create<ProcessEdit>(Constructor.Mocked, Behavior.Strict);
            var parentWindow = Mock.Create<ITopLevelWindow>(Behavior.Strict);
            var unitOfWork = Mock.Create<ProcessEditRetriever>(Constructor.Mocked, Behavior.Strict);
            var storedProcedureNames = new List<string>();
            Mock.Arrange(() => unitOfWork.RuntimeDatabaseProcedures).Returns(storedProcedureNames);

            Mock.Arrange(() => parentViewModel.Model).Returns(parentProcess);
            Mock.Arrange(() => parentViewModel.Parent).Returns(parentWindow);
            Mock.Arrange(() => parentViewModel.UnitOfWork).Returns(unitOfWork);

            var vm = new StoredProcedureDataTriggerViewModel();

            // Act.
            vm.Initialize(model, parentViewModel);

            // Assert.
            Assert.AreSame(model, vm.Model);
            Assert.AreSame(parentViewModel, vm.ParentViewModel);
            Assert.AreSame(parentProcess, vm.ParentProcess);
            Assert.AreSame(parentWindow, vm.ParentWindow);
            Assert.AreSame(storedProcedureNames, vm.RuntimeDatabaseProcedures);

            // Argument validation.
            TestsHelper.VerifyThrow<ArgumentNullException>(() => vm.Initialize(null, parentViewModel));
            TestsHelper.VerifyThrow<ArgumentNullException>(() => vm.Initialize(model, null));
        }
        public void CanUpdateParametersTest()
        {
            // Arrange.
            var model = Mock.Create<ProcessDataTriggerEdit>(Behavior.Loose);
            var parentViewModel = Mock.Create<IProcessDataTriggersViewModel>(Behavior.Loose);

            // Assert - when model is null, cannot execute UpdateParametersCommand.
            var vm = new StoredProcedureDataTriggerViewModel();
            Assert.IsFalse(vm.UpdateParametersCommand.CanExecute(null));

            // Assert - when StoredProcedureName is empty, cannot execute UpdateParametersCommand.
            vm.Initialize(model, parentViewModel);
            Assert.IsFalse(vm.UpdateParametersCommand.CanExecute(null));

            // Assert - when StoredProcedureName is not empty, can execute UpdateParametersCommand.
            Mock.Arrange(() => model.StoredProcedureName).Returns("Test");
            Assert.IsTrue(vm.UpdateParametersCommand.CanExecute(null));
        }
        public void ExecuteUpdateParametersTest()
        {
            // Arrange.
            const string ProcedureName = "TestProcedure";

            var model = ProcessDataTriggerEdit.NewProcessDataTrigger();
            var oldParam1 = DataTriggerStoredProcedureParameterEdit.NewParameter();
            var oldParam2 = DataTriggerStoredProcedureParameterEdit.NewParameter();
            model.StoredProcedureParameters.Add(oldParam1);
            model.StoredProcedureParameters.Add(oldParam2);
            model.StoredProcedureName = ProcedureName;

            var parentViewModel = Mock.Create<IProcessDataTriggersViewModel>(Behavior.Loose);

            var vm = new StoredProcedureDataTriggerViewModel();
            vm.Initialize(model, parentViewModel);

            var newParam1 = DataTriggerStoredProcedureParameterEdit.NewParameter();
            var newParam2 = DataTriggerStoredProcedureParameterEdit.NewParameter();
            var newParam3 = DataTriggerStoredProcedureParameterEdit.NewParameter();
            var retriever = Mock.Create<StoredProcedureParametersRetriever>(Behavior.Loose);
            Mock.Arrange(() => retriever.Parameters).Returns(new[] { newParam1, newParam2, newParam3 });

            Mock.Arrange(() => StoredProcedureParametersRetriever.GetParametersAsync(ProcedureName)).Returns(TaskEx.FromResult(retriever));

            // Act.
            vm.UpdateParametersCommand.Execute(null);

            // Assert.
            Assert.AreEqual(3, model.StoredProcedureParameters.Count);
            Assert.IsTrue(model.StoredProcedureParameters.Contains(newParam1));
            Assert.IsTrue(model.StoredProcedureParameters.Contains(newParam2));
            Assert.IsTrue(model.StoredProcedureParameters.Contains(newParam3));
        }
        public void UpdateStoredProceduresCommand_WhenExecuted_UpdatesRuntimeDatabaseProcedures()
        {
            // Arrange.
            var model = ProcessDataTriggerEdit.NewProcessDataTrigger();
            var parentViewModel = Mock.Create<IProcessDataTriggersViewModel>(Behavior.Loose);

            var vm = new StoredProcedureDataTriggerViewModel();
            vm.Initialize(model, parentViewModel);

            var storedProcedures = new List<string> { "Proc1", "Proc2" };
            var retriever = Mock.Create<StoredProceduresRetriever>(Behavior.Loose);
            Mock.Arrange(retriever, x => x.StoredProcedures).Returns(storedProcedures);
            Mock.Arrange(() => StoredProceduresRetriever.GetProceduresAsync()).Returns(TaskEx.FromResult(retriever));

            var changedProperties = new HashSet<string>();
            vm.PropertyChanged += (sender, args) => changedProperties.Add(args.PropertyName);

            // Act.
            vm.UpdateStoredProceduresCommand.Execute(null);

            // Assert.
            Assert.IsTrue(
                changedProperties.Contains(ReflectionHelper.GetProperty<StoredProcedureDataTriggerViewModel>(x => x.RuntimeDatabaseProcedures).Name));
            Assert.AreSame(storedProcedures, vm.RuntimeDatabaseProcedures);
        }
        public void RemoveParameterCommand_WhenExecuted_RemovesParameter()
        {
            // Arrange.
            var model = ProcessDataTriggerEdit.NewProcessDataTrigger();
            var parameter1 = DataTriggerStoredProcedureParameterEdit.NewParameter();
            model.StoredProcedureParameters.Add(parameter1);
            var parameter2 = DataTriggerStoredProcedureParameterEdit.NewParameter();
            model.StoredProcedureParameters.Add(parameter2);
            var parentViewModel = Mock.Create<IProcessDataTriggersViewModel>(Behavior.Loose);

            var vm = new StoredProcedureDataTriggerViewModel();
            vm.Initialize(model, parentViewModel);

            // Act.
            vm.RemoveParameterCommand.Execute(parameter2);

            // Assert.
            Assert.AreEqual(1, model.StoredProcedureParameters.Count);
            Assert.IsFalse(model.StoredProcedureParameters.Contains(parameter2));
        }
        public void AddParameterCommand_WhenExecuted_AddsNewParameter()
        {
            // Arrange.
            var model = ProcessDataTriggerEdit.NewProcessDataTrigger();
            var parentViewModel = Mock.Create<IProcessDataTriggersViewModel>(Behavior.Loose);

            var vm = new StoredProcedureDataTriggerViewModel();
            vm.Initialize(model, parentViewModel);

            // Act.
            vm.AddParameterCommand.Execute(null);

            // Assert.
            Assert.AreEqual(1, model.StoredProcedureParameters.Count);
        }
        public void WhenProcedureNameChanges_UpdateParametersCommand_RaisesCanExecuteChanged()
        {
            // Arrange.
            var model = ProcessDataTriggerEdit.NewProcessDataTrigger();
            var parentViewModel = Mock.Create<IProcessDataTriggersViewModel>(Behavior.Loose);

            var vm = new StoredProcedureDataTriggerViewModel();
            vm.Initialize(model, parentViewModel);

            bool? canExecute = null;
            vm.UpdateParametersCommand.CanExecuteChanged += (sender, args) => canExecute = vm.UpdateParametersCommand.CanExecute(null);

            // Act.
            model.StoredProcedureName = "TestProcedure";

            // Assert.
            Assert.AreEqual(true, canExecute);
        }
        public void EditParameterValueCommandTest()
        {
            // Arrange.
            var model = ProcessDataTriggerEdit.NewProcessDataTrigger();
            var parentViewModel = Mock.Create<IProcessDataTriggersViewModel>(Behavior.Loose);
            var parentProcess = Mock.Create<ProcessEdit>(Constructor.Mocked, Behavior.Strict);
            Mock.Arrange(() => parentViewModel.Model).Returns(parentProcess);
            var parentWindow = Mock.Create<ITopLevelWindow>(Behavior.Strict);
            Mock.Arrange(() => parentViewModel.Parent).Returns(parentWindow);

            var expressionDesinger = Mock.Create<IStoredProcedureParameterExpressionDesignerViewModel>();
            var vm = new StoredProcedureDataTriggerViewModel { ExpressionDesigner = expressionDesinger };

            vm.Initialize(model, parentViewModel);

            // Act / Assert.
            Assert.IsFalse(vm.EditParameterValueCommand.CanExecute(null));

            var parameter = DataTriggerStoredProcedureParameterEdit.NewParameter();

            Assert.IsTrue(vm.EditParameterValueCommand.CanExecute(parameter));
            vm.EditParameterValueCommand.Execute(parameter);

            Mock.Assert(() => expressionDesinger.EditExpression(parentWindow, parentProcess, parameter), Occurs.Once());
        }
 /// <summary>
 /// Handles the <see cref="INotifyPropertyChanged.PropertyChanged" /> event.
 /// </summary>
 /// <param name="viewModel">The view model.</param>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="PropertyChangedEventArgs"/> instance containing the event data.</param>
 private static void OnModelPropertyChanged(StoredProcedureDataTriggerViewModel viewModel, object sender, PropertyChangedEventArgs e)
 {
     if (e.PropertyName == ProcessDataTriggerEdit.StoredProcedureNameProperty.Name)
     {
         viewModel.RaisePropertyChanged(() => viewModel.StoredProcedureName);
         ((IActionCommand)viewModel.UpdateParametersCommand).RaiseCanExecuteChanged();
     }
 }