Skip to content

Commit f843709

Browse files
committed
fix(issue-1092): add documentation for graphQlTester & constructor for resourceDocumentSource
Signed-off-by: Marco Schaeck <[email protected]>
1 parent b62c29f commit f843709

File tree

5 files changed

+126
-23
lines changed

5 files changed

+126
-23
lines changed

spring-graphql-docs/modules/ROOT/pages/testing.adoc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ xref:testing.adoc#testing.requests[execute requests] using the same API, indepen
182182
=== Builder
183183

184184
`GraphQlTester` defines a parent `Builder` with common configuration options for the
185-
builders of all extensions. It lets you configure the following:
185+
builders of all extensions. These include builders like the one of the ExecutionGraphQlServiceTester, listed above. It lets you configure at least the following:
186186

187187
- `errorFilter` - a predicate to suppress expected errors, so you can inspect the data
188188
of the response.
@@ -191,8 +191,7 @@ the classpath or from anywhere else.
191191
- `responseTimeout` - how long to wait for request execution to complete before timing
192192
out.
193193

194-
195-
194+
include-code::GraphQlTesterBuilder[tag=inlineDocument,indent=0]
196195

197196
[[testing.requests]]
198197
== Requests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2020-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.graphql.docs.testing.graphqltester.builder;
18+
19+
import java.time.Duration;
20+
import java.util.function.Predicate;
21+
22+
import org.springframework.core.io.ClassPathResource;
23+
import org.springframework.graphql.ResponseError;
24+
import org.springframework.graphql.client.GraphQlTransport;
25+
import org.springframework.graphql.support.DocumentSource;
26+
import org.springframework.graphql.support.ResourceDocumentSource;
27+
import org.springframework.graphql.test.tester.GraphQlTester;
28+
29+
public class GraphQlTesterBuilder {
30+
31+
void inlineDocument() {
32+
// tag::inlineDocument[]
33+
GraphQlTransport transport = /**/ null;
34+
Predicate<ResponseError> errorFilter = /**/ null;
35+
ClassPathResource resource = new ClassPathResource("custom-folder/");
36+
DocumentSource documentSource = new ResourceDocumentSource(resource);
37+
38+
GraphQlTester.builder(transport)
39+
.documentSource(documentSource)
40+
.errorFilter(errorFilter)
41+
.responseTimeout(Duration.ofSeconds(5))
42+
.build();
43+
// end::inlineDocument[]
44+
}
45+
46+
}

spring-graphql/src/main/java/org/springframework/graphql/support/ResourceDocumentSource.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
import org.springframework.util.FileCopyUtils;
3535

