Пример #1
0
        public void EditAnimal_Fail_DublicatedName()
        {
            // Arrange
            AnimalLight animalLight = EditedAnimalLight();
            Animal      animal      = animals.OrderBy(a => a.AnimalName).FirstOrDefault();

            animalLight.AnimalName = animal.AnimalName; //  "Bear";

            // ModelState.AddModelError("animalLight", "Ошибка: другое животное с таким названием - уже существует в списке животных!");
            //
            string key = "animalLight";
            string msg = "Ошибка: другое животное с таким названием - уже существует в списке животных!";

            // Act
            var actionResult  = controller.EditAnimal(animalLight.AnimalId, animalLight) as IHttpActionResult;
            var contentResult = actionResult as InvalidModelStateResult;

            // Assert
            Assert.IsNotNull(contentResult);
            Assert.IsInstanceOfType(actionResult, typeof(InvalidModelStateResult));
            Assert.IsNotNull(contentResult.ModelState);
            Assert.IsNotNull(contentResult.ModelState[key]);
            Assert.IsNotNull(contentResult.ModelState[key].Errors);
            Assert.IsNotNull(contentResult.ModelState[key].Errors[0]);
            Assert.AreEqual(msg, contentResult.ModelState[key].Errors[0].ErrorMessage);
        }
Пример #2
0
        public void EditAnimal_Fail_LongName()
        {
            // Arrange
            AnimalLight animalLight = EditedAnimalLight();

            animalLight.AnimalName = new String('s', 101);

            // ModelState.AddModelError("animalLight", "Ошибка: название животного должно содержать от 2-х до 100 символов!");
            //
            string key = "animalLight";
            string msg = "Ошибка: название животного должно содержать от 2-х до 100 символов!";

            // Act
            var actionResult  = controller.EditAnimal(animalLight.AnimalId, animalLight) as IHttpActionResult;
            var contentResult = actionResult as InvalidModelStateResult;

            // Assert
            Assert.IsNotNull(contentResult);
            Assert.IsInstanceOfType(actionResult, typeof(InvalidModelStateResult));
            Assert.IsNotNull(contentResult.ModelState);
            Assert.IsNotNull(contentResult.ModelState[key]);
            Assert.IsNotNull(contentResult.ModelState[key].Errors);
            Assert.IsNotNull(contentResult.ModelState[key].Errors[0]);
            Assert.AreEqual(msg, contentResult.ModelState[key].Errors[0].ErrorMessage);
        }
Пример #3
0
        public void EditAnimal_Return_CorrectUpdatedAnimal()
        {
            // Arrange
            AnimalLight animalLight = EditedAnimalLight();

            // Act
            var actionResult  = controller.EditAnimal(animalLight.AnimalId, animalLight) as IHttpActionResult;
            var contentResult = actionResult as NegotiatedContentResult <AnimalLight>;

            // Assert
            Assert.IsNotNull(contentResult);
            Assert.IsNotNull(contentResult.Content);

            // Assert
            Assert.AreEqual(animalLight.AnimalId, contentResult.Content.AnimalId);
            Assert.AreEqual(animalLight.AnimalName, contentResult.Content.AnimalName);
            Assert.AreEqual(animalLight.SkinId, contentResult.Content.SkinId);
            Assert.AreEqual(animalLight.KindId, contentResult.Content.KindId);
            Assert.AreEqual(animalLight.LocationId, contentResult.Content.LocationId);
            Assert.AreEqual(animalLight.RegIds.Count(), contentResult.Content.RegIds.Count());

            foreach (var id in animalLight.RegIds)
            {
                Assert.IsTrue(contentResult.Content.RegIds.Contains(id));
            }
        }
Пример #4
0
        public void EditAnimal_Invoke_Locations_GetAll()
        {
            // Arrange
            AnimalLight animalLight = EditedAnimalLight();

            // Act
            var actionResult = controller.EditAnimal(animalLight.AnimalId, animalLight) as IHttpActionResult;

            // Assert
            mockLocation.Verify(m => m.GetAll());
        }
Пример #5
0
        public void EditAnimal_Return_NotNullResult()
        {
            // Arrange
            AnimalLight animalLight = EditedAnimalLight();

            // Act
            var actionResult = controller.EditAnimal(animalLight.AnimalId, animalLight) as IHttpActionResult;

            // Assert
            Assert.IsNotNull(actionResult);
        }
Пример #6
0
        public void CreateAnimal_Invoke_Regions_GetAll()
        {
            // Arrange
            AnimalLight animalLight = NewAnimalLight();

            // Act
            var actionResult = controller.CreateAnimal(animalLight) as IHttpActionResult;

            // Assert
            mockRegion.Verify(m => m.GetAll());
        }
Пример #7
0
        public void CreateAnimal_Return_NotNullResult()
        {
            // Arrange
            AnimalLight animalLight = NewAnimalLight();

            // Act
            var actionResult = controller.CreateAnimal(animalLight) as IHttpActionResult;

            // Assert
            Assert.IsNotNull(actionResult);
        }
