View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  package org.kuali.mobility.shared.controllers;
16  
17  import org.apache.log4j.Logger;
18  import org.junit.AfterClass;
19  import org.junit.Before;
20  import org.junit.BeforeClass;
21  import org.junit.Test;
22  import org.kuali.mobility.campus.service.CampusService;
23  import org.kuali.mobility.security.user.api.User;
24  import org.kuali.mobility.security.user.entity.UserImpl;
25  import org.kuali.mobility.shared.Constants;
26  import org.springframework.context.ApplicationContext;
27  import org.springframework.context.support.FileSystemXmlApplicationContext;
28  import org.springframework.mock.web.MockHttpServletRequest;
29  import org.springframework.mock.web.MockHttpServletResponse;
30  import org.springframework.mock.web.MockHttpSession;
31  import org.springframework.mock.web.MockServletContext;
32  import org.springframework.ui.ExtendedModelMap;
33  import org.springframework.ui.Model;
34  
35  import java.util.Properties;
36  
37  import static org.junit.Assert.assertTrue;
38  
39  /**
40   * @author Kuali Mobility Team (mobility.collab@kuali.org)
41   */
42  public class CampusControllerTest {
43  	private static final Logger LOG = Logger.getLogger(CampusControllerTest.class);
44  
45  	private static ApplicationContext applicationContext;
46  
47  	private static MockServletContext servletContext;
48  	private CampusController controller;
49  	private Properties kmeProperties;
50  
51  	private static final String REDIRECT_HOME = "redirect:/home";
52  	private static final String REDIRECT_DINING = "redirect:/dining";
53  	private static final String REDIRECT_LABS = "redirect:/computerlabs";
54  	private static final String REDIRECT_GRADES = "redirect:/grades";
55  	private static final String REDIRECT_CAMPUS = "campus";
56  	private static final String USER = "fauxUser";
57  	private static final String[] VIEW_CAMPUS = {"ALL","BL","CO","TEST"};
58  	private static final String[] TOOL_NAMES = {"home","dining","computerlabs","",null,"TEST_TOOL","grades","maps"};
59  
60  	private static String[] getConfigLocations() {
61  		return new String[] { "classpath:/CampusSpringBeans.xml" };
62  	}
63  
64  	@BeforeClass
65  	public static void setUpClass() throws Exception {
66  		CampusControllerTest.setApplicationContext(new FileSystemXmlApplicationContext(getConfigLocations()));
67  		servletContext = new MockServletContext();
68  	}
69  
70  	@AfterClass
71  	public static void tearDownClass() throws Exception {
72  	}
73  
74  	@Before
75  	public void preTest() {
76  		this.setController((CampusController)getApplicationContext().getBean("campusController"));
77  		setKmeProperties(new Properties());
78  		getKmeProperties().setProperty("kme.secure.cookie", "false");
79  		getController().setKmeProperties(this.getKmeProperties());
80  	}
81  
82  	@Test
83  	public void testGetList() {
84  		MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
85  		Model uiModel = new ExtendedModelMap();
86  
87  		User user = new UserImpl();
88  		user.setLoginName(USER);
89  		user.setViewCampus(VIEW_CAMPUS[0]);
90  
91  		MockHttpSession session = new MockHttpSession();
92  		session.setAttribute(Constants.KME_USER_KEY,user);
93  
94  		request.setSession(session);
95  
96  		String viewName = getController().getList(request,uiModel,TOOL_NAMES[0]);
97  
98  		assertTrue("View name is incorrect.", REDIRECT_HOME.equals(viewName));
99  	}
100 
101 	@Test
102 	public void testGetListB() {
103 		MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
104 		Model uiModel = new ExtendedModelMap();
105 
106 		User user = new UserImpl();
107 		user.setLoginName(USER);
108 		user.setViewCampus(VIEW_CAMPUS[0]);
109 
110 		MockHttpSession session = new MockHttpSession();
111 		session.setAttribute(Constants.KME_USER_KEY,user);
112 
113 		request.setSession(session);
114 
115 		String viewName;
116 		try {
117 			viewName = getController().getList(request,uiModel,TOOL_NAMES[1]);
118 		} catch(NullPointerException npe) {
119 			LOG.error(npe.getLocalizedMessage(),npe);
120 			viewName=null;
121 		}
122 
123 		assertTrue("View name is incorrect.", REDIRECT_DINING.equals(viewName));
124 	}
125 
126 	@Test
127 	public void testGetListC() {
128 		MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
129 		Model uiModel = new ExtendedModelMap();
130 
131 		User user = new UserImpl();
132 		user.setLoginName(USER);
133 		user.setViewCampus(VIEW_CAMPUS[3]);
134 
135 		MockHttpSession session = new MockHttpSession();
136 		session.setAttribute(Constants.KME_USER_KEY,user);
137 
138 		request.setSession(session);
139 
140 		String viewName;
141 		try {
142 			viewName = getController().getList(request,uiModel,TOOL_NAMES[5]);
143 		} catch(NullPointerException npe) {
144 			LOG.error(npe.getLocalizedMessage(),npe);
145 			viewName=null;
146 		}
147 
148 		assertTrue("View name is incorrect: Expected " + REDIRECT_HOME + " and found " + viewName, REDIRECT_HOME.equals(viewName));
149 	}
150 
151 	@Test
152 	public void testGetListD() {
153 		MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
154 		Model uiModel = new ExtendedModelMap();
155 
156 		User user = new UserImpl();
157 		user.setLoginName(USER);
158 		user.setViewCampus(VIEW_CAMPUS[3]);
159 
160 		MockHttpSession session = new MockHttpSession();
161 		session.setAttribute(Constants.KME_USER_KEY,user);
162 
163 		request.setSession(session);
164 
165 		String viewName;
166 		try {
167 			viewName = getController().getList(request,uiModel,TOOL_NAMES[6]);
168 		} catch(NullPointerException npe) {
169 			LOG.error(npe.getLocalizedMessage(),npe);
170 			viewName=null;
171 		}
172 
173 		assertTrue("View name is incorrect: Expected "+REDIRECT_GRADES+" and found "+viewName, REDIRECT_GRADES.equals(viewName));
174 	}
175 
176 	@Test
177 	public void testGetListE() {
178 		MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
179 		Model uiModel = new ExtendedModelMap();
180 
181 		User user = new UserImpl();
182 		user.setLoginName(USER);
183 		user.setViewCampus(VIEW_CAMPUS[3]);
184 
185 		MockHttpSession session = new MockHttpSession();
186 		session.setAttribute(Constants.KME_USER_KEY,user);
187 
188 		request.setSession(session);
189 
190 		String viewName;
191 		try {
192 			viewName = getController().getList(request,uiModel,TOOL_NAMES[7]);
193 		} catch(NullPointerException npe) {
194 			LOG.error(npe.getLocalizedMessage(),npe);
195 			viewName=null;
196 		}
197 
198 		assertTrue("View name is incorrect: Expected "+REDIRECT_CAMPUS+" and found "+viewName, REDIRECT_CAMPUS.equals(viewName));
199 	}
200 
201 	@Test
202 	public void testSelectCampus() {
203 		MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
204 		MockHttpServletResponse response = new MockHttpServletResponse();
205 		Model uiModel = new ExtendedModelMap();
206 
207 		User user = new UserImpl();
208 		user.setLoginName(USER);
209 		user.setViewCampus(VIEW_CAMPUS[3]);
210 
211 		MockHttpSession session = new MockHttpSession();
212 		session.setAttribute(Constants.KME_USER_KEY,user);
213 
214 		request.setSession(session);
215 
216 		String viewName;
217 		try {
218 			viewName = getController().selectCampus(request,response,uiModel,VIEW_CAMPUS[3],TOOL_NAMES[2]);
219 		} catch(NullPointerException npe) {
220 			LOG.error(npe.getLocalizedMessage(),npe);
221 			viewName=null;
222 		}
223 
224 		assertTrue("View name is incorrect: Expected "+REDIRECT_LABS+" and found "+viewName, REDIRECT_LABS.equals(viewName));
225 	}
226 
227 	@Test
228 	public void testSelectCampusB() {
229 		MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
230 		MockHttpServletResponse response = new MockHttpServletResponse();
231 		Model uiModel = new ExtendedModelMap();
232 
233 		User user = new UserImpl();
234 		user.setLoginName(USER);
235 		user.setViewCampus(VIEW_CAMPUS[3]);
236 
237 		MockHttpSession session = new MockHttpSession();
238 		session.setAttribute(Constants.KME_USER_KEY,user);
239 
240 		request.setSession(session);
241 
242 		getKmeProperties().setProperty("kme.secure.cookie", "true");
243 
244 		String viewName;
245 		try {
246 			viewName = getController().selectCampus(request,response,uiModel,VIEW_CAMPUS[3],TOOL_NAMES[5]);
247 		} catch(NullPointerException npe) {
248 			LOG.error(npe.getLocalizedMessage(),npe);
249 			viewName=null;
250 		}
251 
252 		assertTrue("View name is incorrect: Expected "+REDIRECT_HOME+" and found "+viewName, REDIRECT_HOME.equals(viewName));
253 	}
254 
255 	@Test
256 	public void testGetService() {
257 		CampusService testService = getController().getCampusService();
258 		assertTrue("Failed to find campus service.",testService!=null && testService.equals((CampusService)getApplicationContext().getBean("campusService")));
259 	}
260 
261 	public static ApplicationContext getApplicationContext() {
262 		return applicationContext;
263 	}
264 
265 	public static void setApplicationContext(ApplicationContext applicationContext) {
266 		CampusControllerTest.applicationContext = applicationContext;
267 	}
268 
269 	public static MockServletContext getServletContext() {
270 		return servletContext;
271 	}
272 
273 	public static void setServletContext(MockServletContext servletContext) {
274 		CampusControllerTest.servletContext = servletContext;
275 	}
276 
277 	public CampusController getController() {
278 		return controller;
279 	}
280 
281 	public void setController(CampusController controller) {
282 		this.controller = controller;
283 	}
284 
285 	public Properties getKmeProperties() {
286 		return kmeProperties;
287 	}
288 
289 	public void setKmeProperties(Properties kmeProperties) {
290 		this.kmeProperties = kmeProperties;
291 	}
292 }