View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.department.lunch.rule;
17  
18  import java.math.BigDecimal;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.joda.time.DateTime;
23  import org.junit.Assert;
24  import org.junit.Test;
25  import org.kuali.hr.KPMEWebTestCase;
26  import org.kuali.kpme.core.FunctionalTest;
27  import org.kuali.kpme.core.util.HrConstants;
28  import org.kuali.kpme.core.util.TKUtils;
29  import org.kuali.kpme.tklm.time.rules.lunch.department.DeptLunchRule;
30  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
31  import org.kuali.kpme.tklm.time.timeblock.TimeBlock;
32  import org.kuali.kpme.tklm.time.timehourdetail.TimeHourDetail;
33  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
34  import org.kuali.kpme.tklm.utils.TkTestUtils;
35  import org.kuali.rice.krad.service.KRADServiceLocator;
36  
37  @FunctionalTest
38  public class DepartmentLunchRuleTest extends KPMEWebTestCase {
39      private DateTime JAN_AS_OF_DATE = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
40  
41  	@Test
42  	public void testDepartmentLunchRuleFetch() throws Exception{
43  		DeptLunchRule deptLunchRule = new DeptLunchRule();
44  		deptLunchRule.setActive(true);
45  		deptLunchRule.setDept("TEST");
46  		deptLunchRule.setWorkArea(1234L);
47  		deptLunchRule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
48  		deptLunchRule.setJobNumber(0L);
49  		deptLunchRule.setPrincipalId("admin");
50  		deptLunchRule.setDeductionMins(new BigDecimal(30));
51  		deptLunchRule.setShiftHours(new BigDecimal(6));
52  		deptLunchRule.setTkDeptLunchRuleId("1001");
53  
54  		KRADServiceLocator.getBusinessObjectService().save(deptLunchRule);
55  
56  		deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST",
57  											1234L, "admin", 0L, JAN_AS_OF_DATE.toLocalDate());
58  		Assert.assertTrue("dept lunch rule fetched ", deptLunchRule!=null);
59  
60  	}
61  
62  	/**
63  	 * Test if the minute deduction rule is applied correctly if there is a valid department lunch rule
64  	 */
65  
66  	@Test
67  	public void testDepartmentLunchRule() throws Exception {
68  		// create a dept lunch rule
69  		DeptLunchRule deptLunchRule = new DeptLunchRule();
70  		deptLunchRule.setActive(true);
71  		deptLunchRule.setDept("TEST-DEPT");
72  		deptLunchRule.setWorkArea(1234L);
73  		deptLunchRule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
74  		deptLunchRule.setJobNumber(1L);
75  		deptLunchRule.setPrincipalId("edna");
76  		deptLunchRule.setDeductionMins(new BigDecimal(30));
77  		deptLunchRule.setShiftHours(new BigDecimal(6));
78  		deptLunchRule.setTkDeptLunchRuleId("1001");
79  
80  		KRADServiceLocator.getBusinessObjectService().save(deptLunchRule);
81  
82  		deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST-DEPT",
83  											1234L, "edna", 1L, JAN_AS_OF_DATE.toLocalDate());
84  		Assert.assertTrue("dept lunch rule fetched ", deptLunchRule!=null);
85  
86  		TimesheetDocument doc = TkTestUtils.populateTimesheetDocument(JAN_AS_OF_DATE, "edna");
87  
88  		for(TimeBlock tb : doc.getTimeBlocks()){
89  			tb.setClockLogCreated(true);
90  		}
91          //reset time block
92          //TkServiceLocator.getTimesheetService().resetTimeBlock(doc.getTimeBlocks());
93  		//TkServiceLocator.getTkRuleControllerService().applyRules(TkConstants.ACTIONS.ADD_TIME_BLOCK, doc.getTimeBlocks(), doc.getCalendarEntry(), doc, "admin");
94  		for(TimeBlock tb : doc.getTimeBlocks()) {
95  			if(tb.getHours().compareTo(deptLunchRule.getShiftHours()) == 1) {
96  				for(TimeHourDetail thd : tb.getTimeHourDetails()){
97  					// 	this assumes the hours for the dummy timeblocks are always 10
98  					if(!StringUtils.equals(thd.getEarnCode(), HrConstants.LUNCH_EARN_CODE)){
99  						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 }