Search in sources :

Example 26 with Error

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

the class ChannelFactoryProtocolNegotiationTest method should_negotiate_if_version_not_specified_and_server_supports_legacy.

@Test
@UseDataProvider("unsupportedProtocolCodes")
public void should_negotiate_if_version_not_specified_and_server_supports_legacy(int errorCode) {
    // Given
    when(defaultProfile.isDefined(DefaultDriverOption.PROTOCOL_VERSION)).thenReturn(false);
    when(protocolVersionRegistry.highestNonBeta()).thenReturn(DefaultProtocolVersion.V4);
    when(protocolVersionRegistry.downgrade(DefaultProtocolVersion.V4)).thenReturn(Optional.of(DefaultProtocolVersion.V3));
    ChannelFactory factory = newChannelFactory();
    // When
    CompletionStage<DriverChannel> channelFuture = factory.connect(SERVER_ADDRESS, DriverChannelOptions.DEFAULT, NoopNodeMetricUpdater.INSTANCE);
    Frame requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Options.class);
    writeInboundFrame(requestFrame, TestResponses.supportedResponse("mock_key", "mock_value"));
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.protocolVersion).isEqualTo(DefaultProtocolVersion.V4.getCode());
    // Server does not support v4
    writeInboundFrame(requestFrame, new Error(errorCode, "Invalid or unsupported protocol version"));
    // Then
    // Factory should initialize a new connection, that retries with the lower version
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.message).isInstanceOf(Options.class);
    writeInboundFrame(requestFrame, TestResponses.supportedResponse("mock_key", "mock_value"));
    requestFrame = readOutboundFrame();
    assertThat(requestFrame.protocolVersion).isEqualTo(DefaultProtocolVersion.V3.getCode());
    writeInboundFrame(requestFrame, new Ready());
    requestFrame = readOutboundFrame();
    writeInboundFrame(requestFrame, TestResponses.clusterNameResponse("mockClusterName"));
    assertThatStage(channelFuture).isSuccess(channel -> assertThat(channel.getClusterName()).isEqualTo("mockClusterName"));
    assertThat(factory.protocolVersion).isEqualTo(DefaultProtocolVersion.V3);
}
Also used : Ready(com.datastax.oss.protocol.internal.response.Ready) Frame(com.datastax.oss.protocol.internal.Frame) Error(com.datastax.oss.protocol.internal.response.Error) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 27 with Error

use of com.datastax.oss.protocol.internal.response.Error 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)));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Frame(com.datastax.oss.protocol.internal.Frame) Authenticate(com.datastax.oss.protocol.internal.response.Authenticate) InetSocketAddress(java.net.InetSocketAddress) Error(com.datastax.oss.protocol.internal.response.Error) AuthProvider(com.datastax.oss.driver.api.core.auth.AuthProvider) Test(org.junit.Test)

Aggregations

Error (com.datastax.oss.protocol.internal.response.Error)27 Test (org.junit.Test)24 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)15 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)8 Node (com.datastax.oss.driver.api.core.metadata.Node)8 SpeculativeExecutionPolicy (com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy)8 CapturedTimeout (com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout)8 Frame (com.datastax.oss.protocol.internal.Frame)8 AsyncGraphResultSet (com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)4 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)4 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)4 AllNodesFailedException (com.datastax.oss.driver.api.core.AllNodesFailedException)4 ServerError (com.datastax.oss.driver.api.core.servererrors.ServerError)4 PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)4 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)4 List (java.util.List)4 UnsupportedProtocolVersionException (com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException)3 InetSocketAddress (java.net.InetSocketAddress)3 ContinuousAsyncResultSet (com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet)2 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)2