Search in sources :

Example 1 with MandatoryAnswerNotFoundException

use of org.mifos.platform.questionnaire.exceptions.MandatoryAnswerNotFoundException in project head by mifos.

the class QuestionnaireFlowAdapter method validateResponses.

public ActionErrors validateResponses(HttpServletRequest request, QuestionResponseCapturer form) {
    List<QuestionGroupDetail> groups = form.getQuestionGroups();
    QuestionnaireServiceFacade questionnaireServiceFacade = serviceLocator.getService(request);
    if ((groups == null) || (questionnaireServiceFacade == null)) {
        return null;
    }
    ActionErrors errors = new ActionErrors();
    try {
        questionnaireServiceFacade.validateResponses(groups);
    } catch (ValidationException e) {
        if (e.hasChildExceptions()) {
            for (ValidationException ve : e.getChildExceptions()) {
                if (ve instanceof MandatoryAnswerNotFoundException) {
                    errors.add(ClientConstants.ERROR_REQUIRED, new ActionMessage(ClientConstants.ERROR_REQUIRED, ve.getIdentifier()));
                } else if (ve instanceof BadNumericResponseException) {
                    populateNumericError((BadNumericResponseException) ve, errors);
                }
            }
        }
    }
    setQuestionnaireAttributesToRequest(request, form);
    return errors;
}
Also used : BadNumericResponseException(org.mifos.platform.questionnaire.exceptions.BadNumericResponseException) QuestionGroupDetail(org.mifos.platform.questionnaire.service.QuestionGroupDetail) ValidationException(org.mifos.platform.validations.ValidationException) MandatoryAnswerNotFoundException(org.mifos.platform.questionnaire.exceptions.MandatoryAnswerNotFoundException) ActionMessage(org.apache.struts.action.ActionMessage) QuestionnaireServiceFacade(org.mifos.platform.questionnaire.service.QuestionnaireServiceFacade) ActionErrors(org.apache.struts.action.ActionErrors)

Example 2 with MandatoryAnswerNotFoundException

use of org.mifos.platform.questionnaire.exceptions.MandatoryAnswerNotFoundException in project head by mifos.

the class QuestionnaireServiceFacadeTest method testValidateResponse.

@Test
public void testValidateResponse() {
    List<QuestionDetail> questionDetails = asList(new QuestionDetail(12, "Question 1", QuestionType.FREETEXT, true, true));
    List<SectionDetail> sectionDetails = asList(getSectionDetailWithQuestions("Sec1", questionDetails, null, true));
    QuestionGroupDetail questionGroupDetail = new QuestionGroupDetail(1, "QG1", Arrays.asList(new EventSourceDto("Create", "Client", null)), sectionDetails, true);
    try {
        Mockito.doThrow(new MandatoryAnswerNotFoundException("Title")).when(questionnaireService).validateResponses(asList(questionGroupDetail));
        questionnaireServiceFacade.validateResponses(asList(questionGroupDetail));
        Assert.fail("Should not have thrown the validation exception");
    } catch (ValidationException e) {
        verify(questionnaireService, times(1)).validateResponses(asList(questionGroupDetail));
    }
}
Also used : ValidationException(org.mifos.platform.validations.ValidationException) MandatoryAnswerNotFoundException(org.mifos.platform.questionnaire.exceptions.MandatoryAnswerNotFoundException) EventSourceDto(org.mifos.platform.questionnaire.service.dtos.EventSourceDto) Test(org.junit.Test)

Example 3 with MandatoryAnswerNotFoundException

use of org.mifos.platform.questionnaire.exceptions.MandatoryAnswerNotFoundException in project head by mifos.

the class QuestionnaireValidatorTest method shouldThrowExceptionWhenAMandatoryQuestionHasNoAnswer.