3636
/**
37-
* {@link DocumentSource} that looks for a document {@link Resource} under a set
38-
* of locations and trying a number of different file extension.
37+
* {@link DocumentSource} that searches for document {@link Resource}s across
38+
* multiple locations while trying different file extensions.
3939
*
4040
* @author Rossen Stoyanchev
41+
* @author Marco Schäck
4142
* @since 1.0.0
4243
*/
4344
public class ResourceDocumentSource implements DocumentSource {
@@ -61,6 +62,14 @@ public ResourceDocumentSource() {
6162
this(Collections.singletonList(new ClassPathResource("graphql/")), FILE_EXTENSIONS);
6263
}
6364

65+
/**
66+
* Constructor with a single location and the default file extensions.
67+
* @param location the resource location
68+
*/
69+
public ResourceDocumentSource(Resource location) {
70+
this(Collections.singletonList(location), FILE_EXTENSIONS);
71+
}
72+
6473
/**
6574
* Constructor with given locations and extensions.
6675
* @param locations the resource locations

spring-graphql/src/test/java/org/springframework/graphql/support/ResourceDocumentSourceTests.java

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.time.Duration;
2020
import java.util.Collections;
2121

22-
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Nested;
2323
import org.junit.jupiter.api.Test;
2424
import reactor.test.StepVerifier;
2525

@@ -30,33 +30,75 @@
3030
/**
3131
* Unit tests for {@link ResourceDocumentSource}.
3232
* @author Rossen Stoyanchev
33+
* @author Marco Schäck
3334
*/
3435
class ResourceDocumentSourceTests {
3536

36-
private ResourceDocumentSource source;
37+
@Nested
38+
class DefaultConstructorTests {
3739

40+
@Test
41+
void getDocument() {
42+
ResourceDocumentSource source = new ResourceDocumentSource();
43+
String content = source.getDocument("test-document").block(Duration.ofSeconds(5));
44+
assertThat(content).startsWith("query GetUser($id: ID!)");
45+
}
3846

39-
@BeforeEach
40-
void setUp() {
41-
this.source = new ResourceDocumentSource(
42-
Collections.singletonList(new ClassPathResource("books/")),
43-
ResourceDocumentSource.FILE_EXTENSIONS);
47+
@Test
48+
void getDocumentNotFound() {
49+
ResourceDocumentSource source = new ResourceDocumentSource();
50+
StepVerifier.create(source.getDocument("invalid"))
51+
.expectErrorMessage(
52+
"Failed to find document, name='invalid', " +
53+
"under location(s)=[class path resource [graphql/]]")
54+
.verify(Duration.ofSeconds(5));
55+
}
4456
}
4557

58+
@Nested
59+
class SingleResourceConstructorTests {
4660

47-
@Test
48-
void getDocument() {
49-
String content = this.source.getDocument("book-document").block(Duration.ofSeconds(5));
50-
assertThat(content).startsWith("bookById(id:\"1\"");
61+
@Test
62+
void getDocument() {
63+
ResourceDocumentSource source = new ResourceDocumentSource(new ClassPathResource("books/"));
64+
String content = source.getDocument("book-document").block(Duration.ofSeconds(5));
65+
assertThat(content).startsWith("bookById(id:\"1\"");
66+
}
67+
68+
@Test
69+
void getDocumentNotFound() {
70+
ResourceDocumentSource source = new ResourceDocumentSource(new ClassPathResource("books/"));
71+
StepVerifier.create(source.getDocument("invalid"))
72+
.expectErrorMessage(
73+
"Failed to find document, name='invalid', " +
74+
"under location(s)=[class path resource [books/]]")
75+
.verify(Duration.ofSeconds(5));
76+
}
5177
}
5278

53-
@Test
54-
void getDocumentNotFound() {
55-
StepVerifier.create(this.source.getDocument("invalid"))
56-
.expectErrorMessage(
57-
"Failed to find document, name='invalid', " +
58-
"under location(s)=[class path resource [books/]]")
59-
.verify(Duration.ofSeconds(5));
79+
@Nested
80+
class ListResourceConstructorTests {
81+
82+
@Test
83+
void getDocument() {
84+
ResourceDocumentSource source = new ResourceDocumentSource(
85+
Collections.singletonList(new ClassPathResource("books/")),
86+
ResourceDocumentSource.FILE_EXTENSIONS);
87+
String content = source.getDocument("book-document").block(Duration.ofSeconds(5));
88+
assertThat(content).startsWith("bookById(id:\"1\"");
89+
}
90+
91+
@Test
92+
void getDocumentNotFound() {
93+
ResourceDocumentSource source = new ResourceDocumentSource(
94+
Collections.singletonList(new ClassPathResource("books/")),
95+
ResourceDocumentSource.FILE_EXTENSIONS);
96+
StepVerifier.create(source.getDocument("invalid"))
97+
.expectErrorMessage(
98+
"Failed to find document, name='invalid', " +
99+
"under location(s)=[class path resource [books/]]")
100+
.verify(Duration.ofSeconds(5));
101+
}
60102
}
61103

62104
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
query GetUser($id: ID!) {
2+
user(id: $id) {
3+
id
4+
name
5+
email
6+
}
7+
}

0 commit comments

Comments
 (0)