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     */
016    package org.kuali.hr.time.systemlunch;
017    
018    import java.util.LinkedHashMap;
019    import java.util.Map;
020    
021    import org.joda.time.DateTime;
022    import org.junit.Assert;
023    import org.junit.Test;
024    import org.kuali.hr.KPMEWebTestCase;
025    import org.kuali.hr.util.HtmlUnitUtil;
026    import org.kuali.kpme.core.FunctionalTest;
027    import org.kuali.kpme.core.util.HrTestConstants;
028    import org.kuali.kpme.core.util.TKUtils;
029    import org.kuali.kpme.tklm.common.TkConstants;
030    import org.kuali.kpme.tklm.time.rules.lunch.sys.SystemLunchRule;
031    import org.kuali.kpme.tklm.time.service.TkServiceLocator;
032    import org.kuali.kpme.tklm.utils.TkTestConstants;
033    
034    import com.gargoylesoftware.htmlunit.ElementNotFoundException;
035    import com.gargoylesoftware.htmlunit.html.HtmlForm;
036    import com.gargoylesoftware.htmlunit.html.HtmlPage;
037    import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
038    
039    @FunctionalTest
040    public class SystemLunchRuleTest extends KPMEWebTestCase {
041            
042            SystemLunchRule systemLunchRule;
043            DateTime date = new DateTime(2010, 1, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone());
044            
045            @Test
046            public void testSystemLunchRuleFetch() throws Exception{
047                    this.systemLunchRule = TkServiceLocator.getSystemLunchRuleService().getSystemLunchRule(date.toLocalDate());
048                    Assert.assertTrue("System lunch rule is pulled back", this.systemLunchRule!=null);
049            }
050            
051            /**
052             * Test if the lunch in/out button shows and if the time block is created with the correct clock action
053             */
054            
055            @Test
056            public void testSystemLunchRule() throws Exception {
057                    
058                    systemLunchRule = TkServiceLocator.getSystemLunchRuleService().getSystemLunchRule(date.toLocalDate());
059                    Assert.assertTrue("System lunch rule is pulled back", systemLunchRule!=null);
060    
061            String baseUrl = TkTestConstants.Urls.CLOCK_URL;
062            HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl, false);
063            Thread.sleep(3000);
064    //      HtmlPage page = HtmlUnitUtil.gotoPageAnBatchJobEntryTestdLogin(TkTestConstants.Urls.CLOCK_URL);
065            Assert.assertNotNull(page);
066            
067            Map<String, Object> criteria = new LinkedHashMap<String, Object>();
068            criteria.put("selectedAssignment", new String[]{HrTestConstants.FormElementTypes.DROPDOWN, "2_1234_2"});
069            HtmlUnitUtil.createTempFile(page);
070            // choose the first assignment from the drop down
071            page = HtmlUnitUtil.fillOutForm(page, criteria);
072            Assert.assertNotNull(page);
073            //Thread.sleep(3000);
074            // clock in
075            page = HtmlUnitUtil.clickClockInOrOutButton(page);
076            Thread.sleep(3000);
077            HtmlUnitUtil.createTempFile(page);
078            Assert.assertTrue("The take lunch button didn't appear", page.asXml().contains("lunchOut"));
079            Thread.sleep(3000);
080            // the lunch in button should display after clocking in
081            page = HtmlUnitUtil.clickLunchInOrOutButton(page, "LO");
082            //Thread.sleep(3000);
083            Assert.assertTrue("The return from lunch button didn't appear", page.asXml().contains("lunchIn"));
084            Thread.sleep(3000);
085            Assert.assertEquals(TkConstants.LUNCH_OUT, TkServiceLocator.getClockLogService().getLastClockLog("admin").getClockAction());
086            
087            if(systemLunchRule.getShowLunchButton()) {
088                    //should be in its own test case, but do not want to expect the exception for purposes of another operation encountering it.
089                    HtmlForm defaultForm = HtmlUnitUtil.getDefaultForm(page);
090                    try {
091                            @SuppressWarnings("unused")
092                                    HtmlSubmitInput input = defaultForm.getInputByName("clockAction");
093                    } catch (Exception e) {
094                    Assert.assertTrue("Clock Out button should not be present", e instanceof ElementNotFoundException);
095                    }
096                    
097            }
098            //Thread.sleep(3000);
099            // 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    }