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 */
016package org.kuali.hr.time.detail.web;
017
018import com.gargoylesoftware.htmlunit.html.HtmlForm;
019import com.gargoylesoftware.htmlunit.html.HtmlPage;
020import org.joda.time.DateTime;
021import org.junit.Assert;
022import org.junit.Test;
023import org.kuali.hr.KPMEWebTestCase;
024import org.kuali.hr.time.util.TimeDetailTestUtils;
025import org.kuali.hr.util.HtmlUnitUtil;
026import org.kuali.kpme.core.FunctionalTest;
027import org.kuali.kpme.core.api.assignment.Assignment;
028import org.kuali.kpme.core.api.assignment.AssignmentDescriptionKey;
029import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
030import org.kuali.kpme.core.api.earncode.EarnCode;
031import org.kuali.kpme.core.service.HrServiceLocator;
032import org.kuali.kpme.core.util.TKUtils;
033import org.kuali.kpme.tklm.time.detail.web.TimeDetailActionFormBase;
034import org.kuali.kpme.tklm.time.service.TkServiceLocator;
035import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
036import org.kuali.kpme.tklm.utils.TkTestConstants;
037
038import java.util.List;
039
040@FunctionalTest
041public class SimpleTimeEntryValidationTest extends KPMEWebTestCase {
042
043    public static final String USER_PRINCIPAL_ID = "admin";
044        private DateTime JAN_AS_OF_DATE = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
045        private DateTime CALENDAR_ENTRY_DATE = new DateTime(2011, 1, 15, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
046        private String documentId;
047        
048    @Override
049    public void setUp() throws Exception {
050        super.setUp();
051
052        CalendarEntry calendarEntry =  HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates(USER_PRINCIPAL_ID, CALENDAR_ENTRY_DATE);
053        TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, calendarEntry);
054        documentId = timesheetDocument.getDocumentId();
055    }
056    
057    @Test
058    /**
059     * Example test to demonstrate adding time blocks to a time sheet using
060     * HTMLUnit rather than JS calls.
061     */
062    public void testExample1ShouldBeValidationErrors() throws Exception {
063        String baseUrl = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=" + documentId;
064        HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
065        Assert.assertNotNull(page);
066        String pageAsText = page.asText();
067        
068        Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
069        Assert.assertTrue("Login info not present.", pageAsText.contains("admin, admin"));
070
071        HtmlForm form = page.getFormByName("TimeDetailActionForm");
072        Assert.assertNotNull(form);
073
074        // 1. Obtain User Data
075        Assignment assignment = HrServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, AssignmentDescriptionKey.get("IU-BL_30_30_30"), JAN_AS_OF_DATE.toLocalDate());
076                EarnCode earnCode = HrServiceLocator.getEarnCodeService().getEarnCode("RGN", JAN_AS_OF_DATE.toLocalDate());
077
078        TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
079
080        // 2. Set Timeblock Start and End time
081        // Note - in this case, we're setting time that is outside of the valid
082        // pay period for document 2.
083        DateTime start = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
084        DateTime end = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
085
086        // Build an action form - we're using it as a POJO, it ties into the
087        // existing TK validation setup
088        TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(timesheetDocument,assignment, earnCode, start, end, null, false, null, true, null, null, null, null, null, null);
089        List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
090
091        // Check for errors
092        Assert.assertEquals("There should be 1 error in this time detail submission", 1, errors.size());
093        Assert.assertEquals("Error String Unexpected", "The start date/time is outside the calendar period", errors.get(0));
094    }
095
096
097    @Test
098    /**
099     * Example test to demonstrate adding time blocks to a time sheet using
100     * HTMLUnit rather than JS calls.
101     */
102    public void testExample2AddValidTimeBlock() throws Exception {
103        String baseUrl = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=" + documentId;
104        HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
105        Assert.assertNotNull(page);
106        //HtmlUnitUtil.createTempFile(page, "SimpleTimeEntry");
107        String pageAsText = page.asText();
108        Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
109        Assert.assertTrue("Login info not present.", pageAsText.contains("admin, admin"));
110
111        HtmlForm form = page.getFormByName("TimeDetailActionForm");
112        Assert.assertNotNull(form);
113
114        // 1. Obtain User Data
115        Assignment assignment = HrServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, AssignmentDescriptionKey.get("IU-IN_30_30_30"), JAN_AS_OF_DATE.toLocalDate());
116                EarnCode earnCode = HrServiceLocator.getEarnCodeService().getEarnCode("RGN", JAN_AS_OF_DATE.toLocalDate());
117
118        TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
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, null, null, null, null, null, null);
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(getWebClient(), baseUrl, tdaf);
136        Assert.assertNotNull(page);
137        page = getWebClient().getPage(baseUrl);
138        //HtmlUnitUtil.createTempFile(page, "TimeBlockPresent");
139
140        // Verify block present on rendered page.
141        pageAsText = page.asText();
142        Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("08:00 AM - 10:00 AM"));
143        Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("RGN - 2.00 hours"));
144    }
145
146
147}