Ejemplo n.º 1
0
        public void Given_Valid_Model_When_Save_Then_Send_Update_Request_To_Service()
        {
            // Given
            var editSummaryViewModel = new EditSummaryViewModel()
            {
                RiskAssessmentId = 100,
                CompanyId = 200,
                Title = "New Title",
                Reference = "New Reference",
                RiskAssessorId = 1234L,
                SiteId = 5678L
            };
            var passedSaveRiskAssessmentRequest = new SaveRiskAssessmentSummaryRequest();
            _riskAssessmentService
                .Setup(x => x.UpdateRiskAssessmentSummary(It.IsAny<SaveRiskAssessmentSummaryRequest>()))
                .Callback<SaveRiskAssessmentSummaryRequest>(y => passedSaveRiskAssessmentRequest = y);
            var target = GetTarget();

            // When
            target.Save(editSummaryViewModel);

            // Then
            _riskAssessmentService.Verify(x => x.UpdateRiskAssessmentSummary(It.IsAny<SaveRiskAssessmentSummaryRequest>()));
            Assert.That(passedSaveRiskAssessmentRequest.Id, Is.EqualTo(editSummaryViewModel.RiskAssessmentId));
            Assert.That(passedSaveRiskAssessmentRequest.CompanyId, Is.EqualTo(editSummaryViewModel.CompanyId));
            Assert.That(passedSaveRiskAssessmentRequest.Title, Is.EqualTo(editSummaryViewModel.Title));
            Assert.That(passedSaveRiskAssessmentRequest.Reference, Is.EqualTo(editSummaryViewModel.Reference));
            Assert.That(passedSaveRiskAssessmentRequest.UserId, Is.EqualTo(target.CurrentUser.UserId));
        }
Ejemplo n.º 2
0
        public ActionResult Save(EditSummaryViewModel viewModel)
        {
            Validate(viewModel);

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

            var viewModelReturned = _viewModelFactory
                .WithRiskAssessmentId(viewModel.RiskAssessmentId)
                .WithCompanyId(viewModel.CompanyId)
                .WithAllowableSiteIds(CurrentUser.GetSitesFilter())
                .GetViewModel();
            return View("Index", viewModelReturned);
        }
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");
     }
 }
 public EditSummaryViewModel GetViewModel()
 {
     var riskAssessment = _riskAssessmentService.GetRiskAssessment(_riskAssessmentId, _companyId);
     
     var model = new EditSummaryViewModel()
            {
                RiskAssessmentId = _riskAssessmentId,
                CompanyId = _companyId,
                Reference = riskAssessment.Reference,
                Title = riskAssessment.Title,
                DateOfAssessment = riskAssessment.AssessmentDate ?? DateTime.Today,
                RiskAssessorId = riskAssessment.RiskAssessor != null ? riskAssessment.RiskAssessor.Id: (long?) null,
                RiskAssessor = riskAssessment.RiskAssessor != null ? riskAssessment.RiskAssessor.FormattedName : string.Empty,
                SiteId = riskAssessment.RiskAssessmentSite != null ? riskAssessment.RiskAssessmentSite.Id : 0,
                Site = riskAssessment.RiskAssessmentSite != null ? riskAssessment.RiskAssessmentSite.Name: string.Empty,
            };
     model = AttachDropdownData(model);
     return model;
 }
        private EditSummaryViewModel AttachDropdownData(EditSummaryViewModel model)
        {
            var riskAssessmentAssessors = _riskAssessorService.Search(new SearchRiskAssessorRequest()
            {
                SiteId = (long)model.SiteId,
                CompanyId = _companyId,
                MaximumResults = 100
            });
            var assessors = riskAssessmentAssessors.ToList().Select(x => AutoCompleteViewModel.ForEmployeeNoJobTitle(x.Employee)).AddDefaultOption();
            model.RiskAssessmentAssessors = assessors;

            var siteStructureElements = _siteService.Search(new SearchSitesRequest
            {
                CompanyId = _companyId,
                AllowedSiteIds = _allowedSiteIds
            });
            var sites = siteStructureElements.Select(x => new AutoCompleteViewModel(x.Name, x.Id.ToString())).AddDefaultOption();
            model.Sites = sites;

            return model;
        }
Ejemplo n.º 6
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();
     }
 }
Ejemplo n.º 7
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,
         SiteId = model.SiteId
     });
 }
 public EditSummaryViewModel GetViewModel(EditSummaryViewModel viewModel)
 {
     viewModel = AttachDropdownData(viewModel);
     return viewModel;
 }
Ejemplo n.º 9
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.º 10
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);

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

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

            // When
            target.Save(editSummaryViewModel);

            // Then
            _viewModelFactory.VerifyAll();
        }
Ejemplo n.º 11
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",
                RiskAssessorId = 1234L,
                SiteId = 5678L
            };
            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);
        }