From 305c6cea0431e00c8ea5c8b350193b62ee0a152e Mon Sep 17 00:00:00 2001 From: Vincent Paturet Date: Tue, 7 May 2024 14:36:28 +0200 Subject: [PATCH] Add unit tests --- .../apis/transmodel/support/GqlUtilTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/test/java/org/opentripplanner/apis/transmodel/support/GqlUtilTest.java b/src/test/java/org/opentripplanner/apis/transmodel/support/GqlUtilTest.java index d2d15168baf..40a95a6661a 100644 --- a/src/test/java/org/opentripplanner/apis/transmodel/support/GqlUtilTest.java +++ b/src/test/java/org/opentripplanner/apis/transmodel/support/GqlUtilTest.java @@ -2,11 +2,14 @@ import static graphql.execution.ExecutionContextBuilder.newExecutionContextBuilder; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import graphql.ExecutionInput; import graphql.execution.ExecutionContext; import graphql.execution.ExecutionId; +import graphql.schema.DataFetchingEnvironment; import graphql.schema.DataFetchingEnvironmentImpl; +import java.util.HashMap; import java.util.Locale; import java.util.Map; import org.junit.jupiter.api.Test; @@ -14,6 +17,7 @@ public class GqlUtilTest { static final ExecutionContext executionContext; + private static final String TEST_ARGUMENT = "testArgument"; static { ExecutionInput executionInput = ExecutionInput @@ -29,6 +33,54 @@ public class GqlUtilTest { .build(); } + @Test + void testGetPositiveNonNullIntegerArgumentWithStrictlyPositiveValue() { + var env = buildEnvWithTestValue(1); + assertEquals(1, GqlUtil.getPositiveNonNullIntegerArgument(env, TEST_ARGUMENT)); + } + + @Test + void testGetPositiveNonNullIntegerArgumentWithZeroValue() { + var env = buildEnvWithTestValue(0); + assertEquals(0, GqlUtil.getPositiveNonNullIntegerArgument(env, TEST_ARGUMENT)); + } + + @Test + void testGetPositiveNonNullIntegerArgumentWithNegativeValue() { + var env = buildEnvWithTestValue(-1); + assertThrows( + IllegalArgumentException.class, + () -> GqlUtil.getPositiveNonNullIntegerArgument(env, TEST_ARGUMENT) + ); + } + + @Test + void testGetPositiveNonNullIntegerArgumentWithNullValue() { + var env = buildEnvWithTestValue(null); + assertThrows( + IllegalArgumentException.class, + () -> GqlUtil.getPositiveNonNullIntegerArgument(env, TEST_ARGUMENT) + ); + } + + @Test + void testGetPositiveNonNullIntegerArgumentWithoutValue() { + var env = DataFetchingEnvironmentImpl.newDataFetchingEnvironment(executionContext).build(); + assertThrows( + IllegalArgumentException.class, + () -> GqlUtil.getPositiveNonNullIntegerArgument(env, TEST_ARGUMENT) + ); + } + + private static DataFetchingEnvironment buildEnvWithTestValue(Integer value) { + Map argsMap = new HashMap<>(); + argsMap.put(TEST_ARGUMENT, value); + return DataFetchingEnvironmentImpl + .newDataFetchingEnvironment(executionContext) + .arguments(argsMap) + .build(); + } + @Test void testGetLocaleWithLangArgument() { var env = DataFetchingEnvironmentImpl