public void FoodItemAddedToChildrenWhenAddedToRepository()
        {
            var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             DataRepository dataRepository = new DataRepository( configurationMock.Object );

             FoodItemNodeViewModel foodItemNodeViewModel = new FoodItemNodeViewModel( dataRepository, null );

             Int32 originalChildCount = foodItemNodeViewModel.Children.Count;

             FoodItem newFoodItem = new FoodItem( Guid.NewGuid(), "New Food Item", "Some Description", 92 );
             newFoodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( dataRepository.GetAllFoodGroups()[0], 1 ) );
             dataRepository.SaveItem( newFoodItem );
             Assert.AreEqual( originalChildCount + 1, foodItemNodeViewModel.Children.Count );

             foreach (ClickableTreeNodeViewModel node in foodItemNodeViewModel.Children)
             {
            FoodItem foodItem = dataRepository.GetFoodItem( (Guid)node.Parameter );
            Assert.IsNotNull( foodItem );
            Assert.AreEqual( foodItem.Name, node.Name );
             }
        }
        public void FoodItemNodeViewModelRemoveFoodItem()
        {
            var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             DataRepository dataRepository = new DataRepository( configurationMock.Object );

             FoodItemNodeViewModel foodItemNodeViewModel = new FoodItemNodeViewModel( dataRepository, null );

             Int32 originalChildCount = foodItemNodeViewModel.Children.Count;

             FoodItem removedFoodItem = dataRepository.GetAllFoodItems().ElementAt( 0 );
             dataRepository.Remove( removedFoodItem );
             Assert.AreEqual( originalChildCount - 1, foodItemNodeViewModel.Children.Count );

             foreach (ClickableTreeNodeViewModel node in foodItemNodeViewModel.Children)
             {
            FoodItem foodItem = dataRepository.GetFoodItem( (Guid)node.Parameter );
            Assert.IsNotNull( foodItem );
            Assert.AreEqual( foodItem.Name, node.Name );
             }
        }
        public void NonFoodItemNotAddedToChildrenWhenAddedToRepository()
        {
            var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             DataRepository dataRepository = new DataRepository( configurationMock.Object );

             FoodItemNodeViewModel foodItemNodeViewModel = new FoodItemNodeViewModel( dataRepository, null );

             Int32 originalChildCount = foodItemNodeViewModel.Children.Count;

             var newMealType = new MealType( Guid.NewGuid(), "New Meal Type", "Some Description", DateTime.Now, false );
             Assert.IsTrue( newMealType.IsValid );
             dataRepository.SaveItem( newMealType );
             Assert.AreEqual( originalChildCount, foodItemNodeViewModel.Children.Count );

             foreach (ClickableTreeNodeViewModel node in foodItemNodeViewModel.Children)
             {
            FoodItem foodItem = dataRepository.GetFoodItem( (Guid)node.Parameter );
            Assert.IsNotNull( foodItem );
            Assert.AreEqual( foodItem.Name, node.Name );
             }
        }
        public void FoodItemNodeViewModelDefault()
        {
            var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             DataRepository dataRepository = new DataRepository( configurationMock.Object );

             FoodItemNodeViewModel foodItemNodeViewModel = new FoodItemNodeViewModel( dataRepository, null );

             Assert.AreEqual( DisplayStrings.AdminFoodItemsTitle, foodItemNodeViewModel.Name );

             // If the counts are the same, and every child is in the repository, then the data should be fine.
             Assert.AreEqual( dataRepository.GetAllFoodItems().Count, foodItemNodeViewModel.Children.Count );
             foreach (ClickableTreeNodeViewModel node in foodItemNodeViewModel.Children)
             {
            FoodItem foodItem = dataRepository.GetFoodItem( (Guid)node.Parameter );
            Assert.IsNotNull( foodItem );
            Assert.AreEqual( foodItem.Name, node.Name );
             }
        }
        public void FoodItemViewModelName()
        {
            var loggerMock = new Mock<ILoggerFacade>();
             var regionNavigationServiceMock = new Mock<IRegionNavigationService>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();

             var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             var dataRepository = new DataRepository( configurationMock.Object );

             PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();
             FoodItem testFoodItem = dataRepository.GetFoodItem( FullTestData.CheeseBurgerID );
             FoodItemViewModel testFoodItemViewModel =
            new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );

             // Tests:
             //   o Name is the same as the object from the repository
             //   o Changing the name causes appropriate properties to be marked as changed
             NavigationContext navigationContext = new NavigationContext(
            regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + testFoodItem.ID.ToString(), UriKind.Relative ) );
             testFoodItemViewModel.OnNavigatedTo( navigationContext );
             testFoodItemViewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             Assert.AreEqual( testFoodItem.Name, testFoodItemViewModel.Name );

             testFoodItemViewModel.Name = "Some other name";
             Assert.AreEqual( "Some other name", testFoodItemViewModel.Name );

             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Name" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
        }
        public void FoodItemViewModelIsDirty()
        {
            var loggerMock = new Mock<ILoggerFacade>();
             var regionNavigationServiceMock = new Mock<IRegionNavigationService>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();

             var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             var dataRepository = new DataRepository( configurationMock.Object );

             // Existing Food Item
             //   Starts clean
             //   Change Name - dirty
             //   Undo - clean
             //   Change Description - dirty
             //   Undo - clean
             //   Change Calories - dirty
             //   Undo - clean
             //   Change Food Group Serving - dirty
             //   Undo - clean
             //   Add food group - dirty
             //   undo - clean
             //
             FoodItem cheeseBurger = dataRepository.GetFoodItem( FullTestData.CheeseBurgerID );
             Assert.IsNotNull( cheeseBurger );
             String originalName = cheeseBurger.Name;
             String originalDescription = cheeseBurger.Description;

             FoodItemViewModel cheeseBurgerViewModel =
            new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );
             NavigationContext navigationContext = new NavigationContext(
            regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + cheeseBurger.ID.ToString(), UriKind.Relative ) );
             cheeseBurgerViewModel.OnNavigatedTo( navigationContext );
             Assert.IsFalse( cheeseBurgerViewModel.IsDirty );

             cheeseBurgerViewModel.Name += "Modified";
             Assert.IsTrue( cheeseBurgerViewModel.IsDirty );

             cheeseBurgerViewModel.Name = originalName;
             Assert.IsFalse( cheeseBurgerViewModel.IsDirty );

             cheeseBurgerViewModel.Description += "Different";
             Assert.IsTrue( cheeseBurgerViewModel.IsDirty );

             cheeseBurgerViewModel.Description = originalDescription;
             Assert.IsFalse( cheeseBurgerViewModel.IsDirty );

             cheeseBurgerViewModel.Name = null;
             Assert.IsTrue( cheeseBurgerViewModel.IsDirty );

             cheeseBurgerViewModel.Name = originalName;
             Assert.IsFalse( cheeseBurgerViewModel.IsDirty );

             cheeseBurgerViewModel.CaloriesPerServing += 90;
             Assert.IsTrue( cheeseBurgerViewModel.IsDirty );

             cheeseBurgerViewModel.CaloriesPerServing -= 90;
             Assert.IsFalse( cheeseBurgerViewModel.IsDirty );

             cheeseBurgerViewModel.FoodGroupsPerServing[0].Quantity += 1;
             Assert.IsTrue( cheeseBurgerViewModel.IsDirty );

             cheeseBurgerViewModel.FoodGroupsPerServing[0].Quantity -= 1;
             Assert.IsFalse( cheeseBurgerViewModel.IsDirty );

             ServingViewModel<FoodGroup> serving = cheeseBurgerViewModel.FoodGroupsPerServing[0];
             cheeseBurgerViewModel.FoodGroupsPerServing.Remove( serving );
             Assert.IsTrue( cheeseBurgerViewModel.IsDirty );

             // Create an exact duplicate of the servings.
             ServingViewModel<FoodGroup> copyOfServing = new ServingViewModel<FoodGroup>( serving.Entity, serving.Quantity );

             cheeseBurgerViewModel.FoodGroupsPerServing.Add( copyOfServing );
             Assert.IsFalse( cheeseBurgerViewModel.IsDirty );

             serving = new ServingViewModel<FoodGroup>(
            dataRepository.FindFoodGroup( fg => fg.Name == "Water" ), 1 );
             cheeseBurgerViewModel.FoodGroupsPerServing.Add( serving );
             Assert.IsTrue( cheeseBurgerViewModel.IsDirty );

             cheeseBurgerViewModel.FoodGroupsPerServing.Remove( serving );
             Assert.IsFalse( cheeseBurgerViewModel.IsDirty );
        }
        public void FoodItemViewModelFoodGroupServings()
        {
            var loggerMock = new Mock<ILoggerFacade>();
             var regionNavigationServiceMock = new Mock<IRegionNavigationService>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();

             var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             var dataRepository = new DataRepository( configurationMock.Object );

             PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();

             // Navigate to a known food group mealType and make sure the food group servings makes sense.
             // NOTE: No need to get too fancy, the model tests makes sure the data loaded properly.
             FoodItem foodItem = dataRepository.GetFoodItem( FullTestData.CheeseBurgerID );
             FoodItemViewModel foodItemViewModel =
            new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );
             NavigationContext navigationContext = new NavigationContext(
            regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + foodItem.ID.ToString(), UriKind.Relative ) );
             foodItemViewModel.OnNavigatedTo( navigationContext );
             foodItemViewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             Assert.AreEqual( foodItem.FoodGroupsPerServing.Count, foodItemViewModel.FoodGroupsPerServing.Count );
             Assert.IsFalse( foodItemViewModel.IsDirty );
             Assert.IsTrue( foodItemViewModel.IsValid );

             // Add a food group
             ServingViewModel<FoodGroup> testFoodServings = new ServingViewModel<FoodGroup>(
            dataRepository.GetFoodGroup( FullTestData.JunkFoodID ), 1 );
             foodItemViewModel.FoodGroupsPerServing.Add( testFoodServings );

             Assert.IsTrue( foodItemViewModel.IsDirty );
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );

             foodItemViewModel.FoodGroupsPerServing.Remove( testFoodServings );
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsFalse( foodItemViewModel.IsDirty );
             propertyChangedHandler.Reset();

             // Delete a food group
             testFoodServings = foodItemViewModel.FoodGroupsPerServing.ElementAt( 0 );
             foodItemViewModel.FoodGroupsPerServing.Remove( testFoodServings );
             Assert.IsTrue( foodItemViewModel.IsDirty );
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );

             foodItemViewModel.FoodGroupsPerServing.Add( testFoodServings );
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsFalse( foodItemViewModel.IsDirty );
             propertyChangedHandler.Reset();

             // Modify a food group
             foodItemViewModel.FoodGroupsPerServing[0].Quantity += 1;
             Assert.AreEqual( foodItem.FoodGroupsPerServing.Count, foodItemViewModel.FoodGroupsPerServing.Count );
             Serving<FoodGroup> modelServing = foodItem.FoodGroupsPerServing.Find( f => f.Entity.ID == foodItemViewModel.FoodGroupsPerServing[0].Entity.ID );
             Assert.IsNotNull( modelServing );
             Assert.IsTrue( foodItemViewModel.IsDirty );
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );

             foodItemViewModel.FoodGroupsPerServing[0].Quantity -= 1;
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsFalse( foodItemViewModel.IsDirty );
             propertyChangedHandler.Reset();

             // Make a serving invalid.
             foodItemViewModel.FoodGroupsPerServing[0].Quantity *= -1;
             Assert.AreEqual( foodItem.FoodGroupsPerServing.Count, foodItemViewModel.FoodGroupsPerServing.Count );
             Assert.IsTrue( foodItemViewModel.IsDirty );
             Assert.IsFalse( foodItemViewModel.IsValid );
             Assert.AreEqual( Messages.Error_No_Quantity, foodItemViewModel.Error );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );

             foodItemViewModel.FoodGroupsPerServing[0].Quantity *= -1;
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsFalse( foodItemViewModel.IsDirty );
             propertyChangedHandler.Reset();

             // Remove all food groups
             foodItemViewModel.FoodGroupsPerServing.Clear();
             Assert.IsTrue( foodItemViewModel.IsDirty );
             Assert.IsFalse( foodItemViewModel.IsValid );
             Assert.AreEqual( Messages.Error_No_FoodGroups, foodItemViewModel.Error );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );
        }
        public void FoodItemViewModelCaloriesPerServing()
        {
            var loggerMock = new Mock<ILoggerFacade>();
             var regionNavigationServiceMock = new Mock<IRegionNavigationService>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();

             var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             var dataRepository = new DataRepository( configurationMock.Object );

             PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();

             // Construct a known food group mealType and make sure the food group servings makes sense.
             FoodItem foodItem = dataRepository.GetFoodItem( FullTestData.CheeseBurgerID );
             FoodItemViewModel foodItemViewModel =
            new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );
             NavigationContext navigationContext = new NavigationContext(
            regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + foodItem.ID.ToString(), UriKind.Relative ) );
             foodItemViewModel.OnNavigatedTo( navigationContext );
             foodItemViewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             Assert.AreEqual( foodItem.CaloriesPerServing, foodItemViewModel.CaloriesPerServing );
             Assert.IsFalse( foodItemViewModel.IsDirty );
             Assert.IsTrue( foodItemViewModel.IsValid );

             // Change the calories
             foodItemViewModel.CaloriesPerServing = 700;
             Assert.IsTrue( foodItemViewModel.IsDirty );
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "CaloriesPerServing" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );

             foodItemViewModel.CaloriesPerServing = 650;
             Assert.IsFalse( foodItemViewModel.IsDirty );
             Assert.IsTrue( foodItemViewModel.IsValid );
             propertyChangedHandler.Reset();

             // Make the calories invalid
             foodItemViewModel.CaloriesPerServing = -700;
             Assert.IsTrue( foodItemViewModel.IsDirty );
             Assert.IsFalse( foodItemViewModel.IsValid );
             Assert.AreEqual( Messages.Error_Negative_Calories, foodItemViewModel.Error );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "CaloriesPerServing" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );
        }