Skip to content

Commit

Permalink
Add tests for cars on trips.
Browse files Browse the repository at this point in the history
  • Loading branch information
VillePihlava committed Sep 3, 2024
1 parent 3efe676 commit a555f3d
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.opentripplanner.apis.gtfs.mapping;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Arrays;
import org.junit.jupiter.api.Test;
import org.opentripplanner.transit.model.network.CarAccess;

class CarsAllowedMapperTest {

@Test
void mapping() {
Arrays
.stream(CarAccess.values())
.filter(ba -> ba != CarAccess.UNKNOWN)
.forEach(d -> {
var mapped = CarsAllowedMapper.map(d);
assertEquals(d.toString(), mapped.toString());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import static org.opentripplanner.street.model.StreetTraversalPermission.PEDESTRIAN;
import static org.opentripplanner.transit.model._data.TransitModelForTest.id;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.opentripplanner.ext.flex.trip.UnscheduledTrip;
import org.opentripplanner.framework.application.OTPFeature;
import org.opentripplanner.framework.geometry.WgsCoordinate;
import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore;
import org.opentripplanner.model.StopTime;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.street.model._data.StreetModelForTest;
import org.opentripplanner.street.model.edge.Edge;
Expand All @@ -23,7 +25,15 @@
import org.opentripplanner.street.model.vertex.TransitStopVertex;
import org.opentripplanner.transit.model._data.TransitModelForTest;
import org.opentripplanner.transit.model.framework.Deduplicator;
import org.opentripplanner.transit.model.network.CarAccess;
import org.opentripplanner.transit.model.network.Route;
import org.opentripplanner.transit.model.network.StopPattern;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.model.timetable.RealTimeTripTimes;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripTimesFactory;
import org.opentripplanner.transit.service.StopModel;
import org.opentripplanner.transit.service.TransitModel;

Expand Down Expand Up @@ -92,6 +102,37 @@ void linkFlexStop() {
});
}

@Test
void linkCarsAllowedStop() {
var model = new TestModel();
var carsAllowedTrip = TransitModelForTest
.of()
.trip("carsAllowedTrip")
.withCarsAllowed(CarAccess.ALLOWED)
.build();
model.withCarsAllowedTrip(carsAllowedTrip, model.stop());

var module = model.streetLinkerModule();

module.buildGraph();

assertTrue(model.stopVertex().isConnectedToGraph());

// Because the stop is used by a carsAllowed trip it needs to be linked to both the walk and car edge
assertEquals(2, model.stopVertex().getOutgoing().size());
var linkToWalk = model.outgoingLinks().getFirst();
SplitterVertex walkSplit = (SplitterVertex) linkToWalk.getToVertex();

assertTrue(walkSplit.isConnectedToWalkingEdge());
assertFalse(walkSplit.isConnectedToDriveableEdge());

var linkToCar = model.outgoingLinks().getLast();
SplitterVertex carSplit = (SplitterVertex) linkToCar.getToVertex();

assertFalse(carSplit.isConnectedToWalkingEdge());
assertTrue(carSplit.isConnectedToDriveableEdge());
}

private static class TestModel {

private final TransitStopVertex stopVertex;
Expand Down Expand Up @@ -155,5 +196,33 @@ public RegularStop stop() {
public void withFlexTrip(UnscheduledTrip flexTrip) {
transitModel.addFlexTrip(flexTrip.getId(), flexTrip);
}

public void withCarsAllowedTrip(Trip trip, StopLocation... stops) {
Route route = TransitModelForTest.route("carsAllowedRoute").build();
var stopTimes = Arrays
.stream(stops)
.map(s -> {
var stopTime = new StopTime();
stopTime.setStop(s);
stopTime.setArrivalTime(30);
stopTime.setDepartureTime(60);
stopTime.setTrip(trip);
return stopTime;
})
.toList();
StopPattern stopPattern = new StopPattern(stopTimes);
TripPattern tripPattern = TransitModelForTest
.tripPattern("carsAllowedTripPattern", route)
.withStopPattern(stopPattern)
.build();
RealTimeTripTimes tripTimes = TripTimesFactory.tripTimes(
trip,
stopTimes,
transitModel.getDeduplicator()
);

tripPattern.add(tripTimes);
transitModel.addTripPattern(tripPattern.getId(), tripPattern);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.opentripplanner.gtfs.mapping;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.onebusaway.gtfs.model.Route;
import org.onebusaway.gtfs.model.Trip;
import org.opentripplanner.transit.model.network.CarAccess;

public class CarAccessMapperTest {

private static final int CARS_ALLOWED = 1;
private static final int CARS_NOT_ALLOWED = 2;

@Test
public void testTripProvidedValues() {
Trip trip = new Trip();
assertEquals(CarAccess.UNKNOWN, CarAccessMapper.mapForTrip(trip));

trip.setCarsAllowed(CARS_ALLOWED);
assertEquals(CarAccess.ALLOWED, CarAccessMapper.mapForTrip(trip));

trip.setCarsAllowed(CARS_NOT_ALLOWED);
assertEquals(CarAccess.NOT_ALLOWED, CarAccessMapper.mapForTrip(trip));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore;
import org.opentripplanner.transit.model.basic.Accessibility;
import org.opentripplanner.transit.model.network.BikeAccess;
import org.opentripplanner.transit.model.network.CarAccess;
import org.opentripplanner.transit.model.timetable.Direction;

public class TripMapperTest {

private static final String FEED_ID = "FEED";
private static final AgencyAndId AGENCY_AND_ID = new AgencyAndId("A", "1");
private static final int BIKES_ALLOWED = 1;
private static final int CARS_ALLOWED = 1;
private static final String BLOCK_ID = "Block Id";
private static final int DIRECTION_ID = 1;
private static final String TRIP_HEADSIGN = "Trip Headsign";
Expand All @@ -47,6 +49,7 @@ private static TripMapper defaultTripMapper() {

TRIP.setId(AGENCY_AND_ID);
TRIP.setBikesAllowed(BIKES_ALLOWED);
TRIP.setCarsAllowed(CARS_ALLOWED);
TRIP.setBlockId(BLOCK_ID);
TRIP.setDirectionId(Integer.toString(DIRECTION_ID));
TRIP.setRoute(data.route);
Expand Down Expand Up @@ -78,6 +81,7 @@ void testMap() throws Exception {
assertEquals(TRIP_SHORT_NAME, result.getShortName());
assertEquals(Accessibility.POSSIBLE, result.getWheelchairBoarding());
assertEquals(BikeAccess.ALLOWED, result.getBikesAllowed());
assertEquals(CarAccess.ALLOWED, result.getCarsAllowed());
}

@Test
Expand All @@ -99,6 +103,7 @@ void testMapWithNulls() throws Exception {
assertEquals(Direction.UNKNOWN, result.getDirection());
assertEquals(Accessibility.NO_INFORMATION, result.getWheelchairBoarding());
assertEquals(BikeAccess.UNKNOWN, result.getBikesAllowed());
assertEquals(CarAccess.UNKNOWN, result.getCarsAllowed());
}

/** Mapping the same object twice, should return the same instance. */
Expand Down
Loading

0 comments on commit a555f3d

Please sign in to comment.