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.ApplicationConstant; |
18 | import edu.iu.uis.sit.portal.portlet.admin.domain.PortletMapping; |
19 | import edu.iu.uis.sit.portal.portlet.admin.services.AdminService; |
20 | import edu.iu.uis.sit.portal.user.User; |
21 | import edu.iu.uis.sit.portal.user.domain.OneStartUser; |
22 | import edu.iu.uis.sit.portal.user.service.UserService; |
23 | |
24 | public class AllPortletMappingControllersTest extends PersistenceBrokerAbstractTransactionalDataSourceSpringContextTests { |
25 | |
26 | private AdminService adminService; |
27 | |
28 | private UserService userService; |
29 | |
30 | private PortletMappingDeleteController portletMappingDeleteController; |
31 | |
32 | private PortletMappingValidator portletMappingValidator; |
33 | |
34 | private PortletMappingViewController portletMappingViewController; |
35 | |
36 | private PortletMappingController portletMappingController; |
37 | |
38 | private OneStartUser oneStartUser; |
39 | |
40 | private PortletMapping portletMapping; |
41 | |
42 | protected void onSetUpInTransaction() throws Exception { |
43 | OneStartUser oneStartUser = new OneStartUser(); |
44 | oneStartUser.setEmplId("01234567890"); |
45 | oneStartUser.setUserName("jdoe"); |
46 | oneStartUser.setLastLoginDate(new Timestamp(System.currentTimeMillis())); |
47 | getUserService().saveOneStartUser(oneStartUser); |
48 | this.oneStartUser = oneStartUser; |
49 | |
50 | PortletMapping portletMapping = new PortletMapping(); |
51 | portletMapping.setActive(true); |
52 | portletMapping.setDisplayName("My Portlet Mapping"); |
53 | portletMapping.setEdsType("EDS"); |
54 | portletMapping.setPortletType("Portlet"); |
55 | |
56 | getAdminService().savePortletMapping(portletMapping); |
57 | this.portletMapping = portletMapping; |
58 | |
59 | PortletMapping portletMapping2 = new PortletMapping(); |
60 | portletMapping2.setActive(true); |
61 | portletMapping2.setDisplayName("My Portlet Mapping 2"); |
62 | portletMapping2.setEdsType("EDS2"); |
63 | portletMapping2.setPortletType("Portlet2"); |
64 | |
65 | getAdminService().savePortletMapping(portletMapping2); |
66 | } |
67 | |
68 | private User getUser() { |
69 | String userName = "jdoe"; |
70 | String firstName = "John"; |
71 | String lastName = "Doe"; |
72 | String emailAddress = "jdoe@university.edu"; |
73 | String campusCode = "BL"; |
74 | List groups = new ArrayList(); |
75 | groups.add("group1"); |
76 | groups.add("group2"); |
77 | groups.add("group3"); |
78 | |
79 | OneStartUser oneStartUser = getUserService().findOneStartUserByUserName(userName); |
80 | return new User(oneStartUser, lastName, firstName, emailAddress, campusCode, groups); |
81 | } |
82 | |
83 | public void testPortletMappingValidatorValidate() { |
84 | try { |
85 | PortletMapping portletMapping = new PortletMapping(); |
86 | Errors errors = new BindException(portletMapping, "applicationConstant"); |
87 | getPortletMappingValidator().validate(portletMapping, errors); |
88 | assertTrue("There should be errors from the validation. Required field displayName, portletType, and edsType.", errors.hasErrors()); |
89 | } catch (Exception e) { |
90 | assertTrue("An error occured. " + e.toString(), false); |
91 | } |
92 | } |
93 | |
94 | public void testPortletMappingOnSubmitAction() { |
95 | try { |
96 | portletMapping.setDisplayName("change"); |
97 | getPortletMappingController().onSubmitAction(portletMapping); |
98 | assertEquals("Portlet mapping should have been updated.", "change", getAdminService().findPortletMappingById(portletMapping.getMappingId()).getDisplayName()); |
99 | } catch (Exception e) { |
100 | assertTrue("An error occured. " + e.toString(), false); |
101 | } |
102 | } |
103 | |
104 | public void testPortletMappingOnSubmitRender() { |
105 | try { |
106 | ModelAndView modelAndView = getPortletMappingController().onSubmitRender(portletMapping); |
107 | |
108 | assertNotNull("ModelAndView does not contain content.", modelAndView.getModel()); |
109 | assertTrue("ModelAndView does not contain a confirmation message.", modelAndView.getModel().containsKey("message")); |
110 | } catch (Exception e) { |
111 | assertTrue("An error occured. " + e.toString(), false); |
112 | } |
113 | } |
114 | |
115 | public void testPortletMappingFormBackingObject() { |
116 | MockPortletRequest portletRequest = new MockPortletRequest(); |
117 | |
118 | try { |
119 | Object object = getPortletMappingController().formBackingObject(portletRequest); |
120 | assertTrue("Command object should be of type PortletMapping.", object instanceof PortletMapping); |
121 | |
122 | PortletMapping portletMapping = (PortletMapping) object; |
123 | assertNull("Portlet mapping should be a new empty object.", portletMapping.getMappingId()); |
124 | |
125 | portletRequest.addParameter("mappingId", this.portletMapping.getMappingId().toString()); |
126 | |
127 | object = getPortletMappingController().formBackingObject(portletRequest); |
128 | assertTrue("Command object should be of type PortletMapping.", object instanceof PortletMapping); |
129 | |
130 | PortletMapping found = (PortletMapping) object; |
131 | assertEquals("Portlet mapping id is incorrect.", this.portletMapping.getMappingId(), found.getMappingId()); |
132 | |
133 | } catch (Exception e) { |
134 | assertTrue("An error occured. " + e.toString(), false); |
135 | } |
136 | } |
137 | |
138 | public void testPortletMappingDeleteOnSubmitRender() { |
139 | try { |
140 | ModelAndView modelAndView = getPortletMappingDeleteController().onSubmitRender(portletMapping); |
141 | |
142 | assertNotNull("ModelAndView does not contain content.", modelAndView.getModel()); |
143 | assertTrue("ModelAndView does not contain a confirmation message.", modelAndView.getModel().containsKey("message")); |
144 | } catch (Exception e) { |
145 | assertTrue("An error occured. " + e.toString(), false); |
146 | } |
147 | } |
148 | |
149 | public void testPortletMappingDeleteOnSubmitAction() { |
150 | try { |
151 | getPortletMappingDeleteController().onSubmitAction(portletMapping); |
152 | assertNull("Portlet Mapping should have been deleted.", getAdminService().findPortletMappingById(portletMapping.getMappingId())); |
153 | } catch (Exception e) { |
154 | assertTrue("An error occured. " + e.toString(), false); |
155 | } |
156 | } |
157 | |
158 | public void testPortletMappingDeleteFormBackingObject() { |
159 | MockPortletRequest portletRequest = new MockPortletRequest(); |
160 | |
161 | try { |
162 | Object object = getPortletMappingDeleteController().formBackingObject(portletRequest); |
163 | assertTrue("Command object should be of type PortletMapping.", object instanceof PortletMapping); |
164 | |
165 | PortletMapping portletMapping = (PortletMapping) object; |
166 | assertNull("Portlet Mapping should be a new empty object.", portletMapping.getMappingId()); |
167 | |
168 | portletRequest.addParameter("mappingId", this.portletMapping.getMappingId().toString()); |
169 | |
170 | object = getPortletMappingDeleteController().formBackingObject(portletRequest); |
171 | assertTrue("Command object should be of type PortletMapping.", object instanceof PortletMapping); |
172 | |
173 | PortletMapping found = (PortletMapping) object; |
174 | assertEquals("Portlet mapping id is incorrect.", this.portletMapping.getMappingId(), found.getMappingId()); |
175 | |
176 | } catch (Exception e) { |
177 | assertTrue("An error occured. " + e.toString(), false); |
178 | } |
179 | } |
180 | |
181 | public void testPortletMappingViewHandleRenderRequestInternal() { |
182 | MockRenderRequest renderRequest = new MockRenderRequest(); |
183 | MockRenderResponse renderResponse = new MockRenderResponse(); |
184 | try { |
185 | ModelAndView modelAndView = getPortletMappingViewController().handleRenderRequestInternal(renderRequest, renderResponse); |
186 | |
187 | assertNotNull("ModelAndView does not contain content.", modelAndView.getModel()); |
188 | if (modelAndView.getModel() != null) { |
189 | assertTrue("ModelAndView does not contain constants.", modelAndView.getModel().containsKey("portletMappings")); |
190 | |
191 | for (Iterator iter = modelAndView.getModel().entrySet().iterator(); iter.hasNext();) { |
192 | Map.Entry entry = (Map.Entry) iter.next(); |
193 | if (entry.getKey().equals("portletMappings")) { |
194 | assertTrue("There should be at least 2 portlet mappings.", ((List) entry.getValue()).size() > 1); |
195 | } |
196 | } |
197 | } |
198 | } catch (Exception e) { |
199 | assertTrue("An error occured. " + e.toString(), false); |
200 | } |
201 | } |
202 | |
203 | public OneStartUser getOneStartUser() { |
204 | return oneStartUser; |
205 | } |
206 | |
207 | public UserService getUserService() { |
208 | return userService; |
209 | } |
210 | |
211 | public void setUserService(UserService userService) { |
212 | this.userService = userService; |
213 | } |
214 | |
215 | public AdminService getAdminService() { |
216 | return adminService; |
217 | } |
218 | |
219 | public void setAdminService(AdminService adminService) { |
220 | this.adminService = adminService; |
221 | } |
222 | |
223 | public PortletMappingController getPortletMappingController() { |
224 | return portletMappingController; |
225 | } |
226 | |
227 | public void setPortletMappingController(PortletMappingController portletMappingController) { |
228 | this.portletMappingController = portletMappingController; |
229 | } |
230 | |
231 | public PortletMappingDeleteController getPortletMappingDeleteController() { |
232 | return portletMappingDeleteController; |
233 | } |
234 | |
235 | public void setPortletMappingDeleteController(PortletMappingDeleteController portletMappingDeleteController) { |
236 | this.portletMappingDeleteController = portletMappingDeleteController; |
237 | } |
238 | |
239 | public PortletMappingValidator getPortletMappingValidator() { |
240 | return portletMappingValidator; |
241 | } |
242 | |
243 | public void setPortletMappingValidator(PortletMappingValidator portletMappingValidator) { |
244 | this.portletMappingValidator = portletMappingValidator; |
245 | } |
246 | |
247 | public PortletMappingViewController getPortletMappingViewController() { |
248 | return portletMappingViewController; |
249 | } |
250 | |
251 | public void setPortletMappingViewController(PortletMappingViewController portletMappingViewController) { |
252 | this.portletMappingViewController = portletMappingViewController; |
253 | } |
254 | |
255 | } |