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.web.portlet.ModelAndView; |
13 | |
14 | import edu.iu.uis.sit.portal.PersistenceBrokerAbstractTransactionalDataSourceSpringContextTests; |
15 | import edu.iu.uis.sit.portal.portlet.admin.services.AdminService; |
16 | import edu.iu.uis.sit.portal.portlet.publishing.domain.Acl; |
17 | import edu.iu.uis.sit.portal.portlet.publishing.domain.AclExpression; |
18 | import edu.iu.uis.sit.portal.portlet.publishing.domain.AclExpressionHtml; |
19 | import edu.iu.uis.sit.portal.portlet.publishing.domain.Fragment; |
20 | import edu.iu.uis.sit.portal.portlet.publishing.domain.Tab; |
21 | import edu.iu.uis.sit.portal.portlet.publishing.services.PublishingService; |
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 AllPublishingAclControllersTest extends PersistenceBrokerAbstractTransactionalDataSourceSpringContextTests { |
28 | |
29 | private AdminService adminService; |
30 | |
31 | private PublishingService publishingService; |
32 | |
33 | private UserService userService; |
34 | |
35 | private PublishingAclViewController publishingAclViewController; |
36 | |
37 | private PublishingAclController publishingAclController; |
38 | |
39 | private OneStartUser oneStartUser; |
40 | |
41 | private Fragment fragment; |
42 | |
43 | private Tab tab; |
44 | |
45 | protected void onSetUpInTransaction() throws Exception { |
46 | OneStartUser oneStartUser = new OneStartUser(); |
47 | oneStartUser.setEmplId("01234567890"); |
48 | oneStartUser.setUserName("jdoe"); |
49 | oneStartUser.setLastLoginDate(new Timestamp(System.currentTimeMillis())); |
50 | getUserService().saveOneStartUser(oneStartUser); |
51 | this.oneStartUser = oneStartUser; |
52 | |
53 | Fragment fragment = new Fragment(); |
54 | fragment.setActive(true); |
55 | fragment.setType(Constants.XML_TYPE); |
56 | fragment.setContactEmail("admin@university.edu"); |
57 | fragment.setCreateDate(new Timestamp(System.currentTimeMillis())); |
58 | fragment.setDescription("my description"); |
59 | fragment.setLastUpdateDate(new Timestamp(System.currentTimeMillis())); |
60 | fragment.setTitle("My Fragment"); |
61 | |
62 | AclExpression aclExpression = new AclExpression(); |
63 | aclExpression.setAclExpression("<aclfragmentexpression>"); |
64 | aclExpression.setAclExpressionType(Constants.OR); |
65 | aclExpression.setFragment(fragment); |
66 | |
67 | fragment.setAclExpression(aclExpression); |
68 | |
69 | AclExpressionHtml aclExpressionHtml = new AclExpressionHtml(); |
70 | aclExpressionHtml.setAclExpressionHtml("<aclfragmentexpression html>"); |
71 | aclExpressionHtml.setAclExpressionType(Constants.OR); |
72 | aclExpressionHtml.setFragment(fragment); |
73 | |
74 | fragment.setAclExpressionHtml(aclExpressionHtml); |
75 | |
76 | Acl acl = new Acl(); |
77 | acl.setFragment(fragment); |
78 | acl.setKey(Constants.GROUP_KEY); |
79 | acl.setType(Constants.GROUP_PUBLISH); |
80 | acl.setValue("group1"); |
81 | |
82 | Acl acl2 = new Acl(); |
83 | acl2.setFragment(fragment); |
84 | acl2.setKey(Constants.GROUP_KEY); |
85 | acl2.setType(Constants.GROUP_VIEW); |
86 | acl2.setValue("group2"); |
87 | |
88 | Acl acl3 = new Acl(); |
89 | acl3.setFragment(fragment); |
90 | acl3.setKey("rolekey"); |
91 | acl3.setType(Constants.ROLE_VIEW); |
92 | acl3.setValue("role1"); |
93 | |
94 | List acls = new ArrayList(); |
95 | acls.add(acl); |
96 | acls.add(acl2); |
97 | acls.add(acl3); |
98 | |
99 | fragment.setAcls(acls); |
100 | |
101 | getPublishingService().saveFragment(fragment); |
102 | this.fragment = fragment; |
103 | |
104 | Tab tab = new Tab(); |
105 | tab.setActive(true); |
106 | tab.setContactEmail("admin@university.edu"); |
107 | tab.setCreateDate(new Timestamp(System.currentTimeMillis())); |
108 | tab.setDescription("my description"); |
109 | tab.setLastUpdateDate(new Timestamp(System.currentTimeMillis())); |
110 | tab.setName("My Tab"); |
111 | |
112 | aclExpression = new AclExpression(); |
113 | aclExpression.setAclExpression("<acltabexpression>"); |
114 | aclExpression.setAclExpressionType(Constants.OR); |
115 | aclExpression.setTab(tab); |
116 | |
117 | tab.setAclExpression(aclExpression); |
118 | |
119 | aclExpressionHtml = new AclExpressionHtml(); |
120 | aclExpressionHtml.setAclExpressionHtml("<acltabexpression html>"); |
121 | aclExpressionHtml.setAclExpressionType(Constants.OR); |
122 | aclExpressionHtml.setTab(tab); |
123 | |
124 | tab.setAclExpressionHtml(aclExpressionHtml); |
125 | |
126 | acl = new Acl(); |
127 | acl.setTab(tab); |
128 | acl.setKey(Constants.GROUP_KEY); |
129 | acl.setType(Constants.GROUP_PUBLISH); |
130 | acl.setValue("group1"); |
131 | |
132 | acl2 = new Acl(); |
133 | acl2.setTab(tab); |
134 | acl2.setKey(Constants.GROUP_KEY); |
135 | acl2.setType(Constants.GROUP_VIEW); |
136 | acl2.setValue("group2"); |
137 | |
138 | acl3 = new Acl(); |
139 | acl3.setTab(tab); |
140 | acl3.setKey("rolekey"); |
141 | acl3.setType(Constants.ROLE_VIEW); |
142 | acl3.setValue("role1"); |
143 | |
144 | acls = new ArrayList(); |
145 | acls.add(acl); |
146 | acls.add(acl2); |
147 | acls.add(acl3); |
148 | |
149 | tab.setAcls(acls); |
150 | |
151 | getPublishingService().saveTab(tab); |
152 | this.tab = tab; |
153 | } |
154 | |
155 | private User getUser() { |
156 | String userName = "jdoe"; |
157 | String firstName = "John"; |
158 | String lastName = "Doe"; |
159 | String emailAddress = "jdoe@university.edu"; |
160 | String campusCode = "BL"; |
161 | List groups = new ArrayList(); |
162 | groups.add("group1"); |
163 | groups.add("group2"); |
164 | groups.add("group3"); |
165 | |
166 | OneStartUser oneStartUser = getUserService().findOneStartUserByUserName(userName); |
167 | return new User(oneStartUser, lastName, firstName, emailAddress, campusCode, groups); |
168 | } |
169 | |
170 | public void testPublishingAclOnSubmitAction() { |
171 | try { |
172 | WebPublishingAcl webPublishingAcl = new WebPublishingAcl(fragment); |
173 | webPublishingAcl.setAclExpression("change"); |
174 | webPublishingAcl.setAclExpressionHtml("change"); |
175 | webPublishingAcl.setType(Constants.FRAGMENT); |
176 | getPublishingAclController().onSubmitAction(webPublishingAcl); |
177 | assertEquals("Fragment acl expression should have been updated.", "change", getPublishingService().findFragmentById(fragment.getFragmentId()).getAclExpression().getAclExpression()); |
178 | assertEquals("Fragment acl expression html should have been updated.", "change", getPublishingService().findFragmentById(fragment.getFragmentId()).getAclExpressionHtml().getAclExpressionHtml()); |
179 | |
180 | webPublishingAcl = new WebPublishingAcl(tab); |
181 | webPublishingAcl.setAclExpression("change"); |
182 | webPublishingAcl.setAclExpressionHtml("change"); |
183 | webPublishingAcl.setType(Constants.TAB); |
184 | getPublishingAclController().onSubmitAction(webPublishingAcl); |
185 | assertEquals("Tab acl expression should have been updated.", "change", getPublishingService().findTabById(tab.getTabId()).getAclExpression().getAclExpression()); |
186 | assertEquals("Tab acl expression html should have been updated.", "change", getPublishingService().findTabById(tab.getTabId()).getAclExpressionHtml().getAclExpressionHtml()); |
187 | } catch (Exception e) { |
188 | assertTrue("An error occured. " + e.toString(), false); |
189 | } |
190 | } |
191 | |
192 | public void testPublishingAclOnSubmitRender() { |
193 | try { |
194 | WebPublishingAcl webPublishingAcl = new WebPublishingAcl(fragment); |
195 | ModelAndView modelAndView = getPublishingAclController().onSubmitRender(webPublishingAcl); |
196 | |
197 | assertNotNull("ModelAndView does not contain content.", modelAndView.getModel()); |
198 | if (modelAndView.getModel() != null) { |
199 | assertTrue("ModelAndView does not contain constants.", modelAndView.getModel().containsKey("fragmentsTabs")); |
200 | |
201 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
202 | Map.Entry entry = (Map.Entry) iter.next(); |
203 | if (entry.getKey().equals("fragmentsTabs")) { |
204 | assertTrue("There should be at least 2 fragments and tabs.", ((List) entry.getValue()).size() > 1); |
205 | } |
206 | } |
207 | } |
208 | } catch (Exception e) { |
209 | assertTrue("An error occured. " + e.toString(), false); |
210 | } |
211 | } |
212 | |
213 | public void testPublishingAclFormBackingObject() { |
214 | MockPortletRequest portletRequest = new MockPortletRequest(); |
215 | |
216 | try { |
217 | portletRequest.addParameter("id", this.tab.getTabId().toString()); |
218 | portletRequest.addParameter("type", Constants.TAB); |
219 | |
220 | Object object = getPublishingAclController().formBackingObject(portletRequest); |
221 | assertTrue("Command object should be of type WebPublishingAcl.", object instanceof WebPublishingAcl); |
222 | |
223 | WebPublishingAcl found = (WebPublishingAcl) object; |
224 | assertEquals("Tab id is incorrect.", this.tab.getTabId(), found.getTab().getTabId()); |
225 | |
226 | portletRequest.setParameter("id", this.fragment.getFragmentId().toString()); |
227 | portletRequest.setParameter("type", Constants.FRAGMENT); |
228 | |
229 | object = getPublishingAclController().formBackingObject(portletRequest); |
230 | assertTrue("Command object should be of type WebPublishingAcl.", object instanceof WebPublishingAcl); |
231 | |
232 | found = (WebPublishingAcl) object; |
233 | assertEquals("Fragment id is incorrect.", this.fragment.getFragmentId(), found.getFragment().getFragmentId()); |
234 | } catch (Exception e) { |
235 | assertTrue("An error occured. " + e.toString(), false); |
236 | } |
237 | } |
238 | |
239 | public void testPublishingAclViewHandleRenderRequestInternal() { |
240 | MockRenderRequest renderRequest = new MockRenderRequest(); |
241 | MockRenderResponse renderResponse = new MockRenderResponse(); |
242 | try { |
243 | ModelAndView modelAndView = getPublishingAclViewController().handleRenderRequestInternal(renderRequest, renderResponse); |
244 | |
245 | assertNotNull("ModelAndView does not contain content.", modelAndView.getModel()); |
246 | if (modelAndView.getModel() != null) { |
247 | assertTrue("ModelAndView does not contain constants.", modelAndView.getModel().containsKey("fragmentsTabs")); |
248 | |
249 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
250 | Map.Entry entry = (Map.Entry) iter.next(); |
251 | if (entry.getKey().equals("fragmentsTabs")) { |
252 | assertTrue("There should be at least 2 fragments and tabs.", ((List) entry.getValue()).size() > 1); |
253 | } |
254 | } |
255 | } |
256 | } catch (Exception e) { |
257 | assertTrue("An error occured. " + e.toString(), false); |
258 | } |
259 | } |
260 | |
261 | public OneStartUser getOneStartUser() { |
262 | return oneStartUser; |
263 | } |
264 | |
265 | public UserService getUserService() { |
266 | return userService; |
267 | } |
268 | |
269 | public void setUserService(UserService userService) { |
270 | this.userService = userService; |
271 | } |
272 | |
273 | public AdminService getAdminService() { |
274 | return adminService; |
275 | } |
276 | |
277 | public void setAdminService(AdminService adminService) { |
278 | this.adminService = adminService; |
279 | } |
280 | |
281 | public PublishingAclController getPublishingAclController() { |
282 | return publishingAclController; |
283 | } |
284 | |
285 | public void setPublishingAclController(PublishingAclController publishingAclController) { |
286 | this.publishingAclController = publishingAclController; |
287 | } |
288 | |
289 | public PublishingAclViewController getPublishingAclViewController() { |
290 | return publishingAclViewController; |
291 | } |
292 | |
293 | public void setPublishingAclViewController(PublishingAclViewController publishingAclViewController) { |
294 | this.publishingAclViewController = publishingAclViewController; |
295 | } |
296 | |
297 | public PublishingService getPublishingService() { |
298 | return publishingService; |
299 | } |
300 | |
301 | public void setPublishingService(PublishingService publishingService) { |
302 | this.publishingService = publishingService; |
303 | } |
304 | |
305 | } |