-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #367 from AngeloAvv/features/stops-for-agency
Issue #366: Add an endpoint to retrieve the stops of a particular agency
- Loading branch information
Showing
14 changed files
with
374 additions
and
6 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...y-api-webapp/src/main/java/org/onebusaway/api/actions/api/where/StopsForAgencyAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Copyright (C) 2011 Brian Ferris <[email protected]> | ||
* | ||
* 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)); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...org/onebusaway/transit_data_federation_webapp/controllers/StopsForAgencyIdController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright (C) 2011 Brian Ferris <[email protected]> | ||
* | ||
* 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); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...usaway-transit-data-federation-webapp/src/main/webapp/WEB-INF/jsp/stops-for-agency-id.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<%-- | ||
Copyright (C) 2011 Brian Ferris <[email protected]> | ||
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...test/java/org/onebusaway/transit_data_federation/impl/beans/StopsBeanServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.