Skip to content

Commit 65d1d42

Browse files
committed
Added unit tests for NettyChannelInitializer
1 parent 9c9e61d commit 65d1d42

File tree

3 files changed

+102
-9
lines changed

3 files changed

+102
-9
lines changed

driver/src/main/java/org/neo4j/driver/internal/async/ChannelConnectedListener.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,11 @@ public void operationComplete( ChannelFuture future )
5555
channel.pipeline().addLast( new HandshakeResponseHandler( handshakeCompletedPromise, logging ) );
5656
ChannelFuture handshakeFuture = channel.writeAndFlush( handshake() );
5757

58-
handshakeFuture.addListener( new ChannelFutureListener()
58+
handshakeFuture.addListener( channelFuture ->
5959
{
60-
@Override
61-
public void operationComplete( ChannelFuture future ) throws Exception
60+
if ( !channelFuture.isSuccess() )
6261
{
63-
if ( !future.isSuccess() )
64-
{
65-
handshakeCompletedPromise.setFailure( future.cause() );
66-
}
62+
handshakeCompletedPromise.setFailure( channelFuture.cause() );
6763
}
6864
} );
6965
}

driver/src/main/java/org/neo4j/driver/internal/async/HandshakeResponseHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ public HandshakeResponseHandler( ChannelPromise handshakeCompletedPromise, Loggi
5454
}
5555

5656
@Override
57-
public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause ) throws Exception
57+
public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause )
5858
{
5959
fail( ctx, cause );
6060
}
6161

6262
@Override
63-
protected void decode( ChannelHandlerContext ctx, ByteBuf in, List<Object> out ) throws Exception
63+
protected void decode( ChannelHandlerContext ctx, ByteBuf in, List<Object> out )
6464
{
6565
int serverSuggestedVersion = in.readInt();
6666
log.debug( "Server suggested protocol version: %s", serverSuggestedVersion );
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2002-2017 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal.async;
20+
21+
import io.netty.channel.embedded.EmbeddedChannel;
22+
import io.netty.channel.pool.ChannelPoolHandler;
23+
import io.netty.handler.ssl.SslHandler;
24+
import org.junit.Test;
25+
26+
import org.neo4j.driver.internal.security.SecurityPlan;
27+
import org.neo4j.driver.internal.util.Clock;
28+
import org.neo4j.driver.internal.util.FakeClock;
29+
30+
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertNotNull;
32+
import static org.junit.Assert.assertNull;
33+
import static org.mockito.Mockito.mock;
34+
import static org.mockito.Mockito.verify;
35+
import static org.mockito.Mockito.when;
36+
import static org.neo4j.driver.internal.async.ChannelAttributes.address;
37+
import static org.neo4j.driver.internal.async.ChannelAttributes.creationTimestamp;
38+
import static org.neo4j.driver.internal.async.ChannelAttributes.messageDispatcher;
39+
import static org.neo4j.driver.internal.net.BoltServerAddress.LOCAL_DEFAULT;
40+
41+
public class NettyChannelInitializerTest
42+
{
43+
@Test
44+
public void shouldAddSslHandlerWhenRequiresEncryption() throws Exception
45+
{
46+
SecurityPlan securityPlan = SecurityPlan.forAllCertificates();
47+
NettyChannelInitializer initializer = new NettyChannelInitializer( LOCAL_DEFAULT, securityPlan,
48+
mock( ChannelPoolHandler.class ), new FakeClock() );
49+
50+
EmbeddedChannel channel = new EmbeddedChannel();
51+
initializer.initChannel( channel );
52+
53+
assertNotNull( channel.pipeline().get( SslHandler.class ) );
54+
}
55+
56+
@Test
57+
public void shouldNotAddSslHandlerWhenDoesNotRequireEncryption() throws Exception
58+
{
59+
SecurityPlan securityPlan = SecurityPlan.insecure();
60+
NettyChannelInitializer initializer = new NettyChannelInitializer( LOCAL_DEFAULT, securityPlan,
61+
mock( ChannelPoolHandler.class ), new FakeClock() );
62+
63+
EmbeddedChannel channel = new EmbeddedChannel();
64+
initializer.initChannel( channel );
65+
66+
assertNull( channel.pipeline().get( SslHandler.class ) );
67+
}
68+
69+
@Test
70+
public void shouldUpdateChannelAttributes() throws Exception
71+
{
72+
Clock clock = mock( Clock.class );
73+
when( clock.millis() ).thenReturn( 42L );
74+
NettyChannelInitializer initializer = new NettyChannelInitializer( LOCAL_DEFAULT, SecurityPlan.insecure(),
75+
mock( ChannelPoolHandler.class ), clock );
76+
77+
EmbeddedChannel channel = new EmbeddedChannel();
78+
initializer.initChannel( channel );
79+
80+
assertEquals( LOCAL_DEFAULT, address( channel ) );
81+
assertEquals( 42L, creationTimestamp( channel ) );
82+
assertNotNull( messageDispatcher( channel ) );
83+
}
84+
85+
@Test
86+
public void shouldNotifyPoolHandlerAboutCreatedConnection() throws Exception
87+
{
88+
ChannelPoolHandler poolHandler = mock( ChannelPoolHandler.class );
89+
NettyChannelInitializer initializer = new NettyChannelInitializer( LOCAL_DEFAULT, SecurityPlan.insecure(),
90+
poolHandler, new FakeClock() );
91+
92+
EmbeddedChannel channel = new EmbeddedChannel();
93+
initializer.initChannel( channel );
94+
95+
verify( poolHandler ).channelCreated( channel );
96+
}
97+
}

0 commit comments

Comments
 (0)