From c2a996e564fd6b6be01c55fbff9a70016a074f8c Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 2 Aug 2021 13:16:49 +0100 Subject: [PATCH] Refactoring in spring-graphql tests --- .../org/springframework/graphql/Author.java | 52 +++++++++++++++++++ .../graphql/{web => }/Book.java | 10 ++-- .../springframework/graphql/BookSource.java | 50 ++++++++++++++++++ .../graphql/GraphQlTestUtils.java | 11 ++-- .../graphql/web/BookTestUtils.java | 31 ++++++----- .../src/test/resources/books/schema.graphqls | 7 ++- 6 files changed, 135 insertions(+), 26 deletions(-) create mode 100644 spring-graphql/src/test/java/org/springframework/graphql/Author.java rename spring-graphql/src/test/java/org/springframework/graphql/{web => }/Book.java (85%) create mode 100644 spring-graphql/src/test/java/org/springframework/graphql/BookSource.java diff --git a/spring-graphql/src/test/java/org/springframework/graphql/Author.java b/spring-graphql/src/test/java/org/springframework/graphql/Author.java new file mode 100644 index 00000000..97b3b642 --- /dev/null +++ b/spring-graphql/src/test/java/org/springframework/graphql/Author.java @@ -0,0 +1,52 @@ +/* + * Copyright 2002-2021 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; + +public class Author { + + String firstName; + + String lastName; + + public Author() { + } + + public Author(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + + public String getFirstName() { + return this.firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return this.lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getFullName() { + return this.firstName + " " + this.lastName; + } +} diff --git a/spring-graphql/src/test/java/org/springframework/graphql/web/Book.java b/spring-graphql/src/test/java/org/springframework/graphql/Book.java similarity index 85% rename from spring-graphql/src/test/java/org/springframework/graphql/web/Book.java rename to spring-graphql/src/test/java/org/springframework/graphql/Book.java index 1dadfe62..dbe24efb 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/web/Book.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/Book.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.graphql.web; +package org.springframework.graphql; public class Book { @@ -22,12 +22,12 @@ public class Book { String name; - String author; + Author author; public Book() { } - public Book(Long id, String name, String author) { + public Book(Long id, String name, Author author) { this.id = id; this.name = name; this.author = author; @@ -49,11 +49,11 @@ public class Book { this.name = name; } - public String getAuthor() { + public Author getAuthor() { return this.author; } - public void setAuthor(String author) { + public void setAuthor(Author author) { this.author = author; } diff --git a/spring-graphql/src/test/java/org/springframework/graphql/BookSource.java b/spring-graphql/src/test/java/org/springframework/graphql/BookSource.java new file mode 100644 index 00000000..490620e2 --- /dev/null +++ b/spring-graphql/src/test/java/org/springframework/graphql/BookSource.java @@ -0,0 +1,50 @@ +/* + * Copyright 2002-2021 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; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class BookSource { + + private static final Map booksMap = new HashMap<>(4); + + static { + booksMap.put(1L, new Book(1L, "Nineteen Eighty-Four", new Author("George", "Orwell"))); + booksMap.put(2L, new Book(2L, "The Great Gatsby", new Author("F. Scott", "Fitzgerald"))); + booksMap.put(3L, new Book(3L, "Catch-22", new Author("Joseph", "Heller"))); + booksMap.put(4L, new Book(4L, "To The Lighthouse", new Author("Virginia", "Woolf"))); + booksMap.put(5L, new Book(5L, "Animal Farm", new Author("George", "Orwell"))); + booksMap.put(42L, new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author("Douglas", "Adams"))); + booksMap.put(53L, new Book(53L, "Breaking Bad", new Author("Vince", "Gilligan"))); + } + + + public static Map booksMap() { + return booksMap; + } + + public static List books() { + return new ArrayList<>(booksMap.values()); + } + + public static Book getBook(Long id) { + return booksMap.get(id); + } + +} diff --git a/spring-graphql/src/test/java/org/springframework/graphql/GraphQlTestUtils.java b/spring-graphql/src/test/java/org/springframework/graphql/GraphQlTestUtils.java index 0fce2d33..5c62d807 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/GraphQlTestUtils.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/GraphQlTestUtils.java @@ -31,13 +31,16 @@ import org.springframework.graphql.execution.GraphQlSource; */ public abstract class GraphQlTestUtils { - public static GraphQL initGraphQl(String schemaContent, String typeName, String fieldName, DataFetcher fetcher) { + public static GraphQL initGraphQl( + String schemaContent, String typeName, String fieldName, DataFetcher fetcher) { + return initGraphQlSource(schemaContent, typeName, fieldName, fetcher) .build() .graphQl(); } - public static GraphQL initGraphQl(String schemaContent, String typeName, String fieldName, DataFetcher fetcher, + public static GraphQL initGraphQl( + String schemaContent, String typeName, String fieldName, DataFetcher fetcher, DataFetcherExceptionResolver... resolvers) { return initGraphQlSource(schemaContent, typeName, fieldName, fetcher) @@ -46,8 +49,8 @@ public abstract class GraphQlTestUtils { .graphQl(); } - public static GraphQlSource.Builder initGraphQlSource(String schemaContent, String typeName, String fieldName, - DataFetcher fetcher) { + public static GraphQlSource.Builder initGraphQlSource( + String schemaContent, String typeName, String fieldName, DataFetcher fetcher) { return GraphQlSource.builder() .schemaResources(new ByteArrayResource(schemaContent.getBytes(StandardCharsets.UTF_8))) diff --git a/spring-graphql/src/test/java/org/springframework/graphql/web/BookTestUtils.java b/spring-graphql/src/test/java/org/springframework/graphql/web/BookTestUtils.java index 53327750..f803f91f 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/web/BookTestUtils.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/web/BookTestUtils.java @@ -17,13 +17,12 @@ package org.springframework.graphql.web; import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; import graphql.schema.idl.TypeRuntimeWiring; import reactor.core.publisher.Flux; import org.springframework.core.io.ClassPathResource; +import org.springframework.graphql.BookSource; import org.springframework.graphql.execution.ExecutionGraphQlService; import org.springframework.graphql.execution.GraphQlSource; @@ -37,8 +36,13 @@ public abstract class BookTestUtils { "\"payload\":{\"query\": \"" + " query TestQuery {" + " bookById(id: \\\"1\\\"){ " + - " id" + " name" + - " author" + " }}\"}" + + " id" + + " name" + + " author {" + + " firstName" + + " lastName" + + " }" + + " }}\"}" + "}"; public static final String BOOK_SUBSCRIPTION = "{" + @@ -49,19 +53,13 @@ public abstract class BookTestUtils { " bookSearch(author: \\\"George\\\") {" + " id" + " name" + - " author" + + " author {" + + " firstName" + + " lastName" + + " }" + " }}\"}" + "}"; - private static final Map booksMap = new HashMap<>(4); - static { - booksMap.put(1L, new Book(1L, "Nineteen Eighty-Four", "George Orwell")); - booksMap.put(2L, new Book(2L, "The Great Gatsby", "F. Scott Fitzgerald")); - booksMap.put(3L, new Book(3L, "Catch-22", "Joseph Heller")); - booksMap.put(4L, new Book(4L, "To The Lighthouse", "Virginia Woolf")); - booksMap.put(5L, new Book(5L, "Animal Farm", "George Orwell")); - } - public static WebGraphQlHandler initWebGraphQlHandler(WebInterceptor... interceptors) { return WebGraphQlHandler.builder(new ExecutionGraphQlService(graphQlSource())) .interceptors(Arrays.asList(interceptors)) @@ -74,12 +72,13 @@ public abstract class BookTestUtils { .configureRuntimeWiring(builder -> builder.type(TypeRuntimeWiring.newTypeWiring("Query") .dataFetcher("bookById", (env) -> { Long id = Long.parseLong(env.getArgument("id")); - return booksMap.get(id); + return BookSource.getBook(id); })) .type(TypeRuntimeWiring.newTypeWiring("Subscription") .dataFetcher("bookSearch", (env) -> { String author = env.getArgument("author"); - return Flux.fromIterable(booksMap.values()).filter((book) -> book.getAuthor().contains(author)); + return Flux.fromIterable(BookSource.books()) + .filter((book) -> book.getAuthor().getFullName().contains(author)); }))) .build(); } diff --git a/spring-graphql/src/test/resources/books/schema.graphqls b/spring-graphql/src/test/resources/books/schema.graphqls index b6920e1c..3df829fe 100644 --- a/spring-graphql/src/test/resources/books/schema.graphqls +++ b/spring-graphql/src/test/resources/books/schema.graphqls @@ -6,7 +6,12 @@ type Query { type Book { id: ID name: String - author: String + author: Author +} + +type Author { + firstName: String + lastName: String } type Subscription {