Search in sources :

Example 1 with ResponseBean

use of org.onebusaway.api.model.ResponseBean in project onebusaway-application-modules by camsys.

the class VehiclePositionsForAgencyActionTest 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);
        TripBean trip = new TripBean();
        trip.setId("1_t0");
        trip.setRoute(route);
        tripStatus.setActiveTrip(trip);
        vehicle.setLocation(new CoordinatePoint(47.0, -122.0));
    }
    {
        VehicleStatusBean vehicle = new VehicleStatusBean();
        vehicles.add(vehicle);
        vehicle.setLastUpdateTime(5678 * 1000);
        vehicle.setVehicleId("1_v2");
        TripStatusBean tripStatus = new TripStatusBean();
        vehicle.setTripStatus(tripStatus);
        TripBean trip = new TripBean();
        trip.setId("1_t1");
        trip.setRoute(route);
        tripStatus.setActiveTrip(trip);
        vehicle.setLocation(new CoordinatePoint(47.1, -122.1));
    }
    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());
        VehiclePosition vehiclePosition = entity.getVehicle();
        assertEquals("t0", vehiclePosition.getTrip().getTripId());
        assertEquals("r1", vehiclePosition.getTrip().getRouteId());
        assertEquals("v1", vehiclePosition.getVehicle().getId());
        assertEquals(1234, vehiclePosition.getTimestamp());
        assertEquals(47.0, vehiclePosition.getPosition().getLatitude(), 0.01);
        assertEquals(-122.0, vehiclePosition.getPosition().getLongitude(), 0.01);
    }
    {
        FeedEntity entity = feed.getEntity(1);
        assertEquals("2", entity.getId());
        VehiclePosition vehiclePosition = entity.getVehicle();
        assertEquals("t1", vehiclePosition.getTrip().getTripId());
        assertEquals("r1", vehiclePosition.getTrip().getRouteId());
        assertEquals("v2", vehiclePosition.getVehicle().getId());
        assertEquals(5678, vehiclePosition.getTimestamp());
        assertEquals(47.1, vehiclePosition.getPosition().getLatitude(), 0.01);
        assertEquals(-122.1, vehiclePosition.getPosition().getLongitude(), 0.01);
    }
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) ArrayList(java.util.ArrayList) ListBean(org.onebusaway.transit_data.model.ListBean) TripBean(org.onebusaway.transit_data.model.trips.TripBean) Date(java.util.Date) VehicleStatusBean(org.onebusaway.transit_data.model.VehicleStatusBean) RouteBean(org.onebusaway.transit_data.model.RouteBean) FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage) VehiclePosition(com.google.transit.realtime.GtfsRealtime.VehiclePosition) ResponseBean(org.onebusaway.api.model.ResponseBean) TripStatusBean(org.onebusaway.transit_data.model.trips.TripStatusBean) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) Test(org.junit.Test)

Example 2 with ResponseBean

use of org.onebusaway.api.model.ResponseBean in project onebusaway-application-modules by camsys.

the class CurrentTimeActionTest method test.

@Test
public void test() throws ParseException {
    CurrentTimeAction action = new CurrentTimeAction();
    long t = System.currentTimeMillis();
    DefaultHttpHeaders headers = action.index();
    assertEquals(200, headers.getStatus());
    ResponseBean response = action.getModel();
    assertEquals(200, response.getCode());
    assertEquals(2, response.getVersion());
    @SuppressWarnings("unchecked") EntryWithReferencesBean<TimeBean> entry = (EntryWithReferencesBean<TimeBean>) response.getData();
    TimeBean time = entry.getEntry();
    assertNotNull(time);
    long delta = Math.abs(time.getTime() - t);
    assertTrue("check that time delta is within limits: delta=" + delta, delta < 100);
    String readableTime = DateLibrary.getTimeAsIso8601String(new Date(time.getTime()));
    assertEquals(readableTime, time.getReadableTime());
}
Also used : EntryWithReferencesBean(org.onebusaway.api.model.transit.EntryWithReferencesBean) DefaultHttpHeaders(org.apache.struts2.rest.DefaultHttpHeaders) ResponseBean(org.onebusaway.api.model.ResponseBean) TimeBean(org.onebusaway.api.model.TimeBean) Date(java.util.Date) Test(org.junit.Test)

Example 3 with ResponseBean

use of org.onebusaway.api.model.ResponseBean in project onebusaway-application-modules by camsys.

