Polishing annotated DataFetcher tests

See gh-130
This commit is contained in:
Rossen Stoyanchev
2021-09-23 12:23:17 +01:00
parent 5a8a99aea2
commit d68edb47c9
3 changed files with 50 additions and 44 deletions

View File

@@ -122,7 +122,7 @@ public class AnnotatedDataFetcherConfigurer
findHandlerMethods().forEach((info) -> {
FieldCoordinates coordinates = info.getCoordinates();
HandlerMethod handlerMethod = info.getHandlerMethod();
DataFetcher<?> dataFetcher = new AnnotatedDataFetcher(coordinates, handlerMethod, this.argumentResolvers);
DataFetcher<?> dataFetcher = new SchemaMappingDataFetcher(coordinates, handlerMethod, this.argumentResolvers);
builder.type(coordinates.getTypeName(), typeBuilder ->
typeBuilder.dataFetcher(coordinates.getFieldName(), dataFetcher));
});
@@ -267,7 +267,7 @@ public class AnnotatedDataFetcherConfigurer
/**
* {@link DataFetcher} that wrap and invokes a {@link HandlerMethod}.
*/
static class AnnotatedDataFetcher implements DataFetcher<Object> {
static class SchemaMappingDataFetcher implements DataFetcher<Object> {
private final FieldCoordinates coordinates;
@@ -276,7 +276,7 @@ public class AnnotatedDataFetcherConfigurer
private final HandlerMethodArgumentResolverComposite argumentResolvers;
public AnnotatedDataFetcher(FieldCoordinates coordinates, HandlerMethod handlerMethod,
public SchemaMappingDataFetcher(FieldCoordinates coordinates, HandlerMethod handlerMethod,
HandlerMethodArgumentResolverComposite resolvers) {
this.coordinates = coordinates;

View File

@@ -32,6 +32,7 @@ import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.graphql.data.method.annotation.SubscriptionMapping;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -41,40 +42,42 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Rossen Stoyanchev
*/
public class AnnotatedDataFetcherDetectionTests {
public class SchemaMappingDetectionTests {
@Test
void registerWithDefaultCoordinates() {
RuntimeWiring.Builder wiringBuilder = initRuntimeWiringBuilder(BookController.class);
Map<String, Map<String, DataFetcher>> map = wiringBuilder.build().getDataFetchers();
Map<String, Map<String, DataFetcher>> map =
initRuntimeWiringBuilder(BookController.class).build().getDataFetchers();
assertThat(map).containsOnlyKeys("Query", "Mutation", "Subscription", "Book");
assertThat(map.get("Query")).containsOnlyKeys("bookById", "bookByIdCustomized");
assertThat(map.get("Mutation")).containsOnlyKeys("saveBook", "saveBookCustomized");
assertThat(map.get("Subscription")).containsOnlyKeys("bookSearch", "bookSearchCustomized");
assertThat(map.get("Book")).containsOnlyKeys("author", "authorCustomized");
checkMappedMethod(map, "Query", "bookById", "bookById");
checkMappedMethod(map, "Mutation", "saveBook", "saveBook");
checkMappedMethod(map, "Subscription", "bookSearch", "bookSearch");
checkMappedMethod(map, "Book", "author", "author");
assertMapping(map, "Query.bookById", "bookById");
assertMapping(map, "Mutation.saveBook", "saveBook");
assertMapping(map, "Subscription.bookSearch", "bookSearch");
assertMapping(map, "Book.author", "author");
}
@Test
void registerWithExplicitCoordinates() {
RuntimeWiring.Builder wiringBuilder = initRuntimeWiringBuilder(BookController.class);
Map<String, Map<String, DataFetcher>> map = wiringBuilder.build().getDataFetchers();
Map<String, Map<String, DataFetcher>> map =
initRuntimeWiringBuilder(BookController.class).build().getDataFetchers();
assertThat(map).containsOnlyKeys("Query", "Mutation", "Subscription", "Book");
assertThat(map.get("Query")).containsOnlyKeys("bookById", "bookByIdCustomized");
assertThat(map.get("Mutation")).containsOnlyKeys("saveBook", "saveBookCustomized");
assertThat(map.get("Subscription")).containsOnlyKeys("bookSearch", "bookSearchCustomized");
assertThat(map.get("Book")).containsOnlyKeys("author", "authorCustomized");
checkMappedMethod(map, "Query", "bookByIdCustomized", "bookByIdWithNonMatchingMethodName");
checkMappedMethod(map, "Mutation", "saveBookCustomized", "saveBookWithNonMatchingMethodName");
checkMappedMethod(map, "Subscription", "bookSearchCustomized", "bookSearchWithNonMatchingMethodName");
checkMappedMethod(map, "Book", "authorCustomized", "authorWithNonMatchingMethodName");
assertMapping(map, "Query.bookByIdCustomized", "bookByIdWithNonMatchingMethodName");
assertMapping(map, "Mutation.saveBookCustomized", "saveBookWithNonMatchingMethodName");
assertMapping(map, "Subscription.bookSearchCustomized", "bookSearchWithNonMatchingMethodName");
assertMapping(map, "Book.authorCustomized", "authorWithNonMatchingMethodName");
}
private RuntimeWiring.Builder initRuntimeWiringBuilder(Class<?> handlerType) {
@@ -92,11 +95,14 @@ public class AnnotatedDataFetcherDetectionTests {
}
@SuppressWarnings("rawtypes")
private void checkMappedMethod(
Map<String, Map<String, DataFetcher>> dataFetcherMap, String typeName, String fieldName, String methodName) {
private void assertMapping(Map<String, Map<String, DataFetcher>> map, String coordinates, String methodName) {
AnnotatedDataFetcherConfigurer.AnnotatedDataFetcher dataFetcher =
(AnnotatedDataFetcherConfigurer.AnnotatedDataFetcher) dataFetcherMap.get(typeName).get(fieldName);
String[] strings = StringUtils.tokenizeToStringArray(coordinates, ".");
String typeName = strings[0];
String field = strings[1];
AnnotatedDataFetcherConfigurer.SchemaMappingDataFetcher dataFetcher =
(AnnotatedDataFetcherConfigurer.SchemaMappingDataFetcher) map.get(typeName).get(field);
assertThat(dataFetcher.getHandlerMethod().getMethod().getName()).isEqualTo(methodName);
}

View File

@@ -48,15 +48,17 @@ import org.springframework.graphql.execution.BatchLoaderRegistry;
import org.springframework.graphql.execution.DefaultBatchLoaderRegistry;
import org.springframework.graphql.execution.ExecutionGraphQlService;
import org.springframework.graphql.execution.GraphQlSource;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests with invocation of DataFetcher's from annotated methods.
* Test GraphQL requests handled through {@code @SchemaMapping} methods.
*
* @author Rossen Stoyanchev
*/
public class AnnotatedDataFetcherInvocationTests {
public class SchemaMappingInvocationTests {
@Test
void queryWithScalarArgument() {
@@ -71,13 +73,11 @@ public class AnnotatedDataFetcherInvocationTests {
" }" +
"}";
ExecutionResult result = initGraphQlService(BookController.class)
ExecutionResult result = initGraphQlService()
.execute(new RequestInput(query, null, null))
.block();
assertThat(result.getErrors()).isEmpty();
Map<String, Object> data = result.getData();
assertThat(data).isNotNull();
Map<String, Object> data = getData(result);
Map<String, Object> book = getValue(data, "bookById");
assertThat(book.get("id")).isEqualTo("1");
@@ -97,14 +97,11 @@ public class AnnotatedDataFetcherInvocationTests {
" }" +
"}";
ExecutionResult result = initGraphQlService(BookController.class)
ExecutionResult result = initGraphQlService()
.execute(new RequestInput(query, null, null))
.block();
assertThat(result.getErrors()).isEmpty();
Map<String, Object> data = result.getData();
assertThat(data).isNotNull();
Map<String, Object> data = getData(result);
List<Map<String, Object>> bookList = getValue(data, "booksByCriteria");
assertThat(bookList).hasSize(2);
assertThat(bookList.get(0).get("name")).isEqualTo("Nineteen Eighty-Four");
@@ -128,13 +125,11 @@ public class AnnotatedDataFetcherInvocationTests {
return executionInput;
});
ExecutionResult result = initGraphQlService(BookController.class)
ExecutionResult result = initGraphQlService()
.execute(requestInput)
.block();
assertThat(result.getErrors()).isEmpty();
Map<String, Object> data = result.getData();
assertThat(data).isNotNull();
Map<String, Object> data = getData(result);
Map<String, Object> author = getValue(data, "authorById");
assertThat(author.get("id")).isEqualTo("101");
@@ -154,13 +149,11 @@ public class AnnotatedDataFetcherInvocationTests {
" }" +
"}";
ExecutionResult result = initGraphQlService(BookController.class)
ExecutionResult result = initGraphQlService()
.execute(new RequestInput(operation, null, null))
.block();
assertThat(result.getErrors()).isEmpty();
Map<String, Object> data = result.getData();
assertThat(data).isNotNull();
Map<String, Object> data = getData(result);
Map<String, Object> author = getValue(data, "addAuthor");
assertThat(author.get("id")).isEqualTo("99");
@@ -177,13 +170,11 @@ public class AnnotatedDataFetcherInvocationTests {
" }" +
"}";
ExecutionResult result = initGraphQlService(BookController.class)
ExecutionResult result = initGraphQlService()
.execute(new RequestInput(operation, null, null))
.block();
assertThat(result.getErrors()).isEmpty();
Publisher<ExecutionResult> publisher = result.getData();
assertThat(publisher).isNotNull();
Publisher<ExecutionResult> publisher = getData(result);
Flux<Map<String, Object>> bookFlux = Flux.from(publisher).map(rs -> {
Map<String, Object> map = rs.getData();
@@ -203,7 +194,7 @@ public class AnnotatedDataFetcherInvocationTests {
}
private ExecutionGraphQlService initGraphQlService(Class<?> beanClass) {
private ExecutionGraphQlService initGraphQlService() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(TestConfig.class);
applicationContext.refresh();
@@ -211,6 +202,14 @@ public class AnnotatedDataFetcherInvocationTests {
return applicationContext.getBean(ExecutionGraphQlService.class);
}
private <T> T getData(@Nullable ExecutionResult result) {
assertThat(result).isNotNull();
assertThat(result.getErrors()).isEmpty();
T data = result.getData();
assertThat(data).isNotNull();
return data;
}
@SuppressWarnings("unchecked")
private <T> T getValue(Map<String, Object> data, String key) {
return (T) data.get(key);
@@ -253,6 +252,7 @@ public class AnnotatedDataFetcherInvocationTests {
}
@SuppressWarnings("unused")
@Controller
private static class BookController {