Пример #8
0
        public void EditAnimal_Invoke_Update()
        {
            // Arrange
            AnimalLight animalLight = EditedAnimalLight();

            // Act
            var actionResult = controller.EditAnimal(animalLight.AnimalId, animalLight) as IHttpActionResult;

            // Assert
            mockAnimal.Verify(m => m.Update(It.Is <Animal>(a => a.AnimalId == animalLight.AnimalId)));
            mockAnimal.Verify(m => m.Save());
        }
Пример #9
0
        public void EditAnimal_Return_CorrectContentResult()
        {
            // Arrange
            AnimalLight animalLight = EditedAnimalLight();

            // Act
            var actionResult  = controller.EditAnimal(animalLight.AnimalId, animalLight) as IHttpActionResult;
            var contentResult = actionResult as NegotiatedContentResult <AnimalLight>;

            // Assert
            Assert.AreEqual(HttpStatusCode.Accepted, contentResult.StatusCode);
            Assert.AreEqual(animalLight.AnimalId, contentResult.Content.AnimalId);
        }
Пример #10
0
        public void CreateAnimal_Return_CorrectRoute()
        {
            // Arrange
            AnimalLight animalLight = NewAnimalLight();

            // Act
            var actionResult  = controller.CreateAnimal(animalLight) as IHttpActionResult;
            var contentResult = actionResult as CreatedAtRouteNegotiatedContentResult <AnimalLight>;

            // Assert
            Assert.AreEqual("DefaultApi", contentResult.RouteName);
            Assert.AreEqual(animalLight.AnimalId, contentResult.RouteValues["id"]);
        }
Пример #11
0
        public void CreateAnimal_Return_CorrectDomainModel()
        {
            // Arrange
            AnimalLight animalLight = NewAnimalLight();

            // Act
            var actionResult  = controller.CreateAnimal(animalLight) as IHttpActionResult;
            var contentResult = actionResult as CreatedAtRouteNegotiatedContentResult <AnimalLight>;

            // Assert
            Assert.IsNotNull(contentResult);
            Assert.IsNotNull(contentResult.Content);
        }
Пример #12
0
        public void EditAnimal_Return_BadRequest()
        {
            // Arrange
            AnimalLight animalLight = EditedAnimalLight();

            // Act
            var actionResult  = controller.EditAnimal(animalLight.AnimalId, null) as IHttpActionResult;
            var contentResult = actionResult as BadRequestResult;

            // Assert
            Assert.IsNotNull(contentResult);
            Assert.IsInstanceOfType(actionResult, typeof(BadRequestResult));
        }
Пример #13
0
        public void EditAnimal_Return_CorrectDomainModel()
        {
            // Arrange
            AnimalLight animalLight = EditedAnimalLight();

            // Act
            var actionResult  = controller.EditAnimal(animalLight.AnimalId, animalLight) as IHttpActionResult;
            var contentResult = actionResult as NegotiatedContentResult <AnimalLight>;

            // Assert
            Assert.IsNotNull(contentResult);
            Assert.IsNotNull(contentResult.Content);
        }
Пример #14
0
        public IHttpActionResult CreateAnimal([FromBody] AnimalLight animalLight)
        {
            Trace.WriteLine("--- CreateAnimal() ---");

            if (animalLight == null)
            {
                return(BadRequest());
            }

            if (_repoFactory.CreateAnimalRepository().GetAll().Count(a => a.AnimalName.Equals(animalLight.AnimalName, StringComparison.InvariantCultureIgnoreCase)) > 0)
            {
                ModelState.AddModelError("animalLight", "Ошибка: животное с таким названием - уже существует в списке животных!");
            }

            // Проверка на допустимое кол-во символов в имени нового животного (ставим условие: от 2-х до 100 символов)
            //
            if (string.IsNullOrEmpty(animalLight.AnimalName) || animalLight.AnimalName.Length < 2 || animalLight.AnimalName.Length > 100)
            {
                ModelState.AddModelError("animalLight", "Ошибка: название животного должно содержать от 2-х до 100 символов!");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                // сопоставление
                Animal animal = Mapper.Map <AnimalLight, Animal>(animalLight);

                var regions = _repoFactory.CreateRegionRepository().GetAll().Where(r => animalLight.RegIds.Contains(r.Id)).ToList <Region>();

                animal.Regions = regions;

                _repoFactory.CreateAnimalRepository().Create(animal);
                _repoFactory.CreateAnimalRepository().Save();

                animalLight.AnimalId = animal.AnimalId;

                // если запрос без ошибок
                return(CreatedAtRoute("DefaultApi", new { id = animalLight.AnimalId }, animalLight));
            }
            catch (Exception x)
            {
                Trace.WriteLine("CreateAnimal() Exception: " + x.Message);

                ModelState.AddModelError("animal", x.Message);
                return(BadRequest(ModelState));
            }
        }
