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