From 0d52bfc2071f1f8596ad2a3c9c0e73b7be5bc0ff Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 15 Apr 2021 13:01:19 +0100 Subject: [PATCH] Update samples to use GraphQLTester See gh-43 --- samples/webflux-websocket/build.gradle | 3 + .../graphql/SubscriptionGraphQLTests.java | 73 +++++++++++++++++++ samples/webmvc-http/build.gradle | 1 + .../sample/graphql/project/JsonRequest.java | 68 ----------------- .../sample/graphql/project/JsonResponse.java | 46 ------------ ...entTests.java => MockMvcGraphQLTests.java} | 63 ++++++---------- 6 files changed, 100 insertions(+), 154 deletions(-) create mode 100644 samples/webflux-websocket/src/test/java/io/spring/sample/graphql/SubscriptionGraphQLTests.java delete mode 100644 samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/JsonRequest.java delete mode 100644 samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/JsonResponse.java rename samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/{MockWebTestClientTests.java => MockMvcGraphQLTests.java} (54%) diff --git a/samples/webflux-websocket/build.gradle b/samples/webflux-websocket/build.gradle index fa8ce2d70..da812d479 100644 --- a/samples/webflux-websocket/build.gradle +++ b/samples/webflux-websocket/build.gradle @@ -13,7 +13,10 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-webflux' implementation 'org.springframework.boot:spring-boot-starter-actuator' developmentOnly 'org.springframework.boot:spring-boot-devtools' + testImplementation project(':spring-graphql-test') + testImplementation 'org.springframework:spring-webflux' testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation 'io.projectreactor:reactor-test' } test { useJUnitPlatform() diff --git a/samples/webflux-websocket/src/test/java/io/spring/sample/graphql/SubscriptionGraphQLTests.java b/samples/webflux-websocket/src/test/java/io/spring/sample/graphql/SubscriptionGraphQLTests.java new file mode 100644 index 000000000..5f7f14cef --- /dev/null +++ b/samples/webflux-websocket/src/test/java/io/spring/sample/graphql/SubscriptionGraphQLTests.java @@ -0,0 +1,73 @@ +/* + * 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 io.spring.sample.graphql; + +import graphql.GraphQL; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import reactor.core.publisher.Flux; +import reactor.test.StepVerifier; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.graphql.WebGraphQLService; +import org.springframework.graphql.test.query.GraphQLTester; + +/** + * GraphQL subscription tests directly via {@link GraphQL}. + */ +@SpringBootTest +public class SubscriptionGraphQLTests { + + private GraphQLTester graphQLTester; + + + @BeforeEach + public void setUp(@Autowired WebGraphQLService service) { + this.graphQLTester = GraphQLTester.create(service); + } + + + @Test + void subscriptionWithEntityPath() { + String query = "subscription { greetings }"; + + Flux result = this.graphQLTester.query(query) + .executeSubscription() + .toFlux("greetings", String.class); + + StepVerifier.create(result) + .expectNext("Hi", "Bonjour", "Hola", "Ciao", "Zdravo") + .verifyComplete(); + } + + @Test + void subscriptionWithResponseSpec() { + String query = "subscription { greetings }"; + + Flux result = this.graphQLTester.query(query) + .executeSubscription() + .toFlux(); + + StepVerifier.create(result) + .consumeNextWith(spec -> spec.path("greetings").valueExists()) + .consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Bonjour\"")) + .consumeNextWith(spec -> spec.path("greetings").matchesJson("\"Hola\"")) + .expectNextCount(2) + .verifyComplete(); + } + +} diff --git a/samples/webmvc-http/build.gradle b/samples/webmvc-http/build.gradle index e7c0b87c6..437b136af 100644 --- a/samples/webmvc-http/build.gradle +++ b/samples/webmvc-http/build.gradle @@ -16,6 +16,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-actuator' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'com.h2database:h2' + testImplementation project(':spring-graphql-test') testImplementation 'org.springframework:spring-webflux' testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/JsonRequest.java b/samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/JsonRequest.java deleted file mode 100644 index 5e8f01bdf..000000000 --- a/samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/JsonRequest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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 io.spring.sample.graphql.project; - -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.Map; - -import org.springframework.lang.Nullable; -import org.springframework.util.CollectionUtils; - -class JsonRequest { - - private final String query; - - @Nullable - private final String operationName; - - private final Map variables; - - - private JsonRequest(String query, @Nullable String operationName, Map variables) { - this.query = query; - this.operationName = operationName; - this.variables = (!CollectionUtils.isEmpty(variables) ? - new LinkedHashMap<>(variables) : Collections.emptyMap()); - } - - - public String getQuery() { - return query; - } - - public String query() { - return this.query; - } - - @Nullable - public String operationName() { - return this.operationName; - } - - public Map variables() { - return this.variables; - } - - public static JsonRequest create(String query) { - return new JsonRequest(query, null, Collections.emptyMap()); - } - - public static JsonRequest create(String query, String operationName, Map variables) { - return new JsonRequest(query, operationName, variables); - } - -} diff --git a/samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/JsonResponse.java b/samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/JsonResponse.java deleted file mode 100644 index 8db0ecffa..000000000 --- a/samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/JsonResponse.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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 io.spring.sample.graphql.project; - -import java.util.Collections; -import java.util.Map; - -import org.springframework.lang.Nullable; - -class JsonResponse { - - private Map data = Collections.emptyMap(); - - private Map> errors = Collections.emptyMap(); - - - public void setData(@Nullable Map data) { - this.data = data; - } - - public T getDataEntry() { - return this.data.values().iterator().next(); - } - - public void setErrors(@Nullable Map> errors) { - this.errors = errors; - } - - public Map> getErrors() { - return this.errors; - } - -} diff --git a/samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/MockWebTestClientTests.java b/samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/MockMvcGraphQLTests.java similarity index 54% rename from samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/MockWebTestClientTests.java rename to samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/MockMvcGraphQLTests.java index 97bbba81a..bdc2d1feb 100644 --- a/samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/MockWebTestClientTests.java +++ b/samples/webmvc-http/src/test/java/io/spring/sample/graphql/project/MockMvcGraphQLTests.java @@ -15,17 +15,13 @@ */ package io.spring.sample.graphql.project; -import java.util.List; -import java.util.function.Consumer; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.core.ParameterizedTypeReference; -import org.springframework.http.MediaType; +import org.springframework.graphql.test.query.GraphQLTester; import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.client.MockMvcWebTestClient; @@ -33,23 +29,22 @@ import static org.assertj.core.api.Assertions.assertThat; /** - * Example tests using {@link WebTestClient} to connect to {@link MockMvc} as - * the "mock" server, i.e. without running an HTTP server. + * GraphQL requests via {@link WebTestClient} connecting to {@link MockMvc}. */ @SpringBootTest @AutoConfigureMockMvc -public class MockWebTestClientTests { +public class MockMvcGraphQLTests { + + private GraphQLTester graphQLTester; - private WebTestClient client; @BeforeEach public void setUp(@Autowired MockMvc mockMvc) { - this.client = MockMvcWebTestClient.bindTo(mockMvc) - .baseUrl("/graphql") - .defaultHeaders(headers -> headers.setContentType(MediaType.APPLICATION_JSON)) - .build(); + WebTestClient client = MockMvcWebTestClient.bindTo(mockMvc).baseUrl("/graphql").build(); + this.graphQLTester = GraphQLTester.create(client); } + @Test void jsonPath() { String query = "{" + @@ -60,13 +55,12 @@ void jsonPath() { " }" + "}"; - this.client.post().bodyValue(JsonRequest.create(query)) - .exchange() - .expectStatus().isOk() - .expectBody().jsonPath("$.data.project.releases[*].version") - .value((Consumer>) versions -> { - assertThat(versions).hasSizeGreaterThan(1); - }); + this.graphQLTester.query(query) + .execute() + .path("project.releases[*].version") + .entityList(String.class) + .hasSizeGreaterThan(1); + } @Test @@ -77,18 +71,10 @@ void jsonContent() { " }" + "}"; - String expectedJson = "{" + - " \"data\":{" + - " \"project\":{" + - " \"repositoryUrl\":\"http://github.com/spring-projects/spring-framework\"" + - " }" + - " }" + - "}"; - - this.client.post().bodyValue(JsonRequest.create(query)) - .exchange() - .expectStatus().isOk() - .expectBody().json(expectedJson); + this.graphQLTester.query(query) + .execute() + .path("project") + .matchesJson("{\"repositoryUrl\":\"http://github.com/spring-projects/spring-framework\"}"); } @Test @@ -101,14 +87,11 @@ void decodedResponse() { " }" + "}"; - this.client.post().bodyValue(JsonRequest.create(query)) - .exchange() - .expectStatus().isOk() - .expectBody(new ParameterizedTypeReference>() {}) - .consumeWith(exchangeResult -> { - Project project = exchangeResult.getResponseBody().getDataEntry(); - assertThat(project.getReleases()).hasSizeGreaterThan(1); - }); + this.graphQLTester.query(query) + .execute() + .path("project") + .entity(Project.class) + .satisfies(project -> assertThat(project.getReleases()).hasSizeGreaterThan(1)); } }