From 4f89618c1ffd1403133613d408d65fd85461dee7 Mon Sep 17 00:00:00 2001 From: Joel Lappalainen Date: Tue, 31 Dec 2024 16:23:12 +0200 Subject: [PATCH] Add tests for default injection --- .../apis/gtfs/SchemaFactoryTest.java | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/application/src/test/java/org/opentripplanner/apis/gtfs/SchemaFactoryTest.java b/application/src/test/java/org/opentripplanner/apis/gtfs/SchemaFactoryTest.java index 69122d9459d..547ec1ff86b 100644 --- a/application/src/test/java/org/opentripplanner/apis/gtfs/SchemaFactoryTest.java +++ b/application/src/test/java/org/opentripplanner/apis/gtfs/SchemaFactoryTest.java @@ -1,13 +1,18 @@ package org.opentripplanner.apis.gtfs; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertSame; +import graphql.language.FloatValue; +import graphql.language.IntValue; +import graphql.language.Value; import graphql.schema.AsyncDataFetcher; import graphql.schema.DataFetcher; import graphql.schema.FieldCoordinates; import graphql.schema.GraphQLFieldDefinition; +import graphql.schema.GraphQLInputObjectType; import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; import org.junit.jupiter.api.Test; @@ -19,11 +24,49 @@ public class SchemaFactoryTest { @Test - public void createSchema() { + void createSchema() { var schema = SchemaFactory.createSchema(new RouteRequest()); assertNotNull(schema); } + @Test + void testDefaultValueInjection() { + var routeRequest = new RouteRequest(); + double walkSpeed = 15; + routeRequest.withPreferences(preferences -> + preferences.withWalk(walk -> walk.withSpeed(walkSpeed)) + ); + var maxTransfers = 2; + routeRequest.withPreferences(preferences -> + preferences.withTransfer(transfer -> transfer.withMaxTransfers(maxTransfers + 1)) + ); + var numItineraries = 63; + routeRequest.setNumItineraries(numItineraries); + var schema = SchemaFactory.createSchema(routeRequest); + assertNotNull(schema); + + var defaultSpeed = (FloatValue) getDefaultValueForField( + schema, + "WalkPreferencesInput", + "speed" + ); + assertEquals(walkSpeed, defaultSpeed.getValue().doubleValue(), 0.01f); + + var defaultMaxTransfers = (IntValue) getDefaultValueForField( + schema, + "TransferPreferencesInput", + "maximumTransfers" + ); + assertEquals(maxTransfers, defaultMaxTransfers.getValue().intValue()); + + var defaultNumberOfItineraries = (IntValue) getDefaultValueForArgument( + schema, + "planConnection", + "first" + ); + assertEquals(numItineraries, defaultNumberOfItineraries.getValue().intValue()); + } + @ValueSource(strings = { "plan", "nearest" }) @ParameterizedTest(name = "\"{0}\" must be a an async fetcher") void asyncDataFetchers(String fieldName) { @@ -51,4 +94,22 @@ private static DataFetcher getQueryType(String fieldName, GraphQLSchema schem .build() ); } + + private static Value getDefaultValueForField( + GraphQLSchema schema, + String inputObjectName, + String fieldName + ) { + GraphQLInputObjectType inputObject = schema.getTypeAs(inputObjectName); + return (Value) inputObject.getField(fieldName).getInputFieldDefaultValue().getValue(); + } + + private static Value getDefaultValueForArgument( + GraphQLSchema schema, + String queryName, + String argumentName + ) { + var query = schema.getQueryType().getField(queryName); + return (Value) query.getArgument(argumentName).getArgumentDefaultValue().getValue(); + } }