Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #366: Add an endpoint to retrieve the stops of a particular agency #367

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright (C) 2011 Brian Ferris <bdferris@onebusaway.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onebusaway.api.actions.api.where;

import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.onebusaway.api.actions.api.ApiActionSupport;
import org.onebusaway.api.model.transit.BeanFactoryV2;
import org.onebusaway.transit_data.model.ListBean;
import org.onebusaway.transit_data.model.StopsBean;
import org.onebusaway.transit_data.services.TransitDataService;
import org.springframework.beans.factory.annotation.Autowired;

public class StopsForAgencyAction extends ApiActionSupport {

private static final long serialVersionUID = 1L;

private static final int V2 = 2;

@Autowired
private TransitDataService _service;

private String _id;

public StopsForAgencyAction() {
super(V2);
}

@RequiredFieldValidator
public void setId(String id) {
_id = id;
}

public String getId() {
return _id;
}

public DefaultHttpHeaders show() {

if (hasErrors())
return setValidationErrorsResponse();

if( ! isVersion(V2))
return setUnknownVersionResponse();

StopsBean stops = _service.getStopsForAgencyId(_id);
BeanFactoryV2 factory = getBeanFactoryV2();
return setOkResponse(factory.getResponse(stops));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (C) 2011 Brian Ferris <bdferris@onebusaway.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onebusaway.transit_data_federation_webapp.controllers;

import org.onebusaway.transit_data.model.StopsBean;
import org.onebusaway.transit_data_federation.services.beans.StopsBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/stops-for-agency-id.action")
public class StopsForAgencyIdController {

@Autowired
private StopsBeanService _service;

@RequestMapping()
public ModelAndView index(@RequestParam String agencyId) {
StopsBean stops = _service.getStopsForAgencyId(agencyId);
return new ModelAndView("stops-for-agency-id.jsp", "stops", stops);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<%--

Copyright (C) 2011 Brian Ferris <bdferris@onebusaway.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<jsp:directive.page contentType="text/html" />
<head>
<title>Stops For Agency</title>
</head>
<body>
<c:forEach var="stop" items="${stops}">
<value>${stop.name}</value>
</c:forEach>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class StopBeanServiceImpl implements StopBeanService {
private ConsolidatedStopsService _consolidatedStopsService;

@Autowired
public void setTranstiGraphDao(TransitGraphDao transitGraphDao) {
public void setTransitGraphDao(TransitGraphDao transitGraphDao) {
_transitGraphDao = transitGraphDao;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ class StopsBeanServiceImpl implements StopsBeanService {
@Autowired
private TransitGraphDao _transitGraphDao;

@Autowired
public void setTransitGraphDao(TransitGraphDao transitGraphDao) {
_transitGraphDao = transitGraphDao;
}

@Autowired
public void setStopBeanService(StopBeanService stopBeanService) {
_stopBeanService = stopBeanService;
}

@Override
public StopsBean getStops(SearchQueryBean queryBean) throws ServiceException {
String query = queryBean.getQuery();
Expand Down Expand Up @@ -231,6 +241,20 @@ private void sortByDistance(List<StopBean> stopBeans, Map<String, Double> distan
Collections.sort(stopBeans, new DistanceAwayComparator());
}

@Override
public StopsBean getStopsForAgencyId(String agencyId) {
AgencyEntry agency = _transitGraphDao.getAgencyForId(agencyId);
if (agency == null)
throw new NoSuchAgencyServiceException(agencyId);
List<StopBean> stopBeans = new ArrayList<StopBean>();
for (StopEntry stop : agency.getStops()) {
AgencyAndId id = stop.getId();
StopBean stopBean = _stopBeanService.getStopForId(id, null);
stopBeans.add(stopBean);
}
return constructResult(stopBeans, false);
}

@Override
public ListBean<String> getStopsIdsForAgencyId(String agencyId) {
AgencyEntry agency = _transitGraphDao.getAgencyForId(agencyId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ public ListBean<String> getStopIdsForAgencyId(String agencyId) {
return _transitDataService.getStopIdsForAgencyId(agencyId);
}

@Override
public StopsBean getStopsForAgencyId(String agencyId) {
blockUntilBundleIsReady();
return _transitDataService.getStopsForAgencyId(agencyId);
}

@Override
public StopWithArrivalsAndDeparturesBean getStopWithArrivalsAndDepartures(
String stopId, ArrivalsAndDeparturesQueryBean query, AgencyServiceInterval serviceInterval)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ public ListBean<String> getStopIdsForAgencyId(String agencyId) {
return _stopsBeanService.getStopsIdsForAgencyId(agencyId);
}

//@Override
public StopsBean getStopsForAgencyId(String agencyId) {

return _stopsBeanService.getStopsForAgencyId(agencyId);
}

//@Override
public StopWithArrivalsAndDeparturesBean getStopWithArrivalsAndDepartures(
String stopId, ArrivalsAndDeparturesQueryBean query, AgencyServiceInterval serviceInterval)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ public interface StopsBeanService {
public StopsBean getStops(SearchQueryBean query) throws ServiceException;

/**
*
*
* @param agencyId see {@link Agency#getId()}
* @return the list of all stops for the specified agency
*/
public StopsBean getStopsForAgencyId(String agencyId);

/**
*
* @param agencyId see {@link Agency#getId()}
* @return the list of all stop ids for the specified agency
*/
public ListBean<String> getStopsIdsForAgencyId(String agencyId);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setup() {
_service = new StopBeanServiceImpl();

_transitGraphDao = Mockito.mock(TransitGraphDao.class);
_service.setTranstiGraphDao(_transitGraphDao);
_service.setTransitGraphDao(_transitGraphDao);

_narrativeService = Mockito.mock(NarrativeService.class);
_service.setNarrativeService(_narrativeService);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/**
* Copyright (C) 2024 Angelo Cassano
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.onebusaway.transit_data_federation.impl.beans;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.onebusaway.exceptions.NoSuchAgencyServiceException;
import org.onebusaway.gtfs.model.AgencyAndId;
import org.onebusaway.transit_data.model.ListBean;
import org.onebusaway.transit_data.model.StopBean;
import org.onebusaway.transit_data.model.StopsBean;
import org.onebusaway.transit_data_federation.impl.transit_graph.AgencyEntryImpl;
import org.onebusaway.transit_data_federation.impl.transit_graph.StopEntryImpl;
import org.onebusaway.transit_data_federation.services.beans.StopBeanService;
import org.onebusaway.transit_data_federation.services.transit_graph.StopEntry;
import org.onebusaway.transit_data_federation.services.transit_graph.TransitGraphDao;
import org.onebusaway.util.AgencyAndIdLibrary;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.*;

public class StopsBeanServiceImplTest {
private StopsBeanServiceImpl _service;
private StopBeanService _stopBeanService;
private TransitGraphDao _transitGraphDao;

@Before
public void setup() {
_service = new StopsBeanServiceImpl();

_stopBeanService = Mockito.mock(StopBeanService.class);
_service.setStopBeanService(_stopBeanService);

_transitGraphDao = Mockito.mock(TransitGraphDao.class);
_service.setTransitGraphDao(_transitGraphDao);
}

@Test
public void testGetStopsIdsForAgencyId() {
List<StopEntry> stopEntries = new ArrayList<StopEntry>();
List<String> stopIds = new ArrayList<String>();

String agencyId = "1";
AgencyEntryImpl agency = new AgencyEntryImpl();
agency.setStops(stopEntries);

Mockito.when(_transitGraphDao.getAgencyForId(agencyId)).thenReturn(agency);
for (int i = 0; i < 10; i++) {
AgencyAndId id = new AgencyAndId(agencyId, Integer.toString(i));
StopEntryImpl stopEntry = new StopEntryImpl(id, 10 * (i + 1), -20 * (i + 1));
stopEntries.add(stopEntry);
stopIds.add(AgencyAndIdLibrary.convertToString(id));
}

ListBean<String> stopIdsFromService = _service.getStopsIdsForAgencyId(agencyId);

assertNotNull(stopIdsFromService);
assertNotNull(stopIdsFromService.getList());
assertEquals(10, stopIdsFromService.getList().size());

for (int i = 0; i < 10; i++) {
assertEquals(stopIds.get(i), stopIdsFromService.getList().get(i));
}
}

@Test
public void testGetStopsIdsForAgencyIdThrowsNoSuchAgencyServiceException() {
String agencyId = "1";
Mockito.when(_transitGraphDao.getAgencyForId(agencyId)).thenReturn(null);

try {
_service.getStopsIdsForAgencyId(agencyId);
fail("Should throw NoSuchAgencyServiceException");
} catch (Throwable e) {
assertTrue(e instanceof NoSuchAgencyServiceException);
}
}

@Test
public void testGetStopsForAgencyId() {
List<StopEntry> stopEntries = new ArrayList<StopEntry>();
List<StopBean> stopBeans = new ArrayList<StopBean>();

String agencyId = "1";
AgencyEntryImpl agency = new AgencyEntryImpl();
agency.setStops(stopEntries);

Mockito.when(_transitGraphDao.getAgencyForId(agencyId)).thenReturn(agency);
for (int i = 0; i < 10; i++) {
AgencyAndId id = new AgencyAndId(agencyId, Integer.toString(i));
StopEntryImpl stopEntry = new StopEntryImpl(id, 10 * (i + 1), -20 * (i + 1));
StopBean stopBean = new StopBean();

stopBean.setId(id.getId());
stopBean.setLat(stopEntry.getStopLat());
stopBean.setLon(stopEntry.getStopLon());

stopEntries.add(stopEntry);
stopBeans.add(stopBean);

Mockito.when(_transitGraphDao.getStopEntryForId(id)).thenReturn(stopEntry);
Mockito.when(_stopBeanService.getStopForId(id, null)).thenReturn(stopBean);
}

StopsBean stopsBean = _service.getStopsForAgencyId(agencyId);

assertNotNull(stopsBean);
assertNotNull(stopsBean.getStops());
assertEquals(10, stopsBean.getStops().size());

for (int i = 0; i < 10; i++) {
StopBean stopBean = stopBeans.get(i);
StopBean stopBeanFromService = stopsBean.getStops().get(i);

assertNotNull(stopBeanFromService);
assertEquals(stopBean.getId(), stopBeanFromService.getId());
assertEquals(stopBean.getLat(), stopBeanFromService.getLat(), 0.0);
assertEquals(stopBean.getLon(), stopBeanFromService.getLon(), 0.0);
}
}

@Test
public void testGetStopsForAgencyIdThrowsNoSuchAgencyServiceException() {
String agencyId = "1";
Mockito.when(_transitGraphDao.getAgencyForId(agencyId)).thenReturn(null);

try {
_service.getStopsForAgencyId(agencyId);
fail("Should throw NoSuchAgencyServiceException");
} catch (Throwable e) {
assertTrue(e instanceof NoSuchAgencyServiceException);
}
}
}

Loading
Loading