Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vpaturet committed May 7, 2024
1 parent 63bce5e commit 305c6ce
Showing 1 changed file with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

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;

public class GqlUtilTest {

static final ExecutionContext executionContext;
private static final String TEST_ARGUMENT = "testArgument";

static {
ExecutionInput executionInput = ExecutionInput
Expand All @@ -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<String, Object> argsMap = new HashMap<>();
argsMap.put(TEST_ARGUMENT, value);
return DataFetchingEnvironmentImpl
.newDataFetchingEnvironment(executionContext)
.arguments(argsMap)
.build();
}

@Test
void testGetLocaleWithLangArgument() {
var env = DataFetchingEnvironmentImpl
Expand Down

0 comments on commit 305c6ce

Please sign in to comment.