1 package net.jaekl.frank.octranspo;
3 import java.io.IOException;
4 import java.text.DateFormat;
5 import java.text.ParseException;
6 import java.text.SimpleDateFormat;
7 import java.util.ArrayList;
10 import org.junit.Assert;
11 import net.jaekl.frank.FrankException;
12 import net.jaekl.qd.QDException;
13 import net.jaekl.qd.http.RequestBrokerMock;
15 import org.apache.http.NameValuePair;
16 import org.apache.http.message.BasicNameValuePair;
17 import org.junit.Test;
19 public class ServerTest {
20 private static final double TINY_DELTA = 0.00000000001;
21 private static final String ROUTE_SUMMARY_FOR_STOP =
22 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
23 + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
24 + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
25 + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
27 + " <GetRouteSummaryForStopResponse xmlns=\"http://octranspo.com\">\n"
28 + " <GetRouteSummaryForStopResult>\n"
29 + " <StopNo xmlns=\"http://tempuri.org/\">1234</StopNo>\n"
31 + " xmlns=\"http://tempuri.org/\">ONE-TWO-THREE-FOUR</StopDescription>\n"
32 + " <Error xmlns=\"http://tempuri.org/\"/>\n"
33 + " <Routes xmlns=\"http://tempuri.org/\">\n"
35 + " <RouteNo>123</RouteNo>\n"
36 + " <DirectionID>0</DirectionID>\n"
37 + " <Direction>NORTH</Direction>\n"
38 + " <RouteHeading>First Mall</RouteHeading>\n"
41 + " <RouteNo>123</RouteNo>\n"
42 + " <DirectionID>1</DirectionID>\n"
43 + " <Direction>SOUTH</Direction>\n"
44 + " <RouteHeading>Second Mall</RouteHeading>\n"
47 + " </GetRouteSummaryForStopResult>\n"
48 + " </GetRouteSummaryForStopResponse>\n"
50 + "</soap:Envelope>\n";
52 private static final String NEXT_TRIPS_FOR_STOP =
53 "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
54 "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
56 "<GetNextTripsForStopResponse xmlns=\"http://octranspo.com\">" +
57 "<GetNextTripsForStopResult>" +
58 "<StopNo xmlns=\"http://tempuri.org/\">7659</StopNo>" +
59 "<StopLabel xmlns=\"http://tempuri.org/\">BANK FIFTH</StopLabel>" +
60 "<Error xmlns=\"http://tempuri.org/\" />" +
61 "<Route xmlns=\"http://tempuri.org/\">" +
63 "<RouteNo>1</RouteNo><RouteLabel>Ottawa-Rockcliffe</RouteLabel>" +
64 "<Direction>Northbound</Direction><Error /><RequestProcessingTime>20141130111450</RequestProcessingTime>" +
67 "<TripDestination>Ottawa Rockcliffe</TripDestination>" +
68 "<TripStartTime>11:10</TripStartTime>" +
69 "<AdjustedScheduleTime>20</AdjustedScheduleTime>" +
70 "<AdjustmentAge>0.18</AdjustmentAge>" +
71 "<LastTripOfSchedule>false</LastTripOfSchedule>" +
72 "<BusType>4LA - DEH</BusType>" +
73 "<Latitude>45.352026</Latitude><Longitude>-75.649993</Longitude><GPSSpeed>50.0</GPSSpeed>" +
76 "<TripDestination>Ottawa Rockcliffe</TripDestination>" +
77 "<TripStartTime>11:40</TripStartTime>" +
78 "<AdjustedScheduleTime>51</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge>" +
79 "<LastTripOfSchedule>false</LastTripOfSchedule><BusType>4E - DEH</BusType>" +
80 "<Latitude /><Longitude /><GPSSpeed />" +
83 "<TripDestination>Ottawa Rockcliffe</TripDestination>" +
84 "<TripStartTime>12:00</TripStartTime>" +
85 "<AdjustedScheduleTime>71</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge>" +
86 "<LastTripOfSchedule>false</LastTripOfSchedule><BusType>4E - DEH</BusType>" +
87 "<Latitude /><Longitude /><GPSSpeed />" +
91 "</GetNextTripsForStopResult>" +
92 "</GetNextTripsForStopResponse>" +
96 private static final String NEXT_TRIPS_FOR_STOP_ALL_ROUTES =
97 "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
98 "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
100 "<GetRouteSummaryForStopResponse xmlns=\"http://octranspo.com\">" +
101 "<GetRouteSummaryForStopResult>" +
102 "<StopNo xmlns=\"http://tempuri.org/\">7659</StopNo>" +
103 "<StopDescription xmlns=\"http://tempuri.org/\">BANK FIFTH</StopDescription>" +
104 "<Error xmlns=\"http://tempuri.org/\"/>" +
105 "<Routes xmlns=\"http://tempuri.org/\">" +
107 "<RouteNo>1</RouteNo><DirectionID>1</DirectionID><Direction>Northbound</Direction>" +
108 "<RouteHeading>Ottawa-Rockcliffe</RouteHeading>" +
111 "<TripDestination>Ottawa Rockcliffe</TripDestination><TripStartTime>11:40</TripStartTime>" +
112 "<AdjustedScheduleTime>2</AdjustedScheduleTime><AdjustmentAge>0.55</AdjustmentAge>" +
113 "<LastTripOfSchedule/><BusType>4E - DEH</BusType>" +
114 "<Latitude>45.392965</Latitude><Longitude>-75.682561</Longitude><GPSSpeed>35.7</GPSSpeed>" +
117 "<TripDestination>Ottawa Rockcliffe</TripDestination><TripStartTime>12:00</TripStartTime>" +
118 "<AdjustedScheduleTime>17</AdjustedScheduleTime><AdjustmentAge>0.62</AdjustmentAge>" +
119 "<LastTripOfSchedule/><BusType>4E - DEH</BusType>" +
120 "<Latitude>45.362237</Latitude><Longitude>-75.658112</Longitude><GPSSpeed>57.6</GPSSpeed>" +
123 "<TripDestination>Ottawa Rockcliffe</TripDestination><TripStartTime>12:20</TripStartTime>" +
124 "<AdjustedScheduleTime>37</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge>" +
125 "<LastTripOfSchedule/><BusType>4E - DEH</BusType>" +
126 "<Latitude/><Longitude/><GPSSpeed/>" +
131 "<RouteNo>7</RouteNo><DirectionID>1</DirectionID><Direction>Eastbound</Direction>" +
132 "<RouteHeading>St-Laurent</RouteHeading>" +
133 "<Trips><Trip><TripDestination>St. Laurent</TripDestination><TripStartTime>12:11</TripStartTime>" +
134 "<AdjustedScheduleTime>12</AdjustedScheduleTime><AdjustmentAge>0.61</AdjustmentAge>" +
135 "<LastTripOfSchedule/><BusType>4LB - IN</BusType>" +
136 "<Latitude>45.384370</Latitude><Longitude>-75.696361</Longitude><GPSSpeed>0.5</GPSSpeed>" +
139 "<TripDestination>St. Laurent</TripDestination><TripStartTime>12:31</TripStartTime>" +
140 "<AdjustedScheduleTime>32</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge>" +
141 "<LastTripOfSchedule/><BusType>4L - DEH</BusType><Latitude/><Longitude/><GPSSpeed/>" +
144 "<TripDestination>St. Laurent</TripDestination><TripStartTime>12:51</TripStartTime>" +
145 "<AdjustedScheduleTime>52</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge>" +
146 "<LastTripOfSchedule/><BusType>4LA - DEH</BusType>" +
147 "<Latitude/><Longitude/><GPSSpeed/>" +
152 "</GetRouteSummaryForStopResult>" +
153 "</GetRouteSummaryForStopResponse>" +
158 public void testGetRouteSummaryForStop() throws IOException, QDException, FrankException {
159 ServerMock sm = new ServerMock();
160 RequestBrokerMock broker = sm.mock_getBroker();
162 ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
163 params.add(new BasicNameValuePair(Server.STOP_NO, "1234"));
165 broker.setResult(Server.GET_ROUTE_SUMMARY_FOR_STOP, params, ROUTE_SUMMARY_FOR_STOP);
167 StopInfo stopInfo = sm.getRouteSummaryForStop(1234);
169 Assert.assertNotNull(stopInfo);
170 Assert.assertEquals(1234, stopInfo.getStopNo());
171 Assert.assertEquals("ONE-TWO-THREE-FOUR", stopInfo.getDescr());
172 Assert.assertEquals("", stopInfo.getError());
173 Assert.assertEquals(2, stopInfo.getNumRoutes());
177 route = stopInfo.getRoute(0);
178 Assert.assertEquals(123, route.getRouteNo());
179 Assert.assertEquals(0, route.getDirectionID());
180 Assert.assertEquals("NORTH", route.getDirection());
181 Assert.assertEquals("First Mall", route.getRouteHeading());
183 route = stopInfo.getRoute(1);
184 Assert.assertEquals(123, route.getRouteNo());
185 Assert.assertEquals(1, route.getDirectionID());
186 Assert.assertEquals("SOUTH", route.getDirection());
187 Assert.assertEquals("Second Mall", route.getRouteHeading());
191 public void testGetNextTripsForStop() throws FrankException, IOException, ParseException {
195 DateFormat hourMinFormat = new SimpleDateFormat("hh:mm");
197 ServerMock sm = new ServerMock();
198 RequestBrokerMock broker = sm.mock_getBroker();
200 ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
201 params.add(new BasicNameValuePair(Server.STOP_NO, "7659"));
202 params.add(new BasicNameValuePair(Server.ROUTE_NO, "1"));
204 broker.setResult(Server.GET_NEXT_TRIPS_FOR_STOP, params, NEXT_TRIPS_FOR_STOP);
206 StopInfo stopInfo = sm.getNextTripsForStop(7659, 1);
208 Assert.assertNotNull(stopInfo);
209 Assert.assertEquals(7659, stopInfo.getStopNo());
210 Assert.assertEquals("BANK FIFTH", stopInfo.getDescr());
211 Assert.assertEquals("", stopInfo.getError());
212 Assert.assertEquals(1, stopInfo.getNumRoutes());
214 route = stopInfo.getRoute(0);
215 Assert.assertEquals(1, route.getRouteNo());
216 Assert.assertEquals("Northbound", route.getDirection());
217 Assert.assertEquals("Ottawa-Rockcliffe", route.getRouteHeading());
218 Assert.assertEquals(3, route.getNumTrips());
220 trip = route.getTrip(0);
221 Assert.assertEquals("Ottawa Rockcliffe", trip.getDest());
222 expectedDate = hourMinFormat.parse("11:10");
223 Assert.assertEquals(expectedDate, trip.getStart());
224 Assert.assertEquals(20, trip.getAdjTime());
225 Assert.assertEquals(0.18, trip.getAdjAge(), TINY_DELTA);
226 Assert.assertEquals(false, trip.isLastTrip());
227 Assert.assertEquals("4LA - DEH", trip.getBusType());
228 Assert.assertEquals(50.0, trip.getSpeed(), TINY_DELTA);
229 Assert.assertEquals(45.352026, trip.getLatitude(), TINY_DELTA);
230 Assert.assertEquals(-75.649993, trip.getLongitude(), TINY_DELTA);
232 trip = route.getTrip(2);
233 Assert.assertEquals("Ottawa Rockcliffe", trip.getDest());
234 expectedDate = hourMinFormat.parse("12:00");
235 Assert.assertEquals(expectedDate, trip.getStart());
236 Assert.assertEquals(71, trip.getAdjTime());
237 Assert.assertEquals(-1.0, trip.getAdjAge(), TINY_DELTA);
238 Assert.assertEquals(false, trip.isLastTrip());
239 Assert.assertEquals("4E - DEH", trip.getBusType());
240 Assert.assertEquals(0.0, trip.getSpeed(), TINY_DELTA);
241 Assert.assertEquals(0.0, trip.getLatitude(), TINY_DELTA);
242 Assert.assertEquals(0.0, trip.getLongitude(), TINY_DELTA);
246 public void testGetNextTripsForStopAllRoutes() throws IOException, FrankException, ParseException {
250 DateFormat hourMinFormat = new SimpleDateFormat("hh:mm");
252 ServerMock sm = new ServerMock();
253 RequestBrokerMock broker = sm.mock_getBroker();
255 ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
256 params.add(new BasicNameValuePair(Server.STOP_NO, "7659"));
258 broker.setResult(Server.GET_NEXT_TRIPS_FOR_STOP_ALL_ROUTES, params, NEXT_TRIPS_FOR_STOP_ALL_ROUTES);
260 StopInfo stopInfo = sm.getNextTripsForStopAllRoutes(7659);
262 Assert.assertNotNull(stopInfo);
263 Assert.assertEquals(7659, stopInfo.getStopNo());
264 Assert.assertEquals("BANK FIFTH", stopInfo.getDescr());
265 Assert.assertEquals("", stopInfo.getError());
266 Assert.assertEquals(2, stopInfo.getNumRoutes());
268 route = stopInfo.getRoute(0);
269 Assert.assertEquals(1, route.getRouteNo());
270 Assert.assertEquals("Northbound", route.getDirection());
271 Assert.assertEquals("Ottawa-Rockcliffe", route.getRouteHeading());
272 Assert.assertEquals(3, route.getNumTrips());
274 trip = route.getTrip(0);
275 Assert.assertEquals("Ottawa Rockcliffe", trip.getDest());
276 expectedDate = hourMinFormat.parse("11:40");
277 Assert.assertEquals(expectedDate, trip.getStart());
278 Assert.assertEquals(2, trip.getAdjTime());
279 Assert.assertEquals(0.55, trip.getAdjAge(), TINY_DELTA);
280 Assert.assertEquals(false, trip.isLastTrip());
281 Assert.assertEquals("4E - DEH", trip.getBusType());
282 Assert.assertEquals(35.7, trip.getSpeed(), TINY_DELTA);
283 Assert.assertEquals(45.392965, trip.getLatitude(), TINY_DELTA);
284 Assert.assertEquals(-75.682561, trip.getLongitude(), TINY_DELTA);
286 trip = route.getTrip(2);
287 Assert.assertEquals("Ottawa Rockcliffe", trip.getDest());
288 expectedDate = hourMinFormat.parse("12:20");
289 Assert.assertEquals(expectedDate, trip.getStart());
290 Assert.assertEquals(37, trip.getAdjTime());
291 Assert.assertEquals(-1.0, trip.getAdjAge(), TINY_DELTA);
292 Assert.assertEquals(false, trip.isLastTrip());
293 Assert.assertEquals("4E - DEH", trip.getBusType());
294 Assert.assertEquals(0.0, trip.getSpeed(), TINY_DELTA);
295 Assert.assertEquals(0.0, trip.getLatitude(), TINY_DELTA);
296 Assert.assertEquals(0.0, trip.getLongitude(), TINY_DELTA);