Skip to content

Commit 3ebdce0

Browse files
committed
Pagination test refactoring
1 parent 911b782 commit 3ebdce0

File tree

6 files changed

+67
-118
lines changed

6 files changed

+67
-118
lines changed

spring-graphql/src/test/java/org/springframework/graphql/BookSource.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@
2222
import java.util.function.Function;
2323
import java.util.stream.Collectors;
2424

25+
import jakarta.annotation.Nullable;
2526
import reactor.core.publisher.Flux;
2627

2728
import org.springframework.core.io.ClassPathResource;
2829
import org.springframework.core.io.Resource;
30+
import org.springframework.util.StringUtils;
2931

3032
public class BookSource {
3133

3234
public static final Resource schema = new ClassPathResource("books/schema.graphqls");
3335

36+
public static final Resource paginationSchema = new ClassPathResource("books/pagination-schema.graphqls");
37+
3438

3539
private static final Map<Long, Book> booksMap = new HashMap<>();
3640

@@ -88,4 +92,25 @@ public static Author getAuthor(Long id) {
8892
return authorsMap.get(id);
8993
}
9094

95+
public static String booksConnectionQuery(@Nullable String arguments) {
96+
arguments = StringUtils.hasText(arguments) ? "(" + arguments + ")" : "";
97+
return "{" +
98+
" books" + arguments + " {" +
99+
" edges {" +
100+
" cursor," +
101+
" node {" +
102+
" id" +
103+
" name" +
104+
" }" +
105+
" }" +
106+
" pageInfo {" +
107+
" startCursor," +
108+
" endCursor," +
109+
" hasPreviousPage," +
110+
" hasNextPage" +
111+
" }" +
112+
" }" +
113+
"}";
114+
}
115+
91116
}

spring-graphql/src/test/java/org/springframework/graphql/ResponseHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ public Error error(int index) {
131131
return new Error(index);
132132
}
133133

134+
public void assertData(String expectedJson) {
135+
expectedJson = "{\"data\":" + expectedJson + "}";
136+
assertThat(this.documentContext.jsonString()).as("Errors: " + this.errors).isEqualTo(expectedJson);
137+
}
138+
134139

