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.gl.batch;
017:
018: import java.math.BigDecimal;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.kuali.core.dbplatform.RawSQL;
024: import org.kuali.core.service.DateTimeService;
025: import org.kuali.core.util.Guid;
026: import org.kuali.core.util.UnitTestSqlDao;
027: import org.kuali.kfs.batch.Step;
028: import org.kuali.kfs.context.KualiTestBase;
029: import org.kuali.kfs.context.SpringContext;
030: import org.kuali.test.ConfigureContext;
031:
032: /**
033: * Tests the PurgeStep
034: */
035: @ConfigureContext
036: @RawSQL
037: public class PurgeTest extends KualiTestBase {
038: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
039: .getLogger(PurgeTest.class);
040:
041: private UnitTestSqlDao unitTestSqlDao;
042: private DateTimeService dateTimeService;
043:
044: /**
045: * Sets up this method by getting the needed services from the SpringContext
046: * @see junit.framework.TestCase#setUp()
047: */
048: @Override
049: protected void setUp() throws Exception {
050: super .setUp();
051:
052: unitTestSqlDao = SpringContext.getBean(UnitTestSqlDao.class);
053: dateTimeService = SpringContext.getBean(DateTimeService.class);
054: }
055:
056: /**
057: * Tests that entries created before 2002 are purged
058: *
059: * @throws Exception thrown if something (likely a SQL issue) goes wrong
060: */
061: public void testPurgeEntry() throws Exception {
062: LOG.debug("testPurgeEntry() started");
063:
064: // The data keeps changing, so this will make sure the test always succeeds
065: unitTestSqlDao.sqlCommand("delete from GL_ENTRY_T");
066:
067: // Shouldn't be deleted
068: unitTestSqlDao
069: .sqlCommand("insert into GL_ENTRY_T (UNIV_FISCAL_YR, FIN_COA_CD, ACCOUNT_NBR, SUB_ACCT_NBR, FIN_OBJECT_CD, FIN_SUB_OBJ_CD, FIN_BALANCE_TYP_CD, FIN_OBJ_TYP_CD, UNIV_FISCAL_PRD_CD, FDOC_TYP_CD,"
070: + "FS_ORIGIN_CD, FDOC_NBR, TRN_ENTR_SEQ_NBR, TRN_LDGR_ENTR_DESC, TRN_LDGR_ENTR_AMT, TRN_DEBIT_CRDT_CD, TRANSACTION_DT,"
071: + "ORG_DOC_NBR, PROJECT_CD, ORG_REFERENCE_ID, FDOC_REF_TYP_CD, FS_REF_ORIGIN_CD, FDOC_REF_NBR, FDOC_REVERSAL_DT, TRN_ENCUM_UPDT_CD, TRN_POST_DT, TIMESTAMP) "
072: + "values (2002, 'BL', '1031400', '-----', '5000', '---', 'AC', 'EX', '01', 'JV', '01', 'XXX', 1,'YYY', 0, 'D', "
073: + unitTestSqlDao.getDbPlatform()
074: .getCurTimeFunction()
075: + ", "
076: + "'XX', '----------', 'X', null,null,null,null,' ',"
077: + unitTestSqlDao.getDbPlatform()
078: .getCurTimeFunction()
079: + ","
080: + unitTestSqlDao.getDbPlatform()
081: .getCurTimeFunction() + ")");
082:
083: // Should be deleted
084: unitTestSqlDao
085: .sqlCommand("insert into GL_ENTRY_T (UNIV_FISCAL_YR, FIN_COA_CD, ACCOUNT_NBR, SUB_ACCT_NBR, FIN_OBJECT_CD, FIN_SUB_OBJ_CD, FIN_BALANCE_TYP_CD, FIN_OBJ_TYP_CD, UNIV_FISCAL_PRD_CD, FDOC_TYP_CD,"
086: + "FS_ORIGIN_CD, FDOC_NBR, TRN_ENTR_SEQ_NBR, TRN_LDGR_ENTR_DESC, TRN_LDGR_ENTR_AMT, TRN_DEBIT_CRDT_CD, TRANSACTION_DT,"
087: + "ORG_DOC_NBR, PROJECT_CD, ORG_REFERENCE_ID, FDOC_REF_TYP_CD, FS_REF_ORIGIN_CD, FDOC_REF_NBR, FDOC_REVERSAL_DT, TRN_ENCUM_UPDT_CD, TRN_POST_DT, TIMESTAMP) "
088: + "values (2001, 'BL', '1031400', '-----', '5000', '---', 'AC', 'EX', '01', 'JV', '01', 'XXX', 1,'YYY', 0, 'D', "
089: + unitTestSqlDao.getDbPlatform()
090: .getCurTimeFunction()
091: + ", "
092: + "'XX', '----------', 'X', null,null,null,null,' ',"
093: + unitTestSqlDao.getDbPlatform()
094: .getCurTimeFunction()
095: + ","
096: + unitTestSqlDao.getDbPlatform()
097: .getCurTimeFunction() + ")");
098:
099: Step purgeStep = SpringContext.getBean(PurgeEntryStep.class);
100:
101: // Run the purge
102: assertTrue("Should return true", purgeStep.execute(getClass()
103: .getName()));
104:
105: // Check the results (should be 1 row for 2002)
106: List counts = unitTestSqlDao
107: .sqlSelect("select univ_fiscal_yr,count(*) from gl_entry_t group by univ_fiscal_yr order by univ_fiscal_yr");
108: assertEquals("Wrong number of years found in gl_entry_t", 1,
109: counts.size());
110:
111: Map count2002 = (Map) counts.get(0);
112: assertEquals("Selected year is wrong", 2002, getInt(count2002,
113: "UNIV_FISCAL_YR"));
114: assertEquals("Wrong count for year found", 1, getInt(count2002,
115: "COUNT(*)"));
116: }
117:
118: /**
119: * Tests that balances are purged before 1999
120: *
121: * @throws Exception thrown if something (likely a SQL issue) goes wrong
122: */
123: public void testPurgeBalance() throws Exception {
124: LOG.debug("testPurgeBalance() started");
125:
126: // The data keeps changing, so this will make sure the test always succeeds
127: unitTestSqlDao.sqlCommand("delete from GL_balance_T");
128:
129: // Should be deleted
130: unitTestSqlDao
131: .sqlCommand("insert into gl_balance_t (UNIV_FISCAL_YR, FIN_COA_CD, ACCOUNT_NBR, SUB_ACCT_NBR, FIN_OBJECT_CD, FIN_SUB_OBJ_CD, FIN_BALANCE_TYP_CD, FIN_OBJ_TYP_CD, "
132: + "ACLN_ANNL_BAL_AMT, FIN_BEG_BAL_LN_AMT, CONTR_GR_BB_AC_AMT, MO1_ACCT_LN_AMT, MO2_ACCT_LN_AMT, MO3_ACCT_LN_AMT, MO4_ACCT_LN_AMT, MO5_ACCT_LN_AMT, MO6_ACCT_LN_AMT, "
133: + "MO7_ACCT_LN_AMT, MO8_ACCT_LN_AMT, MO9_ACCT_LN_AMT, MO10_ACCT_LN_AMT, MO11_ACCT_LN_AMT, MO12_ACCT_LN_AMT, MO13_ACCT_LN_AMT, TIMESTAMP) "
134: + "values (1998, 'BL', '1031400', '-----', '5000', '---', 'AC', 'EX',"
135: + " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
136: + unitTestSqlDao.getDbPlatform()
137: .getCurTimeFunction() + ")");
138:
139: // Shouldn't be deleted
140: unitTestSqlDao
141: .sqlCommand("insert into gl_balance_t (UNIV_FISCAL_YR, FIN_COA_CD, ACCOUNT_NBR, SUB_ACCT_NBR, FIN_OBJECT_CD, FIN_SUB_OBJ_CD, FIN_BALANCE_TYP_CD, FIN_OBJ_TYP_CD, "
142: + "ACLN_ANNL_BAL_AMT, FIN_BEG_BAL_LN_AMT, CONTR_GR_BB_AC_AMT, MO1_ACCT_LN_AMT, MO2_ACCT_LN_AMT, MO3_ACCT_LN_AMT, MO4_ACCT_LN_AMT, MO5_ACCT_LN_AMT, MO6_ACCT_LN_AMT, "
143: + "MO7_ACCT_LN_AMT, MO8_ACCT_LN_AMT, MO9_ACCT_LN_AMT, MO10_ACCT_LN_AMT, MO11_ACCT_LN_AMT, MO12_ACCT_LN_AMT, MO13_ACCT_LN_AMT, TIMESTAMP) "
144: + "values (1999, 'BL', '1031400', '-----', '5000', '---', 'AC', 'EX',"
145: + " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
146: + unitTestSqlDao.getDbPlatform()
147: .getCurTimeFunction() + ")");
148:
149: Step purgeStep = SpringContext.getBean(PurgeBalanceStep.class);
150:
151: // Run the purge
152: assertTrue("Should return true", purgeStep.execute(getClass()
153: .getName()));
154:
155: // Check the results (should be 1 row for 1999)
156: List counts = unitTestSqlDao
157: .sqlSelect("select univ_fiscal_yr,count(*) from gl_balance_t group by univ_fiscal_yr order by univ_fiscal_yr");
158: assertEquals("Wrong number of years found in gl_balance_t", 1,
159: counts.size());
160:
161: Map count1999 = (Map) counts.get(0);
162: assertEquals("Selected year is wrong", 1999, getInt(count1999,
163: "UNIV_FISCAL_YR"));
164: assertEquals("Wrong count for year found", 1, getInt(count1999,
165: "COUNT(*)"));
166: }
167:
168: /**
169: * Tests that account balances are purged before 1999
170: *
171: * @throws Exception thrown if something (likely a SQL issue) goes wrong
172: */
173: public void testPurgeAccountBalances() throws Exception {
174: LOG.debug("testPurgeAccountBalances() started");
175:
176: // Clear out the table to start with
177: unitTestSqlDao.sqlCommand("DELETE FROM GL_ACCT_BALANCES_T");
178:
179: // Should be deleted
180: unitTestSqlDao
181: .sqlCommand("INSERT INTO GL_ACCT_BALANCES_T (UNIV_FISCAL_YR, FIN_COA_CD, ACCOUNT_NBR, SUB_ACCT_NBR, FIN_OBJECT_CD, FIN_SUB_OBJ_CD, "
182: + "CURR_BDLN_BAL_AMT, ACLN_ACTLS_BAL_AMT, ACLN_ENCUM_BAL_AMT, TIMESTAMP) VALUES (1998, 'BL', '1031400', '-----', '5000', "
183: + "'---',"
184: + " 0, 0, 0, "
185: + unitTestSqlDao.getDbPlatform()
186: .getCurTimeFunction() + ")");
187:
188: // Shouldn't be deleted
189: unitTestSqlDao
190: .sqlCommand("INSERT INTO GL_ACCT_BALANCES_T (UNIV_FISCAL_YR, FIN_COA_CD, ACCOUNT_NBR, SUB_ACCT_NBR, FIN_OBJECT_CD, FIN_SUB_OBJ_CD, "
191: + "CURR_BDLN_BAL_AMT, ACLN_ACTLS_BAL_AMT, ACLN_ENCUM_BAL_AMT, TIMESTAMP) VALUES (1999, 'BL', '1031400', '-----', '5000', "
192: + "'---',"
193: + " 0, 0, 0, "
194: + unitTestSqlDao.getDbPlatform()
195: .getCurTimeFunction() + ")");
196:
197: Step purgeStep = SpringContext
198: .getBean(PurgeAccountBalancesStep.class);
199:
200: // Run the purge
201: assertTrue("Should return true", purgeStep.execute(getClass()
202: .getName()));
203:
204: // Check the results (should be 1 row for 1999)
205: List counts = unitTestSqlDao
206: .sqlSelect("select univ_fiscal_yr,count(*) from gl_acct_balances_t group by univ_fiscal_yr order by univ_fiscal_yr");
207: assertEquals(
208: "Wrong number of years found in gl_acct_balances_t", 1,
209: counts.size());
210:
211: Map count1999 = (Map) counts.get(0);
212: assertEquals("Selected year is wrong", 1999, getInt(count1999,
213: "UNIV_FISCAL_YR"));
214: assertEquals("Wrong count for year found", 1, getInt(count1999,
215: "COUNT(*)"));
216: }
217:
218: /**
219: * Tests that encumbrances are purged before 2002
220: *
221: * @throws Exception thrown if something (likely a SQL issue) goes wrong
222: */
223: public void testPurgeEncumbrance() throws Exception {
224: LOG.debug("testPurgeEncumbrance() started");
225:
226: // Clear out the table
227: unitTestSqlDao.sqlCommand("DELETE FROM GL_ENCUMBRANCE_T");
228:
229: // Should be deleted
230: unitTestSqlDao
231: .sqlCommand("insert into GL_ENCUMBRANCE_T (UNIV_FISCAL_YR, FIN_COA_CD, ACCOUNT_NBR, SUB_ACCT_NBR, FIN_OBJECT_CD, FIN_SUB_OBJ_CD, "
232: + "FIN_BALANCE_TYP_CD, FDOC_TYP_CD, FS_ORIGIN_CD, FDOC_NBR, TRN_ENCUM_DESC, TRN_ENCUM_DT, ACLN_ENCUM_AMT, ACLN_ENCUM_CLS_AMT, "
233: + "ACLN_ENCUM_PRG_CD, TIMESTAMP) values (2001, 'BL', '1031400', '-----', '5000', '---', 'AC', 'JV', '01', 'XXX','Desc',"
234: + unitTestSqlDao.getDbPlatform()
235: .getCurTimeFunction()
236: + ", 0, 0, 'N', "
237: + unitTestSqlDao.getDbPlatform()
238: .getCurTimeFunction() + ")");
239:
240: // Shouldn't be deleted
241: unitTestSqlDao
242: .sqlCommand("insert into GL_ENCUMBRANCE_T (UNIV_FISCAL_YR, FIN_COA_CD, ACCOUNT_NBR, SUB_ACCT_NBR, FIN_OBJECT_CD, FIN_SUB_OBJ_CD, "
243: + "FIN_BALANCE_TYP_CD, FDOC_TYP_CD, FS_ORIGIN_CD, FDOC_NBR, TRN_ENCUM_DESC, TRN_ENCUM_DT, ACLN_ENCUM_AMT, ACLN_ENCUM_CLS_AMT, "
244: + "ACLN_ENCUM_PRG_CD, TIMESTAMP) values (2002, 'BL', '1031400', '-----', '5000', '---', 'AC', 'JV', '01', 'XXX', 'Desc', "
245: + unitTestSqlDao.getDbPlatform()
246: .getCurTimeFunction()
247: + ", "
248: + "0, 0, 'N', "
249: + unitTestSqlDao.getDbPlatform()
250: .getCurTimeFunction() + ")");
251:
252: Step purgeStep = SpringContext
253: .getBean(PurgeEncumbranceStep.class);
254:
255: // Run the purge
256: assertTrue("Should return true", purgeStep.execute(getClass()
257: .getName()));
258:
259: // Check the results (should be 1 row for 2002)
260: List counts = unitTestSqlDao
261: .sqlSelect("select univ_fiscal_yr,count(*) from gl_encumbrance_t group by univ_fiscal_yr order by univ_fiscal_yr");
262: assertEquals("Wrong number of years found in gl_encumbrance_t",
263: 1, counts.size());
264:
265: Map count2002 = (Map) counts.get(0);
266: assertEquals("Selected year is wrong", 2002, getInt(count2002,
267: "UNIV_FISCAL_YR"));
268: assertEquals("Wrong count for year found", 1, getInt(count2002,
269: "COUNT(*)"));
270: }
271:
272: /**
273: * Tests that collector details are purged before 2002
274: *
275: * @throws Exception thrown if something (likely a SQL issue) goes wrong
276: */
277: public void testPurgeCollectorDetail() throws Exception {
278: LOG.debug("testPurgeCollectorDetail() started");
279:
280: // Clear out the table
281: unitTestSqlDao.sqlCommand("DELETE FROM GL_ID_BILL_T");
282:
283: // Should be deleted
284: unitTestSqlDao
285: .sqlCommand("insert into GL_ID_BILL_T (FS_ORIGIN_CD,UNIV_FISCAL_PRD_CD, UNIV_FISCAL_YR, CREATE_DT, CREATE_SEQ, FIN_COA_CD, ACCOUNT_NBR, "
286: + "SUB_ACCT_NBR, FIN_OBJECT_CD, FIN_SUB_OBJ_CD, FDOC_IDBIL_SEQ_NBR, FDOC_TYP_CD, FDOC_NBR, OBJ_ID, VER_NBR, FDOC_IDBIL_ITM_AMT, "
287: + "FDOC_IDBIL_NTE_TXT, FIN_OBJ_TYP_CD, FIN_BALANCE_TYP_CD) values ('01','01', 2001, "
288: + unitTestSqlDao.getDbPlatform()
289: .getCurTimeFunction()
290: + ", '1', 'BL', '1031400', '-----', '5000', '---', '1', 'ID22', 'XXX','"
291: + new Guid().toString()
292: + "', 1, 0, 'x', 'EX', 'AC')");
293:
294: // Shouldn't be deleted
295: unitTestSqlDao
296: .sqlCommand("insert into GL_ID_BILL_T (FS_ORIGIN_CD,UNIV_FISCAL_PRD_CD, UNIV_FISCAL_YR, CREATE_DT, CREATE_SEQ, FIN_COA_CD, ACCOUNT_NBR, "
297: + "SUB_ACCT_NBR, FIN_OBJECT_CD, FIN_SUB_OBJ_CD, FDOC_IDBIL_SEQ_NBR, FDOC_TYP_CD, FDOC_NBR, OBJ_ID, VER_NBR, FDOC_IDBIL_ITM_AMT, "
298: + "FDOC_IDBIL_NTE_TXT, FIN_OBJ_TYP_CD, FIN_BALANCE_TYP_CD) values ('01','01', 2002, "
299: + unitTestSqlDao.getDbPlatform()
300: .getCurTimeFunction()
301: + ", '1', 'BL', '1031400', '-----', '5000', '---', '1', 'ID22', 'XXX','"
302: + new Guid().toString()
303: + "', 1, 0, 'x', 'EX', 'AC')");
304:
305: Step purgeStep = SpringContext
306: .getBean(PurgeCollectorDetailStep.class);
307:
308: // Run the purge
309: assertTrue("Should return true", purgeStep.execute(getClass()
310: .getName()));
311:
312: // Check the results (should be 1 row for 2002)
313: List counts = unitTestSqlDao
314: .sqlSelect("select univ_fiscal_yr,count(*) from gl_id_bill_t group by univ_fiscal_yr order by univ_fiscal_yr");
315: assertEquals("Wrong number of years found in gl_id_bill_t", 1,
316: counts.size());
317:
318: Map count2002 = (Map) counts.get(0);
319: assertEquals("Selected year is wrong", 2002, getInt(count2002,
320: "UNIV_FISCAL_YR"));
321: assertEquals("Wrong count for year found", 1, getInt(count2002,
322: "COUNT(*)"));
323: }
324:
325: /**
326: * Tests that sufficient funds balances are purged before 1999
327: *
328: * @throws Exception thrown if something (likely a SQL issue) goes wrong
329: */
330: public void testPurgeSufficientFundsBalances() throws Exception {
331: LOG.debug("testPurgeSufficientFundsBalances() started");
332:
333: // Clear out the table
334: unitTestSqlDao.sqlCommand("DELETE FROM GL_SF_BALANCES_T");
335:
336: // Should be deleted
337: unitTestSqlDao
338: .sqlCommand("insert into gl_sf_balances_t (UNIV_FISCAL_YR, FIN_COA_CD, ACCOUNT_NBR, FIN_OBJECT_CD, ACCT_SF_CD, "
339: + "CURR_BDGT_BAL_AMT, ACCT_ACTL_XPND_AMT, ACCT_ENCUM_AMT, TIMESTAMP) values (1998, 'BL', '1031400', '5000','H', 0, 0, 0, "
340: + unitTestSqlDao.getDbPlatform()
341: .getCurTimeFunction() + ")");
342:
343: // Shouldn't be deleted
344: unitTestSqlDao
345: .sqlCommand("insert into gl_sf_balances_t (UNIV_FISCAL_YR, FIN_COA_CD, ACCOUNT_NBR, FIN_OBJECT_CD, ACCT_SF_CD, "
346: + "CURR_BDGT_BAL_AMT, ACCT_ACTL_XPND_AMT, ACCT_ENCUM_AMT, TIMESTAMP) values (1999, 'BL', '1031400', '5000','H', 0, 0, 0, "
347: + unitTestSqlDao.getDbPlatform()
348: .getCurTimeFunction() + ")");
349:
350: Step purgeStep = SpringContext
351: .getBean(PurgeSufficientFundBalancesStep.class);
352:
353: // Run the purge
354: assertTrue("Should return true", purgeStep.execute(getClass()
355: .getName()));
356:
357: // Check the results (should be 1 row for 1999)
358: List counts = unitTestSqlDao
359: .sqlSelect("select univ_fiscal_yr,count(*) from gl_sf_balances_t group by univ_fiscal_yr order by univ_fiscal_yr");
360: assertEquals("Wrong number of years found in gl_sf_balances_t",
361: 1, counts.size());
362:
363: Map count1999 = (Map) counts.get(0);
364: assertEquals("Selected year is wrong", 1999, getInt(count1999,
365: "UNIV_FISCAL_YR"));
366: assertEquals("Wrong count for year found", 1, getInt(count1999,
367: "COUNT(*)"));
368: }
369:
370: /**
371: * Prints the contents of a List of Maps to System.err
372: *
373: * @param maps a List of Maps
374: */
375: private void printList(List maps) {
376: for (Iterator iter = maps.iterator(); iter.hasNext();) {
377: Map element = (Map) iter.next();
378: StringBuffer sb = new StringBuffer();
379: for (Iterator iterator = element.keySet().iterator(); iterator
380: .hasNext();) {
381: String field = (String) iterator.next();
382: sb.append(field);
383: sb.append(" = ");
384: sb.append(element.get(field));
385: sb.append(",");
386: }
387: System.err.println(sb);
388: }
389: }
390:
391: /**
392: * Attempts to convert the value of a MapEntry with the given key into an int
393: *
394: * @param values a Map of values
395: * @param field the key of the value to convert
396: * @return the converted value or -1 if conversion was unsuccessful
397: */
398: private int getInt(Map values, String field) {
399: Object o = values.get(field);
400: if (o == null) {
401: return -1;
402: } else if (o instanceof BigDecimal) {
403: BigDecimal number = (BigDecimal) o;
404: return number.intValue();
405: } else if (o instanceof Long) {
406: Long number = (Long) o;
407: return number.intValue();
408: } else {
409: return -1;
410: }
411: }
412: }
|