001    /**
002     * Copyright 2004-2014 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                    page = getWebClient().getPage(TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId));
184                    Assert.assertNotNull(page);
185                    // get Timeblocks objects from Timeblock string
186                    String dataText = page.getElementById("timeBlockString").getFirstChild().getNodeValue();
187    
188                    // check the values in timeblockString
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                    // check value ....
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("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                    // Setup TimeDetailActionForm for adding time block
238                    TimeDetailActionFormBase addTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, null, true);
239                    List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
240    
241                    // Check for errors
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                    // chk if the page contains the created time block.
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                    // now updating the time block
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("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);
266    
267                    // validation of time block
268                    errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, updateTB);
269                    Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
270    
271                    // update it
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                    // chk the timesheet contains the changes done with the time block
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("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                    // Setup TimeDetailActionForm
296                    TimeDetailActionFormBase addTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, null, true);
297                    List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
298    
299                    // Check for errors
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                    // chk the page must contain the created time block
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                    // Delete the timeblock
312                    String createTBId = timeDoc.getTimeBlocks().get(0).getTkTimeBlockId();
313                    HtmlUnitUtil.createTempFile(page);
314                    form = page.getFormByName("TimeDetailActionForm");
315                    Assert.assertNotNull(form);
316    
317                    // set detail for deleting time block
318                    TimeDetailActionFormBase deleteTB = TimeDetailTestUtils.buildDetailActionForm(timeDoc, assignment, earnCode, startTime, endTime, null, true, createTBId, true);
319                    deleteTB.setMethodToCall("deleteTimeBlock");
320    
321                    // submitting the page
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                    // chk the timesheet does not contain the time block
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            // KPME-1446
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                    // Assignment of user
343                    Assignment assToBeSelected = assignmentsOfUser.get(4);
344    
345            // retrieving earncode for the assignment
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());  // Thursday
353                    DateTime endTime = new DateTime(2011, 2, 20, 11, 0, 0, 0,
354                                    TKUtils.getSystemDateTimeZone());  // Sunday
355    
356                    // Setup TimeDetailActionForm1
357                    TimeDetailActionFormBase addTB = TimeDetailTestUtils
358                                    .buildDetailActionForm(timeDoc, assToBeSelected, earnCode, startTime,
359                                                    endTime, null, true, null, true); // last argument true = include weekends
360                    List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form,
361                                    addTB);
362                    // Check for errors - spanning weeks includes weekends, and include weekends box is checked - should give no error
363                    Assert.assertEquals(
364                                    "There should be one error in this time detail submission", 1,
365                                    errors.size());
366                    
367                    Assert.assertTrue("Error is not related to number of hours in a day.", errors.contains("Hours cannot exceed 24."));
368    
369                    startTime = new DateTime(2011, 2, 19, 9, 0, 0, 0,
370                                    TKUtils.getSystemDateTimeZone());  // Saturday
371                    endTime = new DateTime(2011, 2, 20, 11, 0, 0, 0,
372                                    TKUtils.getSystemDateTimeZone());  // Sunday
373    
374                    // Setup TimeDetailActionForm2
375                    addTB = TimeDetailTestUtils
376                                    .buildDetailActionForm(timeDoc, assToBeSelected, earnCode, startTime,
377                                                    endTime, null, true, null, false); // last argument false = do not include weekends
378                    errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
379                    // Check for errors - spanning weeks includes weekends, and include weekends box is not not checked - should give an error
380                    Assert.assertEquals(
381                                    "There should two errors in this time detail submission", 1,
382                                    errors.size());
383                    
384                    Assert.assertTrue("Errors does not contain spanning weeks error", errors.contains("Weekend day is selected, but include weekends checkbox is not checked"));
385                    startTime = new DateTime(2011, 2, 17, 9, 0, 0, 0,
386                                    TKUtils.getSystemDateTimeZone());       // Thursday
387                    endTime = new DateTime(2011, 2, 18, 11, 0, 0, 0,
388                                    TKUtils.getSystemDateTimeZone());  // Friday
389    
390                    // Setup TimeDetailActionForm2
391                    addTB = TimeDetailTestUtils
392                                    .buildDetailActionForm(timeDoc, assToBeSelected, earnCode, startTime,
393                                                    endTime, null, true, null, false); // last argument false = do not include weekends
394                    errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, addTB);
395                    // Check for errors - spanning weeks includes weekends, an include weekends box is not not checked - should give an error
396                    // hours > 24.
397                    Assert.assertEquals(
398                                    "There should be no error in this time detail submission", 1,
399                                    errors.size());
400            }
401    
402    }