001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.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/ecl2.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.hr.time.accrual.service;
017    
018    import java.math.BigDecimal;
019    import java.sql.Date;
020    import java.util.Calendar;
021    import java.util.List;
022    
023    import org.joda.time.DateTime;
024    import org.junit.Assert;
025    import org.junit.Test;
026    import org.kuali.hr.lm.LMConstants;
027    import org.kuali.hr.lm.leaveblock.LeaveBlock;
028    import org.kuali.hr.lm.leaveblock.LeaveBlockHistory;
029    import org.kuali.hr.test.KPMETestCase;
030    import org.kuali.hr.time.service.base.TkServiceLocator;
031    import org.kuali.hr.time.util.TKUtils;
032    
033    public class AccrualServiceTest extends KPMETestCase {
034             Date START_DATE = new Date((new DateTime(2012, 2, 20, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
035             Date END_DATE = new Date((new DateTime(2012, 5, 3, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
036            
037            @Test
038            /*the employee has job A starts from 03/01/2012, ends on 04/01/2012 with standard hours of 40, so fte is 1.0
039             * job B starts on 04/01/2012, no end to it with standard hours of 20, fte is 0.5
040             * on 04/10/2012, there's a system scheduled time off of 8 hrs
041             */
042            public void testRunAccrualForStatusChange() {
043                     String principal_id = "testUser";
044                     List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, END_DATE);
045                     Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
046                     
047                     // first time to run accrual from 02/20/2012 to 05/03/2012
048                     TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, END_DATE, false);
049                     verifyLeaveBlocksForStatusChange();
050                     
051                     List<LeaveBlockHistory> historyList = TkServiceLocator.getLeaveBlockHistoryService().getLeaveBlockHistories(principal_id, null);
052                     Assert.assertTrue("There should be 5 leave block history for emplyee " + principal_id + ", not " + historyList.size(), historyList.size()== 5);
053                     LeaveBlockHistory lbh = historyList.get(0);
054                     Assert.assertTrue("Leave Block Type of leave block history should be " + LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE + ", not " + lbh.getLeaveBlockType()
055                                            , lbh.getLeaveBlockType().equals(LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE));
056                     
057                     // second time to run accrual from 02/20/2012 to 05/03/2012, should get the same results as first run
058                     TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, END_DATE, false);
059                     verifyLeaveBlocksForStatusChange();
060                     
061                     historyList = TkServiceLocator.getLeaveBlockHistoryService().getLeaveBlockHistories(principal_id, null);
062                     Assert.assertTrue("There should be 15 leave block history for employee " + principal_id + ", not " + historyList.size(), historyList.size()== 15);
063            }
064            private void verifyLeaveBlocksForStatusChange() {
065                    String principal_id = "testUser";
066                    List<LeaveBlock> leaveBlockList;
067                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, END_DATE);
068                    Assert.assertFalse("No leave blocks created by runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
069                    Assert.assertTrue("There should be 5 leave blocks for emplyee 'testUser', not " + leaveBlockList.size(), leaveBlockList.size()== 5);
070                     
071                    // there should be one leave block of 16 hours on 03/31/2012
072                    Date intervalDate = new Date((new DateTime(2012, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
073                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
074                    Assert.assertTrue("There should be 1 leave block for date 03/31/2012.", leaveBlockList.size()==1);
075                    LeaveBlock lb = leaveBlockList.get(0);
076                    Assert.assertTrue("Hours of the leave block for date 03/31/2012 should be 16, not " + lb.getLeaveAmount().toString(), lb.getLeaveAmount().equals(new BigDecimal(16)));
077                    Assert.assertNull("lm_sys_schd_timeoff_id should be null for regular accrual leave block", lb.getScheduleTimeOffId());
078                    Assert.assertTrue("Leave Block Type of leave block should be " + LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE + ", not " + lb.getLeaveBlockType()
079                                    , lb.getLeaveBlockType().equals(LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE));
080                    Assert.assertTrue("Requst status of leave block should be " + LMConstants.REQUEST_STATUS.APPROVED + ", not " + lb.getRequestStatus()
081                                    , lb.getRequestStatus().equals(LMConstants.REQUEST_STATUS.APPROVED));
082                    
083                    // employee changed status on 04/01, fte is changed from 1 to 0.5
084                    // there should be an empty leave block for status change on 04/01/2012
085                    intervalDate = new Date((new DateTime(2012, 4, 01, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
086                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
087                    Assert.assertTrue("There should be 1 leave block for date 04/01/2012.", leaveBlockList.size()==1);
088                    lb = leaveBlockList.get(0);
089                    Assert.assertTrue("Hours of the leave block for date 04/01/2012 should be 0, not " + lb.getLeaveAmount().toString(), lb.getLeaveAmount().equals(BigDecimal.ZERO));
090                    Assert.assertTrue("Leave Code of the leave block for date 04/01/2012 should be " + LMConstants.STATUS_CHANGE_EARN_CODE + ", not " + lb.getEarnCode()
091                                    , lb.getEarnCode().equals(LMConstants.STATUS_CHANGE_EARN_CODE));
092                    Assert.assertNull("accrual_category should be null for empty status change leave block", lb.getAccrualCategory()); 
093                    Assert.assertTrue("Leave Block Type of leave block should be " + LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE + ", not " + lb.getLeaveBlockType()
094                                    , lb.getLeaveBlockType().equals(LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE));
095                    Assert.assertTrue("Requst status of leave block should be " + LMConstants.REQUEST_STATUS.APPROVED + ", not " + lb.getRequestStatus()
096                                    , lb.getRequestStatus().equals(LMConstants.REQUEST_STATUS.APPROVED));
097                    
098                    // there should be two holiday leave blocks of 4 hours on 04/10/2012, one positive, one negative
099                    intervalDate = new Date((new DateTime(2012, 4, 10, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
100                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
101                    Assert.assertTrue("There should be 2 leave block for date 04/10/2012.", leaveBlockList.size()==2);
102                    for(LeaveBlock aLeaveBlock : leaveBlockList) {
103                            Assert.assertNotNull("lm_sys_schd_timeoff_id should NOT be null for holiday accrual leave block", aLeaveBlock.getScheduleTimeOffId());
104                            Assert.assertTrue("Leave Block Type of leave block should be " + LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE + ", not " + aLeaveBlock.getLeaveBlockType()
105                                            , aLeaveBlock.getLeaveBlockType().equals(LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE));
106                            Assert.assertTrue("Requst status of leave block should be " + LMConstants.REQUEST_STATUS.APPROVED + ", not " + aLeaveBlock.getRequestStatus()
107                                            , aLeaveBlock.getRequestStatus().equals(LMConstants.REQUEST_STATUS.APPROVED));
108                            if(!aLeaveBlock.getLeaveAmount().equals(new BigDecimal(4)) && !aLeaveBlock.getLeaveAmount().equals(new BigDecimal(-4))) {
109                                    Assert.fail("Hours of the leave blocks for date 04/10/2012 should be either 4 or -4, not " + aLeaveBlock.getLeaveAmount().toString());
110                            } 
111                    }
112                    
113                    // the disabled system scheduled time off on 04/20/2012 should not generate accruals
114                    intervalDate = new Date((new DateTime(2012, 4, 20, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
115                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
116                    Assert.assertTrue("There should be 0 leave block for date 04/20/2012.", leaveBlockList.isEmpty());
117                    
118                    // there should be 1 leave blocks on 04/30/2012, regular accrual of 8 hours
119                    intervalDate = new Date((new DateTime(2012, 4, 30, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
120                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
121                    Assert.assertTrue("There should be 1 leave block for date 04/30/2012.", leaveBlockList.size()==1);
122                    lb = leaveBlockList.get(0);
123                    Assert.assertTrue("Hours of the regular accrual leave block for date 04/30/2012 should be 8, not " + lb.getLeaveAmount().toString(), lb.getLeaveAmount().equals(new BigDecimal(8)));
124                    Assert.assertNull("lm_sys_schd_timeoff_id should be null for regular accrual leave block", lb.getScheduleTimeOffId());
125                    Assert.assertTrue("Leave Block Type of leave block should be " + LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE + ", not " + lb.getLeaveBlockType()
126                                    , lb.getLeaveBlockType().equals(LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE));
127                    Assert.assertTrue("Requst status of leave block should be " + LMConstants.REQUEST_STATUS.APPROVED + ", not " + lb.getRequestStatus()
128                                    , lb.getRequestStatus().equals(LMConstants.REQUEST_STATUS.APPROVED));
129            }
130            
131            @Test
132            /* testUser's leavePlan "testLP" has planning month of 12
133             * after calculateFutureAccrualUsingPlanningMonth, try to get leaveBlock for 18 months in the future
134             * should still get 12 or 13 leave blocks depends on the date the test is running. 
135             * The accrual service also goes back 1 year for accrual runs.
136             */
137            public void testCalculateFutureAccrualUsingPlanningMonth() {
138                    String principal_id = "testUser";
139                    // the planning month of this leave plan is set to 12
140                    Date currentDate = TKUtils.getCurrentDate();            
141                    TkServiceLocator.getLeaveAccrualService().calculateFutureAccrualUsingPlanningMonth(principal_id, currentDate);          
142                    Calendar aCal = Calendar.getInstance();
143                    aCal.setTime(currentDate);
144                    int futureSize = 12;
145                    int allSize = 17;
146                    if(aCal.getActualMaximum(Calendar.DAY_OF_MONTH) == aCal.get(Calendar.DATE)) {
147                            futureSize ++;
148                            allSize ++;
149                    }
150                    
151                    aCal.add(Calendar.MONTH, 18);
152                    
153                    Date endDate = new java.sql.Date(aCal.getTime().getTime());
154                    // lookup future leave blocks up to 18 months in the future
155                    List<LeaveBlock> leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, currentDate, aCal.getTime());
156                    
157                    Assert.assertFalse("No leave blocks created by calculateF?utureAccrualUsingPlanningMonth for princiapl id " + principal_id, leaveBlockList.isEmpty());
158                    Assert.assertTrue("There should be " + futureSize + " leave blocks for employee 'testUser', not " + leaveBlockList.size(), leaveBlockList.size()== futureSize);
159                    
160                    aCal.setTime(currentDate);
161                    aCal.add(Calendar.MONTH, -5);
162                    Date startDate = new java.sql.Date(aCal.getTime().getTime());
163                    // lookup leave blocks including past and future
164                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, startDate, endDate);
165                    Assert.assertTrue("There should be  " + allSize + " leave blocks for employee 'testUser', not " + leaveBlockList.size(), leaveBlockList.size()== allSize);
166            }
167            
168            @Test
169            /*
170              *testUser2 has two accrual category rules, rule 1 goes from 0 month to 5 with accrual rate of 16
171              *rule 2 goes from 5 to 900 with accrual rate of 24
172              *run accrual for testUser2 for 15 months
173             */
174            public void testRunAccrualForRuleChanges() {
175                     String principal_id = "testUser2";
176                     Calendar aCal = Calendar.getInstance();
177                     aCal.setTime(START_DATE);
178                     aCal.add(Calendar.MONTH, 15);
179                     Date endDate = new java.sql.Date(aCal.getTime().getTime());
180                     
181                     List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
182                     Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
183                    
184                     TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
185                     
186                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
187                     Assert.assertTrue("There should be 14 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 14);
188                     
189                     // July of 2012 is the 5th month of this user's employment, the accrual rate should be 16
190                     Date intervalDate = new Date((new DateTime(2012, 7, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
191                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
192                     Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date 07/31/2012.", leaveBlockList.size()==1);
193                     LeaveBlock lb = leaveBlockList.get(0);
194                     Assert.assertTrue("Hours of the leave block on date 07/31/2012 for employee " + principal_id + " should be 16, not " + lb.getLeaveAmount().toString()
195                                     , lb.getLeaveAmount().equals(new BigDecimal(16)));
196                     
197                     // August of 2012 is the 6th month of this user's employment, the accrual rate should be 24 from now on
198                     intervalDate = new Date((new DateTime(2012, 8, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
199                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
200                     Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date 08/31/2012.", leaveBlockList.size()==1);
201                     lb = leaveBlockList.get(0);
202                     Assert.assertTrue("Hours of the leave block for date 08/31/2012 for employee " + principal_id + " should be 24, not " + lb.getLeaveAmount().toString()
203                                     , lb.getLeaveAmount().equals(new BigDecimal(24)));
204                     
205            }
206            @Test
207            /* testUser3's leavePlan has 2 accrual categories with different accrual intervals
208             * testAC3 has accrual interval semi-monthly
209             * testAC4 has accrual interval monthly
210             */
211            public void testRunAccrualWithDifferentAccrualIntervals() {
212                     String principal_id = "testUser3";
213                     List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, END_DATE);
214                     Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
215                     
216                     TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, END_DATE, false);
217                     leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, END_DATE);
218                     Assert.assertTrue("There should be 6 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 6);
219                     
220                     Date semiMonthlyDate = new Date((new DateTime(2012, 3, 15, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
221                     Date monthlyDate = new Date((new DateTime(2012, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
222                     this.verifyLeaveBlocksWithDifferentAccrualIntervals(semiMonthlyDate, monthlyDate);
223    
224                     semiMonthlyDate = new Date((new DateTime(2012, 4, 15, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
225                     monthlyDate = new Date((new DateTime(2012, 4, 30, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
226                     this.verifyLeaveBlocksWithDifferentAccrualIntervals(semiMonthlyDate, monthlyDate);
227                     
228            }
229            public void verifyLeaveBlocksWithDifferentAccrualIntervals(Date semiMonthlyDate, Date monthlyDate) {
230                     String principal_id = "testUser3";
231                     List<LeaveBlock> leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, semiMonthlyDate);
232                     Assert.assertTrue("There should be 1 leave block for date " + semiMonthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
233                     LeaveBlock lb = leaveBlockList.get(0);
234                     Assert.assertTrue("Hours of the leave block on date " + semiMonthlyDate.toString() + " should be 8, not " + lb.getLeaveAmount().toString(), lb.getLeaveAmount().equals(new BigDecimal(8)));
235                     Assert.assertTrue("Leave code of the leave block on date " + semiMonthlyDate.toString() + " should be EC1, not " + lb.getEarnCode(), lb.getEarnCode().equals("EC1"));
236                     Assert.assertTrue("accrual_category of the leave block on date " + semiMonthlyDate.toString() + " should be testAC3, not " + lb.getAccrualCategory(), lb.getAccrualCategory().equals("testAC3"));
237                    
238                    
239                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
240                     Assert.assertTrue("There should be 2 leave block for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==2);
241                     for(LeaveBlock aLeaveBlock : leaveBlockList) {
242                             if(aLeaveBlock.getAccrualCategory().equals("testAC3")) {
243                                     Assert.assertTrue("Leave code of the leave block with accrualCategory testAC3 on date " + monthlyDate.toString() + " should be EC1, not " + aLeaveBlock.getEarnCode()
244                                                     , aLeaveBlock.getEarnCode().equals("EC1")); 
245                                     Assert.assertTrue("Hours of the leave block with accrualCategory testAC4 on date " + monthlyDate.toString() + " should be 8, not " + aLeaveBlock.getLeaveAmount().toString()
246                                                     , aLeaveBlock.getLeaveAmount().equals(new BigDecimal(8)));
247                             } else if(aLeaveBlock.getAccrualCategory().equals("testAC4")) {
248                                     Assert.assertTrue("Leave code of the leave block with accrualCategory testAC4 on date " + monthlyDate.toString() + " should be EC2, not " + aLeaveBlock.getEarnCode()
249                                                     , aLeaveBlock.getEarnCode().equals("EC2")); 
250                                     Assert.assertTrue("Hours of the leave block with accrualCategory testAC4 on date " + monthlyDate.toString() + " should be 24, not " + aLeaveBlock.getLeaveAmount().toString()
251                                                     , aLeaveBlock.getLeaveAmount().equals(new BigDecimal(24)));
252                             } else {
253                                     Assert.fail("Leave block on date " + monthlyDate.toString() + " should not have accrual_category " + aLeaveBlock.getAccrualCategory());
254                             }
255                     }
256            }
257            
258            @Test
259            /* testUser4 has PrincipalHRAttributes that's associated with 2 accrual categories, one is testAC5, the other is testAC6
260             * testAC5 has minimum accrual of 0.5, proration = true
261             * testAC6 has minimum accrual of 0
262             * testUser4 has records of principalHRAttributes, the employment is from 03/18/2012 to 08/08/2012
263             * There should not be accrual for testAC5 in March and August of 2012 since minimum percentage is not reached
264             */
265            public void testMinNotReachedProrationTrueFirstLastPeriod() {
266                     String principal_id = "testUser4";
267                     Date end = new Date((new DateTime(2012, 9, 25, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
268                     List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, end);
269                     Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
270                     
271                     TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, end, false);
272                     leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, end);
273                     Assert.assertTrue("There should be 10 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 10);
274                     
275                     Date monthlyDate = new Date((new DateTime(2012, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
276                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
277                     Assert.assertTrue("There should be 1 leave block for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
278                     LeaveBlock lb = leaveBlockList.get(0);
279                     Assert.assertTrue("Leave block on date " + monthlyDate.toString() + " should have accrual_category testAC6, not " + lb.getAccrualCategory()
280                                     , lb.getAccrualCategory().equals("testAC6"));
281                     Assert.assertTrue("Leave block on date " + monthlyDate.toString() + " should have 7 hours, not " + lb.getLeaveAmount()
282                                     , lb.getLeaveAmount().equals(new BigDecimal(7)));
283                    
284                     monthlyDate = new Date((new DateTime(2012, 4, 30, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
285                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
286                     Assert.assertTrue("There should be 2 leave block for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==2);
287                     
288    // should the accrual of the last days show up on the end day or the interval day of the last pay period?????
289                     monthlyDate = new Date((new DateTime(2012, 8, 8, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
290                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
291                     Assert.assertTrue("There should be 1 leave block for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
292                     lb = leaveBlockList.get(0);
293                     Assert.assertTrue("Leave block on date " + monthlyDate.toString() + " should have accrual_category testAC6, not " + lb.getAccrualCategory()
294                                     , lb.getAccrualCategory().equals("testAC6"));
295                     Assert.assertTrue("Leave block on date " + monthlyDate.toString() + " should have 4 hours, not " + lb.getLeaveAmount()
296                                     , lb.getLeaveAmount().equals(new BigDecimal(4)));
297                     
298                     monthlyDate = new Date((new DateTime(2012, 8, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
299                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
300                     Assert.assertTrue("There should NOT be any leave blocks for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==0);
301            }
302            @Test
303            /* testUser9 has PrincipalHRAttributes that's associated with accrual categories "testAC12"
304             * testAC12 has minimum accrual of 0.5, proration = true, fte of the ac rule is 8
305             * testUser9 has records of principalHRAttributes, the employment is from 03/10/2012 to 08/20/2012
306             * There should be partial fte accrued for testAC12 in March and August of 2012 since minimum percentage is reached
307             */
308            public void testMinReachedProrationTrueFirstLastPeriod() {
309                    String principal_id = "testUser9";
310                     Date end = new Date((new DateTime(2012, 9, 25, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
311                     List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, end);
312                     Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
313                     
314                     TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, end, false);
315                     leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, end);
316                     Assert.assertTrue("There should be 6 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 6);
317                     
318                     Date monthlyDate = new Date((new DateTime(2012, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
319                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
320                     Assert.assertTrue("There should be 1 leave block for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
321                     LeaveBlock lb = leaveBlockList.get(0);
322                     Assert.assertTrue("Leave block on date " + monthlyDate.toString() + " should have 5 hours, not " + lb.getLeaveAmount()
323                                     , lb.getLeaveAmount().equals(new BigDecimal(5)));
324                    
325                     monthlyDate = new Date((new DateTime(2012, 4, 30, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
326                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
327                     Assert.assertTrue("There should be 1 leave block for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
328                     lb = leaveBlockList.get(0);
329                     Assert.assertTrue("Leave block on date " + monthlyDate.toString() + " should have 8 hours, not " + lb.getLeaveAmount()
330                                     , lb.getLeaveAmount().equals(new BigDecimal(8)));
331                     
332                     monthlyDate = new Date((new DateTime(2012, 8, 20, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
333                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
334                     Assert.assertTrue("There should be 1 leave block for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
335                     lb = leaveBlockList.get(0);
336                     Assert.assertTrue("Leave block on date " + monthlyDate.toString() + " should have 5 hours, not " + lb.getLeaveAmount()
337                                     , lb.getLeaveAmount().equals(new BigDecimal(5)));
338                     
339                     monthlyDate = new Date((new DateTime(2012, 8, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
340                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
341                     Assert.assertTrue("There should NOT be any leave blocks for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==0);
342            }
343            @Test
344            /* testUser10 has PrincipalHRAttributes that's associated with accrual categories "testAC13"
345             * testAC13 has minimum accrual of 0.5, proration = false
346             * testUser10 has records of principalHRAttributes, the employment is from 03/10/2012 to 08/20/2012
347             * There should be whole fte accrued for testAC13 in March and August of 2012 since minimum percentage is reached
348             */
349            public void testMinReachedProrationFalseFirstLastPeriod() {
350                    String principal_id = "testUser10";
351                    Date end = new Date((new DateTime(2012, 9, 25, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
352                    List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, end);
353                    Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
354                             
355                    TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, end, false);
356                    leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, end);
357                    Assert.assertTrue("There should be 6 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 6);
358                             
359                    Date monthlyDate = new Date((new DateTime(2012, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
360                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
361                    Assert.assertTrue("There should be 1 leave block for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
362                    LeaveBlock lb = leaveBlockList.get(0);
363                    Assert.assertTrue("Leave block on date " + monthlyDate.toString() + " should have 8 hours, not " + lb.getLeaveAmount()
364                             , lb.getLeaveAmount().equals(new BigDecimal(8)));
365                    
366                    monthlyDate = new Date((new DateTime(2012, 4, 30, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
367                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
368                    Assert.assertTrue("There should be 1 leave block for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
369                    lb = leaveBlockList.get(0);
370                    Assert.assertTrue("Leave block on date " + monthlyDate.toString() + " should have 8 hours, not " + lb.getLeaveAmount()
371                             , lb.getLeaveAmount().equals(new BigDecimal(8)));
372                    
373                    monthlyDate = new Date((new DateTime(2012, 8, 20, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
374                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
375                    Assert.assertTrue("There should be 1 leave block for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
376                    lb = leaveBlockList.get(0);
377                    Assert.assertTrue("Leave block on date " + monthlyDate.toString() + " should have 8 hours, not " + lb.getLeaveAmount()
378                                    , lb.getLeaveAmount().equals(new BigDecimal(8)));
379                             
380                    monthlyDate = new Date((new DateTime(2012, 8, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
381                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
382                    Assert.assertTrue("There should NOT be any leave blocks for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==0);                
383            }
384            
385            @Test
386            /* testUser11 has PrincipalHRAttributes that's associated with accrual categories "testAC14"
387             * testAC14 has minimum accrual of 0.5, proration = false
388             * testUser11 has records of principalHRAttributes, the employment is from 03/20/2012 to 08/10/2012
389             * There should NOT be accrual leave blocks for testAC14 in March and August of 2012 since minimum percentage is NOT reached
390             */
391            public void testMinNotReachedProrationFalseFirstLastPeriod() {
392                    String principal_id = "testUser11";
393                    Date end = new Date((new DateTime(2012, 9, 25, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
394                    List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, end);
395                    Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
396                             
397                    TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, end, false);
398                    leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, end);
399                    Assert.assertTrue("There should be 4 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 4);
400                             
401                    Date monthlyDate = new Date((new DateTime(2012, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
402                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
403                    Assert.assertTrue("There should be 0 leave block for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.isEmpty());
404                    
405                    monthlyDate = new Date((new DateTime(2012, 4, 30, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
406                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
407                    Assert.assertTrue("There should be 1 leave block for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
408                    LeaveBlock lb = leaveBlockList.get(0);
409                    Assert.assertTrue("Leave block on date " + monthlyDate.toString() + " should have 8 hours, not " + lb.getLeaveAmount()
410                             , lb.getLeaveAmount().equals(new BigDecimal(8)));
411                    
412                    monthlyDate = new Date((new DateTime(2012, 8, 20, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
413                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
414                    Assert.assertTrue("There should be 0 leave block for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.isEmpty());
415                                             
416                    monthlyDate = new Date((new DateTime(2012, 8, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
417                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, monthlyDate);
418                    Assert.assertTrue("There should NOT be any leave blocks for date " + monthlyDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==0);                
419            }
420            @Test
421            /* testUser5's service date is 2012-03-10
422             * testUser5 has two accrual category rules, rule 1 goes from 0 month to 12 with accrual rate of 16
423             * rule 2 goes from 12 to 900 with accrual rate of 24
424             * accrual category associated with the two rules has Proration=false, and minimum percentage of 0.5
425             * run accrual for testUser5 for 18 months
426             */
427            public void testMinReachedProrationFalseAndRuleChange() {
428                     String principal_id = "testUser5";
429                     Calendar aCal = Calendar.getInstance();
430                     aCal.setTime(START_DATE);
431                     aCal.add(Calendar.MONTH, 18);
432                     Date endDate = new java.sql.Date(aCal.getTime().getTime());
433                     
434                     List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
435                     Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
436                    
437                     TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
438                     
439                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
440                     Assert.assertTrue("There should be 17 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 17);
441                     
442                     // 03/31/2012 is the first accrual interval date, service starts on 2012-03-10, so minimum percentage is reached for that month
443                     // since proration is false, the whole accrual rate is created for the first pay period
444                     Date intervalDate = new Date((new DateTime(2012, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
445                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
446                     Assert.assertTrue("There should be 1 leave block for date " + intervalDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
447                     LeaveBlock lb = leaveBlockList.get(0);
448                     Assert.assertTrue("Leave block on date " + intervalDate.toString() + " should have 16 hours, not " + lb.getLeaveAmount()
449                                     , lb.getLeaveAmount().equals(new BigDecimal(16)));
450                     
451                     // 02/28/2013 is the 12th month of this user's employment, the accrual rate should be 16
452                     intervalDate = new Date((new DateTime(2013, 2, 28, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
453                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
454                     Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date 02/28/2013.", leaveBlockList.size()==1);
455                     lb = leaveBlockList.get(0);
456                     Assert.assertTrue("Hours of the leave block on date " + intervalDate.toString() + " should be 16, not " + lb.getLeaveAmount().toString()
457                                     , lb.getLeaveAmount().equals(new BigDecimal(16)));
458                     
459                     // 03/31/2013 is the 13th month of this user's employment, since the minimum percentage of days is meet for
460                     // that month (03/10 - 03/31) and proration = false, the accrual rate should be 24 from now on
461                     intervalDate = new Date((new DateTime(2013, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
462                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
463                     Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
464                     lb = leaveBlockList.get(0);
465                     Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 24, not " + lb.getLeaveAmount().toString()
466                                     , lb.getLeaveAmount().equals(new BigDecimal(24)));
467            }
468            @Test
469            /* testUser12's service date is 2012-03-20
470             * testUser12 has two accrual category rules, rule 1 goes from 6 month to 12 with accrual rate of 16
471             * rule 2 goes from 6 to 900 with accrual rate of 24
472             * accrual category "testAC15" is associated with the two rules. testAC15 has Proration=false, and minimum percentage of 0.5
473             * run accrual for testUser12 for 12 months
474             * for 2012-09-30, the accrual should still be 16 since the minimum is NOT reached for the rule 2
475             */
476            public void testMinNotReachedProrationFalseAndRuleChange() {
477                    String principal_id = "testUser12";
478                    Calendar aCal = Calendar.getInstance();
479                    aCal.setTime(START_DATE);
480                    aCal.add(Calendar.MONTH, 18);
481                    Date endDate = new java.sql.Date(aCal.getTime().getTime());
482                     
483                    List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
484                     Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
485                    
486                     TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
487                     
488                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
489                     Assert.assertTrue("There should be 17 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 17);
490                     // 08/31/2013 
491                     Date intervalDate = new Date((new DateTime(2012, 8, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
492                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
493                     Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
494                     LeaveBlock lb = leaveBlockList.get(0);
495                     Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 16, not " + lb.getLeaveAmount().toString()
496                                     , lb.getLeaveAmount().equals(new BigDecimal(16)));             
497                     
498                     // 09/30/2012 is the 6th month of this user's employment, since minimum percentage is not reached, the accrual rate should be 16
499                     intervalDate = new Date((new DateTime(2012, 9, 30, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
500                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
501                     Assert.assertTrue("There should be 1 leave block for date " + intervalDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
502                     lb = leaveBlockList.get(0);
503                     Assert.assertTrue("Leave block on date " + intervalDate.toString() + " should have 16 hours, not " + lb.getLeaveAmount()
504                                     , lb.getLeaveAmount().equals(new BigDecimal(16)));
505                     
506                     // 10/31/2013 
507                     intervalDate = new Date((new DateTime(2012, 10, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
508                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
509                     Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
510                     lb = leaveBlockList.get(0);
511                     Assert.assertTrue("Hours of the leave block on date " + intervalDate.toString() + " should be 24, not " + lb.getLeaveAmount().toString()
512                                     , lb.getLeaveAmount().equals(new BigDecimal(24)));
513            }
514            @Test
515            /* testUser13's service date is 2012-03-10
516             * testUser13 has two accrual category rules, rule 1 goes from 0 month to 6 with accrual rate of 16
517             * rule 2 goes from 6 to 900 with accrual rate of 24
518             * accrual category "testAC16" is associated with the two rules. testAC16 has Proration=true, and minimum percentage of 0.5
519             * run accrual for testUser13 for 12 months
520             * for 2012-09-30, the accrual should be based on actual work days for those two rules, ie 22 because the minimum is reached for rule 2
521             */
522            public void testMinReachedProrationTrueAndRuleChange() {
523                    String principal_id = "testUser13";
524                    Calendar aCal = Calendar.getInstance();
525                    aCal.setTime(START_DATE);
526                    aCal.add(Calendar.MONTH, 18);
527                    Date endDate = new java.sql.Date(aCal.getTime().getTime());
528                     
529                    List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
530                    Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
531                    
532                    TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
533                     
534                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
535                    Assert.assertTrue("There should be 17 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 17);
536                    // 08/31/2013 
537                    Date intervalDate = new Date((new DateTime(2012, 8, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
538                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
539                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
540                    LeaveBlock lb = leaveBlockList.get(0);
541                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 16, not " + lb.getLeaveAmount().toString()
542                                     , lb.getLeaveAmount().equals(new BigDecimal(16)));             
543                     
544                    // 09/30/2012 is the 6th month of this user's employment, since minimum percentage is reached, the accrual rate is 16 for 6 work days
545                    // and 24 hrs for 14 work days, so the final accrual hrs is 22
546                    intervalDate = new Date((new DateTime(2012, 9, 30, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
547                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
548                    Assert.assertTrue("There should be 1 leave block for date " + intervalDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
549                    lb = leaveBlockList.get(0);
550                    Assert.assertTrue("Leave block on date " + intervalDate.toString() + " should have 22 hours, not " + lb.getLeaveAmount()
551                             , lb.getLeaveAmount().equals(new BigDecimal(22)));
552                     
553                    // 10/31/2013 
554                    intervalDate = new Date((new DateTime(2012, 10, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
555                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
556                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
557                    lb = leaveBlockList.get(0);
558                    Assert.assertTrue("Hours of the leave block on date " + intervalDate.toString() + " should be 24, not " + lb.getLeaveAmount().toString()
559                             , lb.getLeaveAmount().equals(new BigDecimal(24)));             
560            }
561            @Test
562            /* testUser14's service date is 2012-03-20
563             * testUser14 has two accrual category rules, rule 1 goes from 0 month to 6 with accrual rate of 16
564             * rule 2 goes from 6 to 900 with accrual rate of 24
565             * accrual category "testAC17" is associated with the two rules. testAC17 has Proration=true, and minimum percentage of 0.5
566             * run accrual for testUser14 for 12 months
567             * for 2012-09-30, the accrual should be 16 since the minimum is NOT reached for the rule 2
568             */
569            public void testMinNotReachedProrationTrueAndRuleChange() {
570                    String principal_id = "testUser14";
571                    Calendar aCal = Calendar.getInstance();
572                    aCal.setTime(START_DATE);
573                    aCal.add(Calendar.MONTH, 18);
574                    Date endDate = new java.sql.Date(aCal.getTime().getTime());
575                     
576                    List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
577                    Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
578                    
579                    TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
580                    // only 16 leave blocks since the first interval 03/31/2012 does not have accruals due to minimum not reached
581                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
582                    Assert.assertTrue("There should be 16 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 16);
583                    
584                    // 03/31/2013 
585                    Date intervalDate = new Date((new DateTime(2012, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
586                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
587                    Assert.assertTrue("There should be 0 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.isEmpty());
588                            
589                    // 08/31/2013 
590                    intervalDate = new Date((new DateTime(2012, 8, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
591                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
592                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
593                    LeaveBlock lb = leaveBlockList.get(0);
594                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 16, not " + lb.getLeaveAmount().toString()
595                                     , lb.getLeaveAmount().equals(new BigDecimal(16)));             
596                     
597                    // 09/30/2012 is the 6th month of this user's employment, since minimum percentage is NOT reached, the accrual rate should be 16
598                    intervalDate = new Date((new DateTime(2012, 9, 30, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
599                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
600                    Assert.assertTrue("There should be 1 leave block for date " + intervalDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
601                    lb = leaveBlockList.get(0);
602                    Assert.assertTrue("Leave block on date " + intervalDate.toString() + " should have 16 hours, not " + lb.getLeaveAmount()
603                             , lb.getLeaveAmount().equals(new BigDecimal(16)));
604                     
605                    // 10/31/2013 
606                    intervalDate = new Date((new DateTime(2012, 10, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
607                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
608                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
609                    lb = leaveBlockList.get(0);
610                    Assert.assertTrue("Hours of the leave block on date " + intervalDate.toString() + " should be 24, not " + lb.getLeaveAmount().toString()
611                             , lb.getLeaveAmount().equals(new BigDecimal(24)));                     
612            }
613            @Test
614            /* testUser6's service date is 2012-03-25
615             * testUser6 has two accrual category rules, rule 1 goes from 0 to 6 month with accrual rate of 16
616             * rule 2 goes from 6 month to 900 with accrual rate of 24
617             * accrual category testAC8 associated with the two rules has Proration=false, and minimum percentage of 0.5 and earn interval=semi_monthly
618             * run accrual for testUser5 for 10 months
619             */
620            public void testMinNOTReachedProrationFalseAndRuleChange() {
621                     String principal_id = "testUser6";
622                     Calendar aCal = Calendar.getInstance();
623                     aCal.setTime(START_DATE);      // 02/10/2012   
624                     aCal.add(Calendar.MONTH, 10);  // 12/10/2012
625                     Date endDate = new java.sql.Date(aCal.getTime().getTime());
626                     
627                     List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
628                     Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
629                    
630                     TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
631                     
632                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
633                     Assert.assertTrue("There should be 17 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 17);
634                     
635                     // 03/31/2012 is the first accrual interval date, since minimum percentage is not reached (03/25-03/31) and proration=false
636                     // there should not be leave blocks
637                     Date intervalDate = new Date((new DateTime(2012, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
638                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
639                     Assert.assertTrue("There should be 0 leave block for date " + intervalDate.toString(), leaveBlockList.isEmpty());
640                     
641                     // 04/15/2012 should have the first leave block for testUser6 and the accrual hours should be the full 16 
642                     intervalDate = new Date((new DateTime(2012, 4, 15, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
643                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
644                     Assert.assertTrue("There should be 1 leave block for date " + intervalDate.toString() + " for emplyee " + principal_id, leaveBlockList.size()==1);
645                     LeaveBlock lb = leaveBlockList.get(0);
646                     Assert.assertTrue("Leave block on date " + intervalDate.toString() + " should have 16 hours, not " + lb.getLeaveAmount()
647                                     , lb.getLeaveAmount().equals(new BigDecimal(16)));
648                     
649                     // accrual rate for 09/15/2012 should still be 16
650                     intervalDate = new Date((new DateTime(2012, 9, 15, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
651                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
652                     Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
653                     lb = leaveBlockList.get(0);
654                     Assert.assertTrue("Hours of the leave block on date " + intervalDate.toString() + " should be 16, not " + lb.getLeaveAmount().toString()
655                                     , lb.getLeaveAmount().equals(new BigDecimal(16)));
656                     
657                     // 09/30/2013 is the first pay interval of rule 2, since the minimum percentage is not reached (09/25-09/30)
658                     // the accrual rate should still be 16
659                     intervalDate = new Date((new DateTime(2012, 9, 30, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
660                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
661                     Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
662                     lb = leaveBlockList.get(0);
663                     Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 16, not " + lb.getLeaveAmount().toString()
664                                     , lb.getLeaveAmount().equals(new BigDecimal(16)));
665                     
666                     // 10/15/2013, should use new rate of 24
667                     intervalDate = new Date((new DateTime(2012, 10, 15, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
668                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
669                     Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
670                     lb = leaveBlockList.get(0);
671                     Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 24, not " + lb.getLeaveAmount().toString()
672                                     , lb.getLeaveAmount().equals(new BigDecimal(24)));
673            }
674            
675            @Test
676            /* testUser7's service date is 2012-03-10
677             * testUser7 has two leave blocks scheduled on 04/26/2012 for 2 hours and 05/08/2012 for 5 hours, they both are NOT eligible for accrual
678             * testUser7 has one leave block scheduled on 05/22/1012 of 8 horus, it's eligible for accrual
679             * testUser7 has one leave block scheduled on 06/12/1012 of 15 horus, it's NOT eligible for accrual
680             * testUser7 has one accrual category rule of 32 hours of accrual rate, two jobs are eligible for leave with total of 40 standard hours
681             * run accrual for testUser7 for 5 months
682             */
683            public void testNotEligibleForAccrualAdjustment() {
684                    String principal_id = "testUser7";
685                    Calendar aCal = Calendar.getInstance();
686                     aCal.setTime(START_DATE);      // 02/20/2012   
687                     aCal.add(Calendar.MONTH, 5);   // 7/20/2012
688                     Date endDate = new java.sql.Date(aCal.getTime().getTime());
689                     
690                     List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
691                     Assert.assertTrue("There should be 4 leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.size() == 4);
692                    
693                     TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
694                     
695                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
696                     Assert.assertTrue("There should be 10 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 10);
697                     
698                     // 03/31/2012, 
699                     Date intervalDate = new Date((new DateTime(2012, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
700                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
701                     Assert.assertTrue("There should be 1 leave block for date " + intervalDate.toString(), leaveBlockList.size() == 1);
702                     LeaveBlock lb = leaveBlockList.get(0);
703                     Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 22, not " + lb.getLeaveAmount().toString()
704                                     , lb.getLeaveAmount().equals(new BigDecimal(22)));
705                     
706                     // 04/30/2012, 
707                     intervalDate = new Date((new DateTime(2012, 4, 30, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
708                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
709                     Assert.assertTrue("There should be 1 leave block for date " + intervalDate.toString(), leaveBlockList.size() == 1);
710                     lb = leaveBlockList.get(0);
711                     Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 32, not " + lb.getLeaveAmount().toString()
712                                     , lb.getLeaveAmount().equals(new BigDecimal(32)));
713                     
714                     //05/31/2012
715                     intervalDate = new Date((new DateTime(2012, 5, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
716                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
717                     Assert.assertTrue("There should be 2 leave block for date " + intervalDate.toString(), leaveBlockList.size() == 2);
718                     for(LeaveBlock aLeaveBlock : leaveBlockList) {
719                             if(aLeaveBlock.getLeaveAmount().equals(new BigDecimal(-1))) {
720                                     Assert.assertTrue("Accrual category of the leave block for date  " + intervalDate.toString() + " should be 'testAC9' , not " + aLeaveBlock.getAccrualCategory()
721                                                     , aLeaveBlock.getAccrualCategory().equals("testAC9")); 
722                             } else if(aLeaveBlock.getLeaveAmount().equals(new BigDecimal(32))) {
723                                     Assert.assertTrue("Accrual category of the leave block for date  " + intervalDate.toString() + " should be 'testAC9' , not " + aLeaveBlock.getAccrualCategory()
724                                                     , aLeaveBlock.getAccrualCategory().equals("testAC9")); 
725                             } else {
726                                     Assert.fail("Hours of the leave block for date  " + intervalDate.toString() + " should be either 32 or -1, not " + aLeaveBlock.getLeaveAmount().toString());
727                             }
728                     }
729                     
730                     //06/30/2012
731                     intervalDate = new Date((new DateTime(2012, 6, 30, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
732                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
733                     Assert.assertTrue("There should be 2 leave block for date " + intervalDate.toString(), leaveBlockList.size() == 2);
734                     for(LeaveBlock aLeaveBlock : leaveBlockList) {
735                             if(aLeaveBlock.getLeaveAmount().equals(new BigDecimal(-3))) {
736                                     Assert.assertTrue("Accrual category of the leave block for date  " + intervalDate.toString() + " should be 'testAC9' , not " + lb.getAccrualCategory()
737                                                     , lb.getAccrualCategory().equals("testAC9")); 
738                             } else if(aLeaveBlock.getLeaveAmount().equals(new BigDecimal(32))) {
739                                     Assert.assertTrue("Accrual category of the leave block for date  " + intervalDate.toString() + " should be 'testAC9' , not " + lb.getAccrualCategory()
740                                                     , lb.getAccrualCategory().equals("testAC9")); 
741                             } else {
742                                     Assert.fail("Hours of the leave block for date  " + intervalDate.toString() + " should be either 32 or -3, not " + lb.getLeaveAmount().toString());
743                             }
744                     }
745            }
746            
747            @Test
748            /* testUser8's service date is 2012-03-10
749             * testUser8 has one accrual category, two entries of the same accrual category. 
750             * The first entry has effectiveDate = 2012-03-01. The rule associated with it has 16 as the accrual rate
751             * the second entry has effectiveDate = 2012-5-01. The rule associated with it has 32 as the accrual rate
752             * run accrual for testUser8 for 6 months
753             */
754            public void testAccrualCategoryChanges() {
755                    String principal_id = "testUser8";
756                    Calendar aCal = Calendar.getInstance();
757                     aCal.setTime(START_DATE);      // 02/20/2012   
758                     aCal.add(Calendar.MONTH, 6);   // 8/20/2012
759                     Date endDate = new java.sql.Date(aCal.getTime().getTime());
760                     
761                     List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
762                     Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
763                    
764                     TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
765                     
766                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
767                     Assert.assertTrue("There should be 5 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 5);          
768                     
769                     // 03/31/2012, 
770                     Date intervalDate = new Date((new DateTime(2012, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
771                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
772                     Assert.assertTrue("There should be 1 leave block for date " + intervalDate.toString(), leaveBlockList.size() == 1);
773                     LeaveBlock lb = leaveBlockList.get(0);
774                     Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 11, not " + lb.getLeaveAmount().toString()
775                                     , lb.getLeaveAmount().equals(new BigDecimal(11)));
776                     
777                     // 04/30/2012, 
778                     intervalDate = new Date((new DateTime(2012, 4, 30, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
779                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
780                     Assert.assertTrue("There should be 1 leave block for date " + intervalDate.toString(), leaveBlockList.size() == 1);
781                     lb = leaveBlockList.get(0);
782                     Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 16, not " + lb.getLeaveAmount().toString()
783                                     , lb.getLeaveAmount().equals(new BigDecimal(16)));
784                     
785                     //05/31/2012
786                     intervalDate = new Date((new DateTime(2012, 5, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
787                     leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
788                     Assert.assertTrue("There should be 1 leave block for date " + intervalDate.toString(), leaveBlockList.size() == 1);
789                     lb = leaveBlockList.get(0);
790                     Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 32, not " + lb.getLeaveAmount().toString()
791                                     , lb.getLeaveAmount().equals(new BigDecimal(32)));
792                     
793            }
794            
795            @Test
796            /*      testUser15's service Date is 2012-03-10
797             *      testUser15 has one accrual category, the effectiveDate of the AC is 2012-03-01
798             *  The rule associated with the AC has 24 as the accrual rate
799             *  There's Leave Calendar document for calendar entry 2012-04-01 -- 2012-05-01
800             *  run accrual for testUser15 for 6 months
801             */
802            public void testLeaveBlocksWithLeaveCalendarDocId() {
803                    String principal_id = "testUser15";
804                    Calendar aCal = Calendar.getInstance();
805                    aCal.setTime(START_DATE);
806                    aCal.add(Calendar.MONTH, 6);
807                    Date endDate = new java.sql.Date(aCal.getTime().getTime());
808                     
809                    List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
810                    Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
811                    
812                    TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
813                    
814                    // 04/30/2012 
815                    Date intervalDate = new Date((new DateTime(2012, 4, 30, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
816                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
817                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
818                    LeaveBlock lb = leaveBlockList.get(0);
819                    Assert.assertTrue("DocumentId of the leave block for date  " + intervalDate.toString() + " should be 5000, not " + lb.getDocumentId()
820                                     , lb.getDocumentId().equals("5000"));          
821                     
822            }
823            
824            @Test
825            /*      testUser16's service Date is 2012-03-26 which is a Monday
826             *      testUser16 has one accrual category "testAC19" with effectiveDate of 2012-03-01
827             *  testAC19 has proration = false, minimum percentage = 0
828             *  testAC19 has "pay calendar" as the earn interval, so the accrual interval will be based on the pay calendar entries of testUser16
829             *  testUser16 has "BI-WE" as the pay calendar, it's bi-weekly with start day as Sunday
830             *  The rule associated with the AC has 24 as the accrual rate
831             *  run accrual for testUser16 for 6 months
832             */
833            public void testPayCalAsEarnInterval() {
834                    String principal_id = "testUser16";
835                    Calendar aCal = Calendar.getInstance();
836                    aCal.setTime(START_DATE);
837                    aCal.add(Calendar.MONTH, 6);
838                    Date endDate = new java.sql.Date(aCal.getTime().getTime());
839                     
840                    List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
841                    Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
842                    
843                    TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
844                    
845                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
846                    Assert.assertTrue("There should be 11 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 11);        
847                    
848                    // 03/31/2012, testAC19 has proration= false, minimum percentage = 0, so whole FTE of 24 hours is given to the first interval
849                    Date intervalDate = new Date((new DateTime(2012, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
850                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
851                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
852                    LeaveBlock lb = leaveBlockList.get(0);          
853                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 24, not " + lb.getLeaveAmount().toString()
854                                     , lb.getLeaveAmount().equals(new BigDecimal(24)));
855                    
856                    // 04/28/2012
857                    intervalDate = new Date((new DateTime(2012, 4, 28, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
858                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
859                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
860                    lb = leaveBlockList.get(0);             
861                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 24, not " + lb.getLeaveAmount().toString()
862                                     , lb.getLeaveAmount().equals(new BigDecimal(24)));
863                    // 05/12/2012
864                    intervalDate = new Date((new DateTime(2012, 5, 12, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
865                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
866                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
867                    // 08/04/2012
868                    intervalDate = new Date((new DateTime(2012, 8, 4, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
869                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
870                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
871                    // 08/18/2012
872                    intervalDate = new Date((new DateTime(2012, 8, 18, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
873                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
874                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
875            }
876            
877            @Test
878            /*      testUser17's service Date is 2012-03-26 which is a Monday
879             *      testUser17 has one accrual category "testAC20" with effectiveDate of 2012-03-01
880             *  testAC20 has proration = true, minimum percentage = 0.5
881             *  testAC20 has "pay calendar" as the earn interval, so the accrual interval will be based on the pay calendar entries of testUser17
882             *  testUser17 has "BI-WE" as the pay calendar, it's bi-weekly with start day as Sunday
883             *  The rule associated with the AC has 24 as the accrual rate
884             *  run accrual for testUser17 for 6 months
885             */
886            public void testPayCalAsEarnIntervalProrationFalseMinReached() {
887                    String principal_id = "testUser17";
888                    Calendar aCal = Calendar.getInstance();
889                    aCal.setTime(START_DATE);
890                    aCal.add(Calendar.MONTH, 6);
891                    Date endDate = new java.sql.Date(aCal.getTime().getTime());
892                     
893                    List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
894                    Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
895                    
896                    TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
897                    
898                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
899                    Assert.assertTrue("There should be 11 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 11);        
900                    
901                    // 03/31/2012, testAC20 has proration= true, minimum percentage = 0.5, so only 12 hours is given to the first interval
902                    Date intervalDate = new Date((new DateTime(2012, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
903                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
904                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
905                    LeaveBlock lb = leaveBlockList.get(0);          
906                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 12, not " + lb.getLeaveAmount().toString()
907                                     , lb.getLeaveAmount().equals(new BigDecimal(12)));
908                    
909                    // 04/28/2012
910                    intervalDate = new Date((new DateTime(2012, 4, 28, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
911                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
912                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
913                    lb = leaveBlockList.get(0);             
914                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 24, not " + lb.getLeaveAmount().toString()
915                                     , lb.getLeaveAmount().equals(new BigDecimal(24)));
916                    // 05/12/2012
917                    intervalDate = new Date((new DateTime(2012, 5, 12, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
918                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
919                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
920                    // 08/04/2012
921                    intervalDate = new Date((new DateTime(2012, 8, 4, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
922                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
923                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
924                    // 08/18/2012
925                    intervalDate = new Date((new DateTime(2012, 8, 18, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
926                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
927                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
928            }
929    
930            @Test
931            /*      testUser18's service Date is 2012-03-20 which is a Tuesday
932             *      testUser18 has one accrual category "testAC21" with effectiveDate of 2012-03-01
933             *  testAC21 has proration = false, minimum percentage = 0
934             *  testAC21 has "weekly" as the earn interval
935             *  The rule associated with the AC has 24 as the accrual rate
936             *  run accrual for testUser18 for 6 months
937             */
938            public void testWeeklyAsEarnInterval() {
939                    String principal_id = "testUser18";
940                    Calendar aCal = Calendar.getInstance();
941                    aCal.setTime(START_DATE);
942                    aCal.add(Calendar.MONTH, 6);
943                    Date endDate = new java.sql.Date(aCal.getTime().getTime());
944                     
945                    List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
946                    Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
947                    
948                    TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
949                    
950                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
951                    Assert.assertTrue("There should be 22 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 22);        
952                    
953                    // 03/24/2012
954                    Date intervalDate = new Date((new DateTime(2012, 3, 24, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
955                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
956                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
957                    LeaveBlock lb = leaveBlockList.get(0);          
958                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 24, not " + lb.getLeaveAmount().toString()
959                                     , lb.getLeaveAmount().equals(new BigDecimal(24)));             
960                    // 03/31/2012
961                    intervalDate = new Date((new DateTime(2012, 3, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
962                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
963                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
964                    lb = leaveBlockList.get(0);             
965                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 24, not " + lb.getLeaveAmount().toString()
966                                     , lb.getLeaveAmount().equals(new BigDecimal(24)));
967                    // 08/11/2012
968                    intervalDate = new Date((new DateTime(2012, 8, 11, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
969                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
970                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
971                    lb = leaveBlockList.get(0);             
972                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 24, not " + lb.getLeaveAmount().toString()
973                                     , lb.getLeaveAmount().equals(new BigDecimal(24)));
974                    // 08/18/2012
975                    intervalDate = new Date((new DateTime(2012, 8, 18, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
976                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
977                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
978                    lb = leaveBlockList.get(0);             
979                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 24, not " + lb.getLeaveAmount().toString()
980                                     , lb.getLeaveAmount().equals(new BigDecimal(24)));
981            }
982            @Test
983            /*      testUser19's service Date is 2012-03-20
984             *      testUser19 has one accrual category "testAC22" with effectiveDate of 2012-03-01
985             *  testAC22 has "yearly" as the earn interval
986             *  The rule associated with the AC has 100 hours as the accrual rate
987             *  run accrual for testUser19 for 18 months
988             */
989            public void testYearlyAsEarnInterval() {
990                    String principal_id = "testUser19";
991                    Calendar aCal = Calendar.getInstance();
992                    aCal.setTime(START_DATE);
993                    aCal.add(Calendar.MONTH, 18);
994                    Date endDate = new java.sql.Date(aCal.getTime().getTime());
995                     
996                    List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
997                    Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
998                    
999                    TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
1000                    
1001                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
1002                    Assert.assertTrue("There should be 1 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 1);
1003                    
1004                    // 12/31/2013
1005                    Date intervalDate = new Date((new DateTime(2012, 12, 31, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
1006                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
1007                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
1008                    LeaveBlock lb = leaveBlockList.get(0);          
1009                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 100, not " + lb.getLeaveAmount().toString()
1010                                     , lb.getLeaveAmount().equals(new BigDecimal(100)));            
1011            }
1012            @Test
1013            /*      testUser20's service Date is 2012-03-20
1014             *      testUser20 has one accrual category "testAC23" with effectiveDate of 2012-03-01
1015             *  testAC23 has "daily" as the earn interval
1016             *  The rule associated with the AC has 2 as the accrual rate
1017             *  run accrual for testUser20 for 3 months
1018             */
1019            public void testDailyAsEarnInterval() {
1020                    String principal_id = "testUser20";
1021                    Calendar aCal = Calendar.getInstance();
1022                    aCal.setTime(START_DATE);
1023                    aCal.add(Calendar.MONTH, 3);
1024                    Date endDate = new java.sql.Date(aCal.getTime().getTime());
1025                     
1026                    List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
1027                    Assert.assertTrue("There are leave blocks before runAccrual for princiapl id " + principal_id, leaveBlockList.isEmpty());
1028                    
1029                    TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
1030                    
1031                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
1032                    Assert.assertTrue("There should be 44 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 44);
1033                    
1034                    // 03/20/2012
1035                    Date intervalDate = new Date((new DateTime(2012, 3, 20, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
1036                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
1037                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
1038                    LeaveBlock lb = leaveBlockList.get(0);          
1039                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 2, not " + lb.getLeaveAmount().toString()
1040                                     , lb.getLeaveAmount().equals(new BigDecimal(2)));      
1041                    
1042                    // 05/28/2012
1043                    intervalDate = new Date((new DateTime(2012, 5, 18, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
1044                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
1045                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
1046                    lb = leaveBlockList.get(0);             
1047                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 2, not " + lb.getLeaveAmount().toString()
1048                                     , lb.getLeaveAmount().equals(new BigDecimal(2)));
1049            }
1050    
1051            @Test
1052            /*      testUser21's service Date is 2012-03-20
1053             *      testUser21 has one accrual category "testAC24" with effectiveDate of 2012-03-01
1054             *  testAC24 has "monthly" as the earn interval
1055             *  There's a accrual ssto leave block on 04/10/2012 for testUser21,no usage lb, so that's a banked ssto accrual
1056             *  run accrual for testUser21 for 3 months, the accrual service should not delete the existing leaveblock
1057             *  and should not generate new ssto accrual/usage lbs on 04/10/2012
1058             *  There's a balance transferred leave block on 04/15/2012, sstoId is not empty, 
1059             *  accrual service should not generate ssto accrual leave blocks on 04/15/2012 
1060             */
1061            public void testSSTOBankedOrTransferred() {
1062                    String principal_id = "testUser21";
1063                    Calendar aCal = Calendar.getInstance();
1064                    aCal.setTime(START_DATE);
1065                    aCal.add(Calendar.MONTH, 3);
1066                    Date endDate = new java.sql.Date(aCal.getTime().getTime());
1067                     
1068                    List<LeaveBlock> leaveBlockList = (List<LeaveBlock>) TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
1069                    Assert.assertTrue("There should be 1 leave blocks for princiapl id before runAccrual" + principal_id, leaveBlockList.size() == 2);
1070                    
1071                    TkServiceLocator.getLeaveAccrualService().runAccrual(principal_id, START_DATE, endDate, false);
1072                    
1073                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks(principal_id, START_DATE, endDate);
1074                    Assert.assertTrue("There should be 4 leave blocks for emplyee " + principal_id + ", not " + leaveBlockList.size(), leaveBlockList.size()== 4);
1075                    
1076                    // 04/10/2012
1077                    Date intervalDate = new Date((new DateTime(2012, 4, 10, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
1078                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
1079                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
1080                    LeaveBlock lb = leaveBlockList.get(0);          
1081                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 8, not " + lb.getLeaveAmount().toString()
1082                                     , lb.getLeaveAmount().equals(new BigDecimal(8)));      
1083                    Assert.assertTrue("LeaveBlockId of the leave block for date  " + intervalDate.toString() + " should be 5004, not " + lb.getLmLeaveBlockId()
1084                                     , lb.getLmLeaveBlockId().equals("5004"));      
1085                    
1086                    // 04/15/2012
1087                    intervalDate = new Date((new DateTime(2012, 4, 15, 5, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
1088                    leaveBlockList = TkServiceLocator.getLeaveBlockService().getLeaveBlocksForDate(principal_id, intervalDate);
1089                    Assert.assertTrue("There should be 1 leave block for employee " + principal_id + " for date " + intervalDate.toString(), leaveBlockList.size()==1);
1090                    lb = leaveBlockList.get(0);             
1091                    Assert.assertTrue("Hours of the leave block for date  " + intervalDate.toString() + " should be 4, not " + lb.getLeaveAmount().toString()
1092                                     , lb.getLeaveAmount().equals(new BigDecimal(4)));      
1093                    Assert.assertTrue("LeaveBlockId of the leave block for date  " + intervalDate.toString() + " should be 5005, not " + lb.getLmLeaveBlockId()
1094                                     , lb.getLmLeaveBlockId().equals("5005"));
1095            }
1096    
1097    }