public void CancelCommandExecute_ShouldSetTheDialogResultToFalse()
        {
            //Arrange
            var mockDomainClient = new Mock <FakeDomainClient>();

            mockDomainClient.Setup(dc => dc.Query(It.Is <EntityQuery>(query => query.EntityType == typeof(DataItem))))
            .Returns(() => new Entity[]     // a data item
            {
                new DataItem {
                }
            }.AsQueryable());

            //Act
            ReportWizardContext   context   = new ReportWizardContext(mockDomainClient.Object);
            EditDataItemViewModel viewModel = new EditDataItemViewModel(It.IsAny <Guid>(), context);

            EnqueueConditional(() =>  //Wait until DataItemToEditIsPopulated
                               viewModel.DataItemToEdit != null);

            //Assert
            EnqueueCallback(() => //test that executing the save command sets the dialog result to true
            {
                viewModel.CancelCommand.Execute(null);
                Assert.IsTrue(viewModel.DialogResult.Value == false, "CancelCommand has not set the dialog result to false");
            });

            EnqueueTestComplete();
        }
        public void SaveCommandCanExecute_ShouldBeFalseIfLocationOnSystemIsCleared()
        {
            //Arrange
            var mockDomainClient = new Mock <FakeDomainClient>();

            mockDomainClient.Setup(dc => dc.Query(It.Is <EntityQuery>(query => query.EntityType == typeof(DataItem))))
            .Returns(() => new Entity[]     // a data item
            {
                new DataItem {
                    Name             = "ProgrammeBalance",
                    Caption          = "Programme Balance",
                    Description      = "The current programme balance",
                    LocationOnSystem = "Location On System"
                }
            }.AsQueryable());

            //Act
            ReportWizardContext   context   = new ReportWizardContext(mockDomainClient.Object);
            EditDataItemViewModel viewModel = new EditDataItemViewModel(It.IsAny <Guid>(), context);

            EnqueueConditional(() =>  //Wait until DataItemToEditIsPopulated
                               viewModel.DataItemToEdit != null);

            //Assert
            EnqueueCallback(() =>                               //test that clearing the description sets SaveCommand.CanExecute to false
            {
                viewModel.DataItemToEdit.LocationOnSystem = ""; //change the caption to be empty
                Assert.IsTrue(viewModel.SaveCommand.CanExecute(null) == false,
                              "Save Command should not be executable if the location on sytem is cleared");
            });

            EnqueueTestComplete();
        }
        public void SaveCommandCanExecute_ShouldBeTrueIfAllFieldsAreAmendedAndPopulated()
        {
            //Arrange
            var mockDomainClient = new Mock <FakeDomainClient>();

            mockDomainClient.Setup(dc => dc.Query(It.Is <EntityQuery>(query => query.EntityType == typeof(DataItem))))
            .Returns(() => new Entity[]     // a data item
            {
                new DataItem {
                    Name             = "ProgrammeBalance",
                    Caption          = "",
                    Description      = "",
                    LocationOnSystem = ""
                }
            }.AsQueryable());

            //Act
            ReportWizardContext   context   = new ReportWizardContext(mockDomainClient.Object);
            EditDataItemViewModel viewModel = new EditDataItemViewModel(It.IsAny <Guid>(), context);

            EnqueueConditional(() =>  //Wait until DataItemToEditIsPopulated
                               viewModel.DataItemToEdit != null);

            //Assert
            EnqueueCallback(() => //test that populating the fields  sets SaveCommand.CanExecute to true
            {
                viewModel.DataItemToEdit.Caption          = "Programme Balance";
                viewModel.DataItemToEdit.Description      = "The name of the programme";
                viewModel.DataItemToEdit.LocationOnSystem = "Location On System";
                Assert.IsTrue(viewModel.SaveCommand.CanExecute(null) == true,
                              "Save Command should  be executable if all the fields are populated");
            });

            EnqueueTestComplete();
        }
        public void LocationOnSystem_ShouldRaisePropertyChangedEventWhenAltered()
        {
            //Arrange
            bool wasCalled        = false;
            var  mockDomainClient = new Mock <FakeDomainClient>();

            mockDomainClient.Setup(dc => dc.Query(It.Is <EntityQuery>(query => query.EntityType == typeof(DataItem))))
            .Returns(() => new Entity[]     // a data item
            {
                new DataItem {
                    Name             = "ProgrammeBalance",
                    Caption          = "Programme Balance",
                    Description      = "The current programme balance",
                    LocationOnSystem = "Location On System"
                }
            }.AsQueryable());

            //Act
            ReportWizardContext   context   = new ReportWizardContext(mockDomainClient.Object);
            EditDataItemViewModel viewModel = new EditDataItemViewModel(It.IsAny <Guid>(), context);

            EnqueueConditional(() =>  //Wait until DataItemToEditIsPopulated
                               viewModel.DataItemToEdit != null);

            //Assert
            EnqueueCallback(() => //test that amending the location on system triggers the property changed event
            {
                viewModel.DataItemToEdit.PropertyChanged  += (s, e) => { wasCalled = true; };
                viewModel.DataItemToEdit.LocationOnSystem += " Test"; //change the location on system
                Assert.IsTrue(wasCalled, "PropertyChanged event not fired when the location on system was changed");
            });

            EnqueueTestComplete();
        }
        public void Constructor_ShouldAttemptToLoadADataItem()
        {
            //Arrange
            bool loadExecuted = false;

            //I expect that within the constructor, my mock domain client will have its Query method called.
            //I expect that it will be called with an EntityQuery for DataItem objects. When it gets called,
            //I want to set a flag (load executed)to verify that it was called.

            var mockDomainClient = new Mock <FakeDomainClient>();

            mockDomainClient.Setup(dc => dc.Query(It.Is <EntityQuery>(query => query.EntityType == typeof(DataItem))))
            .Callback(() => loadExecuted = true) // used to verify it was called
            .Returns(() => new Entity[]          // a data item
            {
                new DataItem {
                },
            }.AsQueryable());

            //Act
            ReportWizardContext   context   = new ReportWizardContext(mockDomainClient.Object);
            EditDataItemViewModel viewModel = new EditDataItemViewModel(It.IsAny <Guid>(), context);

            //Assert
            Assert.IsTrue(loadExecuted, "The load was not called"); //Test if the method was executed

            EnqueueTestComplete();
        }
        public void LoadDataItemCallBack_CancelCommandShouldBeARelayCommand()
        {
            //Arrange
            var mockDomainClient = new Mock <FakeDomainClient>();

            mockDomainClient.Setup(dc => dc.Query(It.Is <EntityQuery>(query => query.EntityType == typeof(DataItem))))
            .Returns(() => new Entity[]     // a data item
            {
                new DataItem {
                }
            }.AsQueryable());
            //Act
            ReportWizardContext   context   = new ReportWizardContext(mockDomainClient.Object);
            EditDataItemViewModel viewModel = new EditDataItemViewModel(It.IsAny <Guid>(), context);

            //Assert
            EnqueueConditional(() =>  //Wait until CancelCommand has been initialised
                               viewModel.CancelCommand != null);

            EnqueueCallback(() => //test that the cancel command is a relay command
            {
                Assert.IsInstanceOfType(viewModel.CancelCommand,
                                        typeof(RelayCommand), "Cancel Command is not a RelayCommand");
            });
            EnqueueTestComplete();
        }
        public void LoadDataItemCallBack_IfLocationOnSystemNotPopulatedTheSaveCommandIsNotExecutable()
        {
            //Arrange
            var  mockDomainClient = new Mock <FakeDomainClient>();
            bool loadExecuted     = false;

            mockDomainClient.Setup(dc => dc.Query(It.Is <EntityQuery>(query => query.EntityType == typeof(DataItem))))
            .Callback(() => loadExecuted = true) // used to verify it was called
            .Returns(() => new Entity[]          // a data item
            {
                new DataItem {
                    Name             = "ProgrammeBalance",
                    Caption          = "Programme Balance",
                    Description      = "The current programme balance",
                    LocationOnSystem = ""
                }
            }.AsQueryable());

            //Act
            ReportWizardContext   context   = new ReportWizardContext(mockDomainClient.Object);
            EditDataItemViewModel viewModel = new EditDataItemViewModel(It.IsAny <Guid>(), context);

            //Assert
            EnqueueConditional(() =>  //Wait until DataItemToEditIsPopulated
                               viewModel.DataItemToEdit != null);

            EnqueueCallback(() =>
            {
                Assert.IsTrue(viewModel.SaveCommand.CanExecute(null) == false,
                              "Save Command should not be executable if the location on system in empty");
            });
            EnqueueTestComplete();
        }
        public void SaveCommandExecute_ShouldCallSubmitChanges()
        {
            //Arrange

            bool submitExecuted         = false;
            bool loadExecuted           = false;
            var  mockDomainClient       = new Mock <FakeDomainClient>();
            ReportWizardContext context = new ReportWizardContext(mockDomainClient.Object);

            mockDomainClient.Setup(dc => dc.Query(It.Is <EntityQuery>(query => query.EntityType == typeof(DataItem))))
            .Callback(() => loadExecuted = true) // used to verify it was called
            .Returns(() => new Entity[]          // a data item
            {
                new DataItem {
                    Name             = "ProgrammeBalance",
                    Caption          = "Programme Balance",
                    Description      = "The current programme balance",
                    LocationOnSystem = "The location on the system"
                }
            }.AsQueryable());


            EntityChangeSet resultSet = context.EntityContainer.GetChanges();

            //resultSet.GetChangeSetEntries


            mockDomainClient.Setup(dc => dc.Submit(It.Is <EntityChangeSet>(ecs => ecs.ModifiedEntities.Count == 1)))
            //mockDomainClient.Setup(dc => dc.Submit(It.Is<EntityChangeSet>(ecs => ecs.AddedEntities.Count == 0)))
            .Callback(() => submitExecuted = true)
            .Returns(() => new List <ChangeSetEntry>());
            //.Returns(resultSet.GetChangeSetEntries());

            //Act
            EditDataItemViewModel viewModel = new EditDataItemViewModel(It.IsAny <Guid>(), context);


            EnqueueConditional(() =>  //Wait until the load has finished
                               context.IsLoading == false);

            EnqueueCallback(() => //Amend the description of the data item and submit the changes to the database
            {
                context.DataItems.FirstOrDefault(x => x.Name == "ProgrammeBalance").Description = "Amended Description";
                viewModel.SaveCommand.Execute(null);
            });


            EnqueueConditional(() =>  //Wait until the submit has finished
                               context.IsSubmitting == false);

            //Assert
            EnqueueCallback(() =>                                           //test that the method was executed
            {
                Assert.IsTrue(submitExecuted, "The submit was not called"); //Test if the method was executed
                //mockDomainClient.Verify(dc => dc.Submit(It.Is<EntityChangeSet>(ecs => ecs.ModifiedEntities.Count == 1)));
            });

            EnqueueTestComplete();
        }
        public void CancelCommand_ShouldRaisePropertyChangeWhenAltered()
        {
            //Arrange
            var mockDomainClient = new Mock <FakeDomainClient>();

            EditDataItemViewModel viewModel = null;

            var firedEvents = new List <string>();

            mockDomainClient.Setup(dc => dc.Query(It.Is <EntityQuery>(query => query.EntityType == typeof(DataItem))))
            .Returns(() => new Entity[]     // a data item
            {
                new DataItem {
                    Name             = "ProgrammeBalance",
                    Caption          = "",
                    Description      = "",
                    LocationOnSystem = ""
                }
            }.AsQueryable());

            //Act
            ReportWizardContext context = new ReportWizardContext(mockDomainClient.Object);

            viewModel = new EditDataItemViewModel(It.IsAny <Guid>(), context);

            viewModel.PropertyChanged += ((sender, e) => firedEvents.Add(e.PropertyName));

            //Assert
            EnqueueConditional(() =>  //Wait until CancelCommand has been initialised
                               viewModel.CancelCommand != null);

            EnqueueCallback(() =>
            {
                Assert.IsTrue(firedEvents.Contains("CancelCommand"), "Cancel Command did not fire OnPropertyChanged");
            });

            //Assert
            EnqueueTestComplete();
        }
        public void LoadDataItemCallBack_LoadsASingleDataItem()
        {
            //Arrange
            var  mockDomainClient = new Mock <FakeDomainClient>();
            bool loadExecuted     = false;

            mockDomainClient.Setup(dc => dc.Query(It.Is <EntityQuery>(query => query.EntityType == typeof(DataItem))))
            .Callback(() => loadExecuted = true) // used to verify it was called
            .Returns(() => new Entity[]          // a data item
            {
                new DataItem {
                    Name             = "ProgrammeBalance",
                    Caption          = "Programme Balance",
                    Description      = "The current programme balance",
                    LocationOnSystem = "The location on the system"
                }
            }.AsQueryable());

            //Act
            ReportWizardContext   context   = new ReportWizardContext(mockDomainClient.Object);
            EditDataItemViewModel viewModel = new EditDataItemViewModel(It.IsAny <Guid>(), context);



            //Assert
            //Assert.IsTrue(loadExecuted); //Test if the method was executed

            EnqueueConditional(() =>  //Wait until DataItemToEditIsPopulated
                               viewModel.DataItemToEdit != null);


            EnqueueCallback(() => // Did the load return 1 data item
            {
                Assert.IsTrue(context.DataItems.Count() == 1,
                              "The load did not return 1 data item");
                //Assert.AreEqual("ProgrammeBalance", viewModel.DataItemToEdit.Name);
            });
            EnqueueTestComplete();
        }