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