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.detail.web;
017    
018    import java.sql.Date;
019    import java.util.List;
020    
021    import org.joda.time.DateTime;
022    import org.junit.Assert;
023    import org.junit.Test;
024    import org.kuali.hr.test.KPMETestCase;
025    import org.kuali.hr.time.assignment.Assignment;
026    import org.kuali.hr.time.earncode.EarnCode;
027    import org.kuali.hr.time.service.base.TkServiceLocator;
028    import org.kuali.hr.time.test.HtmlUnitUtil;
029    import org.kuali.hr.time.test.TkTestConstants;
030    import org.kuali.hr.time.timesheet.TimesheetDocument;
031    import org.kuali.hr.time.util.*;
032    
033    import com.gargoylesoftware.htmlunit.html.HtmlForm;
034    import com.gargoylesoftware.htmlunit.html.HtmlPage;
035    
036    /**
037     * Test data should contain:
038     *
039     * insert into tk_document_header_t values ('2', 'admin', '2011-02-01 00:00:00', 'I', '2011-01-15 00:00:00', NULL, '1');
040     */
041    public class SimpleTimeEntryValidationTest extends KPMETestCase {
042    
043        public static final String USER_PRINCIPAL_ID = "admin";
044            private Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
045    
046        @Test
047        /**
048         * Example test to demonstrate adding time blocks to a time sheet using
049         * HTMLUnit rather than JS calls.
050         */
051        public void testExample1ShouldBeValidationErrors() throws Exception {
052            // Load document 2 -- an initiated document 1/15 - 2/01 // 2011
053            String tdocId = "2"; // The timesheet to open.
054            String baseUrl = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=" + tdocId;
055            HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(baseUrl);
056            Assert.assertNotNull(page);
057            String pageAsText = page.asText();
058            
059            Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
060            Assert.assertTrue("Login info not present.", pageAsText.contains("admin, admin"));
061    
062            HtmlForm form = page.getFormByName("TimeDetailActionForm");
063            Assert.assertNotNull(form);
064    
065            // 1. Obtain User Data
066            List<Assignment> assignments = TkServiceLocator.getAssignmentService().getAssignments(TKContext.getPrincipalId(), JAN_AS_OF_DATE);
067            Assignment assignment = assignments.get(0);
068    
069            List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodes(assignment, JAN_AS_OF_DATE);
070            EarnCode earnCode = earnCodes.get(0);
071    
072    
073            TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
074    
075            // 2. Set Timeblock Start and End time
076            // Note - in this case, we're setting time that is outside of the valid
077            // pay period for document 2.
078            DateTime start = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
079            DateTime end = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
080    
081            // Build an action form - we're using it as a POJO, it ties into the
082            // existing TK validation setup
083            TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(timesheetDocument, assignment, earnCode, start, end, null, false, null, true);
084            List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
085    
086            // Check for errors
087            Assert.assertEquals("There should be 1 error in this time detail submission", 1, errors.size());
088            Assert.assertEquals("Error String Unexpected", "The start date/time is outside the pay period", errors.get(0));
089        }
090    
091    
092        @Test
093        /**
094         * Example test to demonstrate adding time blocks to a time sheet using
095         * HTMLUnit rather than JS calls.
096         */
097        public void testExample2AddValidTimeBlock() throws Exception {
098            // Load document 2 -- an initiated document 1/15 - 2/01 // 2011
099            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    }