Skip to content

Commit

Permalink
Add isDefatulFareCategory to RiderCategories
Browse files Browse the repository at this point in the history
Enum that specifies if a rider category should be treated as the default (e.g. full adult fare) vs a category with special eligibility.
  • Loading branch information
sberkley committed Jan 29, 2025
1 parent 5ee59d9 commit 20e1d46
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import org.onebusaway.csv_entities.schema.annotations.CsvField;
import org.onebusaway.csv_entities.schema.annotations.CsvFields;
import org.onebusaway.gtfs.serialization.mappings.AgencyIdFieldMappingFactory;
import org.onebusaway.gtfs.serialization.mappings.DefaultAgencyIdFieldMappingFactory;

/**
Expand All @@ -30,8 +29,14 @@ public final class RiderCategory extends IdentityBean<AgencyAndId> {

@CsvField(name = "rider_category_id", mapping = DefaultAgencyIdFieldMappingFactory.class)
private AgencyAndId id;
@CsvField(name = "rider_category_name", optional = true)
@CsvField(name = "rider_category_name", optional = false)
private String name;

/**
* 0 = not default category, 1 = default category
*/
@CsvField(optional = true, defaultValue = "0")
private int isDefaultFareCategory = 0;
@CsvField(optional = true)
private int minAge = MISSING_VALUE;
@CsvField(optional = true)
Expand All @@ -47,6 +52,14 @@ public void setName(String name) {
this.name = name;
}

public int getIsDefaultFareCategory() {
return isDefaultFareCategory;
}

public void setIsDefaultFareCategory(int isDefaultFareCategory) {
this.isDefaultFareCategory = isDefaultFareCategory;
}

public int getMinAge() {
return minAge;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* Copyright (C) 2025 Sound Transit
*
* 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.gtfs.model;

import java.io.File;
import java.io.IOException;
import java.util.*;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.onebusaway.gtfs.serialization.GtfsWriter;
import org.onebusaway.gtfs.serialization.GtfsWriterTest;
import org.onebusaway.gtfs.services.MockGtfs;
import org.onebusaway.gtfs.services.GtfsRelationalDao;

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

public class RiderCategoryTest {

private MockGtfs _gtfs;

private File _tmpDirectory;

@BeforeEach
public void before() throws IOException {
_gtfs = MockGtfs.create();

//make temp directory for gtfs writing output
_tmpDirectory = File.createTempFile("RiderCategoryTest-", "-tmp");
if (_tmpDirectory.exists())
GtfsWriterTest.deleteFileRecursively(_tmpDirectory);
_tmpDirectory.mkdirs();
}

@Test
public void testBasicNetworks() throws IOException {
_gtfs.putMinimal();
_gtfs.putDefaultTrips();
_gtfs.putDefaultStops();
_gtfs.putLines("rider_categories.txt",
"rider_category_id,rider_category_name,is_default_fare_category,eligibility_url",
"cat1,Adult,1,https://www.example.com/adult-fares",
"cat2,Reduced,0,https://www.example.com/reduced-fares",
"cat3,Youth,0,https://www.example.com/youth-fares"
);

GtfsRelationalDao dao = _gtfs.read();
assertEquals(3, dao.getAllRiderCategories().size());

GtfsWriter writer = new GtfsWriter();
writer.setOutputLocation(_tmpDirectory);
writer.run(dao);

Scanner scan = new Scanner(new File(_tmpDirectory + "/rider_categories.txt"));
Set<String> expectedRiderCategoryNames = new HashSet<String>();
Set<String> foundRiderCategoryNames = new HashSet<String>();
expectedRiderCategoryNames.add("Adult");
expectedRiderCategoryNames.add("Reduced");
expectedRiderCategoryNames.add("Youth");
boolean onHeader = true;
while(scan.hasNext()){
String line = scan.nextLine();
if (onHeader) {
onHeader = false;
} else {
String[] lineParts = line.split(",");

if (lineParts.length > 1) {
foundRiderCategoryNames.add(lineParts[1]);
}
}
}
scan.close();

assertEquals(expectedRiderCategoryNames, foundRiderCategoryNames, "Did not find rider category names in output");
}

@Test
public void testPutMinimal() throws IOException {
_gtfs.putMinimal();
// Just make sure it parses without throwing an error.
_gtfs.read();
}

@AfterEach
public void teardown() {
deleteFileRecursively(_tmpDirectory);
}

public static void deleteFileRecursively(File file) {

if (!file.exists())
return;

if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File child : files)
deleteFileRecursively(child);
}
}

file.delete();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ public void mdotMetroFaresV2() throws CsvEntityIOException, IOException {
assertEquals("charmcard_senior", medium.getId().getId());
assertEquals("Senior CharmCard", medium.getName());

List<RiderCategory> riderCats = new ArrayList<>(dao.getAllRiderCategories());
assertEquals(5, riderCats.size());

RiderCategory riderCat = riderCats.stream().sorted(Comparator.comparing(RiderCategory::getId)).filter(c -> c.getId().getId().equals("reg")).findAny().get();
assertEquals("reg", riderCat.getId().getId());
assertEquals("Regular", riderCat.getName());
assertEquals(1, riderCat.getIsDefaultFareCategory());
assertEquals(RiderCategory.MISSING_VALUE, riderCat.getMaxAge());
assertEquals(RiderCategory.MISSING_VALUE, riderCat.getMinAge());
assertEquals("https://www.mta.maryland.gov/regular-fares", riderCat.getEligibilityUrl());

RiderCategory riderCat2 = riderCats.stream().sorted(Comparator.comparing(RiderCategory::getId)).filter(c -> c.getId().getId().equals("sen")).findAny().get();
assertEquals("sen", riderCat2.getId().getId());
assertEquals("Senior", riderCat2.getName());
assertEquals(0, riderCat2.getIsDefaultFareCategory());
assertEquals(RiderCategory.MISSING_VALUE, riderCat2.getMaxAge());
assertEquals(65, riderCat2.getMinAge());
assertEquals("https://www.mta.maryland.gov/senior-reduced-fare-program", riderCat2.getEligibilityUrl());


List<StopAreaElement> stopAreaElements = new ArrayList<>(dao.getAllStopAreaElements());
assertEquals(0, stopAreaElements.size());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
rider_category_id,rider_category_name,min_age,max_age,eligibility_url
sen,Senior,65,,https://www.mta.maryland.gov/senior-reduced-fare-program
dis,Disability,,,https://www.mta.maryland.gov/disabled-reduced-fare-program
stu,Student,,,https://www.mta.maryland.gov/student-fares
mob,Mobility,,,https://www.mta.maryland.gov/mobility
rider_category_id,rider_category_name,is_default_fare_category,min_age,max_age,eligibility_url
reg,Regular,1,,,https://www.mta.maryland.gov/regular-fares
sen,Senior,0,65,,https://www.mta.maryland.gov/senior-reduced-fare-program
dis,Disability,0,,,https://www.mta.maryland.gov/disabled-reduced-fare-program
stu,Student,0,,,https://www.mta.maryland.gov/student-fares
mob,Mobility,0,,,https://www.mta.maryland.gov/mobility

0 comments on commit 20e1d46

Please sign in to comment.