From f56ac1a085f9a5b61fd959c9d963eee63fa1de27 Mon Sep 17 00:00:00 2001 From: Angelo Cassano Date: Tue, 27 Aug 2024 16:37:37 +0200 Subject: [PATCH] Issue #366: Added unit tests for StopsBeanServiceImpl --- .../impl/beans/StopBeanServiceImpl.java | 2 +- .../impl/beans/StopsBeanServiceImpl.java | 10 ++ .../impl/beans/StopBeanServiceImplTest.java | 2 +- .../impl/beans/StopsBeanServiceImplTest.java | 152 ++++++++++++++++++ 4 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 onebusaway-transit-data-federation/src/test/java/org/onebusaway/transit_data_federation/impl/beans/StopsBeanServiceImplTest.java diff --git a/onebusaway-transit-data-federation/src/main/java/org/onebusaway/transit_data_federation/impl/beans/StopBeanServiceImpl.java b/onebusaway-transit-data-federation/src/main/java/org/onebusaway/transit_data_federation/impl/beans/StopBeanServiceImpl.java index 32dd24a8a3..803c4ed84e 100644 --- a/onebusaway-transit-data-federation/src/main/java/org/onebusaway/transit_data_federation/impl/beans/StopBeanServiceImpl.java +++ b/onebusaway-transit-data-federation/src/main/java/org/onebusaway/transit_data_federation/impl/beans/StopBeanServiceImpl.java @@ -58,7 +58,7 @@ class StopBeanServiceImpl implements StopBeanService { private ConsolidatedStopsService _consolidatedStopsService; @Autowired - public void setTranstiGraphDao(TransitGraphDao transitGraphDao) { + public void setTransitGraphDao(TransitGraphDao transitGraphDao) { _transitGraphDao = transitGraphDao; } diff --git a/onebusaway-transit-data-federation/src/main/java/org/onebusaway/transit_data_federation/impl/beans/StopsBeanServiceImpl.java b/onebusaway-transit-data-federation/src/main/java/org/onebusaway/transit_data_federation/impl/beans/StopsBeanServiceImpl.java index e1c9a56cf9..43cc44d4ee 100644 --- a/onebusaway-transit-data-federation/src/main/java/org/onebusaway/transit_data_federation/impl/beans/StopsBeanServiceImpl.java +++ b/onebusaway-transit-data-federation/src/main/java/org/onebusaway/transit_data_federation/impl/beans/StopsBeanServiceImpl.java @@ -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(); diff --git a/onebusaway-transit-data-federation/src/test/java/org/onebusaway/transit_data_federation/impl/beans/StopBeanServiceImplTest.java b/onebusaway-transit-data-federation/src/test/java/org/onebusaway/transit_data_federation/impl/beans/StopBeanServiceImplTest.java index 1c03038d0d..288d247470 100644 --- a/onebusaway-transit-data-federation/src/test/java/org/onebusaway/transit_data_federation/impl/beans/StopBeanServiceImplTest.java +++ b/onebusaway-transit-data-federation/src/test/java/org/onebusaway/transit_data_federation/impl/beans/StopBeanServiceImplTest.java @@ -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); diff --git a/onebusaway-transit-data-federation/src/test/java/org/onebusaway/transit_data_federation/impl/beans/StopsBeanServiceImplTest.java b/onebusaway-transit-data-federation/src/test/java/org/onebusaway/transit_data_federation/impl/beans/StopsBeanServiceImplTest.java new file mode 100644 index 0000000000..2d5f0d9e55 --- /dev/null +++ b/onebusaway-transit-data-federation/src/test/java/org/onebusaway/transit_data_federation/impl/beans/StopsBeanServiceImplTest.java @@ -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 stopEntries = new ArrayList(); + List stopIds = new ArrayList(); + + 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 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 stopEntries = new ArrayList(); + List stopBeans = new ArrayList(); + + 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); + } + } +} +