use of org.onebusaway.api.model.ResponseBean in project onebusaway-application-modules by camsys.
the class TripUpdatesForAgencyActionTest method test.
@Test
public void test() {
long now = System.currentTimeMillis();
List<VehicleStatusBean> vehicles = new ArrayList<VehicleStatusBean>();
RouteBean.Builder routeBuilder = RouteBean.builder();
routeBuilder.setId("1_r1");
RouteBean route = routeBuilder.create();
{
VehicleStatusBean vehicle = new VehicleStatusBean();
vehicles.add(vehicle);
vehicle.setLastUpdateTime(1234 * 1000);
vehicle.setVehicleId("1_v1");
TripStatusBean tripStatus = new TripStatusBean();
vehicle.setTripStatus(tripStatus);
tripStatus.setScheduleDeviation(2 * 60);
TripBean trip = new TripBean();
trip.setId("1_t0");
trip.setRoute(route);
tripStatus.setActiveTrip(trip);
StopBean stop = new StopBean();
stop.setId("1_s2");
tripStatus.setNextStop(stop);
tripStatus.setNextStopTimeOffset(5 * 60);
}
{
VehicleStatusBean vehicle = new VehicleStatusBean();
vehicles.add(vehicle);
vehicle.setLastUpdateTime(5678 * 1000);
vehicle.setVehicleId("1_v2");
TripStatusBean tripStatus = new TripStatusBean();
vehicle.setTripStatus(tripStatus);
tripStatus.setScheduleDeviation(3 * 60);
TripBean trip = new TripBean();
trip.setId("1_t1");
trip.setRoute(route);
tripStatus.setActiveTrip(trip);
StopBean stop = new StopBean();
stop.setId("1_s3");
tripStatus.setNextStop(stop);
tripStatus.setNextStopTimeOffset(10 * 60);
}
ListBean<VehicleStatusBean> bean = new ListBean<VehicleStatusBean>();
bean.setList(vehicles);
Mockito.when(_service.getAllVehiclesForAgency("1", now)).thenReturn(bean);
_action.setId("1");
_action.setTime(new Date(now));
_action.show();
ResponseBean model = _action.getModel();
FeedMessage feed = (FeedMessage) model.getData();
assertEquals(now / 1000, feed.getHeader().getTimestamp());
assertEquals(2, feed.getEntityCount());
{
FeedEntity entity = feed.getEntity(0);
assertEquals("1", entity.getId());
TripUpdate tripUpdate = entity.getTripUpdate();
assertEquals("t0", tripUpdate.getTrip().getTripId());
assertEquals("r1", tripUpdate.getTrip().getRouteId());
assertEquals("v1", tripUpdate.getVehicle().getId());
assertEquals(1234, tripUpdate.getTimestamp());
assertEquals(120, tripUpdate.getDelay());
assertEquals(1, tripUpdate.getStopTimeUpdateCount());
StopTimeUpdate stopTimeUpdate = tripUpdate.getStopTimeUpdate(0);
assertEquals("s2", stopTimeUpdate.getStopId());
assertEquals(now / 1000 + 5 * 60, stopTimeUpdate.getDeparture().getTime());
}
{
FeedEntity entity = feed.getEntity(1);
assertEquals("2", entity.getId());
TripUpdate tripUpdate = entity.getTripUpdate();
assertEquals("t1", tripUpdate.getTrip().getTripId());
assertEquals("r1", tripUpdate.getTrip().getRouteId());
assertEquals("v2", tripUpdate.getVehicle().getId());
assertEquals(5678, tripUpdate.getTimestamp());
assertEquals(180, tripUpdate.getDelay());
assertEquals(1, tripUpdate.getStopTimeUpdateCount());
StopTimeUpdate stopTimeUpdate = tripUpdate.getStopTimeUpdate(0);
assertEquals("s3", stopTimeUpdate.getStopId());
assertEquals(now / 1000 + 10 * 60, stopTimeUpdate.getDeparture().getTime());
}
}
use of org.onebusaway.api.model.ResponseBean in project onebusaway-application-modules by camsys.
the class CustomProtocolBufferTextHandler method fromObject.
@Override
public String fromObject(Object obj, String resultCode, Writer stream) throws IOException {
ResponseBean response = (ResponseBean) obj;
if (response.getData() != null && response.getData() instanceof Message) {
Message message = (Message) response.getData();
stream.write(message.toString());
} else {
stream.write(response.getText());
}
return null;
}
use of org.onebusaway.api.model.ResponseBean in project onebusaway-application-modules by camsys.
the class ApiActionSupport method setValidationErrorsResponse.
protected DefaultHttpHeaders setValidationErrorsResponse() {
ValidationErrorBean bean = new ValidationErrorBean(new ArrayList<String>(getActionErrors()), getFieldErrors());
_response = new ResponseBean(getReturnVersion(), ResponseCodes.RESPONSE_INVALID_ARGUMENT, "validation error", bean);
return new DefaultHttpHeaders().withStatus(_response.getCode());
}
use of org.onebusaway.api.model.ResponseBean in project onebusaway-application-modules by camsys.
the class ApiKeyInterceptor method unauthorized.
// package private for unit tests
String unauthorized(ActionInvocation invocation, ApiKeyPermissionService.Status reason) throws IOException {
ActionProxy proxy = invocation.getProxy();
int httpCode = ResponseCodes.RESPONSE_UNAUTHORIZED;
String message = "permission denied";
switch(reason) {
case UNAUTHORIZED:
httpCode = ResponseCodes.RESPONSE_UNAUTHORIZED;
break;
case RATE_EXCEEDED:
httpCode = ResponseCodes.RESPONSE_TOO_MANY_REQUESTS;
message = "rate limit exceeded";
break;
case AUTHORIZED:
// this should never happen!
throw new IllegalStateException("Valid status code " + reason + " in unauthorized response");
default:
httpCode = ResponseCodes.RESPONSE_UNAUTHORIZED;
}
ResponseBean response = new ResponseBean(1, httpCode, message, null);
DefaultHttpHeaders methodResult = new DefaultHttpHeaders().withStatus(response.getCode());
return _handlerSelector.handleResult(proxy.getConfig(), methodResult, response);
}
Aggregations