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.department.lunch.rule;
017    
018    import java.math.BigDecimal;
019    import java.sql.Date;
020    import java.util.Calendar;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.joda.time.DateTime;
024    import org.junit.Assert;
025    import org.junit.Test;
026    import org.kuali.hr.test.KPMETestCase;
027    import org.kuali.hr.time.dept.lunch.DeptLunchRule;
028    import org.kuali.hr.time.service.base.TkServiceLocator;
029    import org.kuali.hr.time.test.TkTestUtils;
030    import org.kuali.hr.time.timeblock.TimeBlock;
031    import org.kuali.hr.time.timeblock.TimeHourDetail;
032    import org.kuali.hr.time.timesheet.TimesheetDocument;
033    import org.kuali.hr.time.util.TKContext;
034    import org.kuali.hr.time.util.TKUser;
035    import org.kuali.hr.time.util.TKUtils;
036    import org.kuali.hr.time.util.TkConstants;
037    import org.kuali.rice.kim.api.identity.Person;
038    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
039    import org.kuali.rice.krad.service.KRADServiceLocator;
040    
041    public class DepartmentLunchRuleTest extends KPMETestCase {
042        private Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
043    
044            @Test
045            public void testDepartmentLunchRuleFetch() throws Exception{
046                    DeptLunchRule deptLunchRule = new DeptLunchRule();
047                    deptLunchRule.setActive(true);
048                    deptLunchRule.setDept("TEST");
049                    deptLunchRule.setWorkArea(1234L);
050                    deptLunchRule.setEffectiveDate(JAN_AS_OF_DATE);
051                    deptLunchRule.setJobNumber(0L);
052                    deptLunchRule.setPrincipalId("admin");
053                    deptLunchRule.setDeductionMins(new BigDecimal(30));
054                    deptLunchRule.setShiftHours(new BigDecimal(6));
055                    deptLunchRule.setTkDeptLunchRuleId("1001");
056    
057                    KRADServiceLocator.getBusinessObjectService().save(deptLunchRule);
058    
059                    deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST",
060                                                                                            1234L, "admin", 0L, JAN_AS_OF_DATE);
061                    Assert.assertTrue("dept lunch rule fetched ", deptLunchRule!=null);
062    
063            }
064    
065            /**
066             * Test if the minute deduction rule is applied correctly if there is a valid department lunch rule
067             */
068    
069            @Test
070            public void testDepartmentLunchRule() throws Exception {
071                    // create a dept lunch rule
072                    DeptLunchRule deptLunchRule = new DeptLunchRule();
073                    deptLunchRule.setActive(true);
074                    deptLunchRule.setDept("TEST-DEPT");
075                    deptLunchRule.setWorkArea(1234L);
076                    Calendar cal = Calendar.getInstance();
077                    cal.set(2010, 1, 1);
078                    deptLunchRule.setEffectiveDate(JAN_AS_OF_DATE);
079                    deptLunchRule.setJobNumber(1L);
080                    deptLunchRule.setPrincipalId("edna");
081                    deptLunchRule.setDeductionMins(new BigDecimal(30));
082                    deptLunchRule.setShiftHours(new BigDecimal(6));
083                    deptLunchRule.setTkDeptLunchRuleId("1001");
084    
085                    KRADServiceLocator.getBusinessObjectService().save(deptLunchRule);
086    
087                    deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST-DEPT",
088                                                                                            1234L, "edna", 1L, JAN_AS_OF_DATE);
089                    Assert.assertTrue("dept lunch rule fetched ", deptLunchRule!=null);
090    
091            Person testUser = KimApiServiceLocator.getPersonService().getPerson("edna");
092            TKUser.setTargetPerson(testUser);
093                    TimesheetDocument doc = TkTestUtils.populateTimesheetDocument(JAN_AS_OF_DATE);
094    
095                    for(TimeBlock tb : doc.getTimeBlocks()){
096                            tb.setClockLogCreated(true);
097                    }
098            //reset time block
099            //TkServiceLocator.getTimesheetService().resetTimeBlock(doc.getTimeBlocks());
100                    //TkServiceLocator.getTkRuleControllerService().applyRules(TkConstants.ACTIONS.ADD_TIME_BLOCK, doc.getTimeBlocks(), doc.getCalendarEntry(), doc, "admin");
101                    for(TimeBlock tb : doc.getTimeBlocks()) {
102                            if(tb.getHours().compareTo(deptLunchRule.getShiftHours()) == 1) {
103                                    for(TimeHourDetail thd : tb.getTimeHourDetails()){
104                                            //      this assumes the hours for the dummy timeblocks are always 10
105                                            if(!StringUtils.equals(thd.getEarnCode(), TkConstants.LUNCH_EARN_CODE)){
106                                                    Assert.assertEquals(new BigDecimal(9.50).setScale(2), tb.getHours());
107                                            }
108                                    }
109                            }
110                    }
111    
112            }
113    
114        /*@Override
115        public void tearDown() throws Exception {
116            TKUser.clearTargetUser();
117            super.tearDown();
118        }*/
119    }