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.service;
017:
018: import java.sql.Date;
019: import java.text.ParseException;
020: import java.text.SimpleDateFormat;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.kuali.core.util.KualiInteger;
026: import org.kuali.kfs.context.KualiTestBase;
027: import org.kuali.kfs.context.SpringContext;
028: import org.kuali.module.cg.bo.Agency;
029: import org.kuali.module.kra.bo.BudgetNonpersonnelTest;
030: import org.kuali.module.kra.budget.bo.AgencyExtension;
031: import org.kuali.module.kra.budget.bo.Budget;
032: import org.kuali.module.kra.budget.bo.BudgetModular;
033: import org.kuali.module.kra.budget.bo.BudgetModularPeriod;
034: import org.kuali.module.kra.budget.bo.BudgetNonpersonnel;
035: import org.kuali.module.kra.budget.bo.BudgetPeriod;
036: import org.kuali.module.kra.budget.bo.UserAppointmentTaskPeriod;
037: import org.kuali.module.kra.budget.service.BudgetModularService;
038: import org.kuali.module.kra.budget.service.BudgetNonpersonnelService;
039: import org.kuali.test.ConfigureContext;
040:
041: /**
042: * This class tests service methods in BudgetModularService.
043: */
044: @ConfigureContext
045: public class BudgetModularServiceTest extends KualiTestBase {
046:
047: protected Budget setupBudget() {
048: Budget budget = new Budget();
049:
050: Agency agency = new Agency();
051: agency.setAgencyExtension(new AgencyExtension());
052: agency.getAgencyExtension().setAgencyModularIndicator(true);
053: agency.getAgencyExtension().setBudgetModularIncrementAmount(
054: new KualiInteger(25000));
055: agency.getAgencyExtension().setBudgetPeriodMaximumAmount(
056: new KualiInteger(250000));
057:
058: budget.setPeriods(createBudgetPeriods(2));
059:
060: budget.setBudgetAgency(agency);
061: return budget;
062: }
063:
064: public static List createBudgetPeriods(int numberOfPeriods) {
065: List budgetPeriods = new ArrayList();
066:
067: SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
068:
069: for (int i = 0; i < numberOfPeriods; i++) {
070: BudgetPeriod budgetPeriod = new BudgetPeriod();
071: budgetPeriod.setBudgetPeriodSequenceNumber(new Integer(
072: i + 1));
073: try {
074: budgetPeriod.setBudgetPeriodBeginDate(new Date(sdf
075: .parse("07/01/" + 2000 + i).getTime()));
076: budgetPeriod.setBudgetPeriodEndDate(new Date(sdf.parse(
077: "06/30/" + 2001 + i).getTime()));
078: } catch (ParseException e) {
079: throw new NullPointerException();
080: }
081: budgetPeriods.add(budgetPeriod);
082: }
083:
084: return budgetPeriods;
085: }
086:
087: public void testGenerateModularBudget() {
088: List nonpersonnelCategories = SpringContext.getBean(
089: BudgetNonpersonnelService.class)
090: .getAllNonpersonnelCategories();
091: KualiInteger zeroValue = new KualiInteger(0);
092:
093: // Case 1: Budget with no costs
094: Budget budget = setupBudget();
095: SpringContext.getBean(BudgetModularService.class)
096: .generateModularBudget(budget, nonpersonnelCategories);
097: BudgetModular modularBudget = budget.getModularBudget();
098:
099: assertFalse(modularBudget.isInvalidMode());
100: assertEquals(modularBudget.getIncrements().size(), 11);
101: assertEquals(modularBudget.getBudgetModularDirectCostAmount(),
102: zeroValue);
103: assertEquals(modularBudget.getTotalActualDirectCostAmount(),
104: zeroValue);
105: assertEquals(modularBudget
106: .getTotalAdjustedModularDirectCostAmount(),
107: new KualiInteger(0));
108: assertEquals(modularBudget.getTotalConsortiumAmount(),
109: zeroValue);
110: assertEquals(modularBudget.getTotalDirectCostAmount(),
111: new KualiInteger(0));
112: assertEquals(modularBudget.getTotalModularDirectCostAmount(),
113: zeroValue);
114: assertEquals(modularBudget.getTotalModularVarianceAmount(),
115: new KualiInteger(0));
116:
117: for (Iterator iter = modularBudget.getBudgetModularPeriods()
118: .iterator(); iter.hasNext();) {
119: BudgetModularPeriod modularPeriod = (BudgetModularPeriod) iter
120: .next();
121: assertEquals(modularPeriod.getActualDirectCostAmount(),
122: zeroValue);
123: assertEquals(modularPeriod.getConsortiumAmount(), zeroValue);
124: assertEquals(
125: modularPeriod.getTotalPeriodDirectCostAmount(),
126: new KualiInteger(0));
127: assertEquals(modularPeriod
128: .getBudgetAdjustedModularDirectCostAmount(),
129: new KualiInteger(0));
130: assertEquals(modularPeriod.getModularVarianceAmount(),
131: new KualiInteger(0));
132: }
133:
134: // Case 2: Budget with personnel, nonpersonnel, consortium costs
135: budget = setupBudget();
136:
137: String[] categories = { "CO", "CO", "FL", "SC" };
138: String[] subCategories = { "C1", "C1", "F5", "R2" };
139: String[] subcontractorNumber = { "", "", "", "1" };
140: List nonpersonnelItems = BudgetNonpersonnelTest
141: .createBudgetNonpersonnel(categories, subCategories,
142: subcontractorNumber);
143: for (Iterator iter = nonpersonnelItems.iterator(); iter
144: .hasNext();) {
145: BudgetNonpersonnel nonpersonnel = (BudgetNonpersonnel) iter
146: .next();
147: nonpersonnel.setBudgetPeriodSequenceNumber(new Integer(2));
148: }
149: budget.setNonpersonnelItems(nonpersonnelItems);
150:
151: List userAppointmentTaskPeriods = new ArrayList();
152:
153: BudgetPeriod period1 = (BudgetPeriod) budget.getPeriods()
154: .get(0);
155:
156: UserAppointmentTaskPeriod taskPeriod = new UserAppointmentTaskPeriod();
157: taskPeriod.setBudgetPeriodSequenceNumber(period1
158: .getBudgetPeriodSequenceNumber());
159: taskPeriod.setAgencyRequestTotalAmount(new KualiInteger(39000));
160: taskPeriod.setAgencyFringeBenefitTotalAmount(new KualiInteger(
161: 13000));
162: userAppointmentTaskPeriods.add(taskPeriod);
163:
164: UserAppointmentTaskPeriod taskPeriod2 = new UserAppointmentTaskPeriod();
165: taskPeriod2.setBudgetPeriodSequenceNumber(period1
166: .getBudgetPeriodSequenceNumber());
167: taskPeriod2
168: .setAgencyRequestTotalAmount(new KualiInteger(43000));
169: taskPeriod2.setAgencyFringeBenefitTotalAmount(new KualiInteger(
170: 8500));
171: userAppointmentTaskPeriods.add(taskPeriod2);
172:
173: BudgetPeriod period2 = (BudgetPeriod) budget.getPeriods()
174: .get(1);
175:
176: UserAppointmentTaskPeriod taskPeriod3 = new UserAppointmentTaskPeriod();
177: taskPeriod3.setBudgetPeriodSequenceNumber(period2
178: .getBudgetPeriodSequenceNumber());
179: taskPeriod3
180: .setAgencyRequestTotalAmount(new KualiInteger(74000));
181: taskPeriod3.setAgencyFringeBenefitTotalAmount(new KualiInteger(
182: 21500));
183: userAppointmentTaskPeriods.add(taskPeriod3);
184:
185: budget
186: .setAllUserAppointmentTaskPeriods(userAppointmentTaskPeriods);
187:
188: SpringContext.getBean(BudgetModularService.class)
189: .generateModularBudget(budget, nonpersonnelCategories);
190: modularBudget = budget.getModularBudget();
191:
192: assertFalse(modularBudget.isInvalidMode());
193: assertEquals(modularBudget.getIncrements().size(), 10);
194: assertEquals(modularBudget.getBudgetModularDirectCostAmount(),
195: new KualiInteger(125000));
196: assertEquals(modularBudget.getTotalActualDirectCostAmount(),
197: new KualiInteger(202000));
198: assertEquals(modularBudget
199: .getTotalAdjustedModularDirectCostAmount(),
200: new KualiInteger(250000));
201: assertEquals(modularBudget.getTotalConsortiumAmount(),
202: new KualiInteger(1000));
203: assertEquals(modularBudget.getTotalDirectCostAmount(),
204: new KualiInteger(251000));
205: assertEquals(modularBudget.getTotalModularDirectCostAmount(),
206: new KualiInteger(250000));
207: assertEquals(modularBudget.getTotalModularVarianceAmount(),
208: new KualiInteger(48000));
209:
210: BudgetModularPeriod modularPeriod1 = (BudgetModularPeriod) modularBudget
211: .getBudgetModularPeriods().get(0);
212: assertEquals(modularPeriod1.getActualDirectCostAmount(),
213: new KualiInteger(103500));
214: assertEquals(modularPeriod1.getConsortiumAmount(), zeroValue);
215: assertEquals(modularPeriod1.getTotalPeriodDirectCostAmount(),
216: new KualiInteger(125000));
217: assertEquals(modularPeriod1
218: .getBudgetAdjustedModularDirectCostAmount(),
219: new KualiInteger(125000));
220: assertEquals(modularPeriod1.getModularVarianceAmount(),
221: new KualiInteger(21500));
222:
223: BudgetModularPeriod modularPeriod2 = (BudgetModularPeriod) modularBudget
224: .getBudgetModularPeriods().get(1);
225: assertEquals(modularPeriod2.getActualDirectCostAmount(),
226: new KualiInteger(98500));
227: assertEquals(modularPeriod2.getConsortiumAmount(),
228: new KualiInteger(1000));
229: assertEquals(modularPeriod2.getTotalPeriodDirectCostAmount(),
230: new KualiInteger(126000));
231: assertEquals(modularPeriod2
232: .getBudgetAdjustedModularDirectCostAmount(),
233: new KualiInteger(125000));
234: assertEquals(modularPeriod2.getModularVarianceAmount(),
235: new KualiInteger(26500));
236:
237: // Case 3: Budget with costs > # of periods * period maximum
238: budget = setupBudget();
239:
240: BudgetPeriod periodInvalid = (BudgetPeriod) budget.getPeriods()
241: .get(0);
242:
243: UserAppointmentTaskPeriod taskPeriod4 = new UserAppointmentTaskPeriod();
244: taskPeriod4.setBudgetPeriodSequenceNumber(periodInvalid
245: .getBudgetPeriodSequenceNumber());
246: taskPeriod4.setAgencyRequestTotalAmount(new KualiInteger(
247: 1000000));
248: userAppointmentTaskPeriods = new ArrayList();
249: userAppointmentTaskPeriods.add(taskPeriod4);
250: budget
251: .setAllUserAppointmentTaskPeriods(userAppointmentTaskPeriods);
252:
253: SpringContext.getBean(BudgetModularService.class)
254: .generateModularBudget(budget, nonpersonnelCategories);
255: modularBudget = budget.getModularBudget();
256:
257: assertTrue(modularBudget.isInvalidMode());
258: assertTrue(modularBudget.getIncrements().size() == 1);
259: assertNull(modularBudget.getBudgetModularDirectCostAmount());
260: assertEquals(modularBudget.getTotalActualDirectCostAmount(),
261: new KualiInteger(1000000));
262: assertNull(modularBudget
263: .getTotalAdjustedModularDirectCostAmount());
264: assertEquals(modularBudget.getTotalConsortiumAmount(),
265: zeroValue);
266: assertNull(modularBudget.getTotalDirectCostAmount());
267: assertNull(modularBudget.getTotalModularDirectCostAmount());
268: assertEquals(modularBudget.getTotalModularVarianceAmount(),
269: zeroValue);
270:
271: BudgetModularPeriod modularPeriodInvalid1 = (BudgetModularPeriod) modularBudget
272: .getBudgetModularPeriods().get(0);
273: assertEquals(modularPeriodInvalid1.getActualDirectCostAmount(),
274: new KualiInteger(1000000));
275: assertEquals(modularPeriodInvalid1.getConsortiumAmount(),
276: zeroValue);
277: assertNull(modularPeriodInvalid1
278: .getTotalPeriodDirectCostAmount());
279: assertNull(modularPeriodInvalid1
280: .getBudgetAdjustedModularDirectCostAmount());
281: assertEquals(modularPeriodInvalid1.getModularVarianceAmount(),
282: zeroValue);
283:
284: BudgetModularPeriod modularPeriodInvalid2 = (BudgetModularPeriod) modularBudget
285: .getBudgetModularPeriods().get(1);
286: assertEquals(modularPeriodInvalid2.getActualDirectCostAmount(),
287: zeroValue);
288: assertEquals(modularPeriodInvalid2.getConsortiumAmount(),
289: zeroValue);
290: assertNull(modularPeriodInvalid2
291: .getTotalPeriodDirectCostAmount());
292: assertNull(modularPeriodInvalid2
293: .getBudgetAdjustedModularDirectCostAmount());
294: assertEquals(modularPeriodInvalid2.getModularVarianceAmount(),
295: zeroValue);
296: }
297:
298: public void testResetModularBudget() {
299:
300: // Case 1: Budget with no costs
301: Budget budget = setupBudget();
302: SpringContext.getBean(BudgetModularService.class)
303: .resetModularBudget(budget);
304: BudgetModular modularBudget = budget.getModularBudget();
305:
306: assertFalse(modularBudget.isInvalidMode());
307: for (Iterator iter = modularBudget.getBudgetModularPeriods()
308: .iterator(); iter.hasNext();) {
309: BudgetModularPeriod modularPeriod = (BudgetModularPeriod) iter
310: .next();
311: assertEquals(modularPeriod
312: .getBudgetAdjustedModularDirectCostAmount(),
313: new KualiInteger(0));
314: }
315:
316: // Case 2: Budget with personnel, nonpersonnel, consortium costs
317: budget = setupBudget();
318:
319: String[] categories = { "CO", "CO", "FL", "SC" };
320: String[] subCategories = { "C1", "C1", "F5", "R2" };
321: String[] subcontractorNumber = { "", "", "", "1" };
322: List nonpersonnelItems = BudgetNonpersonnelTest
323: .createBudgetNonpersonnel(categories, subCategories,
324: subcontractorNumber);
325: for (Iterator iter = nonpersonnelItems.iterator(); iter
326: .hasNext();) {
327: BudgetNonpersonnel nonpersonnel = (BudgetNonpersonnel) iter
328: .next();
329: nonpersonnel.setBudgetPeriodSequenceNumber(new Integer(2));
330: }
331: budget.setNonpersonnelItems(nonpersonnelItems);
332:
333: List userAppointmentTaskPeriods = new ArrayList();
334:
335: BudgetPeriod period1 = (BudgetPeriod) budget.getPeriods()
336: .get(0);
337:
338: UserAppointmentTaskPeriod taskPeriod = new UserAppointmentTaskPeriod();
339: taskPeriod.setBudgetPeriodSequenceNumber(period1
340: .getBudgetPeriodSequenceNumber());
341: taskPeriod.setAgencyRequestTotalAmount(new KualiInteger(39000));
342: taskPeriod.setAgencyFringeBenefitTotalAmount(new KualiInteger(
343: 13000));
344: userAppointmentTaskPeriods.add(taskPeriod);
345:
346: UserAppointmentTaskPeriod taskPeriod2 = new UserAppointmentTaskPeriod();
347: taskPeriod2.setBudgetPeriodSequenceNumber(period1
348: .getBudgetPeriodSequenceNumber());
349: taskPeriod2
350: .setAgencyRequestTotalAmount(new KualiInteger(43000));
351: taskPeriod2.setAgencyFringeBenefitTotalAmount(new KualiInteger(
352: 8500));
353: userAppointmentTaskPeriods.add(taskPeriod2);
354:
355: BudgetPeriod period2 = (BudgetPeriod) budget.getPeriods()
356: .get(1);
357:
358: UserAppointmentTaskPeriod taskPeriod3 = new UserAppointmentTaskPeriod();
359: taskPeriod3.setBudgetPeriodSequenceNumber(period2
360: .getBudgetPeriodSequenceNumber());
361: taskPeriod3
362: .setAgencyRequestTotalAmount(new KualiInteger(74000));
363: taskPeriod3.setAgencyFringeBenefitTotalAmount(new KualiInteger(
364: 21500));
365: userAppointmentTaskPeriods.add(taskPeriod3);
366:
367: budget
368: .setAllUserAppointmentTaskPeriods(userAppointmentTaskPeriods);
369:
370: SpringContext.getBean(BudgetModularService.class)
371: .resetModularBudget(budget);
372: modularBudget = budget.getModularBudget();
373:
374: assertFalse(modularBudget.isInvalidMode());
375:
376: BudgetModularPeriod modularPeriod1 = (BudgetModularPeriod) modularBudget
377: .getBudgetModularPeriods().get(0);
378: assertEquals(modularPeriod1
379: .getBudgetAdjustedModularDirectCostAmount(),
380: new KualiInteger(125000));
381:
382: BudgetModularPeriod modularPeriod2 = (BudgetModularPeriod) modularBudget
383: .getBudgetModularPeriods().get(1);
384: assertEquals(modularPeriod2
385: .getBudgetAdjustedModularDirectCostAmount(),
386: new KualiInteger(125000));
387:
388: // Case 3: Budget with costs > # of periods * period maximum
389: budget = setupBudget();
390:
391: BudgetPeriod periodInvalid = (BudgetPeriod) budget.getPeriods()
392: .get(0);
393:
394: UserAppointmentTaskPeriod taskPeriod4 = new UserAppointmentTaskPeriod();
395: taskPeriod4.setBudgetPeriodSequenceNumber(periodInvalid
396: .getBudgetPeriodSequenceNumber());
397: taskPeriod4.setAgencyRequestTotalAmount(new KualiInteger(
398: 1000000));
399: userAppointmentTaskPeriods = new ArrayList();
400: userAppointmentTaskPeriods.add(taskPeriod4);
401: budget
402: .setAllUserAppointmentTaskPeriods(userAppointmentTaskPeriods);
403:
404: SpringContext.getBean(BudgetModularService.class)
405: .resetModularBudget(budget);
406: modularBudget = budget.getModularBudget();
407:
408: BudgetModularPeriod modularPeriodInvalid1 = (BudgetModularPeriod) modularBudget
409: .getBudgetModularPeriods().get(0);
410: assertNull(modularPeriodInvalid1
411: .getBudgetAdjustedModularDirectCostAmount());
412:
413: BudgetModularPeriod modularPeriodInvalid2 = (BudgetModularPeriod) modularBudget
414: .getBudgetModularPeriods().get(1);
415: assertNull(modularPeriodInvalid2
416: .getBudgetAdjustedModularDirectCostAmount());
417: }
418:
419: public void testAgencySupportsModular() {
420:
421: // Case 1: Agency is null.
422: assertFalse(SpringContext.getBean(BudgetModularService.class)
423: .agencySupportsModular(null));
424:
425: // Case 2: Supports modular is true.
426: Agency agency = new Agency();
427: AgencyExtension agencyExtension = new AgencyExtension();
428: agencyExtension.setAgencyModularIndicator(true);
429: agency.setAgencyExtension(agencyExtension);
430: assertTrue(SpringContext.getBean(BudgetModularService.class)
431: .agencySupportsModular(agency));
432:
433: // Case 3: Supports modular is false.
434: agency.getAgencyExtension().setAgencyModularIndicator(false);
435: assertFalse(SpringContext.getBean(BudgetModularService.class)
436: .agencySupportsModular(agency));
437:
438: // Case 5: Agency extension and reports to agency both null.
439: agency.setAgencyExtension(null);
440: assertFalse(SpringContext.getBean(BudgetModularService.class)
441: .agencySupportsModular(agency));
442: }
443: }
|