001/**
002 * Copyright 2004-2015 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 */
016package org.kuali.hr.time.department.lunch.rule;
017
018import java.math.BigDecimal;
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.commons.lang.StringUtils;
023import org.joda.time.DateTime;
024import org.junit.Assert;
025import org.junit.Test;
026import org.kuali.hr.KPMEWebTestCase;
027import org.kuali.kpme.core.FunctionalTest;
028import org.kuali.kpme.core.util.HrConstants;
029import org.kuali.kpme.core.util.TKUtils;
030import org.kuali.kpme.tklm.api.time.timeblock.TimeBlock;
031import org.kuali.kpme.tklm.api.time.timehourdetail.TimeHourDetail;
032import org.kuali.kpme.tklm.time.rules.lunch.department.DeptLunchRule;
033import org.kuali.kpme.tklm.time.service.TkServiceLocator;
034import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
035import org.kuali.kpme.tklm.utils.TkTestUtils;
036import org.kuali.rice.krad.service.KRADServiceLocator;
037
038@FunctionalTest
039public class DepartmentLunchRuleTest extends KPMEWebTestCase {
040    private DateTime JAN_AS_OF_DATE = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
041
042        @Test
043        public void testDepartmentLunchRuleFetch() throws Exception{
044                DeptLunchRule deptLunchRule = new DeptLunchRule();
045                deptLunchRule.setActive(true);
046                deptLunchRule.setDept("TEST");
047                deptLunchRule.setWorkArea(1234L);
048                deptLunchRule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
049                deptLunchRule.setJobNumber(0L);
050                deptLunchRule.setPrincipalId("admin");
051                deptLunchRule.setDeductionMins(new BigDecimal(30));
052                deptLunchRule.setShiftHours(new BigDecimal(6));
053                deptLunchRule.setTkDeptLunchRuleId("1001");
054        deptLunchRule.setUserPrincipalId("admin");
055        deptLunchRule.setGroupKeyCode("IU-BL");
056        
057                KRADServiceLocator.getBusinessObjectService().save(deptLunchRule);
058
059                deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST",
060                                                                                        1234L, "admin", 0L, "IU-BL", JAN_AS_OF_DATE.toLocalDate());
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                deptLunchRule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
077                deptLunchRule.setJobNumber(1L);
078                deptLunchRule.setPrincipalId("edna");
079                deptLunchRule.setDeductionMins(new BigDecimal(30));
080                deptLunchRule.setShiftHours(new BigDecimal(6));
081                deptLunchRule.setTkDeptLunchRuleId("1001");
082        deptLunchRule.setUserPrincipalId("admin");
083        deptLunchRule.setGroupKeyCode("IU-BL");
084        
085                KRADServiceLocator.getBusinessObjectService().save(deptLunchRule);
086
087                deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST-DEPT",
088                                                                                        1234L, "edna", 1L, "IU-BL", JAN_AS_OF_DATE.toLocalDate());
089                Assert.assertTrue("dept lunch rule fetched ", deptLunchRule!=null);
090
091                TimesheetDocument doc = TkTestUtils.populateTimesheetDocument(JAN_AS_OF_DATE, "edna");
092
093        List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>(doc.getTimeBlocks());
094                for(TimeBlock tb : doc.getTimeBlocks()){
095            TimeBlock.Builder b = TimeBlock.Builder.create(tb);
096                        b.setClockLogCreated(true);
097            timeBlocks.add(b.build());
098                }
099                for(TimeBlock tb : timeBlocks) {
100                        if(tb.getHours().compareTo(deptLunchRule.getShiftHours()) == 1) {
101                                for(TimeHourDetail thd : tb.getTimeHourDetails()){
102                                        //      this assumes the hours for the dummy timeblocks are always 10
103                                        if(!StringUtils.equals(thd.getEarnCode(), HrConstants.LUNCH_EARN_CODE)){
104                                                Assert.assertEquals(new BigDecimal(9.50).setScale(2), tb.getHours());
105                                        }
106                                }
107                        }
108                }
109
110        }
111
112        @Test
113        public void testSearchDepartmentLunchRules() throws Exception {
114                //List<DeptLunchRule> allResults = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRules("admin", null, null, null, null, "IU-BL", null, null, "Y", "N");
115                //Assert.assertEquals("Search returned the correct number of results.", 2, allResults.size());
116                
117                //List<DeptLunchRule> restrictedResults = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRules("fran", null, null, null, null, "IU-BL", null, null, "Y", "N");
118                //Assert.assertEquals("Search returned the correct number of results.", 0, restrictedResults.size());
119        }
120        
121}