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 com.gargoylesoftware.htmlunit.html.HtmlForm;
19 import com.gargoylesoftware.htmlunit.html.HtmlPage;
20 import org.joda.time.DateTime;
21 import org.joda.time.DateTimeZone;
22 import org.joda.time.LocalDate;
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.util.TimeDetailTestUtils;
29 import org.kuali.hr.util.HtmlUnitUtil;
30 import org.kuali.kpme.core.FunctionalTest;
31 import org.kuali.kpme.core.api.assignment.Assignment;
32 import org.kuali.kpme.core.api.assignment.AssignmentDescriptionKey;
33 import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
34 import org.kuali.kpme.core.api.earncode.EarnCode;
35 import org.kuali.kpme.core.api.earncode.EarnCodeContract;
36 import org.kuali.kpme.core.api.earncode.service.EarnCodeService;
37 import org.kuali.kpme.core.service.HrServiceLocator;
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 import org.kuali.kpme.tklm.time.timesheet.service.TimesheetService;
43
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
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<? extends EarnCodeContract> earnCodes1t = timesheetService.getEarnCodesForTime(assignment1, asOfDate);
103 Assert.assertEquals("Wrong number of earn codes returned.", 7, earnCodes1t.size());
104 List<? extends EarnCodeContract> earnCodes1l = earnCodeService.getEarnCodesForLeave(assignment1, asOfDate, false);
105 Assert.assertEquals("Wrong number of earn codes returned.", 0, earnCodes1l.size());
106
107
108 List<? extends EarnCodeContract> earnCodes2t = timesheetService.getEarnCodesForTime(assignment2, asOfDate);
109 Assert.assertEquals("Wrong number of earn codes returned.", 2, earnCodes2t.size());
110 List<? extends EarnCodeContract> earnCodes2l = earnCodeService.getEarnCodesForLeave(assignment2, asOfDate, false);
111 Assert.assertEquals("Wrong number of earn codes returned.", 0, earnCodes2l.size());
112
113
114 List<? extends EarnCodeContract> earnCodes3t = timesheetService.getEarnCodesForTime(assignment3, asOfDate);
115 Assert.assertEquals("Wrong number of earn codes returned.",1, earnCodes3t.size());
116 List<? extends EarnCodeContract> 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("IU-IN_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, null, null, null, null, null, null);
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 page = getWebClient().getPage(TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId));
184 Assert.assertNotNull(page);
185
186 String dataText = page.getElementById("timeBlockString").getFirstChild().getNodeValue();
187
188
189 JSONArray jsonData = (JSONArray) JSONValue.parse(dataText);
190 final JSONObject jsonDataObject = (JSONObject) jsonData.get(0);
191 final String assignmentKey = assignment.getAssignmentKey();
192 Assert.assertTrue("TimeBlock Data Missing.", checkJSONValues(new JSONObject() {
193 {
194 put("outer", jsonDataObject);
195 }
196 }, new ArrayList<Map<String, Object>>() {
197 {
198 add(new HashMap<String, Object>() {
199 {
200 put("earnCode", "RGN");
201 put("hours", "2.0");
202 put("amount", null);
203 }
204 });
205 }
206 }, new HashMap<String, Object>() {
207 {
208 put("earnCode", "RGN");
209 put("startNoTz", "2011-02-15T09:00:00");
210 put("endNoTz", "2011-02-15T11:00:00");
211 put("assignment", assignmentKey);
212 }
213 }));
214
215
216
217 Assert.assertTrue("TimeBlock did not created successfully.", page.asText()
218 .contains("work area description-description 1"));
219 Assert.assertTrue("TimeBlock did not created successfully.", page.asText()
220 .contains("RGN - 2.00 hours"));
221
222 }
223
224 @Test
225 public void testEditTimeBlock() throws Exception {
226 HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), USER_PRINCIPAL_ID, tdocId, true);
227
228 Assignment assignment = HrServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, AssignmentDescriptionKey.get("IU-IN_4_1234_1"), TIME_SHEET_DATE.toLocalDate());
229 EarnCode earnCode = HrServiceLocator.getEarnCodeService().getEarnCode("RGN", TIME_SHEET_DATE.toLocalDate());
230
231 DateTime startTime = new DateTime(2011, 2, 15, 9, 0, 0, 0, TKUtils.getSystemDateTimeZone());
232 DateTime endTime = new DateTime(2011, 2, 15, 11, 0, 0, 0, TKUtils.getSystemDateTimeZone());
233
234 HtmlForm form = page.getFormByName("TimeDetailActionForm");
235 Assert.assertNotNull(form);
236
237
238 TimeDetailActionFormBase addTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, null, true, null, null, null, null, null, null);
239 List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
240
241
242 Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
243
244 page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
245 Assert.assertNotNull(page);
246 page = getWebClient().getPage(TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId));
247
248 Assert.assertTrue("TimeBlock not Present.", page.asText().contains("work area description-description 1"));
249 Assert.assertTrue("TimeBlock not Present.", page.asText().contains("RGN - 2.00 hours"));
250
251
252 timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, payCal);
253
254 String createdTBId = timeDoc.getTimeBlocks().get(0).getTkTimeBlockId();
255
256 Assignment newAssignment = HrServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, AssignmentDescriptionKey.get("IU-IN_1_1234_1"), TIME_SHEET_DATE.toLocalDate());
257 HtmlUnitUtil.createTempFile(page);
258
259
260 DateTime startTime1 = new DateTime(2011, 2, 15, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
261 DateTime endTime1 = new DateTime(2011, 2, 15, 17, 0, 0, 0, TKUtils.getSystemDateTimeZone());
262
263 form = page.getFormByName("TimeDetailActionForm");
264
265 TimeDetailActionFormBase updateTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, newAssignment, earnCode, startTime1, endTime1, null, true, createdTBId, true, null, null, null, null, null, null);
266
267
268 errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, updateTB);
269 Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
270
271
272 page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), updateTB);
273 Assert.assertNotNull(page);
274 page = getWebClient().getPage(TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId));
275 Assert.assertNotNull(page);
276
277 Assert.assertTrue("TimeBlock did not updated properly.", page.asText().contains("work area description-description 1"));
278 Assert.assertTrue("TimeBlock did not updated properly.", page.asText().contains("RGN - 3.00 hours"));
279
280 }
281
282 @Test
283 public void testDeleteTimeBlock() throws Exception {
284 HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), USER_PRINCIPAL_ID,tdocId, true);
285
286 Assignment assignment = HrServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, AssignmentDescriptionKey.get("IU-IN_4_1234_1"), TIME_SHEET_DATE.toLocalDate());
287 EarnCode earnCode = HrServiceLocator.getEarnCodeService().getEarnCode("RGN", TIME_SHEET_DATE.toLocalDate());
288
289 DateTime startTime = new DateTime(2011, 2, 15, 9, 0, 0, 0, TKUtils.getSystemDateTimeZone());
290 DateTime endTime = new DateTime(2011, 2, 15, 11, 0, 0, 0, TKUtils.getSystemDateTimeZone());
291
292 HtmlForm form = page.getFormByName("TimeDetailActionForm");
293 Assert.assertNotNull(form);
294
295
296 TimeDetailActionFormBase addTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, null, true, null, null, null, null, null, null);
297 List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
298
299
300 Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
301
302 page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
303 Assert.assertNotNull(page);
304 page = getWebClient().getPage(TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId));
305
306 Assert.assertTrue("TimeBlock did not created successfully.", page.asText().contains("work area description-description 1"));
307 Assert.assertTrue("TimeBlock did not created successfully.", page.asText().contains("RGN - 2.00 hours"));
308
309 timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, payCal);
310
311
312 String createTBId = timeDoc.getTimeBlocks().get(0).getTkTimeBlockId();
313 HtmlUnitUtil.createTempFile(page);
314 form = page.getFormByName("TimeDetailActionForm");
315 Assert.assertNotNull(form);
316
317
318 TimeDetailActionFormBase deleteTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, createTBId, true, null, null, null, null, null, null);
319 deleteTB.setMethodToCall("deleteTimeBlock");
320
321
322 page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), deleteTB);
323 Assert.assertNotNull(page);
324 page = getWebClient().getPage(TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId));
325 Assert.assertNotNull(page);
326
327 Assert.assertTrue("TimeBlock did not deleted successfully.", !page.asText().contains("work area description-description 1"));
328 Assert.assertTrue("TimeBlock did not deleted successfully.", !page.asText().contains("RGN - 2.00 hours"));
329 }
330
331
332 @Test
333 public void testValidateTimeBlock() throws Exception {
334
335 EarnCode earnCode = null;
336 HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), USER_PRINCIPAL_ID,
337 tdocId, true);
338
339 HtmlForm form = page.getFormByName("TimeDetailActionForm");
340 Assert.assertNotNull(form);
341
342
343 Assignment assToBeSelected = assignmentsOfUser.get(4);
344
345
346 List<EarnCode> earnCodes = TkServiceLocator.getTimesheetService().getEarnCodesForTime(assToBeSelected, TIME_SHEET_DATE.toLocalDate());
347 if (earnCodes != null && !earnCodes.isEmpty()) {
348 earnCode = earnCodes.get(0);
349 }
350
351 DateTime startTime = new DateTime(2011, 2, 17, 9, 0, 0, 0,
352 TKUtils.getSystemDateTimeZone()
353 );
354 DateTime endTime = new DateTime(2011, 2, 20, 11, 0, 0, 0,
355 TKUtils.getSystemDateTimeZone()
356 );
357
358
359 TimeDetailActionFormBase addTB = TimeDetailTestUtils
360 .buildDetailActionForm(timeDoc, assToBeSelected, earnCode, startTime,
361 endTime, null, true, null, true, null, null, null, null, null, null);
362 List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form,
363 addTB);
364
365 Assert.assertEquals(
366 "There should be one error in this time detail submission", 1,
367 errors.size());
368
369 Assert.assertTrue("Error is not related to number of hours in a day.", errors.contains("Hours cannot exceed 24."));
370
371 startTime = new DateTime(2011, 2, 19, 9, 0, 0, 0,
372 TKUtils.getSystemDateTimeZone());
373 endTime = new DateTime(2011, 2, 20, 11, 0, 0, 0,
374 TKUtils.getSystemDateTimeZone());
375
376
377 addTB = TimeDetailTestUtils
378 .buildDetailActionForm(timeDoc, assToBeSelected, earnCode, startTime,
379 endTime, null, true, null, false, null, null, null, null, null, null);
380 errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
381
382 Assert.assertEquals(
383 "There should two errors in this time detail submission", 1,
384 errors.size());
385
386
387 startTime = new DateTime(2011, 2, 17, 9, 0, 0, 0,
388 TKUtils.getSystemDateTimeZone());
389 endTime = new DateTime(2011, 2, 18, 11, 0, 0, 0,
390 TKUtils.getSystemDateTimeZone());
391
392
393 addTB = TimeDetailTestUtils
394 .buildDetailActionForm(timeDoc, assToBeSelected, earnCode, startTime,
395 endTime, null, true, null, false, null, null, null, null, null, null);
396 errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
397
398
399 Assert.assertEquals(
400 "There should be no error in this time detail submission", 1,
401 errors.size());
402 }
403
404 }