Skip to content

Commit aa46f2a

Browse files
committed
Add tests
1 parent ace6826 commit aa46f2a

1 file changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package org.opentripplanner.ext.legacygraphqlapi;
2+
3+
import static graphql.execution.ExecutionContextBuilder.newExecutionContextBuilder;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import graphql.ExecutionInput;
7+
import graphql.execution.ExecutionContext;
8+
import graphql.execution.ExecutionId;
9+
import graphql.schema.DataFetchingEnvironmentImpl;
10+
import java.util.List;
11+
import java.util.Locale;
12+
import java.util.Map;
13+
import org.junit.jupiter.api.Test;
14+
import org.opentripplanner._support.time.ZoneIds;
15+
import org.opentripplanner.ext.fares.impl.DefaultFareService;
16+
import org.opentripplanner.routing.api.request.RouteRequest;
17+
import org.opentripplanner.routing.graph.Graph;
18+
import org.opentripplanner.routing.graphfinder.GraphFinder;
19+
import org.opentripplanner.service.vehiclepositions.internal.DefaultVehiclePositionService;
20+
import org.opentripplanner.service.vehiclerental.internal.DefaultVehicleRentalService;
21+
import org.opentripplanner.transit.service.DefaultTransitService;
22+
import org.opentripplanner.transit.service.TransitModel;
23+
24+
class LegacyGraphQLUtilsTest {
25+
26+
static final LegacyGraphQLRequestContext context;
27+
28+
static {
29+
Graph graph = new Graph();
30+
var transitModel = new TransitModel();
31+
transitModel.initTimeZone(ZoneIds.BERLIN);
32+
final DefaultTransitService transitService = new DefaultTransitService(transitModel);
33+
context =
34+
new LegacyGraphQLRequestContext(
35+
new TestRoutingService(List.of()),
36+
transitService,
37+
new DefaultFareService(),
38+
graph.getVehicleParkingService(),
39+
new DefaultVehicleRentalService(),
40+
new DefaultVehiclePositionService(),
41+
GraphFinder.getInstance(graph, transitService::findRegularStop),
42+
new RouteRequest()
43+
);
44+
}
45+
46+
@Test
47+
void testGetLocaleWithDefinedLocaleArg() {
48+
var executionContext = getGenericExecutionContext();
49+
50+
var env = DataFetchingEnvironmentImpl
51+
.newDataFetchingEnvironment(executionContext)
52+
.localContext(Map.of("locale", Locale.GERMAN))
53+
.locale(Locale.ENGLISH)
54+
.build();
55+
56+
var frenchLocale = Locale.FRENCH;
57+
58+
var locale = LegacyGraphQLUtils.getLocale(env, frenchLocale.toString());
59+
60+
assertEquals(frenchLocale, locale);
61+
}
62+
63+
@Test
64+
void testGetLocaleWithEnvLocale() {
65+
var executionContext = getGenericExecutionContext();
66+
67+
var frenchLocale = Locale.FRENCH;
68+
var env = DataFetchingEnvironmentImpl
69+
.newDataFetchingEnvironment(executionContext)
70+
.localContext(Map.of("locale", Locale.GERMAN))
71+
.locale(frenchLocale)
72+
.build();
73+
74+
var locale = LegacyGraphQLUtils.getLocale(env);
75+
76+
assertEquals(frenchLocale, locale);
77+
}
78+
79+
@Test
80+
void testGetLocaleWithLocalContextLocale() {
81+
var executionContext = getGenericExecutionContext();
82+
83+
// Should use locale from local context if env locale is not defined
84+
85+
var frenchLocale = Locale.FRENCH;
86+
var envWithNoLocale = DataFetchingEnvironmentImpl
87+
.newDataFetchingEnvironment(executionContext)
88+
.localContext(Map.of("locale", Locale.FRENCH))
89+
.build();
90+
91+
var locale = LegacyGraphQLUtils.getLocale(envWithNoLocale);
92+
93+
assertEquals(frenchLocale, locale);
94+
95+
// Wildcard locale from env should not override locale from local context if it's defined
96+
97+
var wildcardLocale = new Locale("*");
98+
99+
var envWithWildcardLocale = DataFetchingEnvironmentImpl
100+
.newDataFetchingEnvironment(executionContext)
101+
.locale(wildcardLocale)
102+
.localContext(Map.of("locale", Locale.FRENCH))
103+
.build();
104+
105+
locale = LegacyGraphQLUtils.getLocale(envWithWildcardLocale);
106+
107+
assertEquals(frenchLocale, locale);
108+
}
109+
110+
private ExecutionContext getGenericExecutionContext() {
111+
ExecutionInput executionInput = ExecutionInput
112+
.newExecutionInput()
113+
.query("")
114+
.operationName("plan")
115+
.context(context)
116+
.locale(Locale.ENGLISH)
117+
.build();
118+
119+
return newExecutionContextBuilder()
120+
.executionInput(executionInput)
121+
.executionId(ExecutionId.from(this.getClass().getName()))
122+
.build();
123+
}
124+
}

0 commit comments

Comments
 (0)