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