View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.lm.util;
17  
18  import com.gargoylesoftware.htmlunit.WebClient;
19  import com.gargoylesoftware.htmlunit.html.HtmlForm;
20  import com.gargoylesoftware.htmlunit.html.HtmlPage;
21  import org.apache.log4j.Logger;
22  import org.joda.time.DateTime;
23  import org.joda.time.Interval;
24  import org.kuali.hr.lm.LMConstants;
25  import org.kuali.hr.lm.leave.web.LeaveCalendarWSForm;
26  import org.kuali.hr.lm.leaveSummary.LeaveSummary;
27  import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument;
28  import org.kuali.hr.lm.leavecalendar.web.LeaveCalendarSubmitForm;
29  import org.kuali.hr.time.assignment.Assignment;
30  import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
31  import org.kuali.hr.time.earncode.EarnCode;
32  import org.kuali.hr.time.test.HtmlUnitUtil;
33  import org.kuali.hr.time.util.TKUtils;
34  import org.kuali.hr.time.util.TkConstants;
35  
36  import java.math.BigDecimal;
37  import java.net.URLEncoder;
38  
39  public class LeaveCalendarTestUtils {
40  
41      private static final Logger LOG = Logger.getLogger(LeaveCalendarTestUtils.class);
42  
43      /**
44       * From the provided set of parameters, build an action form suitable for
45       * submitting to the TimeDetailAction servlet. In our case, we are mostly
46       * using it in a mock type of situation, the situation of leave block addition.
47       *
48       * @param leaveCalendarDocument
49       * @param assignment
50       * @param earnCode
51       * @param start
52       * @param end
53       * @param amount
54       *
55       * @return A populated TimeDetailActionFormBase object.
56       */
57      public static LeaveCalendarWSForm buildLeaveCalendarForm(LeaveCalendarDocument leaveCalendarDocument, Assignment assignment, EarnCode earnCode, DateTime start, DateTime end, BigDecimal amount, boolean spanningWeeks) {
58          LeaveCalendarWSForm lcf = new LeaveCalendarWSForm();
59  
60          BigDecimal hours = null;
61          String startTimeS = null;
62          String endTimeS = null;
63          String startDateS;
64          String endDateS;
65          String selectedEarnCode;
66          String selectedAssignment;
67  
68          if (amount == null) {
69              if (start != null && end != null) {
70                  Interval se_i = new Interval(start, end);
71                  hours = TKUtils.convertMillisToHours(se_i.toDurationMillis());
72              }
73  
74              // the date/time format is defined in tk.calendar.js. For now, the format is 11/17/2010 8:0
75              startTimeS = start.toString("H:mm");
76              endTimeS = end.toString("H:mm");
77          } else {
78              hours = amount;
79          }
80  
81          startDateS = start.toString("MM/dd/YYYY");
82          endDateS = end.toString("MM/dd/YYYY");
83  
84          AssignmentDescriptionKey adk = new AssignmentDescriptionKey(assignment);
85          selectedAssignment = adk.toAssignmentKeyString();
86  
87          selectedEarnCode = earnCode.getEarnCode();
88  
89          //lcf.setAcrossDays(acrossDays ? "y" : "n");
90  
91          lcf.setSpanningWeeks(spanningWeeks ? "y" : "n"); // KPME-1446
92  
93          lcf.setLeaveAmount(hours);
94          //lcf.setHours(hours);
95          lcf.setStartDate(startDateS);
96          lcf.setEndDate(endDateS);
97          //lcf.setTkTimeBlockId(timeblockId);
98          lcf.setLeaveCalendarDocument(leaveCalendarDocument);
99          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 }