1 | package edu.iu.uis.sit.portal.portlet.personalize.web; |
2 | |
3 | import java.sql.Timestamp; |
4 | import java.util.ArrayList; |
5 | import java.util.Collections; |
6 | import java.util.Iterator; |
7 | import java.util.List; |
8 | import java.util.Map; |
9 | |
10 | import javax.portlet.PortletSession; |
11 | |
12 | import org.springframework.mock.web.portlet.MockActionRequest; |
13 | import org.springframework.mock.web.portlet.MockActionResponse; |
14 | import org.springframework.mock.web.portlet.MockRenderRequest; |
15 | import org.springframework.mock.web.portlet.MockRenderResponse; |
16 | import org.springframework.web.portlet.ModelAndView; |
17 | |
18 | import edu.iu.uis.sit.portal.PersistenceBrokerAbstractTransactionalDataSourceSpringContextTests; |
19 | import edu.iu.uis.sit.portal.cache.service.CacheService; |
20 | import edu.iu.uis.sit.portal.portlet.personalize.domain.GroupLink; |
21 | import edu.iu.uis.sit.portal.portlet.personalize.services.PersonalizeService; |
22 | import edu.iu.uis.sit.portal.portlet.publishing.domain.Tab; |
23 | import edu.iu.uis.sit.portal.portlet.publishing.services.PublishingService; |
24 | import edu.iu.uis.sit.portal.user.User; |
25 | import edu.iu.uis.sit.portal.user.domain.OneStartUser; |
26 | import edu.iu.uis.sit.portal.user.service.UserService; |
27 | |
28 | public class GroupLinksControllerTest extends PersistenceBrokerAbstractTransactionalDataSourceSpringContextTests { |
29 | private PersonalizeService personalizeService; |
30 | |
31 | private PublishingService publishingService; |
32 | |
33 | private CacheService cacheService; |
34 | |
35 | private UserService userService; |
36 | |
37 | private GroupLinksController groupLinksController; |
38 | |
39 | private OneStartUser oneStartUser; |
40 | private OneStartUser oneStartUser2; |
41 | |
42 | private Long tabId; |
43 | |
44 | // TODO when acls are added for tabs. The user must have a group in his user object equal to a group in each tab. |
45 | protected void onSetUpInTransaction() throws Exception { |
46 | |
47 | OneStartUser oneStartUser = new OneStartUser(); |
48 | oneStartUser.setEmplId("01234567890"); |
49 | oneStartUser.setUserName("jdoe"); |
50 | oneStartUser.setLastLoginDate(new Timestamp(System.currentTimeMillis())); |
51 | getUserService().saveOneStartUser(oneStartUser); |
52 | this.oneStartUser = oneStartUser; |
53 | |
54 | OneStartUser oneStartUser2 = new OneStartUser(); |
55 | oneStartUser2.setEmplId("91234567890"); |
56 | oneStartUser2.setUserName("jadoe"); |
57 | oneStartUser2.setLastLoginDate(new Timestamp(System.currentTimeMillis())); |
58 | getUserService().saveOneStartUser(oneStartUser2); |
59 | this.oneStartUser2 = oneStartUser2; |
60 | |
61 | Tab tab = new Tab(); |
62 | tab.setActive(true); |
63 | tab.setContactEmail("admin@university.edu"); |
64 | tab.setCreateDate(new Timestamp(System.currentTimeMillis())); |
65 | tab.setDescription("my description"); |
66 | tab.setLastUpdateDate(new Timestamp(System.currentTimeMillis())); |
67 | tab.setName("My Tab"); |
68 | getPublishingService().saveTab(tab); |
69 | this.tabId = tab.getTabId(); |
70 | |
71 | Tab tab2 = new Tab(); |
72 | tab2.setActive(true); |
73 | tab2.setContactEmail("admin@university.edu"); |
74 | tab2.setCreateDate(new Timestamp(System.currentTimeMillis())); |
75 | tab2.setDescription("my 2nd description"); |
76 | tab2.setLastUpdateDate(new Timestamp(System.currentTimeMillis())); |
77 | tab2.setName("My 2nd Tab"); |
78 | getPublishingService().saveTab(tab2); |
79 | |
80 | } |
81 | |
82 | private User getUser() { |
83 | String userName = "jdoe"; |
84 | String firstName = "John"; |
85 | String lastName = "Doe"; |
86 | String emailAddress = "jdoe@university.edu"; |
87 | String campusCode = "BL"; |
88 | List groups = new ArrayList(); |
89 | groups.add("group1"); |
90 | groups.add("group2"); |
91 | groups.add("group3"); |
92 | |
93 | OneStartUser oneStartUser = getUserService().findOneStartUserByUserName(userName); |
94 | |
95 | User user = new User(oneStartUser, lastName, firstName, emailAddress, campusCode, groups); |
96 | List groupQuickLinks = new ArrayList(); |
97 | for (Iterator iter = getPersonalizeService().findUsersGroupLinks(oneStartUser.getPersonId()).iterator(); iter.hasNext();) { |
98 | GroupLink groupLink = (GroupLink) iter.next(); |
99 | WebGroup webGroup = new WebGroup(groupLink); |
100 | Tab tab = getCacheService().findTab(user, groupLink.getTabId()); |
101 | if (tab != null) { |
102 | webGroup.setGroupLinkName(tab.getName()); |
103 | groupQuickLinks.add(webGroup); |
104 | } |
105 | } |
106 | Collections.sort(groupQuickLinks); |
107 | user.setGroupQuickLinks(groupQuickLinks); |
108 | return user; |
109 | } |
110 | |
111 | private User getUser2() { |
112 | String userName = "jadoe"; |
113 | String firstName = "Jane"; |
114 | String lastName = "Doe"; |
115 | String emailAddress = "jadoe@university.edu"; |
116 | String campusCode = "BL"; |
117 | List groups = new ArrayList(); |
118 | groups.add("group1"); |
119 | groups.add("group2"); |
120 | groups.add("group3"); |
121 | |
122 | OneStartUser oneStartUser = getUserService().findOneStartUserByUserName(userName); |
123 | |
124 | User user = new User(oneStartUser, lastName, firstName, emailAddress, campusCode, groups); |
125 | List groupQuickLinks = new ArrayList(); |
126 | for (Iterator iter = getPersonalizeService().findUsersGroupLinks(oneStartUser.getPersonId()).iterator(); iter.hasNext();) { |
127 | GroupLink groupLink = (GroupLink) iter.next(); |
128 | WebGroup webGroup = new WebGroup(groupLink); |
129 | Tab tab = getCacheService().findTab(user, groupLink.getTabId()); |
130 | if (tab != null) { |
131 | webGroup.setGroupLinkName(tab.getName()); |
132 | groupQuickLinks.add(webGroup); |
133 | } |
134 | } |
135 | Collections.sort(groupQuickLinks); |
136 | user.setGroupQuickLinks(groupQuickLinks); |
137 | return user; |
138 | } |
139 | |
140 | public void testAddGroupLink() { |
141 | User user = getUser(); |
142 | MockActionRequest actionRequest = new MockActionRequest(); |
143 | actionRequest.getPortletSession(true).setAttribute(User.USER_KEY, user, PortletSession.APPLICATION_SCOPE); |
144 | actionRequest.addParameter("tabId", tabId.toString()); |
145 | MockActionResponse actionResponse = new MockActionResponse(); |
146 | |
147 | MockRenderRequest renderRequest = new MockRenderRequest(); |
148 | renderRequest.getPortletSession(true).setAttribute(User.USER_KEY, user, PortletSession.APPLICATION_SCOPE); |
149 | MockRenderResponse renderResponse = new MockRenderResponse(); |
150 | |
151 | GroupLinksController groupLinksController = getGroupLinksController(); |
152 | try { |
153 | |
154 | List groupLinks = getPersonalizeService().findUsersGroupLinks(user.getOnestartUser().getPersonId()); |
155 | assertEquals("User should have 0 group quick links.", 0, groupLinks.size()); |
156 | |
157 | groupLinksController.addGroupLink(actionRequest, actionResponse); |
158 | |
159 | ModelAndView modelAndView = groupLinksController.addGroupLink(renderRequest, renderResponse); |
160 | |
161 | assertNotNull("ModelAndView does not contain content groupLinks or usersTabs.", modelAndView.getModel()); |
162 | if (modelAndView.getModel() != null) { |
163 | assertTrue("ModelAndView does not contain groupLinks.", modelAndView.getModel().containsKey("groupLinks")); |
164 | assertTrue("ModelAndView does not contain usersTabs.", modelAndView.getModel().containsKey("usersTabs")); |
165 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
166 | Map.Entry entry = (Map.Entry) iter.next(); |
167 | if (entry.getKey().equals("groupLinks")) { |
168 | assertEquals("User should have 1 group link.", 1, ((List) entry.getValue()).size()); |
169 | |
170 | for (Iterator iterator = ((List) entry.getValue()).iterator(); iterator.hasNext();) { |
171 | WebGroup webGroup = (WebGroup) iterator.next(); |
172 | assertEquals("The wrong tab name is in the users group quick links.", "My Tab", webGroup.getGroupLinkName()); |
173 | } |
174 | } |
175 | if (entry.getKey().equals("usersTabs")) { |
176 | assertTrue("User should have more than 1 tab remaining.", ((List) entry.getValue()).size() > 1); |
177 | } |
178 | } |
179 | } |
180 | } catch (Exception e) { |
181 | assertTrue("An error occured. " + e.getMessage(), false); |
182 | } |
183 | } |
184 | |
185 | public void testRemoveGroupLink() { |
186 | GroupLink groupLink = new GroupLink(); |
187 | groupLink.setPersonId(oneStartUser.getPersonId()); |
188 | groupLink.setTabId(tabId); |
189 | getPersonalizeService().saveGroupLink(groupLink); |
190 | |
191 | User user2 = getUser2(); |
192 | |
193 | MockActionRequest actionRequest = new MockActionRequest(); |
194 | actionRequest.getPortletSession(true).setAttribute(User.USER_KEY, user2, PortletSession.APPLICATION_SCOPE); |
195 | actionRequest.addParameter("groupLinkId", groupLink.getGroupLinkId().toString()); |
196 | MockActionResponse actionResponse = new MockActionResponse(); |
197 | |
198 | MockRenderRequest renderRequest = new MockRenderRequest(); |
199 | renderRequest.getPortletSession(true).setAttribute(User.USER_KEY, user2, PortletSession.APPLICATION_SCOPE); |
200 | MockRenderResponse renderResponse = new MockRenderResponse(); |
201 | |
202 | GroupLinksController groupLinksController = getGroupLinksController(); |
203 | try { |
204 | groupLinksController.removeGroupLink(actionRequest, actionResponse); |
205 | |
206 | User user = getUser(); |
207 | actionRequest.getPortletSession(true).setAttribute(User.USER_KEY, user, PortletSession.APPLICATION_SCOPE); |
208 | renderRequest.getPortletSession(true).setAttribute(User.USER_KEY, user, PortletSession.APPLICATION_SCOPE); |
209 | |
210 | List groupLinks = getPersonalizeService().findUsersGroupLinks(user.getOnestartUser().getPersonId()); |
211 | assertEquals("User should have 1 group quick link because previous user2 did not have permission to delete this group quick link.", 1, groupLinks.size()); |
212 | |
213 | groupLinksController.removeGroupLink(actionRequest, actionResponse); |
214 | ModelAndView modelAndView = groupLinksController.removeGroupLink(renderRequest, renderResponse); |
215 | |
216 | assertNotNull("ModelAndView does not contain content groupLinks or usersTabs.", modelAndView.getModel()); |
217 | if (modelAndView.getModel() != null) { |
218 | assertTrue("ModelAndView does not contain groupLinks.", modelAndView.getModel().containsKey("groupLinks")); |
219 | assertTrue("ModelAndView does not contain usersTabs.", modelAndView.getModel().containsKey("usersTabs")); |
220 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
221 | Map.Entry entry = (Map.Entry) iter.next(); |
222 | if (entry.getKey().equals("groupLinks")) { |
223 | assertEquals("User should have 0 group link.", 0, ((List) entry.getValue()).size()); |
224 | |
225 | for (Iterator iterator = ((List) entry.getValue()).iterator(); iterator.hasNext();) { |
226 | WebGroup webGroup = (WebGroup) iterator.next(); |
227 | assertEquals("The wrong tab name is in the users group quick links.", "My Tab", webGroup.getGroupLinkName()); |
228 | } |
229 | } |
230 | if (entry.getKey().equals("usersTabs")) { |
231 | assertTrue("User should have more than 2 tabs remaining.", ((List) entry.getValue()).size() > 2); |
232 | } |
233 | } |
234 | } |
235 | } catch (Exception e) { |
236 | assertTrue("An error occured. " + e.getMessage(), false); |
237 | } |
238 | } |
239 | |
240 | public void testViewGroupLinks() { |
241 | GroupLink groupLink = new GroupLink(); |
242 | groupLink.setPersonId(oneStartUser.getPersonId()); |
243 | groupLink.setTabId(tabId); |
244 | getPersonalizeService().saveGroupLink(groupLink); |
245 | |
246 | User user = getUser(); |
247 | MockRenderRequest request = new MockRenderRequest(); |
248 | request.getPortletSession(true).setAttribute(User.USER_KEY, user, PortletSession.APPLICATION_SCOPE); |
249 | MockRenderResponse response = new MockRenderResponse(); |
250 | |
251 | GroupLinksController groupLinksController = getGroupLinksController(); |
252 | try { |
253 | ModelAndView modelAndView = groupLinksController.viewGroupLinks(request, response); |
254 | |
255 | assertNotNull("ModelAndView does not contain content groupLinks or usersTabs.", modelAndView.getModel()); |
256 | if (modelAndView.getModel() != null) { |
257 | assertTrue("ModelAndView does not contain groupLinks.", modelAndView.getModel().containsKey("groupLinks")); |
258 | assertTrue("ModelAndView does not contain usersTabs.", modelAndView.getModel().containsKey("usersTabs")); |
259 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
260 | Map.Entry entry = (Map.Entry) iter.next(); |
261 | if (entry.getKey().equals("groupLinks")) { |
262 | assertEquals("User should have 1 group link.", 1, ((List) entry.getValue()).size()); |
263 | |
264 | for (Iterator iterator = ((List) entry.getValue()).iterator(); iterator.hasNext();) { |
265 | WebGroup webGroup = (WebGroup) iterator.next(); |
266 | assertEquals("The wrong tab name is in the users group quick links.", "My Tab", webGroup.getGroupLinkName()); |
267 | } |
268 | } |
269 | if (entry.getKey().equals("usersTabs")) { |
270 | assertTrue("User should have more than 1 tab remaining.", ((List) entry.getValue()).size() > 1); |
271 | } |
272 | } |
273 | } |
274 | } catch (Exception e) { |
275 | assertTrue("An error occured. " + e.getMessage(), false); |
276 | } |
277 | } |
278 | |
279 | public PersonalizeService getPersonalizeService() { |
280 | return personalizeService; |
281 | } |
282 | |
283 | public void setPersonalizeService(PersonalizeService personalizeService) { |
284 | this.personalizeService = personalizeService; |
285 | } |
286 | |
287 | public CacheService getCacheService() { |
288 | return cacheService; |
289 | } |
290 | |
291 | public void setCacheService(CacheService cacheService) { |
292 | this.cacheService = cacheService; |
293 | } |
294 | |
295 | public UserService getUserService() { |
296 | return userService; |
297 | } |
298 | |
299 | public void setUserService(UserService userService) { |
300 | this.userService = userService; |
301 | } |
302 | |
303 | public PublishingService getPublishingService() { |
304 | return publishingService; |
305 | } |
306 | |
307 | public void setPublishingService(PublishingService publishingService) { |
308 | this.publishingService = publishingService; |
309 | } |
310 | |
311 | public GroupLinksController getGroupLinksController() { |
312 | return groupLinksController; |
313 | } |
314 | |
315 | public void setGroupLinksController(GroupLinksController groupLinksController) { |
316 | this.groupLinksController = groupLinksController; |
317 | } |
318 | |
319 | } |