Update samples to use GraphQLTester
See gh-43
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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<String> 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<GraphQLTester.ResponseSpec> 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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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'
|
||||
}
|
||||
|
||||
@@ -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<String, Object> variables;
|
||||
|
||||
|
||||
private JsonRequest(String query, @Nullable String operationName, Map<String, Object> 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<String, Object> 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<String, Object> variables) {
|
||||
return new JsonRequest(query, operationName, variables);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<T> {
|
||||
|
||||
private Map<String, T> data = Collections.emptyMap();
|
||||
|
||||
private Map<String, Map<String, Object>> errors = Collections.emptyMap();
|
||||
|
||||
|
||||
public void setData(@Nullable Map<String, T> data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public T getDataEntry() {
|
||||
return this.data.values().iterator().next();
|
||||
}
|
||||
|
||||
public void setErrors(@Nullable Map<String, Map<String, Object>> errors) {
|
||||
this.errors = errors;
|
||||
}
|
||||
|
||||
public Map<String, Map<String, Object>> getErrors() {
|
||||
return this.errors;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 org.springframework.test.web.servlet.client.MockMvcWebTestClient;
|
||||
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 @@ public class MockWebTestClientTests {
|
||||
" }" +
|
||||
"}";
|
||||
|
||||
this.client.post().bodyValue(JsonRequest.create(query))
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody().jsonPath("$.data.project.releases[*].version")
|
||||
.value((Consumer<List<String>>) versions -> {
|
||||
assertThat(versions).hasSizeGreaterThan(1);
|
||||
});
|
||||
this.graphQLTester.query(query)
|
||||
.execute()
|
||||
.path("project.releases[*].version")
|
||||
.entityList(String.class)
|
||||
.hasSizeGreaterThan(1);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -77,18 +71,10 @@ public class MockWebTestClientTests {
|
||||
" }" +
|
||||
"}";
|
||||
|
||||
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 @@ public class MockWebTestClientTests {
|
||||
" }" +
|
||||
"}";
|
||||
|
||||
this.client.post().bodyValue(JsonRequest.create(query))
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(new ParameterizedTypeReference<JsonResponse<Project>>() {})
|
||||
.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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user