Пример #15
0
        //
        //-----------------------------------     Tests for DeleteAnimal()   ----------------------------------------
        //
        #region Tests for DeleteAnimal()

        AnimalLight DeletedAnimalLight()
        {
            Animal animal = animals.OrderBy(a => a.AnimalName).FirstOrDefault();

            AnimalLight animalLight = new AnimalLight
            {
                AnimalId   = animal.AnimalId,
                AnimalName = animal.AnimalName,
                SkinId     = animal.SkinId,
                KindId     = animal.KindId,
                LocationId = animal.LocationId,
                RegIds     = animal.Regions.Select(m => m.Id).ToArray <int>()
            };

            return(animalLight);
        }
Пример #16
0
        //
        //-----------------------------------     Tests for EditAnimal()   ----------------------------------------
        //
        #region Tests for EditAnimal()

        AnimalLight EditedAnimalLight()
        {
            Animal animal       = animals.OrderBy(a => a.AnimalName).LastOrDefault();
            Animal animal_first = animals.OrderBy(a => a.AnimalName).FirstOrDefault();

            AnimalLight animalLight = new AnimalLight()
            {
                AnimalId   = animal.AnimalId,
                AnimalName = "swan",
                SkinId     = animal_first.SkinId,
                KindId     = animal_first.KindId,
                LocationId = animal_first.LocationId,
                RegIds     = animal_first.Regions.Select(m => m.Id).ToArray <int>()
            };

            return(animalLight);
        }
Пример #17
0
        public void CreateAnimal_Invoke_Create()
        {
            // Arrange
            AnimalLight animalLight = NewAnimalLight();

            // Act
            var actionResult = controller.CreateAnimal(animalLight) as IHttpActionResult;

            // Assert
            mockAnimal.Verify(m => m.Create(It.Is <Animal>(a => a.AnimalId == animalLight.AnimalId &&
                                                           a.AnimalName == animalLight.AnimalName &&
                                                           a.KindId == animalLight.KindId &&
                                                           a.LocationId == animalLight.LocationId &&
                                                           a.SkinId == animalLight.SkinId &&
                                                           a.Regions.Count() == animalLight.RegIds.Count)));

            mockAnimal.Verify(m => m.Save());
        }
Пример #18
0
        public IHttpActionResult EditAnimal(int id, [FromBody] AnimalLight animalLight)
        {
            Trace.WriteLine("--- EditAnimal() ---");

            if (animalLight == null)
            {
                return(BadRequest());
            }

            if (_repoFactory.CreateAnimalRepository().GetAll().Count(a => a.AnimalId != animalLight.AnimalId &&
                                                                     a.AnimalName.Equals(animalLight.AnimalName, StringComparison.InvariantCultureIgnoreCase)) > 0)
            {
                ModelState.AddModelError("animalLight", "Ошибка: другое животное с таким названием - уже существует в списке животных!");
            }

            // Проверка на допустимое кол-во символов в имени нового животного (ставим условие: от 2-х до 100 символов)
            //
            if (string.IsNullOrEmpty(animalLight.AnimalName) || animalLight.AnimalName.Length < 2 || animalLight.AnimalName.Length > 100)
            {
                ModelState.AddModelError("animalLight", "Ошибка: название животного должно содержать от 2-х до 100 символов!");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                Animal animal = _repoFactory.CreateAnimalRepository().GetAll().Include(a => a.Skin).Include(a => a.Kind).Include(a => a.Location).Include(a => a.Regions)
                                .FirstOrDefault(a => a.AnimalId == animalLight.AnimalId);

                // Изменяем поля для выбранного животного - на новые
                //
                if (id == animal.AnimalId)
                {
                    var regions = _repoFactory.CreateRegionRepository().GetAll().Where(r => animalLight.RegIds.Contains(r.Id)).ToList <Region>();
                    animal.Regions = regions;

                    animal.Skin     = _repoFactory.CreateSkinRepository().GetAll().FirstOrDefault(s => s.Id == animalLight.SkinId);
                    animal.Kind     = _repoFactory.CreateKindRepository().GetAll().FirstOrDefault(s => s.Id == animalLight.KindId);
                    animal.Location = _repoFactory.CreateLocationRepository().GetAll().FirstOrDefault(s => s.Id == animalLight.LocationId);

                    animal.AnimalName = animalLight.AnimalName;

                    _repoFactory.CreateAnimalRepository().Update(animal);
                    _repoFactory.CreateAnimalRepository().Save();

                    animalLight.AnimalId = animal.AnimalId;
                }
                else
                {
                    ModelState.AddModelError("animal", "Ошибка редактирования: Такого животного уже не существует в базе данных");
                }
            }
            catch (Exception x)
            {
                ModelState.AddModelError("animal", x.Message);
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // если запрос без ошибок
            return(Content(HttpStatusCode.Accepted, animalLight));
        }