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());
}
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());
}
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());
}
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());
}
Aggregations