Skip to content

Commit

Permalink
Update samples to use GraphQLTester
Browse files Browse the repository at this point in the history
See gh-43
  • Loading branch information
rstoyanchev committed Apr 15, 2021
1 parent efb72c7 commit 0d52bfc
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 154 deletions.
3 changes: 3 additions & 0 deletions samples/webflux-websocket/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
1 change: 1 addition & 0 deletions samples/webmvc-http/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,36 @@
*/
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;

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 = "{" +
Expand All @@ -60,13 +55,12 @@ void jsonPath() {
" }" +
"}";

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
Expand All @@ -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
Expand All @@ -101,14 +87,11 @@ void decodedResponse() {
" }" +
"}";

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

}

0 comments on commit 0d52bfc

Please sign in to comment.