Search in sources :

Example 6 with Subscribe

use of com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe 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 Subscribe

use of com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe 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 8 with Subscribe

use of com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe 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 9 with Subscribe

use of com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe 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)

Aggregations

Subscribe (com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe)9 GraphQLTest (com.yahoo.elide.graphql.GraphQLTest)7 SubscriptionWebSocket (com.yahoo.elide.graphql.subscriptions.websocket.SubscriptionWebSocket)7 Test (org.junit.jupiter.api.Test)7 ConnectionInit (com.yahoo.elide.graphql.subscriptions.websocket.protocol.ConnectionInit)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)2 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)2 Complete (com.yahoo.elide.graphql.subscriptions.websocket.protocol.Complete)2 MessageType (com.yahoo.elide.graphql.subscriptions.websocket.protocol.MessageType)2 Book (example.Book)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Error (com.yahoo.elide.graphql.subscriptions.websocket.protocol.Error)1 Next (com.yahoo.elide.graphql.subscriptions.websocket.protocol.Next)1 GraphQLError (graphql.GraphQLError)1 CloseReason (javax.websocket.CloseReason)1 OnError (javax.websocket.OnError)1 OnMessage (javax.websocket.OnMessage)1