|
| 1 | +package org.opentripplanner.ext.fares; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import java.time.Duration; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.Set; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.opentripplanner.ext.fares.model.FareAttribute; |
| 11 | +import org.opentripplanner.ext.fares.model.FareRuleSet; |
| 12 | +import org.opentripplanner.transit.model.basic.Money; |
| 13 | +import org.opentripplanner.transit.model.framework.FeedScopedId; |
| 14 | + |
| 15 | +class FareRuleSetTest { |
| 16 | + |
| 17 | + private FareRuleSet fareRuleSet; |
| 18 | + static final Money TWO_FIFTY = Money.usDollars(2.50f); |
| 19 | + |
| 20 | + @BeforeEach |
| 21 | + void setUp() { |
| 22 | + FeedScopedId id = new FeedScopedId("feed", "fare1"); |
| 23 | + FareAttribute fareAttribute = FareAttribute |
| 24 | + .of(id) |
| 25 | + .setPrice(TWO_FIFTY) |
| 26 | + .setPaymentMethod(1) |
| 27 | + .setTransfers(1) |
| 28 | + .setTransferDuration(7200) |
| 29 | + .build(); |
| 30 | + fareRuleSet = new FareRuleSet(fareAttribute); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void testHasNoRules() { |
| 35 | + assertFalse(fareRuleSet.hasRules()); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void testAddOriginDestination() { |
| 40 | + fareRuleSet.addOriginDestination("A", "B"); |
| 41 | + assertTrue(fareRuleSet.hasRules()); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void testAddRouteOriginDestination() { |
| 46 | + fareRuleSet.addRouteOriginDestination("Route1", "A", "B"); |
| 47 | + assertTrue(fareRuleSet.hasRules()); |
| 48 | + assertEquals(1, fareRuleSet.getRouteOriginDestinations().size()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testAddContains() { |
| 53 | + fareRuleSet.addContains("Zone1"); |
| 54 | + assertTrue(fareRuleSet.hasRules()); |
| 55 | + assertEquals(1, fareRuleSet.getContains().size()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testAddRoute() { |
| 60 | + FeedScopedId routeId = new FeedScopedId("feed", "route1"); |
| 61 | + fareRuleSet.addRoute(routeId); |
| 62 | + assertTrue(fareRuleSet.hasRules()); |
| 63 | + assertEquals(1, fareRuleSet.getRoutes().size()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void testMatchesWithNoRules() { |
| 68 | + var routes = Set.of(new FeedScopedId("feed", "route1")); |
| 69 | + var trips = Set.of(new FeedScopedId("feed", "trip1")); |
| 70 | + var zones = Set.of("zone1"); |
| 71 | + assertTrue( |
| 72 | + fareRuleSet.matches( |
| 73 | + "A", |
| 74 | + "B", |
| 75 | + new HashSet<>(), |
| 76 | + new HashSet<>(), |
| 77 | + new HashSet<>(), |
| 78 | + 0, |
| 79 | + Duration.ZERO, |
| 80 | + Duration.ZERO |
| 81 | + ) |
| 82 | + ); |
| 83 | + assertTrue( |
| 84 | + fareRuleSet.matches( |
| 85 | + "A", |
| 86 | + "B", |
| 87 | + zones, |
| 88 | + routes, |
| 89 | + trips, |
| 90 | + 0, |
| 91 | + Duration.ofMinutes(100), |
| 92 | + Duration.ofMinutes(100) |
| 93 | + ) |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testMatchesWithOriginDestination() { |
| 99 | + fareRuleSet.addOriginDestination("A", "B"); |
| 100 | + assertTrue( |
| 101 | + fareRuleSet.matches( |
| 102 | + "A", |
| 103 | + "B", |
| 104 | + new HashSet<>(), |
| 105 | + new HashSet<>(), |
| 106 | + new HashSet<>(), |
| 107 | + 0, |
| 108 | + Duration.ZERO, |
| 109 | + Duration.ZERO |
| 110 | + ) |
| 111 | + ); |
| 112 | + assertFalse( |
| 113 | + fareRuleSet.matches( |
| 114 | + "B", |
| 115 | + "C", |
| 116 | + new HashSet<>(), |
| 117 | + new HashSet<>(), |
| 118 | + new HashSet<>(), |
| 119 | + 0, |
| 120 | + Duration.ZERO, |
| 121 | + Duration.ZERO |
| 122 | + ) |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void testMatchesWithContains() { |
| 128 | + Set<String> zones = new HashSet<>(); |
| 129 | + zones.add("Zone1"); |
| 130 | + zones.add("Zone2"); |
| 131 | + fareRuleSet.addContains("Zone1"); |
| 132 | + fareRuleSet.addContains("Zone2"); |
| 133 | + assertTrue( |
| 134 | + fareRuleSet.matches( |
| 135 | + "A", |
| 136 | + "B", |
| 137 | + zones, |
| 138 | + new HashSet<>(), |
| 139 | + new HashSet<>(), |
| 140 | + 0, |
| 141 | + Duration.ZERO, |
| 142 | + Duration.ZERO |
| 143 | + ) |
| 144 | + ); |
| 145 | + assertFalse( |
| 146 | + fareRuleSet.matches( |
| 147 | + "A", |
| 148 | + "B", |
| 149 | + new HashSet<>(), |
| 150 | + new HashSet<>(), |
| 151 | + new HashSet<>(), |
| 152 | + 0, |
| 153 | + Duration.ZERO, |
| 154 | + Duration.ZERO |
| 155 | + ) |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void testMatchesWithRoutes() { |
| 161 | + Set<FeedScopedId> routes = new HashSet<>(); |
| 162 | + FeedScopedId routeId = new FeedScopedId("feed", "route1"); |
| 163 | + FeedScopedId otherRouteId = new FeedScopedId("feed", "route2"); |
| 164 | + routes.add(routeId); |
| 165 | + fareRuleSet.addRoute(routeId); |
| 166 | + assertTrue( |
| 167 | + fareRuleSet.matches( |
| 168 | + "A", |
| 169 | + "B", |
| 170 | + new HashSet<>(), |
| 171 | + routes, |
| 172 | + new HashSet<>(), |
| 173 | + 0, |
| 174 | + Duration.ZERO, |
| 175 | + Duration.ZERO |
| 176 | + ) |
| 177 | + ); |
| 178 | + assertFalse( |
| 179 | + fareRuleSet.matches( |
| 180 | + "A", |
| 181 | + "B", |
| 182 | + new HashSet<>(), |
| 183 | + Set.of(otherRouteId), |
| 184 | + new HashSet<>(), |
| 185 | + 0, |
| 186 | + Duration.ZERO, |
| 187 | + Duration.ZERO |
| 188 | + ) |
| 189 | + ); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + void testMatchesWithTransfers() { |
| 194 | + assertTrue( |
| 195 | + fareRuleSet.matches( |
| 196 | + "A", |
| 197 | + "B", |
| 198 | + new HashSet<>(), |
| 199 | + new HashSet<>(), |
| 200 | + new HashSet<>(), |
| 201 | + 1, |
| 202 | + Duration.ZERO, |
| 203 | + Duration.ZERO |
| 204 | + ) |
| 205 | + ); |
| 206 | + assertFalse( |
| 207 | + fareRuleSet.matches( |
| 208 | + "A", |
| 209 | + "B", |
| 210 | + new HashSet<>(), |
| 211 | + new HashSet<>(), |
| 212 | + new HashSet<>(), |
| 213 | + 2, |
| 214 | + Duration.ZERO, |
| 215 | + Duration.ZERO |
| 216 | + ) |
| 217 | + ); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + void testMatchesWithTransferDuration() { |
| 222 | + assertTrue( |
| 223 | + fareRuleSet.matches( |
| 224 | + "A", |
| 225 | + "B", |
| 226 | + new HashSet<>(), |
| 227 | + new HashSet<>(), |
| 228 | + new HashSet<>(), |
| 229 | + 0, |
| 230 | + Duration.ofSeconds(7000), |
| 231 | + Duration.ZERO |
| 232 | + ) |
| 233 | + ); |
| 234 | + assertFalse( |
| 235 | + fareRuleSet.matches( |
| 236 | + "A", |
| 237 | + "B", |
| 238 | + new HashSet<>(), |
| 239 | + new HashSet<>(), |
| 240 | + new HashSet<>(), |
| 241 | + 0, |
| 242 | + Duration.ofSeconds(8000), |
| 243 | + Duration.ZERO |
| 244 | + ) |
| 245 | + ); |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + void testMatchesWithJourneyDuration() { |
| 250 | + FareAttribute journeyFare = FareAttribute |
| 251 | + .of(new FeedScopedId("feed", "journey")) |
| 252 | + .setPrice(Money.usDollars(3.00f)) |
| 253 | + .setPaymentMethod(1) |
| 254 | + .setJourneyDuration(7200) |
| 255 | + .build(); |
| 256 | + FareRuleSet journeyRuleSet = new FareRuleSet(journeyFare); |
| 257 | + |
| 258 | + assertTrue( |
| 259 | + journeyRuleSet.matches( |
| 260 | + "A", |
| 261 | + "B", |
| 262 | + new HashSet<>(), |
| 263 | + new HashSet<>(), |
| 264 | + new HashSet<>(), |
| 265 | + 0, |
| 266 | + Duration.ZERO, |
| 267 | + Duration.ofSeconds(7000) |
| 268 | + ) |
| 269 | + ); |
| 270 | + assertFalse( |
| 271 | + journeyRuleSet.matches( |
| 272 | + "A", |
| 273 | + "B", |
| 274 | + new HashSet<>(), |
| 275 | + new HashSet<>(), |
| 276 | + new HashSet<>(), |
| 277 | + 0, |
| 278 | + Duration.ZERO, |
| 279 | + Duration.ofSeconds(8000) |
| 280 | + ) |
| 281 | + ); |
| 282 | + } |
| 283 | + |
| 284 | + @Test |
| 285 | + void testAgencyMethods() { |
| 286 | + assertFalse(fareRuleSet.hasAgencyDefined()); |
| 287 | + assertNull(fareRuleSet.getAgency()); |
| 288 | + |
| 289 | + FeedScopedId agencyId = new FeedScopedId("feed", "agency1"); |
| 290 | + fareRuleSet.setAgency(agencyId); |
| 291 | + assertTrue(fareRuleSet.hasAgencyDefined()); |
| 292 | + assertEquals(agencyId, fareRuleSet.getAgency()); |
| 293 | + } |
| 294 | +} |
0 commit comments