135140
public static ResponseHelper forResult(ExecutionResult result) {
136141
return new ResponseHelper(result.toSpecification(), result.getErrors());

spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/SchemaMappingPaginationTests.java

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
package org.springframework.graphql.data.method.annotation.support;
1717

1818
import java.util.List;
19-
import java.util.function.BiConsumer;
2019

2120
import org.junit.jupiter.api.Test;
22-
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
21+
import reactor.core.publisher.Mono;
2322

2423
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2524
import org.springframework.data.domain.OffsetScrollPosition;
@@ -29,6 +28,7 @@
2928
import org.springframework.graphql.ExecutionGraphQlResponse;
3029
import org.springframework.graphql.ExecutionGraphQlService;
3130
import org.springframework.graphql.GraphQlSetup;
31+
import org.springframework.graphql.ResponseHelper;
3232
import org.springframework.graphql.TestExecutionRequest;
3333
import org.springframework.graphql.data.method.annotation.QueryMapping;
3434
import org.springframework.graphql.data.pagination.ConnectionFieldTypeVisitor;
@@ -39,56 +39,22 @@
3939
import org.springframework.graphql.execution.ConnectionTypeDefinitionConfigurer;
4040
import org.springframework.stereotype.Controller;
4141

42-
import static org.assertj.core.api.Assertions.assertThat;
43-
4442
/**
4543
* GraphQL paginated requests handled through {@code @SchemaMapping} methods.
4644
*
4745
* @author Rossen Stoyanchev
4846
*/
4947
public class SchemaMappingPaginationTests {
5048

51-
private static final String SCHEMA = """
52-
type Query {
53-
books(first:Int, after:String): BookConnection
54-
}
55-
type Book {
56-
id: ID
57-
name: String
58-
}
59-
""";
60-
6149

6250
@Test
63-
void forwardPagination() throws Exception {
64-
65-
String document = """
66-
{
67-
books(first:2, after:"O_3") {
68-
edges {
69-
cursor,
70-
node {
71-
id
72-
name
73-
}
74-
}
75-
pageInfo {
76-
startCursor,
77-
endCursor,
78-
hasPreviousPage,
79-
hasNextPage
80-
}
81-
}
82-
}
83-
""";
84-
85-
ExecutionGraphQlService graphQlService = graphQlService();
86-
87-
ExecutionGraphQlResponse response =
88-
graphQlService.execute(TestExecutionRequest.forDocument(document)).block();
89-
90-
assertThat(new ObjectMapper().writeValueAsString(response.getData()))
91-
.as("Errors: " + response.getErrors()).isEqualTo(
51+
void forwardPagination() {
52+
53+
String query = BookSource.booksConnectionQuery("first:2, after:\"O_3\"");
54+
55+
Mono<ExecutionGraphQlResponse> response = graphQlService().execute(TestExecutionRequest.forDocument(query));
56+
57+
ResponseHelper.forResponse(response).assertData(
9258
"{\"books\":{" +
9359
"\"edges\":[" +
9460
"{\"cursor\":\"O_0\",\"node\":{\"id\":\"4\",\"name\":\"To The Lighthouse\"}}," +
@@ -115,7 +81,7 @@ private ExecutionGraphQlService graphQlService() {
11581
configurer.setApplicationContext(context);
11682
configurer.afterPropertiesSet();
11783

118-
GraphQlSetup setup = GraphQlSetup.schemaContent(SCHEMA).runtimeWiring(configurer);
84+
GraphQlSetup setup = GraphQlSetup.schemaResource(BookSource.paginationSchema).runtimeWiring(configurer);
11985
setup.typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer());
12086
setup.typeVisitor(ConnectionFieldTypeVisitor.create(List.of(new WindowConnectionAdapter(cursorStrategy))));
12187

spring-graphql/src/test/java/org/springframework/graphql/data/pagination/ConnectionFieldTypeVisitorTests.java

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@
2020
import java.util.List;
2121

2222
import org.junit.jupiter.api.Test;
23-
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
23+
import reactor.core.publisher.Mono;
2424

2525
import org.springframework.graphql.BookSource;
2626
import org.springframework.graphql.ExecutionGraphQlResponse;
2727
import org.springframework.graphql.GraphQlSetup;
28+
import org.springframework.graphql.ResponseHelper;
2829
import org.springframework.graphql.TestExecutionRequest;
2930
import org.springframework.graphql.execution.ConnectionTypeDefinitionConfigurer;
3031

31-
import static org.assertj.core.api.Assertions.assertThat;
32-
3332
/**
3433
* Unit tests for {@link ConnectionFieldTypeVisitor}.
3534
*
@@ -39,49 +38,22 @@ public class ConnectionFieldTypeVisitorTests {
3938

4039

4140
@Test
42-
void dataFetcherDecoration() throws Exception {
43-
44-
String schemaContent = """
45-
type Query {
46-
books: BookConnection
47-
}
48-
type Book {
49-
id: ID
50-
name: String
51-
}
52-
""";
53-
54-
String document = "{ " +
55-
" books { " +
56-
" edges {" +
57-
" cursor," +
58-
" node {" +
59-
" id" +
60-
" name" +
61-
" }" +
62-
" }" +
63-
" pageInfo {" +
64-
" startCursor," +
65-
" endCursor," +
66-
" hasPreviousPage," +
67-
" hasNextPage" +
68-
" }" +
69-
" }" +
70-
"}";
41+
void dataFetcherDecoration() {
42+
43+
String document = BookSource.booksConnectionQuery("");
7144

7245
TestConnectionAdapter adapter = new TestConnectionAdapter();
7346
adapter.setInitialOffset(30);
7447
adapter.setHasNext(true);
7548

76-
ExecutionGraphQlResponse response = GraphQlSetup.schemaContent(schemaContent)
49+
Mono<ExecutionGraphQlResponse> response = GraphQlSetup.schemaResource(BookSource.paginationSchema)
7750
.dataFetcher("Query", "books", env -> BookSource.books())
7851
.typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer())
7952
.typeVisitor(ConnectionFieldTypeVisitor.create(List.of(adapter)))
8053
.toGraphQlService()
81-
.execute(TestExecutionRequest.forDocument(document))
82-
.block();
54+
.execute(TestExecutionRequest.forDocument(document));
8355

84-
assertThat(new ObjectMapper().writeValueAsString(response.getData())).isEqualTo(
56+
ResponseHelper.forResponse(response).assertData(
8557
"{\"books\":{" +
8658
"\"edges\":[" +
8759
"{\"cursor\":\"T_30\",\"node\":{\"id\":\"1\",\"name\":\"Nineteen Eighty-Four\"}}," +

spring-graphql/src/test/java/org/springframework/graphql/execution/ConnectionTypeDefinitionConfigurerTests.java

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,15 @@
2727
import graphql.relay.Edge;
2828
import graphql.schema.DataFetcher;
2929
import org.junit.jupiter.api.Test;
30-
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
30+
import reactor.core.publisher.Mono;
3131

3232
import org.springframework.graphql.Book;
3333
import org.springframework.graphql.BookSource;
3434
import org.springframework.graphql.ExecutionGraphQlResponse;
3535
import org.springframework.graphql.GraphQlSetup;
36+
import org.springframework.graphql.ResponseHelper;
3637
import org.springframework.graphql.TestExecutionRequest;
3738

38-
import static org.assertj.core.api.Assertions.assertThat;
39-
4039
/**
4140
* Unit tests for {@link ConnectionTypeDefinitionConfigurer}.
4241
*
@@ -46,48 +45,21 @@
4645
public class ConnectionTypeDefinitionConfigurerTests {
4746

4847
@Test
49-
void connectionTypeGeneration() throws Exception {
50-
51-
String schema = """
52-
type Query {
53-
books: BookConnection
54-
}
55-
type Book {
56-
id: ID
57-
name: String
58-
}
59-
""";
48+
void connectionTypeGeneration() {
6049

6150
List<Book> books = BookSource.books();
6251

6352
DataFetcher<?> dataFetcher = environment ->
6453
createConnection(books, book -> new DefaultConnectionCursor("book:" + book.getId()));
6554

66-
String document = "{ " +
67-
" books { " +
68-
" edges {" +
69-
" cursor," +
70-
" node {" +
71-
" id" +
72-
" name" +
73-
" }" +
74-
" }" +
75-
" pageInfo {" +
76-
" startCursor," +
77-
" endCursor," +
78-
" hasPreviousPage," +
79-
" hasNextPage" +
80-
" }" +
81-
" }" +
82-
"}";
83-
84-
ExecutionGraphQlResponse response = initGraphQlSetup(schema)
55+
String document = BookSource.booksConnectionQuery("");
56+
57+
Mono<ExecutionGraphQlResponse> response = initGraphQlSetup()
8558
.dataFetcher("Query", "books", dataFetcher)
8659
.toGraphQlService()
87-
.execute(TestExecutionRequest.forDocument(document))
88-
.block();
60+
.execute(TestExecutionRequest.forDocument(document));
8961

90-
assertThat(new ObjectMapper().writeValueAsString(response.getData())).isEqualTo(
62+
ResponseHelper.forResponse(response).assertData(
9163
"{\"books\":{" +
9264
"\"edges\":[" +
9365
"{\"cursor\":\"book:1\",\"node\":{\"id\":\"1\",\"name\":\"Nineteen Eighty-Four\"}}," +
@@ -107,8 +79,9 @@ void connectionTypeGeneration() throws Exception {
10779
);
10880
}
10981

110-
private GraphQlSetup initGraphQlSetup(String schema) {
111-
return GraphQlSetup.schemaContent(schema).typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer());
82+
private GraphQlSetup initGraphQlSetup() {
83+
return GraphQlSetup.schemaResource(BookSource.paginationSchema)
84+
.typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer());
11285
}
11386

11487
private static <N> Connection<N> createConnection(
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type Query {
2+
books(first:Int, after:String): BookConnection
3+
}
4+
5+
type Book {
6+
id: ID
7+
name: String
8+
}

0 commit comments

Comments
 (0)