Search in sources :

Example 11 with GraphQlMessage

use of org.springframework.graphql.web.support.GraphQlMessage in project spring-graphql by spring-projects.

the class GraphQlWebSocketHandlerTests method connectionInitHandling.

@Test
void connectionInitHandling() throws Exception {
    WebSocketInterceptor interceptor = new WebSocketInterceptor() {

        @Override
        public Mono<Object> handleConnectionInitialization(String sessionId, Map<String, Object> payload) {
            Object value = payload.get("key");
            return Mono.just(Collections.singletonMap("key", value + " acknowledged"));
        }
    };
    handle(initWebSocketHandler(interceptor), new TextMessage("{\"type\":\"connection_init\",\"payload\":{\"key\":\"A\"}}"));
    StepVerifier.create(session.getOutput()).consumeNextWith((webSocketMessage) -> {
        GraphQlMessage message = decode(webSocketMessage);
        assertThat(message.resolvedType()).isEqualTo(GraphQlMessageType.CONNECTION_ACK);
        assertThat(message.<Map<String, Object>>getPayload()).containsEntry("key", "A acknowledged");
    }).then(// Complete output Flux
    this.session::close).expectComplete().verify(TIMEOUT);
}
Also used : GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) StepVerifier(reactor.test.StepVerifier) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GraphQlSetup(org.springframework.graphql.GraphQlSetup) CloseStatus(org.springframework.web.socket.CloseStatus) ArrayList(java.util.ArrayList) TextMessage(org.springframework.web.socket.TextMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) Assertions.as(org.assertj.core.api.Assertions.as) Duration(java.time.Duration) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) WebInterceptor(org.springframework.graphql.web.WebInterceptor) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) HttpHeaders(org.springframework.http.HttpHeaders) ConsumeOneAndNeverCompleteInterceptor(org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) WebSocketHandlerTestSupport(org.springframework.graphql.web.WebSocketHandlerTestSupport) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) GraphQlMessageType(org.springframework.graphql.web.support.GraphQlMessageType) HttpInputMessage(org.springframework.http.HttpInputMessage) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Collections(java.util.Collections) WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) InputStream(java.io.InputStream) GenericHttpMessageConverter(org.springframework.http.converter.GenericHttpMessageConverter) WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) Map(java.util.Map) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 12 with GraphQlMessage

use of org.springframework.graphql.web.support.GraphQlMessage in project spring-graphql by spring-projects.

the class GraphQlWebSocketHandlerTests method subscriptionExists.

@Test
void subscriptionExists() throws Exception {
    handle(initWebSocketHandler(new ConsumeOneAndNeverCompleteInterceptor()), new TextMessage("{\"type\":\"connection_init\"}"), new TextMessage(BOOK_SUBSCRIPTION), new TextMessage(BOOK_SUBSCRIPTION));
    // Collect messages until session closed
    List<GraphQlMessage> messages = new ArrayList<>();
    this.session.getOutput().subscribe((message) -> messages.add(decode(message)));
    StepVerifier.create(this.session.closeStatus()).expectNext(new CloseStatus(4409, "Subscriber for " + SUBSCRIPTION_ID + " already exists")).expectComplete().verify(TIMEOUT);
    assertThat(messages.size()).isEqualTo(2);
    assertThat(messages.get(0).resolvedType()).isEqualTo(GraphQlMessageType.CONNECTION_ACK);
    assertThat(messages.get(1).resolvedType()).isEqualTo(GraphQlMessageType.NEXT);
}
Also used : ConsumeOneAndNeverCompleteInterceptor(org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor) GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) ArrayList(java.util.ArrayList) CloseStatus(org.springframework.web.socket.CloseStatus) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Aggregations

GraphQlMessage (org.springframework.graphql.web.support.GraphQlMessage)12 Map (java.util.Map)8 Test (org.junit.jupiter.api.Test)8 WebSocketInterceptor (org.springframework.graphql.web.WebSocketInterceptor)8 Mono (reactor.core.publisher.Mono)8 Duration (java.time.Duration)7 ArrayList (java.util.ArrayList)7 Collections (java.util.Collections)7 List (java.util.List)7 ConsumeOneAndNeverCompleteInterceptor (org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor)7 WebGraphQlHandler (org.springframework.graphql.web.WebGraphQlHandler)7 Flux (reactor.core.publisher.Flux)7 GraphQlMessageType (org.springframework.graphql.web.support.GraphQlMessageType)6 CloseStatus (org.springframework.web.socket.CloseStatus)6 TextMessage (org.springframework.web.socket.TextMessage)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 BiConsumer (java.util.function.BiConsumer)5