Skip to content

Commit

Permalink
Merge pull request #5299 from leonardehrenfried/clean-up-test-resources
Browse files Browse the repository at this point in the history
Clean up test resource loading
  • Loading branch information
leonardehrenfried authored Oct 3, 2023
2 parents 9363ab3 + 60261b0 commit 3228c57
Show file tree
Hide file tree
Showing 84 changed files with 286 additions and 825 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.opentripplanner.routing.core.FareType;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.standalone.api.OtpServerRequestContext;
import org.opentripplanner.test.support.ResourceLoader;
import org.opentripplanner.transit.model.basic.Money;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.service.TransitModel;
Expand Down Expand Up @@ -107,7 +108,9 @@ public void testPortland() {

@Test
public void testFareComponent() {
TestOtpModel model = ConstantsForTests.buildGtfsGraph(ConstantsForTests.FARE_COMPONENT_GTFS);
TestOtpModel model = ConstantsForTests.buildGtfsGraph(
ResourceLoader.of(this).file("farecomponents.gtfs.zip")
);
Graph graph = model.graph();
TransitModel transitModel = model.transitModel();
String feedId = transitModel.getFeedIds().iterator().next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opentripplanner.graph_builder.module.FakeGraph.getFileForResource;
import static org.opentripplanner.routing.api.request.StreetMode.FLEXIBLE;
import static org.opentripplanner.street.search.TraverseMode.WALK;
import static org.opentripplanner.transit.model.basic.TransitMode.BUS;

import java.io.File;
import java.net.URISyntaxException;
import java.time.Duration;
import java.time.Instant;
import java.time.ZonedDateTime;
Expand All @@ -36,6 +34,7 @@
import org.opentripplanner.routing.api.request.RouteRequest;
import org.opentripplanner.routing.api.request.request.filter.AllowAllTransitFilter;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.test.support.ResourceLoader;
import org.opentripplanner.transit.service.TransitModel;

/**
Expand All @@ -58,16 +57,15 @@ public class FlexIntegrationTest {
@BeforeAll
static void setup() {
OTPFeature.enableFeatures(Map.of(OTPFeature.FlexRouting, true));
var osmPath = getAbsolutePath(FlexTest.COBB_OSM);
var cobblincGtfsPath = getAbsolutePath(FlexTest.COBB_BUS_30_GTFS);
var martaGtfsPath = getAbsolutePath(FlexTest.MARTA_BUS_856_GTFS);
var flexGtfsPath = getAbsolutePath(FlexTest.COBB_FLEX_GTFS);

TestOtpModel model = ConstantsForTests.buildOsmGraph(osmPath);
TestOtpModel model = ConstantsForTests.buildOsmGraph(FlexTest.COBB_OSM);
graph = model.graph();
transitModel = model.transitModel();

addGtfsToGraph(graph, transitModel, List.of(cobblincGtfsPath, martaGtfsPath, flexGtfsPath));
addGtfsToGraph(
graph,
transitModel,
List.of(FlexTest.COBB_BUS_30_GTFS, FlexTest.MARTA_BUS_856_GTFS, FlexTest.COBB_FLEX_GTFS)
);
service = TestServerContext.createServerContext(graph, transitModel).routingService();
}

Expand Down Expand Up @@ -176,21 +174,9 @@ static void teardown() {
OTPFeature.enableFeatures(Map.of(OTPFeature.FlexRouting, false));
}

private static String getAbsolutePath(String cobbOsm) {
try {
return getFileForResource(cobbOsm).getAbsolutePath();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

private static void addGtfsToGraph(
Graph graph,
TransitModel transitModel,
List<String> gtfsFiles
) {
private static void addGtfsToGraph(Graph graph, TransitModel transitModel, List<File> gtfsFiles) {
// GTFS
var gtfsBundles = gtfsFiles.stream().map(f -> new GtfsBundle(new File(f))).toList();
var gtfsBundles = gtfsFiles.stream().map(GtfsBundle::new).toList();
GtfsModule gtfsModule = new GtfsModule(
gtfsBundles,
transitModel,
Expand Down
28 changes: 12 additions & 16 deletions src/ext-test/java/org/opentripplanner/ext/flex/FlexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,34 @@

import gnu.trove.set.hash.TIntHashSet;
import java.io.File;
import java.net.URISyntaxException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import org.opentripplanner.TestOtpModel;
import org.opentripplanner.ext.flex.flexpathcalculator.DirectFlexPathCalculator;
import org.opentripplanner.framework.application.OTPFeature;
import org.opentripplanner.graph_builder.module.FakeGraph;
import org.opentripplanner.gtfs.graphbuilder.GtfsBundle;
import org.opentripplanner.gtfs.graphbuilder.GtfsModule;
import org.opentripplanner.model.calendar.ServiceDateInterval;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.test.support.ResourceLoader;
import org.opentripplanner.transit.model.framework.Deduplicator;
import org.opentripplanner.transit.service.StopModel;
import org.opentripplanner.transit.service.TransitModel;

public abstract class FlexTest {

protected static final String ASPEN_GTFS = "/flex/aspen-flex-on-demand.gtfs.zip";
protected static final String COBB_FLEX_GTFS = "/flex/cobblinc-scheduled-deviated-flex.gtfs.zip";
protected static final String COBB_BUS_30_GTFS = "/flex/cobblinc-bus-30-only.gtfs.zip";
protected static final String MARTA_BUS_856_GTFS = "/flex/marta-bus-856-only.gtfs.zip";
protected static final String LINCOLN_COUNTY_GBFS = "/flex/lincoln-county-flex.gtfs.zip";
protected static final String COBB_OSM = "/flex/cobb-county.filtered.osm.pbf";
private static final ResourceLoader RES = ResourceLoader.of(FlexTest.class);

protected static final File ASPEN_GTFS = RES.file("aspen-flex-on-demand.gtfs.zip");
protected static final File COBB_FLEX_GTFS = RES.file(
"cobblinc-scheduled-deviated-flex.gtfs.zip"
);
protected static final File COBB_BUS_30_GTFS = RES.file("cobblinc-bus-30-only.gtfs.zip");
protected static final File MARTA_BUS_856_GTFS = RES.file("marta-bus-856-only.gtfs.zip");
protected static final File LINCOLN_COUNTY_GBFS = RES.file("lincoln-county-flex.gtfs.zip");
protected static final File COBB_OSM = RES.file("cobb-county.filtered.osm.pbf");

protected static final DirectFlexPathCalculator calculator = new DirectFlexPathCalculator();
protected static final LocalDate serviceDate = LocalDate.of(2021, 4, 11);
Expand All @@ -39,14 +42,7 @@ public abstract class FlexTest {
new TIntHashSet()
);

protected static TestOtpModel buildFlexGraph(String fileName) {
File file = null;
try {
file = FakeGraph.getFileForResource(fileName);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}

protected static TestOtpModel buildFlexGraph(File file) {
var deduplicator = new Deduplicator();
var graph = new Graph(deduplicator);
var transitModel = new TransitModel(new StopModel(), deduplicator);
Expand Down
Empty file.
74 changes: 19 additions & 55 deletions src/test/java/org/opentripplanner/ConstantsForTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,70 +39,41 @@
import org.opentripplanner.standalone.config.OtpConfigLoader;
import org.opentripplanner.street.search.TraverseMode;
import org.opentripplanner.street.search.TraverseModeSet;
import org.opentripplanner.test.support.ResourceLoader;
import org.opentripplanner.transit.model.framework.Deduplicator;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.service.StopModel;
import org.opentripplanner.transit.service.TransitModel;

public class ConstantsForTests {

public static final String CALTRAIN_GTFS = "src/test/resources/gtfs/caltrain_gtfs.zip";
private static final ResourceLoader RES = ResourceLoader.of(ConstantsForTests.class);

public static final String NETEX_MINIMAL = "src/test/resources/netex/netex_minimal.zip";
public static final File CALTRAIN_GTFS = RES.file("/gtfs/caltrain_gtfs.zip");

private static final String PORTLAND_GTFS = "src/test/resources/portland/portland.gtfs.zip";
private static final File PORTLAND_GTFS = RES.file("/portland/portland.gtfs.zip");

public static final String PORTLAND_CENTRAL_OSM =
"src/test/resources/portland/portland-central-filtered.osm.pbf";
private static final File PORTLAND_CENTRAL_OSM = RES.file(
"/portland/portland-central-filtered.osm.pbf"
);

private static final String PORTLAND_BIKE_SHARE_CSV =
"src/test/resources/portland/portland-vehicle-rental.csv";

private static final String PORTLAND_NED = "src/test/resources/portland/portland-ned.tif";

private static final String PORTLAND_NED_WITH_NODATA =
"src/test/resources/portland/portland-ned-nodata.tif";

private static final String OSLO_EAST_OSM = "src/test/resources/oslo-east-filtered.osm.pbf";

public static final String KCM_GTFS = "src/test/resources/gtfs/kcm_gtfs.zip";
private static final File OSLO_EAST_OSM = RES.file("oslo-east-filtered.osm.pbf");

public static final String FAKE_GTFS = "src/test/resources/testagency";
public static final File SIMPLE_GTFS = RES.file("/gtfs/simple/");

public static final String FARE_COMPONENT_GTFS =
"src/test/resources/gtfs/farecomponents.gtfs.zip";

public static final String SHAPE_DIST_GTFS = "src/test/resources/gtfs/shape_dist_traveled/";
public static final File SHAPE_DIST_GTFS = RES.file("/gtfs/shape_dist_traveled/");

private static final String NETEX_NORDIC_DIR = "src/test/resources/netex/nordic";

private static final String NETEX_NORDIC_FILENAME = "netex_minimal.zip";
private static final String NETEX_EPIP_DIR = "src/test/resources/netex/epip/";
private static final String NETEX_EPIP_DATA_DIR = NETEX_EPIP_DIR + "netex_epip_minimal/";
/* Stuttgart area, Germany */
public static final String DEUFRINGEN_OSM =
"src/test/resources/germany/deufringen-minimal.osm.pbf";
public static final String BOEBLINGEN_OSM =
"src/test/resources/germany/boeblingen-minimal.osm.pbf";
public static final String VVS_BUS_764_ONLY =
"src/test/resources/germany/vvs-bus-764-only.gtfs.zip";
public static final String VVS_BUS_751_ONLY =
"src/test/resources/germany/vvs-bus-751-only.gtfs.zip";
public static final String HERRENBERG_HINDENBURG_STR_UNDER_CONSTRUCTION_OSM =
"src/test/resources/germany/herrenberg-hindenburgstr-under-construction.osm.pbf";
public static final String HERRENBERG_BARRIER_GATES_OSM =
"src/test/resources/germany/herrenberg-barrier-gates.osm.pbf";
public static final String HERRENBERG_OSM =
"src/test/resources/germany/herrenberg-minimal.osm.pbf";
public static final String ISLAND_PRUNE_OSM =
"src/test/resources/germany/herrenberg-island-prune-nothru.osm.pbf";
public static final String ADAPTIVE_PRUNE_OSM = "src/test/resources/isoiiluoto.pbf";

/* filenames encoded with cp437 and utf8 */
public static final String UMLAUT_CP437_ZIP = "src/test/resources/umlaut-cp437.zip";
public static final String UMLAUT_TXT = "ümläüt.txt";
public static final String UMLAUT_UTF8_ZIP = "src/test/resources/umlaut-utf8.zip";
public static final String UMLAUT_UTF8_ZIP_NO_EFS = "src/test/resources/umlaut-utf8-no-efs.zip";

private static final CompositeDataSource NETEX_MINIMAL_DATA_SOURCE = new ZipFileDataSource(
new File(NETEX_NORDIC_DIR, NETEX_NORDIC_FILENAME),
Expand Down Expand Up @@ -151,8 +122,7 @@ public static TestOtpModel buildNewPortlandGraph(boolean withElevation) {
var transitModel = new TransitModel(new StopModel(), deduplicator);
// Add street data from OSM
{
File osmFile = new File(PORTLAND_CENTRAL_OSM);
OsmProvider osmProvider = new OsmProvider(osmFile, false);
OsmProvider osmProvider = new OsmProvider(PORTLAND_CENTRAL_OSM, false);
OsmModule osmModule = OsmModule
.of(osmProvider, graph)
.withStaticParkAndRide(true)
Expand Down Expand Up @@ -189,14 +159,13 @@ public static TestOtpModel buildNewPortlandGraph(boolean withElevation) {
}
}

public static TestOtpModel buildOsmGraph(String osmPath) {
public static TestOtpModel buildOsmGraph(File osmFile) {
try {
var deduplicator = new Deduplicator();
var stopModel = new StopModel();
var graph = new Graph(deduplicator);
var transitModel = new TransitModel(stopModel, deduplicator);
// Add street data from OSM
File osmFile = new File(osmPath);
OsmProvider osmProvider = new OsmProvider(osmFile, true);
OsmModule osmModule = OsmModule.of(osmProvider, graph).build();
osmModule.buildGraph();
Expand All @@ -206,7 +175,7 @@ public static TestOtpModel buildOsmGraph(String osmPath) {
}
}

public static TestOtpModel buildOsmAndGtfsGraph(String osmPath, String gtfsPath) {
public static TestOtpModel buildOsmAndGtfsGraph(File osmPath, File gtfsPath) {
var otpModel = buildOsmGraph(osmPath);

addGtfsToGraph(
Expand All @@ -223,19 +192,16 @@ public static TestOtpModel buildOsmAndGtfsGraph(String osmPath, String gtfsPath)
return otpModel;
}

public static TestOtpModel buildGtfsGraph(String gtfsPath) {
public static TestOtpModel buildGtfsGraph(File gtfsPath) {
return buildGtfsGraph(gtfsPath, new DefaultFareServiceFactory());
}

public static TestOtpModel buildGtfsGraph(
String gtfsPath,
FareServiceFactory fareServiceFactory
) {
public static TestOtpModel buildGtfsGraph(File gtfsFile, FareServiceFactory fareServiceFactory) {
var deduplicator = new Deduplicator();
var stopModel = new StopModel();
var graph = new Graph(deduplicator);
var transitModel = new TransitModel(stopModel, deduplicator);
addGtfsToGraph(graph, transitModel, gtfsPath, fareServiceFactory, null);
addGtfsToGraph(graph, transitModel, gtfsFile, fareServiceFactory, null);
return new TestOtpModel(graph, transitModel);
}

Expand All @@ -247,9 +213,7 @@ public static TestOtpModel buildNewMinimalNetexGraph() {
var transitModel = new TransitModel(stopModel, deduplicator);
// Add street data from OSM
{
File osmFile = new File(OSLO_EAST_OSM);

OsmProvider osmProvider = new OsmProvider(osmFile, false);
OsmProvider osmProvider = new OsmProvider(OSLO_EAST_OSM, false);
OsmModule osmModule = OsmModule.of(osmProvider, graph).build();
osmModule.buildGraph();
}
Expand Down Expand Up @@ -298,11 +262,11 @@ public synchronized TestOtpModel getCachedPortlandGraphWithElevation() {
public static void addGtfsToGraph(
Graph graph,
TransitModel transitModel,
String file,
File file,
FareServiceFactory fareServiceFactory,
@Nullable String feedId
) {
var bundle = new GtfsBundle(new File(file));
var bundle = new GtfsBundle(file);
bundle.setFeedId(new GtfsFeedId.Builder().id(feedId).build());

var module = new GtfsModule(
Expand Down
Loading

0 comments on commit 3228c57

Please sign in to comment.