001    /**
002     * Copyright 2004-2013 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.missedpunch.validation;
017    
018    import java.util.List;
019    
020    import org.apache.log4j.Logger;
021    import com.gargoylesoftware.htmlunit.html.*;
022    import org.junit.After;
023    import org.junit.Assert;
024    import org.junit.Before;
025    import org.junit.Test;
026    import org.kuali.hr.test.KPMETestCase;
027    import org.kuali.hr.time.test.HtmlUnitUtil;
028    import org.kuali.hr.time.test.TkTestConstants;
029    
030    import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
031    import com.gargoylesoftware.htmlunit.WebClient;
032    import com.google.common.collect.Lists;
033    
034    public class MissedPunchDocumentTest extends KPMETestCase {
035        private static final Logger LOG = Logger.getLogger(MissedPunchDocumentTest.class);
036    
037            @Before
038            public void setUp() throws Exception {
039                    super.setUp();  
040            }
041    
042            @After
043            public void tearDown() throws Exception {
044                    super.tearDown();
045            }
046    
047            public void updateWebClient() {
048            WebClient webClient = getWebClient();
049                    webClient.setJavaScriptEnabled(true);
050                    webClient.setThrowExceptionOnScriptError(true);
051                    webClient.setAjaxController(new NicelyResynchronizingAjaxController());
052                    webClient.waitForBackgroundJavaScript(10000);
053            }
054    
055            @Test
056            public void testMissedPunch() throws Exception {
057    
058                    String baseUrl = TkTestConstants.Urls.CLOCK_URL;
059                    HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
060                    Assert.assertNotNull(page);
061                    Assert.assertTrue("Clock Page contains Missed Punch Button", page.asText()
062                                    .contains("Missed Punch"));
063                    updateWebClient();
064    
065                    LOG.debug("Page is : " + page.asText());
066    
067                    // getting Assignment options
068                    HtmlSelect assOption = (HtmlSelect) page
069                                    .getElementByName("selectedAssignment");
070                    Iterable<HtmlElement> it = assOption.getChildElements();
071                    List<HtmlElement> assList = Lists.newLinkedList(it);
072                    
073                    LOG.debug("Second ele is : "+assList.get(2));
074    
075                    // set Assignment value
076                    HtmlOption ho = null;
077                    if(assList.size() > 2) {
078                            ho = (HtmlOption) assList.get(2);
079                    } else {
080                            ho= (HtmlOption) assList.get(0);
081                    }
082                    LOG.debug("ho is : " + ho.asText());
083                    assOption.setSelectedAttribute(ho, true);
084    
085                                    
086                    // get tdocid 
087                    HtmlTableDataCell htmlTable = (HtmlTableDataCell) page.getByXPath(
088                                    "//tbody//tr//td").get(9);
089                    LOG.debug("htmlTable.getTextContent();"
090                                    + htmlTable.getTextContent());
091    
092                    String docId = htmlTable.getTextContent();
093                    
094                    // redirect to missed punch page
095                    HtmlPage mPunchPage = HtmlUnitUtil
096                                    .gotoPageAndLogin(getWebClient(), HtmlUnitUtil.getBaseURL()
097                                                    + "/missedPunch.do?methodToCall=docHandler&command=initiate&docTypeName=MissedPunchDocumentType&tdocid="
098                                                    + docId);
099                    Assert.assertNotNull(mPunchPage);
100                    
101                    // clock action and assignment are drop down lists that are not readonly
102                    HtmlElement element = (HtmlElement)mPunchPage.getElementById("document.clockAction");
103                    Assert.assertNotNull(element);
104                    element = (HtmlElement)mPunchPage.getElementById("document.assignment");
105                    Assert.assertNotNull(element);
106    
107            LOG.debug("Page1 is : " + mPunchPage.asText());
108    
109                    // set Invalid action time
110                    setFieldValue(mPunchPage, "document.documentHeader.documentDescription",
111                                    "Missed Punch- test");
112                    setFieldValue(mPunchPage, "document.clockAction", "CO");
113                    setFieldValue(mPunchPage, "document.actionDate", "01/16/2012");
114                    setFieldValue(mPunchPage, "document.actionTime", "5:5");
115                    setFieldValue(mPunchPage, "document.assignment", "2_1234_2");
116    
117                    HtmlElement elementSubmit = mPunchPage
118                                    .getElementByName("methodToCall.route");
119                    mPunchPage = elementSubmit.click();
120    
121            LOG.debug("After first click >>>>> page is : "
122                                    + mPunchPage.asText());
123    
124                    Assert.assertTrue("page text:\n" + mPunchPage.asText() + "\n does not contain:\n",
125                                    mPunchPage.asText().contains("5:5 is not a valid time."));
126    
127                    // set Future time 
128                    setFieldValue(mPunchPage, "document.actionDate", "01/20/2020");
129                    setFieldValue(mPunchPage, "document.actionTime", "5:50 AM");
130    
131                    elementSubmit = mPunchPage.getElementByName("methodToCall.route");
132                    mPunchPage = elementSubmit.click();
133    
134                    Assert.assertTrue(
135                                    "page text:\n" + mPunchPage.asText() + "\n does not contain:\n",
136                                    mPunchPage.asText().contains(
137                                                    "Missed Punch Action Date cannot be a future date"));
138    
139            LOG.debug("After second click >>>>> page is : "
140                                    + mPunchPage.asText());
141    
142                    // before last clock in time
143                    setFieldValue(mPunchPage, "document.clockAction", "CI");
144                    setFieldValue(mPunchPage, "document.actionDate", "12/20/2009");
145                    setFieldValue(mPunchPage, "document.actionTime", "5:50 AM");
146    //              setFieldValue(mPunchPage, "document.assignment", "2_1234_2");
147    
148                    elementSubmit = mPunchPage.getElementByName("methodToCall.route");
149    
150                    mPunchPage = elementSubmit.click();
151            LOG.debug("After third click >>>>> page is : "
152                                    + mPunchPage.asText());
153    
154                    Assert.assertTrue(
155                                    "page text:\n" + mPunchPage.asText() + "\n does not contain:\n",
156                                    mPunchPage.asText()
157                                                    .contains(
158                                                                    "Assignment is not effective as of date chosen."));
159    
160            
161                    // set proper value
162                    setFieldValue(mPunchPage, "document.actionDate", "01/19/2012");
163                    setFieldValue(mPunchPage, "document.actionTime", "5:50 AM");
164                    setFieldValue(mPunchPage, "document.clockAction", "CI");
165                    //setFieldValue(mPunchPage, "document.assignment", "2_1234_2");
166    
167                    HtmlElement elementSubmit1 = mPunchPage.getElementByName("methodToCall.route");
168                    mPunchPage = elementSubmit1.click();
169    
170            LOG.debug("After forth click >>>>> page is : "
171                                    + mPunchPage.asText());
172    
173                    Assert.assertTrue("page text:\n" + mPunchPage.asText() + "\n does not contain:\n",
174                                    mPunchPage.asText().contains("Document was successfully submitted."));
175                    
176                    // open another missed punch doc for clock out
177                    mPunchPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), HtmlUnitUtil.getBaseURL()
178                                    + "/missedPunch.do?methodToCall=docHandler&command=initiate&docTypeName=MissedPunchDocumentType&tdocid="
179                                    + docId);
180                    Assert.assertNotNull(mPunchPage);
181                    element = (HtmlElement)mPunchPage.getElementById("document.clockAction");
182                    Assert.assertNotNull(element);
183                    // element not found for assignment since it is a readonly field now
184                    element = (HtmlElement)mPunchPage.getElementById("document.assignment");
185                    Assert.assertNull(element);
186                    Assert.assertTrue("page text:does not contain: \n", 
187                                    mPunchPage.asText().contains("Assignment:        work area description : $0.00 Rcd 2 TEST-DEPT description 2"));
188                    
189            }
190    }