Search in sources :

Example 11 with Ready

use of com.datastax.oss.protocol.internal.response.Ready in project java-driver by datastax.

the class ProtocolInitHandlerTest method should_check_cluster_name_if_provided.

@Test
public void should_check_cluster_name_if_provided() {
    channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, "expectedClusterName", END_POINT, DriverChannelOptions.DEFAULT, heartbeatHandler, false));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
    Frame requestFrame = readOutboundFrame();
    writeInboundFrame(requestFrame, new Ready());
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Query.class);
    Query query = (Query) requestFrame.message;
    assertThat(query.query).isEqualTo("SELECT cluster_name FROM system.local");
    assertThat(connectFuture).isNotDone();
    writeInboundFrame(requestFrame, TestResponses.clusterNameResponse("expectedClusterName"));
    assertThat(connectFuture).isSuccess();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Ready(com.datastax.oss.protocol.internal.response.Ready) Frame(com.datastax.oss.protocol.internal.Frame) Query(com.datastax.oss.protocol.internal.request.Query) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 12 with Ready

use of com.datastax.oss.protocol.internal.response.Ready in project java-driver by datastax.

the class ProtocolInitHandlerTest method should_fail_to_initialize_if_cluster_name_does_not_match.

@Test
public void should_fail_to_initialize_if_cluster_name_does_not_match() throws Throwable {
    channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, "expectedClusterName", END_POINT, DriverChannelOptions.DEFAULT, heartbeatHandler, false));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
    writeInboundFrame(readOutboundFrame(), new Ready());
    writeInboundFrame(readOutboundFrame(), TestResponses.clusterNameResponse("differentClusterName"));
    assertThat(connectFuture).isFailed(e -> assertThat(e).isInstanceOf(ClusterNameMismatchException.class).hasMessageContaining(String.format("Node %s reports cluster name 'differentClusterName' that doesn't match our cluster name 'expectedClusterName'.", END_POINT)));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Ready(com.datastax.oss.protocol.internal.response.Ready) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 13 with Ready

use of com.datastax.oss.protocol.internal.response.Ready in project java-driver by datastax.

the class ProtocolInitHandlerTest method should_initialize.

@Test
public void should_initialize() {
    channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, null, END_POINT, DriverChannelOptions.DEFAULT, heartbeatHandler, false));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
    // 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();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Ready(com.datastax.oss.protocol.internal.response.Ready) Frame(com.datastax.oss.protocol.internal.Frame) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 14 with Ready

use of com.datastax.oss.protocol.internal.response.Ready in project java-driver by datastax.

the class ProtocolInitHandlerTest method should_invoke_auth_provider_when_server_does_not_send_challenge.

@Test
public void should_invoke_auth_provider_when_server_does_not_send_challenge() {
    channel.pipeline().addLast(ChannelFactory.INIT_HANDLER_NAME, new ProtocolInitHandler(internalDriverContext, DefaultProtocolVersion.V4, null, END_POINT, DriverChannelOptions.DEFAULT, heartbeatHandler, false));
    AuthProvider authProvider = mock(AuthProvider.class);
    when(internalDriverContext.getAuthProvider()).thenReturn(Optional.of(authProvider));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("localhost", 9042));
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Startup.class);
    // Simulate a READY response, the provider should be notified
    writeInboundFrame(buildInboundFrame(requestFrame, new Ready()));
    verify(authProvider).onMissingChallenge(END_POINT);
    // Since our mock does nothing, init should proceed normally
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Query.class);
    writeInboundFrame(requestFrame, TestResponses.clusterNameResponse("someClusterName"));
    assertThat(connectFuture).isSuccess();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Ready(com.datastax.oss.protocol.internal.response.Ready) Frame(com.datastax.oss.protocol.internal.Frame) InetSocketAddress(java.net.InetSocketAddress) AuthProvider(com.datastax.oss.driver.api.core.auth.AuthProvider) Test(org.junit.Test)

Example 15 with Ready

use of com.datastax.oss.protocol.internal.response.Ready 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();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Ready(com.datastax.oss.protocol.internal.response.Ready) Frame(com.datastax.oss.protocol.internal.Frame) Query(com.datastax.oss.protocol.internal.request.Query) Register(com.datastax.oss.protocol.internal.request.Register) InetSocketAddress(java.net.InetSocketAddress) SetKeyspace(com.datastax.oss.protocol.internal.response.result.SetKeyspace) Test(org.junit.Test)

Aggregations

Ready (com.datastax.oss.protocol.internal.response.Ready)16 Test (org.junit.Test)15 Frame (com.datastax.oss.protocol.internal.Frame)12 ChannelFuture (io.netty.channel.ChannelFuture)10 InetSocketAddress (java.net.InetSocketAddress)10 Query (com.datastax.oss.protocol.internal.request.Query)4 Register (com.datastax.oss.protocol.internal.request.Register)2 Error (com.datastax.oss.protocol.internal.response.Error)2 SetKeyspace (com.datastax.oss.protocol.internal.response.result.SetKeyspace)2 AuthProvider (com.datastax.oss.driver.api.core.auth.AuthProvider)1 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)1 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)1 List (java.util.List)1