Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 104d152

Browse files
refactor: test template refactoring, additional tests
1 parent 6039dfb commit 104d152

File tree

6 files changed

+54
-15
lines changed

6 files changed

+54
-15
lines changed

graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestTemplate.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.charset.Charset;
2323
import java.nio.charset.StandardCharsets;
2424
import java.util.Arrays;
25+
import java.util.Collections;
2526
import java.util.List;
2627

2728
import static java.util.Objects.nonNull;
@@ -218,7 +219,7 @@ public GraphQLResponse perform(String graphqlResource) throws IOException {
218219
* @throws IOException if the resource cannot be loaded from the classpath
219220
*/
220221
public GraphQLResponse perform(String graphqlResource, ObjectNode variables) throws IOException {
221-
return perform(graphqlResource, null, variables);
222+
return perform(graphqlResource, null, variables, Collections.emptyList());
222223
}
223224

224225
/**
@@ -230,7 +231,7 @@ public GraphQLResponse perform(String graphqlResource, ObjectNode variables) thr
230231
* @throws IOException if the resource cannot be loaded from the classpath
231232
*/
232233
public GraphQLResponse perform(String graphqlResource, String operationName) throws IOException {
233-
return perform(graphqlResource, operationName, null);
234+
return perform(graphqlResource, operationName, null, Collections.emptyList());
234235
}
235236

236237
/**
@@ -243,9 +244,7 @@ public GraphQLResponse perform(String graphqlResource, String operationName) thr
243244
* @throws IOException if the resource cannot be loaded from the classpath
244245
*/
245246
public GraphQLResponse perform(String graphqlResource, String operation, ObjectNode variables) throws IOException {
246-
String graphql = loadQuery(graphqlResource);
247-
String payload = createJsonQuery(graphql, operation, variables);
248-
return post(payload);
247+
return perform(graphqlResource, operation, variables, Collections.emptyList());
249248
}
250249

251250
/**
@@ -288,7 +287,7 @@ public GraphQLResponse perform(String graphqlResource, String operationName, Obj
288287
* @throws IOException if the resource cannot be loaded from the classpath
289288
*/
290289
public GraphQLResponse postForResource(String graphqlResource) throws IOException {
291-
return perform(graphqlResource, (String) null, null);
290+
return perform(graphqlResource, null, null, Collections.emptyList());
292291
}
293292

294293
/**
@@ -301,7 +300,7 @@ public GraphQLResponse postForResource(String graphqlResource) throws IOExceptio
301300
* @throws IOException if the resource cannot be loaded from the classpath
302301
*/
303302
public GraphQLResponse postForResource(String graphqlResource, List<String> fragmentResources) throws IOException {
304-
return perform(graphqlResource, null, fragmentResources);
303+
return perform(graphqlResource, null, null, fragmentResources);
305304
}
306305

307306
public GraphQLResponse postMultipart(String query, String variables) {

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLTestTemplateIntegrationTest.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.io.IOException;
1515
import java.util.Collections;
16+
import java.util.List;
1617

1718
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
1819
public class GraphQLTestTemplateIntegrationTest {
@@ -22,6 +23,8 @@ public class GraphQLTestTemplateIntegrationTest {
2223
private static final String TEST_FRAGMENT_FILE = "foo-bar-fragment.graphql";
2324
private static final String QUERY_WITH_VARIABLES = "query-with-variables.graphql";
2425
private static final String MULTIPLE_QUERIES = "multiple-queries.graphql";
26+
private static final String INPUT_STRING_VALUE = "input-value";
27+
private static final String INPUT_STRING_NAME = "input";
2528

2629
@Autowired
2730
private ResourceLoader resourceLoader;
@@ -63,13 +66,41 @@ void testPostForResourceWithFragments() throws IOException {
6366
@DisplayName("Test perform with variables.")
6467
void testPerformWithVariables() throws IOException {
6568
// GIVEN
66-
final String inputString = "test-input-string";
6769
final ObjectNode variables = objectMapper.createObjectNode();
68-
variables.put("input", inputString);
70+
variables.put(INPUT_STRING_NAME, INPUT_STRING_VALUE);
6971
// WHEN - THEN
7072
graphQLTestTemplate.perform(QUERY_WITH_VARIABLES, variables)
7173
.assertThatNoErrorsArePresent()
72-
.assertThatField("$.data.queryWithVariables").asString().isEqualTo(inputString);
74+
.assertThatField("$.data.queryWithVariables").asString().isEqualTo(INPUT_STRING_VALUE);
75+
}
76+
77+
@Test
78+
@DisplayName("Test perform with variables and operation name")
79+
void testPerformWithOperationAndVariables() throws IOException {
80+
// GIVEN
81+
final ObjectNode variables = objectMapper.createObjectNode();
82+
variables.put(INPUT_STRING_NAME, INPUT_STRING_VALUE);
83+
// WHEN - THEN
84+
graphQLTestTemplate.perform(MULTIPLE_QUERIES, "withVariable", variables)
85+
.assertThatNoErrorsArePresent()
86+
.assertThatField("$.data.queryWithVariables").asString().isEqualTo(INPUT_STRING_VALUE);
87+
}
88+
89+
@Test
90+
@DisplayName("Test perform with variables and fragments")
91+
void testPerformWithVariablesAndFragments() throws IOException {
92+
// GIVEN
93+
final String customFoo = "custom-foo";
94+
final String customBar = "custom-bar";
95+
final ObjectNode variables = objectMapper.createObjectNode();
96+
variables.put("foo", customFoo);
97+
variables.put("bar", customBar);
98+
final FooBar expected = new FooBar(customFoo, customBar);
99+
// WHEN - THEN
100+
graphQLTestTemplate.perform(SIMPLE_TEST_QUERY_WITH_FRAGMENTS, variables, List.of(TEST_FRAGMENT_FILE))
101+
.assertThatNoErrorsArePresent()
102+
.assertThatField("$.data.fooBar")
103+
.as(FooBar.class).usingRecursiveComparison().ignoringAllOverriddenEquals().isEqualTo(expected);
73104
}
74105

75106
@Test

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/beans/DummyQuery.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import graphql.kickstart.tools.GraphQLQueryResolver;
44
import org.springframework.stereotype.Service;
55

6+
import java.util.Optional;
7+
68
@Service
79
public class DummyQuery implements GraphQLQueryResolver {
810

@@ -14,8 +16,11 @@ public String otherQuery() {
1416
return "TEST";
1517
}
1618

17-
public FooBar fooBar() {
18-
return FooBar.builder().bar("BAR").foo("FOO").build();
19+
public FooBar fooBar(String foo, String bar) {
20+
return FooBar.builder()
21+
.bar(Optional.ofNullable(bar).orElse("BAR"))
22+
.foo(Optional.ofNullable(foo).orElse("FOO"))
23+
.build();
1924
}
2025

2126
public String queryWithVariables(final String input) {

graphql-spring-boot-test/src/test/resources/multiple-queries.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ query testQuery1 {
44

55
query testQuery2 {
66
otherQuery
7+
}
8+
9+
query withVariable($input: String!) {
10+
queryWithVariables(input: $input)
711
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
query {
2-
fooBar {
1+
query($foo: String, $bar: String) {
2+
fooBar(foo: $foo, bar: $bar) {
33
...FooBarFragment
44
}
55
}

graphql-spring-boot-test/src/test/resources/test-schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ type Subscription {
1414
type Query {
1515
dummy: Boolean!
1616
otherQuery: String!
17-
fooBar: FooBar!
17+
fooBar(foo: String, bar: String): FooBar!
1818
queryWithVariables(input: String!): String!
1919
}

0 commit comments

Comments
 (0)