HelloResponseHandler implements ResponseHandler { @Override public void onSuccess( Map<String,Value> metadata ) { try { if ( BoltProtocolV3.VERSION.equals( protocolVersion ) ) { setServerVersion( channel, extractNeo4jServerVersion( metadata ) ); } else { setServerVersion( channel, fromBoltProtocolVersion( protocolVersion ) ); } String connectionId = extractConnectionId( metadata ); setConnectionId( channel, connectionId ); connectionInitializedPromise.setSuccess(); } catch ( Throwable error ) { onFailure( error ); throw error; } } HelloResponseHandler( ChannelPromise connectionInitializedPromise, BoltProtocolVersion protocolVersion ); @Override void onSuccess( Map<String,Value> metadata ); @Override void onFailure( Throwable error ); @Override void onRecord( Value[] fields );  }
@Test void shouldSetServerVersionOnChannel() { ChannelPromise channelPromise = channel.newPromise(); HelloResponseHandler handler = new HelloResponseHandler( channelPromise, BoltProtocolV3.VERSION ); Map<String,Value> metadata = metadata( anyServerVersion(), "bolt-1" ); handler.onSuccess( metadata ); assertTrue( channelPromise.isSuccess() ); assertEquals( anyServerVersion(), serverVersion( channel ) ); }
@Test void shouldThrowWhenServerVersionNotReturned() { ChannelPromise channelPromise = channel.newPromise(); HelloResponseHandler handler = new HelloResponseHandler( channelPromise, BoltProtocolV3.VERSION ); Map<String,Value> metadata = metadata( null, "bolt-1" ); assertThrows( UntrustedServerException.class, () -> handler.onSuccess( metadata ) ); assertFalse( channelPromise.isSuccess() ); assertTrue( channel.closeFuture().isDone() ); }
@Test void shouldThrowWhenServerVersionIsNull() { ChannelPromise channelPromise = channel.newPromise(); HelloResponseHandler handler = new HelloResponseHandler( channelPromise, BoltProtocolV3.VERSION ); Map<String,Value> metadata = metadata( Values.NULL, "bolt-x" ); assertThrows( UntrustedServerException.class, () -> handler.onSuccess( metadata ) ); assertFalse( channelPromise.isSuccess() ); assertTrue( channel.closeFuture().isDone() ); }
@Test void shouldThrowWhenServerVersionCantBeParsed() { ChannelPromise channelPromise = channel.newPromise(); HelloResponseHandler handler = new HelloResponseHandler( channelPromise, BoltProtocolV3.VERSION ); Map<String,Value> metadata = metadata( "WrongServerVersion", "bolt-x" ); assertThrows( IllegalArgumentException.class, () -> handler.onSuccess( metadata ) ); assertFalse( channelPromise.isSuccess() ); assertTrue( channel.closeFuture().isDone() ); }
@Test void shouldUseProtocolVersionForServerVersionWhenConnectedWithBoltV4() { ChannelPromise channelPromise = channel.newPromise(); HelloResponseHandler handler = new HelloResponseHandler( channelPromise, BoltProtocolV4.VERSION ); Map<String,Value> metadata = metadata( ServerVersion.vInDev, "bolt-1" ); handler.onSuccess( metadata ); assertTrue( channelPromise.isSuccess() ); assertEquals( ServerVersion.v4_0_0, serverVersion( channel ) ); }
@Test void shouldUseProtocolVersionForServerVersionWhenConnectedWithBoltV41() { ChannelPromise channelPromise = channel.newPromise(); HelloResponseHandler handler = new HelloResponseHandler( channelPromise, BoltProtocolV41.VERSION ); Map<String,Value> metadata = metadata( ServerVersion.vInDev, "bolt-1" ); handler.onSuccess( metadata ); assertTrue( channelPromise.isSuccess() ); assertEquals( ServerVersion.v4_1_0, serverVersion( channel ) ); }
@Test void shouldSetConnectionIdOnChannel() { ChannelPromise channelPromise = channel.newPromise(); HelloResponseHandler handler = new HelloResponseHandler( channelPromise, BoltProtocolV3.VERSION ); Map<String,Value> metadata = metadata( anyServerVersion(), "bolt-42" ); handler.onSuccess( metadata ); assertTrue( channelPromise.isSuccess() ); assertEquals( "bolt-42", connectionId( channel ) ); }
@Test void shouldThrowWhenConnectionIdNotReturned() { ChannelPromise channelPromise = channel.newPromise(); HelloResponseHandler handler = new HelloResponseHandler( channelPromise, BoltProtocolV3.VERSION ); Map<String,Value> metadata = metadata( anyServerVersion(), null ); assertThrows( IllegalStateException.class, () -> handler.onSuccess( metadata ) ); assertFalse( channelPromise.isSuccess() ); assertTrue( channel.closeFuture().isDone() ); }
@Test void shouldThrowWhenConnectionIdIsNull() { ChannelPromise channelPromise = channel.newPromise(); HelloResponseHandler handler = new HelloResponseHandler( channelPromise, BoltProtocolV3.VERSION ); Map<String,Value> metadata = metadata( anyServerVersion(), Values.NULL ); assertThrows( IllegalStateException.class, () -> handler.onSuccess( metadata ) ); assertFalse( channelPromise.isSuccess() ); assertTrue( channel.closeFuture().isDone() ); }