1 | package edu.iu.uis.sit.portal.portlet.publishing.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 javax.portlet.PortletSession; |
10 | |
11 | import org.springframework.mock.web.portlet.MockActionRequest; |
12 | import org.springframework.mock.web.portlet.MockActionResponse; |
13 | import org.springframework.mock.web.portlet.MockPortletRequest; |
14 | import org.springframework.mock.web.portlet.MockRenderRequest; |
15 | import org.springframework.mock.web.portlet.MockRenderResponse; |
16 | import org.springframework.validation.BindException; |
17 | import org.springframework.validation.Errors; |
18 | import org.springframework.web.portlet.ModelAndView; |
19 | |
20 | import edu.iu.uis.sit.portal.PersistenceBrokerAbstractTransactionalDataSourceSpringContextTests; |
21 | import edu.iu.uis.sit.portal.portlet.publishing.domain.Acl; |
22 | import edu.iu.uis.sit.portal.portlet.publishing.domain.Fragment; |
23 | import edu.iu.uis.sit.portal.portlet.publishing.domain.Tab; |
24 | import edu.iu.uis.sit.portal.portlet.publishing.services.PublishingService; |
25 | import edu.iu.uis.sit.portal.portlet.utility.Constants; |
26 | import edu.iu.uis.sit.portal.user.User; |
27 | import edu.iu.uis.sit.portal.user.domain.OneStartUser; |
28 | import edu.iu.uis.sit.portal.user.service.UserService; |
29 | |
30 | public class AllTabControllersTest extends PersistenceBrokerAbstractTransactionalDataSourceSpringContextTests { |
31 | |
32 | private PublishingService publishingService; |
33 | |
34 | private UserService userService; |
35 | |
36 | private TabDeleteController tabDeleteController; |
37 | |
38 | private TabsViewController tabsViewController; |
39 | |
40 | private TabWizardController tabWizardController; |
41 | |
42 | private OneStartUser oneStartUser; |
43 | |
44 | private Tab tab; |
45 | |
46 | // TODO when acls are added for fragments, the user must have a group in his user object equal to a group in each fragment. |
47 | protected void onSetUpInTransaction() throws Exception { |
48 | OneStartUser oneStartUser = new OneStartUser(); |
49 | oneStartUser.setEmplId("01234567890"); |
50 | oneStartUser.setUserName("jdoe"); |
51 | oneStartUser.setLastLoginDate(new Timestamp(System.currentTimeMillis())); |
52 | getUserService().saveOneStartUser(oneStartUser); |
53 | this.oneStartUser = oneStartUser; |
54 | |
55 | Tab tab = new Tab(); |
56 | tab.setActive(true); |
57 | tab.setContactEmail("admin@university.edu"); |
58 | tab.setCreateDate(new Timestamp(System.currentTimeMillis())); |
59 | tab.setDescription("my description"); |
60 | tab.setLastUpdateDate(new Timestamp(System.currentTimeMillis())); |
61 | tab.setName("My Tab"); |
62 | |
63 | Acl acl = new Acl(); |
64 | acl.setTab(tab); |
65 | acl.setKey(Constants.GROUP_KEY); |
66 | acl.setType(Constants.GROUP_PUBLISH); |
67 | acl.setValue("group1"); |
68 | |
69 | Acl acl2 = new Acl(); |
70 | acl2.setTab(tab); |
71 | acl2.setKey(Constants.GROUP_KEY); |
72 | acl2.setType(Constants.GROUP_VIEW); |
73 | acl2.setValue("group2"); |
74 | |
75 | Acl acl3 = new Acl(); |
76 | acl3.setTab(tab); |
77 | acl3.setKey("rolekey"); |
78 | acl3.setType(Constants.ROLE_VIEW); |
79 | acl3.setValue("role1"); |
80 | |
81 | List acls = new ArrayList(); |
82 | acls.add(acl); |
83 | acls.add(acl2); |
84 | acls.add(acl3); |
85 | |
86 | tab.setAcls(acls); |
87 | |
88 | getPublishingService().saveTab(tab); |
89 | this.tab = tab; |
90 | |
91 | Tab tab2 = new Tab(); |
92 | tab2.setActive(true); |
93 | tab2.setContactEmail("admin@university.edu"); |
94 | tab2.setCreateDate(new Timestamp(System.currentTimeMillis())); |
95 | tab2.setDescription("my 2nd description"); |
96 | tab2.setLastUpdateDate(new Timestamp(System.currentTimeMillis())); |
97 | tab2.setName("My 2nd Tab"); |
98 | getPublishingService().saveTab(tab2); |
99 | } |
100 | |
101 | private User getUser() { |
102 | String userName = "jdoe"; |
103 | String firstName = "John"; |
104 | String lastName = "Doe"; |
105 | String emailAddress = "jdoe@university.edu"; |
106 | String campusCode = "BL"; |
107 | List groups = new ArrayList(); |
108 | groups.add("group1"); |
109 | groups.add("group2"); |
110 | groups.add("group3"); |
111 | |
112 | OneStartUser oneStartUser = getUserService().findOneStartUserByUserName(userName); |
113 | return new User(oneStartUser, lastName, firstName, emailAddress, campusCode, groups); |
114 | } |
115 | |
116 | public void testTabWizardHandleActionRequestInternal() { |
117 | User user = getUser(); |
118 | |
119 | MockActionRequest actionRequest = new MockActionRequest(); |
120 | actionRequest.getPortletSession(true).setAttribute(User.USER_KEY, user, PortletSession.APPLICATION_SCOPE); |
121 | actionRequest.addParameter("tabId", tab.getTabId().toString()); |
122 | actionRequest.addParameter("addPublishGroup", "true"); |
123 | actionRequest.addParameter("addViewGroup", "true"); |
124 | actionRequest.addParameter("addViewRole", "true"); |
125 | actionRequest.addParameter("_page", "0"); |
126 | |
127 | for (Iterator iter = tab.getAcls().iterator(); iter.hasNext();) { |
128 | Acl acl = (Acl) iter.next(); |
129 | actionRequest.addParameter("removeAclValue", acl.getValue()); |
130 | actionRequest.addParameter("removeAclKey", acl.getKey()); |
131 | break; |
132 | } |
133 | |
134 | MockActionResponse actionResponse = new MockActionResponse(); |
135 | try { |
136 | TabWizardController tabWizardController = getTabWizardController(); |
137 | WebTab webTab = new WebTab(); |
138 | webTab.setTab(getPublishingService().findTabById(tab.getTabId())); |
139 | actionRequest.getPortletSession(true).setAttribute("edu.iu.uis.sit.portal.portlet.publishing.web.TabWizardController.FORM.webTab", webTab); |
140 | |
141 | tabWizardController.handleActionRequestInternal(actionRequest, actionResponse); |
142 | |
143 | assertTrue("Publish group flag should be true.", webTab.isAddPublishGroup()); |
144 | assertTrue("View group flag should be true.", webTab.isAddViewGroup()); |
145 | assertTrue("View role flag should be true.", webTab.isAddViewRole()); |
146 | assertEquals("There should be 3 acls. No removal should have taken place because removeAclType should be missing.", 3, webTab.getTab().getAcls().size()); |
147 | |
148 | actionRequest.setParameter("removeAclKey", ""); |
149 | for (Iterator iter = tab.getAcls().iterator(); iter.hasNext();) { |
150 | Acl acl = (Acl) iter.next(); |
151 | actionRequest.setParameter("removeAclValue", acl.getValue()); |
152 | actionRequest.addParameter("removeAclType", acl.getType()); |
153 | break; |
154 | } |
155 | actionRequest.getPortletSession(true).setAttribute("edu.iu.uis.sit.portal.portlet.publishing.web.TabWizardController.FORM.webTab", webTab); |
156 | tabWizardController.handleActionRequestInternal(actionRequest, actionResponse); |
157 | assertEquals("There should be 3 acls. No removal should have taken place because removeAclKey should be missing.", 3, webTab.getTab().getAcls().size()); |
158 | |
159 | actionRequest.setParameter("removeAclValue", ""); |
160 | for (Iterator iter = tab.getAcls().iterator(); iter.hasNext();) { |
161 | Acl acl = (Acl) iter.next(); |
162 | actionRequest.setParameter("removeAclKey", acl.getKey()); |
163 | actionRequest.setParameter("removeAclType", acl.getType()); |
164 | break; |
165 | } |
166 | actionRequest.getPortletSession(true).setAttribute("edu.iu.uis.sit.portal.portlet.publishing.web.TabWizardController.FORM.webTab", webTab); |
167 | tabWizardController.handleActionRequestInternal(actionRequest, actionResponse); |
168 | assertEquals("There should be 3 acls. No removal should have taken place because removeAclValue should be missing.", 3, webTab.getTab().getAcls().size()); |
169 | |
170 | for (Iterator iter = tab.getAcls().iterator(); iter.hasNext();) { |
171 | Acl acl = (Acl) iter.next(); |
172 | actionRequest.setParameter("removeAclValue", acl.getValue()); |
173 | actionRequest.setParameter("removeAclKey", acl.getKey()); |
174 | actionRequest.setParameter("removeAclType", acl.getType()); |
175 | break; |
176 | } |
177 | actionRequest.getPortletSession(true).setAttribute("edu.iu.uis.sit.portal.portlet.publishing.web.TabWizardController.FORM.webTab", webTab); |
178 | tabWizardController.handleActionRequestInternal(actionRequest, actionResponse); |
179 | assertEquals("There should be 2 acls. One should have been removed.", 2, webTab.getTab().getAcls().size()); |
180 | |
181 | } catch (Exception e) { |
182 | assertTrue("An error occured. " + e.toString(), false); |
183 | } |
184 | } |
185 | |
186 | public void testTabWizardHandleRenderRequestInternal() { |
187 | User user = getUser(); |
188 | |
189 | MockRenderRequest renderRequest = new MockRenderRequest(); |
190 | renderRequest.getPortletSession(true).setAttribute(User.USER_KEY, user, PortletSession.APPLICATION_SCOPE); |
191 | renderRequest.addParameter("tabId", tab.getTabId().toString()); |
192 | MockRenderResponse renderResponse = new MockRenderResponse(); |
193 | try { |
194 | TabWizardController tabWizardController = getTabWizardController(); |
195 | WebTab webTab = new WebTab(); |
196 | webTab.setTab(tab); |
197 | renderRequest.getPortletSession(true).setAttribute("org.springframework.web.portlet.mvc.RenderCommand", webTab); |
198 | |
199 | ModelAndView modelAndView = tabWizardController.handleRenderRequestInternal(renderRequest, renderResponse); |
200 | |
201 | assertNotNull("ModelAndView does not contain content.", modelAndView.getModel()); |
202 | if (modelAndView.getModel() != null) { |
203 | assertTrue("ModelAndView does not contain roles for viewing.", modelAndView.getModel().containsKey("roleViews")); |
204 | assertTrue("ModelAndView does not contain groups for publishing.", modelAndView.getModel().containsKey("groupPublishs")); |
205 | assertTrue("ModelAndView does not contain groups for viewing.", modelAndView.getModel().containsKey("groupViews")); |
206 | |
207 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
208 | Map.Entry entry = (Map.Entry) iter.next(); |
209 | if (entry.getKey().equals("roleViews")) { |
210 | assertEquals("There should be one role for viewing.", 1, ((List) entry.getValue()).size()); |
211 | } |
212 | if (entry.getKey().equals("groupPublishs")) { |
213 | assertEquals("There should be one group for publishing.", 1, ((List) entry.getValue()).size()); |
214 | } |
215 | if (entry.getKey().equals("groupViews")) { |
216 | assertEquals("There should be one group for viewing.", 1, ((List) entry.getValue()).size()); |
217 | } |
218 | if (entry.getKey().equals("page")) { |
219 | assertEquals("Page should be 0.", 0, entry.getValue()); |
220 | } |
221 | } |
222 | } |
223 | assertEquals("View name should be tabPage1.", "tabPage1", modelAndView.getViewName()); |
224 | |
225 | renderRequest.addParameter("updateTab", "true"); |
226 | modelAndView = tabWizardController.handleRenderRequestInternal(renderRequest, renderResponse); |
227 | assertNotNull("ModelAndView does not contain content.", modelAndView.getModel()); |
228 | if (modelAndView.getModel() != null) { |
229 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
230 | Map.Entry entry = (Map.Entry) iter.next(); |
231 | |
232 | if (entry.getKey().equals("page")) { |
233 | assertEquals("Page should be 4.", 4, entry.getValue()); |
234 | } |
235 | } |
236 | } |
237 | assertEquals("View name should be tabPage4.", "tabPage4", modelAndView.getViewName()); |
238 | |
239 | } catch (Exception e) { |
240 | assertTrue("An error occured. " + e.toString(), false); |
241 | } |
242 | } |
243 | |
244 | public void testProcessFinish() { |
245 | User user = getUser(); |
246 | MockActionRequest actionRequest = new MockActionRequest(); |
247 | actionRequest.getPortletSession(true).setAttribute(User.USER_KEY, user, PortletSession.APPLICATION_SCOPE); |
248 | MockActionResponse actionResponse = new MockActionResponse(); |
249 | |
250 | try { |
251 | TabWizardController tabWizardController = getTabWizardController(); |
252 | WebTab webTab = new WebTab(); |
253 | |
254 | Tab tab = new Tab(); |
255 | tab.setActive(true); |
256 | tab.setContactEmail("admin@university.edu"); |
257 | tab.setCreateDate(new Timestamp(System.currentTimeMillis())); |
258 | tab.setDescription("my description 3"); |
259 | tab.setLastUpdateDate(new Timestamp(System.currentTimeMillis())); |
260 | tab.setName("My Tab 3"); |
261 | webTab.setTab(tab); |
262 | |
263 | tabWizardController.processFinish(actionRequest, actionResponse, webTab, null); |
264 | |
265 | Tab found = getPublishingService().findTabById(webTab.getTab().getTabId()); |
266 | assertNotNull("Tab did not save properly.", found); |
267 | } catch (Exception e) { |
268 | assertTrue("An error occured. " + e.toString(), false); |
269 | } |
270 | } |
271 | |
272 | public void testValidatePage() { |
273 | try { |
274 | TabWizardController tabWizardController = getTabWizardController(); |
275 | |
276 | WebTab webTab = new WebTab(); |
277 | Errors errors = new BindException(webTab, "webTab"); |
278 | int page = 0; |
279 | boolean finish = true; |
280 | tabWizardController.validatePage(webTab, errors, page, finish); |
281 | |
282 | assertTrue("There should be errors from the validation.", errors.hasErrors()); |
283 | } catch (Exception e) { |
284 | assertTrue("An error occured. " + e.toString(), false); |
285 | } |
286 | } |
287 | |
288 | public void testRenderFinish() { |
289 | User user = getUser(); |
290 | |
291 | MockRenderRequest renderRequest = new MockRenderRequest(); |
292 | renderRequest.getPortletSession(true).setAttribute(User.USER_KEY, user, PortletSession.APPLICATION_SCOPE); |
293 | MockRenderResponse renderResponse = new MockRenderResponse(); |
294 | try { |
295 | TabWizardController tabWizardController = getTabWizardController(); |
296 | WebTab webTab = new WebTab(); |
297 | webTab.getTab().setName("My tab"); |
298 | |
299 | ModelAndView modelAndView = tabWizardController.renderFinish(renderRequest, renderResponse, webTab, null); |
300 | |
301 | assertNotNull("ModelAndView does not contain content.", modelAndView.getModel()); |
302 | if (modelAndView.getModel() != null) { |
303 | assertTrue("ModelAndView does not contain a web tab.", modelAndView.getModel().containsKey("webTab")); |
304 | assertTrue("ModelAndView does not contain a page.", modelAndView.getModel().containsKey("page")); |
305 | |
306 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
307 | Map.Entry entry = (Map.Entry) iter.next(); |
308 | if (entry.getKey().equals("webTab")) { |
309 | assertEquals("Web tab should have a title of My tab.", "My tab", ((WebTab) entry.getValue()).getTab().getName()); |
310 | } |
311 | if (entry.getKey().equals("page")) { |
312 | assertEquals("This should return page 4.", 4, entry.getValue()); |
313 | } |
314 | } |
315 | } |
316 | renderRequest.addParameter("new", "new"); |
317 | ModelAndView modelAndView2 = tabWizardController.renderFinish(renderRequest, renderResponse, webTab, null); |
318 | |
319 | assertNotNull("ModelAndView does not contain content.", modelAndView2.getModel()); |
320 | if (modelAndView2.getModel() != null) { |
321 | assertTrue("ModelAndView does not contain a web tab.", modelAndView2.getModel().containsKey("message")); |
322 | } |
323 | } catch (Exception e) { |
324 | assertTrue("An error occured. " + e.toString(), false); |
325 | } |
326 | } |
327 | |
328 | public void testGetInitialPage() { |
329 | User user = getUser(); |
330 | |
331 | MockPortletRequest portletRequest = new MockPortletRequest(); |
332 | portletRequest.getPortletSession(true).setAttribute(User.USER_KEY, user, PortletSession.APPLICATION_SCOPE); |
333 | |
334 | try { |
335 | TabWizardController tabWizardController = getTabWizardController(); |
336 | WebTab webTab = new WebTab(); |
337 | int page = tabWizardController.getInitialPage(portletRequest, webTab); |
338 | |
339 | portletRequest.addParameter("tabId", tab.getTabId().toString()); |
340 | WebTab webTab2 = new WebTab(); |
341 | page = tabWizardController.getInitialPage(portletRequest, webTab2); |
342 | |
343 | assertEquals("WebTab was not properly initialized.", tab.getTabId(), webTab2.getTab().getTabId()); |
344 | } catch (Exception e) { |
345 | assertTrue("An error occured. " + e.getMessage(), false); |
346 | } |
347 | } |
348 | |
349 | public void testDeleteTab() { |
350 | User user = getUser(); |
351 | MockActionRequest actionRequest = new MockActionRequest(); |
352 | actionRequest.getPortletSession(true).setAttribute(User.USER_KEY, user, PortletSession.APPLICATION_SCOPE); |
353 | actionRequest.addParameter("tabId", tab.getTabId().toString()); |
354 | MockActionResponse actionResponse = new MockActionResponse(); |
355 | |
356 | MockRenderRequest renderRequest = new MockRenderRequest(); |
357 | renderRequest.getPortletSession(true).setAttribute(User.USER_KEY, user, PortletSession.APPLICATION_SCOPE); |
358 | MockRenderResponse renderResponse = new MockRenderResponse(); |
359 | |
360 | try { |
361 | TabDeleteController tabDeleteController = getTabDeleteController(); |
362 | tabDeleteController.deleteTab(actionRequest, actionResponse); |
363 | |
364 | assertNull("Tab should be null because it should have been deleted.", getPublishingService().findTabById(tab.getTabId())); |
365 | |
366 | ModelAndView modelAndView = tabDeleteController.deleteTab(renderRequest, renderResponse); |
367 | |
368 | assertNotNull("ModelAndView does not contain content.", modelAndView.getModel()); |
369 | if (modelAndView.getModel() != null) { |
370 | assertTrue("ModelAndView does not contain tabs.", modelAndView.getModel().containsKey("tabs")); |
371 | |
372 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
373 | Map.Entry entry = (Map.Entry) iter.next(); |
374 | if (entry.getKey().equals("tabs")) { |
375 | assertTrue("There should be at least one tab.", ((List) entry.getValue()).size() > 1); |
376 | } |
377 | } |
378 | } |
379 | } catch (Exception e) { |
380 | assertTrue("An error occured. " + e.getMessage(), false); |
381 | } |
382 | } |
383 | |
384 | public void testDisplayTab() { |
385 | User user = getUser(); |
386 | |
387 | MockRenderRequest renderRequest = new MockRenderRequest(); |
388 | renderRequest.getPortletSession(true).setAttribute(User.USER_KEY, user, PortletSession.APPLICATION_SCOPE); |
389 | renderRequest.addParameter("tabId", tab.getTabId().toString()); |
390 | MockRenderResponse renderResponse = new MockRenderResponse(); |
391 | |
392 | try { |
393 | TabDeleteController tabDeleteController = getTabDeleteController(); |
394 | ModelAndView modelAndView = tabDeleteController.displayTab(renderRequest, renderResponse); |
395 | |
396 | assertNotNull("ModelAndView does not contain content.", modelAndView.getModel()); |
397 | if (modelAndView.getModel() != null) { |
398 | assertTrue("ModelAndView does not contain tab.", modelAndView.getModel().containsKey("tab")); |
399 | |
400 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
401 | Map.Entry entry = (Map.Entry) iter.next(); |
402 | if (entry.getKey().equals("tab")) { |
403 | assertEquals("Tab name is incorrect.", "My Tab", ((Tab) entry.getValue()).getName()); |
404 | assertEquals("Tab id is incorrect.", tab.getTabId(), ((Tab) entry.getValue()).getTabId()); |
405 | } |
406 | } |
407 | } |
408 | } catch (Exception e) { |
409 | assertTrue("An error occured. " + e.getMessage(), false); |
410 | } |
411 | } |
412 | |
413 | public void testDisplayTabsHandleRenderRequestInternal() { |
414 | User user = getUser(); |
415 | |
416 | MockRenderRequest renderRequest = new MockRenderRequest(); |
417 | renderRequest.getPortletSession(true).setAttribute(User.USER_KEY, user, PortletSession.APPLICATION_SCOPE); |
418 | MockRenderResponse renderResponse = new MockRenderResponse(); |
419 | |
420 | try { |
421 | TabsViewController tabsViewController = getTabsViewController(); |
422 | ModelAndView modelAndView = tabsViewController.handleRenderRequestInternal(renderRequest, renderResponse); |
423 | |
424 | assertNotNull("ModelAndView does not contain content.", modelAndView.getModel()); |
425 | if (modelAndView.getModel() != null) { |
426 | assertTrue("ModelAndView does not contain tabs.", modelAndView.getModel().containsKey("tabs")); |
427 | |
428 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
429 | Map.Entry entry = (Map.Entry) iter.next(); |
430 | if (entry.getKey().equals("tabs")) { |
431 | assertTrue("There should be at least two tabs.", ((List) entry.getValue()).size() > 2); |
432 | } |
433 | } |
434 | } |
435 | } catch (Exception e) { |
436 | assertTrue("An error occured. " + e.getMessage(), false); |
437 | } |
438 | } |
439 | |
440 | public OneStartUser getOneStartUser() { |
441 | return oneStartUser; |
442 | } |
443 | |
444 | public PublishingService getPublishingService() { |
445 | return publishingService; |
446 | } |
447 | |
448 | public void setPublishingService(PublishingService publishingService) { |
449 | this.publishingService = publishingService; |
450 | } |
451 | |
452 | public UserService getUserService() { |
453 | return userService; |
454 | } |
455 | |
456 | public void setUserService(UserService userService) { |
457 | this.userService = userService; |
458 | } |
459 | |
460 | public TabDeleteController getTabDeleteController() { |
461 | return tabDeleteController; |
462 | } |
463 | |
464 | public void setTabDeleteController(TabDeleteController tabDeleteController) { |
465 | this.tabDeleteController = tabDeleteController; |
466 | } |
467 | |
468 | public TabsViewController getTabsViewController() { |
469 | return tabsViewController; |
470 | } |
471 | |
472 | public void setTabsViewController(TabsViewController tabsViewController) { |
473 | this.tabsViewController = tabsViewController; |
474 | } |
475 | |
476 | public TabWizardController getTabWizardController() { |
477 | return tabWizardController; |
478 | } |
479 | |
480 | public void setTabWizardController(TabWizardController tabWizardController) { |
481 | this.tabWizardController = tabWizardController; |
482 | } |
483 | |
484 | } |