Skip to content

Commit fed34b6

Browse files
Add test for mapper
1 parent 22481a6 commit fed34b6

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.opentripplanner.gtfs.mapping;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.time.LocalTime;
6+
import org.junit.jupiter.api.Test;
7+
import org.onebusaway.gtfs.model.AgencyAndId;
8+
import org.onebusaway.gtfs.model.Timeframe;
9+
10+
class TimeframeMapperTest {
11+
12+
private static final IdFactory A = new IdFactory("A");
13+
public static final LocalTime START = LocalTime.NOON;
14+
public static final LocalTime END = START.plusHours(1);
15+
16+
@Test
17+
void map() {
18+
var tf = new Timeframe();
19+
tf.setTimeframeGroupId(new AgencyAndId("a", "1"));
20+
tf.setId(new AgencyAndId("a", "1"));
21+
tf.setStartTime(START);
22+
tf.setEndTime(END);
23+
tf.setServiceId("s1");
24+
25+
var mapper = new TimeframeMapper(A);
26+
var mapped = mapper.map(tf);
27+
assertEquals(START, mapped.startTime());
28+
assertEquals(END, mapped.endTime());
29+
}
30+
}

application/src/ext/java/org/opentripplanner/ext/fares/model/Timeframe.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.Serializable;
66
import java.time.LocalTime;
77
import org.opentripplanner.core.model.id.FeedScopedId;
8+
import org.opentripplanner.utils.tostring.ValueObjectToStringBuilder;
89

910
/**
1011
* A fare timeframe which can model at what times on which dates a fare rule applies.
@@ -36,4 +37,17 @@ public LocalTime startTime() {
3637
public LocalTime endTime() {
3738
return end;
3839
}
40+
41+
@Override
42+
public String toString() {
43+
return ValueObjectToStringBuilder.of()
44+
.addText("[")
45+
.addObj(start)
46+
.addText("-")
47+
.addObj(end)
48+
.addText(",")
49+
.addObj(serviceId)
50+
.addText("]")
51+
.toString();
52+
}
3953
}

application/src/test/java/org/opentripplanner/gtfs/mapping/FareLegRuleMapperTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static com.google.common.truth.Truth.assertThat;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
55

6+
import java.time.LocalTime;
7+
import java.util.Collection;
68
import java.util.List;
79
import org.junit.jupiter.api.Test;
810
import org.junit.jupiter.params.ParameterizedTest;
@@ -11,6 +13,7 @@
1113
import org.onebusaway.gtfs.model.FareLegRule;
1214
import org.onebusaway.gtfs.model.FareMedium;
1315
import org.onebusaway.gtfs.model.FareProduct;
16+
import org.onebusaway.gtfs.model.Timeframe;
1417
import org.opentripplanner.ext.fares.model.FareDistance;
1518
import org.opentripplanner.ext.fares.model.FareDistance.LinearDistance;
1619
import org.opentripplanner.ext.fares.model.FareDistance.Stops;
@@ -157,6 +160,45 @@ void noProductFound() {
157160
);
158161
}
159162

163+
@Test
164+
void timeframes() {
165+
var timeframeMapper = timeframeMapper();
166+
var productMapper = new FareProductMapper(ID_FACTORY);
167+
var ruleMapper = new FareLegRuleMapper(
168+
ID_FACTORY,
169+
productMapper,
170+
timeframeMapper,
171+
DataImportIssueStore.NOOP
172+
);
173+
174+
var product = cashProduct(null);
175+
productMapper.map(product);
176+
177+
var tfId = new AgencyAndId("1", "tf1");
178+
var tf = new Timeframe();
179+
tf.setTimeframeGroupId(tfId);
180+
tf.setStartTime(LocalTime.NOON);
181+
tf.setEndTime(LocalTime.NOON.plusHours(1));
182+
tf.setServiceId("s1");
183+
timeframeMapper.map(tf);
184+
185+
var obaRule = new FareLegRule();
186+
obaRule.setFareProductId(product.getFareProductId());
187+
obaRule.setFromTimeframeGroupId(tfId);
188+
obaRule.setToTimeframeGroupId(tfId);
189+
190+
var mapped = List.copyOf(ruleMapper.map(List.of(obaRule))).getFirst();
191+
192+
assertEquals("[[12:00-13:00,A:s1]]", toStr(mapped.fromTimeframes()));
193+
assertEquals("[[12:00-13:00,A:s1]]", toStr(mapped.toTimeframes()));
194+
}
195+
196+
private static String toStr(
197+
Collection<org.opentripplanner.ext.fares.model.Timeframe> timeframes
198+
) {
199+
return timeframes.stream().map(t -> t.toString()).toList().toString();
200+
}
201+
160202
private static FareProduct cashProduct(FareMedium creditMedium) {
161203
var productId = new AgencyAndId("1", "1");
162204
var cashProduct = new FareProduct();

0 commit comments

Comments
 (0)