View Javadoc

1   /**
2    * Copyright 2004-2013 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.workflow;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.joda.time.DateTime;
24  import org.json.simple.JSONArray;
25  import org.json.simple.JSONObject;
26  import org.json.simple.JSONValue;
27  import org.junit.Assert;
28  import org.junit.Test;
29  import org.kuali.hr.TestAutoLoginFilter;
30  import org.kuali.hr.time.util.TimeDetailTestUtils;
31  import org.kuali.hr.util.HtmlUnitUtil;
32  import org.kuali.kpme.core.FunctionalTest;
33  import org.kuali.kpme.core.assignment.Assignment;
34  import org.kuali.kpme.core.assignment.AssignmentDescriptionKey;
35  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
36  import org.kuali.kpme.core.earncode.EarnCode;
37  import org.kuali.kpme.core.service.HrServiceLocator;
38  import org.kuali.kpme.core.util.HrContext;
39  import org.kuali.kpme.core.util.TKUtils;
40  import org.kuali.kpme.tklm.time.detail.web.TimeDetailActionFormBase;
41  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
42  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
43  import org.kuali.rice.kew.api.document.DocumentStatus;
44  
45  import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;
46  import com.gargoylesoftware.htmlunit.html.HtmlForm;
47  import com.gargoylesoftware.htmlunit.html.HtmlPage;
48  
49  @FunctionalTest
50  public class TimesheetWorkflowIntegrationTest extends TimesheetWebTestBase {
51  
52      public static final String USER_PRINCIPAL_ID = "admin";
53  	private DateTime JAN_AS_OF_DATE = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
54  
55  
56      /**
57       * @throws Exception
58       */
59      public void setUp() throws Exception {
60          super.setUp();
61          // Data is loaded as part of database loading lifecycle
62          // See: tk-test-data.sql
63          // See: TimesheetWorkflowIntegrationTest.sql
64          // See: TkTestCase.java
65          //
66      }
67  
68      public void tearDown() throws Exception {
69          super.tearDown();
70      }
71  
72      @Test
73      /**
74       * - create timesheet
75       * - add two 8 hour time blocks
76       * - submit timesheet for routing
77       * - ## login as approver
78       * - look for approval button
79       * - approve timeblock
80       * - verify approval button gone
81       * - ## login as original user
82       * - verify submit for routing button gone
83       */
84      public void testTimesheetSubmissionIntegration() throws Exception {
85          DateTime asOfDate = new DateTime(2011, 3, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone());
86          CalendarEntry pcd = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates(USER_PRINCIPAL_ID, asOfDate);
87          Assert.assertNotNull("No PayCalendarDates", pcd);
88          TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, pcd);
89          String tdocId = tdoc.getDocumentId();
90          HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), "admin", tdocId, true);
91  
92          // 1. Obtain User Data
93          Assignment assignment = HrServiceLocator.getAssignmentService().getAssignment(HrContext.getPrincipalId(), AssignmentDescriptionKey.get("30_30_30"), JAN_AS_OF_DATE.toLocalDate());
94          EarnCode earnCode = HrServiceLocator.getEarnCodeService().getEarnCode("RGN", JAN_AS_OF_DATE.toLocalDate());
95  
96          // 2. Set Timeblock Start and End time
97          // 3/02/2011 - 8:00a to 4:00pm
98          DateTime start = new DateTime(2011, 3, 2, 8, 0, 0, 0, TKUtils.getSystemDateTimeZone());
99          DateTime end = new DateTime(2011, 3, 2, 16, 0, 0, 0, TKUtils.getSystemDateTimeZone());
100 
101         HtmlForm form = page.getFormByName("TimeDetailActionForm");
102         Assert.assertNotNull(form);
103 
104         // Build an action form - we're using it as a POJO, it ties into the
105         // existing TK validation setup
106         TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(tdoc, assignment, earnCode, start, end, null, true, null, true);
107         List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
108         // Check for errors
109         Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
110 
111         page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), tdaf);
112         Assert.assertNotNull(page);
113         HtmlUnitUtil.createTempFile(page, "TimeBlockPresent");
114 
115         // Verify block present on rendered page.
116         String pageAsText = page.asText();
117 
118         // JSON
119         //
120         //
121         // Grab the timeblock data from the text area. We can check specifics there
122         // to be more fine grained in our validation.
123         String dataText = page.getElementById("timeBlockString").getFirstChild().getNodeValue();
124         JSONArray jsonData = (JSONArray)JSONValue.parse(dataText);
125         final JSONObject jsonDataObject = (JSONObject) jsonData.get(0);
126         Assert.assertTrue("TimeBlock Data Missing.", checkJSONValues(new JSONObject() {{ put("outer", jsonDataObject); }},
127                 new ArrayList<Map<String, Object>>() {{
128                     add(new HashMap<String, Object>() {{
129                         put("earnCode", "RGN");
130                         put("hours", "8.0");
131                         put("amount", null);
132                     }});
133                 }},
134                 new HashMap<String, Object>() {{
135                     put("earnCode", "RGN");
136                     put("startNoTz", "2011-03-02T08:00:00");
137                     put("endNoTz", "2011-03-02T16:00:00");
138                     put("title", "SDR1 Work Area");
139                     put("assignment", "30_30_30");
140                 }}
141         ));
142 
143         // Check the Display Rendered Text for Time Block, Quick Check
144         Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("08:00 AM - 04:00 PM"));
145         Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("RGN - 8.00 hours"));
146 
147         //
148         // Route Timesheet
149         //
150         // Routing is initiated via javascript, we need to extract the routing
151         // action from the button element to perform this action.
152         HtmlButtonInput routeButton = (HtmlButtonInput)page.getElementById("ts-route-button");
153         String routeHref = HtmlUnitUtil.getOnClickHref(routeButton);
154         // The 'only' way to do the button click.
155         page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), getBaseURL() + "/" + routeHref);
156         //HtmlUnitUtil.createTempFile(page, "RouteClicked");
157         pageAsText = page.asText();
158         // Verify Route Status via UI
159         Assert.assertTrue("Wrong Document Loaded.", pageAsText.contains(tdocId));
160         Assert.assertTrue("Document not routed.", pageAsText.contains(DocumentStatus.ENROUTE.getLabel()));
161         routeButton = (HtmlButtonInput)page.getElementById("ts-route-button");
162         Assert.assertNull("Route button should not be present.", routeButton);
163         HtmlButtonInput approveButton = (HtmlButtonInput)page.getElementById("ts-approve-button");
164         Assert.assertNull("Approval button should not be present.", approveButton);
165 
166         //
167         // Login as Approver, who is not 'admin'
168         page = TimesheetWebTestBase.loginAndGetTimeDetailsHtmlPage(getWebClient(), "eric", tdocId, true);
169         //HtmlUnitUtil.createTempFile(page, "2ndLogin");
170         pageAsText = page.asText();
171         Assert.assertTrue("Document not routed.", pageAsText.contains(DocumentStatus.ENROUTE.getLabel()));
172         approveButton = (HtmlButtonInput)page.getElementById("ts-approve-button");
173         Assert.assertNotNull("No approval button present.", approveButton);
174 
175         // Click Approve
176         // And Verify
177         //
178         routeHref = HtmlUnitUtil.getOnClickHref(approveButton);
179         TestAutoLoginFilter.OVERRIDE_ID = "eric";
180         page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), getBaseURL() + "/" + routeHref);
181         TestAutoLoginFilter.OVERRIDE_ID = "";
182         //HtmlUnitUtil.createTempFile(page, "ApproveClicked");
183         pageAsText = page.asText();
184         Assert.assertTrue("Wrong Document Loaded.", pageAsText.contains(tdocId));
185         Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
186         Assert.assertTrue("Login info not present.", pageAsText.contains("eric, eric"));
187         Assert.assertTrue("Document not routed.", pageAsText.contains(DocumentStatus.FINAL.getLabel()));
188         approveButton = (HtmlButtonInput)page.getElementById("ts-approve-button");
189         Assert.assertNull("Approval button should not be present.", approveButton);
190     }
191 
192 }