@SuppressWarnings({ "ThrowableResultOfMethodCallIgnored" })
@Test
public void shouldThrowExceptionWhenAMandatoryQuestionHasNoAnswer() {
    QuestionGroupDetail questionGroupDetail = getQuestionGroupDetail(0, "Title", "Create", "Client");
    try {
        questionnaireValidator.validateForQuestionGroupResponses(asList(questionGroupDetail));
        fail("Should have thrown the validation exception");
    } catch (ValidationException e) {
        assertEquals(GENERIC_VALIDATION, e.getKey());
        assertEquals(true, e.hasChildExceptions());
        assertEquals(1, e.getChildExceptions().size());
        ValidationException childException = e.getChildExceptions().get(0);
        assertTrue(childException instanceof MandatoryAnswerNotFoundException);
        assertEquals("Q1", childException.getIdentifier());
    }
}
Also used : QuestionGroupDetail(org.mifos.platform.questionnaire.service.QuestionGroupDetail) ValidationException(org.mifos.platform.validations.ValidationException) MandatoryAnswerNotFoundException(org.mifos.platform.questionnaire.exceptions.MandatoryAnswerNotFoundException) Test(org.junit.Test)

Example 4 with MandatoryAnswerNotFoundException

use of org.mifos.platform.questionnaire.exceptions.MandatoryAnswerNotFoundException in project head by mifos.

the class QuestionGroupControllerTest method testSaveQuestionnaireFailure.

@Test
public void testSaveQuestionnaireFailure() {
    ValidationException validationException = new ValidationException(GENERIC_VALIDATION);
    validationException.addChildException(new MandatoryAnswerNotFoundException("q1"));
    doThrow(validationException).when(questionnaireServiceFacade).saveResponses(Mockito.<QuestionGroupDetails>any());
    when(requestContext.getMessageContext()).thenReturn(messageContext);
    String result = questionGroupController.saveQuestionnaire(getQuestionGroupDetails(), 1, requestContext);
    assertThat(result, is("failure"));
    verify(requestContext, times(1)).getMessageContext();
    verify(messageContext).addMessage(argThat(new MessageMatcher("questionnaire.noresponse")));
}
Also used : MessageMatcher(org.mifos.platform.matchers.MessageMatcher) ValidationException(org.mifos.platform.validations.ValidationException) MandatoryAnswerNotFoundException(org.mifos.platform.questionnaire.exceptions.MandatoryAnswerNotFoundException) Test(org.junit.Test)

Example 5 with MandatoryAnswerNotFoundException

use of org.mifos.platform.questionnaire.exceptions.MandatoryAnswerNotFoundException in project head by mifos.

the class ValidationExceptionMessageExtractorTest method testMandatoryAnswerNotFoundException.

@Test
public void testMandatoryAnswerNotFoundException() {
    MandatoryAnswerNotFoundException e = new MandatoryAnswerNotFoundException("Title");
    ValidationException parent = new ValidationException("Key");
    parent.addChildException(e);
    ValidationExceptionMessageExtractor extractor = new ValidationExceptionMessageExtractor();
    extractor.extract(context, parent);
    verify(context).addMessage(any(MessageResolver.class));
}
Also used : ValidationException(org.mifos.platform.validations.ValidationException) MessageResolver(org.springframework.binding.message.MessageResolver) MandatoryAnswerNotFoundException(org.mifos.platform.questionnaire.exceptions.MandatoryAnswerNotFoundException) Test(org.junit.Test)

Aggregations

MandatoryAnswerNotFoundException (org.mifos.platform.questionnaire.exceptions.MandatoryAnswerNotFoundException)6 ValidationException (org.mifos.platform.validations.ValidationException)6 Test (org.junit.Test)5 QuestionGroupDetail (org.mifos.platform.questionnaire.service.QuestionGroupDetail)3 EventSourceDto (org.mifos.platform.questionnaire.service.dtos.EventSourceDto)2 ActionErrors (org.apache.struts.action.ActionErrors)1 ActionMessage (org.apache.struts.action.ActionMessage)1 MessageMatcher (org.mifos.platform.matchers.MessageMatcher)1 BadNumericResponseException (org.mifos.platform.questionnaire.exceptions.BadNumericResponseException)1 QuestionDetail (org.mifos.platform.questionnaire.service.QuestionDetail)1 QuestionnaireServiceFacade (org.mifos.platform.questionnaire.service.QuestionnaireServiceFacade)1 SectionDetail (org.mifos.platform.questionnaire.service.SectionDetail)1 SectionQuestionDetail (org.mifos.platform.questionnaire.service.SectionQuestionDetail)1 MessageResolver (org.springframework.binding.message.MessageResolver)1