001    /**
002     * Copyright 2004-2013 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 com.gargoylesoftware.htmlunit.html.*;
022    import org.joda.time.DateTime;
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.assignment.AssignmentDescriptionKey;
030    import org.kuali.hr.time.calendar.CalendarEntries;
031    import org.kuali.hr.time.detail.web.TimeDetailActionFormBase;
032    import org.kuali.hr.time.earncode.EarnCode;
033    import org.kuali.hr.time.service.base.TkServiceLocator;
034    import org.kuali.hr.time.test.HtmlUnitUtil;
035    import org.kuali.hr.time.test.TkTestConstants;
036    import org.kuali.hr.time.test.TkTestUtils;
037    import org.kuali.hr.time.timesheet.TimesheetDocument;
038    import org.kuali.hr.time.util.*;
039    import org.kuali.hr.time.web.TkLoginFilter;
040    
041    import org.kuali.hr.util.filter.TestAutoLoginFilter;
042    import org.kuali.rice.kew.api.document.DocumentStatus;
043    
044    public class TimesheetWorkflowIntegrationTest extends TimesheetWebTestBase {
045    
046        public static final String USER_PRINCIPAL_ID = "admin";
047            private Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
048    
049    
050        /**
051         * @throws Exception
052         */
053        public void setUp() throws Exception {
054            super.setUp();
055            // Data is loaded as part of database loading lifecycle
056            // See: tk-test-data.sql
057            // See: TimesheetWorkflowIntegrationTest.sql
058            // See: TkTestCase.java
059            //
060        }
061    
062        public void tearDown() throws Exception {
063            super.tearDown();
064        }
065    
066        @Test
067        /**
068         * - create timesheet
069         * - add two 8 hour time blocks
070         * - submit timesheet for routing
071         * - ## login as approver
072         * - look for approval button
073         * - approve timeblock
074         * - verify approval button gone
075         * - ## login as original user
076         * - verify submit for routing button gone
077         */
078        public void testTimesheetSubmissionIntegration() throws Exception {
079            Date asOfDate = new Date((new DateTime(2011, 3, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
080            CalendarEntries pcd = TkServiceLocator.getCalendarService().getCurrentCalendarDates(USER_PRINCIPAL_ID, asOfDate);
081            Assert.assertNotNull("No PayCalendarDates", pcd);
082            TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, pcd);
083            String tdocId = tdoc.getDocumentId();
084            HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), "admin", tdocId, true);
085    
086            // 1. Obtain User Data
087            Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(TKContext.getPrincipalId(), new AssignmentDescriptionKey("30_30_30"), JAN_AS_OF_DATE);
088            EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode("RGN", JAN_AS_OF_DATE);
089    
090            // 2. Set Timeblock Start and End time
091            // 3/02/2011 - 8:00a to 4:00pm
092            DateTime start = new DateTime(2011, 3, 2, 8, 0, 0, 0, TKUtils.getSystemDateTimeZone());
093            DateTime end = new DateTime(2011, 3, 3, 16, 0, 0, 0, TKUtils.getSystemDateTimeZone());
094    
095            HtmlForm form = page.getFormByName("TimeDetailActionForm");
096            Assert.assertNotNull(form);
097    
098            // Build an action form - we're using it as a POJO, it ties into the
099            // existing TK validation setup
100            TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(tdoc, assignment, earnCode, start, end, null, true, null, true);
101            List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
102            // Check for errors
103            Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
104    
105            page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), tdaf);
106            Assert.assertNotNull(page);
107            HtmlUnitUtil.createTempFile(page, "TimeBlockPresent");
108    
109            // Verify block present on rendered page.
110            String pageAsText = page.asText();
111    
112            // JSON
113            //
114            //
115            // Grab the timeblock data from the text area. We can check specifics there
116            // to be more fine grained in our validation.
117            String dataText = page.getElementById("timeBlockString").getFirstChild().getNodeValue();
118            JSONArray jsonData = (JSONArray)JSONValue.parse(dataText);
119            final JSONObject jsonDataObject = (JSONObject) jsonData.get(0);
120            Assert.assertTrue("TimeBlock Data Missing.", checkJSONValues(new JSONObject() {{ put("outer", jsonDataObject); }},
121                    new ArrayList<Map<String, Object>>() {{
122                        add(new HashMap<String, Object>() {{
123                            put("earnCode", "RGN");
124                            put("hours", "8.0");
125                            put("amount", null);
126                        }});
127                    }},
128                    new HashMap<String, Object>() {{
129                        put("earnCode", "RGN");
130                        put("startNoTz", "2011-03-02T08:00:00");
131                        put("endNoTz", "2011-03-02T16:00:00");
132                        put("title", "SDR1 Work Area");
133                        put("assignment", "30_30_30");
134                    }}
135            ));
136    
137            // Check the Display Rendered Text for Time Block, Quick Check
138            Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("08:00 AM - 04:00 PM"));
139            Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("RGN - 8.00 hours"));
140    
141            //
142            // Route Timesheet
143            //
144            // Routing is initiated via javascript, we need to extract the routing
145            // action from the button element to perform this action.
146            HtmlButtonInput routeButton = (HtmlButtonInput)page.getElementById("ts-route-button");
147            String routeHref = TkTestUtils.getOnClickHref(routeButton);
148            // The 'only' way to do the button click.
149            page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.BASE_URL + "/" + routeHref);
150            //HtmlUnitUtil.createTempFile(page, "RouteClicked");
151            pageAsText = page.asText();
152            // Verify Route Status via UI
153            Assert.assertTrue("Wrong Document Loaded.", pageAsText.contains(tdocId));
154            Assert.assertTrue("Document not routed.", pageAsText.contains(DocumentStatus.ENROUTE.getLabel()));
155            routeButton = (HtmlButtonInput)page.getElementById("ts-route-button");
156            Assert.assertNull("Route button should not be present.", routeButton);
157            HtmlButtonInput approveButton = (HtmlButtonInput)page.getElementById("ts-approve-button");
158            Assert.assertNull("Approval button should not be present.", approveButton);
159    
160            //
161            // Login as Approver, who is not 'admin'
162            page = TimesheetWebTestBase.loginAndGetTimeDetailsHtmlPage(getWebClient(), "eric", tdocId, true);
163            //HtmlUnitUtil.createTempFile(page, "2ndLogin");
164            pageAsText = page.asText();
165            Assert.assertTrue("Document not routed.", pageAsText.contains(DocumentStatus.ENROUTE.getLabel()));
166            approveButton = (HtmlButtonInput)page.getElementById("ts-approve-button");
167            Assert.assertNotNull("No approval button present.", approveButton);
168    
169            // Click Approve
170            // And Verify
171            //
172            routeHref = TkTestUtils.getOnClickHref(approveButton);
173            TestAutoLoginFilter.OVERRIDE_ID = "eric";
174            page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.BASE_URL + "/" + routeHref);
175            TestAutoLoginFilter.OVERRIDE_ID = "";
176            //HtmlUnitUtil.createTempFile(page, "ApproveClicked");
177            pageAsText = page.asText();
178            Assert.assertTrue("Wrong Document Loaded.", pageAsText.contains(tdocId));
179            Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
180            Assert.assertTrue("Login info not present.", pageAsText.contains("eric, eric"));
181            Assert.assertTrue("Document not routed.", pageAsText.contains(DocumentStatus.FINAL.getLabel()));
182            approveButton = (HtmlButtonInput)page.getElementById("ts-approve-button");
183            Assert.assertNull("Approval button should not be present.", approveButton);
184        }
185    
186    }