1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
45
46
47
48
49
50
51
52
53
54
55
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
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
90
91 lcf.setSpanningWeeks(spanningWeeks ? "y" : "n");
92
93 lcf.setLeaveAmount(hours);
94
95 lcf.setStartDate(startDateS);
96 lcf.setEndDate(endDateS);
97
98 lcf.setLeaveCalendarDocument(leaveCalendarDocument);
99 lcf.setSelectedAssignment(selectedAssignment);
100 lcf.setSelectedEarnCode(selectedEarnCode);
101 lcf.setMethodToCall("addLeaveBlock");
102
103 return lcf;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
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
131
132
133
134
135
136 public static void setTimeBlockFormDetails(HtmlForm form, LeaveCalendarWSForm tdaf) {
137
138
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
150 form.setAttribute("methodToCall", tdaf.getMethodToCall());
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 public static HtmlPage submitLeaveCalendar(WebClient webClient, String baseUrl, LeaveCalendarWSForm tdaf) {
191
192
193
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
209
210
211
212
213 public static HtmlPage submitLeaveCalendar2(WebClient webClient, String baseUrl, LeaveCalendarWSForm tdaf) {
214
215
216
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
267 if (tdaf.getLeaveAmount() != null) {
268 builder.append("&leaveAmount=").append(URLEncoder.encode(tdaf.getLeaveAmount().toString(), "UTF-8"));
269
270
271
272
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
279
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
295 } catch (Exception e) {
296 LOG.error("Exception building Post String", e);
297 }
298
299 return builder.toString();
300 }
301 }