001    /**
002     * Copyright 2004-2012 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.timesheet.web;
017    
018    import java.sql.Date;
019    import java.util.*;
020    
021    import org.joda.time.DateTime;
022    import org.joda.time.DateTimeZone;
023    import org.json.simple.JSONArray;
024    import org.json.simple.JSONObject;
025    import org.json.simple.JSONValue;
026    import org.junit.Assert;
027    import org.junit.Test;
028    import org.kuali.hr.time.assignment.Assignment;
029    import org.kuali.hr.time.calendar.CalendarEntries;
030    import org.kuali.hr.time.detail.web.TimeDetailActionFormBase;
031    import org.kuali.hr.time.earncode.EarnCode;
032    import org.kuali.hr.time.service.base.TkServiceLocator;
033    import org.kuali.hr.time.test.HtmlUnitUtil;
034    import org.kuali.hr.time.timesheet.TimesheetDocument;
035    import org.kuali.hr.time.util.TKUtils;
036    import org.kuali.hr.time.util.TimeDetailTestUtils;
037    
038    import com.gargoylesoftware.htmlunit.html.HtmlForm;
039    import com.gargoylesoftware.htmlunit.html.HtmlPage;
040    
041    //@Ignore
042    public class TimesheetIntegrationTest extends TimesheetWebTestBase {
043    
044            public static final String USER_PRINCIPAL_ID = "admin";
045            public static final Date TIME_SHEET_DATE = new Date(
046                            (new DateTime(2011, 2, 15, 0, 0, 0, 0, DateTimeZone
047                                            .forID(TKUtils.getSystemTimeZone()))).getMillis());
048            public TimesheetDocument timeDoc;
049            public List<Assignment> assignmentsOfUser;
050            public CalendarEntries payCal;
051            public String tdocId;
052    
053            /**
054             * @throws Exception
055             */
056            public void setUp() throws Exception {
057    
058                    super.setUp();
059    
060                    payCal = TkServiceLocator.getCalendarService().getCurrentCalendarDates(
061                                    USER_PRINCIPAL_ID, TIME_SHEET_DATE);
062                    Assert.assertNotNull("Pay calendar entries not found for admin", payCal);
063    
064                    // retrieving time sheet for according to the pay calendar
065                    timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(
066                                    USER_PRINCIPAL_ID, payCal);
067                    tdocId = timeDoc.getDocumentId();
068    
069                    // login
070                    HtmlPage page = loginAndGetTimeDetailsHtmlPage(USER_PRINCIPAL_ID,
071                                    tdocId, true);
072                    Assert.assertNotNull(page);
073    
074                    assignmentsOfUser = TkServiceLocator.getAssignmentService()
075                                    .getAssignments(USER_PRINCIPAL_ID, TIME_SHEET_DATE);
076                    Assert.assertNotNull("No Assignments found for the user ", assignmentsOfUser);
077    
078                    // check if page contains calendar for February 2011
079                    Assert.assertTrue("Page could not find calendar for Feb 2011", page.asText()
080                                    .contains("February 2011"));
081    
082                    HtmlForm form = page.getFormByName("TimeDetailActionForm");
083                    Assert.assertNotNull(form);
084            }
085    
086            public void tearDown() throws Exception {
087                    super.tearDown();
088            }
089    
090            @Test
091            public void testAddTimeBlock() throws Exception {
092    
093                    EarnCode earnCode = null;
094                    HtmlPage page = loginAndGetTimeDetailsHtmlPage(USER_PRINCIPAL_ID,
095                                    tdocId, true);
096    
097                    HtmlForm form = page.getFormByName("TimeDetailActionForm");
098                    Assert.assertNotNull(form);
099    
100                    // Assignment of user
101                    Assignment assToBeSelected = assignmentsOfUser.get(4);
102    
103            // retrieving earncode for the assignment
104                    List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodes(assToBeSelected, TIME_SHEET_DATE);
105                    if (earnCodes != null && !earnCodes.isEmpty()) {
106                            earnCode = earnCodes.get(0);
107                    }
108    
109                    DateTime startTime = new DateTime(2011, 2, 15, 9, 0, 0, 0,
110                                    TKUtils.getSystemDateTimeZone());
111                    DateTime endTime = new DateTime(2011, 2, 15, 11, 0, 0, 0,
112                                    TKUtils.getSystemDateTimeZone());
113    
114                    // Setup TimeDetailActionForm
115                    TimeDetailActionFormBase addTB = TimeDetailTestUtils
116                                    .buildDetailActionForm(timeDoc, assToBeSelected, earnCode,
117                                                    startTime, endTime, null, true, null, true);
118                    List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form,
119                                    addTB);
120    
121                    // Check for errors
122                    Assert.assertEquals(
123                                    "There should be no errors in this time detail submission", 0,
124                                    errors.size());
125    
126                    // submit the details of Timeblock to be added.
127                    page = TimeDetailTestUtils.submitTimeDetails(
128                                    TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
129                    Assert.assertNotNull(page);
130    
131                    // get Timeblocks objects from Timeblock string
132                    String dataText = page.getElementById("timeBlockString")
133                                    .getFirstChild().getNodeValue();
134    
135                    // check the values in timeblockString
136                    JSONArray jsonData = (JSONArray) JSONValue.parse(dataText);
137                    final JSONObject jsonDataObject = (JSONObject) jsonData.get(0);
138                    final String assignmentKey = assToBeSelected.getAssignmentKey();
139                    Assert.assertTrue("TimeBlock Data Missing.", checkJSONValues(new JSONObject() {
140                            {
141                                    put("outer", jsonDataObject);
142                            }
143                    }, new ArrayList<Map<String, Object>>() {
144                            {
145                                    add(new HashMap<String, Object>() {
146                                            {
147                                                    put("earnCode", "RGN");
148                                                    put("hours", "2.0");
149                                                    put("amount", null);
150                                            }
151                                    });
152                            }
153                    }, new HashMap<String, Object>() {
154                            {
155                                    put("earnCode", "RGN");
156                                    put("startNoTz", "2011-02-15T09:00:00");
157                                    put("endNoTz", "2011-02-15T11:00:00");
158                                    put("assignment", assignmentKey);
159                            }
160                    }));
161    
162    
163                    // check value ....
164                    Assert.assertTrue("TimeBlock did not created successfully.", page.asText()
165                                    .contains("work area description-description 1"));
166                    Assert.assertTrue("TimeBlock did not created successfully.", page.asText()
167                                    .contains("RGN - 2.00 hours"));
168    
169            }
170    
171            @Test
172            public void testEditTimeBlock() throws Exception {
173                    EarnCode earnCode = null;
174    
175                    // login
176                    HtmlPage page = loginAndGetTimeDetailsHtmlPage(USER_PRINCIPAL_ID,
177                                    tdocId, true);
178    
179                    // Assignment
180                    Assignment assToBeSelected = assignmentsOfUser.get(4);
181    
182            // retrieving earncode for the assignment
183                    List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodes(assToBeSelected, TIME_SHEET_DATE);
184                    if (earnCodes != null && !earnCodes.isEmpty()) {
185                            earnCode = earnCodes.get(0);
186                    }
187    
188                    DateTime startTime = new DateTime(2011, 2, 15, 9, 0, 0, 0,
189                                    TKUtils.getSystemDateTimeZone());
190                    DateTime endTime = new DateTime(2011, 2, 15, 11, 0, 0, 0,
191                                    TKUtils.getSystemDateTimeZone());
192    
193                    HtmlForm form = page.getFormByName("TimeDetailActionForm");
194                    Assert.assertNotNull(form);
195    
196                    // Setup TimeDetailActionForm for adding time block
197                    TimeDetailActionFormBase addTB = TimeDetailTestUtils
198                                    .buildDetailActionForm(timeDoc, assToBeSelected, earnCode,
199                                                    startTime, endTime, null, true, null, true);
200                    List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form,
201                                    addTB);
202    
203                    // Check for errors
204                    Assert.assertEquals(
205                                    "There should be no errors in this time detail submission", 0,
206                                    errors.size());
207    
208                    page = TimeDetailTestUtils.submitTimeDetails(
209                                    TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
210                    Assert.assertNotNull(page);
211    
212                    // chk if the page contains the created time block.
213                    Assert.assertTrue("TimeBlock not Present.",
214                                    page.asText().contains("work area description-description 1"));
215                    Assert.assertTrue("TimeBlock not Present.",
216                                    page.asText().contains("RGN - 2.00 hours"));
217    
218                    // now updating the time block
219                    timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(
220                                    USER_PRINCIPAL_ID, payCal);
221    
222                    String createdTBId = timeDoc.getTimeBlocks().get(0).getTkTimeBlockId();
223    
224                    HtmlUnitUtil.createTempFile(page);
225    
226                    // change assignment and time
227                    assToBeSelected = assignmentsOfUser.get(3);
228    
229                    // earn codes related to the assignment
230                    earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodes(assToBeSelected, TIME_SHEET_DATE);
231                    if (earnCodes != null && !earnCodes.isEmpty()) {
232                            earnCode = earnCodes.get(0);
233                    }
234    
235                    DateTime startTime1 = new DateTime(2011, 2, 15, 14, 0, 0, 0,
236                                    TKUtils.getSystemDateTimeZone());
237                    DateTime endTime1 = new DateTime(2011, 2, 15, 17, 0, 0, 0,
238                                    TKUtils.getSystemDateTimeZone());
239    
240                    form = page.getFormByName("TimeDetailActionForm");
241    
242                    TimeDetailActionFormBase updateTB = TimeDetailTestUtils
243                                    .buildDetailActionForm(timeDoc, assToBeSelected, earnCode,
244                                                    startTime1, endTime1, null, true, createdTBId, true);
245    
246                    // validation of time block
247                    errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, updateTB);
248                    Assert.assertEquals(
249                                    "There should be no errors in this time detail submission", 0,
250                                    errors.size());
251    
252                    // update it
253                    page = TimeDetailTestUtils.submitTimeDetails(
254                                    TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), updateTB);
255                    Assert.assertNotNull(page);
256    
257                    // chk the timesheet contains the changes done with the time block
258                    Assert.assertTrue("TimeBlock did not updated properly.", page.asText()
259                                    .contains("work area description-description 1"));
260                    Assert.assertTrue("TimeBlock did not updated properly.", page.asText()
261                                    .contains("RGN - 3.00 hours"));
262    
263            }
264    
265            @Test
266            public void testDeleteTimeBlock() throws Exception {
267    
268                    EarnCode earnCode = null;
269    
270                    // login
271                    HtmlPage page = loginAndGetTimeDetailsHtmlPage(USER_PRINCIPAL_ID,
272                                    tdocId, true);
273    
274                    // Assignment of user
275                    Assignment assToBeSelected = assignmentsOfUser.get(4);
276    
277            // retrieving earncode for the assignment
278                    List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodes(assToBeSelected, TIME_SHEET_DATE);
279                    if (earnCodes != null && !earnCodes.isEmpty()) {
280                            earnCode = earnCodes.get(0);
281                    }
282    
283                    DateTime startTime = new DateTime(2011, 2, 15, 9, 0, 0, 0,
284                                    TKUtils.getSystemDateTimeZone());
285                    DateTime endTime = new DateTime(2011, 2, 15, 11, 0, 0, 0,
286                                    TKUtils.getSystemDateTimeZone());
287    
288                    HtmlForm form = page.getFormByName("TimeDetailActionForm");
289                    Assert.assertNotNull(form);
290    
291                    // Setup TimeDetailActionForm
292                    TimeDetailActionFormBase addTB = TimeDetailTestUtils
293                                    .buildDetailActionForm(timeDoc, assToBeSelected, earnCode,
294                                                    startTime, endTime, null, true, null, true);
295                    List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form,
296                                    addTB);
297    
298                    // Check for errors
299                    Assert.assertEquals(
300                                    "There should be no errors in this time detail submission", 0,
301                                    errors.size());
302    
303                    page = TimeDetailTestUtils.submitTimeDetails(
304                                    TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
305                    Assert.assertNotNull(page);
306    
307                    // chk the page must contain the created time block
308                    Assert.assertTrue("TimeBlock did not created successfully.", page.asText()
309                                    .contains("work area description-description 1"));
310                    Assert.assertTrue("TimeBlock did not created successfully.", page.asText()
311                                    .contains("RGN - 2.00 hours"));
312    
313                    timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(
314                                    USER_PRINCIPAL_ID, payCal);
315    
316                    // Delete the timeblock
317                    String createTBId = timeDoc.getTimeBlocks().get(0).getTkTimeBlockId();
318                    HtmlUnitUtil.createTempFile(page);
319                    form = page.getFormByName("TimeDetailActionForm");
320                    Assert.assertNotNull(form);
321    
322                    // set detail for deleting time block
323                    TimeDetailActionFormBase deleteTB = TimeDetailTestUtils
324                                    .buildDetailActionForm(timeDoc, assToBeSelected, earnCode,
325                                                    startTime, endTime, null, true, createTBId, true);
326                    deleteTB.setMethodToCall("deleteTimeBlock");
327    
328                    // submitting the page
329                    page = TimeDetailTestUtils.submitTimeDetails(
330                                    TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), deleteTB);
331                    Assert.assertNotNull(page);
332    
333                    // chk the timesheet does not contain the time block
334                    Assert.assertTrue("TimeBlock did not deleted successfully.", !page.asText()
335                                    .contains("work area description-description 1"));
336                    Assert.assertTrue("TimeBlock did not deleted successfully.", !page.asText()
337                                    .contains("RGN - 2.00 hours"));
338    
339            }
340    
341    
342    }