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.kra.rule;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020: import java.util.Map;
021:
022: import org.kuali.core.service.DateTimeService;
023: import org.kuali.core.util.GlobalVariables;
024: import org.kuali.kfs.context.KualiTestBase;
025: import org.kuali.kfs.context.SpringContext;
026: import org.kuali.kfs.service.ParameterService;
027: import org.kuali.module.kra.KraConstants;
028: import org.kuali.module.kra.budget.bo.BudgetPeriod;
029: import org.kuali.module.kra.budget.bo.BudgetTask;
030: import org.kuali.module.kra.budget.document.BudgetDocument;
031: import org.kuali.module.kra.budget.rules.budget.BudgetDocumentRule;
032: import org.kuali.test.ConfigureContext;
033:
034: /**
035: * Test basic rule methods of <code>{@link BudgetDocumentRuleBase}</code> convenience class.
036: */
037: @ConfigureContext
038: public class BudgetDocumentRuleTest extends KualiTestBase {
039:
040: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
041: .getLogger(BudgetDocumentRuleTest.class);
042:
043: private BudgetDocumentRule budgetDocumentRule;
044: private String MINIMUM_NUMBER_OF_TASKS;
045: private String MAXIMUM_NUMBER_OF_TASKS;
046:
047: @Override
048: protected void setUp() throws Exception {
049: super .setUp();
050: budgetDocumentRule = new BudgetDocumentRule();
051: MAXIMUM_NUMBER_OF_TASKS = SpringContext.getBean(
052: ParameterService.class).getParameterValue(
053: BudgetDocument.class,
054: KraConstants.MAXIMUM_NUMBER_OF_TASKS);
055: MINIMUM_NUMBER_OF_TASKS = SpringContext.getBean(
056: ParameterService.class).getParameterValue(
057: BudgetDocument.class,
058: KraConstants.MINIMUM_NUMBER_OF_TASKS);
059: }
060:
061: public void testValidPeriods() throws Exception {
062: BudgetPeriod period1 = new BudgetPeriod();
063: period1.setBudgetPeriodBeginDate(SpringContext.getBean(
064: DateTimeService.class).convertToSqlDate("2006-01-01"));
065: period1.setBudgetPeriodEndDate(SpringContext.getBean(
066: DateTimeService.class).convertToSqlDate("2006-12-31"));
067:
068: budgetDocumentRule.isPeriodValid(period1, "period 1",
069: new Integer(1), true);
070:
071: Map errorMap = GlobalVariables.getErrorMap();
072: assertTrue(errorMap.isEmpty());
073:
074: BudgetPeriod period2 = new BudgetPeriod();
075: period2.setBudgetPeriodBeginDate(SpringContext.getBean(
076: DateTimeService.class).convertToSqlDate("2006-07-01"));
077: period2.setBudgetPeriodEndDate(SpringContext.getBean(
078: DateTimeService.class).convertToSqlDate("2007-06-30"));
079:
080: budgetDocumentRule.isPeriodValid(period2, "period 2",
081: new Integer(1), true);
082:
083: errorMap = GlobalVariables.getErrorMap();
084: assertTrue(errorMap.isEmpty());
085: }
086:
087: public void testValidPeriodLeapYear() throws Exception {
088: BudgetPeriod period1 = new BudgetPeriod();
089: period1.setBudgetPeriodBeginDate(SpringContext.getBean(
090: DateTimeService.class).convertToSqlDate("2008-01-01"));
091: period1.setBudgetPeriodEndDate(SpringContext.getBean(
092: DateTimeService.class).convertToSqlDate("2008-12-31"));
093:
094: budgetDocumentRule.isPeriodValid(period1, "period 1",
095: new Integer(1), true);
096:
097: Map errorMap = GlobalVariables.getErrorMap();
098: assertTrue(errorMap.isEmpty());
099:
100: BudgetPeriod period2 = new BudgetPeriod();
101: period2.setBudgetPeriodBeginDate(SpringContext.getBean(
102: DateTimeService.class).convertToSqlDate("2008-02-28"));
103: period2.setBudgetPeriodEndDate(SpringContext.getBean(
104: DateTimeService.class).convertToSqlDate("2009-02-27"));
105:
106: budgetDocumentRule.isPeriodValid(period2, "period 2",
107: new Integer(1), true);
108:
109: errorMap = GlobalVariables.getErrorMap();
110: assertTrue(errorMap.isEmpty());
111:
112: BudgetPeriod period3 = new BudgetPeriod();
113: period3.setBudgetPeriodBeginDate(SpringContext.getBean(
114: DateTimeService.class).convertToSqlDate("2007-03-31"));
115: period3.setBudgetPeriodEndDate(SpringContext.getBean(
116: DateTimeService.class).convertToSqlDate("2008-02-29"));
117:
118: budgetDocumentRule.isPeriodValid(period3, "period 3",
119: new Integer(1), true);
120:
121: errorMap = GlobalVariables.getErrorMap();
122: assertTrue(errorMap.isEmpty());
123: }
124:
125: public void testInvalidPeriodStartAfterEnd() throws Exception {
126: BudgetPeriod period1 = new BudgetPeriod();
127: period1.setBudgetPeriodBeginDate(SpringContext.getBean(
128: DateTimeService.class).convertToSqlDate("2009-01-01"));
129: period1.setBudgetPeriodEndDate(SpringContext.getBean(
130: DateTimeService.class).convertToSqlDate("2008-12-31"));
131:
132: budgetDocumentRule.isPeriodValid(period1, "period 1",
133: new Integer(1), true);
134:
135: Map errorMap = GlobalVariables.getErrorMap();
136: assertTrue(errorMap.size() == 1);
137: }
138:
139: public void testInvalidPeriodLength() throws Exception {
140: BudgetPeriod period1 = new BudgetPeriod();
141: period1.setBudgetPeriodBeginDate(SpringContext.getBean(
142: DateTimeService.class).convertToSqlDate("2007-01-01"));
143: period1.setBudgetPeriodEndDate(SpringContext.getBean(
144: DateTimeService.class).convertToSqlDate("2008-12-31"));
145:
146: budgetDocumentRule.isPeriodValid(period1, "period 1",
147: new Integer(1), true);
148:
149: Map errorMap = GlobalVariables.getErrorMap();
150: assertTrue(errorMap.size() == 1);
151: }
152:
153: public void testValidConsecutivePeriodList() throws Exception {
154: List periodList = new ArrayList();
155:
156: BudgetPeriod period1 = new BudgetPeriod();
157: period1.setBudgetPeriodBeginDate(SpringContext.getBean(
158: DateTimeService.class).convertToSqlDate("2008-01-01"));
159: period1.setBudgetPeriodEndDate(SpringContext.getBean(
160: DateTimeService.class).convertToSqlDate("2008-12-31"));
161:
162: BudgetPeriod period2 = new BudgetPeriod();
163: period2.setBudgetPeriodBeginDate(SpringContext.getBean(
164: DateTimeService.class).convertToSqlDate("2009-01-01"));
165: period2.setBudgetPeriodEndDate(SpringContext.getBean(
166: DateTimeService.class).convertToSqlDate("2009-12-31"));
167:
168: BudgetPeriod period3 = new BudgetPeriod();
169: period3.setBudgetPeriodBeginDate(SpringContext.getBean(
170: DateTimeService.class).convertToSqlDate("2010-01-01"));
171: period3.setBudgetPeriodEndDate(SpringContext.getBean(
172: DateTimeService.class).convertToSqlDate("2010-12-31"));
173:
174: BudgetPeriod period4 = new BudgetPeriod();
175: period4.setBudgetPeriodBeginDate(SpringContext.getBean(
176: DateTimeService.class).convertToSqlDate("2011-01-01"));
177: period4.setBudgetPeriodEndDate(SpringContext.getBean(
178: DateTimeService.class).convertToSqlDate("2011-12-31"));
179:
180: BudgetPeriod period5 = new BudgetPeriod();
181: period5.setBudgetPeriodBeginDate(SpringContext.getBean(
182: DateTimeService.class).convertToSqlDate("2012-01-01"));
183: period5.setBudgetPeriodEndDate(SpringContext.getBean(
184: DateTimeService.class).convertToSqlDate("2012-11-26"));
185:
186: periodList.add(period1);
187: periodList.add(period2);
188: periodList.add(period3);
189: periodList.add(period4);
190: periodList.add(period5);
191:
192: budgetDocumentRule.isPeriodListValid(periodList, false, true);
193:
194: Map errorMap = GlobalVariables.getErrorMap();
195: assertTrue(errorMap.isEmpty());
196: }
197:
198: public void testInvalidConsecutivePeriodList() throws Exception {
199: List periodList = new ArrayList();
200:
201: BudgetPeriod period1 = new BudgetPeriod();
202: period1.setBudgetPeriodSequenceNumber(new Integer(1));
203: period1.setBudgetPeriodBeginDate(SpringContext.getBean(
204: DateTimeService.class).convertToSqlDate("2008-01-01"));
205: period1.setBudgetPeriodEndDate(SpringContext.getBean(
206: DateTimeService.class).convertToSqlDate("2008-12-31"));
207:
208: BudgetPeriod period2 = new BudgetPeriod();
209: period2.setBudgetPeriodSequenceNumber(new Integer(2));
210: period2.setBudgetPeriodBeginDate(SpringContext.getBean(
211: DateTimeService.class).convertToSqlDate("2009-01-02"));
212: period2.setBudgetPeriodEndDate(SpringContext.getBean(
213: DateTimeService.class).convertToSqlDate("2010-01-01"));
214:
215: BudgetPeriod period3 = new BudgetPeriod();
216: period3.setBudgetPeriodSequenceNumber(new Integer(3));
217: period3.setBudgetPeriodBeginDate(SpringContext.getBean(
218: DateTimeService.class).convertToSqlDate("2010-02-06"));
219: period3.setBudgetPeriodEndDate(SpringContext.getBean(
220: DateTimeService.class).convertToSqlDate("2010-12-31"));
221:
222: BudgetPeriod period4 = new BudgetPeriod();
223: period4.setBudgetPeriodSequenceNumber(new Integer(4));
224: period4.setBudgetPeriodBeginDate(SpringContext.getBean(
225: DateTimeService.class).convertToSqlDate("2010-12-01"));
226: period4.setBudgetPeriodEndDate(SpringContext.getBean(
227: DateTimeService.class).convertToSqlDate("2011-11-30"));
228:
229: BudgetPeriod period5 = new BudgetPeriod();
230: period5.setBudgetPeriodSequenceNumber(new Integer(5));
231: period5.setBudgetPeriodBeginDate(SpringContext.getBean(
232: DateTimeService.class).convertToSqlDate("2012-01-01"));
233: period5.setBudgetPeriodEndDate(SpringContext.getBean(
234: DateTimeService.class).convertToSqlDate("2012-11-26"));
235:
236: periodList.add(period1);
237: periodList.add(period2);
238: periodList.add(period3);
239: periodList.add(period4);
240: periodList.add(period5);
241:
242: budgetDocumentRule.isPeriodListValid(periodList, false, true);
243:
244: Map errorMap = GlobalVariables.getErrorMap();
245: assertTrue("should be 4, was " + errorMap.size(), errorMap
246: .size() == 4);
247: }
248:
249: public void testValidTaskList() throws Exception {
250: budgetDocumentRule.isTaskListValid(buildTaskList(new Integer(
251: MINIMUM_NUMBER_OF_TASKS).intValue()));
252: Map errorMap1 = GlobalVariables.getErrorMap();
253: assertTrue(errorMap1.isEmpty());
254:
255: budgetDocumentRule.isTaskListValid(buildTaskList(new Integer(
256: MINIMUM_NUMBER_OF_TASKS).intValue()));
257: Map errorMap2 = GlobalVariables.getErrorMap();
258: assertTrue(errorMap2.isEmpty());
259:
260: budgetDocumentRule.isTaskListValid(buildTaskList(new Integer(
261: MINIMUM_NUMBER_OF_TASKS).intValue()));
262: Map errorMap3 = GlobalVariables.getErrorMap();
263: assertTrue(errorMap3.isEmpty());
264: }
265:
266: /**
267: * Test for an empty tak list.
268: *
269: * @throws Exception
270: */
271: public void testNotEnoughTasks() throws Exception {
272: budgetDocumentRule.isTaskListValid(buildTaskList(new Integer(
273: MINIMUM_NUMBER_OF_TASKS).intValue() - 1));
274: Map errorMap1 = GlobalVariables.getErrorMap();
275: assertTrue(errorMap1.size() == 1);
276:
277: budgetDocumentRule.isTaskListValid(buildTaskList(new Integer(
278: MINIMUM_NUMBER_OF_TASKS).intValue() - 1));
279: Map errorMap2 = GlobalVariables.getErrorMap();
280: assertTrue(errorMap2.size() == 1);
281:
282: budgetDocumentRule.isTaskListValid(buildTaskList(new Integer(
283: MINIMUM_NUMBER_OF_TASKS).intValue() - 1));
284: Map errorMap3 = GlobalVariables.getErrorMap();
285: assertTrue(errorMap3.size() == 1);
286: }
287:
288: /**
289: * Test for a task list that is too big.
290: */
291: public void testTooManyTasks() throws Exception {
292: budgetDocumentRule.isTaskListValid(buildTaskList(new Integer(
293: MAXIMUM_NUMBER_OF_TASKS) + 1));
294: Map errorMap1 = GlobalVariables.getErrorMap();
295: assertTrue(errorMap1.size() == 1);
296:
297: budgetDocumentRule.isTaskListValid(buildTaskList(new Integer(
298: MAXIMUM_NUMBER_OF_TASKS) + 1));
299: Map errorMap2 = GlobalVariables.getErrorMap();
300: assertTrue(errorMap2.size() == 1);
301:
302: budgetDocumentRule.isTaskListValid(buildTaskList(new Integer(
303: MAXIMUM_NUMBER_OF_TASKS) + 1));
304: Map errorMap3 = GlobalVariables.getErrorMap();
305: assertTrue(errorMap3.size() == 1);
306: }
307:
308: private List buildTaskList(int x) {
309: List taskList = new ArrayList();
310: while (x > 0) {
311: BudgetTask task = new BudgetTask();
312: taskList.add(task);
313: x--;
314: }
315: return taskList;
316: }
317: }
|