diff --git a/spring-graphql-docs/modules/ROOT/pages/testing.adoc b/spring-graphql-docs/modules/ROOT/pages/testing.adoc index 34f77cb0..25a4b897 100644 --- a/spring-graphql-docs/modules/ROOT/pages/testing.adoc +++ b/spring-graphql-docs/modules/ROOT/pages/testing.adoc @@ -182,7 +182,7 @@ xref:testing.adoc#testing.requests[execute requests] using the same API, indepen === Builder `GraphQlTester` defines a parent `Builder` with common configuration options for the -builders of all extensions. It lets you configure the following: +builders of all extensions. These include builders like the one of the ExecutionGraphQlServiceTester, listed above. It lets you configure at least the following: - `errorFilter` - a predicate to suppress expected errors, so you can inspect the data of the response. @@ -191,8 +191,7 @@ the classpath or from anywhere else. - `responseTimeout` - how long to wait for request execution to complete before timing out. - - +include-code::GraphQlTesterBuilder[tag=inlineDocument,indent=0] [[testing.requests]] == Requests diff --git a/spring-graphql-docs/src/main/java/org/springframework/graphql/docs/testing/graphqltester/builder/GraphQlTesterBuilder.java b/spring-graphql-docs/src/main/java/org/springframework/graphql/docs/testing/graphqltester/builder/GraphQlTesterBuilder.java new file mode 100644 index 00000000..462aee73 --- /dev/null +++ b/spring-graphql-docs/src/main/java/org/springframework/graphql/docs/testing/graphqltester/builder/GraphQlTesterBuilder.java @@ -0,0 +1,46 @@ +/* + * Copyright 2020-present 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.docs.testing.graphqltester.builder; + +import java.time.Duration; +import java.util.function.Predicate; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.graphql.ResponseError; +import org.springframework.graphql.client.GraphQlTransport; +import org.springframework.graphql.support.DocumentSource; +import org.springframework.graphql.support.ResourceDocumentSource; +import org.springframework.graphql.test.tester.GraphQlTester; + +public class GraphQlTesterBuilder { + + void inlineDocument() { + // tag::inlineDocument[] + GraphQlTransport transport = /**/ null; + Predicate errorFilter = /**/ null; + ClassPathResource resource = new ClassPathResource("custom-folder/"); + DocumentSource documentSource = new ResourceDocumentSource(resource); + + GraphQlTester.builder(transport) + .documentSource(documentSource) + .errorFilter(errorFilter) + .responseTimeout(Duration.ofSeconds(5)) + .build(); + // end::inlineDocument[] + } + +} diff --git a/spring-graphql/src/main/java/org/springframework/graphql/support/ResourceDocumentSource.java b/spring-graphql/src/main/java/org/springframework/graphql/support/ResourceDocumentSource.java index 53505c3a..da0a7826 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/support/ResourceDocumentSource.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/support/ResourceDocumentSource.java @@ -34,10 +34,11 @@ import org.springframework.util.FileCopyUtils; /** - * {@link DocumentSource} that looks for a document {@link Resource} under a set - * of locations and trying a number of different file extension. + * {@link DocumentSource} that searches for document {@link Resource}s across + * multiple locations while trying different file extensions. * * @author Rossen Stoyanchev + * @author Marco Schäck * @since 1.0.0 */ public class ResourceDocumentSource implements DocumentSource { @@ -61,6 +62,14 @@ public ResourceDocumentSource() { this(Collections.singletonList(new ClassPathResource("graphql/")), FILE_EXTENSIONS); } + /** + * Constructor with a single location and the default file extensions. + * @param location the resource location + */ + public ResourceDocumentSource(Resource location) { + this(Collections.singletonList(location), FILE_EXTENSIONS); + } + /** * Constructor with given locations and extensions. * @param locations the resource locations diff --git a/spring-graphql/src/test/java/org/springframework/graphql/support/ResourceDocumentSourceTests.java b/spring-graphql/src/test/java/org/springframework/graphql/support/ResourceDocumentSourceTests.java index 6cfe9b47..426fe4ec 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/support/ResourceDocumentSourceTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/support/ResourceDocumentSourceTests.java @@ -19,7 +19,7 @@ import java.time.Duration; import java.util.Collections; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import reactor.test.StepVerifier; @@ -30,33 +30,75 @@ /** * Unit tests for {@link ResourceDocumentSource}. * @author Rossen Stoyanchev + * @author Marco Schäck */ class ResourceDocumentSourceTests { - private ResourceDocumentSource source; + @Nested + class DefaultConstructorTests { + @Test + void getDocument() { + ResourceDocumentSource source = new ResourceDocumentSource(); + String content = source.getDocument("test-document").block(Duration.ofSeconds(5)); + assertThat(content).startsWith("query GetUser($id: ID!)"); + } - @BeforeEach - void setUp() { - this.source = new ResourceDocumentSource( - Collections.singletonList(new ClassPathResource("books/")), - ResourceDocumentSource.FILE_EXTENSIONS); + @Test + void getDocumentNotFound() { + ResourceDocumentSource source = new ResourceDocumentSource(); + StepVerifier.create(source.getDocument("invalid")) + .expectErrorMessage( + "Failed to find document, name='invalid', " + + "under location(s)=[class path resource [graphql/]]") + .verify(Duration.ofSeconds(5)); + } } + @Nested + class SingleResourceConstructorTests { - @Test - void getDocument() { - String content = this.source.getDocument("book-document").block(Duration.ofSeconds(5)); - assertThat(content).startsWith("bookById(id:\"1\""); + @Test + void getDocument() { + ResourceDocumentSource source = new ResourceDocumentSource(new ClassPathResource("books/")); + String content = source.getDocument("book-document").block(Duration.ofSeconds(5)); + assertThat(content).startsWith("bookById(id:\"1\""); + } + + @Test + void getDocumentNotFound() { + ResourceDocumentSource source = new ResourceDocumentSource(new ClassPathResource("books/")); + StepVerifier.create(source.getDocument("invalid")) + .expectErrorMessage( + "Failed to find document, name='invalid', " + + "under location(s)=[class path resource [books/]]") + .verify(Duration.ofSeconds(5)); + } } - @Test - void getDocumentNotFound() { - StepVerifier.create(this.source.getDocument("invalid")) - .expectErrorMessage( - "Failed to find document, name='invalid', " + - "under location(s)=[class path resource [books/]]") - .verify(Duration.ofSeconds(5)); + @Nested + class ListResourceConstructorTests { + + @Test + void getDocument() { + ResourceDocumentSource source = new ResourceDocumentSource( + Collections.singletonList(new ClassPathResource("books/")), + ResourceDocumentSource.FILE_EXTENSIONS); + String content = source.getDocument("book-document").block(Duration.ofSeconds(5)); + assertThat(content).startsWith("bookById(id:\"1\""); + } + + @Test + void getDocumentNotFound() { + ResourceDocumentSource source = new ResourceDocumentSource( + Collections.singletonList(new ClassPathResource("books/")), + ResourceDocumentSource.FILE_EXTENSIONS); + StepVerifier.create(source.getDocument("invalid")) + .expectErrorMessage( + "Failed to find document, name='invalid', " + + "under location(s)=[class path resource [books/]]") + .verify(Duration.ofSeconds(5)); + } } } diff --git a/spring-graphql/src/test/resources/graphql/test-document.graphql b/spring-graphql/src/test/resources/graphql/test-document.graphql new file mode 100644 index 00000000..64224bfe --- /dev/null +++ b/spring-graphql/src/test/resources/graphql/test-document.graphql @@ -0,0 +1,7 @@ +query GetUser($id: ID!) { + user(id: $id) { + id + name + email + } +} \ No newline at end of file