001    /**
002     * Copyright 2004-2014 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.util.List;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.joda.time.DateTime;
023    import org.junit.Assert;
024    import org.junit.Test;
025    import org.kuali.hr.KPMEWebTestCase;
026    import org.kuali.kpme.core.FunctionalTest;
027    import org.kuali.kpme.core.util.HrConstants;
028    import org.kuali.kpme.core.util.TKUtils;
029    import org.kuali.kpme.tklm.time.rules.lunch.department.DeptLunchRule;
030    import org.kuali.kpme.tklm.time.service.TkServiceLocator;
031    import org.kuali.kpme.tklm.time.timeblock.TimeBlock;
032    import org.kuali.kpme.tklm.time.timehourdetail.TimeHourDetail;
033    import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
034    import org.kuali.kpme.tklm.utils.TkTestUtils;
035    import org.kuali.rice.krad.service.KRADServiceLocator;
036    
037    @FunctionalTest
038    public class DepartmentLunchRuleTest extends KPMEWebTestCase {
039        private DateTime JAN_AS_OF_DATE = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
040    
041            @Test
042            public void testDepartmentLunchRuleFetch() throws Exception{
043                    DeptLunchRule deptLunchRule = new DeptLunchRule();
044                    deptLunchRule.setActive(true);
045                    deptLunchRule.setDept("TEST");
046                    deptLunchRule.setWorkArea(1234L);
047                    deptLunchRule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
048                    deptLunchRule.setJobNumber(0L);
049                    deptLunchRule.setPrincipalId("admin");
050                    deptLunchRule.setDeductionMins(new BigDecimal(30));
051                    deptLunchRule.setShiftHours(new BigDecimal(6));
052                    deptLunchRule.setTkDeptLunchRuleId("1001");
053    
054                    KRADServiceLocator.getBusinessObjectService().save(deptLunchRule);
055    
056                    deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST",
057                                                                                            1234L, "admin", 0L, JAN_AS_OF_DATE.toLocalDate());
058                    Assert.assertTrue("dept lunch rule fetched ", deptLunchRule!=null);
059    
060            }
061    
062            /**
063             * Test if the minute deduction rule is applied correctly if there is a valid department lunch rule
064             */
065    
066            @Test
067            public void testDepartmentLunchRule() throws Exception {
068                    // create a dept lunch rule
069                    DeptLunchRule deptLunchRule = new DeptLunchRule();
070                    deptLunchRule.setActive(true);
071                    deptLunchRule.setDept("TEST-DEPT");
072                    deptLunchRule.setWorkArea(1234L);
073                    deptLunchRule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
074                    deptLunchRule.setJobNumber(1L);
075                    deptLunchRule.setPrincipalId("edna");
076                    deptLunchRule.setDeductionMins(new BigDecimal(30));
077                    deptLunchRule.setShiftHours(new BigDecimal(6));
078                    deptLunchRule.setTkDeptLunchRuleId("1001");
079    
080                    KRADServiceLocator.getBusinessObjectService().save(deptLunchRule);
081    
082                    deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST-DEPT",
083                                                                                            1234L, "edna", 1L, JAN_AS_OF_DATE.toLocalDate());
084                    Assert.assertTrue("dept lunch rule fetched ", deptLunchRule!=null);
085    
086                    TimesheetDocument doc = TkTestUtils.populateTimesheetDocument(JAN_AS_OF_DATE, "edna");
087    
088                    for(TimeBlock tb : doc.getTimeBlocks()){
089                            tb.setClockLogCreated(true);
090                    }
091            //reset time block
092            //TkServiceLocator.getTimesheetService().resetTimeBlock(doc.getTimeBlocks());
093                    //TkServiceLocator.getTkRuleControllerService().applyRules(TkConstants.ACTIONS.ADD_TIME_BLOCK, doc.getTimeBlocks(), doc.getCalendarEntry(), doc, "admin");
094                    for(TimeBlock tb : doc.getTimeBlocks()) {
095                            if(tb.getHours().compareTo(deptLunchRule.getShiftHours()) == 1) {
096                                    for(TimeHourDetail thd : tb.getTimeHourDetails()){
097                                            //      this assumes the hours for the dummy timeblocks are always 10
098                                            if(!StringUtils.equals(thd.getEarnCode(), HrConstants.LUNCH_EARN_CODE)){
099                                                    Assert.assertEquals(new BigDecimal(9.50).setScale(2), tb.getHours());
100                                            }
101                                    }
102                            }
103                    }
104    
105            }
106    
107            @Test
108            public void testSearchDepartmentLunchRules() throws Exception {
109                    List<DeptLunchRule> allResults = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRules("admin", null, null, null, null, null, null, "Y", "N");
110                    Assert.assertEquals("Search returned the wrong number of results.", 2, allResults.size());
111                    
112                    List<DeptLunchRule> restrictedResults = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRules("fran", null, null, null, null, null, null, "Y", "N");
113                    Assert.assertEquals("Search returned the wrong number of results.", 0, restrictedResults.size());
114            }
115            
116    }