public void InitTest()
        {
            // Arrange.
            var model = new ProcessReportEditStub();

            model.SecurityConfigurations.Add(new ProcessReportSecurityConfigurationEditStub(111));
            model.SecurityConfigurations.Add(new ProcessReportSecurityConfigurationEditStub(222));

            var parent = Mock.Create<IProcessReportsViewModel>();

            var securityConfigurationViewModelFactory = new ExportFactory<IReportSecurityConfigurationViewModel>(
                () =>
                    {
                        var configViewModel = Mock.Create<IReportSecurityConfigurationViewModel>();
                        Mock.Arrange(() => configViewModel.Init(Arg.IsAny<ProcessReportSecurityConfigurationEdit>()))
                            .DoInstead<ProcessReportSecurityConfigurationEdit>(config => Mock.Arrange(() => configViewModel.Model).Returns(config));

                        return Tuple.Create<IReportSecurityConfigurationViewModel, Action>(configViewModel, () => { });
                    });

            var vm = new ProcessReportViewModel
                         {
                             SecurityConfigurationViewModelFactory = securityConfigurationViewModelFactory
                         };

            // Act.
            vm.Init(model, parent);

            // Assert.
            Assert.AreSame(model, vm.Model);
            Assert.AreSame(parent, vm.Parent);
            Assert.AreEqual(2, vm.SecurityConfigurations.Count);
            Assert.IsTrue(new[] { 111, 222 }.SequenceEqual(vm.SecurityConfigurations.Select(x => x.Model.Id)));
        }
        public void AddSecurityConfigurationTest()
        {
            // Arrange.
            var model = new ProcessReportEditStub();
            var parent = Mock.Create<IProcessReportsViewModel>();

            var securityConfigurationViewModelFactory = new ExportFactory<IReportSecurityConfigurationViewModel>(
                () =>
                    {
                        var configViewModel = Mock.Create<IReportSecurityConfigurationViewModel>();
                        Mock.Arrange(() => configViewModel.Init(Arg.IsAny<ProcessReportSecurityConfigurationEdit>()))
                            .DoInstead<ProcessReportSecurityConfigurationEdit>(config => Mock.Arrange(() => configViewModel.Model).Returns(config));

                        return Tuple.Create<IReportSecurityConfigurationViewModel, Action>(configViewModel, () => { });
                    });

            var vm = new ProcessReportViewModel
            {
                SecurityConfigurationViewModelFactory = securityConfigurationViewModelFactory
            };

            vm.Init(model, parent);

            // Act.
            var configuration1 = vm.AddSecurityConfiguration(1, 2, "Test");

            // Assert.
            Assert.AreEqual(1, vm.SecurityConfigurations.Count);
            Assert.IsTrue(vm.SecurityConfigurations.Contains(configuration1));
            Assert.AreEqual(1, model.SecurityConfigurations.Count);
            Assert.IsTrue(model.SecurityConfigurations.Contains(configuration1.Model));

            // Act - try to add an existing configuration.
            var configuration2 = vm.AddSecurityConfiguration(1, 2, "Test");

            // Assert - existing configuration is returned.
            Assert.AreEqual(1, vm.SecurityConfigurations.Count);
            Assert.AreEqual(1, model.SecurityConfigurations.Count);
            Assert.AreSame(configuration1, configuration2);
        }
        public void WhenParentIsNotEnabledCannotSelectFile()
        {
            var guid1 = new Guid("67286609-6B5F-4764-B18D-DF61EF574D5A");
            var report = DataPortal.CreateChild<ProcessReportEdit>();
            report.Guid = guid1;

            var parent = Mock.Create<IProcessReportsViewModel>();
            Mock.Arrange(() => parent.Enabled).Returns(false);

            var securityConfigurationViewModelFactory = new ExportFactory<IReportSecurityConfigurationViewModel>(
                () =>
                    {
                        var configViewModel = Mock.Create<IReportSecurityConfigurationViewModel>();
                        Mock.Arrange(() => configViewModel.Init(Arg.IsAny<ProcessReportSecurityConfigurationEdit>()))
                            .DoInstead<ProcessReportSecurityConfigurationEdit>(config => Mock.Arrange(() => configViewModel.Model).Returns(config));

                        return Tuple.Create<IReportSecurityConfigurationViewModel, Action>(configViewModel, () => { });
                    });

            var vm = new ProcessReportViewModel { SecurityConfigurationViewModelFactory = securityConfigurationViewModelFactory };

            vm.Init(report, parent);

            Assert.IsFalse(vm.SelectFileCommand.CanExecute(null));
        }
        public void SelectFileCommandSelectsReportFile()
        {
            const string oldReportFileName = "oldReportFileName";
            const string reportFileName = "reportFileName";
            var guid1 = new Guid("67286609-6B5F-4764-B18D-DF61EF574D5A");
            var guid2 = new Guid("BE649638-FC93-4019-B97E-62FE6BEB5EBD");

            var report = DataPortal.CreateChild<ProcessReportEdit>();

            report.Guid = guid1;

            report = DataPortal.CreateChild<ProcessReportEdit>();
            report.Guid = guid2;

            var selectFileViewModel = Mock.Create<ISelectReportFileViewModel>(Behavior.Loose);
            Mock.Arrange(() => selectFileViewModel.SelectedFile).Returns(reportFileName);

            var parent = Mock.Create<IProcessReportsViewModel>(Behavior.Loose);
            Mock.Arrange(() => parent.Enabled).Returns(true);

            var securityConfigurationViewModelFactory = new ExportFactory<IReportSecurityConfigurationViewModel>(
                () =>
                    {
                        var configViewModel = Mock.Create<IReportSecurityConfigurationViewModel>(Behavior.Loose);
                        Mock.Arrange(() => configViewModel.Init(Arg.IsAny<ProcessReportSecurityConfigurationEdit>()))
                            .DoInstead<ProcessReportSecurityConfigurationEdit>(config => Mock.Arrange(() => configViewModel.Model).Returns(config));

                        return Tuple.Create<IReportSecurityConfigurationViewModel, Action>(configViewModel, () => { });
                    });

            var vm = new ProcessReportViewModel
                         {
                             SecurityConfigurationViewModelFactory = securityConfigurationViewModelFactory
                         };

            vm.Init(report, parent);

            var popupBuilder = Mock.Create<PopupBuilder>(Behavior.CallOriginal);

            Mock.Arrange(() => popupBuilder.Show(Arg.IsAny<Action>(), Arg.IsAny<Func<bool>>())).DoInstead<Action, Func<bool>>((onOk, onCancel) => onOk());

            Mock.Arrange(() => popupBuilder.SetCustomViewModel(Arg.IsAny<INotificationViewModel>())).Returns(popupBuilder);

            var popupFactory = Mock.Create<PopupFactory>(Behavior.Loose);

            Mock.Arrange(() => popupFactory.Popup()).Returns(popupBuilder);

            vm.SelectReportFileViewModelFactory = new ExportFactory<ISelectReportFileViewModel>(() => new Tuple<ISelectReportFileViewModel, Action>(selectFileViewModel, () => { }));

            vm.PopupFactory = new Lazy<PopupFactory>(() => popupFactory);

            vm.Model.FileName = oldReportFileName;

            vm.SelectFileCommand.Execute(null);

            Assert.AreEqual(reportFileName, vm.Model.FileName);
            Assert.IsTrue(vm.SelectFileCommand.CanExecute(null));
            Mock.AssertSet(() => selectFileViewModel.SelectedFile = oldReportFileName);
            Mock.Assert(() => selectFileViewModel.Refresh(), Occurs.Once());
        }
        public void RemoveSecurityConfigurationTest()
        {
            // Arrange.
            var model = new ProcessReportEditStub();

            model.SecurityConfigurations.Add(new ProcessReportSecurityConfigurationEditStub(111));
            model.SecurityConfigurations.Add(new ProcessReportSecurityConfigurationEditStub(222));

            var parent = Mock.Create<IProcessReportsViewModel>();

            var securityConfigurationViewModelFactory = new ExportFactory<IReportSecurityConfigurationViewModel>(
                () =>
                    {
                        var configViewModel = Mock.Create<IReportSecurityConfigurationViewModel>();
                        Mock.Arrange(() => configViewModel.Init(Arg.IsAny<ProcessReportSecurityConfigurationEdit>()))
                            .DoInstead<ProcessReportSecurityConfigurationEdit>(config => Mock.Arrange(() => configViewModel.Model).Returns(config));

                        return Tuple.Create<IReportSecurityConfigurationViewModel, Action>(configViewModel, () => { });
                    });

            var vm = new ProcessReportViewModel
                         {
                             SecurityConfigurationViewModelFactory = securityConfigurationViewModelFactory
                         };

            vm.Init(model, parent);

            // Act.
            var configuration = vm.SecurityConfigurations[0];
            vm.RemoveSecurityConfiguration(configuration);

            // Assert.
            Assert.AreEqual(1, vm.SecurityConfigurations.Count);
            Assert.IsFalse(vm.SecurityConfigurations.Contains(configuration));
            Assert.AreEqual(1, model.SecurityConfigurations.Count);
            Assert.IsFalse(model.SecurityConfigurations.Contains(configuration.Model));
        }