1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.util;
17
18 import java.math.BigDecimal;
19 import java.net.URLEncoder;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.log4j.Logger;
24 import org.joda.time.DateTime;
25 import org.joda.time.Interval;
26 import org.kuali.hr.TestAutoLoginFilter;
27 import org.kuali.hr.util.HtmlUnitUtil;
28 import org.kuali.kpme.core.assignment.Assignment;
29 import org.kuali.kpme.core.assignment.AssignmentDescriptionKey;
30 import org.kuali.kpme.core.earncode.EarnCode;
31 import org.kuali.kpme.core.util.TKUtils;
32 import org.kuali.kpme.tklm.time.detail.validation.TimeDetailValidationUtil;
33 import org.kuali.kpme.tklm.time.detail.web.TimeDetailActionFormBase;
34 import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
35
36 import com.gargoylesoftware.htmlunit.WebClient;
37 import com.gargoylesoftware.htmlunit.html.HtmlButton;
38 import com.gargoylesoftware.htmlunit.html.HtmlForm;
39 import com.gargoylesoftware.htmlunit.html.HtmlPage;
40
41 public class TimeDetailTestUtils {
42
43 private static final Logger LOG = Logger.getLogger(TimeDetailTestUtils.class);
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public static TimeDetailActionFormBase buildDetailActionForm(TimesheetDocument timeshetDocument, Assignment assignment, EarnCode earnCode, DateTime start, DateTime end, BigDecimal amount, boolean acrossDays, String timeblockId, boolean spanningWeeks) {
61 TimeDetailActionFormBase tdaf = new TimeDetailActionFormBase();
62
63
64
65
66
67 BigDecimal hours = null;
68 String startTimeS = null;
69 String endTimeS = null;
70 String startDateS;
71 String endDateS;
72 String selectedEarnCode;
73 String selectedAssignment;
74
75 if (hours == null) {
76 if (start != null && end != null) {
77 Interval se_i = new Interval(start, end);
78 hours = TKUtils.convertMillisToHours(se_i.toDurationMillis());
79 }
80
81
82 startTimeS = start.toString("H:mm");
83 endTimeS = end.toString("H:mm");
84 }
85
86 startDateS = start.toString("MM/dd/YYYY");
87 endDateS = end.toString("MM/dd/YYYY");
88
89 AssignmentDescriptionKey adk = new AssignmentDescriptionKey(assignment);
90 selectedAssignment = adk.toAssignmentKeyString();
91
92 selectedEarnCode = earnCode.getEarnCode();
93
94 tdaf.setAcrossDays(acrossDays ? "y" : "n");
95
96 tdaf.setAmount(amount);
97 tdaf.setHours(hours);
98 tdaf.setStartTime(startTimeS);
99 tdaf.setEndTime(endTimeS);
100 tdaf.setStartDate(startDateS);
101 tdaf.setEndDate(endDateS);
102 tdaf.setTkTimeBlockId(timeblockId);
103 tdaf.setTimesheetDocument(timeshetDocument);
104 tdaf.setSelectedAssignment(selectedAssignment);
105 tdaf.setSelectedEarnCode(selectedEarnCode);
106
107 tdaf.setMethodToCall("addTimeBlock");
108
109 return tdaf;
110 }
111
112
113
114
115
116
117
118
119
120
121 public static List<String> setTimeBlockFormDetails(HtmlForm form, TimeDetailActionFormBase tdaf) {
122
123 List<String> errors = TimeDetailValidationUtil.validateTimeEntryDetails(tdaf);
124
125
126 if (errors.size() == 0) {
127 if (tdaf.getTkTimeBlockId() != null) {
128 form.setAttribute("tkTimeBlockId", tdaf.getTkTimeBlockId().toString());
129 }
130 form.setAttribute("startDate", tdaf.getStartDate());
131 form.setAttribute("endDate", tdaf.getEndDate());
132
133 if (tdaf.getAmount() != null) {
134 form.setAttribute("amount", tdaf.getAmount().toString());
135 } else {
136 form.setAttribute("startTime", tdaf.getStartTime());
137 form.setAttribute("endTime", tdaf.getEndTime());
138 form.setAttribute("hours", tdaf.getHours().toString());
139 }
140
141 form.setAttribute("selectedEarnCode", tdaf.getSelectedEarnCode());
142 form.setAttribute("selectedAssignment", tdaf.getSelectedAssignment());
143 form.setAttribute("acrossDays", tdaf.getAcrossDays());
144 form.setAttribute("methodToCall", tdaf.getMethodToCall());
145
146 }
147
148 return errors;
149 }
150
151
152
153
154
155
156
157
158
159
160 private static HtmlPage submitTimeDetailsDep(HtmlPage page, HtmlForm form) {
161 HtmlButton submitButton = null;
162
163
164
165 if (submitButton == null) {
166 submitButton = (HtmlButton)page.createElement("button");
167 submitButton.setAttribute("type", "submit");
168 form.appendChild(submitButton);
169 }
170
171 HtmlPage newPage = null;
172 try {
173 submitButton.click();
174 } catch (Exception e) {
175 LOG.error("While submitting time detail form", e);
176 }
177
178 return newPage;
179 }
180
181
182
183
184
185
186
187
188 public static HtmlPage submitTimeDetails(WebClient webClient, String baseUrl, TimeDetailActionFormBase 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 submitTimeDetails(WebClient webClient, String principalId, String baseUrl, TimeDetailActionFormBase tdaf) {
212
213
214
215
216 String url = baseUrl + buildPostFromFormParams(tdaf);
217 HtmlPage page = null;
218
219 try {
220 TestAutoLoginFilter.OVERRIDE_ID = principalId;
221 page = HtmlUnitUtil.gotoPageAndLogin(webClient, url);
222 TestAutoLoginFilter.OVERRIDE_ID = "";
223 } catch (Exception e) {
224 LOG.error("Error while submitting form", e);
225 }
226
227 return page;
228 }
229
230 private static String buildPostFromFormParams(TimeDetailActionFormBase tdaf) {
231 StringBuilder builder = new StringBuilder();
232
233 try {
234 builder.append("&methodToCall=").append(URLEncoder.encode(tdaf.getMethodToCall(), "UTF-8"));
235 builder.append("&acrossDays=").append(URLEncoder.encode(tdaf.getAcrossDays(), "UTF-8"));
236 if (tdaf.getAmount() != null) {
237 builder.append("&amount=").append(URLEncoder.encode(tdaf.getAmount().toString(), "UTF-8"));
238 } else {
239 builder.append("&hours=").append(URLEncoder.encode(tdaf.getHours().toString(), "UTF-8"));
240 builder.append("&startTime=").append(URLEncoder.encode(tdaf.getStartTime(), "UTF-8"));
241 builder.append("&endTime=").append(URLEncoder.encode(tdaf.getEndTime(), "UTF-8"));
242 }
243 builder.append("&startDate=").append(URLEncoder.encode(tdaf.getStartDate(), "UTF-8"));
244 builder.append("&endDate=").append(URLEncoder.encode(tdaf.getEndDate(), "UTF-8"));
245 builder.append("&selectedAssignment=").append(URLEncoder.encode(tdaf.getSelectedAssignment(), "UTF-8"));
246 builder.append("&selectedEarnCode=").append(URLEncoder.encode(tdaf.getSelectedEarnCode(), "UTF-8"));
247
248 if (tdaf.getTkTimeBlockId() != null) {
249 builder.append("&tkTimeBlockId=").append(URLEncoder.encode(tdaf.getTkTimeBlockId().toString(), "UTF-8"));
250 }
251 } catch (Exception e) {
252 LOG.error("Exception building Post String", e);
253 }
254
255 return builder.toString();
256 }
257 }