public void When_Complete_Set_Complete_Date()
        {
            // Given
            EmployeeChecklist passedEmployeeChecklist = null;

            _employeeChecklistrepo
                .Setup(x => x.SaveOrUpdate(It.IsAny<EmployeeChecklist>()))
                .Callback<EmployeeChecklist>(y => passedEmployeeChecklist = y);

            var returnedEmployeeChecklist = new Mock<EmployeeChecklist>() { CallBase = true };
            returnedEmployeeChecklist.Setup(x => x.AreAllQuestionsAnswered()).Returns(true);
            returnedEmployeeChecklist.Setup((x=> x.Id)).Returns(Guid.NewGuid());

             _employeeChecklistrepo
                .Setup(x => x.GetById(It.IsAny<Guid>()))
                .Returns(returnedEmployeeChecklist.Object);

             _questionRepository
                 .Setup(x => x.GetById(It.IsAny<long>()))
                 .Returns(new Question());

            var request = new CompleteEmployeeChecklistRequest()
                          {
                              EmployeeChecklistId = returnedEmployeeChecklist.Object.Id,
                              Answers = new List<SubmitAnswerRequest>
                              {
                                  new SubmitAnswerRequest
                                      {
                                          QuestionId = 1L,
                                          BooleanResponse = true,
                                          AdditionalInfo = "Additional Info 1"
                                      },
                                  new SubmitAnswerRequest
                                      {
                                          QuestionId = 2L,
                                          BooleanResponse = false,
                                          AdditionalInfo = "Additional Info 2"
                                      },
                              },
                              CompletedDate = DateTime.Now
                          };

            var target = GetTarget();

            // When
            target.Complete(request);

            // Then
            Assert.That(passedEmployeeChecklist.CompletedDate, Is.Not.Null);
            Assert.That(passedEmployeeChecklist.CompletedDate, Is.EqualTo(request.CompletedDate));
            Assert.AreEqual(passedEmployeeChecklist.Id, request.EmployeeChecklistId);
        }
        public void When_ValidateComplete_is_called_Then_correct_methods_are_called()
        {
            var employeeChecklistId = Guid.NewGuid();
            var employeeChecklist = new Mock<EmployeeChecklist>();
            var validationMessages = new ValidationMessageCollection();

            var request = new CompleteEmployeeChecklistRequest
                              {
                                  EmployeeChecklistId = employeeChecklistId
                              };

            _employeeChecklistrepo
                .Setup(x => x.GetById(employeeChecklistId))
                .Returns(employeeChecklist.Object);

            employeeChecklist
                .Setup(x => x.ValidateComplete())
                .Returns(validationMessages);

            GetTarget().ValidateComplete(request);
            _employeeChecklistrepo.Verify(x => x.GetById(employeeChecklistId));
            employeeChecklist.Verify(x => x.ValidateComplete());
        }
        public void Complete(CompleteEmployeeChecklistRequest request)
        {
            //verify that all questions have been entered, then throw exception?????
            try
            {
                var empChecklist = _employeeChecklistRepository.GetById(request.EmployeeChecklistId);


                if (empChecklist == null)
                {
                    throw new Exception(string.Format("Employee checklist not found with id:{0}", request.EmployeeChecklistId.ToString()));
                }

                if (empChecklist.CompletedDate.HasValue) return;


                var systemUser = _auditedUserRepository.GetSystemUser();
                var submitAnswerParameterClasses = new List<SubmitPersonalAnswerParameters>();

                // TODO : Can we do something better than this
                var completedOnEmployeesBehalfBy = request.CompletedOnEmployeesBehalfBy.HasValue
                                                       ? _userRepository.GetById(request.CompletedOnEmployeesBehalfBy.Value)
                                                       : null;

       

                foreach (var submitAnswerRequest in request.Answers)
                {
                    var question = _questionRepository.GetById(submitAnswerRequest.QuestionId);

                    submitAnswerParameterClasses.Add(new SubmitPersonalAnswerParameters
                    {
                        AdditionalInfo = submitAnswerRequest.AdditionalInfo,
                        BooleanResponse = submitAnswerRequest.BooleanResponse,
                        Question = question
                    });
                }

                empChecklist.Complete(submitAnswerParameterClasses, completedOnEmployeesBehalfBy, systemUser, request.CompletedDate);

                _employeeChecklistRepository.SaveOrUpdate(empChecklist);
            }
            catch (Exception ex)
            {
                _log.Add(ex);
                throw;
            }
        }
 public ValidationMessageCollection ValidateComplete(CompleteEmployeeChecklistRequest request)
 {
     try
     {
         var employeeChecklist = _employeeChecklistRepository.GetById(request.EmployeeChecklistId);
         var validationMessages = employeeChecklist.ValidateComplete();
         return validationMessages;
     }
     catch (Exception ex)
     {
         _log.Add(ex);
         throw;
     }
 }
 private static CompleteEmployeeChecklist CreateCompleteEmployeeChecklistCommand(CompleteEmployeeChecklistRequest request)
 {
     var completeCommand = new CompleteEmployeeChecklist
                               {
                                   Answers =
                                       request.Answers.Select(
                                           a =>
                                           new SubmitAnswer
                                               {
                                                   AdditionalInfo = a.AdditionalInfo,
                                                   BooleanResponse = a.BooleanResponse,
                                                   QuestionId = a.QuestionId
                                               }).ToList()
                                   ,
                                   CompletedOnBehalf = request.CompletedOnBehalf,
                                   CompletedOnEmployeesBehalfBy = request.CompletedOnEmployeesBehalfBy,
                                   EmployeeChecklistId = request.EmployeeChecklistId,
                                   CompletedDate =  DateTime.Now
                               };
     return completeCommand;
 }
        public ActionResult Complete(EmployeeChecklistViewModel model, FormCollection formCollection)
        {
            var request = new CompleteEmployeeChecklistRequest
            {
                EmployeeChecklistId = model.EmployeeChecklistId,
                Answers = GetListOfAnswers(model)
            };

            var authenticationTicket = GetAuthenticationTicket();

            //todo: why isn't it binding to model.CompletedOnEmployeesBehalf ???
            if (Convert.ToBoolean(formCollection["CompletedOnEmployeesBehalf"]) && authenticationTicket != null)
            {
                request.CompletedOnEmployeesBehalfBy = new Guid(authenticationTicket.Name);
            }

            var validationMessages = _employeeChecklistService.ValidateComplete(request);

            if(validationMessages.Any())
            {
                validationMessages.ForEach(message => ModelState.AddModelError("", message.Text));
                return View("Index", model);
            }

          
            var completeCommandMessage = CreateCompleteEmployeeChecklistCommand(request);
            _bus.Send(completeCommandMessage);
            
            return RedirectToAction("Complete");
        }
        public void When_Complete_Retrives_Checklist_from_repository()
        {
            // Given
            EmployeeChecklist passedEmployeeChecklist = null;

            _employeeChecklistrepo
                .Setup(x => x.SaveOrUpdate(It.IsAny<EmployeeChecklist>()))
                .Callback<EmployeeChecklist>(y => passedEmployeeChecklist = y);

            var returnedEmployeeChecklist = new Mock<EmployeeChecklist>() { CallBase = true };
            returnedEmployeeChecklist.Setup(x => x.AreAllQuestionsAnswered()).Returns(true);
            returnedEmployeeChecklist.Setup((x => x.Id)).Returns(Guid.NewGuid());

            _employeeChecklistrepo
                .Setup(x => x.GetById(It.IsAny<Guid>()))
                .Returns(returnedEmployeeChecklist.Object);

            _questionRepository
                .Setup(x => x.GetById(It.IsAny<long>()))
                .Returns(new Question());

            var request = new CompleteEmployeeChecklistRequest()
            {
                EmployeeChecklistId = Guid.NewGuid(),
                Answers = new List<SubmitAnswerRequest>
                              {
                                  new SubmitAnswerRequest
                                      {
                                          QuestionId = 1L,
                                          BooleanResponse = true,
                                          AdditionalInfo = "Additional Info 1"
                                      },
                                  new SubmitAnswerRequest
                                      {
                                          QuestionId = 2L,
                                          BooleanResponse = false,
                                          AdditionalInfo = "Additional Info 2"
                                      },
                              }
            };

            var target = GetTarget();

            // When
            target.Complete(request);

            // Then
           _employeeChecklistrepo.Verify(x=> x.GetById(request.EmployeeChecklistId));
           _questionRepository.Verify(x => x.GetById(1L), Times.Once());
           _questionRepository.Verify(x => x.GetById(2L), Times.Once());
        }