Ejemplo n.º 1
0
        public void Given_valid_model_When_Save_Then_should_call_correct_methods()
        {
            // Given
            var model = new EditSummaryViewModel()
                                       {
                                           RiskAssessmentId = 100,
                                           CompanyId = 200,
                                           Title = "New Title",
                                           Reference = "New Reference",
                                           DateOfAssessment = DateTime.Now,
                                           RiskAssessorId = 364L,
                                           SiteId = 378L
                                       };
            
            _riskAssessmentService
                .Setup(x => x.UpdateRiskAssessmentSummary(It.Is<SaveRiskAssessmentSummaryRequest>(y => 
                                                                y.CompanyId == model.CompanyId && 
                                                                y.Id == model.RiskAssessmentId &&
                                                                y.Title == model.Title && 
                                                                y.Reference == model.Reference &&
                                                                y.RiskAssessorId == model.RiskAssessorId &&
                                                                y.AssessmentDate == model.DateOfAssessment)));
                
            var target = GetTarget();

            // When
            target.Save(model);

            // Then
            _riskAssessmentService.Verify(x => x.UpdateRiskAssessmentSummary(It.IsAny<SaveRiskAssessmentSummaryRequest>()));
        }
 public EditSummaryViewModel GetViewModel(EditSummaryViewModel viewModel)
 {
     var assessors = GetRiskAssessors();
     var sites = GetSites();
     viewModel.RiskAssessmentAssessors = assessors;
     viewModel.Sites = sites;
     return viewModel;
 }
Ejemplo n.º 3
0
 private void Validate(EditSummaryViewModel viewModel)
 {
     if (viewModel.RiskAssessorId == null)
     {
         ModelState.AddModelError("RiskAssessorId", "Please select a Risk Assessor");
     }
     if (viewModel.SiteId == null)
     {
         ModelState.AddModelError("SiteId", "Please select a Site");
     }
 }
Ejemplo n.º 4
0
 private void UpdateRiskAssessmentSummary(EditSummaryViewModel model)
 {
     _riskAssessmentService.UpdateRiskAssessmentSummary(new SaveRiskAssessmentSummaryRequest
     {
         Id = model.RiskAssessmentId,
         CompanyId = model.CompanyId,
         Title = model.Title,
         Reference = model.Reference,
         UserId = CurrentUser.UserId,
         AssessmentDate = model.DateOfAssessment,
         RiskAssessorId = model.RiskAssessorId,
         PersonAppointed = model.PersonAppointed,
         SiteId = model.SiteId
     });
 }
Ejemplo n.º 5
0
        public void Given_valid_model_When_SaveAndNext_Then_should_return_correct_result()
        {
            // Given
            var editSummaryViewModel = new EditSummaryViewModel()
                                           {
                                               RiskAssessmentId = 100,
                                               CompanyId = 200,
                                               Title = "New Title",
                                               Reference = "New Reference"
                                           };
            var target = GetTarget();

            // When
            var result = target.SaveAndNext(editSummaryViewModel) as JsonResult;

            // Then
            Assert.That(result.Data.ToString(), Is.EqualTo("{ Success = True }"));
        }
Ejemplo n.º 6
0
        public ActionResult Save(EditSummaryViewModel viewModelParam)
        {
            Validate(viewModelParam);

            if (ModelState.IsValid)
            {
                try
                {
                    UpdateRiskAssessmentSummary(viewModelParam);
                    TempData["Notice"] = "Risk Assessment Summary Successfully Updated";
                    return RedirectToAction("Index", new { riskAssessmentId = viewModelParam.RiskAssessmentId, companyId = CurrentUser.CompanyId });
                }
                catch (ValidationException validationException)
                {
                    ModelState.AddValidationErrors(validationException);
                }
            }

            var viewModelReturned = _viewModelFactory
                .WithRiskAssessmentId(viewModelParam.RiskAssessmentId)
                .WithCompanyId(viewModelParam.CompanyId)
                .GetViewModel();
            return View("Index", viewModelReturned);
        }
