View Javadoc

1   /**
2    * Copyright 2004-2013 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.assignment.AssignmentDescriptionKey;
27  import org.kuali.hr.time.earncode.EarnCode;
28  import org.kuali.hr.time.service.base.TkServiceLocator;
29  import org.kuali.hr.time.test.HtmlUnitUtil;
30  import org.kuali.hr.time.test.TkTestConstants;
31  import org.kuali.hr.time.timesheet.TimesheetDocument;
32  import org.kuali.hr.time.util.*;
33  
34  import com.gargoylesoftware.htmlunit.html.HtmlForm;
35  import com.gargoylesoftware.htmlunit.html.HtmlPage;
36  
37  /**
38   * Test data should contain:
39   *
40   * insert into tk_document_header_t values ('2', 'admin', '2011-02-01 00:00:00', 'I', '2011-01-15 00:00:00', NULL, '1');
41   */
42  public class SimpleTimeEntryValidationTest extends KPMETestCase {
43  
44      public static final String USER_PRINCIPAL_ID = "admin";
45  	private Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
46  
47      @Test
48      /**
49       * Example test to demonstrate adding time blocks to a time sheet using
50       * HTMLUnit rather than JS calls.
51       */
52      public void testExample1ShouldBeValidationErrors() throws Exception {
53          // Load document 2 -- an initiated document 1/15 - 2/01 // 2011
54          String tdocId = "2"; // The timesheet to open.
55          String baseUrl = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=" + tdocId;
56          HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
57          Assert.assertNotNull(page);
58          String pageAsText = page.asText();
59          
60          Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
61          Assert.assertTrue("Login info not present.", pageAsText.contains("admin, admin"));
62  
63          HtmlForm form = page.getFormByName("TimeDetailActionForm");
64          Assert.assertNotNull(form);
65  
66          // 1. Obtain User Data
67          Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, new AssignmentDescriptionKey("30_30_30"), JAN_AS_OF_DATE);
68  		EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode("RGN", JAN_AS_OF_DATE);
69  
70          TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
71  
72          // 2. Set Timeblock Start and End time
73          // Note - in this case, we're setting time that is outside of the valid
74          // pay period for document 2.
75          DateTime start = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
76          DateTime end = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
77  
78          // Build an action form - we're using it as a POJO, it ties into the
79          // existing TK validation setup
80          TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(timesheetDocument, assignment, earnCode, start, end, null, false, null, true);
81          List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
82  
83          // Check for errors
84          Assert.assertEquals("There should be 1 error in this time detail submission", 1, errors.size());
85          Assert.assertEquals("Error String Unexpected", "The start date/time is outside the pay period", errors.get(0));
86      }
87  
88  
89      @Test
90      /**
91       * Example test to demonstrate adding time blocks to a time sheet using
92       * HTMLUnit rather than JS calls.
93       */
94      public void testExample2AddValidTimeBlock() throws Exception {
95          // Load document 2 -- an initiated document 1/15 - 2/01 // 2011
96          String tdocId = "2"; // The timesheet to open.
97          String baseUrl = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=" + tdocId;
98          HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
99          Assert.assertNotNull(page);
100         //HtmlUnitUtil.createTempFile(page, "SimpleTimeEntry");
101         String pageAsText = page.asText();
102         Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
103         Assert.assertTrue("Login info not present.", pageAsText.contains("admin, admin"));
104 
105         HtmlForm form = page.getFormByName("TimeDetailActionForm");
106         Assert.assertNotNull(form);
107 
108         // 1. Obtain User Data
109         Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, new AssignmentDescriptionKey("30_30_30"), JAN_AS_OF_DATE);
110 		EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode("RGN", JAN_AS_OF_DATE);
111 
112         TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
113 
114         // 2. Set Timeblock Start and End time
115         // 1/18/2011 - 8a to 10a
116         DateTime start = new DateTime(2011, 1, 18, 8, 0, 0, 0, TKUtils.getSystemDateTimeZone());
117         DateTime end = new DateTime(2011, 1, 18, 10, 0, 0, 0, TKUtils.getSystemDateTimeZone());
118 
119         // Build an action form - we're using it as a POJO, it ties into the
120         // existing TK validation setup
121         TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(timesheetDocument, assignment, earnCode, start, end, null, false, null, true);
122         List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
123 
124         // Check for errors
125         Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
126 
127         // Submit the Form to the Page.
128         // Note - This currently uses a less than desirable method to accomplish this...
129         page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), baseUrl, tdaf);
130         Assert.assertNotNull(page);
131         //HtmlUnitUtil.createTempFile(page, "TimeBlockPresent");
132 
133         // Verify block present on rendered page.
134         pageAsText = page.asText();
135         Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("08:00 AM - 10:00 AM"));
136         Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("RGN - 2.00 hours"));
137     }
138 
139 
140 }