Search in sources :

Example 1 with BindException

use of org.springframework.validation.BindException in project cas by apereo.

the class AuthenticationViaFormActionTests method verifyFailedAuthenticationWithNoService.

@Test
public void verifyFailedAuthenticationWithNoService() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockRequestContext context = new MockRequestContext();
    request.addParameter(USERNAME_PARAM, TEST);
    request.addParameter(PASSWORD_PARAM, "test2");
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    final Credential c = CoreAuthenticationTestUtils.getCredentialsWithDifferentUsernameAndPassword();
    putCredentialInRequestScope(context, c);
    context.getRequestScope().put("org.springframework.validation.BindException.credentials", new BindException(c, "credential"));
    assertEquals(CasWebflowConstants.TRANSITION_ID_AUTHENTICATION_FAILURE, this.action.execute(context).getId());
}
Also used : Credential(org.apereo.cas.authentication.Credential) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) BindException(org.springframework.validation.BindException) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with BindException

use of org.springframework.validation.BindException in project head by mifos.

the class SavingsProductFormValidator method validate.

@SuppressWarnings({ "ThrowableInstanceNeverThrown" })
@Override
public void validate(Object target, Errors errors) {
    SavingsProductFormBean formBean = (SavingsProductFormBean) target;
    if (formBean.getGeneralDetails().getName().trim().isEmpty()) {
        errors.reject("NotEmpty.generalDetails.name");
    }
    if (formBean.getGeneralDetails().getDescription().length() > 200) {
        errors.reject("Size.generalDetails.description");
    }
    if (formBean.getGeneralDetails().getShortName().trim().isEmpty()) {
        errors.reject("NotEmpty.generalDetails.shortName");
    }
    if (formBean.getGeneralDetails().getSelectedCategory().trim().isEmpty()) {
        errors.reject("NotEmpty.generalDetails.selectedCategory");
    }
    if (null == formBean.getGeneralDetails().getStartDateDay() || 1 > formBean.getGeneralDetails().getStartDateDay() || 31 < formBean.getGeneralDetails().getStartDateDay()) {
        errors.reject("Min.generalDetails.startDateDay");
    }
    if (null == formBean.getGeneralDetails().getStartDateMonth() || 1 > formBean.getGeneralDetails().getStartDateMonth() || 12 < formBean.getGeneralDetails().getStartDateMonth()) {
        errors.reject("Min.generalDetails.startDateMonth");
    }
    if (null == formBean.getGeneralDetails().getStartDateAsDateTime() || !(formBean.getGeneralDetails().getStartDateYear().length() == 4)) {
        errors.reject("Min.generalDetails.startDate");
    } else {
        MutableDateTime nextYear = new MutableDateTime();
        nextYear.setDate(new Date().getTime());
        nextYear.addYears(1);
        if (formBean.getGeneralDetails().getStartDateAsDateTime() != null && formBean.getGeneralDetails().getStartDateAsDateTime().compareTo(nextYear) > 0) {
            errors.reject("Min.generalDetails.startDate");
        }
    }
    if ((null != formBean.getGeneralDetails().getEndDateDay() || null != formBean.getGeneralDetails().getEndDateMonth() || null != formBean.getGeneralDetails().getEndDateMonth()) && formBean.getGeneralDetails().getEndDateAsDateTime() == null) {
        errors.reject("Min.generalDetails.endDate");
    }
    if (formBean.getGeneralDetails().getStartDateAsDateTime() != null && formBean.getGeneralDetails().getEndDateAsDateTime() != null && formBean.getGeneralDetails().getStartDateAsDateTime().compareTo(formBean.getGeneralDetails().getEndDateAsDateTime()) > 0) {
        errors.reject("Min.generalDetails.endDate");
    }
    if (formBean.getGeneralDetails().getSelectedApplicableFor().trim().isEmpty()) {
        errors.reject("NotEmpty.generalDetails.selectedApplicableFor");
    }
    if (formBean.getSelectedDepositType().trim().isEmpty()) {
        errors.reject("NotEmpty.savingsProduct.selectedDepositType");
    } else if (formBean.isMandatory() && (null == formBean.getAmountForDeposit() || formBean.getAmountForDeposit() <= 0)) {
        errors.reject("Min.savingsProduct.amountForDesposit");
    }
    if (formBean.isGroupSavingAccount() && formBean.getSelectedGroupSavingsApproach().trim().isEmpty()) {
        errors.reject("NotEmpty.savingsProduct.selectedGroupSavingsApproach");
    }
    if (!formBean.isInterestRateZero()) {
        if (null == formBean.getInterestRate() || formBean.getInterestRate().doubleValue() < 1.0 || formBean.getInterestRate().doubleValue() > 100.0 || errorProcessor.getRejectedValue("interestRate") != null) {
            if (errorProcessor.getTarget() == null) {
                errors.reject("NotNull.savingsProduct.interestRate");
            } else {
                BindException bindException = new BindException(errorProcessor.getTarget(), errorProcessor.getObjectName());
                bindException.addError(new FieldError(errorProcessor.getObjectName(), "interestRate", errorProcessor.getRejectedValue("interestRate"), true, new String[] { "NotNull.savingsProduct.interestRate" }, null, null));
                errors.addAllErrors(bindException);
            }
        }
        if (formBean.getSelectedInterestCalculation().trim().isEmpty()) {
            errors.reject("NotEmpty.savingsProduct.selectedInterestCalculation");
        }
        if (null == formBean.getInterestCalculationFrequency() || formBean.getInterestCalculationFrequency() < 1 || errorProcessor.getRejectedValue("interestCalculationFrequency") != null) {
            if (errorProcessor.getTarget() == null) {
                errors.reject("NotNull.savingsProduct.interestCalculationFrequency");
            } else {
                BindException bindException = new BindException(errorProcessor.getTarget(), errorProcessor.getObjectName());
                bindException.addError(new FieldError(errorProcessor.getObjectName(), "interestCalculationFrequency", errorProcessor.getRejectedValue("interestCalculationFrequency"), true, new String[] { "NotNull.savingsProduct.interestCalculationFrequency" }, null, null));
                errors.addAllErrors(bindException);
            }
        }
        if (null == formBean.getInterestPostingMonthlyFrequency() || formBean.getInterestPostingMonthlyFrequency() < 1 || errorProcessor.getRejectedValue("interestPostingMonthlyFrequency") != null) {
            if (errorProcessor.getTarget() == null) {
                errors.reject("Min.savingsProduct.interestPostingMonthlyFrequency");
            } else {
                BindException bindException = new BindException(errorProcessor.getTarget(), errorProcessor.getObjectName());
                bindException.addError(new FieldError(errorProcessor.getObjectName(), "interestPostingMonthlyFrequency", errorProcessor.getRejectedValue("interestPostingMonthlyFrequency"), true, new String[] { "Min.savingsProduct.interestPostingMonthlyFrequency" }, null, null));
                errors.addAllErrors(bindException);
            }
        }
    }
    BigDecimal minBalanceForInterestCalculation;
    try {
        minBalanceForInterestCalculation = BigDecimal.valueOf(Double.valueOf(formBean.getMinBalanceRequiredForInterestCalculation()));
    } catch (NumberFormatException e) {
        minBalanceForInterestCalculation = new BigDecimal("-1");
    }
    if (minBalanceForInterestCalculation.compareTo(BigDecimal.ZERO) < 0) {
        errors.reject("Min.savingsProduct.balanceRequiredForInterestCalculation");
    }
    if (formBean.getSelectedPrincipalGlCode().trim().isEmpty()) {
        errors.reject("NotEmpty.savingsProduct.selectedPrincipalGlCode");
    }
    if (formBean.getSelectedInterestGlCode().trim().isEmpty()) {
        errors.reject("NotEmpty.savingsProduct.selectedInterestGlCode");
    }
    Short digsAfterDec = configurationServiceFacade.getAccountingConfiguration().getDigitsAfterDecimal();
    Short digsBeforeDec = configurationServiceFacade.getAccountingConfiguration().getDigitsBeforeDecimal();
    Double maxWithdrawalObj = formBean.getMaxWithdrawalAmount();
    String maxWithdrawalString;
    if (maxWithdrawalObj == null) {
        maxWithdrawalString = "";
    } else {
        maxWithdrawalString = maxWithdrawalObj.toString();
    }
    int dot = maxWithdrawalString.lastIndexOf('.');
    int max = digsAfterDec + digsBeforeDec + dot;
    int withdrawalLength = maxWithdrawalString.length();
    if (maxWithdrawalString.lastIndexOf(0, dot) > digsBeforeDec) {
        errors.reject("MaxDigitsBefore.savingProduct.withdrawal", new String[] { digsBeforeDec.toString() }, null);
    }
    if (maxWithdrawalString.lastIndexOf(dot, withdrawalLength) > digsAfterDec) {
        errors.reject("MaxDigitsAfter.savingProduct.withdrawal", new String[] { digsAfterDec.toString() }, null);
    }
    if (withdrawalLength > max) {
        errors.reject("MaxDigitsNumber.savingProduct.withdrawal", new String[] { String.valueOf(max) }, null);
    }
    Double amountForDepositObj = formBean.getAmountForDeposit();
    String amountForDepositString;
    if (amountForDepositObj == null) {
        amountForDepositString = "";
    } else {
        amountForDepositString = amountForDepositObj.toString();
    }
    int depositLength = amountForDepositString.length();
    if (amountForDepositString.lastIndexOf(0, dot) > digsBeforeDec) {
        errors.reject("MaxDigitsBefore.savingProduct.deposit", new String[] { digsBeforeDec.toString() }, null);
    }
    if (amountForDepositString.lastIndexOf(dot, depositLength) > digsAfterDec) {
        errors.reject("MaxDigitsAfter.savingProduct.deposit", new String[] { digsAfterDec.toString() }, null);
    }
    if (depositLength > max) {
        errors.reject("MaxDigitsNumber.savingProduct.deposit", new String[] { String.valueOf(max) }, null);
    }
}
Also used : MutableDateTime(org.joda.time.MutableDateTime) BindException(org.springframework.validation.BindException) FieldError(org.springframework.validation.FieldError) Date(java.util.Date) BigDecimal(java.math.BigDecimal)

