Refine documentSource locations

Closes gh-338
This commit is contained in:
rstoyanchev
2022-04-01 21:39:21 +01:00
parent 9289f0a1b5
commit 696f2cd02f
11 changed files with 68 additions and 18 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.graphql.test.tester;
import java.time.Duration;
import java.util.Collections;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
@@ -27,6 +28,7 @@ import com.jayway.jsonpath.spi.mapper.MappingProvider;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.io.ClassPathResource;
import org.springframework.graphql.GraphQlRequest;
import org.springframework.graphql.GraphQlResponse;
import org.springframework.graphql.ResponseError;
@@ -64,13 +66,24 @@ public abstract class AbstractGraphQlTesterBuilder<B extends AbstractGraphQlTest
@Nullable
private Predicate<ResponseError> errorFilter;
private DocumentSource documentSource = new CachingDocumentSource(new ResourceDocumentSource());
private DocumentSource documentSource;
private Configuration jsonPathConfig = Configuration.builder().build();
private Duration responseTimeout = DEFAULT_RESPONSE_DURATION;
public AbstractGraphQlTesterBuilder() {
this.documentSource = initDocumentSource();
}
private static DocumentSource initDocumentSource() {
return new ResourceDocumentSource(
Collections.singletonList(new ClassPathResource("graphql-test/")),
ResourceDocumentSource.FILE_EXTENSIONS);
}
@Override
public B errorFilter(Predicate<ResponseError> predicate) {
this.errorFilter = (this.errorFilter != null ? errorFilter.and(predicate) : predicate);

View File

@@ -64,8 +64,8 @@ public interface GraphQlTester {
/**
* Variant of {@link #document(String)} that uses the given key to resolve
* the GraphQL document from a file, or in another way with the help of the
* {@link DocumentSource} that the client is configured with.
* the GraphQL document from a file with the help of the configured
* {@link Builder#documentSource(DocumentSource) DocumentSource}.
* @return spec for response assertions
* @throws IllegalArgumentException if the documentName cannot be resolved
* @throws AssertionError if the response status is not 200 (OK)
@@ -108,7 +108,9 @@ public interface GraphQlTester {
/**
* Configure a {@link DocumentSource} for use with
* {@link #documentName(String)} for resolving a document by name.
* <p>By default, {@link ResourceDocumentSource} is used.
* <p>By default, this is set to {@link ResourceDocumentSource} with
* classpath location {@code "graphql-test/"} and
* {@link ResourceDocumentSource#FILE_EXTENSIONS} as extensions.
*/
B documentSource(DocumentSource contentLoader);

View File

@@ -35,6 +35,14 @@ public class GraphQlTesterBuilderTests extends GraphQlTesterTestSupport {
private static final String DOCUMENT = "{ Query }";
@Test
void defaultDocumentSource() {
String document = "{ greeting }";
getGraphQlService().setDataAsJson(document, "{}");
graphQlTester().documentName("greeting").execute();
assertThat(getActualRequestDocument()).isEqualTo(document);
}
@Test
void mutateDocumentSource() {

View File

@@ -0,0 +1 @@
{ greeting }