Search in sources :

Example 6 with ValidationException

use of org.mifos.platform.validations.ValidationException in project head by mifos.

the class ValidationExceptionTest method shouldCopyChildExceptionsWithNoChildExceptions.

@Test
public void shouldCopyChildExceptionsWithNoChildExceptions() {
    ValidationException validationException1 = generateValidationException();
    ValidationException validationException2 = new ValidationException(TOP_LEVEL_EXCEPTION);
    validationException1.copyChildExceptions(validationException2);
    assertThat(validationException1.hasChildExceptions(), is(true));
    List<ValidationException> childExceptions = validationException1.getChildExceptions();
    assertThat(childExceptions.size(), is(4));
}
Also used : ValidationException(org.mifos.platform.validations.ValidationException) Test(org.junit.Test)

Example 7 with ValidationException

use of org.mifos.platform.validations.ValidationException 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 8 with ValidationException

use of org.mifos.platform.validations.ValidationException 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 9 with ValidationException

use of org.mifos.platform.validations.ValidationException 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 10 with ValidationException

use of org.mifos.platform.validations.ValidationException in project head by mifos.

the class QuestionnaireValidatorForDtoTest method shouldValidateForInvalidQuestionDto_MissingQuestionTitle.

@Test
public void shouldValidateForInvalidQuestionDto_MissingQuestionTitle() {
    when(eventSourceDao.retrieveCountByEventAndSource("Create", "Client")).thenReturn(asList(1L));
    QuestionGroupDto questionGroupDto = getQuestionGroupDto();
    QuestionDto questionDto = questionGroupDto.getSections().get(0).getQuestions().get(0);
    questionDto.setText(null);
    try {
        questionnaireValidator.validateForDefineQuestion(questionDto);
        fail("Should have thrown validationException");
    } catch (ValidationException e) {
        assertThat(e.getKey(), is(GENERIC_VALIDATION));
        assertThat(e.hasChildExceptions(), is(true));
        List<ValidationException> childExceptions = e.getChildExceptions();
        assertThat(childExceptions, is(notNullValue()));
        assertThat(childExceptions.size(), is(1));
        assertThat(childExceptions.get(0).getKey(), is(QUESTION_TEXT_NOT_PROVIDED));
    }
}
Also used : ValidationException(org.mifos.platform.validations.ValidationException) QuestionGroupDto(org.mifos.platform.questionnaire.service.dtos.QuestionGroupDto) QuestionDto(org.mifos.platform.questionnaire.service.dtos.QuestionDto) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Test(org.junit.Test)

Aggregations

ValidationException (org.mifos.platform.validations.ValidationException)58 Test (org.junit.Test)48 QuestionGroupDto (org.mifos.platform.questionnaire.service.dtos.QuestionGroupDto)30 Arrays.asList (java.util.Arrays.asList)27 List (java.util.List)27 BadNumericResponseException (org.mifos.platform.questionnaire.exceptions.BadNumericResponseException)8 QuestionGroupDetail (org.mifos.platform.questionnaire.service.QuestionGroupDetail)7 MessageMatcher (org.mifos.platform.matchers.MessageMatcher)6 MandatoryAnswerNotFoundException (org.mifos.platform.questionnaire.exceptions.MandatoryAnswerNotFoundException)6 QuestionDetail (org.mifos.platform.questionnaire.service.QuestionDetail)5 SectionDetail (org.mifos.platform.questionnaire.service.SectionDetail)5 SectionQuestionDetail (org.mifos.platform.questionnaire.service.SectionQuestionDetail)5 QuestionChoiceEntity (org.mifos.platform.questionnaire.domain.QuestionChoiceEntity)3 EventSourceDto (org.mifos.platform.questionnaire.service.dtos.EventSourceDto)3 QuestionnaireServiceFacade (org.mifos.platform.questionnaire.service.QuestionnaireServiceFacade)2 QuestionDto (org.mifos.platform.questionnaire.service.dtos.QuestionDto)2 MessageResolver (org.springframework.binding.message.MessageResolver)2 ActionErrors (org.apache.struts.action.ActionErrors)1 ActionMessage (org.apache.struts.action.ActionMessage)1 Before (org.junit.Before)1