public ValidationResult ValidateFireRiskAssessmentChecklist(ValidateCompleteFireRiskAssessmentChecklistRequest request) { var result = new ValidationResult(); var correspondingAnswers = _fireAnswerRepository.GetByChecklistIdAndQuestionIds(request.ChecklistId, request.QuestionIds); foreach (var questionId in request.QuestionIds) { var answer = correspondingAnswers.SingleOrDefault(x => x.Question != null && x.Question.Id == questionId); if(answer == null) { result.Errors.Add(new ValidationFailure(questionId.ToString(), "Please select a response")); }else if(!answer.IsValidateForCompleteChecklist()) { string errorMessage = string.Empty; switch(answer.YesNoNotApplicableResponse) { case YesNoNotApplicableEnum.No: errorMessage = "Please add a Further Control Measure Task"; break; case YesNoNotApplicableEnum.Yes: errorMessage = "Please enter a comment"; break; } result.Errors.Add(new ValidationFailure(questionId.ToString(), errorMessage)); } } return result; }
public void Given_no_corresponding_answers_for_requested_question_ids_When_Validate_Then_add_error_for_each_one() { // Given var target = GetTarget(); var request = new ValidateCompleteFireRiskAssessmentChecklistRequest() { ChecklistId = 1234L, QuestionIds = new long[] { 123L, 456L, 789L } }; // When var result = target.ValidateFireRiskAssessmentChecklist(request); // Then Assert.That(result.Errors.Count, Is.EqualTo(request.QuestionIds.Count())); }
public void When_Validate_Then_retrieve_required_fire_answers() { // Given var target = GetTarget(); var request = new ValidateCompleteFireRiskAssessmentChecklistRequest() { ChecklistId = 1234L, QuestionIds = new long[] { 123L, 456L, 789L } }; // When target.ValidateFireRiskAssessmentChecklist(request); // Then _fireAnswerRepository.Verify(x => x.GetByChecklistIdAndQuestionIds(request.ChecklistId, request.QuestionIds)); }
public void Given_no_corresponding_answers_for_requested_question_ids_When_Validate_Then_set_question_id_for_each_one() { // Given var target = GetTarget(); var request = new ValidateCompleteFireRiskAssessmentChecklistRequest() { ChecklistId = 1234L, QuestionIds = new long[] { 123L, 456L, 789L } }; // When var result = target.ValidateFireRiskAssessmentChecklist(request); // Then for (var i = 0; i < result.Errors.Count; i++) { Assert.That(result.Errors.ElementAt(i).PropertyName, Is.EqualTo(request.QuestionIds.ElementAt(i).ToString())); } }
public void Given_an_invalid_yes_answer_for_requested_question_id_When_Validate_Then_error_for_yes_is_set() { // Given var target = GetTarget(); const long questionId = 123L; var invalidFireAnswer = new Mock<FireAnswer>(); invalidFireAnswer.Setup(x => x.IsValidateForCompleteChecklist()).Returns(false); invalidFireAnswer.Setup(x => x.YesNoNotApplicableResponse).Returns(YesNoNotApplicableEnum.Yes); invalidFireAnswer.Setup(x => x.AdditionalInfo).Returns(string.Empty); invalidFireAnswer.Setup(x => x.Question).Returns(new Question() { Id = questionId }); var request = new ValidateCompleteFireRiskAssessmentChecklistRequest() { ChecklistId = 1234L, QuestionIds = new[] { questionId } }; _fireAnswerRepository .Setup(x => x.GetByChecklistIdAndQuestionIds(It.IsAny<long>(), It.IsAny<IEnumerable<long>>())) .Returns(new List<FireAnswer>() { invalidFireAnswer.Object }); // When var result = target.ValidateFireRiskAssessmentChecklist(request); // Then Assert.That(result.Errors.First().PropertyName, Is.EqualTo(questionId.ToString(CultureInfo.InvariantCulture))); Assert.That(result.Errors.First().ErrorMessage, Is.EqualTo("Please enter a comment")); }
public void Given_a_valid_no_answer_for_requested_question_id_When_Validate_Then_no_error_is_set() { // Given var target = GetTarget(); const long questionId = 123L; var invalidFireAnswer = new Mock<FireAnswer>(); invalidFireAnswer.Setup(x => x.IsValidateForCompleteChecklist()).Returns(true); invalidFireAnswer.Setup(x => x.YesNoNotApplicableResponse).Returns(YesNoNotApplicableEnum.No); invalidFireAnswer.Setup(x => x.Question).Returns(new Question() { Id = questionId }); var request = new ValidateCompleteFireRiskAssessmentChecklistRequest() { ChecklistId = 1234L, QuestionIds = new long[] { questionId } }; _fireAnswerRepository .Setup(x => x.GetByChecklistIdAndQuestionIds(It.IsAny<long>(), It.IsAny<IEnumerable<long>>())) .Returns(new List<FireAnswer>() { invalidFireAnswer.Object }); // When var result = target.ValidateFireRiskAssessmentChecklist(request); // Then Assert.That(result.Errors.Count, Is.EqualTo(0)); }