Exemple #1
0
        public void TestMealIdReturnsDesignationPassedOnConstructorAsMealID()
        {
            string designation = "Stone Soup";

            MealType type = MealType.ValueOf(ExistingMealTypesService.ExistingMealTypes[0]);

            List <Ingredient> ingredients = new List <Ingredient>();

            ingredients.Add(Ingredient.ValueOf(ExistingIngredientsService.ExistingIngredients[0]));


            List <Descriptor> descriptors = new List <Descriptor>();

            descriptors.Add(Descriptor.ValueOf("Calorie", 50, "cal"));


            List <Allergen> allergens = new List <Allergen>();

            allergens.Add(Allergen.ValueOf(ExistingAllergensService.ExistingAllergens[0]));

            Meal meal = new Meal(designation, type, ingredients, descriptors, allergens);

            string mealID = meal.Id().Id;

            Assert.Equal(designation, mealID);
        }
Exemple #2
0
        public void TestMealDescriptorsReturnsCopyOfDescriptorsPassedOnConstructor()
        {
            string designation = "Stone Soup";

            MealType type = MealType.ValueOf(ExistingMealTypesService.ExistingMealTypes[0]);

            List <Ingredient> ingredients = new List <Ingredient>();

            ingredients.Add(Ingredient.ValueOf(ExistingIngredientsService.ExistingIngredients[0]));


            List <Descriptor> descriptors = new List <Descriptor>();

            descriptors.Add(Descriptor.ValueOf("Calorie", 50, "cal"));


            List <Allergen> allergens = new List <Allergen>();

            allergens.Add(Allergen.ValueOf(ExistingAllergensService.ExistingAllergens[0]));

            Meal meal = new Meal(designation, type, ingredients, descriptors, allergens);

            List <Descriptor> mealDescriptors = meal.Descriptors;

            // now lets add a new descriptor to returned list

            mealDescriptors.Add(Descriptor.ValueOf("Salt", 50, "g"));

            List <Descriptor> expectedDescriptors = meal.Descriptors;

            Assert.NotEqual(expectedDescriptors, mealDescriptors);
        }
Exemple #3
0
        public void MealWithDuplicatedAllergensListElementsThrowsArgumentException()
        {
            string designation = "Stone Soup";

            MealType type = MealType.ValueOf(ExistingMealTypesService.ExistingMealTypes[0]);

            List <Ingredient> ingredients = new List <Ingredient>();

            ingredients.Add(Ingredient.ValueOf(ExistingIngredientsService.ExistingIngredients[0]));


            List <Descriptor> descriptors = new List <Descriptor>();

            descriptors.Add(Descriptor.ValueOf("Calorie", 50, "cal"));


            List <Allergen> allergens = new List <Allergen>();

            allergens.Add(Allergen.ValueOf(ExistingAllergensService.ExistingAllergens[0]));

            allergens.Add(Allergen.ValueOf(ExistingAllergensService.ExistingAllergens[0]));


            Action newMeal = () => new Meal(designation, type, ingredients, descriptors, allergens);

            Assert.Throws <ArgumentException>(newMeal);
        }
Exemple #4
0
        public void MealWithDescriptorsWhichQuantitySumIsGreaterThanOneHundredGramsThrowsArgumentException()
        {
            string designation = "Stone Soup";

            MealType type = MealType.ValueOf(ExistingMealTypesService.ExistingMealTypes[0]);

            List <Ingredient> ingredients = new List <Ingredient>();

            ingredients.Add(Ingredient.ValueOf(ExistingIngredientsService.ExistingIngredients[0]));


            List <Descriptor> descriptors = new List <Descriptor>();

            descriptors.Add(Descriptor.ValueOf("Salt", 50, "g"));

            descriptors.Add(Descriptor.ValueOf("Fat", 50.1, "g"));


            List <Allergen> allergens = new List <Allergen>();

            allergens.Add(Allergen.ValueOf(ExistingAllergensService.ExistingAllergens[0]));


            Action newMeal = () => new Meal(designation, type, ingredients, descriptors, allergens);

            Assert.Throws <ArgumentException>(newMeal);
        }
Exemple #5
0
        public void TestMealTypeWithNameThatDoesNotExistsThrowsArgumentException()
        {
            string name = "this meal type does not exist";

            Action valueOf = () => MealType.ValueOf(name);

            Assert.Throws <ArgumentException>(valueOf);
        }
Exemple #6
0
        public void TestMealTypeWithNullNameThrowsArgumentNullException()
        {
            string name = null;

            Action valueOf = () => MealType.ValueOf(name);

            Assert.Throws <ArgumentNullException>(valueOf);
        }
Exemple #7
0
        public void TestMealTypeDoesNotProveEqualityToMealTypeWithDifferentName()
        {
            string name = ExistingMealTypesService.ExistingMealTypes[0];

            MealType mealType = MealType.ValueOf(name);

            MealType differentMealType = MealType.ValueOf(ExistingMealTypesService.ExistingMealTypes[1]);

            Assert.NotEqual(mealType, differentMealType);
        }
Exemple #8
0
        public void TestMealTypeProvesEqualityToMealTypeWithEqualName()
        {
            string name = ExistingMealTypesService.ExistingMealTypes[0];

            MealType mealType = MealType.ValueOf(name);

            MealType equalMealType = MealType.ValueOf(name);

            Assert.Equal(mealType, equalMealType);
        }
