use of com.datastax.oss.protocol.internal.Frame in project java-driver by datastax.
the class ProtocolInitHandlerTest method should_initialize_with_keyspace_and_events.
@Test
public void should_initialize_with_keyspace_and_events() {
List<String> eventTypes = ImmutableList.of("foo", "bar");
EventCallback eventCallback = mock(EventCallback.class);
DriverChannelOptions driverChannelOptions = DriverChannelOptions.builder().withKeyspace(CqlIdentifier.fromCql("ks")).withEvents(eventTypes, eventCallback).build();
channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, null, END_POINT, driverChannelOptions, heartbeatHandler, false));
ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
writeInboundFrame(readOutboundFrame(), new Ready());
writeInboundFrame(readOutboundFrame(), TestResponses.clusterNameResponse("someClusterName"));
Frame requestFrame = readOutboundFrame();
assertThat(requestFrame.message).isInstanceOf(Query.class);
assertThat(((Query) requestFrame.message).query).isEqualTo("USE \"ks\"");
writeInboundFrame(requestFrame, new SetKeyspace("ks"));
requestFrame = readOutboundFrame();
assertThat(requestFrame.message).isInstanceOf(Register.class);
assertThat(((Register) requestFrame.message).eventTypes).containsExactly("foo", "bar");
writeInboundFrame(requestFrame, new Ready());
assertThat(connectFuture).isSuccess();
}
use of com.datastax.oss.protocol.internal.Frame in project java-driver by datastax.
the class ProtocolInitHandlerTest method should_add_heartbeat_handler_to_pipeline_on_success.
@Test
public void should_add_heartbeat_handler_to_pipeline_on_success() {
ProtocolInitHandler protocolInitHandler = new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, null, END_POINT, DriverChannelOptions.DEFAULT, heartbeatHandler, false);
channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, protocolInitHandler);
ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
// heartbeat should initially not be in pipeline
assertThat(channel.pipeline().get(ChannelFactory.HEARTBEAT_HANDLER_NAME)).isNull();
// It should send a STARTUP message
Frame requestFrame = readOutboundFrame();
assertThat(requestFrame.message).isInstanceOf(Startup.class);
assertThat(connectFuture).isNotDone();
// Simulate a READY response
writeInboundFrame(buildInboundFrame(requestFrame, new Ready()));
// Simulate the cluster name check
requestFrame = readOutboundFrame();
assertThat(requestFrame.message).isInstanceOf(Query.class);
writeInboundFrame(requestFrame, TestResponses.clusterNameResponse("someClusterName"));
// Init should complete
assertThat(connectFuture).isSuccess();
// should have added heartbeat handler to pipeline.
assertThat(channel.pipeline().get(ChannelFactory.HEARTBEAT_HANDLER_NAME)).isEqualTo(heartbeatHandler);
// should have removed itself from pipeline.
assertThat(channel.pipeline().last()).isNotEqualTo(protocolInitHandler);
}
use of com.datastax.oss.protocol.internal.Frame in project java-driver by datastax.
the class ProtocolInitHandlerTest method should_fail_to_initialize_if_server_sends_auth_error.
@Test
public void should_fail_to_initialize_if_server_sends_auth_error() throws Throwable {
channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, null, END_POINT, DriverChannelOptions.DEFAULT, heartbeatHandler, false));
String serverAuthenticator = "mockServerAuthenticator";
AuthProvider authProvider = mock(AuthProvider.class);
MockAuthenticator authenticator = new MockAuthenticator();
when(authProvider.newAuthenticator(END_POINT, serverAuthenticator)).thenReturn(authenticator);
when(internalDriverContext.getAuthProvider()).thenReturn(Optional.of(authProvider));
ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
Frame requestFrame = readOutboundFrame();
assertThat(requestFrame.message).isInstanceOf(Startup.class);
assertThat(connectFuture).isNotDone();
writeInboundFrame(requestFrame, new Authenticate("mockServerAuthenticator"));
requestFrame = readOutboundFrame();
assertThat(requestFrame.message).isInstanceOf(AuthResponse.class);
assertThat(connectFuture).isNotDone();
writeInboundFrame(requestFrame, new Error(ProtocolConstants.ErrorCode.AUTH_ERROR, "mock error"));
assertThat(connectFuture).isFailed(e -> assertThat(e).isInstanceOf(AuthenticationException.class).hasMessage(String.format("Authentication error on node %s: server replied with 'mock error' to AuthResponse request", END_POINT)));
}
use of com.datastax.oss.protocol.internal.Frame in project java-driver by datastax.
the class SegmentToFrameDecoderTest method should_decode_self_contained.
@Test
public void should_decode_self_contained() {
ByteBuf payload = UnpooledByteBufAllocator.DEFAULT.buffer();
payload.writeBytes(encodeFrame(Void.INSTANCE));
payload.writeBytes(encodeFrame(new AuthResponse(Bytes.fromHexString("0xabcdef"))));
channel.writeInbound(new Segment<>(payload, true));
Frame frame1 = channel.readInbound();
assertThat(frame1.message).isInstanceOf(Void.class);
Frame frame2 = channel.readInbound();
assertThat(frame2.message).isInstanceOf(AuthResponse.class);
}
Aggregations