View Javadoc

1   /**
2    * Copyright 2004-2013 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.sql.Date;
20  import java.util.Calendar;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.joda.time.DateTime;
24  import org.junit.Assert;
25  import org.junit.Test;
26  import org.kuali.hr.test.KPMETestCase;
27  import org.kuali.hr.time.dept.lunch.DeptLunchRule;
28  import org.kuali.hr.time.service.base.TkServiceLocator;
29  import org.kuali.hr.time.test.TkTestUtils;
30  import org.kuali.hr.time.timeblock.TimeBlock;
31  import org.kuali.hr.time.timeblock.TimeHourDetail;
32  import org.kuali.hr.time.timesheet.TimesheetDocument;
33  import org.kuali.hr.time.util.TKContext;
34  import org.kuali.hr.time.util.TKUser;
35  import org.kuali.hr.time.util.TKUtils;
36  import org.kuali.hr.time.util.TkConstants;
37  import org.kuali.rice.kim.api.identity.Person;
38  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
39  import org.kuali.rice.krad.service.KRADServiceLocator;
40  
41  public class DepartmentLunchRuleTest extends KPMETestCase {
42      private Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
43  
44  	@Test
45  	public void testDepartmentLunchRuleFetch() throws Exception{
46  		DeptLunchRule deptLunchRule = new DeptLunchRule();
47  		deptLunchRule.setActive(true);
48  		deptLunchRule.setDept("TEST");
49  		deptLunchRule.setWorkArea(1234L);
50  		deptLunchRule.setEffectiveDate(JAN_AS_OF_DATE);
51  		deptLunchRule.setJobNumber(0L);
52  		deptLunchRule.setPrincipalId("admin");
53  		deptLunchRule.setDeductionMins(new BigDecimal(30));
54  		deptLunchRule.setShiftHours(new BigDecimal(6));
55  		deptLunchRule.setTkDeptLunchRuleId("1001");
56  
57  		KRADServiceLocator.getBusinessObjectService().save(deptLunchRule);
58  
59  		deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST",
60  											1234L, "admin", 0L, JAN_AS_OF_DATE);
61  		Assert.assertTrue("dept lunch rule fetched ", deptLunchRule!=null);
62  
63  	}
64  
65  	/**
66  	 * Test if the minute deduction rule is applied correctly if there is a valid department lunch rule
67  	 */
68  
69  	@Test
70  	public void testDepartmentLunchRule() throws Exception {
71  		// create a dept lunch rule
72  		DeptLunchRule deptLunchRule = new DeptLunchRule();
73  		deptLunchRule.setActive(true);
74  		deptLunchRule.setDept("TEST-DEPT");
75  		deptLunchRule.setWorkArea(1234L);
76  		Calendar cal = Calendar.getInstance();
77  		cal.set(2010, 1, 1);
78  		deptLunchRule.setEffectiveDate(JAN_AS_OF_DATE);
79  		deptLunchRule.setJobNumber(1L);
80  		deptLunchRule.setPrincipalId("edna");
81  		deptLunchRule.setDeductionMins(new BigDecimal(30));
82  		deptLunchRule.setShiftHours(new BigDecimal(6));
83  		deptLunchRule.setTkDeptLunchRuleId("1001");
84  
85  		KRADServiceLocator.getBusinessObjectService().save(deptLunchRule);
86  
87  		deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST-DEPT",
88  											1234L, "edna", 1L, JAN_AS_OF_DATE);
89  		Assert.assertTrue("dept lunch rule fetched ", deptLunchRule!=null);
90  
91          Person testUser = KimApiServiceLocator.getPersonService().getPerson("edna");
92          TKUser.setTargetPerson(testUser);
93  		TimesheetDocument doc = TkTestUtils.populateTimesheetDocument(JAN_AS_OF_DATE);
94  
95  		for(TimeBlock tb : doc.getTimeBlocks()){
96  			tb.setClockLogCreated(true);
97  		}
98          //reset time block
99          //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 }