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