the class CustomCsvHandler method getEntityType.

/**
 **
 *
 ***
 */
private Class<?> getEntityType(Object obj) {
    if (obj instanceof ResponseBean) {
        ResponseBean response = (ResponseBean) obj;
        if (response.getData() == null)
            return response.getClass();
        return getEntityType(response.getData());
    } else if (obj instanceof EntryWithReferencesBean) {
        EntryWithReferencesBean<?> entry = (EntryWithReferencesBean<?>) obj;
        return entry.getEntry().getClass();
    } else if (obj instanceof ListWithReferencesBean) {
        ListWithReferencesBean<?> list = (ListWithReferencesBean<?>) obj;
        List<?> values = list.getList();
        if (values.isEmpty())
            return Object.class;
        return values.get(0).getClass();
    }
    return obj.getClass();
}
Also used : ListWithReferencesBean(org.onebusaway.api.model.transit.ListWithReferencesBean) EntryWithReferencesBean(org.onebusaway.api.model.transit.EntryWithReferencesBean) ResponseBean(org.onebusaway.api.model.ResponseBean)

Example 4 with ResponseBean

use of org.onebusaway.api.model.ResponseBean in project onebusaway-application-modules by camsys.

the class CustomProtocolBufferHandler 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();
        /**
         * Instead of writing to the output Writer, we write directly to the
         * HttpServletResponse output stream. That way, we can avoid any weirdness
         * with encoding the serialized protobuf to a String.
         */
        HttpServletResponse res = ServletActionContext.getResponse();
        message.writeTo(res.getOutputStream());
    } else {
        stream.write(response.getText());
    }
    return null;
}
Also used : Message(com.google.protobuf.Message) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResponseBean(org.onebusaway.api.model.ResponseBean)

Example 5 with ResponseBean

use of org.onebusaway.api.model.ResponseBean in project onebusaway-application-modules by camsys.

the class AlertsForAgencyActionTest method test.

