Extract method to GraphQlTestUtils
This commit is contained in:
@@ -18,13 +18,18 @@ package org.springframework.graphql;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
import graphql.ExecutionResult;
|
||||
import graphql.GraphQL;
|
||||
import graphql.schema.DataFetcher;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.graphql.execution.DataFetcherExceptionResolver;
|
||||
import org.springframework.graphql.execution.GraphQlSource;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Utility methods for GraphQL tests.
|
||||
@@ -57,4 +62,19 @@ public abstract class GraphQlTestUtils {
|
||||
.configureRuntimeWiring(wiring -> wiring.type(typeName, (builder) -> builder.dataFetcher(fieldName, fetcher)));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T checkErrorsAndGetData(@Nullable ExecutionResult result, String key) {
|
||||
Map<String, Object> map = checkErrorsAndGetData(result);
|
||||
return (T) map.get(key);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T checkErrorsAndGetData(@Nullable ExecutionResult result) {
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.getErrors()).as("Errors present in GraphQL response").isEmpty();
|
||||
T data = result.getData();
|
||||
assertThat(data).isNotNull();
|
||||
return (T) data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.graphql.GraphQlService;
|
||||
import org.springframework.graphql.GraphQlTestUtils;
|
||||
import org.springframework.graphql.RequestInput;
|
||||
import org.springframework.graphql.data.method.annotation.BatchMapping;
|
||||
import org.springframework.graphql.data.method.annotation.QueryMapping;
|
||||
@@ -45,7 +46,6 @@ import org.springframework.graphql.execution.BatchLoaderRegistry;
|
||||
import org.springframework.graphql.execution.DefaultBatchLoaderRegistry;
|
||||
import org.springframework.graphql.execution.ExecutionGraphQlService;
|
||||
import org.springframework.graphql.execution.GraphQlSource;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -121,9 +121,7 @@ public class BatchMappingInvocationTests {
|
||||
.execute(new RequestInput(query, null, null))
|
||||
.block();
|
||||
|
||||
Map<String, Object> data = getData(result);
|
||||
List<Map<String, Object>> actualCourses = (List<Map<String, Object>>) data.get("courses");
|
||||
|
||||
List<Map<String, Object>> actualCourses = GraphQlTestUtils.checkErrorsAndGetData(result, "courses");
|
||||
List<Course> courses = Course.allCourses();
|
||||
assertThat(actualCourses).hasSize(courses.size());
|
||||
|
||||
@@ -155,8 +153,7 @@ public class BatchMappingInvocationTests {
|
||||
.execute(new RequestInput(query, null, null))
|
||||
.block();
|
||||
|
||||
Map<String, Object> data = getData(result);
|
||||
List<Map<String, Object>> actualCourses = (List<Map<String, Object>>) data.get("courses");
|
||||
List<Map<String, Object>> actualCourses = GraphQlTestUtils.checkErrorsAndGetData(result, "courses");
|
||||
|
||||
List<Course> courses = Course.allCourses();
|
||||
assertThat(actualCourses).hasSize(courses.size());
|
||||
@@ -185,14 +182,6 @@ public class BatchMappingInvocationTests {
|
||||
return applicationContext.getBean(ExecutionGraphQlService.class);
|
||||
}
|
||||
|
||||
private <T> T getData(@Nullable ExecutionResult result) {
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.getErrors()).isEmpty();
|
||||
T data = result.getData();
|
||||
assertThat(data).isNotNull();
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
private static class CourseController {
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ import org.springframework.graphql.Book;
|
||||
import org.springframework.graphql.BookCriteria;
|
||||
import org.springframework.graphql.BookSource;
|
||||
import org.springframework.graphql.GraphQlService;
|
||||
import org.springframework.graphql.GraphQlTestUtils;
|
||||
import org.springframework.graphql.RequestInput;
|
||||
import org.springframework.graphql.data.method.annotation.Argument;
|
||||
import org.springframework.graphql.data.method.annotation.MutationMapping;
|
||||
@@ -48,7 +49,6 @@ import org.springframework.graphql.execution.BatchLoaderRegistry;
|
||||
import org.springframework.graphql.execution.DefaultBatchLoaderRegistry;
|
||||
import org.springframework.graphql.execution.ExecutionGraphQlService;
|
||||
import org.springframework.graphql.execution.GraphQlSource;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -77,13 +77,11 @@ public class SchemaMappingInvocationTests {
|
||||
.execute(new RequestInput(query, null, null))
|
||||
.block();
|
||||
|
||||
Map<String, Object> data = getData(result);
|
||||
|
||||
Map<String, Object> book = getValue(data, "bookById");
|
||||
Map<String, Object> book = GraphQlTestUtils.checkErrorsAndGetData(result, "bookById");
|
||||
assertThat(book.get("id")).isEqualTo("1");
|
||||
assertThat(book.get("name")).isEqualTo("Nineteen Eighty-Four");
|
||||
|
||||
Map<String, Object> author = getValue(book, "author");
|
||||
Map<String, Object> author = (Map<String, Object>) book.get("author");
|
||||
assertThat(author.get("firstName")).isEqualTo("George");
|
||||
assertThat(author.get("lastName")).isEqualTo("Orwell");
|
||||
}
|
||||
@@ -101,8 +99,8 @@ public class SchemaMappingInvocationTests {
|
||||
.execute(new RequestInput(query, null, null))
|
||||
.block();
|
||||
|
||||
Map<String, Object> data = getData(result);
|
||||
List<Map<String, Object>> bookList = getValue(data, "booksByCriteria");
|
||||
List<Map<String, Object>> bookList = GraphQlTestUtils.checkErrorsAndGetData(result, "booksByCriteria");
|
||||
|
||||
assertThat(bookList).hasSize(2);
|
||||
assertThat(bookList.get(0).get("name")).isEqualTo("Nineteen Eighty-Four");
|
||||
assertThat(bookList.get(1).get("name")).isEqualTo("Animal Farm");
|
||||
@@ -129,9 +127,8 @@ public class SchemaMappingInvocationTests {
|
||||
.execute(requestInput)
|
||||
.block();
|
||||
|
||||
Map<String, Object> data = getData(result);
|
||||
Map<String, Object> author = GraphQlTestUtils.checkErrorsAndGetData(result, "authorById");
|
||||
|
||||
Map<String, Object> author = getValue(data, "authorById");
|
||||
assertThat(author.get("id")).isEqualTo("101");
|
||||
assertThat(author.get("firstName")).isEqualTo("George");
|
||||
assertThat(author.get("lastName")).isEqualTo("Orwell");
|
||||
@@ -153,9 +150,7 @@ public class SchemaMappingInvocationTests {
|
||||
.execute(new RequestInput(operation, null, null))
|
||||
.block();
|
||||
|
||||
Map<String, Object> data = getData(result);
|
||||
|
||||
Map<String, Object> author = getValue(data, "addAuthor");
|
||||
Map<String, Object> author = GraphQlTestUtils.checkErrorsAndGetData(result, "addAuthor");
|
||||
assertThat(author.get("id")).isEqualTo("99");
|
||||
assertThat(author.get("firstName")).isEqualTo("James");
|
||||
assertThat(author.get("lastName")).isEqualTo("Joyce");
|
||||
@@ -174,7 +169,7 @@ public class SchemaMappingInvocationTests {
|
||||
.execute(new RequestInput(operation, null, null))
|
||||
.block();
|
||||
|
||||
Publisher<ExecutionResult> publisher = getData(result);
|
||||
Publisher<ExecutionResult> publisher = GraphQlTestUtils.checkErrorsAndGetData(result);
|
||||
|
||||
Flux<Map<String, Object>> bookFlux = Flux.from(publisher).map(rs -> {
|
||||
Map<String, Object> map = rs.getData();
|
||||
@@ -202,19 +197,6 @@ public class SchemaMappingInvocationTests {
|
||||
return applicationContext.getBean(ExecutionGraphQlService.class);
|
||||
}
|
||||
|
||||
private <T> T getData(@Nullable ExecutionResult result) {
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.getErrors()).isEmpty();
|
||||
T data = result.getData();
|
||||
assertThat(data).isNotNull();
|
||||
return data;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T getValue(Map<String, Object> data, String key) {
|
||||
return (T) data.get(key);
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
static class TestConfig {
|
||||
|
||||
Reference in New Issue
Block a user