1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.timesheet.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.joda.time.DateTimeZone;
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.assignment.Assignment;
32 import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
33 import org.kuali.hr.time.calendar.CalendarEntries;
34 import org.kuali.hr.time.detail.web.TimeDetailActionFormBase;
35 import org.kuali.hr.time.earncode.EarnCode;
36 import org.kuali.hr.time.service.base.TkServiceLocator;
37 import org.kuali.hr.time.test.HtmlUnitUtil;
38 import org.kuali.hr.time.timesheet.TimesheetDocument;
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
46 public class TimesheetIntegrationTest extends TimesheetWebTestBase {
47
48 public static final String USER_PRINCIPAL_ID = "admin";
49 public static final Date TIME_SHEET_DATE = new Date(
50 (new DateTime(2011, 2, 15, 0, 0, 0, 0, DateTimeZone
51 .forID(TKUtils.getSystemTimeZone()))).getMillis());
52 public TimesheetDocument timeDoc;
53 public List<Assignment> assignmentsOfUser;
54 public CalendarEntries payCal;
55 public String tdocId;
56
57
58
59
60 public void setUp() throws Exception {
61
62 super.setUp();
63
64 payCal = TkServiceLocator.getCalendarService().getCurrentCalendarDates(
65 USER_PRINCIPAL_ID, TIME_SHEET_DATE);
66 Assert.assertNotNull("Pay calendar entries not found for admin", payCal);
67
68
69 timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(
70 USER_PRINCIPAL_ID, payCal);
71 tdocId = timeDoc.getDocumentId();
72
73
74 HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), USER_PRINCIPAL_ID,
75 tdocId, true);
76 Assert.assertNotNull(page);
77
78 assignmentsOfUser = TkServiceLocator.getAssignmentService()
79 .getAssignments(USER_PRINCIPAL_ID, TIME_SHEET_DATE);
80 Assert.assertNotNull("No Assignments found for the user ", assignmentsOfUser);
81
82
83 Assert.assertTrue("Page could not find calendar for Feb 2011", page.asText()
84 .contains("February 2011"));
85
86 HtmlForm form = page.getFormByName("TimeDetailActionForm");
87 Assert.assertNotNull(form);
88 }
89
90 public void tearDown() throws Exception {
91 super.tearDown();
92 }
93
94 @Test
95 public void testAddTimeBlock() throws Exception {
96 HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), USER_PRINCIPAL_ID, tdocId, true);
97
98 HtmlForm form = page.getFormByName("TimeDetailActionForm");
99 Assert.assertNotNull(form);
100
101 Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, new AssignmentDescriptionKey("4_1234_1"), TIME_SHEET_DATE);
102 EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode("RGN", TIME_SHEET_DATE);
103
104 DateTime startTime = new DateTime(2011, 2, 15, 9, 0, 0, 0, TKUtils.getSystemDateTimeZone());
105 DateTime endTime = new DateTime(2011, 2, 15, 11, 0, 0, 0, TKUtils.getSystemDateTimeZone());
106
107
108 TimeDetailActionFormBase addTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, null, true);
109 List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
110
111
112 Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
113
114
115 page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
116 Assert.assertNotNull(page);
117
118
119 String dataText = page.getElementById("timeBlockString").getFirstChild().getNodeValue();
120
121
122 JSONArray jsonData = (JSONArray) JSONValue.parse(dataText);
123 final JSONObject jsonDataObject = (JSONObject) jsonData.get(0);
124 final String assignmentKey = assignment.getAssignmentKey();
125 Assert.assertTrue("TimeBlock Data Missing.", checkJSONValues(new JSONObject() {
126 {
127 put("outer", jsonDataObject);
128 }
129 }, new ArrayList<Map<String, Object>>() {
130 {
131 add(new HashMap<String, Object>() {
132 {
133 put("earnCode", "RGN");
134 put("hours", "2.0");
135 put("amount", null);
136 }
137 });
138 }
139 }, new HashMap<String, Object>() {
140 {
141 put("earnCode", "RGN");
142 put("startNoTz", "2011-02-15T09:00:00");
143 put("endNoTz", "2011-02-15T11:00:00");
144 put("assignment", assignmentKey);
145 }
146 }));
147
148
149
150 Assert.assertTrue("TimeBlock did not created successfully.", page.asText()
151 .contains("work area description-description 1"));
152 Assert.assertTrue("TimeBlock did not created successfully.", page.asText()
153 .contains("RGN - 2.00 hours"));
154
155 }
156
157 @Test
158 public void testEditTimeBlock() throws Exception {
159 HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), USER_PRINCIPAL_ID, tdocId, true);
160
161 Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, new AssignmentDescriptionKey("4_1234_1"), TIME_SHEET_DATE);
162 EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode("RGN", TIME_SHEET_DATE);
163
164 DateTime startTime = new DateTime(2011, 2, 15, 9, 0, 0, 0, TKUtils.getSystemDateTimeZone());
165 DateTime endTime = new DateTime(2011, 2, 15, 11, 0, 0, 0, TKUtils.getSystemDateTimeZone());
166
167 HtmlForm form = page.getFormByName("TimeDetailActionForm");
168 Assert.assertNotNull(form);
169
170
171 TimeDetailActionFormBase addTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, null, true);
172 List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
173
174
175 Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
176
177 page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
178 Assert.assertNotNull(page);
179
180
181 Assert.assertTrue("TimeBlock not Present.", page.asText().contains("work area description-description 1"));
182 Assert.assertTrue("TimeBlock not Present.", page.asText().contains("RGN - 2.00 hours"));
183
184
185 timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, payCal);
186
187 String createdTBId = timeDoc.getTimeBlocks().get(0).getTkTimeBlockId();
188
189 HtmlUnitUtil.createTempFile(page);
190
191 Assignment newAssignment = TkServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, new AssignmentDescriptionKey("1_1234_1"), TIME_SHEET_DATE);
192
193 DateTime startTime1 = new DateTime(2011, 2, 15, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
194 DateTime endTime1 = new DateTime(2011, 2, 15, 17, 0, 0, 0, TKUtils.getSystemDateTimeZone());
195
196 form = page.getFormByName("TimeDetailActionForm");
197
198 TimeDetailActionFormBase updateTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, newAssignment, earnCode, startTime1, endTime1, null, true, createdTBId, true);
199
200
201 errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, updateTB);
202 Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
203
204
205 page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), updateTB);
206 Assert.assertNotNull(page);
207
208
209 Assert.assertTrue("TimeBlock did not updated properly.", page.asText().contains("work area description-description 1"));
210 Assert.assertTrue("TimeBlock did not updated properly.", page.asText().contains("RGN - 3.00 hours"));
211
212 }
213
214 @Test
215 public void testDeleteTimeBlock() throws Exception {
216 HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), USER_PRINCIPAL_ID,tdocId, true);
217
218 Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, new AssignmentDescriptionKey("4_1234_1"), TIME_SHEET_DATE);
219 EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode("RGN", TIME_SHEET_DATE);
220
221 DateTime startTime = new DateTime(2011, 2, 15, 9, 0, 0, 0, TKUtils.getSystemDateTimeZone());
222 DateTime endTime = new DateTime(2011, 2, 15, 11, 0, 0, 0, TKUtils.getSystemDateTimeZone());
223
224 HtmlForm form = page.getFormByName("TimeDetailActionForm");
225 Assert.assertNotNull(form);
226
227
228 TimeDetailActionFormBase addTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, null, true);
229 List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
230
231
232 Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
233
234 page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
235 Assert.assertNotNull(page);
236
237
238 Assert.assertTrue("TimeBlock did not created successfully.", page.asText().contains("work area description-description 1"));
239 Assert.assertTrue("TimeBlock did not created successfully.", page.asText().contains("RGN - 2.00 hours"));
240
241 timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, payCal);
242
243
244 String createTBId = timeDoc.getTimeBlocks().get(0).getTkTimeBlockId();
245 HtmlUnitUtil.createTempFile(page);
246 form = page.getFormByName("TimeDetailActionForm");
247 Assert.assertNotNull(form);
248
249
250 TimeDetailActionFormBase deleteTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, createTBId, true);
251 deleteTB.setMethodToCall("deleteTimeBlock");
252
253
254 page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), deleteTB);
255 Assert.assertNotNull(page);
256
257
258 Assert.assertTrue("TimeBlock did not deleted successfully.", !page.asText().contains("work area description-description 1"));
259 Assert.assertTrue("TimeBlock did not deleted successfully.", !page.asText().contains("RGN - 2.00 hours"));
260 }
261
262
263 @Test
264 public void testValidateTimeBlock() throws Exception {
265
266 EarnCode earnCode = null;
267 HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), USER_PRINCIPAL_ID,
268 tdocId, true);
269
270 HtmlForm form = page.getFormByName("TimeDetailActionForm");
271 Assert.assertNotNull(form);
272
273
274 Assignment assToBeSelected = assignmentsOfUser.get(4);
275
276
277 List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodesForTime(assToBeSelected, TIME_SHEET_DATE);
278 if (earnCodes != null && !earnCodes.isEmpty()) {
279 earnCode = earnCodes.get(0);
280 }
281
282 DateTime startTime = new DateTime(2011, 2, 17, 9, 0, 0, 0,
283 TKUtils.getSystemDateTimeZone());
284 DateTime endTime = new DateTime(2011, 2, 20, 11, 0, 0, 0,
285 TKUtils.getSystemDateTimeZone());
286
287
288 TimeDetailActionFormBase addTB = TimeDetailTestUtils
289 .buildDetailActionForm(timeDoc, assToBeSelected, earnCode,
290 startTime, endTime, null, true, null, true);
291 List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form,
292 addTB);
293
294 Assert.assertEquals(
295 "There should no error in this time detail submission", 0,
296 errors.size());
297
298 startTime = new DateTime(2011, 2, 19, 9, 0, 0, 0,
299 TKUtils.getSystemDateTimeZone());
300 endTime = new DateTime(2011, 2, 20, 11, 0, 0, 0,
301 TKUtils.getSystemDateTimeZone());
302
303
304 addTB = TimeDetailTestUtils
305 .buildDetailActionForm(timeDoc, assToBeSelected, earnCode,
306 startTime, endTime, null, true, null, false);
307 errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
308
309 Assert.assertEquals(
310 "There should an error in this time detail submission", 1,
311 errors.size());
312 }
313
314 }