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

Fix test resource install #196

Merged
merged 1 commit into from
Feb 17, 2024
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
13 changes: 5 additions & 8 deletions .github/workflows/mta_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,18 @@ jobs:
distribution: temurin
java-version: ${{ matrix.java }}

- name: Create Folder
- name: Install resources
shell: bash
run: mkdir -p "src/test/java/resources"
run: ./install.sh

- name: Load Client ID
run: |-
echo ${{ secrets.BUS_TOKEN }} > src/test/java/resources/bus.txt
run: echo ${{ secrets.BUS_TOKEN }} > src/test/java/resources/bus.txt

- name: Load OAuth Token
run: |-
echo ${{ secrets.SUBWAY_TOKEN }} > src/test/java/resources/subway.txt
run: echo ${{ secrets.SUBWAY_TOKEN }} > src/test/java/resources/subway.txt

- name: Load Test Group
run: |-
echo ${{ matrix.group }} > src/test/java/resources/TEST_GROUP
run: echo ${{ matrix.group }} > src/test/java/resources/TEST_GROUP

- name: Test with Maven
continue-on-error: true
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,13 @@ Sample response data available on the [`reference`](https://github.com/KatsuteDe

For local tests you can use Java 8+, however only methods in the Java 8 API may be used. The `src/main/java9` folder should not be marked as a source root.

- You must run `install.sh` to initialize test resources.
- (Bus) Run tests locally by adding a text file named `bus.txt` that contains the bus token in the `src/test/java/resources` directory.
- (Subway) Run tests locally by adding a text file named `subway.txt` that contains the subway in the `src/test/java/resources` directory.

#### Running Tests using GitHub Actions

Devs running remote tests through GitHub Actions may do so by running the `MTA CI` workflow manually in the actions tab of your fork. Note that this requires two secrets, a `BUS_TOKEN` which contains the bus token, and a `SUBWAY_TOKEN` which contains the subway token.
Developers running remote tests through GitHub Actions may do so by running the `MTA CI` workflow manually in the actions tab of your fork. Note that this requires two secrets, a `BUS_TOKEN` which contains the bus token, and a `SUBWAY_TOKEN` which contains the subway token.

<div align="right"><a href="#top"><code>▲</code></a></div>

Expand Down
20 changes: 15 additions & 5 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

version=25.0

if [ -d protobuf ]; then
exit 0
fi

mkdir protobuf

if [ "$(expr substr $(uname -s) 1 10)" == "MINGW64_NT" ]; then
Expand All @@ -32,4 +28,18 @@ cp "onebusaway-gtfs-realtime-api/src/main/proto/com/google/transit/realtime/gtfs
cp "onebusaway-gtfs-realtime-api/src/main/proto/com/google/transit/realtime/gtfs-realtime-OneBusAway.proto" "protobuf/gtfs-realtime-OneBusAway.proto"
cp "onebusaway-gtfs-realtime-api/src/main/proto/com/google/transit/realtime/gtfs-realtime-crowding.proto" "protobuf/gtfs-realtime-crowding.proto"
cp "onebusaway-gtfs-realtime-api/src/main/proto/com/google/transit/realtime/gtfs-realtime-service-status.proto" "protobuf/gtfs-realtime-service-status.proto"
cp "onebusaway-gtfs-realtime-api/src/main/proto/com/google/transit/realtime/gtfs-realtime.proto" "protobuf/gtfs-realtime.proto"
cp "onebusaway-gtfs-realtime-api/src/main/proto/com/google/transit/realtime/gtfs-realtime.proto" "protobuf/gtfs-realtime.proto"

# test resources

mkdir src/test/java/resources

curl -L -k -o "src/test/java/resources/resource_subway.zip" "http://web.mta.info/developers/data/nyct/subway/google_transit.zip"
curl -L -k -o "src/test/java/resources/resource_bus_bronx.zip" "http://web.mta.info/developers/data/nyct/bus/google_transit_bronx.zip"
curl -L -k -o "src/test/java/resources/resource_bus_brooklyn.zip" "http://web.mta.info/developers/data/nyct/bus/google_transit_brooklyn.zip"
curl -L -k -o "src/test/java/resources/resource_bus_manhattan.zip" "http://web.mta.info/developers/data/nyct/bus/google_transit_manhattan.zip"
curl -L -k -o "src/test/java/resources/resource_bus_queens.zip" "http://web.mta.info/developers/data/nyct/bus/google_transit_queens.zip"
curl -L -k -o "src/test/java/resources/resource_bus_statenisland.zip" "http://web.mta.info/developers/data/nyct/bus/google_transit_staten_island.zip"
curl -L -k -o "src/test/java/resources/resource_longislandrailroad.zip" "http://web.mta.info/developers/data/lirr/google_transit.zip"
curl -L -k -o "src/test/java/resources/resource_metronorthrailroad.zip" "http://web.mta.info/developers/data/mnr/google_transit.zip"
curl -L -k -o "src/test/java/resources/resource_bus_company.zip" "http://web.mta.info/developers/data/busco/google_transit.zip"
40 changes: 12 additions & 28 deletions src/test/java/dev/katsute/onemta/TestProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ public static void acquireTestLock(){
private static final boolean hasBus = bus.exists();
private static final boolean hasSubway = subway.exists();

private static final Map<DataResourceType,String> resources = new HashMap<DataResourceType,String>(){{
put(DataResourceType.Subway , "http://web.mta.info/developers/data/nyct/subway/google_transit.zip");
put(DataResourceType.Bus_Bronx , "http://web.mta.info/developers/data/nyct/bus/google_transit_bronx.zip");
put(DataResourceType.Bus_Brooklyn , "http://web.mta.info/developers/data/nyct/bus/google_transit_brooklyn.zip");
put(DataResourceType.Bus_Manhattan , "http://web.mta.info/developers/data/nyct/bus/google_transit_manhattan.zip");
put(DataResourceType.Bus_Queens , "http://web.mta.info/developers/data/nyct/bus/google_transit_queens.zip");
put(DataResourceType.Bus_StatenIsland , "http://web.mta.info/developers/data/nyct/bus/google_transit_staten_island.zip");
put(DataResourceType.LongIslandRailroad , "http://web.mta.info/developers/data/lirr/google_transit.zip");
put(DataResourceType.MetroNorthRailroad , "http://web.mta.info/developers/data/mnr/google_transit.zip");
put(DataResourceType.Bus_Company , "http://web.mta.info/developers/data/busco/google_transit.zip");
}};
private static final List<DataResourceType> resources = Arrays.asList(
DataResourceType.Subway,
DataResourceType.Bus_Bronx,
DataResourceType.Bus_Brooklyn,
DataResourceType.Bus_Manhattan,
DataResourceType.Bus_Queens,
DataResourceType.Bus_StatenIsland,
DataResourceType.LongIslandRailroad,
DataResourceType.MetroNorthRailroad,
DataResourceType.Bus_Company
);

public static MTA mta;

Expand All @@ -101,24 +101,8 @@ public static MTA getOneMTA(){

acquireTestLock();

for(final Map.Entry<DataResourceType,String> entry : resources.entrySet())
try(final BufferedInputStream IN = new BufferedInputStream(new URL(entry.getValue()).openStream())){
final File file = new File(test_resources, "resource_" + entry.getKey().name().toLowerCase() + ".zip");
System.out.println("[↻] Checking for data resource " + file.getName());
if(!file.exists()){
System.out.println("[⚠] " + file.getName() + " not found, downloading from the MTA...");
try(final FileOutputStream OUT = new FileOutputStream(file)){
byte[] buffer = new byte[1024];
int bytesReads;
while((bytesReads = IN.read(buffer, 0, 1024)) != -1)
OUT.write(buffer, 0, bytesReads);
}
}
System.out.println("[✔] Added " + file.getName() + " as " + entry.getKey().name());
}

final List<DataResource> resources = new ArrayList<>();
for(final DataResourceType type : TestProvider.resources.keySet())
for(final DataResourceType type : TestProvider.resources)
resources.add(DataResource.create(type, new File(test_resources, "resource_" + type.name().toLowerCase() + ".zip")));

return mta = MTA.create(strip(readFile(bus)), strip(readFile(subway)), resources.toArray(new DataResource[0]));
Expand Down
Loading