Skip to content

Commit d68edb4

Browse files
committed
Polishing annotated DataFetcher tests
See gh-130
1 parent 5a8a99a commit d68edb4

File tree

3 files changed

+50
-44
lines changed

3 files changed

+50
-44
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/AnnotatedDataFetcherConfigurer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void configure(RuntimeWiring.Builder builder) {
122122
findHandlerMethods().forEach((info) -> {
123123
FieldCoordinates coordinates = info.getCoordinates();
124124
HandlerMethod handlerMethod = info.getHandlerMethod();
125-
DataFetcher<?> dataFetcher = new AnnotatedDataFetcher(coordinates, handlerMethod, this.argumentResolvers);
125+
DataFetcher<?> dataFetcher = new SchemaMappingDataFetcher(coordinates, handlerMethod, this.argumentResolvers);
126126
builder.type(coordinates.getTypeName(), typeBuilder ->
127127
typeBuilder.dataFetcher(coordinates.getFieldName(), dataFetcher));
128128
});
@@ -267,7 +267,7 @@ public HandlerMethod getHandlerMethod() {
267267
/**
268268
* {@link DataFetcher} that wrap and invokes a {@link HandlerMethod}.
269269
*/
270-
static class AnnotatedDataFetcher implements DataFetcher<Object> {
270+
static class SchemaMappingDataFetcher implements DataFetcher<Object> {
271271

272272
private final FieldCoordinates coordinates;
273273

@@ -276,7 +276,7 @@ static class AnnotatedDataFetcher implements DataFetcher<Object> {
276276
private final HandlerMethodArgumentResolverComposite argumentResolvers;
277277

278278

279-
public AnnotatedDataFetcher(FieldCoordinates coordinates, HandlerMethod handlerMethod,
279+
public SchemaMappingDataFetcher(FieldCoordinates coordinates, HandlerMethod handlerMethod,
280280
HandlerMethodArgumentResolverComposite resolvers) {
281281

282282
this.coordinates = coordinates;
Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.graphql.data.method.annotation.SchemaMapping;
3333
import org.springframework.graphql.data.method.annotation.SubscriptionMapping;
3434
import org.springframework.stereotype.Controller;
35+
import org.springframework.util.StringUtils;
3536

3637
import static org.assertj.core.api.Assertions.assertThat;
3738

@@ -41,40 +42,42 @@
4142
*
4243
* @author Rossen Stoyanchev
4344
*/
44-
public class AnnotatedDataFetcherDetectionTests {
45+
public class SchemaMappingDetectionTests {
4546

4647
@Test
4748
void registerWithDefaultCoordinates() {
48-
RuntimeWiring.Builder wiringBuilder = initRuntimeWiringBuilder(BookController.class);
4949

50-
Map<String, Map<String, DataFetcher>> map = wiringBuilder.build().getDataFetchers();
50+
Map<String, Map<String, DataFetcher>> map =
51+
initRuntimeWiringBuilder(BookController.class).build().getDataFetchers();
52+
5153
assertThat(map).containsOnlyKeys("Query", "Mutation", "Subscription", "Book");
5254
assertThat(map.get("Query")).containsOnlyKeys("bookById", "bookByIdCustomized");
5355
assertThat(map.get("Mutation")).containsOnlyKeys("saveBook", "saveBookCustomized");
5456
assertThat(map.get("Subscription")).containsOnlyKeys("bookSearch", "bookSearchCustomized");
5557
assertThat(map.get("Book")).containsOnlyKeys("author", "authorCustomized");
5658

57-
checkMappedMethod(map, "Query", "bookById", "bookById");
58-
checkMappedMethod(map, "Mutation", "saveBook", "saveBook");
59-
checkMappedMethod(map, "Subscription", "bookSearch", "bookSearch");
60-
checkMappedMethod(map, "Book", "author", "author");
59+
assertMapping(map, "Query.bookById", "bookById");
60+
assertMapping(map, "Mutation.saveBook", "saveBook");
61+
assertMapping(map, "Subscription.bookSearch", "bookSearch");
62+
assertMapping(map, "Book.author", "author");
6163
}
6264

6365
@Test
6466
void registerWithExplicitCoordinates() {
65-
RuntimeWiring.Builder wiringBuilder = initRuntimeWiringBuilder(BookController.class);
6667

67-
Map<String, Map<String, DataFetcher>> map = wiringBuilder.build().getDataFetchers();
68+
Map<String, Map<String, DataFetcher>> map =
69+
initRuntimeWiringBuilder(BookController.class).build().getDataFetchers();
70+
6871
assertThat(map).containsOnlyKeys("Query", "Mutation", "Subscription", "Book");
6972
assertThat(map.get("Query")).containsOnlyKeys("bookById", "bookByIdCustomized");
7073
assertThat(map.get("Mutation")).containsOnlyKeys("saveBook", "saveBookCustomized");
7174
assertThat(map.get("Subscription")).containsOnlyKeys("bookSearch", "bookSearchCustomized");
7275
assertThat(map.get("Book")).containsOnlyKeys("author", "authorCustomized");
7376

74-
checkMappedMethod(map, "Query", "bookByIdCustomized", "bookByIdWithNonMatchingMethodName");
75-
checkMappedMethod(map, "Mutation", "saveBookCustomized", "saveBookWithNonMatchingMethodName");
76-
checkMappedMethod(map, "Subscription", "bookSearchCustomized", "bookSearchWithNonMatchingMethodName");
77-
checkMappedMethod(map, "Book", "authorCustomized", "authorWithNonMatchingMethodName");
77+
assertMapping(map, "Query.bookByIdCustomized", "bookByIdWithNonMatchingMethodName");
78+
assertMapping(map, "Mutation.saveBookCustomized", "saveBookWithNonMatchingMethodName");
79+
assertMapping(map, "Subscription.bookSearchCustomized", "bookSearchWithNonMatchingMethodName");
80+
assertMapping(map, "Book.authorCustomized", "authorWithNonMatchingMethodName");
7881
}
7982

8083
private RuntimeWiring.Builder initRuntimeWiringBuilder(Class<?> handlerType) {
@@ -92,11 +95,14 @@ private RuntimeWiring.Builder initRuntimeWiringBuilder(Class<?> handlerType) {
9295
}
9396

9497
@SuppressWarnings("rawtypes")
95-
private void checkMappedMethod(
96-
Map<String, Map<String, DataFetcher>> dataFetcherMap, String typeName, String fieldName, String methodName) {
98+
private void assertMapping(Map<String, Map<String, DataFetcher>> map, String coordinates, String methodName) {
99+
100+
String[] strings = StringUtils.tokenizeToStringArray(coordinates, ".");
101+
String typeName = strings[0];
102+
String field = strings[1];
97103

98-
AnnotatedDataFetcherConfigurer.AnnotatedDataFetcher dataFetcher =
99-
(AnnotatedDataFetcherConfigurer.AnnotatedDataFetcher) dataFetcherMap.get(typeName).get(fieldName);
104+
AnnotatedDataFetcherConfigurer.SchemaMappingDataFetcher dataFetcher =
105+
(AnnotatedDataFetcherConfigurer.SchemaMappingDataFetcher) map.get(typeName).get(field);
100106

101107
assertThat(dataFetcher.getHandlerMethod().getMethod().getName()).isEqualTo(methodName);
102108
}
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,17 @@
4848
import org.springframework.graphql.execution.DefaultBatchLoaderRegistry;
4949
import org.springframework.graphql.execution.ExecutionGraphQlService;
5050
import org.springframework.graphql.execution.GraphQlSource;
51+
import org.springframework.lang.Nullable;
5152
import org.springframework.stereotype.Controller;
5253

5354
import static org.assertj.core.api.Assertions.assertThat;
5455

5556
/**
56-
* Tests with invocation of DataFetcher's from annotated methods.
57+
* Test GraphQL requests handled through {@code @SchemaMapping} methods.
58+
*
5759
* @author Rossen Stoyanchev
5860
*/
59-
public class AnnotatedDataFetcherInvocationTests {
61+
public class SchemaMappingInvocationTests {
6062

6163
@Test
6264
void queryWithScalarArgument() {
@@ -71,13 +73,11 @@ void queryWithScalarArgument() {
7173
" }" +
7274
"}";
7375

74-
ExecutionResult result = initGraphQlService(BookController.class)
76+
ExecutionResult result = initGraphQlService()
7577
.execute(new RequestInput(query, null, null))
7678
.block();
7779

78-
assertThat(result.getErrors()).isEmpty();
79-
Map<String, Object> data = result.getData();
80-
assertThat(data).isNotNull();
80+
Map<String, Object> data = getData(result);
8181

8282
Map<String, Object> book = getValue(data, "bookById");
8383
assertThat(book.get("id")).isEqualTo("1");
@@ -97,14 +97,11 @@ void queryWithObjectArgument() {
9797
" }" +
9898
"}";
9999

100-
ExecutionResult result = initGraphQlService(BookController.class)
100+
ExecutionResult result = initGraphQlService()
101101
.execute(new RequestInput(query, null, null))
102102
.block();
103103

104-
assertThat(result.getErrors()).isEmpty();
105-
Map<String, Object> data = result.getData();
106-
assertThat(data).isNotNull();
107-
104+
Map<String, Object> data = getData(result);
108105
List<Map<String, Object>> bookList = getValue(data, "booksByCriteria");
109106
assertThat(bookList).hasSize(2);
110107
assertThat(bookList.get(0).get("name")).isEqualTo("Nineteen Eighty-Four");
@@ -128,13 +125,11 @@ void queryWithArgumentViaDataFetchingEnvironment() {
128125
return executionInput;
129126
});
130127

131-
ExecutionResult result = initGraphQlService(BookController.class)
128+
ExecutionResult result = initGraphQlService()
132129
.execute(requestInput)
133130
.block();
134131

135-
assertThat(result.getErrors()).isEmpty();
136-
Map<String, Object> data = result.getData();
137-
assertThat(data).isNotNull();
132+
Map<String, Object> data = getData(result);
138133

139134
Map<String, Object> author = getValue(data, "authorById");
140135
assertThat(author.get("id")).isEqualTo("101");
@@ -154,13 +149,11 @@ void mutation() {
154149
" }" +
155150
"}";
156151

157-
ExecutionResult result = initGraphQlService(BookController.class)
152+
ExecutionResult result = initGraphQlService()
158153
.execute(new RequestInput(operation, null, null))
159154
.block();
160155

161-
assertThat(result.getErrors()).isEmpty();
162-
Map<String, Object> data = result.getData();
163-
assertThat(data).isNotNull();
156+
Map<String, Object> data = getData(result);
164157

165158
Map<String, Object> author = getValue(data, "addAuthor");
166159
assertThat(author.get("id")).isEqualTo("99");
@@ -177,13 +170,11 @@ void subscription() {
177170
" }" +
178171
"}";
179172

180-
ExecutionResult result = initGraphQlService(BookController.class)
173+
ExecutionResult result = initGraphQlService()
181174
.execute(new RequestInput(operation, null, null))
182175
.block();
183176

184-
assertThat(result.getErrors()).isEmpty();
185-
Publisher<ExecutionResult> publisher = result.getData();
186-
assertThat(publisher).isNotNull();
177+
Publisher<ExecutionResult> publisher = getData(result);
187178

188179
Flux<Map<String, Object>> bookFlux = Flux.from(publisher).map(rs -> {
189180
Map<String, Object> map = rs.getData();
@@ -203,14 +194,22 @@ void subscription() {
203194
}
204195

205196

206-
private ExecutionGraphQlService initGraphQlService(Class<?> beanClass) {
197+
private ExecutionGraphQlService initGraphQlService() {
207198
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
208199
applicationContext.register(TestConfig.class);
209200
applicationContext.refresh();
210201

211202
return applicationContext.getBean(ExecutionGraphQlService.class);
212203
}
213204

205+
private <T> T getData(@Nullable ExecutionResult result) {
206+
assertThat(result).isNotNull();
207+
assertThat(result.getErrors()).isEmpty();
208+
T data = result.getData();
209+
assertThat(data).isNotNull();
210+
return data;
211+
}
212+
214213
@SuppressWarnings("unchecked")
215214
private <T> T getValue(Map<String, Object> data, String key) {
216215
return (T) data.get(key);
@@ -253,6 +252,7 @@ public DefaultBatchLoaderRegistry batchLoaderRegistry() {
253252
}
254253

255254

255+
@SuppressWarnings("unused")
256256
@Controller
257257
private static class BookController {
258258

0 commit comments

Comments
 (0)