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