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.lm.util;
017    
018    import com.gargoylesoftware.htmlunit.WebClient;
019    import com.gargoylesoftware.htmlunit.html.HtmlForm;
020    import com.gargoylesoftware.htmlunit.html.HtmlPage;
021    import org.apache.log4j.Logger;
022    import org.joda.time.DateTime;
023    import org.joda.time.Interval;
024    import org.kuali.hr.lm.LMConstants;
025    import org.kuali.hr.lm.leave.web.LeaveCalendarWSForm;
026    import org.kuali.hr.lm.leaveSummary.LeaveSummary;
027    import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument;
028    import org.kuali.hr.lm.leavecalendar.web.LeaveCalendarSubmitForm;
029    import org.kuali.hr.time.assignment.Assignment;
030    import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
031    import org.kuali.hr.time.earncode.EarnCode;
032    import org.kuali.hr.time.test.HtmlUnitUtil;
033    import org.kuali.hr.time.util.TKUtils;
034    import org.kuali.hr.time.util.TkConstants;
035    
036    import java.math.BigDecimal;
037    import java.net.URLEncoder;
038    
039    public class LeaveCalendarTestUtils {
040    
041        private static final Logger LOG = Logger.getLogger(LeaveCalendarTestUtils.class);
042    
043        /**
044         * From the provided set of parameters, build an action form suitable for
045         * submitting to the TimeDetailAction servlet. In our case, we are mostly
046         * using it in a mock type of situation, the situation of leave block addition.
047         *
048         * @param leaveCalendarDocument
049         * @param assignment
050         * @param earnCode
051         * @param start
052         * @param end
053         * @param amount
054         *
055         * @return A populated TimeDetailActionFormBase object.
056         */
057        public static LeaveCalendarWSForm buildLeaveCalendarForm(LeaveCalendarDocument leaveCalendarDocument, Assignment assignment, EarnCode earnCode, DateTime start, DateTime end, BigDecimal amount, boolean spanningWeeks) {
058            LeaveCalendarWSForm lcf = new LeaveCalendarWSForm();
059    
060            BigDecimal hours = null;
061            String startTimeS = null;
062            String endTimeS = null;
063            String startDateS;
064            String endDateS;
065            String selectedEarnCode;
066            String selectedAssignment;
067    
068            if (amount == null) {
069                if (start != null && end != null) {
070                    Interval se_i = new Interval(start, end);
071                    hours = TKUtils.convertMillisToHours(se_i.toDurationMillis());
072                }
073    
074                // the date/time format is defined in tk.calendar.js. For now, the format is 11/17/2010 8:0
075                startTimeS = start.toString("H:mm");
076                endTimeS = end.toString("H:mm");
077            } else {
078                hours = amount;
079            }
080    
081            startDateS = start.toString("MM/dd/YYYY");
082            endDateS = end.toString("MM/dd/YYYY");
083    
084            AssignmentDescriptionKey adk = new AssignmentDescriptionKey(assignment);
085            selectedAssignment = adk.toAssignmentKeyString();
086    
087            selectedEarnCode = earnCode.getEarnCode();
088    
089            //lcf.setAcrossDays(acrossDays ? "y" : "n");
090    
091            lcf.setSpanningWeeks(spanningWeeks ? "y" : "n"); // KPME-1446
092    
093            lcf.setLeaveAmount(hours);
094            //lcf.setHours(hours);
095            lcf.setStartDate(startDateS);
096            lcf.setEndDate(endDateS);
097            //lcf.setTkTimeBlockId(timeblockId);
098            lcf.setLeaveCalendarDocument(leaveCalendarDocument);
099            lcf.setSelectedAssignment(selectedAssignment);
100            lcf.setSelectedEarnCode(selectedEarnCode);
101            lcf.setMethodToCall("addLeaveBlock");
102    
103            return lcf;
104        }
105        
106        /**
107         * Builds a simple "mock" leave calendar form primed for action "approveLeaveCalendar".
108         * Suitable for testing logic LeaveCalendarSubmitAction actions.
109         *
110         * @param leaveCalendarDocument
111         * @param assignment
112         * @param earnCode
113         * @param start
114         * @param end
115         * @param amount
116         *
117         * @return A populated TimeDetailActionFormBase object.
118         */
119        public static LeaveCalendarWSForm buildLeaveCalendarFormForSubmission(LeaveCalendarDocument leaveCalendarDocument, LeaveSummary leaveSummary) {
120            LeaveCalendarWSForm lcf = new LeaveCalendarWSForm();
121    
122            lcf.setMethodToCall("approveLeaveCalendar");
123            lcf.setLeaveSummary(leaveSummary);
124    
125            return lcf;
126        }
127        
128    
129        /**
130         * Set the attributes on the provided html form to the values found in the provided
131         * ActionForm. Returns void, not a List<String> of errors.
132         *
133         * @param form The HtmlForm to populate.
134         * @param tdaf The ActionForm with values we will use to populate.
135         */
136        public static void setTimeBlockFormDetails(HtmlForm form, LeaveCalendarWSForm tdaf) {
137            //if (tdaf.getLeaveBlockId() != null) {
138            //    form.setAttribute("leaveBlockId", tdaf.getLeaveBlockId().toString());
139            //}
140            form.setAttribute("startDate", tdaf.getStartDate());
141            form.setAttribute("endDate", tdaf.getEndDate());
142    
143            if (tdaf.getLeaveAmount() != null) {
144                form.setAttribute("leaveAmount", tdaf.getLeaveAmount().toString());
145            }
146    
147            form.setAttribute("selectedEarnCode", tdaf.getSelectedEarnCode());
148            form.setAttribute("selectedAssignment", tdaf.getSelectedAssignment());
149            //form.setAttribute("acrossDays", tdaf.getAcrossDays());
150            form.setAttribute("methodToCall", tdaf.getMethodToCall());
151        }
152    
153        /**
154         * This is a 'hacker' method to get around the fact that in HtmlUnit you
155         * can no longer directly submit forms if there are no buttons. We
156         * simply add a button to the form, and click it!
157         *
158         * @param page The HtmlPage the form came from.
159         * @param form The HtmlForm you wish to submit.
160         * @return The return results from clicking .submit()
161         */
162        /*private static HtmlPage submitTimeDetailsDep(HtmlPage page, HtmlForm form) {
163            HtmlButton submitButton = null;
164    
165            //ScriptResult sr = page.executeJavaScript("document.forms[\"TimeDetailActionForm\"].submit();");
166    
167            if (submitButton == null) {
168                submitButton = (HtmlButton)page.createElement("button");
169                submitButton.setAttribute("type", "submit");
170                form.appendChild(submitButton);
171            }
172    
173            HtmlPage newPage = null;
174            try {
175                submitButton.click();
176            } catch (Exception e) {
177                LOG.error("While submitting time detail form", e);
178            }
179    
180            return newPage;
181        }*/
182    
183    
184        /**
185         * A method to wrap the submission of the time details.
186         * @param baseUrl
187         * @param tdaf
188         * @return
189         */
190        public static HtmlPage submitLeaveCalendar(WebClient webClient, String baseUrl, LeaveCalendarWSForm tdaf) {
191            // For now, until a more HtmlUnit based click method can be found
192            // workable, we're building a url-encoded string to directly
193            // post to the servlet.
194    
195            String url = baseUrl + buildPostFromFormParams(tdaf);
196            HtmlPage page = null;
197    
198            try {
199                page = HtmlUnitUtil.gotoPageAndLogin(webClient, url);
200            } catch (Exception e) {
201                LOG.error("Error while submitting form", e);
202            }
203    
204            return page;
205        }
206        
207        /**
208         * A method to wrap the submission of the time details.
209         * @param baseUrl
210         * @param tdaf
211         * @return
212         */
213        public static HtmlPage submitLeaveCalendar2(WebClient webClient, String baseUrl, LeaveCalendarWSForm tdaf) {
214            // For now, until a more HtmlUnit based click method can be found
215            // workable, we're building a url-encoded string to directly
216            // post to the servlet.
217    
218            String url = baseUrl + buildPostActionRequested(tdaf);
219            
220            HtmlPage page = null;
221    
222            try {
223                page = HtmlUnitUtil.gotoPageAndLogin(webClient, url);
224            } catch (Exception e) {
225                LOG.error("Error while submitting form", e);
226            }
227    
228            return page;
229        }
230    
231        /**
232         * A method to wrap the submission of the time details.
233         * @param       //baseUrl
234         * @param       //tdaf
235         * @return
236         */
237        /*public static HtmlPage submitTimeDetails(String principalId, String baseUrl, TimeDetailActionFormBase tdaf) {
238            // For now, until a more HtmlUnit based click method can be found
239            // workable, we're building a url-encoded string to directly
240            // post to the servlet.
241    
242            String url = baseUrl + buildPostFromFormParams(tdaf);
243            HtmlPage page = null;
244    
245            try {
246                TestAutoLoginFilter.OVERRIDE_ID = principalId;
247                page = HtmlUnitUtil.gotoPageAndLogin(url);
248                TestAutoLoginFilter.OVERRIDE_ID = "";
249            } catch (Exception e) {
250                LOG.error("Error while submitting form", e);
251            }
252    
253            return page;
254        }*/
255    
256        /**
257         * This will build a form post for the addition of leave blocks on the current leave calendar.
258         * @param tdaf
259         * @return
260         */
261        private static String buildPostFromFormParams(LeaveCalendarWSForm tdaf) {
262            StringBuilder builder = new StringBuilder();
263    
264            try {
265                builder.append("&methodToCall=").append(URLEncoder.encode(tdaf.getMethodToCall(), "UTF-8"));
266                //builder.append("&acrossDays=").append(URLEncoder.encode(tdaf.getAcrossDays(), "UTF-8"));
267                if (tdaf.getLeaveAmount() != null) {
268                    builder.append("&leaveAmount=").append(URLEncoder.encode(tdaf.getLeaveAmount().toString(), "UTF-8"));
269                //} else {
270                    //builder.append("&hours=").append(URLEncoder.encode(tdaf.getHours().toString(), "UTF-8"));
271                    //builder.append("&startDate=").append(URLEncoder.encode(tdaf.getStartDate(), "UTF-8"));
272                    //builder.append("&endDate=").append(URLEncoder.encode(tdaf.getEndDate(), "UTF-8"));
273                }
274                builder.append("&startDate=").append(URLEncoder.encode(tdaf.getStartDate(), "UTF-8"));
275                builder.append("&endDate=").append(URLEncoder.encode(tdaf.getEndDate(), "UTF-8"));
276                builder.append("&selectedAssignment=").append(URLEncoder.encode(tdaf.getSelectedAssignment(), "UTF-8"));
277                builder.append("&selectedEarnCode=").append(URLEncoder.encode(tdaf.getSelectedEarnCode(), "UTF-8"));
278                //if (tdaf.getTkTimeBlockId() != null) {
279                //    builder.append("&tkTimeBlockId=").append(URLEncoder.encode(tdaf.getTkTimeBlockId().toString(), "UTF-8"));
280                //}
281            } catch (Exception e) {
282                LOG.error("Exception building Post String", e);
283            }
284    
285            return builder.toString();
286        }
287        
288        private static String buildPostActionRequested(LeaveCalendarWSForm tdaf) {
289            StringBuilder builder = new StringBuilder();
290    
291            try {
292                    builder.append("&action=").append(URLEncoder.encode(TkConstants.DOCUMENT_ACTIONS.ROUTE,"UTF-8"));
293                builder.append("&methodToCall=").append(URLEncoder.encode(tdaf.getMethodToCall(), "UTF-8"));
294                //add more post params.
295            } catch (Exception e) {
296                LOG.error("Exception building Post String", e);
297            }
298    
299            return builder.toString();
300        }
301    }