1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.bus.controllers;
16
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Properties;
20
21 import org.apache.log4j.Logger;
22 import org.junit.AfterClass;
23 import org.junit.Before;
24 import org.junit.Test;
25 import static org.junit.Assert.*;
26 import org.junit.BeforeClass;
27 import org.junit.runner.RunWith;
28 import org.kuali.mobility.bus.entity.Bus;
29 import org.kuali.mobility.bus.entity.BusRoute;
30 import org.kuali.mobility.bus.entity.BusRouteImpl;
31 import org.kuali.mobility.bus.entity.BusStop;
32 import org.kuali.mobility.bus.entity.BusStopImpl;
33 import org.kuali.mobility.bus.service.BusService;
34 import org.kuali.mobility.bus.service.BusServiceImpl;
35 import org.kuali.mobility.security.user.api.User;
36 import org.kuali.mobility.security.user.entity.UserImpl;
37 import org.kuali.mobility.shared.Constants;
38 import org.mockito.Mock;
39 import static org.mockito.Matchers.any;
40 import static org.mockito.Mockito.when;
41 import static org.mockito.Mockito.doReturn;
42
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.context.support.ClassPathXmlApplicationContext;
45 import org.springframework.mock.web.MockHttpServletRequest;
46 import org.springframework.mock.web.MockHttpServletResponse;
47 import org.springframework.mock.web.MockHttpSession;
48 import org.springframework.mock.web.MockPageContext;
49 import org.springframework.mock.web.MockServletContext;
50 import org.springframework.ui.ExtendedModelMap;
51 import org.springframework.ui.Model;
52
53
54
55
56
57
58 @RunWith(org.mockito.runners.MockitoJUnitRunner.class)
59 public class BusControllerTest {
60
61 private static final Logger LOG = Logger.getLogger( BusControllerTest.class );
62
63 private static final String USER = "fauxUser";
64 private static final String REDIRECT_HOME = "redirect:/campus?toolName=bus";
65 private static final String[] VIEW_CAMPUS = {"ALL","BL","CO","TEST"};
66
67 private static MockServletContext servletContext;
68 private BusController controller;
69 @Mock
70 private static ApplicationContext applicationContext;
71 @Mock
72 private BusService busService;
73 @Mock
74 private Properties busProperties;
75 private MockHttpServletRequest request;
76 private MockHttpServletResponse response;
77 private Model uiModel;
78
79 @BeforeClass
80 public static void setUpClass() throws Exception {
81 servletContext = new MockServletContext();
82 }
83
84 @Before
85 public void preTest() {
86 this.setController(new BusController());
87 this.getController().setApplicationContext(applicationContext);
88 this.getController().setService(this.getBusService());
89 this.getController().setBusProperties(this.getBusProperties());
90 this.setRequest(new MockHttpServletRequest(servletContext));
91 this.setResponse(new MockHttpServletResponse());
92 this.setUiModel(new ExtendedModelMap());
93 this.getRequest().setSession(new MockHttpSession());
94 }
95
96 @AfterClass
97 public static void tearDownClass() throws Exception {
98 }
99
100 @Test
101 public void testIndex() {
102 when(busProperties.getProperty(any(String.class))).thenReturn("map,stops");
103 User user = new UserImpl();
104 user.setLoginName(USER);
105 this.getRequest().getSession().setAttribute(Constants.KME_USER_KEY,user);
106 String viewName = getController().index(getRequest(), getUiModel());
107 assertTrue("did not return \"redirect:/campus?toolName=bus\" but instead " + viewName, REDIRECT_HOME.equalsIgnoreCase(viewName));
108
109 user.setViewCampus(VIEW_CAMPUS[1]);
110 Properties properties = new Properties();
111 properties.setProperty("maps.center.lat", "1");
112 properties.setProperty("maps.center.lon", "-1");
113 when(applicationContext.getBean(any(String.class))).thenReturn(properties);
114 viewName = getController().index(getRequest(), getUiModel());
115 assertTrue("uiModel initialLatitude was not set to 1!", getUiModel().asMap().get("initialLatitude").equals("1"));
116 assertTrue("uiModel initialLongitude was not set to -1!", getUiModel().asMap().get("initialLongitude").equals("-1"));
117 assertTrue("did not return \"bus/viewBusTracking\" but instead " + viewName, "bus/viewBusTracking".equalsIgnoreCase(viewName));
118 }
119
120 @Test
121 public void testViewStops() {
122 User user = new UserImpl();
123 user.setLoginName(USER);
124 this.getRequest().getSession().setAttribute(Constants.KME_USER_KEY,user);
125 when(busProperties.getProperty(any(String.class))).thenReturn("map,stops");
126 String viewName = getController().viewStops(getRequest(), getUiModel());
127 assertTrue("did not return \"redirect:/campus?toolName=bus\" but instead " + viewName, REDIRECT_HOME.equalsIgnoreCase(viewName));
128 List<BusRoute> busRoutes = new ArrayList<BusRoute>();
129 BusRoute busRoute = new BusRouteImpl();
130 busRoute.setId(0);
131 busRoute.setColor(null);
132 busRoute.setName("testRoute");
133 busRoute.setPath(null);
134 busRoute.setStops(null);
135 busRoutes.add(busRoute);
136 List<BusStop> busStops = new ArrayList<BusStop>();
137 BusStop busStop = new BusStopImpl();
138 busStops.add(busStop);
139 doReturn(busRoutes).when(busService).getRoutes(any(String.class));
140 doReturn(busStops).when(busService).getStops(any(String.class));
141 user.setViewCampus(VIEW_CAMPUS[1]);
142 String viewName2 = getController().viewStops(getRequest(), getUiModel());
143 assertTrue("did not return \"bus/viewStops\" but instead " + viewName2, "bus/viewStops".equalsIgnoreCase(viewName2));
144 assertTrue("uiModel campus was not set to BL!", getUiModel().asMap().get("campus").equals(VIEW_CAMPUS[1]));
145 assertTrue("uiModel stops was not set!", getUiModel().asMap().get("stops") != null);
146 }
147
148 @Test
149 public void testViewBus() {
150 when(busProperties.getProperty(any(String.class))).thenReturn("map,stops");
151 BusRoute busRoute = new BusRouteImpl();
152 busRoute.setName("testRoute");
153 when(busService.getRoute(any(String.class), any(Long.class))).thenReturn(busRoute);
154 String viewName = getController().viewBus(getRequest(), getUiModel(), "0", "0", VIEW_CAMPUS[1]);
155 assertTrue("did not return \"bus/viewBus\" but instead " + viewName, "bus/viewBus".equalsIgnoreCase(viewName));
156 assertTrue("uiModel busId was not set to 0", getUiModel().asMap().get("busId").equals("0"));
157 assertTrue("uiModel route was not set", getUiModel().asMap().get("route") != null);
158 }
159
160 @Test
161 public void testViewNearByStops() {
162 User user = new UserImpl();
163 user.setLoginName(USER);
164 this.getRequest().getSession().setAttribute(Constants.KME_USER_KEY,user);
165 when(busProperties.getProperty(any(String.class))).thenReturn("map,stops");
166 String viewName = getController().viewNearByStops(getRequest(), getUiModel(), "47", "-30", "0.9");
167 assertTrue("did not return \"redirect:/campus?toolName=bus\" but instead " + viewName, REDIRECT_HOME.equalsIgnoreCase(viewName));
168
169 user.setViewCampus(VIEW_CAMPUS[1]);
170 String expectedString = "bus/viewNearByStops";
171
172
173 List<BusStop> busStops = new ArrayList<BusStop>();
174 BusStop busStop = new BusStopImpl();
175 busStops.add(busStop);
176 doReturn(busStops).when(busService).getStops(any(String.class));
177
178 String viewNameHugeIfCheck = getController().viewNearByStops(getRequest(), getUiModel(), null, null, null);
179 assertTrue("did not return \"" + expectedString + "\"", viewNameHugeIfCheck.equalsIgnoreCase(expectedString));
180 viewNameHugeIfCheck = getController().viewNearByStops(getRequest(), getUiModel(), null, "-30", "0.9");
181 assertTrue("did not return \"" + expectedString + "\"", viewNameHugeIfCheck.equalsIgnoreCase(expectedString));
182 viewNameHugeIfCheck = getController().viewNearByStops(getRequest(), getUiModel(), null, null, "0.9");
183 assertTrue("did not return \"" + expectedString + "\"", viewNameHugeIfCheck.equalsIgnoreCase(expectedString));
184 viewNameHugeIfCheck = getController().viewNearByStops(getRequest(), getUiModel(), "47", "-30", null);
185 assertTrue("did not return \"" + expectedString + "\"", viewNameHugeIfCheck.equalsIgnoreCase(expectedString));
186 viewNameHugeIfCheck = getController().viewNearByStops(getRequest(), getUiModel(), "47", null, null);
187 assertTrue("did not return \"" + expectedString + "\"", viewNameHugeIfCheck.equalsIgnoreCase(expectedString));
188 viewNameHugeIfCheck = getController().viewNearByStops(getRequest(), getUiModel(), "47", "-30", "0.9");
189 assertTrue("did not return \"" + expectedString + "\"", viewNameHugeIfCheck.equalsIgnoreCase(expectedString));
190 assertTrue("uiModel route was not set", getUiModel().asMap().get("stopsnear") != null);
191 }
192
193 @Test
194 public void testViewNearByStops2() {
195 User user = new UserImpl();
196 user.setLoginName(USER);
197 user.setViewCampus(VIEW_CAMPUS[1]);
198 this.getRequest().getSession().setAttribute(Constants.KME_USER_KEY,user);
199 doReturn(null).when(busService).getStops(any(String.class));
200 String viewName = getController().viewNearByStops(getRequest(), getUiModel(), "47", "-30", "0.9");
201 String expectedString = "bus/viewNearByStops";
202 assertTrue("did not return \"" + expectedString + "\"", viewName.equalsIgnoreCase(expectedString));
203 }
204
205 @Test
206 public void testViewRoute() {
207 when(busService.getRoute(any(String.class), any(Long.class))).thenReturn(null);
208 String viewName = getController().viewRoute(getRequest(), getUiModel(), "0", VIEW_CAMPUS[1]);
209 assertTrue("campus was not set", getUiModel().asMap().get("campus").equals(VIEW_CAMPUS[1]));
210 assertTrue("the return string is not \"bus/viewRoute\"", "bus/viewRoute".equalsIgnoreCase(viewName));
211 }
212
213 @Test
214 public void testViewFavoriteStops() {
215 User user = new UserImpl();
216 user.setLoginName(USER);
217 this.getRequest().getSession().setAttribute(Constants.KME_USER_KEY,user);
218 String viewName = getController().viewFavoriteStops(getRequest(), getUiModel());
219 assertTrue("did not return \"redirect:/campus?toolName=bus\" but instead " + viewName, REDIRECT_HOME.equalsIgnoreCase(viewName));
220 user.setViewCampus(VIEW_CAMPUS[1]);
221 viewName = getController().viewFavoriteStops(getRequest(), getUiModel());
222 assertTrue("campus was not set", getUiModel().asMap().get("campus").equals(VIEW_CAMPUS[1]));
223 assertTrue("the return string is not \"bus/favoriteStops\"", "bus/favoriteStops".equalsIgnoreCase(viewName));
224 }
225
226 @Test
227 public void testViewStop() {
228 when(busProperties.getProperty(any(String.class))).thenReturn("map,stops");
229 List<BusRoute> busRoutes = new ArrayList<BusRoute>();
230 BusStop busStop = new BusStopImpl();
231 doReturn(busRoutes).when(busService).getRoutes(any(String.class));
232 when(busService.getStop(any(String.class), any(Long.class))).thenReturn(busStop);
233 String viewName = getController().viewStop(getRequest(), getUiModel(), "0", "1", VIEW_CAMPUS[1]);
234 assertTrue("campus was not set", "bus/viewStop".equalsIgnoreCase(viewName));
235 assertTrue("campus was not set", getUiModel().asMap().get("campus").equals(VIEW_CAMPUS[1]));
236 assertTrue("campus was not set", getUiModel().asMap().get("routes") != null);
237 assertTrue("campus was not set", getUiModel().asMap().get("routeId").equals("0"));
238 assertTrue("campus was not set", getUiModel().asMap().get("stop") != null);
239 }
240
241 @Test
242 public void testViewRouteTracking() {
243 User user = new UserImpl();
244 user.setLoginName(USER);
245 this.getRequest().getSession().setAttribute(Constants.KME_USER_KEY,user);
246 String viewName = getController().viewRouteTracking(getRequest(), getUiModel());
247 assertTrue("did not return \"redirect:/campus?toolName=bus\" but instead " + viewName, REDIRECT_HOME.equalsIgnoreCase(viewName));
248 user.setViewCampus(VIEW_CAMPUS[1]);
249 viewName = getController().viewRouteTracking(getRequest(), getUiModel());
250 assertTrue("did not return \"bus/viewRouteTracking\" but instead " + viewName, "bus/viewRouteTracking".equalsIgnoreCase(viewName));
251 assertTrue("uiModel campus was not set to BL!", getUiModel().asMap().get("campus").equals(VIEW_CAMPUS[1]));
252 }
253
254 @Test
255 public void testbusTracking() {
256 User user = new UserImpl();
257 user.setLoginName(USER);
258 this.getRequest().getSession().setAttribute(Constants.KME_USER_KEY,user);
259 String viewName = getController().viewBusTracking(getRequest(), getUiModel(), "0", "0");
260 assertTrue("did not return \"redirect:/campus?toolName=bus\" but instead " + viewName, REDIRECT_HOME.equalsIgnoreCase(viewName));
261 user.setViewCampus(VIEW_CAMPUS[1]);
262 Properties properties = new Properties();
263 properties.setProperty("maps.center.lat", "1");
264 properties.setProperty("maps.center.lon", "-1");
265 when(applicationContext.getBean(any(String.class))).thenReturn(properties);
266 viewName = getController().viewBusTracking(getRequest(), getUiModel(), "0", "0");
267 assertTrue("did not return \"bus/viewBusTracking\" but instead " + viewName, "bus/viewBusTracking".equalsIgnoreCase(viewName));
268 assertTrue("uiModel campus was not set to BL!", getUiModel().asMap().get("campus").equals(VIEW_CAMPUS[1]));
269 assertTrue("uiModel initialLatitude was not set to 1!", getUiModel().asMap().get("initialLatitude").equals("1"));
270 assertTrue("uiModel initialLongitude was not set to -1!", getUiModel().asMap().get("initialLongitude").equals("-1"));
271 assertTrue("uiModel routeId was not set to 0!", getUiModel().asMap().get("routeId").equals("0"));
272 viewName = getController().viewBusTracking(getRequest(), getUiModel(), null, null);
273 assertTrue("uiModel routeId was not set to null!", getUiModel().asMap().get("routeId") == null);
274 }
275
276 @Test
277 public void testBusETAs() {
278 List<BusStop> busStops = new ArrayList<BusStop>();
279 doReturn(busStops).when(busService).getStops(any(String.class));
280 String jsonString = getController().busstop(0, getUiModel());
281 assertTrue("null was not returned", jsonString == null);
282 }
283
284 @Test
285 public void testBusETAs2() {
286 BusStop busStop1 = new BusStopImpl();
287 busStop1.setId(1);
288 BusStop busStop2 = new BusStopImpl();
289 busStop2.setId(0);
290 List<BusStop> busStops = new ArrayList<BusStop>();
291 busStops.add(busStop1);
292 busStops.add(busStop2);
293 doReturn(busStops).when(busService).getStops(any(String.class));
294 String jsonString = getController().busstop(0, getUiModel());
295 assertTrue("null was returned", jsonString != null);
296 }
297
298 public BusController getController() {
299 return controller;
300 }
301
302 public void setController(BusController controller) {
303 this.controller = controller;
304 }
305
306 public BusService getBusService() {
307 return busService;
308 }
309
310 public void setBusService(BusService busService) {
311 this.busService = busService;
312 }
313
314 public MockHttpServletRequest getRequest() {
315 return request;
316 }
317
318 public void setRequest(MockHttpServletRequest request) {
319 this.request = request;
320 }
321
322 public MockHttpServletResponse getResponse() {
323 return response;
324 }
325
326 public void setResponse(MockHttpServletResponse response) {
327 this.response = response;
328 }
329
330 public Model getUiModel() {
331 return uiModel;
332 }
333
334 public void setUiModel(Model uiModel) {
335 this.uiModel = uiModel;
336 }
337
338 public Properties getBusProperties() {
339 return busProperties;
340 }
341
342 public void setBusProperties(Properties busProperties) {
343 this.busProperties = busProperties;
344 }
345
346 public ApplicationContext getApplicationContext() {
347 return applicationContext;
348 }
349
350 public void setApplicationContext(ApplicationContext applicationContext) {
351 this.applicationContext = applicationContext;
352 }
353
354
355
356
357
358
359
360
361
362 }