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 junit.framework.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 String ROUTE_SUMMARY_FOR_STOP =
21 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
22 + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
23 + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
24 + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
26 + " <GetRouteSummaryForStopResponse xmlns=\"http://octranspo.com\">\n"
27 + " <GetRouteSummaryForStopResult>\n"
28 + " <StopNo xmlns=\"http://tempuri.org/\">1234</StopNo>\n"
30 + " xmlns=\"http://tempuri.org/\">ONE-TWO-THREE-FOUR</StopDescription>\n"
31 + " <Error xmlns=\"http://tempuri.org/\"/>\n"
32 + " <Routes xmlns=\"http://tempuri.org/\">\n"
34 + " <RouteNo>123</RouteNo>\n"
35 + " <DirectionID>0</DirectionID>\n"
36 + " <Direction>NORTH</Direction>\n"
37 + " <RouteHeading>First Mall</RouteHeading>\n"
40 + " <RouteNo>123</RouteNo>\n"
41 + " <DirectionID>1</DirectionID>\n"
42 + " <Direction>SOUTH</Direction>\n"
43 + " <RouteHeading>Second Mall</RouteHeading>\n"
46 + " </GetRouteSummaryForStopResult>\n"
47 + " </GetRouteSummaryForStopResponse>\n"
49 + "</soap:Envelope>\n";
51 private static final String NEXT_TRIPS_FOR_STOP =
52 "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
53 "<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\">" +
55 "<GetNextTripsForStopResponse xmlns=\"http://octranspo.com\">" +
56 "<GetNextTripsForStopResult>" +
57 "<StopNo xmlns=\"http://tempuri.org/\">7659</StopNo>" +
58 "<StopLabel xmlns=\"http://tempuri.org/\">BANK FIFTH</StopLabel>" +
59 "<Error xmlns=\"http://tempuri.org/\" />" +
60 "<Route xmlns=\"http://tempuri.org/\">" +
62 "<RouteNo>1</RouteNo><RouteLabel>Ottawa-Rockcliffe</RouteLabel>" +
63 "<Direction>Northbound</Direction><Error /><RequestProcessingTime>20141130111450</RequestProcessingTime>" +
66 "<TripDestination>Ottawa Rockcliffe</TripDestination>" +
67 "<TripStartTime>11:10</TripStartTime>" +
68 "<AdjustedScheduleTime>20</AdjustedScheduleTime>" +
69 "<AdjustmentAge>0.18</AdjustmentAge>" +
70 "<LastTripOfSchedule>false</LastTripOfSchedule>" +
71 "<BusType>4LA - DEH</BusType>" +
72 "<Latitude>45.352026</Latitude><Longitude>-75.649993</Longitude><GPSSpeed>50.0</GPSSpeed>" +
75 "<TripDestination>Ottawa Rockcliffe</TripDestination>" +
76 "<TripStartTime>11:40</TripStartTime>" +
77 "<AdjustedScheduleTime>51</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge>" +
78 "<LastTripOfSchedule>false</LastTripOfSchedule><BusType>4E - DEH</BusType>" +
79 "<Latitude /><Longitude /><GPSSpeed />" +
82 "<TripDestination>Ottawa Rockcliffe</TripDestination>" +
83 "<TripStartTime>12:00</TripStartTime>" +
84 "<AdjustedScheduleTime>71</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge>" +
85 "<LastTripOfSchedule>false</LastTripOfSchedule><BusType>4E - DEH</BusType>" +
86 "<Latitude /><Longitude /><GPSSpeed />" +
90 "</GetNextTripsForStopResult>" +
91 "</GetNextTripsForStopResponse>" +
95 private static final String NEXT_TRIPS_FOR_STOP_ALL_ROUTES =
96 "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
97 "<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\">" +
99 "<GetRouteSummaryForStopResponse xmlns=\"http://octranspo.com\">" +
100 "<GetRouteSummaryForStopResult>" +
101 "<StopNo xmlns=\"http://tempuri.org/\">7659</StopNo>" +
102 "<StopDescription xmlns=\"http://tempuri.org/\">BANK FIFTH</StopDescription>" +
103 "<Error xmlns=\"http://tempuri.org/\"/>" +
104 "<Routes xmlns=\"http://tempuri.org/\">" +
106 "<RouteNo>1</RouteNo><DirectionID>1</DirectionID><Direction>Northbound</Direction>" +
107 "<RouteHeading>Ottawa-Rockcliffe</RouteHeading>" +
110 "<TripDestination>Ottawa Rockcliffe</TripDestination><TripStartTime>11:40</TripStartTime>" +
111 "<AdjustedScheduleTime>2</AdjustedScheduleTime><AdjustmentAge>0.55</AdjustmentAge>" +
112 "<LastTripOfSchedule/><BusType>4E - DEH</BusType>" +
113 "<Latitude>45.392965</Latitude><Longitude>-75.682561</Longitude><GPSSpeed>35.7</GPSSpeed>" +
116 "<TripDestination>Ottawa Rockcliffe</TripDestination><TripStartTime>12:00</TripStartTime>" +
117 "<AdjustedScheduleTime>17</AdjustedScheduleTime><AdjustmentAge>0.62</AdjustmentAge>" +
118 "<LastTripOfSchedule/><BusType>4E - DEH</BusType>" +
119 "<Latitude>45.362237</Latitude><Longitude>-75.658112</Longitude><GPSSpeed>57.6</GPSSpeed>" +
122 "<TripDestination>Ottawa Rockcliffe</TripDestination><TripStartTime>12:20</TripStartTime>" +
123 "<AdjustedScheduleTime>37</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge>" +
124 "<LastTripOfSchedule/><BusType>4E - DEH</BusType>" +
125 "<Latitude/><Longitude/><GPSSpeed/>" +
130 "<RouteNo>7</RouteNo><DirectionID>1</DirectionID><Direction>Eastbound</Direction>" +
131 "<RouteHeading>St-Laurent</RouteHeading>" +
132 "<Trips><Trip><TripDestination>St. Laurent</TripDestination><TripStartTime>12:11</TripStartTime>" +
133 "<AdjustedScheduleTime>12</AdjustedScheduleTime><AdjustmentAge>0.61</AdjustmentAge>" +
134 "<LastTripOfSchedule/><BusType>4LB - IN</BusType>" +
135 "<Latitude>45.384370</Latitude><Longitude>-75.696361</Longitude><GPSSpeed>0.5</GPSSpeed>" +
138 "<TripDestination>St. Laurent</TripDestination><TripStartTime>12:31</TripStartTime>" +
139 "<AdjustedScheduleTime>32</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge>" +
140 "<LastTripOfSchedule/><BusType>4L - DEH</BusType><Latitude/><Longitude/><GPSSpeed/>" +
143 "<TripDestination>St. Laurent</TripDestination><TripStartTime>12:51</TripStartTime>" +
144 "<AdjustedScheduleTime>52</AdjustedScheduleTime><AdjustmentAge>-1</AdjustmentAge>" +
145 "<LastTripOfSchedule/><BusType>4LA - DEH</BusType>" +
146 "<Latitude/><Longitude/><GPSSpeed/>" +
151 "</GetRouteSummaryForStopResult>" +
152 "</GetRouteSummaryForStopResponse>" +
157 public void testGetRouteSummaryForStop() throws IOException, QDException, FrankException {
158 ServerMock sm = new ServerMock();
159 RequestBrokerMock broker = sm.mock_getBroker();
161 ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
162 params.add(new BasicNameValuePair(Server.STOP_NO, "1234"));
164 broker.setResult(Server.GET_ROUTE_SUMMARY_FOR_STOP, params, ROUTE_SUMMARY_FOR_STOP);
166 StopInfo stopInfo = sm.getRouteSummaryForStop(1234);
168 Assert.assertNotNull(stopInfo);
169 Assert.assertEquals(1234, stopInfo.getStopNo());
170 Assert.assertEquals("ONE-TWO-THREE-FOUR", stopInfo.getDescr());
171 Assert.assertEquals("", stopInfo.getError());
172 Assert.assertEquals(2, stopInfo.getNumRoutes());
176 route = stopInfo.getRoute(0);
177 Assert.assertEquals(123, route.getRouteNo());
178 Assert.assertEquals(0, route.getDirectionID());
179 Assert.assertEquals("NORTH", route.getDirection());
180 Assert.assertEquals("First Mall", route.getRouteHeading());
182 route = stopInfo.getRoute(1);
183 Assert.assertEquals(123, route.getRouteNo());
184 Assert.assertEquals(1, route.getDirectionID());
185 Assert.assertEquals("SOUTH", route.getDirection());
186 Assert.assertEquals("Second Mall", route.getRouteHeading());
190 public void testGetNextTripsForStop() throws FrankException, IOException, ParseException {
194 DateFormat hourMinFormat = new SimpleDateFormat("hh:mm");
196 ServerMock sm = new ServerMock();
197 RequestBrokerMock broker = sm.mock_getBroker();
199 ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
200 params.add(new BasicNameValuePair(Server.STOP_NO, "7659"));
201 params.add(new BasicNameValuePair(Server.ROUTE_NO, "1"));
203 broker.setResult(Server.GET_NEXT_TRIPS_FOR_STOP, params, NEXT_TRIPS_FOR_STOP);
205 StopInfo stopInfo = sm.getNextTripsForStop(7659, 1);
207 Assert.assertNotNull(stopInfo);
208 Assert.assertEquals(7659, stopInfo.getStopNo());
209 Assert.assertEquals("BANK FIFTH", stopInfo.getDescr());
210 Assert.assertEquals("", stopInfo.getError());
211 Assert.assertEquals(1, stopInfo.getNumRoutes());
213 route = stopInfo.getRoute(0);
214 Assert.assertEquals(1, route.getRouteNo());
215 Assert.assertEquals("Northbound", route.getDirection());
216 Assert.assertEquals("Ottawa-Rockcliffe", route.getRouteHeading());
217 Assert.assertEquals(3, route.getNumTrips());
219 trip = route.getTrip(0);
220 Assert.assertEquals("Ottawa Rockcliffe", trip.getDest());
221 expectedDate = hourMinFormat.parse("11:10");
222 Assert.assertEquals(expectedDate, trip.getStart());
223 Assert.assertEquals(20, trip.getAdjTime());
224 Assert.assertEquals(0.18, trip.getAdjAge());
225 Assert.assertEquals(false, trip.isLastTrip());
226 Assert.assertEquals("4LA - DEH", trip.getBusType());
227 Assert.assertEquals(50.0, trip.getSpeed());
228 Assert.assertEquals(45.352026, trip.getLatitude());
229 Assert.assertEquals(-75.649993, trip.getLongitude());
231 trip = route.getTrip(2);
232 Assert.assertEquals("Ottawa Rockcliffe", trip.getDest());
233 expectedDate = hourMinFormat.parse("12:00");
234 Assert.assertEquals(expectedDate, trip.getStart());
235 Assert.assertEquals(71, trip.getAdjTime());
236 Assert.assertEquals(-1.0, trip.getAdjAge());
237 Assert.assertEquals(false, trip.isLastTrip());
238 Assert.assertEquals("4E - DEH", trip.getBusType());
239 Assert.assertEquals(0.0, trip.getSpeed());
240 Assert.assertEquals(0.0, trip.getLatitude());
241 Assert.assertEquals(0.0, trip.getLongitude());
245 public void testGetNextTripsForStopAllRoutes() throws IOException, FrankException, ParseException {
249 DateFormat hourMinFormat = new SimpleDateFormat("hh:mm");
251 ServerMock sm = new ServerMock();
252 RequestBrokerMock broker = sm.mock_getBroker();
254 ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
255 params.add(new BasicNameValuePair(Server.STOP_NO, "7659"));
257 broker.setResult(Server.GET_NEXT_TRIPS_FOR_STOP_ALL_ROUTES, params, NEXT_TRIPS_FOR_STOP_ALL_ROUTES);
259 StopInfo stopInfo = sm.getNextTripsForStopAllRoutes(7659);
261 Assert.assertNotNull(stopInfo);
262 Assert.assertEquals(7659, stopInfo.getStopNo());
263 Assert.assertEquals("BANK FIFTH", stopInfo.getDescr());
264 Assert.assertEquals("", stopInfo.getError());
265 Assert.assertEquals(2, stopInfo.getNumRoutes());
267 route = stopInfo.getRoute(0);
268 Assert.assertEquals(1, route.getRouteNo());
269 Assert.assertEquals("Northbound", route.getDirection());
270 Assert.assertEquals("Ottawa-Rockcliffe", route.getRouteHeading());
271 Assert.assertEquals(3, route.getNumTrips());
273 trip = route.getTrip(0);
274 Assert.assertEquals("Ottawa Rockcliffe", trip.getDest());
275 expectedDate = hourMinFormat.parse("11:40");
276 Assert.assertEquals(expectedDate, trip.getStart());
277 Assert.assertEquals(2, trip.getAdjTime());
278 Assert.assertEquals(0.55, trip.getAdjAge());
279 Assert.assertEquals(false, trip.isLastTrip());
280 Assert.assertEquals("4E - DEH", trip.getBusType());
281 Assert.assertEquals(35.7, trip.getSpeed());
282 Assert.assertEquals(45.392965, trip.getLatitude());
283 Assert.assertEquals(-75.682561, trip.getLongitude());
285 trip = route.getTrip(2);
286 Assert.assertEquals("Ottawa Rockcliffe", trip.getDest());
287 expectedDate = hourMinFormat.parse("12:20");
288 Assert.assertEquals(expectedDate, trip.getStart());
289 Assert.assertEquals(37, trip.getAdjTime());
290 Assert.assertEquals(-1.0, trip.getAdjAge());
291 Assert.assertEquals(false, trip.isLastTrip());
292 Assert.assertEquals("4E - DEH", trip.getBusType());
293 Assert.assertEquals(0.0, trip.getSpeed());
294 Assert.assertEquals(0.0, trip.getLatitude());
295 Assert.assertEquals(0.0, trip.getLongitude());