1 | package edu.iu.uis.sit.portal.portlet.admin.web; |
2 | |
3 | import java.sql.Timestamp; |
4 | import java.util.ArrayList; |
5 | import java.util.Iterator; |
6 | import java.util.List; |
7 | import java.util.Map; |
8 | |
9 | import org.springframework.mock.web.portlet.MockPortletRequest; |
10 | import org.springframework.mock.web.portlet.MockRenderRequest; |
11 | import org.springframework.mock.web.portlet.MockRenderResponse; |
12 | import org.springframework.validation.BindException; |
13 | import org.springframework.validation.Errors; |
14 | import org.springframework.web.portlet.ModelAndView; |
15 | |
16 | import edu.iu.uis.sit.portal.PersistenceBrokerAbstractTransactionalDataSourceSpringContextTests; |
17 | import edu.iu.uis.sit.portal.portlet.admin.domain.PortalConfig; |
18 | import edu.iu.uis.sit.portal.portlet.admin.domain.PortalSupport; |
19 | import edu.iu.uis.sit.portal.portlet.admin.domain.PortletApplication; |
20 | import edu.iu.uis.sit.portal.portlet.admin.domain.PortletConfig; |
21 | import edu.iu.uis.sit.portal.portlet.admin.services.AdminService; |
22 | import edu.iu.uis.sit.portal.portlet.utility.Constants; |
23 | import edu.iu.uis.sit.portal.user.User; |
24 | import edu.iu.uis.sit.portal.user.domain.OneStartUser; |
25 | import edu.iu.uis.sit.portal.user.service.UserService; |
26 | |
27 | public class AllPortletConfigControllersTest extends PersistenceBrokerAbstractTransactionalDataSourceSpringContextTests { |
28 | |
29 | private AdminService adminService; |
30 | |
31 | private UserService userService; |
32 | |
33 | private PortletConfigValidator portletConfigValidator; |
34 | |
35 | private PortletConfigViewController portletConfigViewController; |
36 | |
37 | private PortletConfigDeleteController portletConfigDeleteController; |
38 | |
39 | private PortletConfigController portletConfigController; |
40 | |
41 | private OneStartUser oneStartUser; |
42 | |
43 | private PortalConfig portalConfig; |
44 | |
45 | private PortletConfig portletConfig; |
46 | |
47 | private PortletApplication portletApplication; |
48 | |
49 | protected void onSetUpInTransaction() throws Exception { |
50 | OneStartUser oneStartUser = new OneStartUser(); |
51 | oneStartUser.setEmplId("01234567890"); |
52 | oneStartUser.setUserName("jdoe"); |
53 | oneStartUser.setLastLoginDate(new Timestamp(System.currentTimeMillis())); |
54 | getUserService().saveOneStartUser(oneStartUser); |
55 | this.oneStartUser = oneStartUser; |
56 | |
57 | PortalConfig portalConfig = getAdminService().getPortalConfig(); |
58 | if (portalConfig == null) { |
59 | portalConfig = new PortalConfig(); |
60 | portalConfig.setPortalVersion("1.1.0-BETA1"); |
61 | portalConfig.setContainerName("Pluto Portal Driver"); |
62 | portalConfig.setPortalName("pluto-portal-driver"); |
63 | |
64 | List supports = new ArrayList(); |
65 | PortalSupport support = new PortalSupport(); |
66 | support.setPortalConfig(portalConfig); |
67 | support.setSupportName("view"); |
68 | support.setSupportType(Constants.PORTLET_MODE_SUPPORT); |
69 | supports.add(support); |
70 | |
71 | support = new PortalSupport(); |
72 | support.setPortalConfig(portalConfig); |
73 | support.setSupportName("edit"); |
74 | support.setSupportType(Constants.PORTLET_MODE_SUPPORT); |
75 | supports.add(support); |
76 | |
77 | support = new PortalSupport(); |
78 | support.setPortalConfig(portalConfig); |
79 | support.setSupportName("help"); |
80 | support.setSupportType(Constants.PORTLET_MODE_SUPPORT); |
81 | supports.add(support); |
82 | |
83 | support = new PortalSupport(); |
84 | support.setPortalConfig(portalConfig); |
85 | support.setSupportName("config"); |
86 | support.setSupportType(Constants.PORTLET_MODE_SUPPORT); |
87 | supports.add(support); |
88 | |
89 | support = new PortalSupport(); |
90 | support.setPortalConfig(portalConfig); |
91 | support.setSupportName("normal"); |
92 | support.setSupportType(Constants.WINDOW_STATE_SUPPORT); |
93 | supports.add(support); |
94 | |
95 | support = new PortalSupport(); |
96 | support.setPortalConfig(portalConfig); |
97 | support.setSupportName("maximized"); |
98 | support.setSupportType(Constants.WINDOW_STATE_SUPPORT); |
99 | supports.add(support); |
100 | |
101 | support = new PortalSupport(); |
102 | support.setPortalConfig(portalConfig); |
103 | support.setSupportName("minimized"); |
104 | support.setSupportType(Constants.WINDOW_STATE_SUPPORT); |
105 | supports.add(support); |
106 | |
107 | List portletApplications = new ArrayList(); |
108 | PortletApplication application = new PortletApplication(); |
109 | application.setContextPath("/my-dev"); |
110 | |
111 | List portletConfigs = new ArrayList(); |
112 | PortletConfig config = new PortletConfig(); |
113 | config.setPortletApplication(application); |
114 | config.setPortletName("AdminPortlet"); |
115 | portletConfigs.add(config); |
116 | |
117 | config = new PortletConfig(); |
118 | config.setPortletApplication(application); |
119 | config.setPortletName("PublishingPortlet"); |
120 | portletConfigs.add(config); |
121 | |
122 | config = new PortletConfig(); |
123 | config.setPortletApplication(application); |
124 | config.setPortletName("GroupsPortlet"); |
125 | portletConfigs.add(config); |
126 | |
127 | config = new PortletConfig(); |
128 | config.setPortletApplication(application); |
129 | config.setPortletName("ExamplePortlet"); |
130 | portletConfigs.add(config); |
131 | |
132 | application.setPortletConfigs(portletConfigs); |
133 | portletApplications.add(application); |
134 | |
135 | application = new PortletApplication(); |
136 | application.setContextPath("/onestartportlets"); |
137 | |
138 | portletConfigs = new ArrayList(); |
139 | config = new PortletConfig(); |
140 | config.setPortletApplication(application); |
141 | config.setPortletName("IframePortlet"); |
142 | portletConfigs.add(config); |
143 | |
144 | config = new PortletConfig(); |
145 | config.setPortletApplication(application); |
146 | config.setPortletName("RSSPortlet"); |
147 | portletConfigs.add(config); |
148 | |
149 | application.setPortletConfigs(portletConfigs); |
150 | portletApplications.add(application); |
151 | |
152 | application = new PortletApplication(); |
153 | application.setContextPath("/ccf-dev"); |
154 | |
155 | portletConfigs = new ArrayList(); |
156 | config = new PortletConfig(); |
157 | config.setPortletApplication(application); |
158 | config.setPortletName("ClassifiedsPortlet"); |
159 | portletConfigs.add(config); |
160 | |
161 | application.setPortletConfigs(portletConfigs); |
162 | portletApplications.add(application); |
163 | |
164 | application = new PortletApplication(); |
165 | application.setContextPath("/SpringPortlets"); |
166 | |
167 | portletConfigs = new ArrayList(); |
168 | config = new PortletConfig(); |
169 | config.setPortletApplication(application); |
170 | config.setPortletName("books"); |
171 | portletConfigs.add(config); |
172 | |
173 | portletConfigs = new ArrayList(); |
174 | config = new PortletConfig(); |
175 | config.setPortletApplication(application); |
176 | config.setPortletName("exceptions"); |
177 | portletConfigs.add(config); |
178 | |
179 | portletConfigs = new ArrayList(); |
180 | config = new PortletConfig(); |
181 | config.setPortletApplication(application); |
182 | config.setPortletName("mode"); |
183 | portletConfigs.add(config); |
184 | |
185 | portletConfigs = new ArrayList(); |
186 | config = new PortletConfig(); |
187 | config.setPortletApplication(application); |
188 | config.setPortletName("redirect"); |
189 | portletConfigs.add(config); |
190 | |
191 | portletConfigs = new ArrayList(); |
192 | config = new PortletConfig(); |
193 | config.setPortletApplication(application); |
194 | config.setPortletName("upload"); |
195 | portletConfigs.add(config); |
196 | |
197 | portletConfigs = new ArrayList(); |
198 | config = new PortletConfig(); |
199 | config.setPortletApplication(application); |
200 | config.setPortletName("helloworld"); |
201 | portletConfigs.add(config); |
202 | |
203 | application.setPortletConfigs(portletConfigs); |
204 | portletApplications.add(application); |
205 | |
206 | getAdminService().savePortalConfig(portalConfig); |
207 | } |
208 | PortletApplication application2 = new PortletApplication(); |
209 | application2.setContextPath("/testing"); |
210 | application2.setPortalConfig(portalConfig); |
211 | |
212 | List portletConfigs = new ArrayList(); |
213 | PortletConfig portletConfig = new PortletConfig(); |
214 | portletConfig.setPortletName("testing"); |
215 | portletConfig.setPortletApplication(application2); |
216 | |
217 | portletConfigs.add(portletConfig); |
218 | application2.setPortletConfigs(portletConfigs); |
219 | |
220 | getAdminService().savePortletApplication(portalConfig, application2); |
221 | |
222 | this.portletConfig = portletConfig; |
223 | this.portalConfig = portalConfig; |
224 | this.portletApplication = application2; |
225 | } |
226 | |
227 | private User getUser() { |
228 | String userName = "jdoe"; |
229 | String firstName = "John"; |
230 | String lastName = "Doe"; |
231 | String emailAddress = "jdoe@university.edu"; |
232 | String campusCode = "BL"; |
233 | List groups = new ArrayList(); |
234 | groups.add("group1"); |
235 | groups.add("group2"); |
236 | groups.add("group3"); |
237 | |
238 | OneStartUser oneStartUser = getUserService().findOneStartUserByUserName(userName); |
239 | return new User(oneStartUser, lastName, firstName, emailAddress, campusCode, groups); |
240 | } |
241 | |
242 | public void testPortletConfigValidatorValidate() { |
243 | try { |
244 | WebPortletConfig webPortletConfig = new WebPortletConfig(); |
245 | Errors errors = new BindException(webPortletConfig, "webPortletConfig"); |
246 | getPortletConfigValidator().validate(webPortletConfig, errors); |
247 | assertTrue("There should be errors from the validation. Required field portlet name.", errors.hasErrors()); |
248 | |
249 | } catch (Exception e) { |
250 | assertTrue("An error occured. " + e.toString(), false); |
251 | } |
252 | } |
253 | |
254 | public void testPortletConfigOnSubmitAction() { |
255 | try { |
256 | WebPortletConfig webPortletConfig = new WebPortletConfig(); |
257 | portletConfig.setPortletName("change"); |
258 | webPortletConfig.setPortletConfig(portletConfig); |
259 | webPortletConfig.setPortletApplication(portletApplication); |
260 | getPortletConfigController().onSubmitAction(webPortletConfig); |
261 | assertEquals("Portlet config should have been updated.", "change", getAdminService().findPortletConfigById(portletConfig.getPortletConfigId()).getPortletName()); |
262 | } catch (Exception e) { |
263 | assertTrue("An error occured. " + e.toString(), false); |
264 | } |
265 | } |
266 | |
267 | public void testPortletConfigOnSubmitRender() { |
268 | try { |
269 | WebPortletConfig webPortletConfig = new WebPortletConfig(portletConfig); |
270 | webPortletConfig.setPortletApplication(portletApplication); |
271 | ModelAndView modelAndView = getPortletConfigController().onSubmitRender(webPortletConfig); |
272 | |
273 | assertNotNull("ModelAndView does not contain content.", modelAndView.getModel()); |
274 | if (modelAndView.getModel() != null) { |
275 | assertTrue("ModelAndView does not contain portlet configs.", modelAndView.getModel().containsKey("portletConfigs")); |
276 | assertTrue("ModelAndView does not contain a portlet application id.", modelAndView.getModel().containsKey("portletApplicationId")); |
277 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
278 | Map.Entry entry = (Map.Entry) iter.next(); |
279 | if (entry.getKey().equals("portletConfigs")) { |
280 | assertTrue("There should be at least 1 portlet config.", ((List) entry.getValue()).size() > 0); |
281 | } |
282 | if (entry.getKey().equals("portletApplicationId")) { |
283 | assertEquals("Portlet Application id is incorrect.", portletApplication.getPortletApplicationId(), entry.getValue()); |
284 | } |
285 | } |
286 | } |
287 | } catch (Exception e) { |
288 | assertTrue("An error occured. " + e.toString(), false); |
289 | } |
290 | } |
291 | |
292 | public void testPortletConfigFormBackingObject() { |
293 | MockPortletRequest portletRequest = new MockPortletRequest(); |
294 | |
295 | try { |
296 | portletRequest.addParameter("portletApplicationId", portletApplication.getPortletApplicationId().toString()); |
297 | Object object = getPortletConfigController().formBackingObject(portletRequest); |
298 | assertTrue("Command object should be of type WebPortletConfig.", object instanceof WebPortletConfig); |
299 | |
300 | WebPortletConfig webPortletConfig = (WebPortletConfig) object; |
301 | assertNotNull("WebPortletConfig should be present.", webPortletConfig); |
302 | assertNull("Portlet Config should be empty because we are creating a new one.", webPortletConfig.getPortletConfig().getPortletName()); |
303 | assertNotNull("Portlet Application should be present.", webPortletConfig.getPortletApplication()); |
304 | assertNotNull("Portlet Application id should be present.", webPortletConfig.getPortletApplication().getPortletApplicationId()); |
305 | |
306 | portletRequest.addParameter("portletConfigId", portletConfig.getPortletConfigId().toString()); |
307 | object = getPortletConfigController().formBackingObject(portletRequest); |
308 | |
309 | webPortletConfig = (WebPortletConfig) object; |
310 | assertEquals("Portlet Config context should be testing.", "testing", webPortletConfig.getPortletConfig().getPortletName()); |
311 | assertEquals("Portlet Application context should be /testing.", "/testing", webPortletConfig.getPortletApplication().getContextPath()); |
312 | } catch (Exception e) { |
313 | assertTrue("An error occured. " + e.toString(), false); |
314 | } |
315 | } |
316 | |
317 | public void testPortletConfigDeleteOnSubmitRender() { |
318 | try { |
319 | ModelAndView modelAndView = getPortletConfigDeleteController().onSubmitRender(portletConfig); |
320 | |
321 | assertNotNull("ModelAndView does not contain content.", modelAndView.getModel()); |
322 | if (modelAndView.getModel() != null) { |
323 | assertTrue("ModelAndView does not contain a portlet configs.", modelAndView.getModel().containsKey("portletConfigs")); |
324 | assertTrue("ModelAndView does not contain a portlet application id.", modelAndView.getModel().containsKey("portletApplicationId")); |
325 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
326 | Map.Entry entry = (Map.Entry) iter.next(); |
327 | if (entry.getKey().equals("portletApplicationId")) { |
328 | assertEquals("There should be a portletApplicationId.", portletApplication.getPortletApplicationId(), entry.getValue()); |
329 | } |
330 | } |
331 | } |
332 | } catch (Exception e) { |
333 | assertTrue("An error occured. " + e.toString(), false); |
334 | } |
335 | } |
336 | |
337 | public void testPortletConfigDeleteOnSubmitAction() { |
338 | try { |
339 | getPortletConfigDeleteController().onSubmitAction(portletConfig); |
340 | assertNull("Portlet Config should have been deleted.", getAdminService().findPortletConfigById(portletConfig.getPortletConfigId())); |
341 | } catch (Exception e) { |
342 | assertTrue("An error occured. " + e.toString(), false); |
343 | } |
344 | } |
345 | |
346 | public void testPortletConfigDeleteFormBackingObject() { |
347 | MockPortletRequest portletRequest = new MockPortletRequest(); |
348 | |
349 | try { |
350 | Object object = getPortletConfigDeleteController().formBackingObject(portletRequest); |
351 | assertTrue("Command object should be of type PortletConfig.", object instanceof PortletConfig); |
352 | |
353 | PortletConfig portletConfig = (PortletConfig) object; |
354 | assertNull("Portlet config should be a new empty object.", portletConfig.getPortletConfigId()); |
355 | |
356 | portletRequest.addParameter("portletConfigId", this.portletConfig.getPortletConfigId().toString()); |
357 | |
358 | object = getPortletConfigDeleteController().formBackingObject(portletRequest); |
359 | assertTrue("Command object should be of type PortletConfig.", object instanceof PortletConfig); |
360 | |
361 | PortletConfig found = (PortletConfig) object; |
362 | assertEquals("Portlet Config id is incorrect.", this.portletConfig.getPortletConfigId(), found.getPortletConfigId()); |
363 | |
364 | } catch (Exception e) { |
365 | assertTrue("An error occured. " + e.toString(), false); |
366 | } |
367 | } |
368 | |
369 | public void testPortletConfigViewHandleRenderRequestInternal() { |
370 | MockRenderRequest renderRequest = new MockRenderRequest(); |
371 | renderRequest.addParameter("portletApplicationId", portletApplication.getPortletApplicationId().toString()); |
372 | MockRenderResponse renderResponse = new MockRenderResponse(); |
373 | try { |
374 | ModelAndView modelAndView = getPortletConfigViewController().handleRenderRequestInternal(renderRequest, renderResponse); |
375 | |
376 | assertNotNull("ModelAndView does not contain content.", modelAndView.getModel()); |
377 | if (modelAndView.getModel() != null) { |
378 | assertTrue("ModelAndView does not contain portlet configs.", modelAndView.getModel().containsKey("portletConfigs")); |
379 | assertTrue("ModelAndView does not contain a portlet application id.", modelAndView.getModel().containsKey("portletApplicationId")); |
380 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
381 | Map.Entry entry = (Map.Entry) iter.next(); |
382 | if (entry.getKey().equals("portletConfigs")) { |
383 | assertTrue("There should be at least 1 portlet configs.", ((List) entry.getValue()).size() > 0); |
384 | } |
385 | } |
386 | } |
387 | } catch (Exception e) { |
388 | assertTrue("An error occured. " + e.toString(), false); |
389 | } |
390 | } |
391 | |
392 | public OneStartUser getOneStartUser() { |
393 | return oneStartUser; |
394 | } |
395 | |
396 | public UserService getUserService() { |
397 | return userService; |
398 | } |
399 | |
400 | public void setUserService(UserService userService) { |
401 | this.userService = userService; |
402 | } |
403 | |
404 | public AdminService getAdminService() { |
405 | return adminService; |
406 | } |
407 | |
408 | public void setAdminService(AdminService adminService) { |
409 | this.adminService = adminService; |
410 | } |
411 | |
412 | public PortletConfigController getPortletConfigController() { |
413 | return portletConfigController; |
414 | } |
415 | |
416 | public void setPortletConfigController(PortletConfigController portletConfigController) { |
417 | this.portletConfigController = portletConfigController; |
418 | } |
419 | |
420 | public PortletConfigDeleteController getPortletConfigDeleteController() { |
421 | return portletConfigDeleteController; |
422 | } |
423 | |
424 | public void setPortletConfigDeleteController(PortletConfigDeleteController portletConfigDeleteController) { |
425 | this.portletConfigDeleteController = portletConfigDeleteController; |
426 | } |
427 | |
428 | public PortletConfigValidator getPortletConfigValidator() { |
429 | return portletConfigValidator; |
430 | } |
431 | |
432 | public void setPortletConfigValidator(PortletConfigValidator portletConfigValidator) { |
433 | this.portletConfigValidator = portletConfigValidator; |
434 | } |
435 | |
436 | public PortletConfigViewController getPortletConfigViewController() { |
437 | return portletConfigViewController; |
438 | } |
439 | |
440 | public void setPortletConfigViewController(PortletConfigViewController portletConfigViewController) { |
441 | this.portletConfigViewController = portletConfigViewController; |
442 | } |
443 | |
444 | } |