forked from opentripplanner/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 30
/
RouteRequestMapperTest.java
222 lines (193 loc) · 7.77 KB
/
RouteRequestMapperTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package org.opentripplanner.ext.gtfsgraphqlapi.mapping;
import static graphql.execution.ExecutionContextBuilder.newExecutionContextBuilder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.params.provider.Arguments.of;
import static org.opentripplanner.routing.core.BicycleOptimizeType.SAFE;
import static org.opentripplanner.routing.core.BicycleOptimizeType.TRIANGLE;
import graphql.ExecutionInput;
import graphql.execution.ExecutionId;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.DataFetchingEnvironmentImpl;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.opentripplanner._support.time.ZoneIds;
import org.opentripplanner.ext.fares.impl.DefaultFareService;
import org.opentripplanner.ext.gtfsgraphqlapi.GraphQLRequestContext;
import org.opentripplanner.ext.gtfsgraphqlapi.TestRoutingService;
import org.opentripplanner.model.plan.PlanTestConstants;
import org.opentripplanner.routing.api.request.RouteRequest;
import org.opentripplanner.routing.api.request.preference.TimeSlopeSafetyTriangle;
import org.opentripplanner.routing.api.request.request.VehicleParkingRequest;
import org.opentripplanner.routing.core.BicycleOptimizeType;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graphfinder.GraphFinder;
import org.opentripplanner.service.realtimevehicles.internal.DefaultRealtimeVehicleService;
import org.opentripplanner.service.vehiclerental.internal.DefaultVehicleRentalService;
import org.opentripplanner.test.support.VariableSource;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitModel;
class RouteRequestMapperTest implements PlanTestConstants {
static final GraphQLRequestContext context;
static {
Graph graph = new Graph();
var transitModel = new TransitModel();
transitModel.initTimeZone(ZoneIds.BERLIN);
final DefaultTransitService transitService = new DefaultTransitService(transitModel);
context =
new GraphQLRequestContext(
new TestRoutingService(List.of()),
transitService,
new DefaultFareService(),
graph.getVehicleParkingService(),
new DefaultVehicleRentalService(),
new DefaultRealtimeVehicleService(),
GraphFinder.getInstance(graph, transitService::findRegularStop),
new RouteRequest()
);
}
@Test
void parkingFilters() {
Map<String, Object> arguments = Map.of(
"parking",
Map.of(
"unpreferredCost",
555,
"filters",
List.of(
Map.of(
"not",
List.of(Map.of("tags", List.of("wheelbender"))),
"select",
List.of(Map.of("tags", List.of("roof", "locker")))
)
),
"preferred",
List.of(Map.of("select", List.of(Map.of("tags", List.of("a", "b")))))
)
);
var env = executionContext(arguments);
var routeRequest = RouteRequestMapper.toRouteRequest(env, context);
assertNotNull(routeRequest);
final VehicleParkingRequest parking = routeRequest.journey().parking();
assertEquals(
"VehicleParkingFilterRequest{not: [tags=[wheelbender]], select: [tags=[locker, roof]]}",
parking.filter().toString()
);
assertEquals(
"VehicleParkingFilterRequest{select: [tags=[a, b]]}",
parking.preferred().toString()
);
assertEquals(555, parking.unpreferredCost());
}
static Stream<Arguments> banningCases = Stream.of(
of(Map.of(), "[TransitFilterRequest{}]"),
of(
Map.of("routes", "trimet:555"),
"[TransitFilterRequest{not: [SelectRequest{transportModes: [], routes: [trimet:555]}]}]"
),
of(Map.of("agencies", ""), "[TransitFilterRequest{not: [SelectRequest{transportModes: []}]}]"),
of(
Map.of("agencies", "trimet:666"),
"[TransitFilterRequest{not: [SelectRequest{transportModes: [], agencies: [trimet:666]}]}]"
),
of(
Map.of("agencies", "trimet:666", "routes", "trimet:444"),
"[TransitFilterRequest{not: [SelectRequest{transportModes: [], routes: [trimet:444]}, SelectRequest{transportModes: [], agencies: [trimet:666]}]}]"
)
);
@ParameterizedTest
@VariableSource("banningCases")
void banning(Map<String, Object> banned, String expectedFilters) {
Map<String, Object> arguments = Map.of("banned", banned);
var routeRequest = RouteRequestMapper.toRouteRequest(executionContext(arguments), context);
assertNotNull(routeRequest);
assertEquals(expectedFilters, routeRequest.journey().transit().filters().toString());
}
static Stream<Arguments> transportModesCases = Stream.of(
of(List.of(), "[ExcludeAllTransitFilter{}]"),
of(List.of(mode("BICYCLE")), "[ExcludeAllTransitFilter{}]"),
of(
List.of(mode("BUS")),
"[TransitFilterRequest{select: [SelectRequest{transportModes: [BUS, COACH]}]}]"
),
of(
List.of(mode("BUS"), mode("MONORAIL")),
"[TransitFilterRequest{select: [SelectRequest{transportModes: [BUS, COACH, MONORAIL]}]}]"
)
);
@ParameterizedTest
@VariableSource("transportModesCases")
void modes(List<Map<String, Object>> modes, String expectedFilters) {
Map<String, Object> arguments = Map.of("transportModes", modes);
var routeRequest = RouteRequestMapper.toRouteRequest(executionContext(arguments), context);
assertNotNull(routeRequest);
assertEquals(expectedFilters, routeRequest.journey().transit().filters().toString());
}
private static Map<String, Object> mode(String mode) {
return Map.of("mode", mode);
}
@Test
void defaultBikeOptimize() {
Map<String, Object> arguments = Map.of();
var routeRequest = RouteRequestMapper.toRouteRequest(executionContext(arguments), context);
assertEquals(SAFE, routeRequest.preferences().bike().optimizeType());
}
@Test
void bikeTriangle() {
Map<String, Object> arguments = Map.of(
"optimize",
"TRIANGLE",
"triangle",
Map.of("safetyFactor", 0.2, "slopeFactor", 0.1, "timeFactor", 0.7)
);
var routeRequest = RouteRequestMapper.toRouteRequest(executionContext(arguments), context);
assertEquals(TRIANGLE, routeRequest.preferences().bike().optimizeType());
assertEquals(
new TimeSlopeSafetyTriangle(0.7, 0.1, 0.2),
routeRequest.preferences().bike().optimizeTriangle()
);
}
static Stream<Arguments> noTriangleCases = BicycleOptimizeType
.nonTriangleValues()
.stream()
.map(Arguments::of);
@ParameterizedTest
@VariableSource("noTriangleCases")
void noTriangle(BicycleOptimizeType bot) {
Map<String, Object> arguments = Map.of(
"optimize",
bot.name(),
"triangle",
Map.of("safetyFactor", 0.2, "slopeFactor", 0.1, "timeFactor", 0.7)
);
var routeRequest = RouteRequestMapper.toRouteRequest(executionContext(arguments), context);
assertEquals(bot, routeRequest.preferences().bike().optimizeType());
assertEquals(
TimeSlopeSafetyTriangle.DEFAULT,
routeRequest.preferences().bike().optimizeTriangle()
);
}
private DataFetchingEnvironment executionContext(Map<String, Object> arguments) {
ExecutionInput executionInput = ExecutionInput
.newExecutionInput()
.query("")
.operationName("plan")
.context(context)
.locale(Locale.ENGLISH)
.build();
var executionContext = newExecutionContextBuilder()
.executionInput(executionInput)
.executionId(ExecutionId.from(this.getClass().getName()))
.build();
return DataFetchingEnvironmentImpl
.newDataFetchingEnvironment(executionContext)
.arguments(arguments)
.build();
}
}