Skip to content

Commit 4d2cde8

Browse files
committed
Polishing contribution
Closes gh-969
1 parent 1318ca4 commit 4d2cde8

File tree

3 files changed

+43
-48
lines changed

3 files changed

+43
-48
lines changed

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ public DefaultRequest variable(String name, @Nullable Object value) {
145145
}
146146

147147
@Override
148-
public DefaultRequest variable(Map<String, Object> values) {
149-
this.variables.putAll(values);
148+
public DefaultRequest variables(Map<String, Object> variables) {
149+
this.variables.putAll(variables);
150150
return this;
151151
}
152152

@@ -159,7 +159,9 @@ public DefaultRequest extension(String name, @Nullable Object value) {
159159
@SuppressWarnings("ConstantConditions")
160160
@Override
161161
public Response execute() {
162-
return DefaultGraphQlTester.this.transport.execute(request()).map((response) -> mapResponse(response, request())).block(DefaultGraphQlTester.this.responseTimeout);
162+
return DefaultGraphQlTester.this.transport.execute(request())
163+
.map((response) -> mapResponse(response, request()))
164+
.block(DefaultGraphQlTester.this.responseTimeout);
163165
}
164166

165167
@Override
@@ -169,15 +171,18 @@ public void executeAndVerify() {
169171

170172
@Override
171173
public Subscription executeSubscription() {
172-
return () -> DefaultGraphQlTester.this.transport.executeSubscription(request()).map((result) -> mapResponse(result, request()));
174+
return () -> DefaultGraphQlTester.this.transport.executeSubscription(request())
175+
.map((result) -> mapResponse(result, request()));
173176
}
174177

175178
private GraphQlRequest request() {
176179
return new DefaultGraphQlRequest(this.document, this.operationName, this.variables, this.extensions);
177180
}
178181

179182
private DefaultResponse mapResponse(GraphQlResponse response, GraphQlRequest request) {
180-
return new DefaultResponse(response, DefaultGraphQlTester.this.errorFilter, assertDecorator(request), DefaultGraphQlTester.this.jsonPathConfig);
183+
return new DefaultResponse(
184+
response, DefaultGraphQlTester.this.errorFilter,
185+
assertDecorator(request), DefaultGraphQlTester.this.jsonPathConfig);
181186
}
182187

183188
private Consumer<Runnable> assertDecorator(GraphQlRequest request) {

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -149,19 +149,19 @@ interface Request<T extends Request<T>> {
149149
* Add a variable.
150150
* @param name the variable name
151151
* @param value the variable value, possibly {@code null} since GraphQL
152-
* supports providing null value vs not providing a value at all.
152+
* supports providing null for a value vs not providing a value at all.
153153
* @return this request spec
154154
*/
155155
T variable(String name, @Nullable Object value);
156156

157157
/**
158-
* Add variables.
159-
* @param values the variables to be set
160-
* the variable of the values, possibly {@code null} since GraphQL
161-
* supports providing null value vs not providing a value at all.
158+
* Add variables from a {@link Map}.
159+
* @param values the variables to add, possibly with {@code null} values
160+
* since GraphQL supports null for a value vs not providing it at all.
162161
* @return this request spec
162+
* @since 1.3.0
163163
*/
164-
T variable(Map<String, Object> values);
164+
T variables(Map<String, Object> values);
165165

166166
/**
167167
* Add a value for a protocol extension.

spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java

+26-36
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.core.ParameterizedTypeReference;
3030
import org.springframework.graphql.ExecutionGraphQlRequest;
3131
import org.springframework.graphql.ExecutionGraphQlService;
32+
import org.springframework.graphql.GraphQlRequest;
3233

3334
import static org.assertj.core.api.Assertions.assertThat;
3435
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -197,13 +198,13 @@ void entityList() {
197198
void nestedPath() {
198199

199200
String document = "{me {name, friends}}";
200-
getGraphQlService().setDataAsJson(document,
201-
"{" +
202-
" \"me\":{" +
203-
" \"name\":\"Luke Skywalker\","
204-
+ " \"friends\":[{\"name\":\"Han Solo\"}, {\"name\":\"Leia Organa\"}]" +
205-
" }" +
206-
"}");
201+
getGraphQlService().setDataAsJson(document, """
202+
{
203+
"me":{
204+
"name":"Luke Skywalker",
205+
"friends":[{"name":"Han Solo"}, {"name":"Leia Organa"}]
206+
}
207+
}""");
207208

208209
graphQlTester().document(document).execute()
209210
.path("me", me -> me
@@ -215,11 +216,12 @@ void nestedPath() {
215216
@Test
216217
void operationNameAndVariables() {
217218

218-
String document = "query HeroNameAndFriends($episode: Episode) {" +
219-
" hero(episode: $episode) {" +
220-
" name"
221-
+ " }" +
222-
"}";
219+
String document = """
220+
query HeroNameAndFriends($episode: Episode) {
221+
hero(episode: $episode) {
222+
name
223+
}
224+
}""";
223225

224226
getGraphQlService().setDataAsJson(document, "{\"hero\": {\"name\":\"R2-D2\"}}");
225227

@@ -242,36 +244,24 @@ void operationNameAndVariables() {
242244
}
243245

244246
@Test
245-
void operationNameAndVariablesAsMap() {
247+
void variablesAsMap() {
246248

247-
String document = "query HeroNameAndFriends($episode: Episode) {" +
248-
" hero(episode: $episode) {" +
249-
" name"
250-
+ " }" +
251-
"}";
249+
String document = """
250+
query HeroNameAndFriends($episode: Episode) {
251+
hero(episode: $episode) {
252+
name
253+
}
254+
}""";
252255

253256
getGraphQlService().setDataAsJson(document, "{\"hero\": {\"name\":\"R2-D2\"}}");
254257

255-
Map<String, Object> variableMap = new LinkedHashMap<>();
256-
257-
variableMap.put("episode", Optional.of("JEDI"));
258-
variableMap.put("foo", Optional.of("bar"));
259-
variableMap.put("keyOnly", Optional.ofNullable(null));
260-
261-
GraphQlTester.Response response = graphQlTester().document(document)
262-
.operationName("HeroNameAndFriends")
263-
.variable(variableMap)
264-
.execute();
258+
Map<String, Object> vars = Map.of(
259+
"episode", Optional.of("JEDI"), "foo", Optional.of("bar"), "keyOnly", Optional.empty());
265260

266-
response.path("hero").entity(MovieCharacter.class).isEqualTo(MovieCharacter.create("R2-D2"));
261+
graphQlTester().document(document).variables(vars).execute();
267262

268-
ExecutionGraphQlRequest request = getGraphQlService().getGraphQlRequest();
269-
assertThat(request.getDocument()).contains(document);
270-
assertThat(request.getOperationName()).isEqualTo("HeroNameAndFriends");
271-
assertThat(request.getVariables()).hasSize(3);
272-
assertThat(request.getVariables()).containsEntry("episode", Optional.of("JEDI"));
273-
assertThat(request.getVariables()).containsEntry("foo", Optional.of("bar"));
274-
assertThat(request.getVariables()).containsEntry("keyOnly", Optional.ofNullable(null));
263+
GraphQlRequest request = getGraphQlService().getGraphQlRequest();
264+
assertThat(request.getVariables()).containsExactlyEntriesOf(vars);
275265
}
276266

277267
@Test

0 commit comments

Comments
 (0)