Update WebFlux WebSocket sample tests

- combine existing tests into one class
- add integration tests
This commit is contained in:
rstoyanchev
2022-04-15 08:06:59 +01:00
parent b8c4b25ee6
commit 8b5e2a3cba
3 changed files with 76 additions and 11 deletions

View File

@@ -2,6 +2,7 @@
- [Data Controller](src/main/java/io/spring/sample/graphql/SampleController.java) with reactive methods for queries and subscriptions.
- [WebFilter](src/main/java/io/spring/sample/graphql/ContextWebFilter.java) to insert Reactor `Context` and check it can be accessed in [DataRepository](src/main/java/io/spring/sample/graphql/DataRepository.java).
- Subscription [tests](src/test/java/io/spring/sample/graphql/SubscriptionTests.java) using Reactor's `StepVerifier` to verify the stream.
- [Server tests](src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleTests.java) without a client.
- [Integration tests](src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleIntegrationTests.java) via `WebSocketGraphQlTester`.
GraphiQL does not support subscriptions. There is a basic index.html page that logs subscriptions the console.

View File

@@ -15,26 +15,59 @@
*/
package io.spring.sample.graphql;
import java.net.URI;
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.autoconfigure.graphql.GraphQlTest;
import org.springframework.context.annotation.Import;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.graphql.test.tester.GraphQlTester;
import org.springframework.graphql.test.tester.WebSocketGraphQlTester;
import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient;
/**
* GraphQL over WebSocket subscription tests.
* GraphQL over WebSocket integration tests.
*/
@GraphQlTest(SampleController.class)
@Import(DataRepository.class)
public class WebFluxWebSocketSampleSubscriptionTests {
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class WebFluxWebSocketSampleIntegrationTests {
@LocalServerPort
private int port;
@Value("http://localhost:${local.server.port}${spring.graphql.websocket.path}")
private String baseUrl;
@Autowired
private GraphQlTester graphQlTester;
@BeforeEach
void setUp() {
URI url = URI.create(baseUrl);
this.graphQlTester = WebSocketGraphQlTester.builder(url, new ReactorNettyWebSocketClient()).build();
}
@Test
void greetingMono() {
this.graphQlTester.document("{greetingMono}")
.execute()
.path("greetingMono")
.entity(String.class)
.isEqualTo("Hello!");
}
@Test
void greetingsFlux() {
this.graphQlTester.document("{greetingsFlux}")
.execute()
.path("greetingsFlux")
.entityList(String.class)
.containsExactly("Hi!", "Bonjour!", "Hola!", "Ciao!", "Zdravo!");
}
@Test
void subscriptionWithEntityPath() {
Flux<String> result = this.graphQlTester.document("subscription { greetings }")

View File

@@ -16,6 +16,8 @@
package io.spring.sample.graphql;
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.autoconfigure.graphql.GraphQlTest;
@@ -23,7 +25,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.graphql.test.tester.GraphQlTester;
/**
* GraphQL over WebSocket single response tests.
* GraphQL over WebSocket, server-side tests, i.e. without a client.
*/
@GraphQlTest(SampleController.class)
@Import(DataRepository.class)
@@ -51,4 +53,33 @@ public class WebFluxWebSocketSampleTests {
.containsExactly("Hi!", "Bonjour!", "Hola!", "Ciao!", "Zdravo!");
}
@Test
void subscriptionWithEntityPath() {
Flux<String> result = this.graphQlTester.document("subscription { greetings }")
.executeSubscription()
.toFlux("greetings", String.class);
StepVerifier.create(result)
.expectNext("Hi!")
.expectNext("Bonjour!")
.expectNext("Hola!")
.expectNext("Ciao!")
.expectNext("Zdravo!")
.verifyComplete();
}
@Test
void subscriptionWithResponse() {
Flux<GraphQlTester.Response> result = this.graphQlTester.document("subscription { greetings }")
.executeSubscription()
.toFlux();
StepVerifier.create(result)
.consumeNextWith(response -> response.path("greetings").hasValue())
.consumeNextWith(response -> response.path("greetings").matchesJson("\"Bonjour!\""))
.consumeNextWith(response -> response.path("greetings").matchesJson("\"Hola!\""))
.expectNextCount(2)
.verifyComplete();
}
}