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.systemlunch;
17  
18  import java.util.LinkedHashMap;
19  import java.util.Map;
20  
21  import org.joda.time.DateTime;
22  import org.junit.Assert;
23  import org.junit.Test;
24  import org.kuali.hr.KPMEWebTestCase;
25  import org.kuali.hr.util.HtmlUnitUtil;
26  import org.kuali.kpme.core.FunctionalTest;
27  import org.kuali.kpme.core.util.HrTestConstants;
28  import org.kuali.kpme.core.util.TKUtils;
29  import org.kuali.kpme.tklm.common.TkConstants;
30  import org.kuali.kpme.tklm.time.rules.lunch.sys.SystemLunchRule;
31  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
32  import org.kuali.kpme.tklm.utils.TkTestConstants;
33  
34  import com.gargoylesoftware.htmlunit.ElementNotFoundException;
35  import com.gargoylesoftware.htmlunit.html.HtmlForm;
36  import com.gargoylesoftware.htmlunit.html.HtmlPage;
37  import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
38  
39  @FunctionalTest
40  public class SystemLunchRuleTest extends KPMEWebTestCase {
41  	
42  	SystemLunchRule systemLunchRule;
43  	DateTime date = new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone());
44  	
45  	@Test
46  	public void testSystemLunchRuleFetch() throws Exception{
47  		this.systemLunchRule = TkServiceLocator.getSystemLunchRuleService().getSystemLunchRule(date.toLocalDate());
48  		Assert.assertTrue("System lunch rule is pulled back", this.systemLunchRule!=null);
49  	}
50  	
51  	/**
52  	 * Test if the lunch in/out button shows and if the time block is created with the correct clock action
53  	 */
54  	
55  	@Test
56  	public void testSystemLunchRule() throws Exception {
57  		
58  		systemLunchRule = TkServiceLocator.getSystemLunchRuleService().getSystemLunchRule(date.toLocalDate());
59  		Assert.assertTrue("System lunch rule is pulled back", systemLunchRule!=null);
60  
61          String baseUrl = TkTestConstants.Urls.CLOCK_URL;
62      	HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl, false);
63          Thread.sleep(3000);
64  //    	HtmlPage page = HtmlUnitUtil.gotoPageAnBatchJobEntryTestdLogin(TkTestConstants.Urls.CLOCK_URL);
65      	Assert.assertNotNull(page);
66      	
67      	Map<String, Object> criteria = new LinkedHashMap<String, Object>();
68      	criteria.put("selectedAssignment", new String[]{HrTestConstants.FormElementTypes.DROPDOWN, "2_1234_2"});
69      	HtmlUnitUtil.createTempFile(page);
70      	// choose the first assignment from the drop down
71      	page = HtmlUnitUtil.fillOutForm(page, criteria);
72      	Assert.assertNotNull(page);
73          //Thread.sleep(3000);
74      	// clock in
75      	page = HtmlUnitUtil.clickClockInOrOutButton(page);
76          Thread.sleep(3000);
77      	HtmlUnitUtil.createTempFile(page);
78      	Assert.assertTrue("The take lunch button didn't appear", page.asXml().contains("lunchOut"));
79          Thread.sleep(3000);
80      	// the lunch in button should display after clocking in
81      	page = HtmlUnitUtil.clickLunchInOrOutButton(page, "LO");
82          //Thread.sleep(3000);
83      	Assert.assertTrue("The return from lunch button didn't appear", page.asXml().contains("lunchIn"));
84      	Thread.sleep(3000);
85      	Assert.assertEquals(TkConstants.LUNCH_OUT, TkServiceLocator.getClockLogService().getLastClockLog("admin").getClockAction());
86      	
87      	if(systemLunchRule.getShowLunchButton()) {
88      		//should be in its own test case, but do not want to expect the exception for purposes of another operation encountering it.
89          	HtmlForm defaultForm = HtmlUnitUtil.getDefaultForm(page);
90          	try {
91          		@SuppressWarnings("unused")
92  				HtmlSubmitInput input = defaultForm.getInputByName("clockAction");
93          	} catch (Exception e) {
94              	Assert.assertTrue("Clock Out button should not be present", e instanceof ElementNotFoundException);
95          	}
96          	
97      	}
98          //Thread.sleep(3000);
99      	// the lunch out button should display after lunching in
100     	page = HtmlUnitUtil.clickLunchInOrOutButton(page, "LI");
101     	Thread.sleep(3000);
102     	Assert.assertEquals(TkConstants.LUNCH_IN, TkServiceLocator.getClockLogService().getLastClockLog("admin").getClockAction());
103     	
104 	}
105 }