Exemple #9
0
        public void TestMealTypeDoesNotProveEqualityToDifferentClassObject()
        {
            string name = ExistingMealTypesService.ExistingMealTypes[0];

            MealType mealType = MealType.ValueOf(name);

            object differentClassObject = "";

            Assert.NotEqual(mealType, differentClassObject);
        }
Exemple #10
0
        public void TestMealTypeNameReturnsStringPassedOnValueOf()
        {
            string name = ExistingMealTypesService.ExistingMealTypes[0];

            MealType type = MealType.ValueOf(name);

            string mealTypeName = type.Name;

            Assert.Equal(name, mealTypeName);
        }
Exemple #11
0
        public void TestMealTypeHasSameHashCodeAsMealTypeWithEqualName()
        {
            string name = ExistingMealTypesService.ExistingMealTypes[0];

            MealType mealType = MealType.ValueOf(name);

            MealType equalMealType = MealType.ValueOf(name);

            int mealTypeHashCode = mealType.GetHashCode();

            int equalMealTypeHashCode = equalMealType.GetHashCode();

            Assert.Equal(mealTypeHashCode, equalMealTypeHashCode);
        }
Exemple #12
0
        public void TestMealTypeHasDifferentHashCodeAsMealTypeWithEqualName()
        {
            string name = ExistingMealTypesService.ExistingMealTypes[0];

            MealType mealType = MealType.ValueOf(name);

            MealType differentMealType = MealType.ValueOf(ExistingMealTypesService.ExistingMealTypes[1]);

            int mealTypeHashCode = mealType.GetHashCode();

            int differentMealTypeHashCode = differentMealType.GetHashCode();

            Assert.NotEqual(mealTypeHashCode, differentMealTypeHashCode);
        }
        public IActionResult CreateMeal([FromBody] CreateMealModelView createModelView)
        {
            try
            {
                string designation = createModelView.Designation;

                MealType type = MealType.ValueOf(createModelView.Type);

                List <Ingredient> ingredients = createModelView.Ingredients.Select((ingredient) => Ingredient.ValueOf(ingredient)).ToList();

                List <Allergen> allergens = createModelView.Allergens.Select((allergen) => Allergen.ValueOf(allergen)).ToList();

                List <Descriptor> descriptors = createModelView.Descriptors.Select((descriptor) => Descriptor.ValueOf(descriptor.Name, descriptor.Quantity, descriptor.QuantityUnit)).ToList();

                Meal meal;

                if (allergens.Count == 0)
                {
                    meal = new Meal(designation, type, ingredients, descriptors);
                }
                else
                {
                    meal = new Meal(designation, type, ingredients, descriptors, allergens);
                }

                meal = factory.MealRepository().Save(meal);

                GetDetailedMealInformationModelView modelview = new GetDetailedMealInformationModelView(meal);

                return(Created("api/meals", modelview));
            }
            catch (InvalidOperationException argumentException)
            {
                // TODO: Log exception
                return(BadRequest(new ErrorModelView(argumentException.Message)));
            }
            catch (ArgumentException argumentException)
            {
                // TODO: Log exception
                return(BadRequest(new ErrorModelView(argumentException.Message)));
            }
            catch (Exception databaseException)
            {
                // TODO: Log exception
                return(StatusCode(500, null));
            }
        }
Exemple #14
0
        public void MealThatCompliesWithRulesCompletesSuccessfully()
        {
            string designation = "Stone Soup";

            MealType type = MealType.ValueOf(ExistingMealTypesService.ExistingMealTypes[0]);

            List <Ingredient> ingredients = new List <Ingredient>();

            ingredients.Add(Ingredient.ValueOf(ExistingIngredientsService.ExistingIngredients[0]));


            List <Descriptor> descriptors = new List <Descriptor>();

            descriptors.Add(Descriptor.ValueOf("Calorie", 50, "cal"));


            List <Allergen> allergens = new List <Allergen>();

            allergens.Add(Allergen.ValueOf(ExistingAllergensService.ExistingAllergens[0]));

            new Meal(designation, type, ingredients, descriptors, allergens);
        }
Exemple #15
0
        public void MealWithNullDescriptorsListThrowsArgumentNullException()
        {
            string designation = "Stone Soup";

            MealType type = MealType.ValueOf(ExistingMealTypesService.ExistingMealTypes[0]);

            List <Ingredient> ingredients = new List <Ingredient>();

            ingredients.Add(Ingredient.ValueOf(ExistingIngredientsService.ExistingIngredients[0]));


            List <Descriptor> descriptors = null;


            List <Allergen> allergens = new List <Allergen>();

            allergens.Add(Allergen.ValueOf(ExistingAllergensService.ExistingAllergens[0]));


            Action newMeal = () => new Meal(designation, type, ingredients, descriptors, allergens);

            Assert.Throws <ArgumentNullException>(newMeal);
        }
Exemple #16
0
        public void TestMealTypeWithNameThatExistsCompletesSuccessfully()
        {
            string name = ExistingMealTypesService.ExistingMealTypes[0];

            MealType type = MealType.ValueOf(name);
        }