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.ArrayList;
20  import java.util.List;
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.KPMEWebTestCase;
27  import org.kuali.kpme.core.FunctionalTest;
28  import org.kuali.kpme.core.util.HrConstants;
29  import org.kuali.kpme.core.util.TKUtils;
30  import org.kuali.kpme.tklm.api.time.timeblock.TimeBlock;
31  import org.kuali.kpme.tklm.api.time.timehourdetail.TimeHourDetail;
32  import org.kuali.kpme.tklm.time.rules.lunch.department.DeptLunchRule;
33  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
34  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
35  import org.kuali.kpme.tklm.utils.TkTestUtils;
36  import org.kuali.rice.krad.service.KRADServiceLocator;
37  
38  @FunctionalTest
39  public class DepartmentLunchRuleTest extends KPMEWebTestCase {
40      private DateTime JAN_AS_OF_DATE = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
41  
42  	@Test
43  	public void testDepartmentLunchRuleFetch() throws Exception{
44  		DeptLunchRule deptLunchRule = new DeptLunchRule();
45  		deptLunchRule.setActive(true);
46  		deptLunchRule.setDept("TEST");
47  		deptLunchRule.setWorkArea(1234L);
48  		deptLunchRule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
49  		deptLunchRule.setJobNumber(0L);
50  		deptLunchRule.setPrincipalId("admin");
51  		deptLunchRule.setDeductionMins(new BigDecimal(30));
52  		deptLunchRule.setShiftHours(new BigDecimal(6));
53  		deptLunchRule.setTkDeptLunchRuleId("1001");
54          deptLunchRule.setUserPrincipalId("admin");
55          deptLunchRule.setGroupKeyCode("IU-BL");
56          
57  		KRADServiceLocator.getBusinessObjectService().save(deptLunchRule);
58  
59  		deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST",
60  											1234L, "admin", 0L, "IU-BL", JAN_AS_OF_DATE.toLocalDate());
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  		deptLunchRule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
77  		deptLunchRule.setJobNumber(1L);
78  		deptLunchRule.setPrincipalId("edna");
79  		deptLunchRule.setDeductionMins(new BigDecimal(30));
80  		deptLunchRule.setShiftHours(new BigDecimal(6));
81  		deptLunchRule.setTkDeptLunchRuleId("1001");
82          deptLunchRule.setUserPrincipalId("admin");
83          deptLunchRule.setGroupKeyCode("IU-BL");
84          
85  		KRADServiceLocator.getBusinessObjectService().save(deptLunchRule);
86  
87  		deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST-DEPT",
88  											1234L, "edna", 1L, "IU-BL", JAN_AS_OF_DATE.toLocalDate());
89  		Assert.assertTrue("dept lunch rule fetched ", deptLunchRule!=null);
90  
91  		TimesheetDocument doc = TkTestUtils.populateTimesheetDocument(JAN_AS_OF_DATE, "edna");
92  
93          List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>(doc.getTimeBlocks());
94  		for(TimeBlock tb : doc.getTimeBlocks()){
95              TimeBlock.Builder b = TimeBlock.Builder.create(tb);
96  			b.setClockLogCreated(true);
97              timeBlocks.add(b.build());
98  		}
99  		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 }