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.overtime.web;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.joda.time.DateTime;
25  import org.json.simple.JSONArray;
26  import org.json.simple.JSONObject;
27  import org.json.simple.JSONValue;
28  import org.junit.Assert;
29  import org.junit.Test;
30  import org.kuali.hr.time.assignment.Assignment;
31  import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
32  import org.kuali.hr.time.calendar.CalendarEntries;
33  import org.kuali.hr.time.detail.web.TimeDetailActionFormBase;
34  import org.kuali.hr.time.earncode.EarnCode;
35  import org.kuali.hr.time.service.base.TkServiceLocator;
36  import org.kuali.hr.time.timesheet.TimesheetDocument;
37  import org.kuali.hr.time.timesheet.web.TimesheetWebTestBase;
38  import org.kuali.hr.time.util.TKContext;
39  import org.kuali.hr.time.util.TKUtils;
40  import org.kuali.hr.time.util.TimeDetailTestUtils;
41  
42  import com.gargoylesoftware.htmlunit.html.HtmlForm;
43  import com.gargoylesoftware.htmlunit.html.HtmlPage;
44  
45  public class DailyOvertimeWebIntegrationTest 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      @Test
51      /**
52       * Tests daily overtime for:
53       *
54       * 3/2/2011 10hrs; 8 RGN, 2 OVT
55       * 3/3/2011 10hrs; 8 RGN, 2 OVT
56       */
57      public void testSimpleDOTCalculationIntegration() throws Exception {
58          Date asOfDate = new Date((new DateTime(2011, 3, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
59  
60          CalendarEntries pcd = TkServiceLocator.getCalendarService().getCurrentCalendarDates(USER_PRINCIPAL_ID, asOfDate);
61          Assert.assertNotNull("No PayCalendarDates", pcd);
62  
63          TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, pcd);
64          String tdocId = tdoc.getDocumentId();
65          HtmlPage page = loginAndGetTimeDetailsHtmlPage("admin", tdocId,true);
66          Assert.assertNotNull(page);
67          HtmlForm form = page.getFormByName("TimeDetailActionForm");
68          Assert.assertNotNull(form);
69  
70          // 1. Obtain User Data
71          Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(TKContext.getPrincipalId(), new AssignmentDescriptionKey("30_30_30"), JAN_AS_OF_DATE);
72          EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode("RGN", JAN_AS_OF_DATE);
73          Assert.assertEquals("There should be no existing time blocks.", 0, tdoc.getTimeBlocks().size());
74  
75          // 2. Set Timeblock Start and End time
76          // 3/02/2011 - 8:00a to 6:00pm
77          // OVT - 2 Hrs Expected
78          DateTime start = new DateTime(2011, 3, 2, 8, 0, 0, 0, TKUtils.getSystemDateTimeZone());
79          DateTime end = new DateTime(2011, 3, 3, 18, 0, 0, 0, TKUtils.getSystemDateTimeZone());
80  
81          // Build an action form - we're using it as a POJO, it ties into the
82          // existing TK validation setup
83          TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(tdoc, assignment, earnCode, start, end, null, true, null, true);
84          List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
85          // Check for errors
86          Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
87  
88          page = TimeDetailTestUtils.submitTimeDetails(getTimesheetDocumentUrl(tdocId), tdaf);
89          Assert.assertNotNull(page);
90          //HtmlUnitUtil.createTempFile(page, "TimeBlockPresent");
91  
92          // Verify block present on rendered page.
93          String pageAsText = page.asText();
94          //HtmlUnitUtil.createTempFile(page, "Hours");
95          // JSON
96          //
97          //
98          // Grab the timeblock data from the text area. We can check specifics there
99          // to be more fine grained in our validation.
100         String dataText = page.getElementById("timeBlockString").getFirstChild().getNodeValue();
101         JSONArray jsonData = (JSONArray) JSONValue.parse(dataText);
102         final JSONObject jsonDataObject = (JSONObject) jsonData.get(0);
103         Assert.assertTrue("TimeBlock #1 Data Missing.", checkJSONValues(new JSONObject() {{ put("outer", jsonDataObject); }},
104                 new ArrayList<Map<String, Object>>() {{
105                     add(new HashMap<String, Object>() {{
106                         put("earnCode", "RGN");
107                         put("hours", "8.0");
108                     }});
109                     add(new HashMap<String, Object>() {{
110                         put("earnCode", "OVT");
111                         put("hours", "2.0");
112                     }});
113                 }},
114                 new HashMap<String, Object>() {{
115                     put("earnCode", "RGN");
116                     put("startNoTz", "2011-03-02T08:00:00");
117                     put("endNoTz", "2011-03-02T18:00:00");
118                     put("title", "SDR1 Work Area");
119                     put("assignment", "30_30_30");
120                 }}
121         ));
122         final JSONObject jsonDataObject2 = (JSONObject) jsonData.get(1);
123         Assert.assertTrue("TimeBlock #2 Data Missing.", checkJSONValues(new JSONObject() {{ put("outer", jsonDataObject2); }},
124                 new ArrayList<Map<String, Object>>() {{
125                     add(new HashMap<String, Object>() {{
126                         put("earnCode", "RGN");
127                         put("hours", "8.0");
128                     }});
129                     add(new HashMap<String, Object>() {{
130                         put("earnCode", "OVT");
131                         put("hours", "2.0");
132                     }});
133                 }},
134                 new HashMap<String, Object>() {{
135                     put("earnCode", "RGN");
136                     put("startNoTz", "2011-03-03T08:00:00");
137                     put("endNoTz", "2011-03-03T18:00:00");
138                     put("title", "SDR1 Work Area");
139                     put("assignment", "30_30_30");
140                 }}
141         ));
142 
143 
144         // Check the Display Rendered Text for Time Block, Quick Check
145         // Not as accurate as teh checkJSONValues tests above.
146         Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("08:00 AM - 06:00 PM"));
147         Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("RGN - 8.00 hours"));
148         Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("OVT - 2.00 hours"));
149     }
150 
151 }