Example 3 with BindException

use of org.springframework.validation.BindException in project opennms by OpenNMS.

the class DefaultStatisticsReportServiceTest method testNullCommandObjectId.

@Test
public void testNullCommandObjectId() {
    StatisticsReportCommand command = new StatisticsReportCommand();
    BindException errors = new BindException(command, "");
    ThrowableAnticipator ta = new ThrowableAnticipator();
    ta.anticipate(new IllegalArgumentException("id property on command object cannot be null"));
    m_mocks.replayAll();
    try {
        m_service.getReport(command, errors);
    } catch (Throwable t) {
        ta.throwableReceived(t);
    }
    ta.verifyAnticipated();
}
Also used : BindException(org.springframework.validation.BindException) StatisticsReportCommand(org.opennms.web.svclayer.model.StatisticsReportCommand) ThrowableAnticipator(org.opennms.test.ThrowableAnticipator) Test(org.junit.Test)

Example 4 with BindException

use of org.springframework.validation.BindException in project opennms by OpenNMS.

the class DefaultStatisticsReportServiceTest method testDatumWithNonExistentResource.

@Test
public void testDatumWithNonExistentResource() {
    StatisticsReport report = new StatisticsReport();
    report.setId(1);
    StatisticsReportData datum = new StatisticsReportData();
    ResourceReference resourceRef = new ResourceReference();
    resourceRef.setId(1);
    resourceRef.setResourceId("node[1].interfaceSnmp[en0]");
    datum.setId(1);
    datum.setResource(resourceRef);
    datum.setReport(report);
    datum.setValue(0.1d);
    report.addData(datum);
    StatisticsReportCommand command = new StatisticsReportCommand();
    command.setId(report.getId());
    BindException errors = new BindException(command, "");
    expect(m_statisticsReportDao.load(report.getId())).andReturn(report);
    m_statisticsReportDao.initialize(report);
    m_statisticsReportDao.initialize(report.getData());
    expect(m_resourceDao.getResourceById(ResourceId.fromString(resourceRef.getResourceId()))).andReturn(null);
    m_mocks.replayAll();
    StatisticsReportModel model = m_service.getReport(command, errors);
    assertNotNull("model should not be null", model);
    assertNotNull("model.getData() should not be null", model.getData());
    SortedSet<Datum> data = model.getData();
    assertEquals("data size", 1, data.size());
    Datum d = data.first();
    assertNotNull("first datum should not be null", d);
    assertNull("first datum resource should be null", d.getResource());
}
Also used : Datum(org.opennms.web.svclayer.model.StatisticsReportModel.Datum) StatisticsReport(org.opennms.netmgt.model.StatisticsReport) StatisticsReportData(org.opennms.netmgt.model.StatisticsReportData) BindException(org.springframework.validation.BindException) ResourceReference(org.opennms.netmgt.model.ResourceReference) StatisticsReportCommand(org.opennms.web.svclayer.model.StatisticsReportCommand) StatisticsReportModel(org.opennms.web.svclayer.model.StatisticsReportModel) Test(org.junit.Test)

