View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.detail.web;
17  
18  import java.sql.Date;
19  import java.util.List;
20  
21  import org.joda.time.DateTime;
22  import org.junit.Assert;
23  import org.junit.Test;
24  import org.kuali.hr.test.KPMETestCase;
25  import org.kuali.hr.time.assignment.Assignment;
26  import org.kuali.hr.time.earncode.EarnCode;
27  import org.kuali.hr.time.service.base.TkServiceLocator;
28  import org.kuali.hr.time.test.HtmlUnitUtil;
29  import org.kuali.hr.time.test.TkTestConstants;
30  import org.kuali.hr.time.timesheet.TimesheetDocument;
31  import org.kuali.hr.time.util.*;
32  
33  import com.gargoylesoftware.htmlunit.html.HtmlForm;
34  import com.gargoylesoftware.htmlunit.html.HtmlPage;
35  
36  /**
37   * Test data should contain:
38   *
39   * insert into tk_document_header_t values ('2', 'admin', '2011-02-01 00:00:00', 'I', '2011-01-15 00:00:00', NULL, '1');
40   */
41  public class SimpleTimeEntryValidationTest extends KPMETestCase {
42  
43      public static final String USER_PRINCIPAL_ID = "admin";
44  	private Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
45  
46      @Test
47      /**
48       * Example test to demonstrate adding time blocks to a time sheet using
49       * HTMLUnit rather than JS calls.
50       */
51      public void testExample1ShouldBeValidationErrors() throws Exception {
52          // Load document 2 -- an initiated document 1/15 - 2/01 // 2011
53          String tdocId = "2"; // The timesheet to open.
54          String baseUrl = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=" + tdocId;
55          HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(baseUrl);
56          Assert.assertNotNull(page);
57          String pageAsText = page.asText();
58          
59          Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
60          Assert.assertTrue("Login info not present.", pageAsText.contains("admin, admin"));
61  
62          HtmlForm form = page.getFormByName("TimeDetailActionForm");
63          Assert.assertNotNull(form);
64  
65          // 1. Obtain User Data
66          List<Assignment> assignments = TkServiceLocator.getAssignmentService().getAssignments(TKContext.getPrincipalId(), JAN_AS_OF_DATE);
67          Assignment assignment = assignments.get(0);
68  
69          List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodes(assignment, JAN_AS_OF_DATE);
70          EarnCode earnCode = earnCodes.get(0);
71  
72  
73          TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
74  
75          // 2. Set Timeblock Start and End time
76          // Note - in this case, we're setting time that is outside of the valid
77          // pay period for document 2.
78          DateTime start = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
79          DateTime end = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
80  
81          // Build an action form - we're using it as a POJO, it ties into the
82          // existing TK validation setup
83          TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(timesheetDocument, assignment, earnCode, start, end, null, false, null, true);
84          List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
85  
86          // Check for errors
87          Assert.assertEquals("There should be 1 error in this time detail submission", 1, errors.size());
88          Assert.assertEquals("Error String Unexpected", "The start date/time is outside the pay period", errors.get(0));
89      }
90  
91  
92      @Test
93      /**
94       * Example test to demonstrate adding time blocks to a time sheet using
95       * HTMLUnit rather than JS calls.
96       */
97      public void testExample2AddValidTimeBlock() throws Exception {
98          // Load document 2 -- an initiated document 1/15 - 2/01 // 2011
99          String tdocId = "2"; // The timesheet to open.
100         String baseUrl = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=" + tdocId;
101         HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(baseUrl);
102         Assert.assertNotNull(page);
103         //HtmlUnitUtil.createTempFile(page, "SimpleTimeEntry");
104         String pageAsText = page.asText();
105         Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
106         Assert.assertTrue("Login info not present.", pageAsText.contains("admin, admin"));
107 
108         HtmlForm form = page.getFormByName("TimeDetailActionForm");
109         Assert.assertNotNull(form);
110 
111         // 1. Obtain User Data
112         List<Assignment> assignments = TkServiceLocator.getAssignmentService().getAssignments(TKContext.getPrincipalId(), JAN_AS_OF_DATE);
113         Assignment assignment = assignments.get(0);
114 
115         List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodes(assignment, JAN_AS_OF_DATE);
116         EarnCode earnCode = earnCodes.get(0);
117 
118         TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
119 
120         // 2. Set Timeblock Start and End time
121         // 1/18/2011 - 8a to 10a
122         DateTime start = new DateTime(2011, 1, 18, 8, 0, 0, 0, TKUtils.getSystemDateTimeZone());
123         DateTime end = new DateTime(2011, 1, 18, 10, 0, 0, 0, TKUtils.getSystemDateTimeZone());
124 
125         // Build an action form - we're using it as a POJO, it ties into the
126         // existing TK validation setup
127         TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(timesheetDocument, assignment, earnCode, start, end, null, false, null, true);
128         List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
129 
130         // Check for errors
131         Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
132 
133         // Submit the Form to the Page.
134         // Note - This currently uses a less than desirable method to accomplish this...
135         page = TimeDetailTestUtils.submitTimeDetails(baseUrl, tdaf);
136         Assert.assertNotNull(page);
137         //HtmlUnitUtil.createTempFile(page, "TimeBlockPresent");
138 
139         // Verify block present on rendered page.
140         pageAsText = page.asText();
141         Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("08:00 AM - 10:00 AM"));
142         Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("RGN - 2.00 hours"));
143     }
144 
145 
146 }