View Javadoc

1   /**
2    * Copyright 2004-2012 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.math.BigDecimal;
19  import java.sql.Date;
20  import java.util.*;
21  
22  import org.joda.time.DateTime;
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.calendar.CalendarEntries;
29  import org.kuali.hr.time.detail.web.TimeDetailActionFormBase;
30  import org.kuali.hr.time.earncode.EarnCode;
31  import org.kuali.hr.time.overtime.daily.rule.DailyOvertimeRule;
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.timesheet.TimesheetDocument;
36  import org.kuali.hr.time.timesheet.web.TimesheetWebTestBase;
37  import org.kuali.hr.time.util.*;
38  
39  import com.gargoylesoftware.htmlunit.html.HtmlForm;
40  import com.gargoylesoftware.htmlunit.html.HtmlPage;
41  
42  /**
43   * https://wiki.kuali.org/display/KPME/Test+Cases+-+Business+Logic+Daily+Overtime
44   *
45   * Implementations of tests from this page.
46   */
47  public class DOTIntegrationConfluenceTests extends TimesheetWebTestBase {
48  
49      @Test
50      /**
51       * Daily OT - Shift hours met (in a single day) but Max Gap minutes exceeded (in a single gap)
52       * Daily OT - Shift hours met (in a single day) but Max Gap matches exactly
53       *
54       * https://jira.kuali.org/browse/KPME-788
55       * https://jira.kuali.org/browse/KPME-789
56       *
57       */
58      public void testKPME788_789() throws Exception {
59          Long jobNumber = 30L;
60          Long workArea = 30L;
61          Long task = 30L;
62          DailyOvertimeRule rule = createDailyOvertimeRule("REG", "OVT", "SD1", "BW", "TEST-DEPT", workArea, task, new BigDecimal(8), new BigDecimal("9.00"), null);
63          String tdocId = KPME788_789(
64                 new ArrayList<Map<String, Object>>() {{
65                      add(new HashMap<String, Object>() {{
66                          put("earnCode", "RGN");
67                          put("hours", "5.0");
68                      }});
69                  }},
70                  new HashMap<String, Object>() {{
71                      put("earnCode", "RGN");
72                      put("startNoTz", "2011-03-02T08:00:00");
73                      put("endNoTz", "2011-03-02T13:00:00");
74                      put("title", "SDR1 Work Area");
75                      put("assignment", "30_30_30");
76                  }},
77                  new ArrayList<Map<String, Object>>() {{
78                      add(new HashMap<String, Object>() {{
79                          put("earnCode", "RGN");
80                          put("hours", "5.0");
81                      }});
82                  }},
83                  new HashMap<String, Object>() {{
84                      put("earnCode", "RGN");
85                      put("startNoTz", "2011-03-02T13:10:00");
86                      put("endNoTz", "2011-03-02T18:10:00");
87                      put("title", "SDR1 Work Area");
88                      put("assignment", "30_30_30");
89                  }}
90          );
91          deleteTimesheet(tdocId);
92  
93          createDailyOvertimeRule("REG", "OVT", "SD1", "BW", "TEST-DEPT", workArea, task, new BigDecimal(8), new BigDecimal("10.00"), null);
94          tdocId = KPME788_789(
95                  new ArrayList<Map<String, Object>>() {{
96                       add(new HashMap<String, Object>() {{
97                           put("earnCode", "RGN");
98                           put("hours", "5.0");
99                       }});
100                  }},
101                  new HashMap<String, Object>() {{
102                      put("earnCode", "RGN");
103                      put("startNoTz", "2011-03-02T08:00:00");
104                      put("endNoTz", "2011-03-02T13:00:00");
105                      put("title", "SDR1 Work Area");
106                      put("assignment", "30_30_30");
107                  }},
108                  new ArrayList<Map<String, Object>>() {{
109                      add(new HashMap<String, Object>() {{
110                          put("earnCode", "RGN");
111                          put("hours", "3.0");
112                          put("earnCode", "OVT");
113                          put("hours", "2.0");
114                      }});
115                  }},
116                  new HashMap<String, Object>() {{
117                      put("earnCode", "RGN");
118                      put("startNoTz", "2011-03-02T13:10:00");
119                      put("endNoTz", "2011-03-02T18:10:00");
120                      put("title", "SDR1 Work Area");
121                      put("assignment", "30_30_30");
122                  }}
123         );
124         deleteTimesheet(tdocId);
125     }
126 
127 
128     public void deleteTimesheet(String tdocId) throws Exception {
129         HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(TkTestConstants.Urls.PORTAL_URL);
130         Assert.assertNotNull(page);
131         page = HtmlUnitUtil.gotoPageAndLogin(TkTestConstants.Urls.DELETE_TIMESHEET_URL + "?deleteDocumentId="+tdocId);
132         HtmlUnitUtil.createTempFile(page, "Deleted");
133     }
134 
135     public String KPME788_789(ArrayList<Map<String, Object>> tb1ThdItems, HashMap<String, Object> tb1Items, ArrayList<Map<String, Object>> tb2ThdItems, HashMap<String, Object> tb2Items) throws Exception {
136         Date asOfDate = new Date((new DateTime(2011, 3, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
137         CalendarEntries pcd = TkServiceLocator.getCalendarService().getCurrentCalendarDates(USER_PRINCIPAL_ID, asOfDate);
138         Assert.assertNotNull("No PayCalendarDates", pcd);
139 
140         TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, pcd);
141         String tdocId = tdoc.getDocumentId();
142         HtmlPage page = loginAndGetTimeDetailsHtmlPage("admin", tdocId, true);
143         Assert.assertNotNull(page);
144         HtmlForm form = page.getFormByName("TimeDetailActionForm");
145         Assert.assertNotNull(form);
146 
147         List<Assignment> assignments = TkServiceLocator.getAssignmentService().getAssignments(TKContext.getPrincipalId(), JAN_AS_OF_DATE);
148         Assignment assignment = assignments.get(0);
149 
150         List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodes(assignment, JAN_AS_OF_DATE);
151         EarnCode earnCode = earnCodes.get(0);
152         Assert.assertEquals("There should be no existing time blocks.", 0, tdoc.getTimeBlocks().size());
153 
154         // 2. Set Timeblock Start and End time
155         // 3/02/2011 - 8:00a to 6:00pm
156         // OVT - 0 Hrs Expected
157         DateTime start = new DateTime(2011, 3, 2, 8, 0, 0, 0, TKUtils.getSystemDateTimeZone());
158         DateTime end = new DateTime(2011, 3, 2, 13, 0, 0, 0, TKUtils.getSystemDateTimeZone());
159         TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(tdoc, assignment, earnCode, start, end, null, false, null, true);
160         List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
161         Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
162         page = TimeDetailTestUtils.submitTimeDetails(getTimesheetDocumentUrl(tdocId), tdaf);
163         Assert.assertNotNull(page);
164 
165         start = new DateTime(2011, 3, 2, 13, 10, 0, 0, TKUtils.getSystemDateTimeZone());
166         end = new DateTime(2011, 3, 2, 18, 10, 0, 0, TKUtils.getSystemDateTimeZone());
167         tdaf = TimeDetailTestUtils.buildDetailActionForm(tdoc, assignment, earnCode, start, end, null, false, null, true);
168         errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
169         Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
170         page = TimeDetailTestUtils.submitTimeDetails(getTimesheetDocumentUrl(tdocId), tdaf);
171         HtmlUnitUtil.createTempFile(page, "Hours");
172         Assert.assertNotNull(page);
173 
174         // Verify block present on rendered page.
175         String pageAsText = page.asText();
176         // Grab the timeblock data from the text area. We can check specifics there
177         // to be more fine grained in our validation.
178         String dataText = page.getElementById("timeBlockString").getFirstChild().getNodeValue();
179         JSONObject jsonData = (JSONObject) JSONValue.parse(dataText);
180         Assert.assertTrue("TimeBlock #1 Data Missing.", checkJSONValues(jsonData, tb1ThdItems, tb1Items));
181         Assert.assertTrue("TimeBlock #2 Data Missing.", checkJSONValues(jsonData, tb2ThdItems, tb2Items));
182 
183         return tdocId;
184     }
185 
186     private DailyOvertimeRule createDailyOvertimeRule(String fromEarnGroup, String earnCode, String location, String paytype, String dept, Long workArea, Long task, BigDecimal minHours, BigDecimal maxGap, String overtimePref) {
187         DailyOvertimeRule rule = new DailyOvertimeRule();
188         rule.setEffectiveDate(JAN_AS_OF_DATE);
189         rule.setFromEarnGroup(fromEarnGroup);
190         rule.setEarnCode(earnCode);
191         rule.setLocation(location);
192         rule.setPaytype(paytype);
193         rule.setDept(dept);
194         rule.setWorkArea(workArea);
195         rule.setMaxGap(maxGap);
196         rule.setMinHours(minHours);
197         rule.setActive(true);
198         TkServiceLocator.getDailyOvertimeRuleService().saveOrUpdate(rule);
199         return rule;
200     }
201 }