Skip to content

Commit

Permalink
Remove enum mapper test for REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Dec 18, 2024
1 parent 977d8eb commit b7cc6fd
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,11 @@
import java.util.Map;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
import org.opentripplanner.ext.restapi.model.ApiAbsoluteDirection;
import org.opentripplanner.ext.restapi.model.ApiRelativeDirection;
import org.opentripplanner.ext.restapi.model.ApiVertexType;
import org.opentripplanner.model.plan.AbsoluteDirection;
import org.opentripplanner.model.plan.RelativeDirection;
import org.opentripplanner.model.plan.VertexType;

public class EnumMapperTest {

private static final String MSG =
"Assert that the API enums have the exact same values that " +
"the domain enums of the same type, and that the specialized mapper is mapping all " +
"values. If this assumtion does not hold, create a new test.";

@Test
public void map() {
try {
verifyExactMatch(
AbsoluteDirection.class,
ApiAbsoluteDirection.class,
AbsoluteDirectionMapper::mapAbsoluteDirection
);
verifyExactMatch(
RelativeDirection.class,
ApiRelativeDirection.class,
RelativeDirectionMapper::mapRelativeDirection
);
} catch (RuntimeException ex) {
System.out.println(MSG);
throw ex;
}
}

@Test
public void testVertexTypeMapping() {
verifyExplicitMatch(
Expand Down Expand Up @@ -75,17 +47,4 @@ private <D extends Enum<?>, A extends Enum<?>> void verifyExplicitMatch(
assertTrue(rest.isEmpty());
}

private <D extends Enum<?>, A extends Enum<?>> void verifyExactMatch(
Class<D> domainClass,
Class<A> apiClass,
Function<D, A> mapper
) {
List<A> rest = new ArrayList<>(List.of(apiClass.getEnumConstants()));
for (D it : domainClass.getEnumConstants()) {
A result = mapper.apply(it);
assertEquals(result.name(), it.name());
rest.remove(result);
}
assertTrue(rest.isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ApiWalkStep mapWalkStep(WalkStep domain) {
api.streetName = domain.getDirectionText().toString(locale);
api.absoluteDirection =
domain.getAbsoluteDirection().map(AbsoluteDirectionMapper::mapAbsoluteDirection).orElse(null);
api.exit = domain.isHighwayExit();
api.exit = domain.highwayExit().orElse(null);
api.stayOn = domain.isStayOn();
api.area = domain.getArea();
api.bogusName = domain.nameIsDerived();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.opentripplanner.apis.gtfs.mapping.DirectionMapper;
import org.opentripplanner.apis.gtfs.mapping.StreetNoteMapper;
import org.opentripplanner.model.plan.ElevationProfile.Step;
import org.opentripplanner.model.plan.RelativeDirection;
import org.opentripplanner.model.plan.WalkStep;
import org.opentripplanner.routing.alertpatch.TransitAlert;

Expand Down Expand Up @@ -51,18 +50,12 @@ public DataFetcher<Iterable<Step>> elevationProfile() {

@Override
public DataFetcher<String> exit() {
return environment -> getSource(environment).isHighwayExit();
return environment -> getSource(environment).highwayExit().orElse(null);
}

@Override
public DataFetcher<Object> feature() {
return environment -> {
WalkStep source = getSource(environment);
if (source.getRelativeDirection() == RelativeDirection.ENTER_OR_EXIT_STATION) {
return source.getEntrance();
}
return null;
};
return environment -> getSource(environment).entrance().orElse(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public static GraphQLObjectType create(GraphQLObjectType elevationStepType) {
.name("exit")
.description("When exiting a highway or traffic circle, the exit name/number.")
.type(Scalars.GraphQLString)
.dataFetcher(environment -> ((WalkStep) environment.getSource()).isHighwayExit())
.dataFetcher(environment ->
((WalkStep) environment.getSource()).highwayExit().orElse(null)
)
.build()
)
.field(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ public Optional<AbsoluteDirection> getAbsoluteDirection() {
/**
* When exiting a highway or traffic circle, the exit name/number.
*/
public String isHighwayExit() {
return exit;
public Optional<String> highwayExit() {
return exit.describeConstable();
}

/**
* Get information about a subway station entrance or exit.
*/
public Entrance getEntrance() {
return entrance;
public Optional<Entrance> entrance() {
return Optional.ofNullable(entrance);
}

/**
Expand Down

0 comments on commit b7cc6fd

Please sign in to comment.