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