Skip to content

Commit

Permalink
Merge branch 'fix-plan-locale' into dev-2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
optionsome committed Mar 16, 2023
2 parents a4395f3 + a7dd518 commit c992859
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/sandbox/LegacyGraphQLApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ on this API and the [Legacy GraphQL Api](LegacyGraphQLApi.md). The new API will
- Rename unpreferredRouteCost to unpreferredCost (October 2022, [#4543](https://github.com/opentripplanner/OpenTripPlanner/pull/4543))
- Make plan fetcher async (December 2022, [#4676](https://github.com/opentripplanner/OpenTripPlanner/pull/4676))
- Fix alerts query severity, effect and cause filters (February 2023, [#4909](https://github.com/opentripplanner/OpenTripPlanner/pull/4909))
- Use accept-language header instead of the default route request locale in the plan query (March 2023, [#4971](https://github.com/opentripplanner/OpenTripPlanner/pull/4971))
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package org.opentripplanner.ext.legacygraphqlapi;

import static graphql.execution.ExecutionContextBuilder.newExecutionContextBuilder;
import static org.junit.jupiter.api.Assertions.assertEquals;

import graphql.ExecutionInput;
import graphql.execution.ExecutionContext;
import graphql.execution.ExecutionId;
import graphql.schema.DataFetchingEnvironmentImpl;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.opentripplanner._support.time.ZoneIds;
import org.opentripplanner.ext.fares.impl.DefaultFareService;
import org.opentripplanner.routing.api.request.RouteRequest;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graphfinder.GraphFinder;
import org.opentripplanner.service.vehiclepositions.internal.DefaultVehiclePositionService;
import org.opentripplanner.service.vehiclerental.internal.DefaultVehicleRentalService;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitModel;

class LegacyGraphQLUtilsTest {

static final LegacyGraphQLRequestContext context;

static {
Graph graph = new Graph();
var transitModel = new TransitModel();
transitModel.initTimeZone(ZoneIds.BERLIN);
final DefaultTransitService transitService = new DefaultTransitService(transitModel);
context =
new LegacyGraphQLRequestContext(
new TestRoutingService(List.of()),
transitService,
new DefaultFareService(),
graph.getVehicleParkingService(),
new DefaultVehicleRentalService(),
new DefaultVehiclePositionService(),
GraphFinder.getInstance(graph, transitService::findRegularStop),
new RouteRequest()
);
}

@Test
void testGetLocaleWithDefinedLocaleArg() {
var executionContext = getGenericExecutionContext();

var env = DataFetchingEnvironmentImpl
.newDataFetchingEnvironment(executionContext)
.localContext(Map.of("locale", Locale.GERMAN))
.locale(Locale.ENGLISH)
.build();

var frenchLocale = Locale.FRENCH;

var locale = LegacyGraphQLUtils.getLocale(env, frenchLocale.toString());

assertEquals(frenchLocale, locale);
}

@Test
void testGetLocaleWithEnvLocale() {
var executionContext = getGenericExecutionContext();

var frenchLocale = Locale.FRENCH;
var env = DataFetchingEnvironmentImpl
.newDataFetchingEnvironment(executionContext)
.localContext(Map.of("locale", Locale.GERMAN))
.locale(frenchLocale)
.build();

var locale = LegacyGraphQLUtils.getLocale(env);

assertEquals(frenchLocale, locale);
}

@Test
void testGetLocaleWithLocalContextLocale() {
var executionContext = getGenericExecutionContext();

// Should use locale from local context if env locale is not defined

var frenchLocale = Locale.FRENCH;
var envWithNoLocale = DataFetchingEnvironmentImpl
.newDataFetchingEnvironment(executionContext)
.localContext(Map.of("locale", Locale.FRENCH))
.build();

var locale = LegacyGraphQLUtils.getLocale(envWithNoLocale);

assertEquals(frenchLocale, locale);

// Wildcard locale from env should not override locale from local context if it's defined

var wildcardLocale = new Locale("*");

var envWithWildcardLocale = DataFetchingEnvironmentImpl
.newDataFetchingEnvironment(executionContext)
.locale(wildcardLocale)
.localContext(Map.of("locale", Locale.FRENCH))
.build();

locale = LegacyGraphQLUtils.getLocale(envWithWildcardLocale);

assertEquals(frenchLocale, locale);
}

private ExecutionContext getGenericExecutionContext() {
ExecutionInput executionInput = ExecutionInput
.newExecutionInput()
.query("")
.operationName("plan")
.context(context)
.locale(Locale.ENGLISH)
.build();

return newExecutionContextBuilder()
.executionInput(executionInput)
.executionId(ExecutionId.from(this.getClass().getName()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ public static Locale getLocale(DataFetchingEnvironment environment, String local
return Locale.forLanguageTag(localeString);
}

// This can come from the accept-language header
var envLocale = environment.getLocale();

Map<String, ?> localContext = environment.getLocalContext();
if (localContext != null && localContext.get("locale") != null) {
if (
(envLocale == null || envLocale.getLanguage().equals("*")) &&
localContext != null &&
localContext.get("locale") != null
) {
return (Locale) localContext.get("locale");
}

return environment.getLocale();
return envLocale;
}

public static String getTranslation(I18NString input, DataFetchingEnvironment environment) {
Expand Down

0 comments on commit c992859

Please sign in to comment.