public void Given_valid_post_When_CreateFurtherControlMeasureTask_Then_mapped_request_to_service_has_correct_values_and_returns_json()
        {
            // Given
            var controller = GetTarget();
            var returnedTaskDto = new HazardousSubstanceRiskAssessmentFurtherControlMeasureTaskDto()
            {
                Id = 123,
                Title = "title",
                Description = "description",
                CreatedDate = "23/06/1980",
                TaskCompletionDueDate = "23/06/2016",
                TaskAssignedTo = new EmployeeDto()
                {
                    Id = Guid.NewGuid(),
                    FullName = "Marcellas Wallace"
                }
            };
            var passedSaveFurtherControlMeasureTaskRequest = new SaveFurtherControlMeasureTaskRequest();

            _furtherControlMeasureTaskService
                .Setup(x => x.AddFurtherControlMeasureTask(It.IsAny<SaveFurtherControlMeasureTaskRequest>()))
                .Returns(returnedTaskDto)
                .Callback<SaveFurtherControlMeasureTaskRequest>(y => passedSaveFurtherControlMeasureTaskRequest = y);

            // When
            var postedAddEditFurtherControlMeasureTaskViewModel = new AddHazardousSubstanceFurtherControlMeasureTaskViewModel()
                                                                  {
                                                                      RiskAssessmentId = 1,
                                                                      Reference = "Test",
                                                                      Description = "Test",
                                                                      Title = "Test",
                                                                      CompanyId = 1,
                                                                      TaskAssignedToId = new Guid(),
                                                                      TaskCompletionDueDate = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"),
                                                                      ExistingDocuments = new ExistingDocumentsViewModel()
                                                                  };

            var result = controller.NewFurtherControlMeasureTask(postedAddEditFurtherControlMeasureTaskViewModel, new DocumentsToSaveViewModel());

            // Then
            _furtherControlMeasureTaskService
                .Verify(x => x.AddFurtherControlMeasureTask(It.IsAny<SaveFurtherControlMeasureTaskRequest>()));
            Assert.That(passedSaveFurtherControlMeasureTaskRequest.RiskAssessmentId, Is.EqualTo(postedAddEditFurtherControlMeasureTaskViewModel.RiskAssessmentId));
            Assert.That(passedSaveFurtherControlMeasureTaskRequest.Reference, Is.EqualTo(postedAddEditFurtherControlMeasureTaskViewModel.Reference));
            Assert.That(passedSaveFurtherControlMeasureTaskRequest.Description, Is.EqualTo(postedAddEditFurtherControlMeasureTaskViewModel.Description));
            Assert.That(passedSaveFurtherControlMeasureTaskRequest.Title, Is.EqualTo(postedAddEditFurtherControlMeasureTaskViewModel.Title));
            Assert.That(passedSaveFurtherControlMeasureTaskRequest.CompanyId, Is.EqualTo(postedAddEditFurtherControlMeasureTaskViewModel.CompanyId));
            Assert.That(passedSaveFurtherControlMeasureTaskRequest.TaskAssignedToId, Is.EqualTo(postedAddEditFurtherControlMeasureTaskViewModel.TaskAssignedToId));
            Assert.That(passedSaveFurtherControlMeasureTaskRequest.TaskCompletionDueDate.Value.ToString("dd/MM/yyyy HH:mm:ss"), Is.EqualTo(postedAddEditFurtherControlMeasureTaskViewModel.TaskCompletionDueDate));


            dynamic data = result.Data;
            var success = data.GetType().GetProperty("Success").GetValue(data, null);
            var id = data.GetType().GetProperty("Id").GetValue(data, null);
            var createdDate = data.GetType().GetProperty("CreatedOn").GetValue(data, null);

            Assert.That(success, Is.EqualTo(true));
            Assert.That(id, Is.EqualTo(returnedTaskDto.Id));
            Assert.That(createdDate, Is.EqualTo(returnedTaskDto.CreatedDate));
        }
        public HazardousSubstanceRiskAssessmentFurtherControlMeasureTaskDto AddFurtherControlMeasureTask(SaveFurtherControlMeasureTaskRequest request)
        {
            _log.Add(request);

            var creatingUser = _userForAuditingRepository.GetByIdAndCompanyId(request.UserId, request.CompanyId);
            var riskAssessment = _riskAssessmentRepository.GetByIdAndCompanyId(request.RiskAssessmentId, request.CompanyId);
            
            var furtherControlMeasureTask = HazardousSubstanceRiskAssessmentFurtherControlMeasureTask.Create(
                request.Reference,
                request.Title,
                request.Description,
                request.TaskCompletionDueDate,
                request.TaskStatus,
                _employeeRepository.GetByIdAndCompanyId(request.TaskAssignedToId, request.CompanyId),
                creatingUser,
                _documentParameterHelper.GetCreateDocumentParameters(request.CreateDocumentRequests, request.CompanyId),
                _responsibilityTaskCategoryRepository.GetHazardousSubstanceRiskAssessmentTaskCategory(),
                request.TaskReoccurringTypeId,
                request.TaskReoccurringEndDate,
                request.SendTaskNotification,
                request.SendTaskCompletedNotification,
                request.SendTaskOverdueNotification,
                request.SendTaskDueTomorrowNotification,
                request.TaskGuid
            );
            
            furtherControlMeasureTask.HazardousSubstanceRiskAssessment = riskAssessment;
            riskAssessment.AddFurtherControlMeasureTask(furtherControlMeasureTask, creatingUser);
            _riskAssessmentRepository.SaveOrUpdate(riskAssessment);
            _riskAssessmentRepository.Flush();
            var dto = new HazardousSubstanceRiskAssessmentFurtherControlMeasureTaskDtoMapper().MapWithAssignedToAndRiskAssessment(furtherControlMeasureTask);
            return dto;
        }
        public static SaveFurtherControlMeasureTaskRequest Create(
            string title,
            string description,
            string reference,
            string taskCompletionDueDate,
            int taskStatusId,
            long companyId,
            long riskAssessmentId,
            Guid taskAssignedToId,
            int taskReoccurringTypeId,
            string taskReoccurringFirstDueDate,
            DateTime? taskReoccurringEndDate,
            Guid userId,
            List<CreateDocumentRequest> createDocumentRequests,
            List<long> deleteDocumentRequests,
            bool sendTaskNotification,
            bool sendTaskCompletedNotification,
            bool sendTaskOverdueNotification,
            bool SendTaskDueTomorrowNotification,
            Guid taskGuid
            )
        {
            var request = new SaveFurtherControlMeasureTaskRequest();

            request.Title = title;
            request.Description = description;
            request.Reference = reference;
            request.TaskStatus = (TaskStatus)taskStatusId;
            request.CompanyId = companyId;
            request.RiskAssessmentId = riskAssessmentId;
            request.TaskAssignedToId = taskAssignedToId;
            request.TaskReoccurringTypeId = taskReoccurringTypeId;
            request.TaskReoccurringEndDate = taskReoccurringEndDate;
            request.UserId = userId;
            request.CreateDocumentRequests = createDocumentRequests;
            request.DocumentLibraryIdsToDelete = deleteDocumentRequests;
            request.SendTaskNotification = sendTaskNotification;
            request.SendTaskCompletedNotification = sendTaskCompletedNotification;
            request.SendTaskOverdueNotification = sendTaskOverdueNotification;
            request.SendTaskDueTomorrowNotification = sendTaskCompletedNotification;
            request.TaskGuid = taskGuid;

            if (request.IsReoccurringTask)
            {
                request.TaskCompletionDueDate = string.IsNullOrEmpty(taskReoccurringFirstDueDate)
                                        ? (DateTime?)null
                                        : DateTime.Parse(taskReoccurringFirstDueDate);
            }
            else
            {
                request.TaskCompletionDueDate = string.IsNullOrEmpty(taskCompletionDueDate)
                                            ? (DateTime?)null
                                            : DateTime.Parse(taskCompletionDueDate);
            }


            return request;
        }
        public void When_AddFurtherControlMeasureTask_Then_associated_risk_assessment_saved()
        {
            // Given
            var creatingUserId = Guid.NewGuid();
            var assignedToEmployeeId = Guid.NewGuid();

            var saveRequest = new SaveFurtherControlMeasureTaskRequest()
                              {
                                  RiskAssessmentId = 1234,
                                  CompanyId = 5678,
                                  Title = "new title",
                                  Reference = "new reference",
                                  Description = "new description",
                                  UserId = creatingUserId,
                                  TaskAssignedToId = assignedToEmployeeId,
                                  TaskCompletionDueDate = DateTime.Now
                              };

            _userRepository
                .Setup(x => x.GetByIdAndCompanyId(It.IsAny<Guid>(), It.IsAny<long>()))
                .Returns(new UserForAuditing() { Id = creatingUserId });

            _employeeRepository
                .Setup(x => x.GetByIdAndCompanyId(It.IsAny<Guid>(), It.IsAny<long>()))
                .Returns(new Employee() { Id = assignedToEmployeeId });

            var returnedRiskAssessment = new Mock<HazardousSubstanceRiskAssessment>();
            returnedRiskAssessment.Setup(x => x.Id).Returns(1234);
            returnedRiskAssessment.Setup(x => x.Reviews).Returns(new List<RiskAssessmentReview>());

            _riskAssessmentRepository
                .Setup(x => x.GetByIdAndCompanyId(It.IsAny<long>(), It.IsAny<long>()))
                .Returns(returnedRiskAssessment.Object);

            var passedFurtherControlMeasureTask = new HazardousSubstanceRiskAssessmentFurtherControlMeasureTask();

            returnedRiskAssessment
                .Setup(x => x.AddFurtherControlMeasureTask(It.IsAny<HazardousSubstanceRiskAssessmentFurtherControlMeasureTask>(), It.IsAny<UserForAuditing>()))
                .Callback<HazardousSubstanceRiskAssessmentFurtherControlMeasureTask, UserForAuditing>((task, user) => passedFurtherControlMeasureTask = task);

            // When
            _target.AddFurtherControlMeasureTask(saveRequest);

            // Then
            _riskAssessmentRepository.Verify(x => x.GetByIdAndCompanyId(saveRequest.RiskAssessmentId, saveRequest.CompanyId));
            _riskAssessmentRepository.Verify(x => x.SaveOrUpdate(It.IsAny<Domain.Entities.HazardousSubstanceRiskAssessment>()));
            _responsibilityTaskCategoryRepository.Verify(x => x.GetHazardousSubstanceRiskAssessmentTaskCategory(), Times.Once());
            _userRepository.Verify(x => x.GetByIdAndCompanyId(creatingUserId, saveRequest.CompanyId));
            returnedRiskAssessment.Verify(x => x.AddFurtherControlMeasureTask(It.IsAny<HazardousSubstanceRiskAssessmentFurtherControlMeasureTask>(), It.IsAny<UserForAuditing>()));

            Assert.That(passedFurtherControlMeasureTask.Title, Is.EqualTo(saveRequest.Title));
            Assert.That(passedFurtherControlMeasureTask.Reference, Is.EqualTo(saveRequest.Reference));
            Assert.That(passedFurtherControlMeasureTask.Description, Is.EqualTo(saveRequest.Description));
            Assert.That(passedFurtherControlMeasureTask.CreatedBy.Id, Is.EqualTo(saveRequest.UserId));
            Assert.That(passedFurtherControlMeasureTask.TaskAssignedTo.Id, Is.EqualTo(saveRequest.TaskAssignedToId));
            Assert.That(passedFurtherControlMeasureTask.TaskCompletionDueDate, Is.EqualTo(saveRequest.TaskCompletionDueDate));
            Assert.That(passedFurtherControlMeasureTask.Category.Id, Is.EqualTo(6));
        }
        public void When_AddFurtherControlMeasureTask_Then_Returned_DTO_Has_Correct_Values()
        {
            // Given
            var creatingUserId = Guid.NewGuid();
            var assignedToEmployeeId = Guid.NewGuid();

            var saveRequest = new SaveFurtherControlMeasureTaskRequest()
            {
                RiskAssessmentId = 1234,
                CompanyId = 5678,
                Title = "new title",
                Reference = "new reference",
                Description = "new description",
                UserId = creatingUserId,
                TaskAssignedToId = assignedToEmployeeId,
                TaskCompletionDueDate = DateTime.Now
            };

            _userRepository
                .Setup(x => x.GetByIdAndCompanyId(It.IsAny<Guid>(), It.IsAny<long>()))
                .Returns(new UserForAuditing() { Id = creatingUserId });

            _employeeRepository
                .Setup(x => x.GetByIdAndCompanyId(It.IsAny<Guid>(), It.IsAny<long>()))
                .Returns(new Employee() { Id = assignedToEmployeeId });

            var returnedRiskAssessment = new Mock<HazardousSubstanceRiskAssessment>();
            returnedRiskAssessment.Setup(x => x.Id).Returns(1234);
            returnedRiskAssessment.Setup(x => x.Reviews).Returns(new List<RiskAssessmentReview>());

            _riskAssessmentRepository
                .Setup(x => x.GetByIdAndCompanyId(It.IsAny<long>(), It.IsAny<long>()))
                .Returns(returnedRiskAssessment.Object);

            var passedFurtherControlMeasureTask = new HazardousSubstanceRiskAssessmentFurtherControlMeasureTask();

            returnedRiskAssessment
                .Setup(x => x.AddFurtherControlMeasureTask(It.IsAny<HazardousSubstanceRiskAssessmentFurtherControlMeasureTask>(), It.IsAny<UserForAuditing>()))
                .Callback<HazardousSubstanceRiskAssessmentFurtherControlMeasureTask, UserForAuditing>((task, user) => passedFurtherControlMeasureTask = task);

            // When
            var result = _target.AddFurtherControlMeasureTask(saveRequest);

            // Then
            Assert.That(DateTime.Parse(result.CreatedDate), Is.GreaterThan(DateTime.MinValue));
        }
        public void When_AddFurtherControlMeasureTask_Then_log_request()
        {
            // Given
            var saveRequest = new SaveFurtherControlMeasureTaskRequest();

            // When
            _target.AddFurtherControlMeasureTask(saveRequest);

            // Then
            _log.Verify(x => x.Add(saveRequest));
        }