001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.financial.service;
017:
018: import java.util.List;
019:
020: import org.kuali.core.util.KualiDecimal;
021: import org.kuali.core.util.UnitTestSqlDao;
022: import org.kuali.kfs.context.KualiTestBase;
023: import org.kuali.kfs.context.SpringContext;
024: import org.kuali.module.chart.bo.Account;
025: import org.kuali.module.chart.bo.Chart;
026: import org.kuali.module.gl.service.BalanceService;
027: import org.kuali.test.ConfigureContext;
028:
029: /**
030: * various tests for BalanceService, especially as it supports Account business rules; using hardcoded SQL for bootstrapping
031: */
032: @ConfigureContext
033: public class BalanceServiceTest extends KualiTestBase {
034: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
035: .getLogger(BalanceServiceTest.class);
036: private final static String ACCOUNT_NUMBER = "6812735";
037: private final static String CHART = "UA";
038: private final static String SUB_ACCT_NUMBER = "sub";
039: private final static String SUB_OBJECT_CODE = "123";
040:
041: private static String DELETE_BALANCES = "delete from GL_BALANCE_T where ";
042: private static String RAW_BALANCES = "select * from GL_BALANCE_T where ";
043: private static String INSERT_BALANCE = "insert into GL_BALANCE_T(FIN_COA_CD,ACCOUNT_NBR,SUB_ACCT_NBR,UNIV_FISCAL_YR,FIN_SUB_OBJ_CD,FIN_OBJECT_CD,FIN_BALANCE_TYP_CD,FIN_OBJ_TYP_CD,FIN_BEG_BAL_LN_AMT,ACLN_ANNL_BAL_AMT) values('"
044: + CHART
045: + "','"
046: + ACCOUNT_NUMBER
047: + "','"
048: + SUB_ACCT_NUMBER
049: + "',";
050:
051: private static boolean runOnce = true;
052:
053: private static Account account = new Account();
054: static {
055: account.setAccountNumber(ACCOUNT_NUMBER);
056: account.setChartOfAccountsCode(CHART);
057: // jkeller: added Chart object since now used by the BalanceServiceImpl class.
058: Chart chart = new Chart();
059: chart.setFundBalanceObjectCode("9899");
060: chart.setChartOfAccountsCode(CHART);
061: account.setChartOfAccounts(chart);
062: }
063:
064: private UnitTestSqlDao unitTestSqlDao;
065:
066: /**
067: * This method performs all the setup steps necessary to run the tests within this test case.
068: *
069: * @see junit.framework.TestCase#setUp()
070: */
071: @Override
072: protected void setUp() throws Exception {
073: super .setUp();
074: unitTestSqlDao = SpringContext.getBean(UnitTestSqlDao.class);
075: Integer fiscalYear = SpringContext.getBean(
076: UniversityDateService.class).getCurrentFiscalYear();
077:
078: if (runOnce) {
079: DELETE_BALANCES += "UNIV_FISCAL_YR=" + fiscalYear
080: + " AND ACCOUNT_NBR='" + ACCOUNT_NUMBER + "'";
081: RAW_BALANCES += "UNIV_FISCAL_YR=" + fiscalYear
082: + " AND ACCOUNT_NBR='" + ACCOUNT_NUMBER + "'";
083: INSERT_BALANCE += fiscalYear + ",";
084: runOnce = false; // do not run again
085: }
086:
087: }
088:
089: /**
090: *
091: * This method creates and makes and SQL command call to perform and insert passing in the provided parameters.
092: * @param objectTypeCode The object type code to be inserted.
093: * @param balanceTypeCode The balance type code to be inserted.
094: * @param objectCode The object code to be inserted.
095: * @param beginningAmount The beginning amount to be inserted.
096: * @param finalAmount The final amount to be inserted.
097: */
098: private void insertBalance(String objectTypeCode,
099: String balanceTypeCode, String objectCode,
100: KualiDecimal beginningAmount, KualiDecimal finalAmount) {
101: unitTestSqlDao.sqlCommand(INSERT_BALANCE + "'"
102: + SUB_OBJECT_CODE + "','" + objectCode + "','"
103: + balanceTypeCode + "','" + objectTypeCode + "',"
104: + beginningAmount + "," + finalAmount + ")");
105: }
106:
107: /**
108: *
109: * This method generates and calls and SQL command to remove all test data from the database.
110: */
111: public void purgeTestData() {
112: unitTestSqlDao.sqlCommand(DELETE_BALANCES);
113:
114: List results = unitTestSqlDao.sqlSelect(RAW_BALANCES);
115: assertNotNull("List shouldn't be null", results);
116: assertEquals("Should return 0 results", 0, results.size());
117:
118: }
119:
120: /**
121: *
122: * This method tests that the net result of of balance inserts is zero for appropriate balance type codes.
123: */
124: public void testNetToZero() {
125: List results;
126: purgeTestData();
127:
128: assertTrue("should net to zero when no rows exist",
129: SpringContext.getBean(BalanceService.class)
130: .fundBalanceWillNetToZero(account));
131:
132: insertBalance("EE", "TR", "9899", new KualiDecimal(1.5),
133: new KualiDecimal(2.5));
134: results = unitTestSqlDao.sqlSelect(RAW_BALANCES);
135: assertNotNull("List shouldn't be null", results);
136: assertEquals("Should return 1 result", 1, results.size());
137:
138: assertTrue("should net to zero with non-AC balance Type Code",
139: SpringContext.getBean(BalanceService.class)
140: .fundBalanceWillNetToZero(account));
141:
142: insertBalance("CH", "AC", "9899", new KualiDecimal(1.5),
143: new KualiDecimal(2.5));
144: assertFalse(SpringContext.getBean(BalanceService.class)
145: .fundBalanceWillNetToZero(account));
146:
147: // Negate the income balance with an equal expense balance
148: insertBalance("EE", "AC", "9899", new KualiDecimal(2),
149: new KualiDecimal(2));
150: assertTrue(
151: "should net to zero after adding corresponding expenses",
152: SpringContext.getBean(BalanceService.class)
153: .fundBalanceWillNetToZero(account));
154: purgeTestData();
155: }
156:
157: /**
158: *
159: * This method tests that appropriate asset object codes yield asset liability fund balances while non-asset codes do not.
160: */
161: public void testHasAssetLiabilityFundBalanceBalances() {
162: List results;
163: purgeTestData();
164: assertFalse("no rows means no balances", SpringContext.getBean(
165: BalanceService.class)
166: .hasAssetLiabilityFundBalanceBalances(account));
167: String fundBalanceObjectCode = "9899"; // TODO - get this from Service? Or System Options?
168: insertBalance("LI", "AC", "9899", new KualiDecimal(1.5),
169: new KualiDecimal(2.5));
170: assertFalse("should ignore 9899 balance", SpringContext
171: .getBean(BalanceService.class)
172: .hasAssetLiabilityFundBalanceBalances(account));
173: insertBalance("LI", "AC", "9900", new KualiDecimal(1.5),
174: new KualiDecimal(2.5));
175: assertTrue("expect nonzero balance for non-9899 balance",
176: SpringContext.getBean(BalanceService.class)
177: .hasAssetLiabilityFundBalanceBalances(account));
178:
179: }
180:
181: }
|