Skip to content

Commit

Permalink
various fixes and test adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Katsute committed Jan 26, 2022
1 parent e70b679 commit 957476b
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 45 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/mta_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ on: [workflow_dispatch]

permissions: read-all

concurrency: mta-test
concurrency:
group: mta-test
cancel-in-progress: true

jobs:
mta_ci:
Expand Down Expand Up @@ -40,4 +42,4 @@ jobs:
echo ${{ matrix.group }} > src/test/java/resources/TEST_GROUP
- name: ✔ Test with Maven
run: mvn test -fae --no-transfer-progress
run: mvn test -fae --no-transfer-progress
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,39 +54,39 @@ OneMTA requires static data from the MTA for most route and stop information. La
Only include static data for the API endpoints you are using. For the bus API you must include all boroughs plus bus company.

```java
DataResource staticBusBx = DataResource.create(
DataResource staticBusBx = DataResource.create(
DataResourceType.Bus_Bronx,
new File("bronx_google_transit.zip")
);
DataResource staticBusBk = DataResource.create(
DataResource staticBusBk = DataResource.create(
DataResourceType.Bus_Brooklyn,
new File("brooklyn_google_transit.zip")
);
DataResource staticBusMt = DataResource.create(
DataResource staticBusMt = DataResource.create(
DataResourceType.Bus_Manhattan,
new File("manhattan_google_transit.zip")
);
DataResource staticBusQn = DataResource.create(
DataResource staticBusQn = DataResource.create(
DataResourceType.Bus_Queens,
new File("queens_google_transit.zip")
);
DataResource staticBusSI = DataResource.create(
DataResource staticBusSI = DataResource.create(
DataResourceType.Bus_StatenIsland,
new File("staten_island_google_transit.zip")
);
DataResource staticBusBC = DataResource.create(
DataResource staticBusBC = DataResource.create(
DataResourceType.Bus_Company,
new File("bus_company_google_transit.zip")
);
DataResource staticSubway = DataResource.create(
DataResourceType.Subway,
new File("subway_google_transit.zip")
);
DataResource staticLIRR = DataResource.create(
DataResource staticLIRR = DataResource.create(
DataResourceType.LongIslandRailroad,
new File("lirr_google_transit.zip")
);
DataResource staticMNR = DataResource.create(
DataResource staticMNR = DataResource.create(
DataResourceType.MetroNorthRailroad,
new File("mnr_google_transit.zip")
);
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/dev/katsute/onemta/MTAImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ public final Subway.Stop getSubwayStop(final int stop_id, final SubwayDirection
public final Subway.Stop getSubwayStop(final String stop_id, final SubwayDirection direction){
return MTASchema_Subway.asStop(
this,
MTASchema_Subway.direction
.matcher(Objects.requireNonNull(stop_id, "Stop ID must not be null"))
.replaceAll("") +
MTASchema_Subway.stripDirection(Objects.requireNonNull(stop_id, "Stop ID must not be null")) +
(Objects.requireNonNull(direction, "Direction must not be null") == SubwayDirection.NORTH ? 'N' : 'S')
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/katsute/onemta/MTASchema_Bus.java
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ public final Stop getStop(){

@Override
public final Trip getTrip(){
if(optionalStop == null)
if(trip != null)
return trip;
else{
final Vehicle bus = mta.getBus(vehicleID);
Expand Down
33 changes: 23 additions & 10 deletions src/main/java/dev/katsute/onemta/MTASchema_Subway.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,20 @@
@SuppressWarnings("SpellCheckingInspection")
abstract class MTASchema_Subway extends MTASchema {

static final Pattern direction = Pattern.compile("N|S$");
private static final Pattern direction = Pattern.compile("N|S$", Pattern.CASE_INSENSITIVE);

static String stripDirection(final String stop){
return direction.matcher(stop).replaceAll("");
}

private static SubwayDirection getDirection(final String stop){
final String stopOnly = stripDirection(stop);
return stop.length() == stopOnly.length() || (!stop.toUpperCase().endsWith("N") && !stop.toUpperCase().endsWith("S"))
? null
: stop.toUpperCase().endsWith("N")
? SubwayDirection.NORTH
: SubwayDirection.SOUTH;
}

static Route asRoute(final MTA mta, final String route_id){
// find row
Expand Down Expand Up @@ -185,12 +198,7 @@ static Stop asStop(final MTA mta, final String stop_id){
private final Double stopLat = Double.valueOf(row.get(csv.getHeaderIndex("stop_lat")));
private final Double stopLon = Double.valueOf(row.get(csv.getHeaderIndex("stop_lon")));

private final SubwayDirection stopDirection =
stopID.endsWith("N") || stopID.endsWith("S")
? stopID.endsWith("N")
? SubwayDirection.NORTH
: SubwayDirection.SOUTH
: null;
private final SubwayDirection stopDirection = MTASchema_Subway.getDirection(stopID);

// static data

Expand Down Expand Up @@ -283,9 +291,14 @@ public final Subway.Alert[] getAlerts(){
final GTFSRealtimeProto.FeedMessage feed = cast(mta).service.alerts.getSubway(cast(mta).subwayToken);
final int len = feed.getEntityCount();
for(int i = 0; i < len; i++){
final Subway.Alert alert = MTASchema_Subway.asTransitAlert(mta, feed.getEntity(i));
if(Arrays.asList(alert.getStopIDs()).contains(stop))
alerts.add(alert);
final Subway.Alert alert = MTASchema_Subway.asTransitAlert(mta, feed.getEntity(i));
for(final String id : alert.getStopIDs())
if(id.equalsIgnoreCase(stop) || // if stop ID matches exactly
// if stop direction is unknown, include alerts for any direction
(stopDirection == null && stripDirection(id).equalsIgnoreCase(stop)) ||
// if alert direction if unknown, include stops for any direction
(MTASchema_Subway.getDirection(id) == null && stripDirection(stop).equalsIgnoreCase(id)))
alerts.add(alert);
}
this.alerts = alerts;
}
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/dev/katsute/onemta/CompilerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,39 @@ private void setup(){

// setup

DataResource staticBusBx = DataResource.create(
DataResource staticBusBx = DataResource.create(
DataResourceType.Bus_Bronx,
new File("bronx_google_transit.zip")
);
DataResource staticBusBk = DataResource.create(
DataResource staticBusBk = DataResource.create(
DataResourceType.Bus_Brooklyn,
new File("brooklyn_google_transit.zip")
);
DataResource staticBusMt = DataResource.create(
DataResource staticBusMt = DataResource.create(
DataResourceType.Bus_Manhattan,
new File("manhattan_google_transit.zip")
);
DataResource staticBusQn = DataResource.create(
DataResource staticBusQn = DataResource.create(
DataResourceType.Bus_Queens,
new File("queens_google_transit.zip")
);
DataResource staticBusSI = DataResource.create(
DataResource staticBusSI = DataResource.create(
DataResourceType.Bus_StatenIsland,
new File("staten_island_google_transit.zip")
);
DataResource staticBusBC = DataResource.create(
DataResource staticBusBC = DataResource.create(
DataResourceType.Bus_Company,
new File("bus_company_google_transit.zip")
);
DataResource staticSubway = DataResource.create(
DataResourceType.Subway,
new File("subway_google_transit.zip")
);
DataResource staticLIRR = DataResource.create(
DataResource staticLIRR = DataResource.create(
DataResourceType.LongIslandRailroad,
new File("lirr_google_transit.zip")
);
DataResource staticMNR = DataResource.create(
DataResource staticMNR = DataResource.create(
DataResourceType.MetroNorthRailroad,
new File("mnr_google_transit.zip")
);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/dev/katsute/onemta/bus/TestBusRoute.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ final void testVehicles(){
}
}
final boolean finalPasses = tested;
annotateTest(() -> assertTrue(finalPasses, "Failed to pass expected arrival tests, there probably wasn't enough vehicles to conclude test (tested " + route.getVehicles().length + " vehicles)"));
annotateTest(() -> assertTrue(finalPasses, "Failed to pass progress tests, there probably wasn't enough vehicles to conclude test (tested " + route.getVehicles().length + " vehicles)"));
}

for(final Vehicle vehicle : route.getVehicles())
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/dev/katsute/onemta/bus/TestBusStop.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ final class VehicleTests {
@Test
final void testVehicles(){
{ // not all bus vehicles have this for some reason
boolean passes = false;
boolean tested = false;
for(final Vehicle vehicle : stop.getVehicles()){
if(vehicle.getExpectedArrivalTime() != null &&
vehicle.getExpectedArrivalTimeEpochMillis() != null &&
vehicle.getExpectedDepartureTime() != null &&
vehicle.getExpectedDepartureTimeEpochMillis() != null
){
passes = true;
tested = true;
break;
}
}

final boolean finalPasses = passes;
final boolean finalPasses = tested;
annotateTest(() -> assertTrue(finalPasses, "Failed to pass expected arrival tests, there probably wasn't enough vehicles to conclude test (tested " + stop.getVehicles().length + " vehicles)"));
}

Expand All @@ -63,7 +63,7 @@ final void testVehicles(){
}
}
final boolean finalPasses = tested;
annotateTest(() -> assertTrue(finalPasses, "Failed to pass expected arrival tests, there probably wasn't enough vehicles to conclude test (tested " + stop.getVehicles().length + " vehicles)"));
annotateTest(() -> assertTrue(finalPasses, "Failed to pass progress tests, there probably wasn't enough vehicles to conclude test (tested " + stop.getVehicles().length + " vehicles)"));
}

for(final Vehicle vehicle : stop.getVehicles())
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/dev/katsute/onemta/subway/TestSubwayStop.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import dev.katsute.onemta.types.*;
import org.junit.jupiter.api.*;

import java.util.regex.Pattern;

import static dev.katsute.jcore.Workflow.*;
import static dev.katsute.onemta.subway.Subway.*;
import static org.junit.jupiter.api.Assertions.*;
Expand Down
27 changes: 19 additions & 8 deletions src/test/java/dev/katsute/onemta/types/AlertValidation.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.katsute.onemta.subway.Subway;

import java.util.Arrays;
import java.util.regex.Pattern;

import static dev.katsute.jcore.Workflow.*;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -36,17 +37,27 @@ public static void testAlert(final TransitAlert<?,?,?,?> alert){
testAlertPeriod(period);
}

private static final Pattern direction = Pattern.compile("N|S$", Pattern.CASE_INSENSITIVE);

private static String stripDirection(final String stop){
return direction.matcher(stop).replaceAll("");
}


public static void testAlertReference(final Alerts<?> reference, final TransitAlert<?,?,?,?> alert){
final boolean stop = reference instanceof TransitStop<?,?,?>;
final Object id = stop ? ((TransitStop<?,?,?>) reference).getStopID() : ((TransitRoute<?,?,?>) reference).getRouteID();
annotateTest(() -> assertTrue(
stop
? reference instanceof Subway.Stop
// remove directional N/S from stop ID
? Arrays.asList(alert.getStopIDs()).contains(id) || Arrays.asList(alert.getStopIDs()).contains(id.toString().substring(0, id.toString().length() - 1))
: Arrays.asList(alert.getStopIDs()).contains(id)
: Arrays.asList(alert.getRouteIDs()).contains(id))
);

if(!stop)
annotateTest(() -> assertTrue(Arrays.asList(alert.getRouteIDs()).contains(id), "Failed to find route " + id + " in " + Arrays.toString(alert.getRouteIDs())));
else if(!(reference instanceof Subway.Stop))
annotateTest(() -> assertTrue(Arrays.asList(alert.getStopIDs()).contains(id), "Failed to find stop " + id + " in " + Arrays.toString(alert.getStopIDs())));
else{
for(final Object stopID : alert.getStopIDs())
if(stripDirection(stopID.toString()).equalsIgnoreCase(stripDirection(id.toString())))
return;
annotateTest(() -> fail("Failed to find stop " + id + " in " + Arrays.toString(alert.getStopIDs())));
}
}

//
Expand Down

0 comments on commit 957476b

Please sign in to comment.