1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.lm.leaveCalendar;
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.util.HtmlUnitUtil;
25 import org.kuali.kpme.core.api.assignment.Assignment;
26 import org.kuali.kpme.core.api.assignment.AssignmentDescriptionKey;
27 import org.kuali.kpme.core.api.earncode.EarnCode;
28 import org.kuali.kpme.core.util.HrConstants;
29 import org.kuali.kpme.core.util.TKUtils;
30 import org.kuali.kpme.tklm.leave.calendar.LeaveCalendarDocument;
31 import org.kuali.kpme.tklm.leave.calendar.web.LeaveCalendarWSForm;
32 import org.kuali.kpme.tklm.leave.summary.LeaveSummary;
33
34 import java.math.BigDecimal;
35 import java.net.URLEncoder;
36
37 public class LeaveCalendarTestUtils {
38
39 private static final Logger LOG = Logger.getLogger(LeaveCalendarTestUtils.class);
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public static LeaveCalendarWSForm buildLeaveCalendarForm(LeaveCalendarDocument leaveCalendarDocument, Assignment assignment, EarnCode earnCode, DateTime start, DateTime end, BigDecimal amount, boolean spanningWeeks) {
56 LeaveCalendarWSForm lcf = new LeaveCalendarWSForm();
57
58 BigDecimal hours = null;
59 String startTimeS = null;
60 String endTimeS = null;
61 String startDateS;
62 String endDateS;
63 String selectedEarnCode;
64 String selectedAssignment;
65
66 if (amount == null) {
67 if (start != null && end != null) {
68 Interval se_i = new Interval(start, end);
69 hours = TKUtils.convertMillisToHours(se_i.toDurationMillis());
70 }
71
72
73 startTimeS = start.toString("H:mm");
74 endTimeS = end.toString("H:mm");
75 } else {
76 hours = amount;
77 }
78
79 startDateS = start.toString("MM/dd/YYYY");
80 endDateS = end.toString("MM/dd/YYYY");
81
82 AssignmentDescriptionKey adk = new AssignmentDescriptionKey(assignment);
83 selectedAssignment = adk.toAssignmentKeyString();
84
85 selectedEarnCode = earnCode.getEarnCode();
86
87
88
89 lcf.setSpanningWeeks(spanningWeeks ? "y" : "n");
90
91 lcf.setLeaveAmount(hours);
92
93 lcf.setStartDate(startDateS);
94 lcf.setEndDate(endDateS);
95
96 lcf.setLeaveCalendarDocument(leaveCalendarDocument);
97 lcf.setSelectedAssignment(selectedAssignment);
98 lcf.setSelectedEarnCode(selectedEarnCode);
99 lcf.setMethodToCall("addLeaveBlock");
100
101 return lcf;
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 public static LeaveCalendarWSForm buildLeaveCalendarFormForSubmission(LeaveCalendarDocument leaveCalendarDocument, LeaveSummary leaveSummary) {
118 LeaveCalendarWSForm lcf = new LeaveCalendarWSForm();
119
120 lcf.setMethodToCall("approveLeaveCalendar");
121 lcf.setLeaveSummary(leaveSummary);
122
123 return lcf;
124 }
125
126
127
128
129
130
131
132
133
134 public static void setTimeBlockFormDetails(HtmlForm form, LeaveCalendarWSForm tdaf) {
135
136
137
138 form.setAttribute("startDate", tdaf.getStartDate());
139 form.setAttribute("endDate", tdaf.getEndDate());
140
141 if (tdaf.getLeaveAmount() != null) {
142 form.setAttribute("leaveAmount", tdaf.getLeaveAmount().toString());
143 }
144
145 form.setAttribute("selectedEarnCode", tdaf.getSelectedEarnCode());
146 form.setAttribute("selectedAssignment", tdaf.getSelectedAssignment());
147
148 form.setAttribute("methodToCall", tdaf.getMethodToCall());
149 }
150
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 public static HtmlPage submitLeaveCalendar(WebClient webClient, String baseUrl, LeaveCalendarWSForm tdaf) {
189
190
191
192
193 String url = baseUrl + buildPostFromFormParams(tdaf);
194 HtmlPage page = null;
195
196 try {
197 page = HtmlUnitUtil.gotoPageAndLogin(webClient, url);
198 } catch (Exception e) {
199 LOG.error("Error while submitting form", e);
200 }
201
202 return page;
203 }
204
205
206
207
208
209
210
211 public static HtmlPage submitLeaveCalendar2(WebClient webClient, String baseUrl, LeaveCalendarWSForm tdaf) {
212
213
214
215
216 String url = baseUrl + buildPostActionRequested(tdaf);
217
218 HtmlPage page = null;
219
220 try {
221 page = HtmlUnitUtil.gotoPageAndLogin(webClient, url);
222 } catch (Exception e) {
223 LOG.error("Error while submitting form", e);
224 }
225
226 return page;
227 }
228
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 private static String buildPostFromFormParams(LeaveCalendarWSForm tdaf) {
260 StringBuilder builder = new StringBuilder();
261
262 try {
263 builder.append("&methodToCall=").append(URLEncoder.encode(tdaf.getMethodToCall(), "UTF-8"));
264
265 if (tdaf.getLeaveAmount() != null) {
266 builder.append("&leaveAmount=").append(URLEncoder.encode(tdaf.getLeaveAmount().toString(), "UTF-8"));
267
268
269
270
271 }
272 builder.append("&startDate=").append(URLEncoder.encode(tdaf.getStartDate(), "UTF-8"));
273 builder.append("&endDate=").append(URLEncoder.encode(tdaf.getEndDate(), "UTF-8"));
274 builder.append("&selectedAssignment=").append(URLEncoder.encode(tdaf.getSelectedAssignment(), "UTF-8"));
275 builder.append("&selectedEarnCode=").append(URLEncoder.encode(tdaf.getSelectedEarnCode(), "UTF-8"));
276
277
278
279 } catch (Exception e) {
280 LOG.error("Exception building Post String", e);
281 }
282
283 return builder.toString();
284 }
285
286 private static String buildPostActionRequested(LeaveCalendarWSForm tdaf) {
287 StringBuilder builder = new StringBuilder();
288
289 try {
290 builder.append("&action=").append(URLEncoder.encode(HrConstants.DOCUMENT_ACTIONS.ROUTE,"UTF-8"));
291 builder.append("&methodToCall=").append(URLEncoder.encode(tdaf.getMethodToCall(), "UTF-8"));
292
293 } catch (Exception e) {
294 LOG.error("Exception building Post String", e);
295 }
296
297 return builder.toString();
298 }
299 }