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 */
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;
037import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
038
039@FunctionalTest
040public class DepartmentLunchRuleTest extends KPMEWebTestCase {
041    private DateTime JAN_AS_OF_DATE = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
042
043        @Test
044        public void testDepartmentLunchRuleFetch() throws Exception{
045                DeptLunchRule deptLunchRule = new DeptLunchRule();
046                deptLunchRule.setActive(true);
047                deptLunchRule.setDept("TEST");
048                deptLunchRule.setWorkArea(1234L);
049                deptLunchRule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
050                deptLunchRule.setJobNumber(0L);
051                deptLunchRule.setPrincipalId("admin");
052                deptLunchRule.setDeductionMins(new BigDecimal(30));
053                deptLunchRule.setShiftHours(new BigDecimal(6));
054                deptLunchRule.setTkDeptLunchRuleId("1001");
055        deptLunchRule.setUserPrincipalId("admin");
056        deptLunchRule.setGroupKeyCode("IU-BL");
057        
058                KRADServiceLocatorWeb.getLegacyDataAdapter().save(deptLunchRule);
059
060                deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST",
061                                                                                        1234L, "admin", 0L, "IU-BL", JAN_AS_OF_DATE.toLocalDate());
062                Assert.assertTrue("dept lunch rule fetched ", deptLunchRule!=null);
063
064        }
065
066        /**
067         * Test if the minute deduction rule is applied correctly if there is a valid department lunch rule
068         */
069
070        @Test
071        public void testDepartmentLunchRule() throws Exception {
072                // create a dept lunch rule
073                DeptLunchRule deptLunchRule = new DeptLunchRule();
074                deptLunchRule.setActive(true);
075                deptLunchRule.setDept("TEST-DEPT");
076                deptLunchRule.setWorkArea(1234L);
077                deptLunchRule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
078                deptLunchRule.setJobNumber(1L);
079                deptLunchRule.setPrincipalId("edna");
080                deptLunchRule.setDeductionMins(new BigDecimal(30));
081                deptLunchRule.setShiftHours(new BigDecimal(6));
082                deptLunchRule.setTkDeptLunchRuleId("1001");
083        deptLunchRule.setUserPrincipalId("admin");
084        deptLunchRule.setGroupKeyCode("IU-BL");
085        
086                KRADServiceLocatorWeb.getLegacyDataAdapter().save(deptLunchRule);
087
088                deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST-DEPT",
089                                                                                        1234L, "edna", 1L, "IU-BL", JAN_AS_OF_DATE.toLocalDate());
090                Assert.assertTrue("dept lunch rule fetched ", deptLunchRule!=null);
091
092                TimesheetDocument doc = TkTestUtils.populateTimesheetDocument(JAN_AS_OF_DATE, "edna");
093
094        List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>(doc.getTimeBlocks());
095                for(TimeBlock tb : doc.getTimeBlocks()){
096            TimeBlock.Builder b = TimeBlock.Builder.create(tb);
097                        b.setClockLogCreated(true);
098            timeBlocks.add(b.build());
099                }
100                for(TimeBlock tb : timeBlocks) {
101                        if(tb.getHours().compareTo(deptLunchRule.getShiftHours()) == 1) {
102                                for(TimeHourDetail thd : tb.getTimeHourDetails()){
103                                        //      this assumes the hours for the dummy timeblocks are always 10
104                                        if(!StringUtils.equals(thd.getEarnCode(), HrConstants.LUNCH_EARN_CODE)){
105                                                Assert.assertEquals(new BigDecimal(9.50).setScale(2), tb.getHours());
106                                        }
107                                }
108                        }
109                }
110
111        }
112
113        @Test
114        public void testSearchDepartmentLunchRules() throws Exception {
115                //List<DeptLunchRule> allResults = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRules("admin", null, null, null, null, "IU-BL", null, null, "Y", "N");
116                //Assert.assertEquals("Search returned the correct number of results.", 2, allResults.size());
117                
118                //List<DeptLunchRule> restrictedResults = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRules("fran", null, null, null, null, "IU-BL", null, null, "Y", "N");
119                //Assert.assertEquals("Search returned the correct number of results.", 0, restrictedResults.size());
120        }
121        
122}