Refactoring in spring-graphql tests

This commit is contained in:
Rossen Stoyanchev
2021-08-02 13:16:49 +01:00
parent 317d75cc8a
commit c2a996e564
6 changed files with 135 additions and 26 deletions

View File

@@ -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;
}
}

View File

@@ -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;
}

View File

@@ -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<Long, Book> 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<Long, Book> booksMap() {
return booksMap;
}
public static List<Book> books() {
return new ArrayList<>(booksMap.values());
}
public static Book getBook(Long id) {
return booksMap.get(id);
}
}

View File

@@ -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)))

View File

@@ -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<Long, Book> 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();
}

View File

@@ -6,7 +6,12 @@ type Query {
type Book {
id: ID
name: String
author: String
author: Author
}
type Author {
firstName: String
lastName: String
}
type Subscription {