Example 5 with BindException

use of org.springframework.validation.BindException in project opennms by OpenNMS.

the class DefaultDistributedPollerServiceTest method testDeleteLocationMonitorNullCommand.

public void testDeleteLocationMonitorNullCommand() {
    ThrowableAnticipator ta = new ThrowableAnticipator();
    ta.anticipate(new IllegalStateException("command argument cannot be null"));
    LocationMonitorIdCommand command = new LocationMonitorIdCommand();
    BindException errors = new BindException(command, "command");
    replayMocks();
    try {
        m_distributedPollerService.deleteLocationMonitor(null, errors);
    } catch (Throwable t) {
        ta.throwableReceived(t);
    }
    ta.verifyAnticipated();
    verifyMocks();
}
Also used : LocationMonitorIdCommand(org.opennms.web.svclayer.model.LocationMonitorIdCommand) BindException(org.springframework.validation.BindException) ThrowableAnticipator(org.opennms.test.ThrowableAnticipator)

Aggregations

BindException (org.springframework.validation.BindException)458 Test (org.junit.Test)402 Errors (org.springframework.validation.Errors)387 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)384 Date (java.util.Date)61 OrderUtilTest (org.openmrs.order.OrderUtilTest)58 DrugOrder (org.openmrs.DrugOrder)56 Patient (org.openmrs.Patient)40 Calendar (java.util.Calendar)30 Concept (org.openmrs.Concept)26 Encounter (org.openmrs.Encounter)25 TestOrder (org.openmrs.TestOrder)23 OrderType (org.openmrs.OrderType)22 Obs (org.openmrs.Obs)20 Order (org.openmrs.Order)20 ConceptReferenceTerm (org.openmrs.ConceptReferenceTerm)19 Visit (org.openmrs.Visit)19 PatientProgram (org.openmrs.PatientProgram)18 PatientIdentifierType (org.openmrs.PatientIdentifierType)17 Drug (org.openmrs.Drug)15