Search in sources :

Example 1 with BadNumericResponseException

use of org.mifos.platform.questionnaire.exceptions.BadNumericResponseException 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 BadNumericResponseException

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

the class QuestionGroupControllerTest method testSaveQuestionnaireFailureForNumericResponseWithoutBounds.

@SuppressWarnings({ "ThrowableInstanceNeverThrown" })
@Test
public void testSaveQuestionnaireFailureForNumericResponseWithoutBounds() {
    ValidationException validationException = new ValidationException(GENERIC_VALIDATION);
    validationException.addChildException(new BadNumericResponseException("q1", null, null));
    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.invalid.numeric.response")));
}
Also used : MessageMatcher(org.mifos.platform.matchers.MessageMatcher) BadNumericResponseException(org.mifos.platform.questionnaire.exceptions.BadNumericResponseException) ValidationException(org.mifos.platform.validations.ValidationException) Test(org.junit.Test)

Example 3 with BadNumericResponseException

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

the class QuestionnaireValidatorTest method shouldThrowExceptionWhenANumericQuestionHasAnswerMoreThanMax.

@SuppressWarnings({ "ThrowableResultOfMethodCallIgnored" })
@Test
public void shouldThrowExceptionWhenANumericQuestionHasAnswerMoreThanMax() {
    QuestionDetail questionDetail = getNumericQuestionDetail("Numeric Question", null, 100);
    SectionDetail sectionWithOneQuestion = getSectionWithOneQuestion("Sec1", questionDetail, "101");
    QuestionGroupDetail questionGroupDetail = getQuestionGroupDetail(0, "Title", "Create", "Client", asList(sectionWithOneQuestion));
    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 BadNumericResponseException);
        assertEquals("Numeric Question", childException.getIdentifier());
        assertNull(((BadNumericResponseException) childException).getAllowedMinValue());
        assertEquals(Integer.valueOf(100), ((BadNumericResponseException) childException).getAllowedMaxValue());
    }
}
Also used : BadNumericResponseException(org.mifos.platform.questionnaire.exceptions.BadNumericResponseException) QuestionGroupDetail(org.mifos.platform.questionnaire.service.QuestionGroupDetail) ValidationException(org.mifos.platform.validations.ValidationException) SectionDetail(org.mifos.platform.questionnaire.service.SectionDetail) QuestionDetail(org.mifos.platform.questionnaire.service.QuestionDetail) SectionQuestionDetail(org.mifos.platform.questionnaire.service.SectionQuestionDetail) Test(org.junit.Test)

Example 4 with BadNumericResponseException

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

the class QuestionnaireValidatorTest method shouldThrowExceptionWhenANumericQuestionHasInvalidAnswer.

@SuppressWarnings({ "ThrowableResultOfMethodCallIgnored" })
@Test
public void shouldThrowExceptionWhenANumericQuestionHasInvalidAnswer() {
    QuestionDetail questionDetail = getNumericQuestionDetail("Numeric Question", 10, 100);
    SectionDetail sectionWithOneQuestion = getSectionWithOneQuestion("Sec1", questionDetail, "123ab");
    QuestionGroupDetail questionGroupDetail = getQuestionGroupDetail(0, "Title", "Create", "Client", asList(sectionWithOneQuestion));
    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 BadNumericResponseException);
        assertEquals("Numeric Question", childException.getIdentifier());
        assertEquals(Integer.valueOf(10), ((BadNumericResponseException) childException).getAllowedMinValue());
        assertEquals(Integer.valueOf(100), ((BadNumericResponseException) childException).getAllowedMaxValue());
    }
}
Also used : BadNumericResponseException(org.mifos.platform.questionnaire.exceptions.BadNumericResponseException) QuestionGroupDetail(org.mifos.platform.questionnaire.service.QuestionGroupDetail) ValidationException(org.mifos.platform.validations.ValidationException) SectionDetail(org.mifos.platform.questionnaire.service.SectionDetail) QuestionDetail(org.mifos.platform.questionnaire.service.QuestionDetail) SectionQuestionDetail(org.mifos.platform.questionnaire.service.SectionQuestionDetail) Test(org.junit.Test)

Example 5 with BadNumericResponseException

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

the class ValidationExceptionMessageExtractorTest method testBadNumericResponseExceptionLowerBoundOnly.

@Test
public void testBadNumericResponseExceptionLowerBoundOnly() {
    BadNumericResponseException e = new BadNumericResponseException("Title", 0, null);
    testBadNumericResponseException(e, "questionnaire.invalid.numeric.min.response");
}
Also used : BadNumericResponseException(org.mifos.platform.questionnaire.exceptions.BadNumericResponseException) Test(org.junit.Test)

Aggregations

BadNumericResponseException (org.mifos.platform.questionnaire.exceptions.BadNumericResponseException)12 Test (org.junit.Test)11 ValidationException (org.mifos.platform.validations.ValidationException)8 MessageMatcher (org.mifos.platform.matchers.MessageMatcher)4 QuestionGroupDetail (org.mifos.platform.questionnaire.service.QuestionGroupDetail)4 QuestionDetail (org.mifos.platform.questionnaire.service.QuestionDetail)3 SectionDetail (org.mifos.platform.questionnaire.service.SectionDetail)3 SectionQuestionDetail (org.mifos.platform.questionnaire.service.SectionQuestionDetail)3 ActionErrors (org.apache.struts.action.ActionErrors)1 ActionMessage (org.apache.struts.action.ActionMessage)1 MandatoryAnswerNotFoundException (org.mifos.platform.questionnaire.exceptions.MandatoryAnswerNotFoundException)1 QuestionnaireServiceFacade (org.mifos.platform.questionnaire.service.QuestionnaireServiceFacade)1