Search in sources :

Example 6 with SubscriptionWebSocket

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

the class SubscriptionWebSocketTest method testSubscribeUnsubscribeSubscribe.

@Test
void testSubscribeUnsubscribeSubscribe() throws IOException {
    SubscriptionWebSocket endpoint = SubscriptionWebSocket.builder().executorService(executorService).elide(elide).build();
    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));
    Complete complete = Complete.builder().id("1").build();
    endpoint.onMessage(session, mapper.writeValueAsString(complete));
    endpoint.onMessage(session, mapper.writeValueAsString(subscribe));
    List<String> expected = List.of("{\"type\":\"connection_ack\"}", "{\"type\":\"complete\",\"id\":\"1\"}", "{\"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) Complete(com.yahoo.elide.graphql.subscriptions.websocket.protocol.Complete) 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 7 with SubscriptionWebSocket

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

the class SubscriptionWebSocketTest method testConnectionTimeout.

@Test
void testConnectionTimeout() throws Exception {
    SubscriptionWebSocket endpoint = SubscriptionWebSocket.builder().executorService(executorService).connectTimeoutMs(0).elide(elide).build();
    endpoint.onOpen(session);
    ArgumentCaptor<CloseReason> closeReason = ArgumentCaptor.forClass(CloseReason.class);
    verify(session, timeout(1000).times(1)).close(closeReason.capture());
    assertEquals(CONNECTION_TIMEOUT, closeReason.getValue());
}
Also used : SubscriptionWebSocket(com.yahoo.elide.graphql.subscriptions.websocket.SubscriptionWebSocket) CloseReason(javax.websocket.CloseReason) Test(org.junit.jupiter.api.Test) GraphQLTest(com.yahoo.elide.graphql.GraphQLTest)

Example 8 with SubscriptionWebSocket

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

the class SubscriptionWebSocketTest method testRootSubscription.

@Test
void testRootSubscription() throws IOException {
    SubscriptionWebSocket endpoint = SubscriptionWebSocket.builder().executorService(executorService).elide(elide).build();
    Book book1 = new Book();
    book1.setTitle("Book 1");
    book1.setId(1);
    Book book2 = new Book();
    book2.setTitle("Book 2");
    book2.setId(2);
    when(dataStoreTransaction.loadObjects(any(), any())).thenReturn(new DataStoreIterableBuilder(List.of(book1, book2)).build());
    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\":{\"book\":{\"id\":\"1\",\"title\":\"Book 1\"}}}}", "{\"type\":\"next\",\"id\":\"1\",\"payload\":{\"data\":{\"book\":{\"id\":\"2\",\"title\":\"Book 2\"}}}}", "{\"type\":\"complete\",\"id\":\"1\"}");
    ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class);
    verify(remote, times(4)).sendText(message.capture());
    assertEquals(expected, message.getAllValues());
}
Also used : SubscriptionWebSocket(com.yahoo.elide.graphql.subscriptions.websocket.SubscriptionWebSocket) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Book(example.Book) 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 9 with SubscriptionWebSocket

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

the class SubscriptionWebSocketTest method testConnectionSetupAndTeardown.

@Test
void testConnectionSetupAndTeardown() throws IOException {
    SubscriptionWebSocket endpoint = SubscriptionWebSocket.builder().executorService(executorService).elide(elide).build();
    ConnectionInit init = new ConnectionInit();
    endpoint.onOpen(session);
    endpoint.onMessage(session, mapper.writeValueAsString(init));
    ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class);
    endpoint.onClose(session);
    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(NORMAL_CLOSE, 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)

Example 10 with SubscriptionWebSocket

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

the class SubscriptionWebSocketTest method testInvalidType.

@Test
void testInvalidType() throws IOException {
    SubscriptionWebSocket endpoint = SubscriptionWebSocket.builder().executorService(executorService).elide(elide).build();
    String invalid = "{ \"type\": \"foo\", \"id\": 123 }";
    endpoint.onOpen(session);
    endpoint.onMessage(session, invalid);
    verify(remote, never()).sendText(any());
    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) 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