<commit_before>/*
 * Copyright (c) 2005-2010 Grameen Foundation USA
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.accounts.loan.struts.actionforms;

import static org.apache.commons.lang.StringUtils.EMPTY;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_LOAN_AMOUNT_100;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_LOAN_AMOUNT_AND_PURPOSE_NULL;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_LOAN_AMOUNT_AND_PURPOSE_NULL2;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_LOAN_AMOUNT_NULL;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_LOAN_AMOUNT_ZERO;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_PURPOSE_EMPTY;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_PURPOSE_NULL;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_VALID_PURPOSE;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.mifos.accounts.fees.business.AmountFeeBO;
import org.mifos.accounts.fees.business.FeeFormulaEntity;
import org.mifos.accounts.fees.business.FeeDto;
import org.mifos.accounts.fees.business.RateFeeBO;
import org.mifos.accounts.fees.util.helpers.FeeFormula;
import org.mifos.accounts.fees.util.helpers.RateAmountFlag;
import org.mifos.accounts.loan.struts.uihelpers.PaymentDataHtmlBean;
import org.mifos.accounts.loan.util.helpers.LoanConstants;
import org.mifos.accounts.loan.util.helpers.LoanExceptionConstants;
import org.mifos.accounts.productdefinition.business.AmountRange;
import org.mifos.accounts.productdefinition.business.LoanAmountSameForAllLoanBO;
import org.mifos.accounts.productdefinition.business.NoOfInstallSameForAllLoanBO;
import org.mifos.accounts.util.helpers.PaymentDataTemplate;
import org.mifos.application.admin.servicefacade.InvalidDateException;
import org.mifos.config.AccountingRules;
import org.mifos.framework.TestUtils;
import org.mifos.framework.components.fieldConfiguration.business.FieldConfigurationEntity;
import org.mifos.framework.util.helpers.DateUtils;
import org.mifos.framework.util.helpers.Money;
import org.mifos.security.util.UserContext;


public class LoanAccountActionFormTest extends TestCase {

    private LoanAccountActionForm form;
    private PaymentDataHtmlBean paymentMock;
    private ActionErrors actionErrors;
    private static final String INTEREST_ERROR_KEY = "interest.invalid";
    private static final String AMOUNT_ERROR_KEY = "amount.invalid";

    public void testShouldAddErrorIfTransactionDateForAPaymentIsInFuture() throws InvalidDateException {
        expect(paymentMock.getTransactionDate()).andReturn(DateUtils.getDateFromToday(1));
        replay(paymentMock);
        ActionErrors errors = new ActionErrors();
        form.validateTransactionDate(errors, paymentMock, DateUtils.getDateFromToday(-3));
        verify(paymentMock);
        assertErrorKey(errors, LoanExceptionConstants.INVALIDTRANSACTIONDATEFORPAYMENT);
    }

    public void testShouldNotAddErrorIfTransactionDateForAPaymentIsToday() throws InvalidDateException {
        validateForNoErrorsOnDate(DateUtils.currentDate());
    }

    public void testShouldNotAddErrorIfTransactionDateForAPaymentIsInPast() throws InvalidDateException {
        validateForNoErrorsOnDate(DateUtils.getDateFromToday(-1));
    }

    public void testShouldAddErrorWhenTransactionDateOnDisbursementDate() throws Exception {
        validateWhenTransactionDateInvalidForDisbursementDate(DateUtils.currentDate());
    }

    public void testShouldAddErrorIfTransactionDateBeforeDisbursementDate() throws Exception {
        validateWhenTransactionDateInvalidForDisbursementDate(DateUtils.getDateFromToday(1));
    }

    public void testShouldNotAddErrorIfTransactionDateAfterDisbursementDate() throws Exception {
        expect(paymentMock.getTransactionDate()).andReturn(DateUtils.currentDate()).anyTimes();
        replay(paymentMock);
        ActionErrors errors = new ActionErrors();
        form.validateTransactionDate(errors, paymentMock, DateUtils.getDateFromToday(-1));
        verify(paymentMock);
       Assert.assertTrue(errors.isEmpty());
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        form = new LoanAccountActionForm();
        paymentMock = createMock(PaymentDataHtmlBean.class);
        expect(paymentMock.getTotalAmount()).andReturn(new Money(TestUtils.RUPEE));
        actionErrors = new ActionErrors();

    }

    private void validateForNoErrorsOnDate(Date transactionDate) throws InvalidDateException {<commit_msg>Revert "Revert "[MIFOS-4350] Missed file in commit""<commit_after>/*
 * Copyright (c) 2005-2010 Grameen Foundation USA
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.accounts.loan.struts.actionforms;

import static org.apache.commons.lang.StringUtils.EMPTY;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_LOAN_AMOUNT_100;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_LOAN_AMOUNT_AND_PURPOSE_NULL;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_LOAN_AMOUNT_AND_PURPOSE_NULL2;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_LOAN_AMOUNT_NULL;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_LOAN_AMOUNT_ZERO;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_PURPOSE_EMPTY;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_PURPOSE_NULL;
import static org.mifos.accounts.loan.util.helpers.LoanAccountActionFormTestConstants.LOAN_ACCOUNT_DETAILS_WITH_VALID_PURPOSE;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.mifos.accounts.fees.business.AmountFeeBO;
import org.mifos.accounts.fees.business.FeeFormulaEntity;
import org.mifos.accounts.fees.business.FeeDto;
import org.mifos.accounts.fees.business.RateFeeBO;
import org.mifos.accounts.fees.util.helpers.FeeFormula;
import org.mifos.accounts.fees.util.helpers.RateAmountFlag;
import org.mifos.accounts.loan.struts.uihelpers.PaymentDataHtmlBean;
import org.mifos.accounts.loan.util.helpers.LoanConstants;
import org.mifos.accounts.loan.util.helpers.LoanExceptionConstants;
import org.mifos.accounts.productdefinition.business.AmountRange;
import org.mifos.accounts.productdefinition.business.LoanAmountSameForAllLoanBO;
import org.mifos.accounts.productdefinition.business.NoOfInstallSameForAllLoanBO;
import org.mifos.accounts.util.helpers.PaymentDataTemplate;
import org.mifos.application.admin.servicefacade.InvalidDateException;
import org.mifos.config.AccountingRules;
import org.mifos.framework.TestUtils;
import org.mifos.framework.components.fieldConfiguration.business.FieldConfigurationEntity;
import org.mifos.framework.util.helpers.DateUtils;
import org.mifos.framework.util.helpers.Money;
import org.mifos.security.util.UserContext;


public class LoanAccountActionFormTest extends TestCase {

    private LoanAccountActionForm form;
    private PaymentDataHtmlBean paymentMock;
    private ActionErrors actionErrors;
    private static final String INTEREST_ERROR_KEY = "interest.invalid";
    private static final String AMOUNT_ERROR_KEY = "amount.invalid";

    public void testShouldAddErrorIfTransactionDateForAPaymentIsInFuture() throws InvalidDateException {
        expect(paymentMock.getTransactionDate()).andReturn(DateUtils.getDateFromToday(1));
        replay(paymentMock);
        ActionErrors errors = new ActionErrors();
        form.validateTransactionDate(errors, paymentMock, DateUtils.getDateFromToday(-3));
        verify(paymentMock);
        assertErrorKey(errors, LoanExceptionConstants.INVALIDTRANSACTIONDATEFORPAYMENT);
    }

    public void testShouldNotAddErrorIfTransactionDateForAPaymentIsToday() throws InvalidDateException {
        validateForNoErrorsOnDate(DateUtils.currentDate());
    }

    public void testShouldNotAddErrorIfTransactionDateForAPaymentIsInPast() throws InvalidDateException {
        validateForNoErrorsOnDate(DateUtils.getDateFromToday(-1));
    }

    public void testShouldAddErrorWhenTransactionDateOnDisbursementDate() throws Exception {
        validateWhenTransactionDateInvalidForDisbursementDate(DateUtils.currentDate());
    }

    public void testShouldAddErrorIfTransactionDateBeforeDisbursementDate() throws Exception {
        validateWhenTransactionDateInvalidForDisbursementDate(DateUtils.getDateFromToday(1));
    }

    public void testShouldNotAddErrorIfTransactionDateAfterDisbursementDate() throws Exception {
        expect(paymentMock.getTransactionDate()).andReturn(DateUtils.currentDate()).anyTimes();
        replay(paymentMock);
        ActionErrors errors = new ActionErrors();
        form.validateTransactionDate(errors, paymentMock, DateUtils.getDateFromToday(-1));
        verify(paymentMock);
       Assert.assertTrue(errors.isEmpty());
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        form = new LoanAccountActionForm();
        paymentMock = createMock(PaymentDataHtmlBean.class);
//        expect(paymentMock.getTotalAmount()).andReturn(new Money(TestUtils.RUPEE));
        expect(paymentMock.getTotal()).andReturn("0");
        actionErrors = new ActionErrors();

    }

    private void validateForNoErrorsOnDate(Date transactionDate) throws InvalidDateException {
{'fraction_non_alphanumeric': 0.08145748987854251, 'fraction_numerical': 0.004129554655870445, 'mean_word_length': 5.775095995611629, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7064829', 'n_tokens_mistral': 3723, 'n_tokens_neox': 3628, 'n_words': 625}