@Test
public void test() {
    long now = System.currentTimeMillis();
    List<ServiceAlertBean> alerts = new ArrayList<ServiceAlertBean>();
    {
        ServiceAlertBean alert = new ServiceAlertBean();
        alerts.add(alert);
        TimeRangeBean range = new TimeRangeBean(1234 * 1000, 5678 * 1000);
        alert.setActiveWindows(Arrays.asList(range));
        SituationAffectsBean affects = new SituationAffectsBean();
        affects.setAgencyId("1");
        affects.setRouteId("1_r0");
        affects.setStopId("1_s0");
        affects.setTripId("1_t0");
        SituationAffectsBean alsoAffects = new SituationAffectsBean();
        alsoAffects.setAgencyId("2");
        alert.setAllAffects(Arrays.asList(affects, alsoAffects));
        alert.setSummaries(Arrays.asList(new NaturalLanguageStringBean("Name", "en"), new NaturalLanguageStringBean("Nombre", "es")));
        alert.setDescriptions(Arrays.asList(new NaturalLanguageStringBean("Description", "en"), new NaturalLanguageStringBean("DescripciĆ³n", "es")));
    }
    {
        ServiceAlertBean alert = new ServiceAlertBean();
        alerts.add(alert);
        TimeRangeBean range = new TimeRangeBean(5678 * 1000, 1234 * 1000);
        alert.setActiveWindows(Arrays.asList(range));
        SituationAffectsBean affects = new SituationAffectsBean();
        affects.setAgencyId("2");
        affects.setRouteId("1_r1");
        affects.setStopId("1_s1");
        affects.setTripId("1_t1");
        alert.setAllAffects(Arrays.asList(affects));
        alert.setSummaries(Arrays.asList(new NaturalLanguageStringBean("Name", "en")));
        alert.setDescriptions(Arrays.asList(new NaturalLanguageStringBean("Description", "en")));
    }
    ListBean<ServiceAlertBean> bean = new ListBean<ServiceAlertBean>();
    bean.setList(alerts);
    Mockito.when(_service.getAllServiceAlertsForAgencyId("1")).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());
        Alert alert = entity.getAlert();
        assertEquals(1, alert.getActivePeriodCount());
        TimeRange range = alert.getActivePeriod(0);
        assertEquals(1234, range.getStart());
        assertEquals(5678, range.getEnd());
        assertEquals(2, alert.getInformedEntityCount());
        {
            EntitySelector affects = alert.getInformedEntity(0);
            assertEquals("1", affects.getAgencyId());
            assertEquals("r0", affects.getRouteId());
            assertEquals("t0", affects.getTrip().getTripId());
            assertEquals("s0", affects.getStopId());
        }
        {
            EntitySelector affects = alert.getInformedEntity(1);
            assertEquals("2", affects.getAgencyId());
        }
        TranslatedString header = alert.getHeaderText();
        assertEquals(2, header.getTranslationCount());
        {
            Translation translation = header.getTranslation(0);
            assertEquals("Name", translation.getText());
            assertEquals("en", translation.getLanguage());
        }
        {
            Translation translation = header.getTranslation(1);
            assertEquals("Nombre", translation.getText());
            assertEquals("es", translation.getLanguage());
        }
        TranslatedString description = alert.getDescriptionText();
        assertEquals(2, description.getTranslationCount());
        {
            Translation translation = description.getTranslation(0);
            assertEquals("Description", translation.getText());
            assertEquals("en", translation.getLanguage());
        }
        {
            Translation translation = description.getTranslation(1);
            assertEquals("DescripciĆ³n", translation.getText());
            assertEquals("es", translation.getLanguage());
        }
    }
    {
        FeedEntity entity = feed.getEntity(1);
        assertEquals("2", entity.getId());
        Alert alert = entity.getAlert();
        assertEquals(1, alert.getActivePeriodCount());
        TimeRange range = alert.getActivePeriod(0);
        assertEquals(5678, range.getStart());
        assertEquals(1234, range.getEnd());
        assertEquals(1, alert.getInformedEntityCount());
        {
            EntitySelector affects = alert.getInformedEntity(0);
            assertEquals("2", affects.getAgencyId());
            assertEquals("r1", affects.getRouteId());
            assertEquals("t1", affects.getTrip().getTripId());
            assertEquals("s1", affects.getStopId());
        }
    }
}
Also used : TranslatedString(com.google.transit.realtime.GtfsRealtime.TranslatedString) EntitySelector(com.google.transit.realtime.GtfsRealtime.EntitySelector) Translation(com.google.transit.realtime.GtfsRealtime.TranslatedString.Translation) ArrayList(java.util.ArrayList) ListBean(org.onebusaway.transit_data.model.ListBean) Date(java.util.Date) TimeRangeBean(org.onebusaway.transit_data.model.service_alerts.TimeRangeBean) TimeRange(com.google.transit.realtime.GtfsRealtime.TimeRange) FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage) SituationAffectsBean(org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean) NaturalLanguageStringBean(org.onebusaway.transit_data.model.service_alerts.NaturalLanguageStringBean) ResponseBean(org.onebusaway.api.model.ResponseBean) Alert(com.google.transit.realtime.GtfsRealtime.Alert) ServiceAlertBean(org.onebusaway.transit_data.model.service_alerts.ServiceAlertBean) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) Test(org.junit.Test)

Aggregations

ResponseBean (org.onebusaway.api.model.ResponseBean)9 Date (java.util.Date)4 Test (org.junit.Test)4 FeedEntity (com.google.transit.realtime.GtfsRealtime.FeedEntity)3 FeedMessage (com.google.transit.realtime.GtfsRealtime.FeedMessage)3 ArrayList (java.util.ArrayList)3 DefaultHttpHeaders (org.apache.struts2.rest.DefaultHttpHeaders)3 ListBean (org.onebusaway.transit_data.model.ListBean)3 Message (com.google.protobuf.Message)2 EntryWithReferencesBean (org.onebusaway.api.model.transit.EntryWithReferencesBean)2 RouteBean (org.onebusaway.transit_data.model.RouteBean)2 VehicleStatusBean (org.onebusaway.transit_data.model.VehicleStatusBean)2 TripBean (org.onebusaway.transit_data.model.trips.TripBean)2 TripStatusBean (org.onebusaway.transit_data.model.trips.TripStatusBean)2 Alert (com.google.transit.realtime.GtfsRealtime.Alert)1 EntitySelector (com.google.transit.realtime.GtfsRealtime.EntitySelector)1 TimeRange (com.google.transit.realtime.GtfsRealtime.TimeRange)1 TranslatedString (com.google.transit.realtime.GtfsRealtime.TranslatedString)1 Translation (com.google.transit.realtime.GtfsRealtime.TranslatedString.Translation)1 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)1