public ActionResult Create(DisasterViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var disaster = new Disaster
                    {
                        Id = model.Id,
                        Name = model.Name,
                        IsActive = model.IsActive
                    };

                    if (model.Id == -1)
                    {
                        _disasterSvc.Create(disaster);
                    }
                    else
                    {
                        _disasterSvc.Update(disaster);
                    }
                }
                catch (DisasterAlreadyExistsException)
                {
                    ModelState.AddModelError("Name", "A Disaster already exists with that Name!");
                    return View("Create", model);
                }

                return Redirect("/Disaster/List");
            }
            return View("Create", model);
        }
        public void Assign_ValidDataAdd_ReturnsListView()
        {
            // Arrange

            // Act
            var viewModel = new DisasterViewModel { Id = -1, Name ="test", IsActive = false};
            var response = _controllerUnderTest.Create(viewModel);

            // Assert
            var result = response as RedirectResult;
            Assert.IsTrue(result.Url.Equals("/Disaster/List"));
        }
        public void Assign_duplicateName_ReturnsCreateView()
        {
            // Arrange
            _disaster.Setup(x => x.Create(
                It.IsAny<Disaster>())).Throws(new DisasterAlreadyExistsException());

            // Act
            var viewModel = new DisasterViewModel { Id = -1, Name = "test", IsActive = true };
            var response = _controllerUnderTest.Create(viewModel);

            // Assert
            var view = response as ViewResult;
            Assert.AreEqual("Create", view.ViewName);
            Assert.IsTrue(view.ViewData.ModelState.Count >= 1);
        }
        public void Assign_InvalidDisasterName_ReturnsErrorMessage()
        {
            //Arrange
            var viewModel = new DisasterViewModel
            {
                Id = -1,
                Name = "test#3",//invalid name with special char
                IsActive = false,
                SelectedDisasterClusters = (new System.Collections.Generic.List<SelectedDisasterCluster>() { new SelectedDisasterCluster { Id = 1, Name = "Test", Selected = true }, }),
            };

            //Act
            var validationResults = ModelValidation.ValidateModel(viewModel);

            // Assert
            Assert.IsTrue(ContainsDisasterValidationResult(validationResults), "Invalid Disaster Message not given");
        }
        public void Assign_ValidDataAdd_ReturnsListView()
        {
            //Arrange
            var DisList = new System.Collections.Generic.List<Disaster>() { new Disaster {Name = "test", Id = 1, IsActive = false } };
            _disaster.Setup(x => x.GetList()).Returns(DisList);
            // Act
            var viewModel = new DisasterViewModel
            {
                Id = -1,
                Name = "test",
                IsActive = false,
                SelectedDisasterClusters = (new System.Collections.Generic.List<SelectedDisasterCluster>() { new SelectedDisasterCluster { Id = 1, Name = "Test", Selected = true }, }),
            };
            var response = _controllerUnderTest.Create(viewModel);

            // Assert
            var result = response as RedirectResult;
            Assert.IsTrue(result.Url.Equals("/Disaster/List"));
        }
 public ActionResult Edit(DisasterViewModel model)
 {
     TempData["EditUrlDeprecatedWarning"] = "POST /Edit is deprecated. Use POST /Create instead";
     return Create(model);
 }
예제 #7
0
        public ActionResult Create(DisasterViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (!model.SelectedDisasterClusters.Exists(x => x.Selected == true))
                    {
                        ModelState.AddModelError("Name", "You have to include at least one Cluster!");
                        return View("Create", model);
                    }

                    var disaster = new Disaster
                    {
                        Id = model.Id,
                        Name = model.Name,
                        IsActive = model.IsActive
                    };

                    var newClusterList = (from selectedList in model.SelectedDisasterClusters
                                          join clusterList in _clusterSvc.GetList()
                                          on selectedList.Id equals clusterList.Id into outerList
                                          from clusterList in outerList.DefaultIfEmpty()
                                          where clusterList == null
                                          select new Cluster { Name = selectedList.Name, Id = selectedList.Id}).ToList();

                    foreach (var item in newClusterList)
                    {
                        _clusterSvc.Create(item);
                    }

                    if (model.Id == -1)
                    {
                        _disasterSvc.Create(disaster);

                        var id = _disasterSvc.GetList().Where(x => x.Name == model.Name).FirstOrDefault().Id;

                        foreach (var item in model.SelectedDisasterClusters)
                        {
                            if (item.Selected == true)
                            {
                                if (newClusterList.Exists(x => x.Name == item.Name))
                                {
                                    _disasterClusterSvc.Create(new DisasterCluster { Id = -1, DisasterId = id, Cluster = newClusterList.Find(x => x.Name == item.Name) });
                                }
                                else
                                {
                                    _disasterClusterSvc.Create(new DisasterCluster { Id = -1, DisasterId = id, ClusterId = item.Id });
                                }
                            }
                        }
                    }
                    else
                    {
                        _disasterSvc.Update(disaster);

                        var disasterClusterList = _disasterClusterSvc.GetClustersForADisaster(model.Id);

                                       
                        foreach (var item in model.SelectedDisasterClusters)
                        {
                            if (item.Selected == true)
                            {
                                if (!disasterClusterList.Exists(x => x.ClusterId == item.Id))
                                {
                                    _disasterClusterSvc.Create(new DisasterCluster { Id = -1, DisasterId = model.Id, ClusterId = item.Id });
                                }
                            }
                            else
                            {
                                if (disasterClusterList.Exists(x => x.ClusterId == item.Id))
                                {
                                    _disasterClusterSvc.Remove(disasterClusterList.Find(x => x.ClusterId == item.Id));
                                }
                            }
                        }
                    }
                }
                catch (DisasterAlreadyExistsException)
                {
                    ModelState.AddModelError("Name", "A Disaster already exists with that Name!");
                    return View("Create", model);
                }

                return Redirect("/Disaster/List");
            }
            return View("Create", model);
        }
        public void Assign_ValidDisasterName_ReturnsValidModel()
        {
            //Arrange
            var viewModel = new DisasterViewModel
            {
                Id = -1,
                Name = "test 3",//valid name with space char
                IsActive = false,
                SelectedDisasterClusters = (new System.Collections.Generic.List<SelectedDisasterCluster>() { new SelectedDisasterCluster { Id = 1, Name = "Test", Selected = true }, }),
            };

            //Act
            var validationResults = ModelValidation.ValidateModel(viewModel);

            // Assert
            Assert.IsFalse(ContainsDisasterValidationResult(validationResults));
        } 
        public void Assign_ValidDataUpdate_ReturnsListView()
        {
            // Arrange
            var disClusList = new System.Collections.Generic.List<DisasterCluster>(){new DisasterCluster { Id = -1, ClusterId = 1, DisasterId = 1 }};
            _disasterClusterService.Setup(x => x.GetClustersForADisaster(1)).Returns(disClusList);

            // Act
            var viewModel = new DisasterViewModel
            {
                Id = 1,
                Name = "updated",
                IsActive = true,
                SelectedDisasterClusters = (new System.Collections.Generic.List<SelectedDisasterCluster>(){new SelectedDisasterCluster{Id = 1, Name = "Test", Selected = true},}),
            };
            var response = _controllerUnderTest.Create(viewModel);

            // Assert

            var result = response as RedirectResult;
            Assert.IsTrue(result.Url.Equals("/Disaster/List"));
        }