Search in sources :

Example 11 with SubscriptionWebSocket

use of com.yahoo.elide.graphql.subscriptions.websocket.SubscriptionWebSocket in project elide by yahoo.

the class SubscriptionWebSocketTest method testSubscribeBeforeInit.

@Test
void testSubscribeBeforeInit() throws IOException {
    SubscriptionWebSocket endpoint = SubscriptionWebSocket.builder().executorService(executorService).elide(elide).build();
    endpoint.onOpen(session);
    Subscribe subscribe = Subscribe.builder().id("1").payload(Subscribe.Payload.builder().query("subscription {book(topic: ADDED) {id title}}").build()).build();
    endpoint.onMessage(session, mapper.writeValueAsString(subscribe));
    verify(remote, never()).sendText(any());
    ArgumentCaptor<CloseReason> closeReason = ArgumentCaptor.forClass(CloseReason.class);
    verify(session, times(1)).close(closeReason.capture());
    assertEquals(UNAUTHORIZED, closeReason.getValue());
}
Also used : SubscriptionWebSocket(com.yahoo.elide.graphql.subscriptions.websocket.SubscriptionWebSocket) CloseReason(javax.websocket.CloseReason) Subscribe(com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe) Test(org.junit.jupiter.api.Test) GraphQLTest(com.yahoo.elide.graphql.GraphQLTest)

Example 12 with SubscriptionWebSocket

use of com.yahoo.elide.graphql.subscriptions.websocket.SubscriptionWebSocket in project elide by yahoo.

the class SubscriptionWebSocketTest method testErrorPriorToStream.

@Test
void testErrorPriorToStream() throws IOException {
    SubscriptionWebSocket endpoint = SubscriptionWebSocket.builder().executorService(executorService).elide(elide).build();
    reset(dataStoreTransaction);
    when(dataStoreTransaction.loadObjects(any(), any())).thenThrow(new BadRequestException("Bad Request"));
    ConnectionInit init = new ConnectionInit();
    endpoint.onOpen(session);
    endpoint.onMessage(session, mapper.writeValueAsString(init));
    Subscribe subscribe = Subscribe.builder().id("1").payload(Subscribe.Payload.builder().query("subscription {book(topic: ADDED) {id title}}").build()).build();
    endpoint.onMessage(session, mapper.writeValueAsString(subscribe));
    List<String> expected = List.of("{\"type\":\"connection_ack\"}", "{\"type\":\"next\",\"id\":\"1\",\"payload\":{\"data\":null,\"errors\":[{\"message\":\"Exception while fetching data (/book) : Bad Request\",\"locations\":[{\"line\":1,\"column\":15}],\"path\":[\"book\"],\"extensions\":{\"classification\":\"DataFetchingException\"}}]}}", "{\"type\":\"complete\",\"id\":\"1\"}");
    ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class);
    verify(remote, times(3)).sendText(message.capture());
    assertEquals(expected, message.getAllValues());
}
Also used : SubscriptionWebSocket(com.yahoo.elide.graphql.subscriptions.websocket.SubscriptionWebSocket) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) ConnectionInit(com.yahoo.elide.graphql.subscriptions.websocket.protocol.ConnectionInit) Subscribe(com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe) Test(org.junit.jupiter.api.Test) GraphQLTest(com.yahoo.elide.graphql.GraphQLTest)

Example 13 with SubscriptionWebSocket

use of com.yahoo.elide.graphql.subscriptions.websocket.SubscriptionWebSocket in project elide by yahoo.

the class SubscriptionWebSocketTest method testInvalidJson.

@Test
void testInvalidJson() throws IOException {
    SubscriptionWebSocket endpoint = SubscriptionWebSocket.builder().executorService(executorService).elide(elide).build();
    // Missing payload field
    String invalid = "{ \"type\": \"subscribe\"}";
    ConnectionInit init = new ConnectionInit();
    endpoint.onOpen(session);
    endpoint.onMessage(session, mapper.writeValueAsString(init));
    endpoint.onMessage(session, invalid);
    ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class);
    verify(remote, times(1)).sendText(message.capture());
    assertEquals("{\"type\":\"connection_ack\"}", message.getAllValues().get(0));
    ArgumentCaptor<CloseReason> closeReason = ArgumentCaptor.forClass(CloseReason.class);
    verify(session, times(1)).close(closeReason.capture());
    assertEquals(INVALID_MESSAGE, closeReason.getValue());
}
Also used : SubscriptionWebSocket(com.yahoo.elide.graphql.subscriptions.websocket.SubscriptionWebSocket) CloseReason(javax.websocket.CloseReason) ConnectionInit(com.yahoo.elide.graphql.subscriptions.websocket.protocol.ConnectionInit) Test(org.junit.jupiter.api.Test) GraphQLTest(com.yahoo.elide.graphql.GraphQLTest)

Aggregations

GraphQLTest (com.yahoo.elide.graphql.GraphQLTest)13 SubscriptionWebSocket (com.yahoo.elide.graphql.subscriptions.websocket.SubscriptionWebSocket)13 Test (org.junit.jupiter.api.Test)13 ConnectionInit (com.yahoo.elide.graphql.subscriptions.websocket.protocol.ConnectionInit)9 Subscribe (com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe)7 CloseReason (javax.websocket.CloseReason)7 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)2 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)2 Book (example.Book)2 Complete (com.yahoo.elide.graphql.subscriptions.websocket.protocol.Complete)1