Skip to content

Commit

Permalink
Fix mapping in Transmodel API
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Jan 10, 2025
1 parent 628bf95 commit 4248ffe
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.opentripplanner.apis.transmodel.mapping;

import org.opentripplanner.model.plan.RelativeDirection;

/**
* This mapper makes sure that only those values are returned which have a mapping in the Transmodel API,
* as we don't really want to return all of them.
*/
public class RelativeDirectionMapper {

public static RelativeDirection map(RelativeDirection relativeDirection) {
return switch (relativeDirection) {
case DEPART,
SLIGHTLY_LEFT,
HARD_LEFT,
LEFT,
CONTINUE,
SLIGHTLY_RIGHT,
RIGHT,
HARD_RIGHT,
CIRCLE_CLOCKWISE,
CIRCLE_COUNTERCLOCKWISE,
ELEVATOR,
UTURN_LEFT,
UTURN_RIGHT -> relativeDirection;
// for these the Transmodel API doesn't have a mapping. should it?
case ENTER_STATION,
EXIT_STATION,
ENTER_OR_EXIT_STATION,
FOLLOW_SIGNS -> RelativeDirection.CONTINUE;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLObjectType;
import org.opentripplanner.apis.transmodel.mapping.RelativeDirectionMapper;
import org.opentripplanner.apis.transmodel.model.EnumTypes;
import org.opentripplanner.framework.graphql.GraphQLUtils;
import org.opentripplanner.model.plan.WalkStep;
Expand All @@ -31,7 +32,9 @@ public static GraphQLObjectType create(GraphQLObjectType elevationStepType) {
.name("relativeDirection")
.description("The relative direction of this step.")
.type(EnumTypes.RELATIVE_DIRECTION)
.dataFetcher(environment -> ((WalkStep) environment.getSource()).getRelativeDirection())
.dataFetcher(environment ->
RelativeDirectionMapper.map(((WalkStep) environment.getSource()).getRelativeDirection())
)
.build()
)
.field(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package org.opentripplanner.apis.transmodel.model;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.opentripplanner.apis.transmodel.model.EnumTypes.RELATIVE_DIRECTION;
import static org.opentripplanner.apis.transmodel.model.EnumTypes.ROUTING_ERROR_CODE;
import static org.opentripplanner.apis.transmodel.model.EnumTypes.map;

import graphql.GraphQLContext;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.opentripplanner.apis.transmodel.mapping.RelativeDirectionMapper;
import org.opentripplanner.framework.doc.DocumentedEnum;
import org.opentripplanner.model.plan.RelativeDirection;
import org.opentripplanner.routing.api.response.RoutingErrorCode;

class EnumTypesTest {
Expand Down Expand Up @@ -75,6 +84,18 @@ void testMap() {
assertEquals("DocumentedEnumMapping[apiName=iH, internal=Hi]", mapping.toString());
}

@ParameterizedTest
@EnumSource(RelativeDirection.class)
void serializeRelativeDirection(RelativeDirection direction) {
var value = RELATIVE_DIRECTION.serialize(
RelativeDirectionMapper.map(direction),
GraphQLContext.getDefault(),
Locale.ENGLISH
);
assertInstanceOf(String.class, value);
assertNotNull(value);
}

@Test
void assertAllRoutingErrorCodesAreMapped() {
var expected = EnumSet.allOf(RoutingErrorCode.class);
Expand Down

0 comments on commit 4248ffe

Please sign in to comment.