View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.kpme.tklm.leave.accrual;
17  
18  import org.joda.time.Interval;
19  import org.joda.time.LocalDate;
20  import org.junit.After;
21  import org.junit.Before;
22  import org.junit.Test;
23  import org.kuali.kpme.core.IntegrationTest;
24  import org.kuali.kpme.core.api.accrualcategory.rule.AccrualCategoryRuleContract;
25  import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
26  import org.kuali.kpme.core.service.HrServiceLocator;
27  import org.kuali.kpme.core.util.HrConstants;
28  import org.kuali.kpme.core.util.TKUtils;
29  import org.kuali.kpme.tklm.TKLMIntegrationTestCase;
30  import org.kuali.kpme.tklm.api.leave.block.LeaveBlock;
31  import org.kuali.kpme.tklm.api.leave.block.LeaveBlock;
32  import org.kuali.kpme.tklm.api.leave.override.EmployeeOverrideContract;
33  import org.kuali.kpme.tklm.api.leave.summary.LeaveSummaryContract;
34  import org.kuali.kpme.tklm.api.leave.summary.LeaveSummaryRowContract;
35  import org.kuali.kpme.tklm.common.LMConstants;
36  import org.kuali.kpme.tklm.leave.block.LeaveBlockBo;
37  import org.kuali.kpme.tklm.leave.calendar.LeaveCalendarDocument;
38  import org.kuali.kpme.tklm.leave.override.EmployeeOverride;
39  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
40  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
41  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
42  import org.kuali.rice.krad.util.ObjectUtils;
43  
44  import java.math.BigDecimal;
45  import java.util.ArrayList;
46  import java.util.List;
47  import java.util.Map;
48  import java.util.Set;
49  
50  import static org.junit.Assert.*;
51  
52  @IntegrationTest
53  public class AccrualCategoryMaxBalanceServiceTest extends TKLMIntegrationTestCase {
54  
55  
56  	/**
57  	 * Leave Calendar Document Test data
58  	 */
59  	private final String USER_ID = "testUser1";
60  	
61  	private LeaveCalendarDocument janLCD;
62  	private CalendarEntry janEntry;
63  	private LeaveCalendarDocument decLCD;
64  	private CalendarEntry decEntry;
65  	
66  	private final String JAN_ID = "5001";
67  	private final String DEC_ID = "5000";
68  	
69  	/**
70  	 * Timesheet Document Test Data;
71  	 */
72  	
73  	private final String TS_USER_ID = "testUser2";	
74  	
75  	private TimesheetDocument endJanTSD;
76  	private CalendarEntry endJanTSDEntry;
77  	private TimesheetDocument midJanTSD;
78  	private CalendarEntry midJanTSDEntry;
79  	private TimesheetDocument endDecTSD;
80  	private CalendarEntry endDecTSDEntry;
81  	private TimesheetDocument midDecTSD;
82  	private CalendarEntry midDecTSDEntry;
83  	
84  	private final String TSD_MID_DEC_PERIOD_ID = "5000";
85  	private final String TSD_END_DEC_PERIOD_ID = "5001";
86  	private final String TSD_MID_JAN_PERIOD_ID = "5002";
87  	private final String TSD_END_JAN_PERIOD_ID = "5003";
88  
89  	/**
90  	 *  Common data
91  	 */
92  	
93  	private final String OD_XFER = "5000";
94  	private final String YE_XFER = "5001";
95  	private final String LA_XFER = "5002";
96  	private final String OD_XFER_MAC = "5003";
97  	private final String YE_XFER_MAC = "5004";
98  	private final String LA_XFER_MAC = "5005";
99  	private final String OD_LOSE = "5006";
100 	private final String YE_LOSE = "5007";
101 	private final String LA_LOSE = "5008";
102 	private final String OD_LOSE_MAC = "5009";
103 	private final String YE_LOSE_MAC = "5010";
104 	private final String LA_LOSE_MAC = "5011";
105 	private final String YE_XFER_EO = "5012";
106 	private final LocalDate LM_FROM = TKUtils.formatDateString("11/01/2012");
107 	private final LocalDate LM_TO = TKUtils.formatDateString("02/01/2013");
108 	private final LocalDate TK_FROM = TKUtils.formatDateString("11/01/2011");
109 	private final LocalDate TK_TO = TKUtils.formatDateString("02/01/2012");
110 	
111 	@Before
112 	public void setUp() throws Exception {
113 		super.setUp();
114 		LmServiceLocator.getAccrualService().runAccrual(USER_ID,LM_FROM.toDateTimeAtStartOfDay(),LM_TO.toDateTimeAtStartOfDay(),true,USER_ID);
115 		janLCD = LmServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(JAN_ID);
116 		janEntry = janLCD.getCalendarEntry();
117 		decLCD = LmServiceLocator.getLeaveCalendarService().getLeaveCalendarDocument(DEC_ID);
118 		decEntry = decLCD.getCalendarEntry();
119 	}
120 	
121 	@After
122 	public void tearDown() throws Exception {
123 		super.tearDown();
124 	}
125 	
126 	@Test
127 	public void testGetMaxBalanceViolationsLeaveApprove() throws Exception {
128 		Map<String, Set<LeaveBlock>> maxBalanceViolations = eligibilityTestHelper(janEntry, USER_ID);
129 		List<LeaveBlock> eligibleTransfers = new ArrayList<LeaveBlock>();
130 		Interval interval = new Interval(janEntry.getBeginPeriodFullDateTime(),janEntry.getEndPeriodFullDateTime());
131 		
132 		for(LeaveBlock violation : maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE)) {
133 			if(interval.contains(violation.getLeaveDateTime())) {
134                 eligibleTransfers.add(violation);
135             }
136 		}
137 		// 6 accrual categories over max balance * 3 leave blocks ( 2 created on leave period end date + 1 created on calendar
138 		// year end date )
139 		assertEquals(6, eligibleTransfers.size());
140 		//assertEquals(8, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.YEAR_END).size());
141 		//assertEquals(6, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.ON_DEMAND).size());
142 	}
143 
144 	@Test
145 	public void testGetMaxBalanceViolationsYearEnd() throws Exception {
146 		Map<String, Set<LeaveBlock>> maxBalanceViolations = eligibilityTestHelper(janEntry, USER_ID);
147 		//assertEquals(6, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE).size());
148 		assertEquals(8, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.YEAR_END).size());
149 		//assertEquals(6, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.ON_DEMAND).size());
150 		List<AccrualCategoryRuleContract> rules = new ArrayList<AccrualCategoryRuleContract>();
151 		for(LeaveBlock eligibleTransfer : maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.YEAR_END))
152 			rules.add(eligibleTransfer.getAccrualCategoryRule());
153 		
154 		// Set should contain an accrual category whose rule's max balance is trumped by an employee override.
155 		// Comparing accrued balance to a rule's defined max balance is insufficient for testing
156 		// whether or not an accrual category is indeed over it's balance limit. Same can be said for FTE-proration.
157 		LeaveSummaryContract summary = LmServiceLocator.getLeaveSummaryService().getLeaveSummary(USER_ID, janEntry);
158 		for(AccrualCategoryRuleContract aRule : rules) {
159 			LeaveSummaryRowContract row = summary.getLeaveSummaryRowForAccrualCategory(aRule.getLmAccrualCategoryId());
160 			BigDecimal maxBalance = aRule.getMaxBalance();
161             EmployeeOverrideContract mbOverride = LmServiceLocator.getEmployeeOverrideService().getEmployeeOverride(USER_ID,
162 					"testLP",
163 					row.getAccrualCategory(),
164 					"MB",
165 					janEntry.getBeginPeriodFullDateTime().toLocalDate());
166 			if(ObjectUtils.isNotNull(mbOverride))
167 				maxBalance = new BigDecimal(mbOverride.getOverrideValue());
168 			assertNotNull("eligible accrual category has no balance limit",maxBalance);
169 			assertTrue("accrual category not eligible for transfer",row.getAccruedBalance().compareTo(maxBalance) > 0);
170 		}
171 	}
172 	
173 	@Test
174 	public void testGetMaxBalanceViolationsOnDemand() throws Exception {
175 		Map<String, Set<LeaveBlock>> maxBalanceViolations = eligibilityTestHelper(janEntry, USER_ID);
176 		
177 		assertEquals(6, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.ON_DEMAND).size());
178 
179 		List<AccrualCategoryRuleContract> rules = new ArrayList<AccrualCategoryRuleContract>();
180 		for(LeaveBlock eligibleTransfer : maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.ON_DEMAND))
181 			rules.add(eligibleTransfer.getAccrualCategoryRule());
182 
183 		LeaveSummaryContract summary = LmServiceLocator.getLeaveSummaryService().getLeaveSummary(USER_ID, janEntry);
184 		for(AccrualCategoryRuleContract aRule : rules) {
185 			LeaveSummaryRowContract row = summary.getLeaveSummaryRowForAccrualCategory(aRule.getLmAccrualCategoryId());
186 			assertNotNull("eligible accrual category has no balance limit",ObjectUtils.isNotNull(aRule.getMaxBalance()));
187 			assertTrue("accrual category not eligible for transfer",row.getAccruedBalance().compareTo(aRule.getMaxBalance()) > 0);
188 		}
189 	}
190 	
191 	@Test
192 	public void testGetMaxBalanceViolationsOnYearEndCaseOne() throws Exception {
193 		//calendar entry is not the last calendar entry of the leave plan's calendar year.
194 		Map<String, Set<LeaveBlock>> maxBalanceViolations = eligibilityTestHelper(decEntry, USER_ID);
195 		//assertEquals(6, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE).size());
196 		assertEquals(8, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.YEAR_END).size());
197 		//assertEquals(6, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.ON_DEMAND).size());
198 		
199 		LeaveBlockBo usage = new LeaveBlockBo();
200 		
201 		usage.setAccrualCategory("ye-xfer");
202 		usage.setAccrualGenerated(true);
203 		usage.setLeaveAmount(new BigDecimal(-34));
204 		usage.setLeaveLocalDate(TKUtils.formatDateString("12/28/2012"));
205 		usage.setDocumentId(DEC_ID);
206 		usage.setPrincipalId(USER_ID);
207 		usage.setRequestStatus(HrConstants.REQUEST_STATUS.APPROVED);
208 		usage.setEarnCode("EC2");
209 		usage.setLeaveBlockType(LMConstants.LEAVE_BLOCK_TYPE.BALANCE_TRANSFER);
210 		usage.setBlockId(0L);
211 		
212 		/*KRADServiceLocatorWeb.getLegacyDataAdapter().save(usage);*/
213 		LmServiceLocator.getLeaveBlockService().saveLeaveBlock(LeaveBlockBo.to(usage), TS_USER_ID);
214 		
215 		maxBalanceViolations = eligibilityTestHelper(decEntry, USER_ID);
216 
217 		//The above leave block should reduce balance of ye-xfer under max limit, reducing the number of violations by 1.
218 		assertEquals(7, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.YEAR_END).size());
219 		
220 		// adding an accrual block beyond the underlying leave calendar end date, but still within the
221 		// bounds of the time period, should not re-mark this accrual category over max balance.
222 /*		usage = new LeaveBlock();
223 		
224 		usage.setAccrualCategory("la-xfer");
225 		usage.setAccrualGenerated(false);
226 		usage.setLeaveAmount(new BigDecimal(10));
227 		usage.setLeaveDate(TKUtils.formatDateString("01/01/2012"));
228 		usage.setDocumentId(TSD_END_DEC_PERIOD_ID);
229 		usage.setPrincipalId(TS_USER_ID);
230 		usage.setRequestStatus(HrConstants.REQUEST_STATUS.APPROVED);
231 		usage.setEarnCode("EC1");
232 		usage.setLeaveBlockType(LMConstants.LEAVE_BLOCK_TYPE.BALANCE_TRANSFER);
233 		usage.setBlockId(0L);
234 		
235 		KRADServiceLocatorWeb.getLegacyDataAdapter().save(usage);
236 		
237 		assertEquals(5, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE).size());*/
238 		
239 	}
240 	
241 	@Test
242 	public void testGetMaxBalanceViolationsOnYearEndCaseTwo() throws Exception {
243 		//calendar entry is the last calendar entry of the leave plan's calendar year.
244 		Map<String, Set<LeaveBlock>> maxBalanceViolations = eligibilityTestHelper(janEntry, USER_ID);
245 		//assertEquals(6, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE).size());
246 		assertEquals(8, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.YEAR_END).size());
247 		//assertEquals(6, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.ON_DEMAND).size());
248 	}
249 	
250 	/**
251 	 * 
252 	 * TIMESHEET ELIGIBLE TESTS
253 	 * 
254 	 */
255 	
256 	@Test
257 	public void testGetMaxBalanceViolationsLeaveApproveForTimesheetCaseOne() throws Exception {
258 		//Timesheet contains leave period ending 12/11/2011
259 		//accruals occur on 12/01/2011
260 		LmServiceLocator.getAccrualService().runAccrual(TS_USER_ID,TK_FROM.toDateTimeAtStartOfDay(),TK_TO.toDateTimeAtStartOfDay(),true,TS_USER_ID);
261 		midDecTSD = TkServiceLocator.getTimesheetService().getTimesheetDocument(TSD_MID_DEC_PERIOD_ID);
262 		midDecTSDEntry = midDecTSD.getCalendarEntry();
263 
264 		Map<String, Set<LeaveBlock>> maxBalanceViolations = eligibilityTestHelper(midDecTSDEntry, TS_USER_ID);
265 		
266 		assertEquals(6, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE).size());
267 		//Assert correct number of transfer eligible for frequency
268 		List<LeaveBlock> eligibleLeaveApproveActions = new ArrayList<LeaveBlock>();
269 		Interval interval = new Interval(midDecTSDEntry.getBeginPeriodFullDateTime(),midDecTSDEntry.getEndPeriodFullDateTime());
270 		List<CalendarEntry> entries = HrServiceLocator.getCalendarEntryService().getCalendarEntriesEndingBetweenBeginAndEndDate("3", interval.getStart(), interval.getEnd());
271 		assertEquals("There should be one leave entry ending within this timesheet", 1, entries.size());
272 		Interval leaveInterval = new Interval(midDecTSDEntry.getBeginPeriodFullDateTime(),entries.get(0).getEndPeriodFullDateTime());
273 		for(LeaveBlock violation : maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE)) {
274 			if(leaveInterval.contains(violation.getLeaveDateTime())) {
275 				eligibleLeaveApproveActions.add(violation);
276 			}
277 		}
278 		assertEquals(6, eligibleLeaveApproveActions.size());
279 	}
280 	
281 	@Test
282 	public void testGetMaxBalanceViolationsYearEndForTimesheetCaseOne() throws Exception {
283 		//Timesheet contains leave period ending 12/11/2011
284 		//this is not the last leave period of the leave plan's calendar year.
285 		LmServiceLocator.getAccrualService().runAccrual(TS_USER_ID,TK_FROM.toDateTimeAtStartOfDay(),TK_TO.toDateTimeAtStartOfDay(),true,TS_USER_ID);
286 		midDecTSD = TkServiceLocator.getTimesheetService().getTimesheetDocument(TSD_MID_DEC_PERIOD_ID);
287 		midDecTSDEntry = midDecTSD.getCalendarEntry();
288 
289 		Map<String, Set<LeaveBlock>> maxBalanceViolations = eligibilityTestHelper(midDecTSDEntry, TS_USER_ID);
290 		//Assert correct number of transfer eligible for frequency
291 		assertEquals(8, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.YEAR_END).size());
292 		List<LeaveBlock> eligibleYearEndActions = new ArrayList<LeaveBlock>();
293 		Interval interval = new Interval(midDecTSDEntry.getBeginPeriodFullDateTime(),midDecTSDEntry.getEndPeriodFullDateTime());
294 		List<CalendarEntry> entries = HrServiceLocator.getCalendarEntryService().getCalendarEntriesEndingBetweenBeginAndEndDate("3", interval.getStart(), interval.getEnd());
295 		assertEquals("There should be one leave entry ending within this timesheet", 1, entries.size());
296 		Interval leaveInterval = new Interval(midDecTSDEntry.getBeginPeriodFullDateTime(),entries.get(0).getEndPeriodFullDateTime());
297 		for(LeaveBlock violation : maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.YEAR_END)) {
298 			if(leaveInterval.contains(violation.getLeaveDateTime())
299 				&& HrServiceLocator.getLeavePlanService().isLastCalendarPeriodOfLeavePlan(entries.get(0), "testLP", TK_TO)) {
300 				eligibleYearEndActions.add(violation);
301 			}
302 		}
303 		assertEquals(0, eligibleYearEndActions.size());
304 	}
305 	
306 	@Test
307 	public void testGetMaxBalanceViolationsOnDemandForTimesheetCaseOne() throws Exception {
308 		//Timesheet contains leave period ending 12/11/2011
309 		LmServiceLocator.getAccrualService().runAccrual(TS_USER_ID,TK_FROM.toDateTimeAtStartOfDay(),TK_TO.toDateTimeAtStartOfDay(),true,TS_USER_ID);
310 		midDecTSD = TkServiceLocator.getTimesheetService().getTimesheetDocument(TSD_MID_DEC_PERIOD_ID);
311 		midDecTSDEntry = midDecTSD.getCalendarEntry();
312 
313 		Map<String, Set<LeaveBlock>> maxBalanceViolations = eligibilityTestHelper(midDecTSDEntry, TS_USER_ID);
314 		//Assert correct number of transfer eligible for frequency
315 		assertEquals(6, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.ON_DEMAND).size());
316 	}
317 	
318 	@Test
319 	public void testGetMaxBalanceViolationsLeaveApproveForTimesheetCaseThree() throws Exception {
320 		//Timesheet contains leave period ending 12/25/2011
321 		LmServiceLocator.getAccrualService().runAccrual(TS_USER_ID,TK_FROM.toDateTimeAtStartOfDay(),TK_TO.toDateTimeAtStartOfDay(),true,TS_USER_ID);
322 		endDecTSD = TkServiceLocator.getTimesheetService().getTimesheetDocument(TSD_END_DEC_PERIOD_ID);
323 		endDecTSDEntry = endDecTSD.getCalendarEntry();
324 
325 		Map<String, Set<LeaveBlock>> maxBalanceViolations = eligibilityTestHelper(endDecTSDEntry, TS_USER_ID);
326 
327 		assertEquals("Incorrect number of max balance violations", 6, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE).size());
328 		//Assert correct base number of transfer eligible for frequency
329 		List<LeaveBlock> eligibleLeaveApproveActions = new ArrayList<LeaveBlock>();
330 		Interval interval = new Interval(endDecTSDEntry.getBeginPeriodFullDateTime(),endDecTSDEntry.getEndPeriodFullDateTime());
331 		List<CalendarEntry> entries = HrServiceLocator.getCalendarEntryService().getCalendarEntriesEndingBetweenBeginAndEndDate("3", interval.getStart(), interval.getEnd());
332 		assertTrue("There should only be one leave period that ends within any given time calendar", entries.size() == 1);
333 		CalendarEntry leaveEntry = entries.get(0);
334 		Interval leaveInterval = new Interval(endDecTSDEntry.getBeginPeriodFullDateTime(),leaveEntry.getEndPeriodFullDateTime());
335 		for(LeaveBlock violation : maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE)) {
336 			if(leaveInterval.contains(violation.getLeaveDateTime())) {
337 				eligibleLeaveApproveActions.add(violation);
338 			}
339 		}
340 		assertEquals("Incorrect number of eligible leave approve actions", 6, eligibleLeaveApproveActions.size());
341 
342 		LeaveBlockBo usage = new LeaveBlockBo();
343 		
344 		usage.setAccrualCategory("la-xfer");
345 		usage.setAccrualGenerated(false);
346 		usage.setLeaveAmount(new BigDecimal(-30));
347 		// 12/15/2011 <= leave date < 12/25/2011
348 		usage.setLeaveLocalDate(TKUtils.formatDateString("12/23/2011"));
349 		usage.setDocumentId(TSD_END_DEC_PERIOD_ID);
350 		usage.setPrincipalId(TS_USER_ID);
351 		usage.setRequestStatus(HrConstants.REQUEST_STATUS.APPROVED);
352 		usage.setEarnCode("EC1");
353 		usage.setLeaveBlockType(LMConstants.LEAVE_BLOCK_TYPE.BALANCE_TRANSFER);
354 		usage.setBlockId(0L);
355 		
356 		//KRADServiceLocatorWeb.getLegacyDataAdapter().save(usage);
357         LmServiceLocator.getLeaveBlockService().saveLeaveBlock(LeaveBlockBo.to(usage), TS_USER_ID);
358 		
359 		
360 		
361 		maxBalanceViolations = eligibilityTestHelper(endDecTSDEntry, TS_USER_ID);
362 		assertEquals("Incorrect number of max balance violation", 5, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE).size());
363 
364 		//The above leave block should remove la-xfer from eligibility, reducing the number of eligibilities by 1.
365 		List<LeaveBlock> eligibleTransfers = new ArrayList<LeaveBlock>();
366 		
367 		for(LeaveBlock violation : maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE)) {
368 			if(leaveInterval.contains(violation.getLeaveDateTime())) {
369 				eligibleTransfers.add(violation);
370 			}
371 		}
372 		//actual reduction is by 2...
373 		assertEquals(5, eligibleTransfers.size());
374 		
375 		// adding an accrual block beyond the underlying leave calendar end date, but still within the
376 		// bounds of the time period, should re-mark this accrual category over max balance, but should not be eligible for action
377 		usage = new LeaveBlockBo();
378 		
379 		usage.setAccrualCategory("la-xfer");
380 		usage.setAccrualGenerated(false);
381 		usage.setLeaveAmount(new BigDecimal(30));
382 		usage.setLeaveLocalDate(TKUtils.formatDateString("12/28/2011"));
383 		usage.setDocumentId(TSD_END_DEC_PERIOD_ID);
384 		usage.setPrincipalId(TS_USER_ID);
385 		usage.setRequestStatus(HrConstants.REQUEST_STATUS.APPROVED);
386 		usage.setEarnCode("EC1");
387 		usage.setLeaveBlockType(LMConstants.LEAVE_BLOCK_TYPE.BALANCE_TRANSFER);
388 		usage.setBlockId(0L);
389 		
390 	 //KRADServiceLocatorWeb.getLegacyDataAdapter().save(usage);
391         LmServiceLocator.getLeaveBlockService().saveLeaveBlock(LeaveBlockBo.to(usage), TS_USER_ID);
392 		
393 		maxBalanceViolations = eligibilityTestHelper(endDecTSDEntry, TS_USER_ID);
394 		assertEquals("Incorrect number of leave approve violations", 5, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE).size());
395 		eligibleTransfers = new ArrayList<LeaveBlock>();
396 		
397 		for(LeaveBlock violation : maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE)) {
398 			if(leaveInterval.contains(violation.getLeaveDateTime())) {
399 				eligibleTransfers.add(violation);
400 			}
401 		}
402 		assertEquals(5, eligibleTransfers.size());
403 	}
404 	
405 	@Test
406 	public void testGetMaxBalanceViolationsLeaveApproveForTimesheetCaseTwo() throws Exception {
407 		//Timesheet includes the leave calendar end period, but does not include the leave plan's start date.
408 		LmServiceLocator.getAccrualService().runAccrual(TS_USER_ID,TK_FROM.toDateTimeAtStartOfDay(),TK_TO.toDateTimeAtStartOfDay(),true,TS_USER_ID);
409 		endDecTSD = TkServiceLocator.getTimesheetService().getTimesheetDocument(TSD_END_DEC_PERIOD_ID);
410 		endDecTSDEntry = endDecTSD.getCalendarEntry();
411 
412 		Map<String, Set<LeaveBlock>> maxBalanceViolations = eligibilityTestHelper(endDecTSDEntry, TS_USER_ID);
413 		//Assert correct number of violations
414 		assertEquals("Incorrect number of leave approve violations", 6, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE).size());
415 
416 		List<LeaveBlock> eligibleTransfers = new ArrayList<LeaveBlock>();
417 		Interval interval = new Interval(endDecTSDEntry.getBeginPeriodFullDateTime(),endDecTSDEntry.getEndPeriodFullDateTime());
418 		List<CalendarEntry> entries = HrServiceLocator.getCalendarEntryService().getCalendarEntriesEndingBetweenBeginAndEndDate("3", interval.getStart(), interval.getEnd());
419 		assertTrue("There should only be one leave period that ends within any given time calendar", entries.size() == 1);
420 		CalendarEntry leaveEntry = entries.get(0);
421 		Interval leaveInterval = new Interval(endDecTSDEntry.getBeginPeriodFullDateTime(),leaveEntry.getEndPeriodFullDateTime());
422 		for(LeaveBlock violation : maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE)) {
423 			if(leaveInterval.contains(violation.getLeaveDateTime())) {
424 				eligibleTransfers.add(violation);
425 			}
426 		}
427 		//Assert correct number of actions eligible for frequency
428 		assertEquals(6, eligibleTransfers.size());
429 	}
430 	
431 	@Test
432 	public void testGetMaxBalanceViolationsYearEndForTimesheetCaseTwo() throws Exception {
433 		//Timesheet includes the leave calendar end period, but does not include the leave plan's start date.
434 		LmServiceLocator.getAccrualService().runAccrual(TS_USER_ID,TK_FROM.toDateTimeAtStartOfDay(),TK_TO.toDateTimeAtStartOfDay(),true,TS_USER_ID);
435 		endDecTSD = TkServiceLocator.getTimesheetService().getTimesheetDocument(TSD_END_DEC_PERIOD_ID);
436 		endDecTSDEntry = endDecTSD.getCalendarEntry();
437 
438 		Map<String, Set<LeaveBlock>> maxBalanceViolations = eligibilityTestHelper(endDecTSDEntry, TS_USER_ID);
439 		//Assert correct number of transfer eligible for frequency
440 		assertEquals(8, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.YEAR_END).size());
441 		
442 		List<LeaveBlock> eligibleTransfers = new ArrayList<LeaveBlock>();
443 		Interval interval = new Interval(endDecTSDEntry.getBeginPeriodFullDateTime(),endDecTSDEntry.getEndPeriodFullDateTime());
444 		List<CalendarEntry> entries = HrServiceLocator.getCalendarEntryService().getCalendarEntriesEndingBetweenBeginAndEndDate("3", interval.getStart(), interval.getEnd());
445 		assertTrue("There should only be one leave period that ends within any given time calendar", entries.size() == 1);
446 		CalendarEntry leaveEntry = entries.get(0);
447 		Interval leaveInterval = new Interval(endDecTSDEntry.getBeginPeriodFullDateTime(),leaveEntry.getEndPeriodFullDateTime());
448 		for(LeaveBlock violation : maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.YEAR_END)) {
449 			if(leaveInterval.contains(violation.getLeaveDateTime())) {
450 				eligibleTransfers.add(violation);
451 			}
452 		}
453 		
454 		assertEquals("There should be no eligible max balance actions", 8, eligibleTransfers.size());
455 	}
456 	
457 	@Test
458 	public void testGetMaxBalanceViolationsYearEndForTimesheetCaseThree() throws Exception {
459 		//Timesheet includes the leave calendar end period, which is the leave plan's final period.
460 		LmServiceLocator.getAccrualService().runAccrual(TS_USER_ID,TK_FROM.toDateTimeAtStartOfDay(),TK_TO.toDateTimeAtStartOfDay(),true,TS_USER_ID);
461 		endJanTSD = TkServiceLocator.getTimesheetService().getTimesheetDocument(TSD_END_JAN_PERIOD_ID);
462 		endJanTSDEntry = endJanTSD.getCalendarEntry();
463 
464 		Map<String, Set<LeaveBlock>> maxBalanceViolations = eligibilityTestHelper(endJanTSDEntry, TS_USER_ID);
465 
466 		assertEquals(8, maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.YEAR_END).size());
467 
468 		//Assert correct number of transfer eligible for frequency
469 		List<LeaveBlock> eligibleTransfers = new ArrayList<LeaveBlock>();
470 		Interval interval = new Interval(endJanTSDEntry.getBeginPeriodFullDateTime(),endJanTSDEntry.getEndPeriodFullDateTime());
471 		List<CalendarEntry> entries = HrServiceLocator.getCalendarEntryService().getCalendarEntriesEndingBetweenBeginAndEndDate("3", interval.getStart(), interval.getEnd());
472 		assertTrue("There should only be one leave period that ends within any given time calendar", entries.size() == 1);
473 		CalendarEntry leaveEntry = entries.get(0);
474 		Interval leaveInterval = new Interval(endJanTSDEntry.getBeginPeriodFullDateTime(),leaveEntry.getEndPeriodFullDateTime());
475 		for(LeaveBlock violation : maxBalanceViolations.get(HrConstants.MAX_BAL_ACTION_FREQ.YEAR_END)) {
476 			if(leaveInterval.contains(violation.getLeaveDateTime())) {
477 				eligibleTransfers.add(violation);
478 			}
479 		}
480 		
481 		assertEquals(8, eligibleTransfers.size());
482 	}
483 	
484 	
485 	private Map<String, Set<LeaveBlock>> eligibilityTestHelper(
486 			CalendarEntry entry, String principalId) throws Exception {
487 		return LmServiceLocator.getAccrualCategoryMaxBalanceService().getMaxBalanceViolations(entry, principalId);
488 	}
489 }