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