public void Given_site_and_risk_assessor_have_been_set_When_UpdateRiskAssessment_Then_site_should_be_set()
        {
            // Given
            var request = new SavePersonalRiskAssessmentRequest()
                              {
                                  CompanyId = _riskAssessment.CompanyId,
                                  Reference = "Reference",
                                  Title = "Title",
                                  UserId = Guid.NewGuid(),
                                  Id = _riskAssessment.Id,
                                  AssessmentDate = DateTime.Now,
                                  RiskAssessorId = _riskAssessor.Id,
                                  SiteId = _site.Id
                              };

            // When
            _target.UpdateRiskAssessment(request);

            // Then
            Assert.IsNotNull(_riskAssessment.RiskAssessmentSite);
            Assert.AreEqual(request.SiteId, _riskAssessment.RiskAssessmentSite.Id);

            Assert.IsNotNull(_riskAssessment.RiskAssessor);
            Assert.AreEqual(request.RiskAssessorId, _riskAssessment.RiskAssessor.Id);
        }
        public void Given_site_not_set_When_UpdateRiskAssessment_Then_site_is_null()
        {
            // Given
            var request = new SavePersonalRiskAssessmentRequest()
            {
                CompanyId = _riskAssessment.CompanyId,
                Reference = "Reference",
                Title = "Title",
                UserId = Guid.NewGuid(),
                Id = _riskAssessment.Id,
                AssessmentDate = DateTime.Now,
                RiskAssessorId = 252L,
                SiteId = null
            };

            // When
            _target.UpdateRiskAssessment(request);

            // Then
            Assert.IsNull(_riskAssessment.RiskAssessmentSite);
        }
        public void Given_values_have_changed_When_UpdateRiskAssessment_Then_GRA_updated()
        {
            // Given
            // Given
            var request = new SavePersonalRiskAssessmentRequest()
            {
                CompanyId = _riskAssessment.CompanyId,
                Reference = "new Reference",
                Title = "new Title",
                UserId = Guid.NewGuid(),
                Id = _riskAssessment.Id,
                AssessmentDate = DateTime.Now.Date,
                RiskAssessorId = _riskAssessor.Id,
                SiteId = _site.Id,
                Location = "the new location",
                TaskProcessDescription = "the new task process description"
                ,Sensitive = true
            };

            // When
            _target.UpdateRiskAssessment(request);

            //then
            Assert.AreEqual(request.Reference, _riskAssessment.Reference);
            Assert.AreEqual(request.Title, _riskAssessment.Title);
            Assert.AreEqual(request.AssessmentDate, _riskAssessment.AssessmentDate);
            Assert.AreEqual(request.Location, _riskAssessment.Location);
            Assert.AreEqual(request.TaskProcessDescription, _riskAssessment.TaskProcessDescription);
            Assert.AreEqual(_riskAssessor.Id, _riskAssessment.RiskAssessor.Id);
            Assert.AreEqual(_site.Id, _riskAssessment.RiskAssessmentSite.Id);
            Assert.AreEqual(request.Sensitive, _riskAssessment.Sensitive);
        }
        public void Given_GRA_With_Reference_X_When_Updating_Another_GRA_To_Set_Reference_To_X_Then_Throw_Error()
        {
            // Given
            var request = new SavePersonalRiskAssessmentRequest()
            {
                CompanyId = _riskAssessment.CompanyId,
                Reference = "Reference",
                Title = "Title",
                UserId = Guid.NewGuid(),
                Id = _riskAssessment.Id,
                AssessmentDate = DateTime.Now,
                RiskAssessorId = 252L,
                SiteId = 200
            };

            _riskAssessmentRepository
                .Setup(x => x.DoesAssessmentExistWithTheSameReference<PersonalRiskAssessment>(It.IsAny<long>(), It.IsAny<string>(), It.IsAny<long?>()))
                .Returns(true);

            // When
            // Then
            Assert.Throws<ValidationException>(() => _target.UpdateRiskAssessment(request));
        }
        /// <summary>
        /// this is used by the API
        /// </summary>
        /// <param name="request"></param>
        public void UpdateRiskAssessment(SavePersonalRiskAssessmentRequest request)
        {
            var user = _userForAuditingRepository.GetByIdAndCompanyId(request.UserId, request.CompanyId);
            var riskAssessor = request.RiskAssessorId.HasValue ? _riskAssessorRepository.GetByIdAndCompanyId(request.RiskAssessorId.Value, request.CompanyId) : null;
            var site = request.SiteId.HasValue ? _siteRepository.GetByIdAndCompanyId(request.SiteId.Value, request.CompanyId) : null;
            var riskAssessment = _personalRiskAssessmentRepository.GetByIdAndCompanyId(request.Id, request.CompanyId, request.UserId);

            if (!riskAssessment.CanUserAccess(request.UserId))
            {
                throw new Exception("Invalid attempt to access Personal Risk Assessment with Id " + request.Id);
            }

            new UpdateRiskAssessmentValidator<PersonalRiskAssessment>(_riskAssessmentRepository).ValidateAndThrow(request);
            riskAssessment.UpdateSummary(request.Title, request.Reference, request.AssessmentDate, riskAssessor, request.Sensitive, site, user);
            riskAssessment.UpdatePremisesInformation(request.Location,request.TaskProcessDescription,user);
            _personalRiskAssessmentRepository.SaveOrUpdate(riskAssessment);
        }