/* * Copyright (c) 2005-2011 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.action; import java.io.IOException; import java.util.Date; import java.util.List; import junit.framework.Assert; import org.hibernate.Session; import org.joda.time.DateTime; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mifos.accounts.loan.business.LoanBO; import org.mifos.accounts.loan.util.helpers.LoanConstants; import org.mifos.accounts.productdefinition.business.LoanOfferingBO; import org.mifos.accounts.util.helpers.AccountConstants; import org.mifos.accounts.util.helpers.AccountState; import org.mifos.application.meeting.business.MeetingBO; import org.mifos.application.util.helpers.ActionForwards; import org.mifos.config.persistence.ConfigurationPersistence; import org.mifos.customers.business.CustomerAccountBO; import org.mifos.customers.business.CustomerBO; import org.mifos.customers.personnel.util.helpers.PersonnelConstants; import org.mifos.customers.util.helpers.CustomerStatus; import org.mifos.dto.screen.TransactionHistoryDto; import org.mifos.framework.MifosMockStrutsTestCase; import org.mifos.framework.hibernate.helper.StaticHibernateUtil; import org.mifos.framework.util.DateTimeService; import org.mifos.framework.util.helpers.Constants; import org.mifos.framework.util.helpers.SessionUtils; import org.mifos.framework.util.helpers.TestObjectFactory; import org.mifos.security.util.UserContext; public class CloseLoanActionStrutsTest extends MifosMockStrutsTestCase { protected UserContext userContext = null; protected LoanBO loanBO = null; protected CustomerBO center = null; protected CustomerBO group = null; private DateTime currentDate = null; private String flowKey; private int lsim; @Override protected void setStrutsConfig() throws IOException { super.setStrutsConfig(); setConfigFile("/WEB-INF/struts-config.xml,/WEB-INF/accounts-struts-config.xml"); } @Before public void setUp() throws Exception { currentDate = new DateTime(2010, 12, 23, 12, 0, 0, 0); new DateTimeService().setCurrentDateTime(currentDate); ConfigurationPersistence configurationPersistence = new ConfigurationPersistence(); lsim = configurationPersistence.getConfigurationValueInteger(LoanConstants.REPAYMENT_SCHEDULES_INDEPENDENT_OF_MEETING_IS_ENABLED); configurationPersistence.updateConfigurationKeyValueInteger(LoanConstants.REPAYMENT_SCHEDULES_INDEPENDENT_OF_MEETING_IS_ENABLED, 1); userContext = TestObjectFactory.getContext(); request.getSession().setAttribute(Constants.USERCONTEXT, userContext); addRequestParameter("recordLoanOfficerId", "1"); addRequestParameter("recordOfficeId", "1"); request.getSession(false).setAttribute("ActivityContext", TestObjectFactory.getActivityContext()); flowKey = createFlow(request, LoanDisbursementAction.class); setRequestPathInfo("/editStatusAction"); } @After public void tearDown() throws Exception { new ConfigurationPersistence().updateConfigurationKeyValueInteger(LoanConstants.REPAYMENT_SCHEDULES_INDEPENDENT_OF_MEETING_IS_ENABLED, lsim); new DateTimeService().resetToCurrentSystemDateTime(); loanBO = null; group = null; center = null; } private LoanBO getLoanAccount() { Date startDate = new DateTimeService().getCurrentJavaDateTime(); MeetingBO meeting = TestObjectFactory.createMeeting(TestObjectFactory.getTypicalMeeting()); center = TestObjectFactory.createWeeklyFeeCenter("Center", meeting); group = TestObjectFactory.createWeeklyFeeGroupUnderCenter("Group", CustomerStatus.GROUP_ACTIVE, center); LoanOfferingBO loanOffering = TestObjectFactory.createLoanOffering(startDate, meeting); return TestObjectFactory.createLoanAccount("42423142341", group, AccountState.LOAN_ACTIVE_IN_GOOD_STANDING, startDate, loanOffering); } @Test public void testRescheduleLoan() throws Exception { loanBO = getLoanAccount(); loanBO.approve(legacyPersonnelDao.getPersonnel(PersonnelConstants.TEST_USER), "approved", currentDate.toLocalDate()); addRequestParameter("recordLoanOfficerId", "1"); addRequestParameter("accountId", loanBO.getAccountId().toString()); request.setAttribute(Constants.CURRENTFLOWKEY, flowKey); SessionUtils.setAttribute(Constants.BUSINESS_KEY, loanBO, request); request.getSession().setAttribute(Constants.USER_CONTEXT_KEY, userContext); addRequestParameter("notes", "reschedule"); addRequestParameter("newStatusId", "8"); addRequestParameter("input", "loan"); addRequestParameter("method", "update"); addRequestParameter(Constants.CURRENTFLOWKEY, (String) request.getAttribute(Constants.CURRENTFLOWKEY)); actionPerform(); verifyForward(ActionForwards.loan_detail_page.toString()); CustomerAccountBO closedAccount = group.getCustomerAccount(); Assert.assertEquals(AccountState.CUSTOMER_ACCOUNT_ACTIVE, closedAccount.getState()); Session session = StaticHibernateUtil.getSessionTL(); // session.beginTransaction(); loanBO = (LoanBO) session.get(LoanBO.class, loanBO.getAccountId()); List<TransactionHistoryDto> history = loanBO.getTransactionHistoryView(); for (TransactionHistoryDto entry : history) { Assert.assertEquals(AccountConstants.LOAN_RESCHEDULED, entry.getType()); } } }