public void When_update_control_measures_Then_should_return_correct_result()
        {
            // Given
            var viewModel = new SaveControlMeasureViewModel()
                                {
                                    ControlMeasureId = 200,
                                    RiskAssessmentId = _riskAssessmentId,
                                    CompanyId = _companyId,
                                    ControlMeasure = "Hello Control Measure"
                                };

            _riskAssessmentService
                .Setup(
                    x =>
                    x.UpdateControlMeasure(
                        It.Is<UpdateControlMeasureRequest>(r => r.ControlMeasureId == viewModel.ControlMeasureId &&
                                                                r.CompanyId ==
                                                                viewModel.CompanyId &&
                                                                r.RiskAssessmentId ==
                                                                viewModel.
                                                                    RiskAssessmentId &&
                                                                r.ControlMeasure ==
                                                                viewModel.ControlMeasure)));
                
            // When
            var result = _target.UpdateControlMeasure(viewModel) as JsonResult;


            // Then
            Assert.That(result.Data.ToString(), Contains.Substring("{ Success = True }"));
        }
        public void When_update_control_measures_Then_should_call_correct_methods()
        {
            // Given

            var viewModel = new SaveControlMeasureViewModel()
                                {
                                    ControlMeasureId = 10,
                                    RiskAssessmentId = _riskAssessmentId,
                                    CompanyId = _companyId,
                                    ControlMeasure = "Hello Control Measure"
                                };

            _riskAssessmentService
                .Setup(x => x.UpdateControlMeasure(It.Is<UpdateControlMeasureRequest>(r =>
                                                                                      r.ControlMeasureId ==
                                                                                      viewModel.ControlMeasureId &&
                                                                                      r.CompanyId == viewModel.CompanyId &&
                                                                                      r.RiskAssessmentId ==
                                                                                      viewModel.RiskAssessmentId &&
                                                                                      r.ControlMeasure ==
                                                                                      viewModel.ControlMeasure)));
            

            // When
            _target.UpdateControlMeasure(viewModel);

            // Then
            _riskAssessmentService.VerifyAll();
        }
        public JsonResult UpdateControlMeasure(SaveControlMeasureViewModel viewModel)
        {
            _riskAssessmentService.UpdateControlMeasure(new UpdateControlMeasureRequest
            {
                CompanyId = viewModel.CompanyId,
                RiskAssessmentId = viewModel.RiskAssessmentId,
                ControlMeasureId = viewModel.ControlMeasureId,
                ControlMeasure = viewModel.ControlMeasure,
                UserId = CurrentUser.UserId
            });

            return Json(new { Success = true });
        }
        public void Given_invalid_viewmodel_When_AddControlMeasureToRiskAssessmentHazard_Then_should_return_correct_result()
        {
            // Given
            var controller = GetTarget();
            var viewModel = new SaveControlMeasureViewModel();

            controller.ModelState.AddModelError("Any", "Any");

            // When
            var result = controller.UpdateControlMeasureForRiskAssessmentHazard(viewModel) as JsonResult;

            // Then
            Assert.That(result.Data.ToString(), Contains.Substring("Success = false"));
        }
        public JsonResult AddControlMeasureToRiskAssessment(SaveControlMeasureViewModel model)
        {
            if (!ModelState.IsValid)
            {
                ModelStateErrorsAsJson();
            }
            var Id = _riskAssessmentService.AddControlMeasureToRiskAssessment(new AddControlMeasureRequest
            {
                CompanyId = model.CompanyId,
                RiskAssessmentId = model.RiskAssessmentId,
                ControlMeasure = model.ControlMeasure,
                UserId = CurrentUser.UserId

            });

            return Json(new { Success = true, Id });
        }
        public JsonResult UpdateControlMeasureForRiskAssessmentHazard(SaveControlMeasureViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return ModelStateErrorsAsJson();
            }

            _riskAssessmentHazardService.UpdateControlMeasureForRiskAssessmentHazard(new UpdateControlMeasureRequest
            {
                CompanyId = CurrentUser.CompanyId,
                RiskAssessmentId = model.RiskAssessmentId,
                RiskAssessmentHazardId = model.RiskAssessmentHazardId,
                ControlMeasureId = model.ControlMeasureId,
                ControlMeasure = model.ControlMeasure,
                UserId = CurrentUser.UserId
            });

            return Json(new { Success = true });
        }
        [Test] public void When_AddControlMeasureToRiskAssessmentHazard_Then_should_return_correct_result()
        {
            // Given
            var controller = GetTarget();
            var viewModel = new SaveControlMeasureViewModel()
            {
                CompanyId = CompanyId,
                RiskAssessmentId = RiskAssessmentId,
                ControlMeasure = "Hello",
                RiskAssessmentHazardId = 10,
            };

            _riskAssessmentHazardService
                .Setup(x => x.AddControlMeasureToRiskAssessmentHazard(It.IsAny<AddControlMeasureRequest>()))
                .Returns(400);

            // When
            var result = controller.AddControlMeasureToRiskAssessmentHazard(viewModel) as JsonResult;

            // Then
            Assert.That(result.Data.ToString(),Contains.Substring("{ Success = True, Id = 400 }"));
        }
        public void When_RemoveControlMeasureFromRiskAssessmentHazard_Then_should_call_correct_methods()
        {
            // Given
            var controller = GetTarget();
            var viewModel = new SaveControlMeasureViewModel()
                                {
                                    CompanyId = CompanyId,
                                    RiskAssessmentId = RiskAssessmentId,
                                    ControlMeasureId = ControlMeasureId,
                                    RiskAssessmentHazardId = RiskAssessmentHazardId,
                                };
            var userId = controller.CurrentUser.UserId;

            // When
            controller.RemoveControlMeasureFromRiskAssessmentHazard(RiskAssessmentId, RiskAssessmentHazardId, ControlMeasureId,CompanyId);

            // Then
            _riskAssessmentHazardService
                .Verify(x => x.RemoveControlMeasureFromRiskAssessmentHazard(It.Is<RemoveControlMeasureRequest>(y => y.CompanyId == viewModel.CompanyId &&
                                                                                                            y.RiskAssessmentId == viewModel.RiskAssessmentId &&
                                                                                                            y.UserId == userId &&
                                                                                                            y.RiskAssessmentHazardId == viewModel.RiskAssessmentHazardId &&
                                                                                                            y.ControlMeasureId == ControlMeasureId)));
        }
        public void When_remove_control_measures_Then_should_call_correct_methods()
        {
            // Given

            var viewModel = new SaveControlMeasureViewModel()
                                {
                                    ControlMeasureId = _controlMeasureId,
                                    RiskAssessmentId = _riskAssessmentId,
                                    CompanyId = _companyId,
                                };

            _riskAssessmentService
                .Setup(x => x.RemoveControlMeasureFromRiskAssessment(It.Is<RemoveControlMeasureRequest>(r =>
                                                                                      r.ControlMeasureId == viewModel.ControlMeasureId &&
                                                                                      r.CompanyId == viewModel.CompanyId &&
                                                                                      r.RiskAssessmentId == viewModel.RiskAssessmentId )));


            // When
            _target.RemoveControlMeasureFromRiskAssessment(_riskAssessmentId, _controlMeasureId, _companyId);

            // Then
            _riskAssessmentService.VerifyAll();
        }