Simplify setup for annotated controller tests

This commit is contained in:
Rossen Stoyanchev
2021-11-12 21:44:10 +00:00
parent 9a7bab42c9
commit 5c0b32912a
7 changed files with 65 additions and 119 deletions

View File

@@ -25,8 +25,10 @@ import graphql.schema.DataFetcher;
import graphql.schema.GraphQLTypeVisitor;
import graphql.schema.TypeResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer;
import org.springframework.graphql.execution.DataFetcherExceptionResolver;
import org.springframework.graphql.execution.DataLoaderRegistrar;
import org.springframework.graphql.execution.ExecutionGraphQlService;
@@ -82,6 +84,13 @@ public class GraphQlSetup implements GraphQlServiceSetup {
return this;
}
public GraphQlSetup runtimeWiringForAnnotatedControllers(ApplicationContext context) {
AnnotatedControllerConfigurer configurer = new AnnotatedControllerConfigurer();
configurer.setApplicationContext(context);
configurer.afterPropertiesSet();
return runtimeWiring(configurer);
}
public GraphQlSetup exceptionResolver(DataFetcherExceptionResolver... resolvers) {
this.graphQlSourceBuilder.exceptionResolvers(Arrays.asList(resolvers));
return this;

View File

@@ -81,13 +81,13 @@ public class BatchMappingDetectionTests {
}
private RuntimeWiring.Builder initRuntimeWiringBuilder(Class<?> handlerType) {
AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext();
appContext.registerBean(handlerType);
appContext.registerBean(BatchLoaderRegistry.class, () -> this.batchLoaderRegistry);
appContext.refresh();
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.registerBean(handlerType);
context.registerBean(BatchLoaderRegistry.class, () -> this.batchLoaderRegistry);
context.refresh();
AnnotatedControllerConfigurer configurer = new AnnotatedControllerConfigurer();
configurer.setApplicationContext(appContext);
configurer.setApplicationContext(context);
configurer.afterPropertiesSet();
RuntimeWiring.Builder wiringBuilder = RuntimeWiring.newRuntimeWiring();

View File

@@ -29,11 +29,9 @@ import org.junit.jupiter.params.provider.MethodSource;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.graphql.GraphQlResponse;
import org.springframework.graphql.RequestInput;
import org.springframework.graphql.data.method.annotation.BatchMapping;
import org.springframework.graphql.execution.ExecutionGraphQlService;
import org.springframework.stereotype.Controller;
import static org.assertj.core.api.Assertions.assertThat;
@@ -48,19 +46,18 @@ import static org.junit.jupiter.params.provider.Arguments.arguments;
@SuppressWarnings("unused")
public class BatchMappingInvocationTests extends BatchMappingTestSupport {
private static Stream<Arguments> controllerClasses() {
private static Stream<Arguments> controllers() {
return Stream.of(
arguments(named("Returning Mono<Map<K,V>>", BatchMonoMapController.class)),
arguments(named("Returning Map<K,V>", BatchMapController.class)),
arguments(named("Returning Flux<V>", BatchFluxController.class)),
arguments(named("Returning List<V>", BatchListController.class))
arguments(named("Returning Mono<Map<K,V>>", new BatchMonoMapController())),
arguments(named("Returning Map<K,V>", new BatchMapController())),
arguments(named("Returning Flux<V>", new BatchFluxController())),
arguments(named("Returning List<V>", new BatchListController()))
);
}
@ParameterizedTest
@MethodSource("controllerClasses")
void oneToOne(Class<?> controllerClass) {
@MethodSource("controllers")
void oneToOne(CourseController controller) {
String query = "{ " +
" courses { " +
" id" +
@@ -73,7 +70,7 @@ public class BatchMappingInvocationTests extends BatchMappingTestSupport {
" }" +
"}";
Mono<ExecutionResult> resultMono = graphQlService(controllerClass)
Mono<ExecutionResult> resultMono = createGraphQlService(controller)
.execute(new RequestInput(query, null, null, null));
List<Course> actualCourses = GraphQlResponse.from(resultMono).toList("courses", Course.class);
@@ -92,8 +89,8 @@ public class BatchMappingInvocationTests extends BatchMappingTestSupport {
}
@ParameterizedTest
@MethodSource("controllerClasses")
void oneToMany(Class<?> controllerClass) {
@MethodSource("controllers")
void oneToMany(CourseController controller) {
String query = "{ " +
" courses { " +
" id" +
@@ -106,7 +103,7 @@ public class BatchMappingInvocationTests extends BatchMappingTestSupport {
" }" +
"}";
Mono<ExecutionResult> resultMono = graphQlService(controllerClass)
Mono<ExecutionResult> resultMono = createGraphQlService(controller)
.execute(new RequestInput(query, null, null, null));
List<Course> actualCourses = GraphQlResponse.from(resultMono).toList("courses", Course.class);
@@ -129,15 +126,6 @@ public class BatchMappingInvocationTests extends BatchMappingTestSupport {
}
}
private ExecutionGraphQlService graphQlService(Class<?>... configClasses) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(configClasses);
applicationContext.register(BatchMappingTestSupport.CourseConfig.class);
applicationContext.refresh();
return applicationContext.getBean(ExecutionGraphQlService.class);
}
@Controller
private static class BatchMonoMapController extends CourseController {

View File

@@ -17,7 +17,6 @@ package org.springframework.graphql.data.method.annotation.support;
import java.security.Principal;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
@@ -32,12 +31,9 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.context.Context;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.graphql.GraphQlResponse;
import org.springframework.graphql.RequestInput;
import org.springframework.graphql.data.method.annotation.BatchMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.execution.ExecutionGraphQlService;
import org.springframework.graphql.execution.ReactorContextManager;
import org.springframework.graphql.security.SecurityContextThreadLocalAccessor;
import org.springframework.lang.Nullable;
@@ -79,13 +75,13 @@ public class BatchMappingPrincipalMethodArgumentResolverTests extends BatchMappi
@ParameterizedTest
@MethodSource("controllers")
void resolveFromReactiveContext(CourseController courseController) {
void resolveFromReactiveContext(PrincipalCourseController courseController) {
testBatchLoading(courseController, this.reactiveContextWriter);
}
@ParameterizedTest
@MethodSource("controllers")
void resolveFromThreadLocalContext(CourseController courseController) {
void resolveFromThreadLocalContext(PrincipalCourseController courseController) {
SecurityContextHolder.setContext(new SecurityContextImpl(authentication));
try {
testBatchLoading(courseController, this.threadLocalContextWriter);
@@ -95,18 +91,11 @@ public class BatchMappingPrincipalMethodArgumentResolverTests extends BatchMappi
}
}
private void testBatchLoading(CourseController controller, Function<Context, Context> contextWriter) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getBeanFactory().registerSingleton("courseController", controller);
context.register(BatchMappingTestSupport.CourseConfig.class);
context.refresh();
ExecutionGraphQlService graphQlService = context.getBean(ExecutionGraphQlService.class);
private void testBatchLoading(PrincipalCourseController controller, Function<Context, Context> contextWriter) {
Mono<ExecutionResult> resultMono = Mono.delay(Duration.ofMillis(10))
.flatMap(aLong -> {
String query = "{ courses { id instructor { id } } }";
return graphQlService.execute(new RequestInput(query, null, null, null));
return createGraphQlService(controller).execute(new RequestInput(query, null, null, null));
})
.contextWrite(contextWriter);
@@ -122,7 +111,7 @@ public class BatchMappingPrincipalMethodArgumentResolverTests extends BatchMappi
@SuppressWarnings("unused")
private static class CourseController {
private static class PrincipalCourseController extends CourseController {
@Nullable
protected Principal principal;
@@ -136,16 +125,12 @@ public class BatchMappingPrincipalMethodArgumentResolverTests extends BatchMappi
this.principal = principal;
}
@QueryMapping
public Collection<Course> courses() {
return BatchMappingTestSupport.courseMap.values();
}
}
@Controller
@SuppressWarnings("unused")
private static class BatchMonoMapController extends CourseController {
private static class BatchMonoMapController extends PrincipalCourseController {
@BatchMapping
public Mono<Map<Course, Person>> instructor(List<Course> courses, Principal principal) {
@@ -158,7 +143,7 @@ public class BatchMappingPrincipalMethodArgumentResolverTests extends BatchMappi
@Controller
@SuppressWarnings("unused")
private static class BatchMapController extends CourseController {
private static class BatchMapController extends PrincipalCourseController {
@BatchMapping
public Map<Course, Person> instructor(List<Course> courses, Principal principal) {
@@ -170,7 +155,7 @@ public class BatchMappingPrincipalMethodArgumentResolverTests extends BatchMappi
@Controller
@SuppressWarnings("unused")
private static class BatchFluxController extends CourseController {
private static class BatchFluxController extends PrincipalCourseController {
@BatchMapping
public Flux<Person> instructor(List<Course> courses, Principal principal) {
@@ -182,7 +167,7 @@ public class BatchMappingPrincipalMethodArgumentResolverTests extends BatchMappi
@Controller
@SuppressWarnings("unused")
private static class BatchListController extends CourseController {
private static class BatchListController extends PrincipalCourseController {
@BatchMapping
public List<Person> instructor(List<Course> courses, Principal principal) {

View File

@@ -27,7 +27,7 @@ import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.graphql.GraphQlService;
import org.springframework.graphql.GraphQlSetup;
import org.springframework.graphql.data.method.annotation.QueryMapping;
@@ -79,6 +79,20 @@ public class BatchMappingTestSupport {
"}";
protected GraphQlService createGraphQlService(CourseController controller) {
BatchLoaderRegistry registry = new DefaultBatchLoaderRegistry();
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.registerBean(CourseController.class, () -> controller);
context.registerBean(BatchLoaderRegistry.class, () -> registry);
context.refresh();
return GraphQlSetup.schemaContent(schema)
.runtimeWiringForAnnotatedControllers(context)
.dataLoaders(registry)
.toGraphQlService();
}
static class Course {
@@ -206,26 +220,4 @@ public class BatchMappingTestSupport {
}
}
static class CourseConfig {
@Bean
public GraphQlService graphQlService(AnnotatedControllerConfigurer configurer, BatchLoaderRegistry registry) {
return GraphQlSetup.schemaContent(schema)
.runtimeWiring(configurer)
.dataLoaders(registry)
.toGraphQlService();
}
@Bean
public AnnotatedControllerConfigurer annotatedDataFetcherConfigurer() {
return new AnnotatedControllerConfigurer();
}
@Bean
public BatchLoaderRegistry batchLoaderRegistry() {
return new DefaultBatchLoaderRegistry();
}
}
}

View File

@@ -29,8 +29,6 @@ import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.graphql.Author;
import org.springframework.graphql.Book;
import org.springframework.graphql.BookCriteria;
@@ -46,7 +44,6 @@ import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.graphql.data.method.annotation.SubscriptionMapping;
import org.springframework.graphql.execution.BatchLoaderRegistry;
import org.springframework.graphql.execution.DefaultBatchLoaderRegistry;
import org.springframework.graphql.execution.ExecutionGraphQlService;
import org.springframework.stereotype.Controller;
import static org.assertj.core.api.Assertions.assertThat;
@@ -173,41 +170,18 @@ public class SchemaMappingInvocationTests {
}
private ExecutionGraphQlService graphQlService() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(TestConfig.class);
applicationContext.refresh();
private GraphQlService graphQlService() {
BatchLoaderRegistry registry = new DefaultBatchLoaderRegistry();
return applicationContext.getBean(ExecutionGraphQlService.class);
}
@Configuration
static class TestConfig {
@Bean
public BookController bookController() {
return new BookController(batchLoaderRegistry());
}
@Bean
public GraphQlService graphQlService(AnnotatedControllerConfigurer configurer, BatchLoaderRegistry registry) {
return GraphQlSetup.schemaResource(BookSource.schema)
.runtimeWiring(configurer)
.dataLoaders(registry)
.toGraphQlService();
}
@Bean
public AnnotatedControllerConfigurer annotatedControllerConfigurer() {
return new AnnotatedControllerConfigurer();
}
@Bean
public BatchLoaderRegistry batchLoaderRegistry() {
return new DefaultBatchLoaderRegistry();
}
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(BookController.class);
context.registerBean(BatchLoaderRegistry.class, () -> registry);
context.refresh();
return GraphQlSetup.schemaResource(BookSource.schema)
.runtimeWiringForAnnotatedControllers(context)
.dataLoaders(registry)
.toGraphQlService();
}

View File

@@ -30,6 +30,7 @@ import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import reactor.util.context.Context;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.MethodParameter;
import org.springframework.graphql.GraphQlResponse;
@@ -153,16 +154,13 @@ public class SchemaMappingPrincipalMethodArgumentResolverTests {
private Mono<ExecutionResult> executeAsync(
String schema, String op, Function<Context, Context> contextWriter) {
StaticApplicationContext context = new StaticApplicationContext();
context.getBeanFactory().registerSingleton("greetingController", greetingController);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.registerBean(GreetingController.class, () -> greetingController);
context.refresh();
AnnotatedControllerConfigurer configurer = new AnnotatedControllerConfigurer();
configurer.setApplicationContext(context);
configurer.afterPropertiesSet();
ExecutionGraphQlService graphQlService =
GraphQlSetup.schemaContent(schema).runtimeWiring(configurer).toGraphQlService();
ExecutionGraphQlService graphQlService = GraphQlSetup.schemaContent(schema)
.runtimeWiringForAnnotatedControllers(context)
.toGraphQlService();
return Mono.delay(Duration.ofMillis(10))
.flatMap(aLong -> graphQlService.execute(new RequestInput(op, null, null, null)))