001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.workflow;
017    
018    import java.util.ArrayList;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.joda.time.DateTime;
024    import org.joda.time.DateTimeZone;
025    import org.joda.time.LocalDate;
026    import org.json.simple.JSONArray;
027    import org.json.simple.JSONObject;
028    import org.json.simple.JSONValue;
029    import org.junit.Assert;
030    import org.junit.Test;
031    import org.kuali.hr.time.util.TimeDetailTestUtils;
032    import org.kuali.hr.util.HtmlUnitUtil;
033    import org.kuali.kpme.core.FunctionalTest;
034    import org.kuali.kpme.core.assignment.Assignment;
035    import org.kuali.kpme.core.assignment.AssignmentDescriptionKey;
036    import org.kuali.kpme.core.calendar.entry.CalendarEntry;
037    import org.kuali.kpme.core.earncode.EarnCode;
038    import org.kuali.kpme.core.earncode.service.EarnCodeService;
039    import org.kuali.kpme.core.service.HrServiceLocator;
040    import org.kuali.kpme.core.util.TKUtils;
041    import org.kuali.kpme.tklm.time.detail.web.TimeDetailActionFormBase;
042    import org.kuali.kpme.tklm.time.service.TkServiceLocator;
043    import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
044    import org.kuali.kpme.tklm.time.timesheet.service.TimesheetService;
045    
046    import com.gargoylesoftware.htmlunit.html.HtmlForm;
047    import com.gargoylesoftware.htmlunit.html.HtmlPage;
048    
049    //@Ignore
050    @FunctionalTest
051    public class TimesheetIntegrationTest extends TimesheetWebTestBase {
052    
053            public static final String USER_PRINCIPAL_ID = "admin";
054            public static final DateTime TIME_SHEET_DATE = new DateTime(2011, 2, 15, 0, 0, 0, 0, DateTimeZone
055                                            .forID(TKUtils.getSystemTimeZone()));
056            public TimesheetDocument timeDoc;
057            public List<Assignment> assignmentsOfUser;
058            public CalendarEntry payCal;
059            public String tdocId;
060    
061            public static final String TEST_USER = "admin";
062            public static final Long TEST_ASSIGNMENT_JOB_NUMBER = 1L;
063            public static final Long TEST_ASSIGNMENT_JOB_NUMBER_2 = 2L;
064            public static final Long TEST_ASSIGNMENT_JOB_NUMBER_3 = 3L;
065            public static final Long TEST_ASSIGNMENT_JOB_NUMBER_4 = 4L;
066    
067            EarnCodeService earnCodeService = null;
068            TimesheetService timesheetService = null;
069    
070            @Test
071            public void getEarnCodes() throws Exception {
072            LocalDate asOfDate = LocalDate.now();
073                    List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(TEST_USER, asOfDate);
074                    Assert.assertNotNull(assignments);
075                    Assert.assertTrue("Emtpy assignment list", !assignments.isEmpty());
076    
077                    Assignment assignment1 = null;
078                    Assignment assignment2 = null;
079                    Assignment assignment3 = null;
080                    Assignment assignment4 = null;
081                    for (Assignment a : assignments) {
082                            if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER)) {
083                                    assignment1 = a;
084                            } else if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER_2)) {
085                                    assignment2 = a;
086                            } else if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER_3)) {
087                                    assignment3 = a;
088                            } else if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER_4)) {
089                                    assignment4 = a;
090                            }
091                    }
092    
093                    // one for each test scenario involving wildcards at least...
094                    Assert.assertNotNull("Test assignment not found.", assignment1);
095                    Assert.assertNotNull("Test assignment not found.", assignment2);
096                    Assert.assertNotNull("Test assignment not found.", assignment3);
097                    Assert.assertNotNull("Test assignment not found.", assignment4);
098    
099            //  Testing getEarnCodes* - these routines are separated among Leave and Time calendars. Run both, then run a combined routine that may not get used in practice.
100            //  As the testing data gets better, the Time and Leave results should have little to no overlap, and the assertions will need to be correspondingly updated.
101            // Testing standard lookup.
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            // Wildcard on SalaryGroup
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            // Dual Wildcards
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             * @throws Exception
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                    // retrieving time sheet for according to the pay calendar
132                    timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(
133                                    USER_PRINCIPAL_ID, payCal);
134                    tdocId = timeDoc.getDocumentId();
135    
136                    // login
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                    // check if page contains calendar for February 2011
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                    // Setup TimeDetailActionForm
174                    TimeDetailActionFormBase addTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, null, true);
175                    List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
176    
177                    // Check for errors
178                    Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
179    
180                    // submit the details of Timeblock to be added.
181                    page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
182                    Assert.assertNotNull(page);
183    
184                    // get Timeblocks objects from Timeblock string
185                    String dataText = page.getElementById("timeBlockString").getFirstChild().getNodeValue();
186    
187                    // check the values in timeblockString
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                    // check value ....
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                    // Setup TimeDetailActionForm for adding time block
237                    TimeDetailActionFormBase addTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, null, true);
238                    List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
239    
240                    // Check for errors
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                    // chk if the page contains the created time block.
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                    // now updating the time block
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                    // validation of time block
267                    errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, updateTB);
268                    Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
269    
270                    // update it
271                    page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), updateTB);
272                    Assert.assertNotNull(page);
273    
274                    // chk the timesheet contains the changes done with the time block
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                    // Setup TimeDetailActionForm
294                    TimeDetailActionFormBase addTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, null, true);
295                    List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
296    
297                    // Check for errors
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                    // chk the page must contain the created time block
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                    // Delete the timeblock
310                    String createTBId = timeDoc.getTimeBlocks().get(0).getTkTimeBlockId();
311                    HtmlUnitUtil.createTempFile(page);
312                    form = page.getFormByName("TimeDetailActionForm");
313                    Assert.assertNotNull(form);
314    
315                    // set detail for deleting time block
316                    TimeDetailActionFormBase deleteTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, createTBId, true);
317                    deleteTB.setMethodToCall("deleteTimeBlock");
318    
319                    // submitting the page
320                    page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), deleteTB);
321                    Assert.assertNotNull(page);
322    
323                    // chk the timesheet does not contain the time block
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            // KPME-1446
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                    // Assignment of user
340                    Assignment assToBeSelected = assignmentsOfUser.get(4);
341    
342            // retrieving earncode for the assignment
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());  // Thursday
350                    DateTime endTime = new DateTime(2011, 2, 20, 11, 0, 0, 0,
351                                    TKUtils.getSystemDateTimeZone());  // Sunday
352    
353                    // Setup TimeDetailActionForm1
354                    TimeDetailActionFormBase addTB = TimeDetailTestUtils
355                                    .buildDetailActionForm(timeDoc, assToBeSelected, earnCode, startTime,
356                                                    endTime, null, true, null, true); // last argument true = include weekends
357                    List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form,
358                                    addTB);
359                    // Check for errors - spanning weeks includes weekends, and include weekends box is checked - should give no error
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());  // Saturday
368                    endTime = new DateTime(2011, 2, 20, 11, 0, 0, 0,
369                                    TKUtils.getSystemDateTimeZone());  // Sunday
370    
371                    // Setup TimeDetailActionForm2
372                    addTB = TimeDetailTestUtils
373                                    .buildDetailActionForm(timeDoc, assToBeSelected, earnCode, startTime,
374                                                    endTime, null, true, null, false); // last argument false = do not include weekends
375                    errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
376                    // Check for errors - spanning weeks includes weekends, and include weekends box is not not checked - should give an error
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());       // Thursday
384                    endTime = new DateTime(2011, 2, 18, 11, 0, 0, 0,
385                                    TKUtils.getSystemDateTimeZone());  // Friday
386    
387                    // Setup TimeDetailActionForm2
388                    addTB = TimeDetailTestUtils
389                                    .buildDetailActionForm(timeDoc, assToBeSelected, earnCode, startTime,
390                                                    endTime, null, true, null, false); // last argument false = do not include weekends
391                    errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
392                    // Check for errors - spanning weeks includes weekends, an include weekends box is not not checked - should give an error
393                    // hours > 24.
394                    Assert.assertEquals(
395                                    "There should be no error in this time detail submission", 1,
396                                    errors.size());
397            }
398    
399    }