1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.bus.service;
16
17 import org.apache.log4j.Logger;
18 import org.junit.AfterClass;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.kuali.mobility.bus.dao.BusDao;
23 import org.kuali.mobility.bus.entity.BusRoute;
24 import org.kuali.mobility.bus.entity.BusRouteImpl;
25 import org.kuali.mobility.bus.entity.BusStop;
26 import org.kuali.mobility.bus.entity.BusStopImpl;
27 import org.kuali.mobility.bus.entity.ScheduledStop;
28 import org.kuali.mobility.bus.entity.ScheduledStopImpl;
29 import org.kuali.mobility.bus.service.util.BusStopDistanceUtil;
30 import org.kuali.mobility.bus.service.util.BusStopTransform;
31 import org.mockito.Mock;
32 import org.springframework.context.ApplicationContext;
33 import static org.mockito.Mockito.when;
34 import static org.mockito.Mockito.doNothing;
35 import java.util.ArrayList;
36 import java.util.List;
37
38 import static org.junit.Assert.assertNotNull;
39 import static org.junit.Assert.assertTrue;
40
41
42
43
44
45
46 @RunWith(org.mockito.runners.MockitoJUnitRunner.class)
47 public class BusServiceImplTest {
48 private static final Logger LOG = Logger.getLogger(BusServiceImplTest.class);
49
50
51 private BusServiceImpl service;
52
53 @Mock
54 BusDao busDao;
55 @Mock
56 BusStopTransform busStopTransform;
57 @Mock
58 ApplicationContext applicationContext;
59
60
61 public BusServiceImplTest() {
62 }
63
64 @Before
65 public void setUpClass() throws Exception {
66 setService(new BusServiceImpl());
67 getService().setDao(busDao);
68 getService().setStopTransform(busStopTransform);
69 getService().setApplicationContext(applicationContext);
70 }
71
72 @AfterClass
73 public static void tearDownClass() throws Exception {
74 }
75
76 @Test
77 public void testGetRoutes() {
78 List<BusRoute> busRoutes = new ArrayList<BusRoute>();
79 BusRoute busRoute = new BusRouteImpl();
80 busRoute.setName("TestRoute");
81 busRoutes.add(busRoute);
82 List<BusRoute> busRoutes2 = new ArrayList<BusRoute>();
83 BusRoute busRoute2 = new BusRouteImpl();
84 busRoute2.setName("TestRoute2");
85 busRoutes2.add(busRoute2);
86
87 doNothing().when(busDao).loadRoutes();
88 when(busDao.getBusRoutes()).thenReturn(busRoutes).thenReturn(null).thenReturn(busRoutes2);
89
90 List<BusRouteImpl> routes = service.getRoutes(null);
91 assertTrue("Failed to return TestRoute.", busRoute.getName().equalsIgnoreCase(routes.get(0).getName()));
92
93 List<BusRouteImpl> routes2 = service.getRoutes(null);
94 assertTrue("Did not find TestRoute2 but instead: " + routes2.get(0).getName(), routes2.get(0).getName().equalsIgnoreCase(busRoute2.getName()));
95 }
96
97 @Test
98 public void testGetRoute() {
99 List<BusRoute> busRoutes = new ArrayList<BusRoute>();
100 BusRoute busRoute = new BusRouteImpl();
101 busRoute.setName("TestRoute");
102 busRoute.setId(0);
103 busRoutes.add(busRoute);
104 BusRoute busRoute2 = new BusRouteImpl();
105 busRoute2.setName("TestRoute2");
106 busRoute2.setId(1);
107 busRoutes.add(busRoute2);
108
109 doNothing().when(busDao).loadRoutes();
110 when(busDao.getBusRoutes()).thenReturn(busRoutes).thenReturn(null).thenReturn(busRoutes).thenReturn(null).thenReturn(new ArrayList<BusRoute>());
111
112 BusRouteImpl foundRoute = service.getRoute(null, 0);
113 assertTrue("Route id 0 not found", busRoute.getName().equalsIgnoreCase(foundRoute.getName()));
114
115 BusRouteImpl foundRoute2 = service.getRoute(null, 1);
116 assertTrue("Route id 1 not found", busRoute2.getName().equalsIgnoreCase(foundRoute2.getName()));
117
118 BusRouteImpl foundRoute3 = service.getRoute(null, 1);
119 assertTrue("Routes was not null.", foundRoute3 == null);
120 }
121
122 @Test
123 public void testGetStops() {
124 List<BusStop> busStops = new ArrayList<BusStop>();
125 BusStop busStop = new BusStopImpl();
126 busStop.setName("TestStop");
127 busStops.add(busStop);
128 List<BusStop> busStops2 = new ArrayList<BusStop>();
129 BusStop busStop2 = new BusStopImpl();
130 busStop2.setName("TestStop2");
131 busStops2.add(busStop2);
132
133 doNothing().when(busDao).loadRoutes();
134 when(busDao.getBusStops()).thenReturn(busStops).thenReturn(null).thenReturn(busStops2);
135
136 List<BusStopImpl> stops = service.getStops(null);
137 assertTrue("Failed to return TestStop.", busStop.getName().equalsIgnoreCase(stops.get(0).getName()));
138
139 List<BusStopImpl> stops2 = service.getStops(null);
140 assertTrue("Failed to return TestStop2", busStop2.getName().equalsIgnoreCase(stops2.get(0).getName()));
141 }
142
143 @Test
144 public void testGetRoutesWithDistance() {
145 List<BusRoute> busRoutes = new ArrayList<BusRoute>();
146 BusRoute busRoute = new BusRouteImpl();
147 busRoute.setName("testRoute");
148 BusStop busStop = new BusStopImpl();
149 busStop.setLatitude("42.2693733");
150 busStop.setLongitude("-83.7460491");
151 List<BusStop> busStops = new ArrayList<BusStop>();
152 busStops.add(busStop);
153 busRoute.setStops(busStops);
154 busRoutes.add(busRoute);
155 double lat1 = 42.2693773;
156 double lon1 = -83.7460591;
157 double radius = 0.9;
158 when(busDao.getBusRoutes()).thenReturn(busRoutes);
159 List<? extends BusRoute> routes = service.getRoutesWithDistance(lat1, lon1, radius);
160 assertTrue("testRoute does not have a distance for its stop", routes.get(0).getStops().get(0).getDistance() > 0);
161 }
162
163 @Test
164 public void testGetNearbyStops() {
165 BusStop busStop = new BusStopImpl();
166 busStop.setLatitude("42.2693763");
167 busStop.setLongitude("-83.7460581");
168 BusStop busStop2 = new BusStopImpl();
169 busStop2.setLatitude("82.2693733");
170 busStop2.setLongitude("-13.7460491");
171 List<BusStop> busStops = new ArrayList<BusStop>();
172 busStops.add(busStop);
173 busStops.add(busStop2);
174
175 List<BusStop> busStops2 = new ArrayList<BusStop>();
176 busStops.add(busStop2);
177
178 double lat1 = 42.2693773;
179 double lon1 = -83.7460591;
180 double radius = 0.9;
181 when(busDao.getBusStops()).thenReturn(busStops).thenReturn(null).thenReturn(busStops2);
182 List<BusStopImpl> stopstest = service.getNearbyStops(lat1, lon1, radius);
183 for (BusStop s : stopstest) {
184 LOG.debug("BusStop ID: " + s.getId() + " ,name :" + s.getName() + ", distance: " + s.getDistance());
185 }
186 assertTrue("Failed to find nearby bus stops.", stopstest.size() > 0);
187 List<BusStopImpl> stopstest2 = service.getNearbyStops(lat1, lon1, radius);
188 assertTrue("Found a nearby bus Stops.", stopstest2.size() == 0);
189 }
190
191 @Test
192 public void testGetStop() {
193 List<BusStop> busStops = new ArrayList<BusStop>();
194 BusStop busStop = new BusStopImpl();
195 busStop.setId(0);
196 busStop.setName("TestStop");
197 busStops.add(busStop);
198 BusStop busStop2 = new BusStopImpl();
199 busStop2.setId(1);
200 busStop2.setName("TestStop2");
201 busStops.add(busStop2);
202
203 doNothing().when(busDao).loadRoutes();
204 when(busDao.getBusStops()).thenReturn(busStops).thenReturn(null).thenReturn(new ArrayList<BusStop>());
205
206 BusStopImpl stop = service.getStop(null, 1);
207 assertTrue("Stops ids do not match", busStop2.getId() == stop.getId());
208 BusStopImpl stop2 = service.getStop(null, 0);
209 assertTrue("Found a stop when it should not have", stop2 == null);
210 }
211
212 @Test
213 public void testGetStopByName() {
214 List<BusStop> busStops = new ArrayList<BusStop>();
215 BusStop busStop = new BusStopImpl();
216 busStop.setDistance(12);
217 busStop.setId(0);
218 busStop.setLatitude("44");
219 busStop.setLongitude("-23");
220 busStop.setName("TestStop");
221 busStop.setSchedule(null);
222 busStops.add(busStop);
223 BusStop busStop3 = new BusStopImpl();
224 busStop3.setDistance(12);
225 busStop3.setId(3);
226 busStop3.setLatitude("44");
227 busStop3.setLongitude("-23");
228 busStop3.setName(null);
229 busStop3.setSchedule(null);
230 busStops.add(busStop3);
231 BusStop busStop2 = new BusStopImpl();
232 busStop2.setDistance(12);
233 busStop2.setId(1);
234 busStop2.setLatitude("44");
235 busStop2.setLongitude("-23");
236 busStop2.setName("TestStop2");
237 busStop2.setSchedule(null);
238 busStops.add(busStop2);
239
240 BusStopImpl busStopImpl = new BusStopImpl();
241 busStopImpl.setId(busStop2.getId());
242 busStopImpl.setName(busStop2.getName());
243 doNothing().when(busDao).loadRoutes();
244
245 when(busDao.getBusStops()).thenReturn(busStops).thenReturn(null).thenReturn(new ArrayList<BusStop>()).thenReturn(busStops);
246 when(busStopTransform.transform(busStop2)).thenReturn(busStopImpl);
247
248 BusStopImpl stop = service.getStopByName(busStop2.getName(), null);
249 assertTrue("Stops names do not match", busStop2.getName().equalsIgnoreCase(stop.getName()));
250 BusStopImpl nullStop = service.getStopByName(busStop.getName(), null);
251 assertTrue("Stop is not null", nullStop == null);
252 }
253
254 @Test
255 public void testBusStopDistanceUtil() {
256 BusStop stop = new BusStopImpl();
257 stop.setLatitude("42.2693733");
258 stop.setLongitude("-83.7460491");
259
260 double lat1 = 42.2693773;
261 double lon1 = -83.7460591;
262 LOG.debug("Busstop 927605132");
263
264 if (stop != null) {
265 BusStopDistanceUtil bsu = new BusStopDistanceUtil();
266 double distance = bsu.calculateDistance(stop, lat1, lon1);
267 stop.setDistance(distance);
268 LOG.debug("BusStop ID: " + stop.getId() + " ,name :" + stop.getName() + ", calculated distance:" + distance);
269 }
270 assertTrue("Failed to calculate bus distance.", stop.getDistance() > 0);
271
272 }
273
274 @Test
275 public void testGetArrivalData() {
276 BusRoute busRoute1 = new BusRouteImpl();
277 busRoute1.setId(0);
278 BusRoute busRoute2 = new BusRouteImpl();
279 busRoute2.setId(1);
280 busRoute2.setName("TestRoute2");
281 BusStop busStop1 = new BusStopImpl();
282 busStop1.setId(0);
283 BusStop busStop2 = new BusStopImpl();
284 busStop2.setId(1);
285 ScheduledStop scheduledStop = new ScheduledStopImpl();
286 scheduledStop.setBusStopRouteName(busRoute2.getName());
287 List<ScheduledStop> scheduledStops = new ArrayList<ScheduledStop>();
288 scheduledStops.add(scheduledStop);
289 busStop2.setSchedule(scheduledStops);
290
291 List<BusStop> busStops = new ArrayList<BusStop>();
292 busStops.add(busStop1);
293 busStops.add(busStop2);
294 busRoute2.setStops(busStops);
295 List<BusRoute> busRoutes = new ArrayList<BusRoute>();
296 busRoutes.add(busRoute1);
297 busRoutes.add(busRoute2);
298
299 when(busDao.getBusRoutes()).thenReturn(busRoutes);
300 when(busDao.getBusStops()).thenReturn(busStops);
301
302 List<ScheduledStopImpl> nullRoutesArrivals = service.getArrivalData("3", "1", "ALL");
303 assertTrue("found scheduled stops when it should not have", nullRoutesArrivals.size() == 0);
304 List<ScheduledStopImpl> noStopsArrivals = service.getArrivalData("0", "1", "ALL");
305 assertTrue("found scheduled stops when it should not have", noStopsArrivals.size() == 0);
306 List<ScheduledStopImpl> arrivals = service.getArrivalData("1", "1", "ALL");
307 assertTrue("No scheduled stops found.", arrivals.size() > 0);
308 List<ScheduledStopImpl> zeroArrivals = service.getArrivalData(null, "3", "ALL");
309 assertTrue("found scheduled stops when it should not have", zeroArrivals.size() == 0);
310 }
311
312 public BusServiceImpl getService() {
313 return service;
314 }
315
316 public void setService(BusServiceImpl service) {
317 this.service = service;
318 }
319 }