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