From 095d72094170ba2824fe5c03ab4b5f6f74a0598f Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Wed, 17 May 2023 07:11:41 +0100 Subject: [PATCH] Simplify request execution in tests --- .../support/BatchMappingInvocationTests.java | 9 ++-- ...gPrincipalMethodArgumentResolverTests.java | 4 +- .../support/BatchMappingTestSupport.java | 6 +-- .../support/SchemaMappingInvocationTests.java | 28 +++++------ .../support/SchemaMappingPaginationTests.java | 10 ++-- ...gPrincipalMethodArgumentResolverTests.java | 9 ++-- .../ConnectionFieldTypeVisitorTests.java | 8 ++-- .../graphql/execution/BatchLoadingTests.java | 10 ++-- .../execution/ClassNameTypeResolverTests.java | 9 ++-- ...nnectionTypeDefinitionConfigurerTests.java | 3 +- ...raphQlObservationInstrumentationTests.java | 24 +++++----- .../graphql/GraphQlServiceSetup.java | 4 +- .../springframework/graphql/GraphQlSetup.java | 4 +- .../graphql/TestExecutionGraphQlService.java | 48 ++++++++++++++++++- 14 files changed, 105 insertions(+), 71 deletions(-) diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/BatchMappingInvocationTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/BatchMappingInvocationTests.java index 971ebe26..2fbaa344 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/BatchMappingInvocationTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/BatchMappingInvocationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,7 +31,6 @@ import reactor.core.publisher.Mono; import org.springframework.graphql.ExecutionGraphQlResponse; import org.springframework.graphql.ResponseHelper; -import org.springframework.graphql.TestExecutionRequest; import org.springframework.graphql.data.method.annotation.BatchMapping; import org.springframework.stereotype.Controller; @@ -72,8 +71,7 @@ public class BatchMappingInvocationTests extends BatchMappingTestSupport { " }" + "}"; - Mono responseMono = createGraphQlService(controller) - .execute(TestExecutionRequest.forDocument(query)); + Mono responseMono = createGraphQlService(controller).execute(query); List actualCourses = ResponseHelper.forResponse(responseMono).toList("courses", Course.class); List courses = Course.allCourses(); @@ -105,8 +103,7 @@ public class BatchMappingInvocationTests extends BatchMappingTestSupport { " }" + "}"; - Mono responseMono = createGraphQlService(controller) - .execute(TestExecutionRequest.forDocument(document)); + Mono responseMono = createGraphQlService(controller).execute(document); List actualCourses = ResponseHelper.forResponse(responseMono).toList("courses", Course.class); List courses = Course.allCourses(); diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/BatchMappingPrincipalMethodArgumentResolverTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/BatchMappingPrincipalMethodArgumentResolverTests.java index 76371721..fa6dd7c2 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/BatchMappingPrincipalMethodArgumentResolverTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/BatchMappingPrincipalMethodArgumentResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -94,7 +94,7 @@ public class BatchMappingPrincipalMethodArgumentResolverTests extends BatchMappi Mono responseMono = Mono.delay(Duration.ofMillis(10)) .flatMap(aLong -> { String document = "{ courses { id instructor { id } } }"; - return createGraphQlService(controller).execute(TestExecutionRequest.forDocument(document)); + return createGraphQlService(controller).execute(document); }) .contextWrite(contextWriter); diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/BatchMappingTestSupport.java b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/BatchMappingTestSupport.java index 790e66f1..9506ad3a 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/BatchMappingTestSupport.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/BatchMappingTestSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,8 +29,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.core.task.SimpleAsyncTaskExecutor; -import org.springframework.graphql.ExecutionGraphQlService; import org.springframework.graphql.GraphQlSetup; +import org.springframework.graphql.TestExecutionGraphQlService; import org.springframework.graphql.data.method.annotation.QueryMapping; import org.springframework.graphql.execution.BatchLoaderRegistry; import org.springframework.graphql.execution.DefaultBatchLoaderRegistry; @@ -80,7 +80,7 @@ public class BatchMappingTestSupport { "}"; - protected ExecutionGraphQlService createGraphQlService(CourseController controller) { + protected TestExecutionGraphQlService createGraphQlService(CourseController controller) { BatchLoaderRegistry registry = new DefaultBatchLoaderRegistry(); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingInvocationTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingInvocationTests.java index 22e4c5a6..8ec4b3a9 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingInvocationTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingInvocationTests.java @@ -40,9 +40,9 @@ import org.springframework.graphql.BookCriteria; import org.springframework.graphql.BookSource; import org.springframework.graphql.ExecutionGraphQlRequest; import org.springframework.graphql.ExecutionGraphQlResponse; -import org.springframework.graphql.ExecutionGraphQlService; import org.springframework.graphql.GraphQlSetup; import org.springframework.graphql.ResponseHelper; +import org.springframework.graphql.TestExecutionGraphQlService; import org.springframework.graphql.TestExecutionRequest; import org.springframework.graphql.data.method.annotation.Argument; import org.springframework.graphql.data.method.annotation.GraphQlExceptionHandler; @@ -80,7 +80,7 @@ public class SchemaMappingInvocationTests { " }" + "}"; - Mono responseMono = graphQlService().execute(TestExecutionRequest.forDocument(document)); + Mono responseMono = graphQlService().execute(document); Book book = ResponseHelper.forResponse(responseMono).toEntity("bookById", Book.class); assertThat(book.getId()).isEqualTo(1); @@ -100,7 +100,7 @@ public class SchemaMappingInvocationTests { " }" + "}"; - Mono responseMono = graphQlService().execute(TestExecutionRequest.forDocument(document)); + Mono responseMono = graphQlService().execute(document); List bookList = ResponseHelper.forResponse(responseMono).toList("booksByCriteria", Book.class); assertThat(bookList).hasSize(2); @@ -117,7 +117,7 @@ public class SchemaMappingInvocationTests { " }" + "}"; - Mono responseMono = graphQlService().execute(TestExecutionRequest.forDocument(document)); + Mono responseMono = graphQlService().execute(document); List bookList = ResponseHelper.forResponse(responseMono).toList("booksByProjectedArguments", Book.class); assertThat(bookList).hasSize(2); @@ -134,7 +134,7 @@ public class SchemaMappingInvocationTests { " }" + "}"; - Mono responseMono = graphQlService().execute(TestExecutionRequest.forDocument(document)); + Mono responseMono = graphQlService().execute(document); List bookList = ResponseHelper.forResponse(responseMono).toList("booksByProjectedCriteria", Book.class); assertThat(bookList).hasSize(2); @@ -179,7 +179,7 @@ public class SchemaMappingInvocationTests { " }" + "}"; - Mono responseMono = graphQlService().execute(TestExecutionRequest.forDocument(document)); + Mono responseMono = graphQlService().execute(document); Author author = ResponseHelper.forResponse(responseMono).toEntity("addAuthor", Author.class); assertThat(author.getId()).isEqualTo(99); @@ -196,7 +196,7 @@ public class SchemaMappingInvocationTests { " }" + "}"; - Mono responseMono = graphQlService().execute(TestExecutionRequest.forDocument(document)); + Mono responseMono = graphQlService().execute(document); Flux bookFlux = ResponseHelper.forSubscription(responseMono) .map(response -> response.toEntity("bookSearch", Book.class)); @@ -222,8 +222,7 @@ public class SchemaMappingInvocationTests { " }" + "}"; - Mono responseMono = - graphQlService().execute(TestExecutionRequest.forDocument(document)); + Mono responseMono = graphQlService().execute(document); ResponseHelper responseHelper = ResponseHelper.forResponse(responseMono); assertThat(responseHelper.errorCount()).isEqualTo(1); @@ -246,12 +245,12 @@ public class SchemaMappingInvocationTests { .message("Rejected: " + ex.getMessage()) .build())); - ExecutionGraphQlService service = graphQlService((configurer, setup) -> { + TestExecutionGraphQlService service = graphQlService((configurer, setup) -> { setup.exceptionResolver(configurer.getExceptionResolver()); // First @ControllerAdvice (no match) setup.exceptionResolver(resolver); // Then resolver }); - Mono responseMono = service.execute(TestExecutionRequest.forDocument(document)); + Mono responseMono = service.execute(document); ResponseHelper responseHelper = ResponseHelper.forResponse(responseMono); assertThat(responseHelper.errorCount()).isEqualTo(1); @@ -268,8 +267,7 @@ public class SchemaMappingInvocationTests { " }" + "}"; - Mono responseMono = - graphQlService().execute(TestExecutionRequest.forDocument(document)); + Mono responseMono = graphQlService().execute(document); Flux bookFlux = ResponseHelper.forSubscription(responseMono) .map(response -> response.toEntity("bookSearch", Book.class)); @@ -286,11 +284,11 @@ public class SchemaMappingInvocationTests { } - private ExecutionGraphQlService graphQlService() { + private TestExecutionGraphQlService graphQlService() { return graphQlService((configurer, setup) -> {}); } - private ExecutionGraphQlService graphQlService(BiConsumer consumer) { + private TestExecutionGraphQlService graphQlService(BiConsumer consumer) { BatchLoaderRegistry registry = new DefaultBatchLoaderRegistry(); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingPaginationTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingPaginationTests.java index 305e94db..66631abc 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingPaginationTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingPaginationTests.java @@ -27,10 +27,9 @@ import org.springframework.data.domain.Window; import org.springframework.graphql.Book; import org.springframework.graphql.BookSource; import org.springframework.graphql.ExecutionGraphQlResponse; -import org.springframework.graphql.ExecutionGraphQlService; import org.springframework.graphql.GraphQlSetup; import org.springframework.graphql.ResponseHelper; -import org.springframework.graphql.TestExecutionRequest; +import org.springframework.graphql.TestExecutionGraphQlService; import org.springframework.graphql.data.method.annotation.QueryMapping; import org.springframework.graphql.data.pagination.CursorStrategy; import org.springframework.graphql.data.query.ScrollPositionCursorStrategy; @@ -50,9 +49,8 @@ public class SchemaMappingPaginationTests { @Test void forwardPagination() { - String query = BookSource.booksConnectionQuery("first:2, after:\"O_3\""); - - Mono response = graphQlService().execute(TestExecutionRequest.forDocument(query)); + String document = BookSource.booksConnectionQuery("first:2, after:\"O_3\""); + Mono response = graphQlService().execute(document); ResponseHelper.forResponse(response).assertData( "{\"books\":{" + @@ -68,7 +66,7 @@ public class SchemaMappingPaginationTests { "}}}"); } - private ExecutionGraphQlService graphQlService() { + private TestExecutionGraphQlService graphQlService() { ScrollPositionCursorStrategy cursorStrategy = new ScrollPositionCursorStrategy(); diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingPrincipalMethodArgumentResolverTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingPrincipalMethodArgumentResolverTests.java index 42a559b2..b0a00d42 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingPrincipalMethodArgumentResolverTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingPrincipalMethodArgumentResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,10 +33,9 @@ import reactor.util.context.Context; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.core.MethodParameter; import org.springframework.graphql.ExecutionGraphQlResponse; -import org.springframework.graphql.ExecutionGraphQlService; import org.springframework.graphql.GraphQlSetup; import org.springframework.graphql.ResponseHelper; -import org.springframework.graphql.TestExecutionRequest; +import org.springframework.graphql.TestExecutionGraphQlService; import org.springframework.graphql.data.method.annotation.QueryMapping; import org.springframework.graphql.data.method.annotation.SubscriptionMapping; import org.springframework.lang.Nullable; @@ -156,12 +155,12 @@ public class SchemaMappingPrincipalMethodArgumentResolverTests { context.registerBean(GreetingController.class, () -> greetingController); context.refresh(); - ExecutionGraphQlService graphQlService = GraphQlSetup.schemaContent(schema) + TestExecutionGraphQlService graphQlService = GraphQlSetup.schemaContent(schema) .runtimeWiringForAnnotatedControllers(context) .toGraphQlService(); return Mono.delay(Duration.ofMillis(10)) - .flatMap(aLong -> graphQlService.execute(TestExecutionRequest.forDocument(document))) + .flatMap(aLong -> graphQlService.execute(document)) .contextWrite(contextWriter); } diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/pagination/ConnectionFieldTypeVisitorTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/pagination/ConnectionFieldTypeVisitorTests.java index 7d38753d..2e0fc71a 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/pagination/ConnectionFieldTypeVisitorTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/pagination/ConnectionFieldTypeVisitorTests.java @@ -27,8 +27,6 @@ import org.springframework.graphql.BookSource; import org.springframework.graphql.ExecutionGraphQlResponse; import org.springframework.graphql.GraphQlSetup; import org.springframework.graphql.ResponseHelper; -import org.springframework.graphql.TestExecutionRequest; -import org.springframework.graphql.execution.ConnectionTypeDefinitionConfigurer; /** * Unit tests for {@link ConnectionFieldTypeVisitor}. @@ -49,7 +47,7 @@ public class ConnectionFieldTypeVisitorTests { .dataFetcher("Query", "books", env -> BookSource.books()) .connectionSupport(adapter) .toGraphQlService() - .execute(TestExecutionRequest.forDocument(BookSource.booksConnectionQuery(null))); + .execute(BookSource.booksConnectionQuery(null)); ResponseHelper.forResponse(response).assertData( "{\"books\":{" + @@ -78,7 +76,7 @@ public class ConnectionFieldTypeVisitorTests { .dataFetcher("Query", "books", new PropertyDataFetcher<>("books")) .connectionSupport(new ListConnectionAdapter()) .toGraphQlService() - .execute(TestExecutionRequest.forDocument(BookSource.booksConnectionQuery(null))); + .execute(BookSource.booksConnectionQuery(null)); ResponseHelper.forResponse(response).assertData("{\"books\":null}"); } @@ -90,7 +88,7 @@ public class ConnectionFieldTypeVisitorTests { .dataFetcher("Query", "books", environment -> null) .connectionSupport(new ListConnectionAdapter()) .toGraphQlService() - .execute(TestExecutionRequest.forDocument(BookSource.booksConnectionQuery(null))); + .execute(BookSource.booksConnectionQuery(null)); ResponseHelper.forResponse(response).assertData( "{\"books\":{" + diff --git a/spring-graphql/src/test/java/org/springframework/graphql/execution/BatchLoadingTests.java b/spring-graphql/src/test/java/org/springframework/graphql/execution/BatchLoadingTests.java index 2c75bf93..fd0f6b9e 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/execution/BatchLoadingTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/execution/BatchLoadingTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,10 +28,10 @@ import org.springframework.graphql.Author; import org.springframework.graphql.Book; import org.springframework.graphql.BookSource; import org.springframework.graphql.ExecutionGraphQlResponse; -import org.springframework.graphql.ResponseHelper; import org.springframework.graphql.ExecutionGraphQlService; import org.springframework.graphql.GraphQlSetup; -import org.springframework.graphql.TestExecutionRequest; +import org.springframework.graphql.ResponseHelper; +import org.springframework.graphql.TestExecutionGraphQlService; import static org.assertj.core.api.Assertions.assertThat; @@ -60,7 +60,7 @@ public class BatchLoadingTests { this.registry.forTypePair(Long.class, Author.class) .registerBatchLoader((ids, env) -> Flux.fromIterable(ids).map(BookSource::getAuthor)); - ExecutionGraphQlService service = GraphQlSetup.schemaResource(BookSource.schema) + TestExecutionGraphQlService service = GraphQlSetup.schemaResource(BookSource.schema) .queryFetcher("booksByCriteria", env -> { Map criteria = env.getArgument("criteria"); String authorName = (String) criteria.get("author"); @@ -76,7 +76,7 @@ public class BatchLoadingTests { .dataLoaders(this.registry) .toGraphQlService(); - Mono responseMono = service.execute(TestExecutionRequest.forDocument(document)); + Mono responseMono = service.execute(document); List books = ResponseHelper.forResponse(responseMono).toList("booksByCriteria", Book.class); assertThat(books).hasSize(2); diff --git a/spring-graphql/src/test/java/org/springframework/graphql/execution/ClassNameTypeResolverTests.java b/spring-graphql/src/test/java/org/springframework/graphql/execution/ClassNameTypeResolverTests.java index 7c3c2e38..c2d2f5b8 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/execution/ClassNameTypeResolverTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/execution/ClassNameTypeResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,9 +23,8 @@ import org.junit.jupiter.api.Test; import reactor.core.publisher.Mono; import org.springframework.graphql.ExecutionGraphQlResponse; -import org.springframework.graphql.ResponseHelper; import org.springframework.graphql.GraphQlSetup; -import org.springframework.graphql.TestExecutionRequest; +import org.springframework.graphql.ResponseHelper; import static org.assertj.core.api.Assertions.assertThat; @@ -85,7 +84,7 @@ public class ClassNameTypeResolverTests { Mono responseMono = graphQlSetup.queryFetcher("animals", env -> animalList) .toGraphQlService() - .execute(TestExecutionRequest.forDocument(document)); + .execute(document); ResponseHelper response = ResponseHelper.forResponse(responseMono); for (int i = 0; i < animalList.size(); i++) { @@ -128,7 +127,7 @@ public class ClassNameTypeResolverTests { Mono responseMono = graphQlSetup.queryFetcher("sightings", env -> animalAndPlantList) .typeResolver(typeResolver) .toGraphQlService() - .execute(TestExecutionRequest.forDocument(document)); + .execute(document); ResponseHelper response = ResponseHelper.forResponse(responseMono); for (int i = 0; i < animalAndPlantList.size(); i++) { diff --git a/spring-graphql/src/test/java/org/springframework/graphql/execution/ConnectionTypeDefinitionConfigurerTests.java b/spring-graphql/src/test/java/org/springframework/graphql/execution/ConnectionTypeDefinitionConfigurerTests.java index a6d053b2..5287e278 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/execution/ConnectionTypeDefinitionConfigurerTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/execution/ConnectionTypeDefinitionConfigurerTests.java @@ -34,7 +34,6 @@ import org.springframework.graphql.BookSource; import org.springframework.graphql.ExecutionGraphQlResponse; import org.springframework.graphql.GraphQlSetup; import org.springframework.graphql.ResponseHelper; -import org.springframework.graphql.TestExecutionRequest; /** * Unit tests for {@link ConnectionTypeDefinitionConfigurer}. @@ -57,7 +56,7 @@ public class ConnectionTypeDefinitionConfigurerTests { Mono response = initGraphQlSetup() .dataFetcher("Query", "books", dataFetcher) .toGraphQlService() - .execute(TestExecutionRequest.forDocument(document)); + .execute(document); ResponseHelper.forResponse(response).assertData( "{\"books\":{" + diff --git a/spring-graphql/src/test/java/org/springframework/graphql/observation/GraphQlObservationInstrumentationTests.java b/spring-graphql/src/test/java/org/springframework/graphql/observation/GraphQlObservationInstrumentationTests.java index c02fbe76..b664ffc9 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/observation/GraphQlObservationInstrumentationTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/observation/GraphQlObservationInstrumentationTests.java @@ -66,7 +66,7 @@ class GraphQlObservationInstrumentationTests { Mono responseMono = graphQlSetup .queryFetcher("bookById", dataFetcher) .toGraphQlService() - .execute(TestExecutionRequest.forDocument(document)); + .execute(document); ResponseHelper response = ResponseHelper.forResponse(responseMono); String name = response.rawValue("bookById.name"); @@ -101,7 +101,7 @@ class GraphQlObservationInstrumentationTests { Mono responseMono = graphQlSetup .queryFetcher("bookById", env -> BookSource.getBookWithoutAuthor(1L)) .toGraphQlService() - .execute(TestExecutionRequest.forDocument(document)); + .execute(document); ResponseHelper response = ResponseHelper.forResponse(responseMono); TestObservationRegistryAssert.assertThat(this.observationRegistry).hasObservationWithNameEqualTo("graphql.request") .that().hasLowCardinalityKeyValue("graphql.outcome", "REQUEST_ERROR") @@ -127,7 +127,7 @@ class GraphQlObservationInstrumentationTests { .queryFetcher("bookById", env -> BookSource.getBookWithoutAuthor(1L)) .dataFetcher("Book", "author", env -> BookSource.getAuthor(101L)) .toGraphQlService() - .execute(TestExecutionRequest.forDocument(document)); + .execute(document); ResponseHelper response = ResponseHelper.forResponse(responseMono); TestObservationRegistryAssert.assertThat(this.observationRegistry).hasObservationWithNameEqualTo("graphql.request") .that().hasLowCardinalityKeyValue("graphql.outcome", "SUCCESS") @@ -169,7 +169,7 @@ class GraphQlObservationInstrumentationTests { .queryFetcher("bookById", env -> CompletableFuture.failedStage(new IllegalStateException("book fetching failure"))) .toGraphQlService() - .execute(TestExecutionRequest.forDocument(document)); + .execute(document); ResponseHelper response = ResponseHelper.forResponse(responseMono); assertThat(response.error(0).message()).isEqualTo("Resolved error: book fetching failure"); @@ -207,15 +207,15 @@ class GraphQlObservationInstrumentationTests { } } """; - ExecutionGraphQlRequest graphQlRequest = TestExecutionRequest.forDocument(document); + ExecutionGraphQlRequest request = TestExecutionRequest.forDocument(document); Observation incoming = Observation.start("incoming", ObservationRegistry.create()); - graphQlRequest.configureExecutionInput((input, builder) -> + request.configureExecutionInput((input, builder) -> builder.graphQLContext(contextBuilder -> contextBuilder.of(ObservationThreadLocalAccessor.KEY, incoming)).build()); Mono responseMono = graphQlSetup .queryFetcher("bookById", env -> BookSource.getBookWithoutAuthor(1L)) .toGraphQlService() - .execute(graphQlRequest); - ResponseHelper response = ResponseHelper.forResponse(responseMono); + .execute(request); + ResponseHelper.forResponse(responseMono); TestObservationRegistryAssert.assertThat(this.observationRegistry).hasObservationWithNameEqualTo("graphql.request") .that().hasParentObservationEqualTo(incoming); @@ -235,8 +235,8 @@ class GraphQlObservationInstrumentationTests { Mono responseMono = graphQlSetup .queryFetcher("bookById", dataFetcher) .toGraphQlService() - .execute(TestExecutionRequest.forDocument(document)); - ResponseHelper response = ResponseHelper.forResponse(responseMono); + .execute(document); + ResponseHelper.forResponse(responseMono); TestObservationRegistryAssert.assertThat(this.observationRegistry).hasObservationWithNameEqualTo("graphql.request") .that().hasLowCardinalityKeyValue("graphql.outcome", "SUCCESS") @@ -275,8 +275,8 @@ class GraphQlObservationInstrumentationTests { .queryFetcher("bookById", bookDataFetcher) .dataFetcher("Book", "author", authorDataFetcher) .toGraphQlService() - .execute(TestExecutionRequest.forDocument(document)); - ResponseHelper response = ResponseHelper.forResponse(responseMono); + .execute(document); + ResponseHelper.forResponse(responseMono); } } diff --git a/spring-graphql/src/testFixtures/java/org/springframework/graphql/GraphQlServiceSetup.java b/spring-graphql/src/testFixtures/java/org/springframework/graphql/GraphQlServiceSetup.java index 41532f81..6081d94e 100644 --- a/spring-graphql/src/testFixtures/java/org/springframework/graphql/GraphQlServiceSetup.java +++ b/spring-graphql/src/testFixtures/java/org/springframework/graphql/GraphQlServiceSetup.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,6 @@ public interface GraphQlServiceSetup extends WebGraphQlSetup { GraphQlServiceSetup dataLoaders(DataLoaderRegistrar... registrars); - ExecutionGraphQlService toGraphQlService(); + TestExecutionGraphQlService toGraphQlService(); } diff --git a/spring-graphql/src/testFixtures/java/org/springframework/graphql/GraphQlSetup.java b/spring-graphql/src/testFixtures/java/org/springframework/graphql/GraphQlSetup.java index 06ca4ba1..d10cb3cc 100644 --- a/spring-graphql/src/testFixtures/java/org/springframework/graphql/GraphQlSetup.java +++ b/spring-graphql/src/testFixtures/java/org/springframework/graphql/GraphQlSetup.java @@ -154,11 +154,11 @@ public class GraphQlSetup implements GraphQlServiceSetup { return this; } - public ExecutionGraphQlService toGraphQlService() { + public TestExecutionGraphQlService toGraphQlService() { GraphQlSource source = graphQlSourceBuilder.build(); DefaultExecutionGraphQlService service = new DefaultExecutionGraphQlService(source); this.dataLoaderRegistrars.forEach(service::addDataLoaderRegistrar); - return service; + return new TestExecutionGraphQlService(service); } diff --git a/spring-graphql/src/testFixtures/java/org/springframework/graphql/TestExecutionGraphQlService.java b/spring-graphql/src/testFixtures/java/org/springframework/graphql/TestExecutionGraphQlService.java index 6b0c80a4..cad66b78 100644 --- a/spring-graphql/src/testFixtures/java/org/springframework/graphql/TestExecutionGraphQlService.java +++ b/spring-graphql/src/testFixtures/java/org/springframework/graphql/TestExecutionGraphQlService.java @@ -1,4 +1,50 @@ +/* + * Copyright 2002-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.graphql; -public interface TestExecutionGraphQlService { +import reactor.core.publisher.Mono; + +/** + * Wrap an {@link ExecutionGraphQlService} to expose an addition convenience + * method that takes a String document, and essentially hides the call to + * {@link TestExecutionRequest#forDocument(String)}. + */ +public class TestExecutionGraphQlService implements ExecutionGraphQlService { + + private final ExecutionGraphQlService delegate; + + + public TestExecutionGraphQlService(ExecutionGraphQlService delegate) { + this.delegate = delegate; + } + + + public ExecutionGraphQlService getDelegate() { + return this.delegate; + } + + public Mono execute(String document) { + ExecutionGraphQlRequest request = TestExecutionRequest.forDocument(document); + return execute(request); + } + + @Override + public Mono execute(ExecutionGraphQlRequest request) { + return this.delegate.execute(request); + } + }