1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.workflow;
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.joda.time.DateTimeZone;
25 import org.joda.time.LocalDate;
26 import org.json.simple.JSONArray;
27 import org.json.simple.JSONObject;
28 import org.json.simple.JSONValue;
29 import org.junit.Assert;
30 import org.junit.Test;
31 import org.kuali.hr.time.util.TimeDetailTestUtils;
32 import org.kuali.hr.util.HtmlUnitUtil;
33 import org.kuali.kpme.core.FunctionalTest;
34 import org.kuali.kpme.core.assignment.Assignment;
35 import org.kuali.kpme.core.assignment.AssignmentDescriptionKey;
36 import org.kuali.kpme.core.calendar.entry.CalendarEntry;
37 import org.kuali.kpme.core.earncode.EarnCode;
38 import org.kuali.kpme.core.earncode.service.EarnCodeService;
39 import org.kuali.kpme.core.service.HrServiceLocator;
40 import org.kuali.kpme.core.util.TKUtils;
41 import org.kuali.kpme.tklm.time.detail.web.TimeDetailActionFormBase;
42 import org.kuali.kpme.tklm.time.service.TkServiceLocator;
43 import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
44 import org.kuali.kpme.tklm.time.timesheet.service.TimesheetService;
45
46 import com.gargoylesoftware.htmlunit.html.HtmlForm;
47 import com.gargoylesoftware.htmlunit.html.HtmlPage;
48
49
50 @FunctionalTest
51 public class TimesheetIntegrationTest extends TimesheetWebTestBase {
52
53 public static final String USER_PRINCIPAL_ID = "admin";
54 public static final DateTime TIME_SHEET_DATE = new DateTime(2011, 2, 15, 0, 0, 0, 0, DateTimeZone
55 .forID(TKUtils.getSystemTimeZone()));
56 public TimesheetDocument timeDoc;
57 public List<Assignment> assignmentsOfUser;
58 public CalendarEntry payCal;
59 public String tdocId;
60
61 public static final String TEST_USER = "admin";
62 public static final Long TEST_ASSIGNMENT_JOB_NUMBER = 1L;
63 public static final Long TEST_ASSIGNMENT_JOB_NUMBER_2 = 2L;
64 public static final Long TEST_ASSIGNMENT_JOB_NUMBER_3 = 3L;
65 public static final Long TEST_ASSIGNMENT_JOB_NUMBER_4 = 4L;
66
67 EarnCodeService earnCodeService = null;
68 TimesheetService timesheetService = null;
69
70 @Test
71 public void getEarnCodes() throws Exception {
72 LocalDate asOfDate = LocalDate.now();
73 List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(TEST_USER, asOfDate);
74 Assert.assertNotNull(assignments);
75 Assert.assertTrue("Emtpy assignment list", !assignments.isEmpty());
76
77 Assignment assignment1 = null;
78 Assignment assignment2 = null;
79 Assignment assignment3 = null;
80 Assignment assignment4 = null;
81 for (Assignment a : assignments) {
82 if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER)) {
83 assignment1 = a;
84 } else if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER_2)) {
85 assignment2 = a;
86 } else if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER_3)) {
87 assignment3 = a;
88 } else if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER_4)) {
89 assignment4 = a;
90 }
91 }
92
93
94 Assert.assertNotNull("Test assignment not found.", assignment1);
95 Assert.assertNotNull("Test assignment not found.", assignment2);
96 Assert.assertNotNull("Test assignment not found.", assignment3);
97 Assert.assertNotNull("Test assignment not found.", assignment4);
98
99
100
101
102 List<EarnCode> earnCodes1t = timesheetService.getEarnCodesForTime(assignment1, asOfDate);
103 Assert.assertEquals("Wrong number of earn codes returned.", 7, earnCodes1t.size());
104 List<EarnCode> earnCodes1l = earnCodeService.getEarnCodesForLeave(assignment1, asOfDate, false);
105 Assert.assertEquals("Wrong number of earn codes returned.", 0, earnCodes1l.size());
106
107
108 List<EarnCode> earnCodes2t = timesheetService.getEarnCodesForTime(assignment2, asOfDate);
109 Assert.assertEquals("Wrong number of earn codes returned.", 2, earnCodes2t.size());
110 List<EarnCode> earnCodes2l = earnCodeService.getEarnCodesForLeave(assignment2, asOfDate, false);
111 Assert.assertEquals("Wrong number of earn codes returned.", 0, earnCodes2l.size());
112
113
114 List<EarnCode> earnCodes3t = timesheetService.getEarnCodesForTime(assignment3, asOfDate);
115 Assert.assertEquals("Wrong number of earn codes returned.",1, earnCodes3t.size());
116 List<EarnCode> earnCodes3l = earnCodeService.getEarnCodesForLeave(assignment3, asOfDate, false);
117 Assert.assertEquals("Wrong number of earn codes returned.",0, earnCodes3l.size());
118 }
119
120
121
122
123 public void setUp() throws Exception {
124
125 super.setUp();
126
127 payCal = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates(
128 USER_PRINCIPAL_ID, TIME_SHEET_DATE);
129 Assert.assertNotNull("Pay calendar entries not found for admin", payCal);
130
131
132 timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(
133 USER_PRINCIPAL_ID, payCal);
134 tdocId = timeDoc.getDocumentId();
135
136
137 HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), USER_PRINCIPAL_ID,
138 tdocId, true);
139 Assert.assertNotNull(page);
140
141 assignmentsOfUser = HrServiceLocator.getAssignmentService()
142 .getAssignments(USER_PRINCIPAL_ID, TIME_SHEET_DATE.toLocalDate());
143 Assert.assertNotNull("No Assignments found for the user ", assignmentsOfUser);
144
145
146 Assert.assertTrue("Page could not find calendar for Feb 2011", page.asText()
147 .contains("February 2011"));
148
149 HtmlForm form = page.getFormByName("TimeDetailActionForm");
150 Assert.assertNotNull(form);
151
152 timesheetService = TkServiceLocator.getTimesheetService();
153 earnCodeService = HrServiceLocator.getEarnCodeService();
154 }
155
156 public void tearDown() throws Exception {
157 super.tearDown();
158 }
159
160 @Test
161 public void testAddTimeBlock() throws Exception {
162 HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), USER_PRINCIPAL_ID, tdocId, true);
163
164 Assignment assignment = HrServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, AssignmentDescriptionKey.get("4_1234_1"), TIME_SHEET_DATE.toLocalDate());
165 HtmlForm form = page.getFormByName("TimeDetailActionForm");
166 Assert.assertNotNull(form);
167
168 EarnCode earnCode = HrServiceLocator.getEarnCodeService().getEarnCode("RGN", TIME_SHEET_DATE.toLocalDate());
169
170 DateTime startTime = new DateTime(2011, 2, 15, 9, 0, 0, 0, TKUtils.getSystemDateTimeZone());
171 DateTime endTime = new DateTime(2011, 2, 15, 11, 0, 0, 0, TKUtils.getSystemDateTimeZone());
172
173
174 TimeDetailActionFormBase addTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, null, true);
175 List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
176
177
178 Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
179
180
181 page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
182 Assert.assertNotNull(page);
183
184
185 String dataText = page.getElementById("timeBlockString").getFirstChild().getNodeValue();
186
187
188 JSONArray jsonData = (JSONArray) JSONValue.parse(dataText);
189 final JSONObject jsonDataObject = (JSONObject) jsonData.get(0);
190 final String assignmentKey = assignment.getAssignmentKey();
191 Assert.assertTrue("TimeBlock Data Missing.", checkJSONValues(new JSONObject() {
192 {
193 put("outer", jsonDataObject);
194 }
195 }, new ArrayList<Map<String, Object>>() {
196 {
197 add(new HashMap<String, Object>() {
198 {
199 put("earnCode", "RGN");
200 put("hours", "2.0");
201 put("amount", null);
202 }
203 });
204 }
205 }, new HashMap<String, Object>() {
206 {
207 put("earnCode", "RGN");
208 put("startNoTz", "2011-02-15T09:00:00");
209 put("endNoTz", "2011-02-15T11:00:00");
210 put("assignment", assignmentKey);
211 }
212 }));
213
214
215
216 Assert.assertTrue("TimeBlock did not created successfully.", page.asText()
217 .contains("work area description-description 1"));
218 Assert.assertTrue("TimeBlock did not created successfully.", page.asText()
219 .contains("RGN - 2.00 hours"));
220
221 }
222
223 @Test
224 public void testEditTimeBlock() throws Exception {
225 HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), USER_PRINCIPAL_ID, tdocId, true);
226
227 Assignment assignment = HrServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, AssignmentDescriptionKey.get("4_1234_1"), TIME_SHEET_DATE.toLocalDate());
228 EarnCode earnCode = HrServiceLocator.getEarnCodeService().getEarnCode("RGN", TIME_SHEET_DATE.toLocalDate());
229
230 DateTime startTime = new DateTime(2011, 2, 15, 9, 0, 0, 0, TKUtils.getSystemDateTimeZone());
231 DateTime endTime = new DateTime(2011, 2, 15, 11, 0, 0, 0, TKUtils.getSystemDateTimeZone());
232
233 HtmlForm form = page.getFormByName("TimeDetailActionForm");
234 Assert.assertNotNull(form);
235
236
237 TimeDetailActionFormBase addTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, null, true);
238 List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
239
240
241 Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
242
243 page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
244 Assert.assertNotNull(page);
245
246
247 Assert.assertTrue("TimeBlock not Present.", page.asText().contains("work area description-description 1"));
248 Assert.assertTrue("TimeBlock not Present.", page.asText().contains("RGN - 2.00 hours"));
249
250
251 timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, payCal);
252
253 String createdTBId = timeDoc.getTimeBlocks().get(0).getTkTimeBlockId();
254
255 Assignment newAssignment = HrServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, AssignmentDescriptionKey.get("1_1234_1"), TIME_SHEET_DATE.toLocalDate());
256 HtmlUnitUtil.createTempFile(page);
257
258
259 DateTime startTime1 = new DateTime(2011, 2, 15, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
260 DateTime endTime1 = new DateTime(2011, 2, 15, 17, 0, 0, 0, TKUtils.getSystemDateTimeZone());
261
262 form = page.getFormByName("TimeDetailActionForm");
263
264 TimeDetailActionFormBase updateTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, newAssignment, earnCode, startTime1, endTime1, null, true, createdTBId, true);
265
266
267 errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, updateTB);
268 Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
269
270
271 page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), updateTB);
272 Assert.assertNotNull(page);
273
274
275 Assert.assertTrue("TimeBlock did not updated properly.", page.asText().contains("work area description-description 1"));
276 Assert.assertTrue("TimeBlock did not updated properly.", page.asText().contains("RGN - 3.00 hours"));
277
278 }
279
280 @Test
281 public void testDeleteTimeBlock() throws Exception {
282 HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), USER_PRINCIPAL_ID,tdocId, true);
283
284 Assignment assignment = HrServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, AssignmentDescriptionKey.get("4_1234_1"), TIME_SHEET_DATE.toLocalDate());
285 EarnCode earnCode = HrServiceLocator.getEarnCodeService().getEarnCode("RGN", TIME_SHEET_DATE.toLocalDate());
286
287 DateTime startTime = new DateTime(2011, 2, 15, 9, 0, 0, 0, TKUtils.getSystemDateTimeZone());
288 DateTime endTime = new DateTime(2011, 2, 15, 11, 0, 0, 0, TKUtils.getSystemDateTimeZone());
289
290 HtmlForm form = page.getFormByName("TimeDetailActionForm");
291 Assert.assertNotNull(form);
292
293
294 TimeDetailActionFormBase addTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, null, true);
295 List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
296
297
298 Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
299
300 page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
301 Assert.assertNotNull(page);
302
303
304 Assert.assertTrue("TimeBlock did not created successfully.", page.asText().contains("work area description-description 1"));
305 Assert.assertTrue("TimeBlock did not created successfully.", page.asText().contains("RGN - 2.00 hours"));
306
307 timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, payCal);
308
309
310 String createTBId = timeDoc.getTimeBlocks().get(0).getTkTimeBlockId();
311 HtmlUnitUtil.createTempFile(page);
312 form = page.getFormByName("TimeDetailActionForm");
313 Assert.assertNotNull(form);
314
315
316 TimeDetailActionFormBase deleteTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, createTBId, true);
317 deleteTB.setMethodToCall("deleteTimeBlock");
318
319
320 page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), deleteTB);
321 Assert.assertNotNull(page);
322
323
324 Assert.assertTrue("TimeBlock did not deleted successfully.", !page.asText().contains("work area description-description 1"));
325 Assert.assertTrue("TimeBlock did not deleted successfully.", !page.asText().contains("RGN - 2.00 hours"));
326 }
327
328
329 @Test
330 public void testValidateTimeBlock() throws Exception {
331
332 EarnCode earnCode = null;
333 HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), USER_PRINCIPAL_ID,
334 tdocId, true);
335
336 HtmlForm form = page.getFormByName("TimeDetailActionForm");
337 Assert.assertNotNull(form);
338
339
340 Assignment assToBeSelected = assignmentsOfUser.get(4);
341
342
343 List<EarnCode> earnCodes = TkServiceLocator.getTimesheetService().getEarnCodesForTime(assToBeSelected, TIME_SHEET_DATE.toLocalDate());
344 if (earnCodes != null && !earnCodes.isEmpty()) {
345 earnCode = earnCodes.get(0);
346 }
347
348 DateTime startTime = new DateTime(2011, 2, 17, 9, 0, 0, 0,
349 TKUtils.getSystemDateTimeZone());
350 DateTime endTime = new DateTime(2011, 2, 20, 11, 0, 0, 0,
351 TKUtils.getSystemDateTimeZone());
352
353
354 TimeDetailActionFormBase addTB = TimeDetailTestUtils
355 .buildDetailActionForm(timeDoc, assToBeSelected, earnCode, startTime,
356 endTime, null, true, null, true);
357 List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form,
358 addTB);
359
360 Assert.assertEquals(
361 "There should be one error in this time detail submission", 1,
362 errors.size());
363
364 Assert.assertTrue("Error is not related to number of hours in a day.", errors.contains("Hours cannot exceed 24."));
365
366 startTime = new DateTime(2011, 2, 19, 9, 0, 0, 0,
367 TKUtils.getSystemDateTimeZone());
368 endTime = new DateTime(2011, 2, 20, 11, 0, 0, 0,
369 TKUtils.getSystemDateTimeZone());
370
371
372 addTB = TimeDetailTestUtils
373 .buildDetailActionForm(timeDoc, assToBeSelected, earnCode, startTime,
374 endTime, null, true, null, false);
375 errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
376
377 Assert.assertEquals(
378 "There should two errors in this time detail submission", 1,
379 errors.size());
380
381 Assert.assertTrue("Errors does not contain spanning weeks error", errors.contains("Weekend day is selected, but include weekends checkbox is not checked"));
382 startTime = new DateTime(2011, 2, 17, 9, 0, 0, 0,
383 TKUtils.getSystemDateTimeZone());
384 endTime = new DateTime(2011, 2, 18, 11, 0, 0, 0,
385 TKUtils.getSystemDateTimeZone());
386
387
388 addTB = TimeDetailTestUtils
389 .buildDetailActionForm(timeDoc, assToBeSelected, earnCode, startTime,
390 endTime, null, true, null, false);
391 errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
392
393
394 Assert.assertEquals(
395 "There should be no error in this time detail submission", 1,
396 errors.size());
397 }
398
399 }