From 3ebdce0f7ab0b8bab5cff78b44b39b406902e327 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Thu, 30 Mar 2023 19:17:35 +0100 Subject: [PATCH] Pagination test refactoring --- .../springframework/graphql/BookSource.java | 25 ++++++++++ .../graphql/ResponseHelper.java | 5 ++ .../support/SchemaMappingPaginationTests.java | 48 +++---------------- .../ConnectionFieldTypeVisitorTests.java | 42 +++------------- ...nnectionTypeDefinitionConfigurerTests.java | 47 ++++-------------- .../books/pagination-schema.graphqls | 8 ++++ 6 files changed, 62 insertions(+), 113 deletions(-) create mode 100644 spring-graphql/src/test/resources/books/pagination-schema.graphqls diff --git a/spring-graphql/src/test/java/org/springframework/graphql/BookSource.java b/spring-graphql/src/test/java/org/springframework/graphql/BookSource.java index 52efe8d2..21cf53e5 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/BookSource.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/BookSource.java @@ -22,15 +22,19 @@ import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; +import jakarta.annotation.Nullable; import reactor.core.publisher.Flux; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; +import org.springframework.util.StringUtils; public class BookSource { public static final Resource schema = new ClassPathResource("books/schema.graphqls"); + public static final Resource paginationSchema = new ClassPathResource("books/pagination-schema.graphqls"); + private static final Map booksMap = new HashMap<>(); @@ -88,4 +92,25 @@ public class BookSource { return authorsMap.get(id); } + public static String booksConnectionQuery(@Nullable String arguments) { + arguments = StringUtils.hasText(arguments) ? "(" + arguments + ")" : ""; + return "{" + + " books" + arguments + " {" + + " edges {" + + " cursor," + + " node {" + + " id" + + " name" + + " }" + + " }" + + " pageInfo {" + + " startCursor," + + " endCursor," + + " hasPreviousPage," + + " hasNextPage" + + " }" + + " }" + + "}"; + } + } diff --git a/spring-graphql/src/test/java/org/springframework/graphql/ResponseHelper.java b/spring-graphql/src/test/java/org/springframework/graphql/ResponseHelper.java index 43c45cbd..eed31b01 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/ResponseHelper.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/ResponseHelper.java @@ -131,6 +131,11 @@ public class ResponseHelper { return new Error(index); } + public void assertData(String expectedJson) { + expectedJson = "{\"data\":" + expectedJson + "}"; + assertThat(this.documentContext.jsonString()).as("Errors: " + this.errors).isEqualTo(expectedJson); + } + public static ResponseHelper forResult(ExecutionResult result) { return new ResponseHelper(result.toSpecification(), result.getErrors()); 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 f95d31fe..c633409d 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 @@ -16,10 +16,9 @@ package org.springframework.graphql.data.method.annotation.support; import java.util.List; -import java.util.function.BiConsumer; import org.junit.jupiter.api.Test; -import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; +import reactor.core.publisher.Mono; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.data.domain.OffsetScrollPosition; @@ -29,6 +28,7 @@ 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.data.method.annotation.QueryMapping; import org.springframework.graphql.data.pagination.ConnectionFieldTypeVisitor; @@ -39,8 +39,6 @@ import org.springframework.graphql.data.query.WindowConnectionAdapter; import org.springframework.graphql.execution.ConnectionTypeDefinitionConfigurer; import org.springframework.stereotype.Controller; -import static org.assertj.core.api.Assertions.assertThat; - /** * GraphQL paginated requests handled through {@code @SchemaMapping} methods. * @@ -48,47 +46,15 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class SchemaMappingPaginationTests { - private static final String SCHEMA = """ - type Query { - books(first:Int, after:String): BookConnection - } - type Book { - id: ID - name: String - } - """; - @Test - void forwardPagination() throws Exception { + void forwardPagination() { - String document = """ - { - books(first:2, after:"O_3") { - edges { - cursor, - node { - id - name - } - } - pageInfo { - startCursor, - endCursor, - hasPreviousPage, - hasNextPage - } - } - } - """; + String query = BookSource.booksConnectionQuery("first:2, after:\"O_3\""); - ExecutionGraphQlService graphQlService = graphQlService(); + Mono response = graphQlService().execute(TestExecutionRequest.forDocument(query)); - ExecutionGraphQlResponse response = - graphQlService.execute(TestExecutionRequest.forDocument(document)).block(); - - assertThat(new ObjectMapper().writeValueAsString(response.getData())) - .as("Errors: " + response.getErrors()).isEqualTo( + ResponseHelper.forResponse(response).assertData( "{\"books\":{" + "\"edges\":[" + "{\"cursor\":\"O_0\",\"node\":{\"id\":\"4\",\"name\":\"To The Lighthouse\"}}," + @@ -115,7 +81,7 @@ public class SchemaMappingPaginationTests { configurer.setApplicationContext(context); configurer.afterPropertiesSet(); - GraphQlSetup setup = GraphQlSetup.schemaContent(SCHEMA).runtimeWiring(configurer); + GraphQlSetup setup = GraphQlSetup.schemaResource(BookSource.paginationSchema).runtimeWiring(configurer); setup.typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer()); setup.typeVisitor(ConnectionFieldTypeVisitor.create(List.of(new WindowConnectionAdapter(cursorStrategy)))); 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 06c310ca..ec62118e 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 @@ -20,16 +20,15 @@ import java.util.Collection; import java.util.List; import org.junit.jupiter.api.Test; -import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; +import reactor.core.publisher.Mono; 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; -import static org.assertj.core.api.Assertions.assertThat; - /** * Unit tests for {@link ConnectionFieldTypeVisitor}. * @@ -39,49 +38,22 @@ public class ConnectionFieldTypeVisitorTests { @Test - void dataFetcherDecoration() throws Exception { + void dataFetcherDecoration() { - String schemaContent = """ - type Query { - books: BookConnection - } - type Book { - id: ID - name: String - } - """; - - String document = "{ " + - " books { " + - " edges {" + - " cursor," + - " node {" + - " id" + - " name" + - " }" + - " }" + - " pageInfo {" + - " startCursor," + - " endCursor," + - " hasPreviousPage," + - " hasNextPage" + - " }" + - " }" + - "}"; + String document = BookSource.booksConnectionQuery(""); TestConnectionAdapter adapter = new TestConnectionAdapter(); adapter.setInitialOffset(30); adapter.setHasNext(true); - ExecutionGraphQlResponse response = GraphQlSetup.schemaContent(schemaContent) + Mono response = GraphQlSetup.schemaResource(BookSource.paginationSchema) .dataFetcher("Query", "books", env -> BookSource.books()) .typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer()) .typeVisitor(ConnectionFieldTypeVisitor.create(List.of(adapter))) .toGraphQlService() - .execute(TestExecutionRequest.forDocument(document)) - .block(); + .execute(TestExecutionRequest.forDocument(document)); - assertThat(new ObjectMapper().writeValueAsString(response.getData())).isEqualTo( + ResponseHelper.forResponse(response).assertData( "{\"books\":{" + "\"edges\":[" + "{\"cursor\":\"T_30\",\"node\":{\"id\":\"1\",\"name\":\"Nineteen Eighty-Four\"}}," + 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 2a908fc6..fb532923 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 @@ -27,16 +27,15 @@ import graphql.relay.DefaultPageInfo; import graphql.relay.Edge; import graphql.schema.DataFetcher; import org.junit.jupiter.api.Test; -import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; +import reactor.core.publisher.Mono; import org.springframework.graphql.Book; 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 static org.assertj.core.api.Assertions.assertThat; - /** * Unit tests for {@link ConnectionTypeDefinitionConfigurer}. * @@ -46,48 +45,21 @@ import static org.assertj.core.api.Assertions.assertThat; public class ConnectionTypeDefinitionConfigurerTests { @Test - void connectionTypeGeneration() throws Exception { - - String schema = """ - type Query { - books: BookConnection - } - type Book { - id: ID - name: String - } - """; + void connectionTypeGeneration() { List books = BookSource.books(); DataFetcher dataFetcher = environment -> createConnection(books, book -> new DefaultConnectionCursor("book:" + book.getId())); - String document = "{ " + - " books { " + - " edges {" + - " cursor," + - " node {" + - " id" + - " name" + - " }" + - " }" + - " pageInfo {" + - " startCursor," + - " endCursor," + - " hasPreviousPage," + - " hasNextPage" + - " }" + - " }" + - "}"; + String document = BookSource.booksConnectionQuery(""); - ExecutionGraphQlResponse response = initGraphQlSetup(schema) + Mono response = initGraphQlSetup() .dataFetcher("Query", "books", dataFetcher) .toGraphQlService() - .execute(TestExecutionRequest.forDocument(document)) - .block(); + .execute(TestExecutionRequest.forDocument(document)); - assertThat(new ObjectMapper().writeValueAsString(response.getData())).isEqualTo( + ResponseHelper.forResponse(response).assertData( "{\"books\":{" + "\"edges\":[" + "{\"cursor\":\"book:1\",\"node\":{\"id\":\"1\",\"name\":\"Nineteen Eighty-Four\"}}," + @@ -107,8 +79,9 @@ public class ConnectionTypeDefinitionConfigurerTests { ); } - private GraphQlSetup initGraphQlSetup(String schema) { - return GraphQlSetup.schemaContent(schema).typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer()); + private GraphQlSetup initGraphQlSetup() { + return GraphQlSetup.schemaResource(BookSource.paginationSchema) + .typeDefinitionConfigurer(new ConnectionTypeDefinitionConfigurer()); } private static Connection createConnection( diff --git a/spring-graphql/src/test/resources/books/pagination-schema.graphqls b/spring-graphql/src/test/resources/books/pagination-schema.graphqls new file mode 100644 index 00000000..f31fb90e --- /dev/null +++ b/spring-graphql/src/test/resources/books/pagination-schema.graphqls @@ -0,0 +1,8 @@ +type Query { + books(first:Int, after:String): BookConnection +} + +type Book { + id: ID + name: String +}