From 8b5e2a3cba520c20b1968db22d07cbf4850afe36 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Fri, 15 Apr 2022 08:06:59 +0100 Subject: [PATCH] Update WebFlux WebSocket sample tests - combine existing tests into one class - add integration tests --- samples/webflux-websocket/README.md | 5 +- ...bFluxWebSocketSampleIntegrationTests.java} | 49 ++++++++++++++++--- .../graphql/WebFluxWebSocketSampleTests.java | 33 ++++++++++++- 3 files changed, 76 insertions(+), 11 deletions(-) rename samples/webflux-websocket/src/test/java/io/spring/sample/graphql/{WebFluxWebSocketSampleSubscriptionTests.java => WebFluxWebSocketSampleIntegrationTests.java} (58%) diff --git a/samples/webflux-websocket/README.md b/samples/webflux-websocket/README.md index 7d116e1e..2ce6addc 100644 --- a/samples/webflux-websocket/README.md +++ b/samples/webflux-websocket/README.md @@ -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. \ No newline at end of file diff --git a/samples/webflux-websocket/src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleSubscriptionTests.java b/samples/webflux-websocket/src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleIntegrationTests.java similarity index 58% rename from samples/webflux-websocket/src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleSubscriptionTests.java rename to samples/webflux-websocket/src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleIntegrationTests.java index 6b839c9c..3f60f381 100644 --- a/samples/webflux-websocket/src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleSubscriptionTests.java +++ b/samples/webflux-websocket/src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleIntegrationTests.java @@ -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 result = this.graphQlTester.document("subscription { greetings }") diff --git a/samples/webflux-websocket/src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleTests.java b/samples/webflux-websocket/src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleTests.java index c7b597c3..54c82ae1 100644 --- a/samples/webflux-websocket/src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleTests.java +++ b/samples/webflux-websocket/src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleTests.java @@ -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 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 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(); + } + }