Ejemplo n.º 7
0
        public void Given_no_riskassessor_Model_When_Save_Then_add_error()
        {
            // Given
            const long riskAssessmentId = 1324L;
            const long companyId = 1324L;

            _viewModelFactory
               .Setup(x => x.WithRiskAssessmentId(riskAssessmentId))
               .Returns(_viewModelFactory.Object);

            _viewModelFactory
               .Setup(x => x.WithCompanyId(companyId))
               .Returns(_viewModelFactory.Object);

            var target = GetTarget();

            var allowableSites = target.CurrentUser.GetSitesFilter();
            _viewModelFactory
               .Setup(x => x.WithAllowableSiteIds(allowableSites))
               .Returns(_viewModelFactory.Object);

            _viewModelFactory
               .Setup(x => x.GetViewModel())
               .Returns(new EditSummaryViewModel());

            var editSummaryViewModel = new EditSummaryViewModel()
            {
                RiskAssessmentId = riskAssessmentId,
                CompanyId = companyId,
                Title = "New Title",
                Reference = "New Reference"
            };
            var passedSaveRiskAssessmentRequest = new SaveRiskAssessmentSummaryRequest();
            _riskAssessmentService
                .Setup(x => x.UpdateRiskAssessmentSummary(It.IsAny<SaveRiskAssessmentSummaryRequest>()))
                .Callback<SaveRiskAssessmentSummaryRequest>(y => passedSaveRiskAssessmentRequest = y);

            // When
            target.Save(editSummaryViewModel);

            // Then
            Assert.That(target.ModelState.IsValid, Is.False);
            Assert.That(target.ModelState.ContainsKey("RiskAssessorId"));
        }
Ejemplo n.º 8
0
        public void Given_invalid_model_When_Save_Then_should_call_correct_methods()
        {
            var riskAssessmentId = 100L;
            var companyId = 200L;

            // Given
            var editSummaryViewModel = new EditSummaryViewModel()
            {
                RiskAssessmentId = riskAssessmentId,
                CompanyId = companyId,
                Title = "",
                Reference = ""
            };

            var target = GetTarget();
            target.ModelState.AddModelError("Anything", "Anything");

            _viewModelFactory
               .Setup(x => x.WithRiskAssessmentId(riskAssessmentId))
               .Returns(_viewModelFactory.Object);

            _viewModelFactory
               .Setup(x => x.WithCompanyId(companyId))
               .Returns(_viewModelFactory.Object);

            _viewModelFactory
               .Setup(x => x.GetViewModel())
               .Returns(new EditSummaryViewModel());

            // When
            target.Save(editSummaryViewModel);

            // Then
            _viewModelFactory.VerifyAll();
        }
Ejemplo n.º 9
0
        public void Given_valid_model_When_Save_Then_should_return_correct_result()
        {
            // Given
            var editSummaryViewModel = new EditSummaryViewModel()
            {
                RiskAssessmentId = 100,
                CompanyId = 200,
                Title = "New Title",
                Reference = "New Reference",
                SiteId = 378L,
                RiskAssessorId = 1234L
            };
            var target = GetTarget();

            // When
            var result = target.Save(editSummaryViewModel) as RedirectToRouteResult;

            // Then
            Assert.That(result.RouteValues["controller"], Is.Null);
            Assert.That(result.RouteValues["action"], Is.EqualTo("Index"));
            Assert.That(result.RouteValues["riskAssessmentId"], Is.EqualTo(editSummaryViewModel.RiskAssessmentId));
            Assert.That(target.TempData["Notice"], Is.Not.Null);
        }
Ejemplo n.º 10
0
        public void Given_invalid_model_When_SaveAndNext_Then_should_return_correct_result()
        {
            // Given
            var editSummaryViewModel = new EditSummaryViewModel()
            {
                RiskAssessmentId = 100,
                CompanyId = 200,
                Title = "New Title",
                Reference = "New Reference"
            };
            var target = GetTarget();

            _riskAssessmentService
                .Setup(x => x.UpdateRiskAssessmentSummary(It.IsAny<SaveRiskAssessmentSummaryRequest>()))
                .Throws(new ValidationException(new BindingList<ValidationFailure>()));
            
            // When
            var result = target.SaveAndNext(editSummaryViewModel) as JsonResult;

            // Then
            Assert.That(result.Data.ToString(), Is.EqualTo("{ Success = false, Errors = System.String[] }"));
        }
Ejemplo n.º 11
0
 public JsonResult SaveAndNext(EditSummaryViewModel model)
 {
     try
     {
         if (ModelState.IsValid)
         {
             UpdateRiskAssessmentSummary(model);
             return Json(new { Success = true });
         }
         else
         {
             return ModelStateErrorsAsJson();
         }
     }
     catch (ValidationException validationException)
     {
         ModelState.AddValidationErrors(validationException);
         return ModelStateErrorsAsJson();
     }
 }