Skip to content

Commit 44835ae

Browse files
authored
Migrate tests to Testkit and exclude routing context when it is null (#974) (#975)
Migrated tests: - shouldBeAbleRunCypher -> test_should_read_successfully_using_write_session_run - shouldSendMultipleBookmarks -> test_send_and_receive_multiple_bookmarks_write_tx - shouldSendNullRoutingContextForBoltUri -> test_should_exclude_routing_context (optimization) - shouldLogConnectionIdInDebugMode -> not portable to Testkit, logic covered by unit tests: - InboundMessageDispatcherTest.shouldCreateChannelActivityLoggerAndLogDebugMessageOnMessageHandling - InboundMessageDispatcherTest.shouldCreateChannelActivityLoggerAndLogDebugMessageOnChannelError - ChannelActivityLoggerTest.* (formatted logging) - shouldSendReadAccessModeInQueryMetadata -> test_should_read_successfully_using_read_session_run - shouldNotSendWriteAccessModeInQueryMetadata -> test_should_read_successfully_using_write_session_run - shouldSendCustomerUserAgentInHelloMessage -> test_should_send_custom_user_agent_using_write_session_run
1 parent 9a80685 commit 44835ae

File tree

12 files changed

+93
-247
lines changed

12 files changed

+93
-247
lines changed

driver/src/main/java/org/neo4j/driver/internal/async/inbound/InboundMessageDispatcher.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,10 @@ enum MessageType
281281

282282
void run( MessageType messageType );
283283
}
284+
285+
// For testing only
286+
Logger getLog()
287+
{
288+
return log;
289+
}
284290
}

driver/src/main/java/org/neo4j/driver/internal/messaging/request/HelloMessage.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ private static Map<String,Value> buildMetadata( String userAgent, Map<String,Val
7878
{
7979
Map<String,Value> result = new HashMap<>( authToken );
8080
result.put( USER_AGENT_METADATA_KEY, value( userAgent ) );
81-
result.put( ROUTING_CONTEXT_METADATA_KEY, value( routingContext ) );
81+
if ( routingContext != null )
82+
{
83+
result.put( ROUTING_CONTEXT_METADATA_KEY, value( routingContext ) );
84+
}
8285
return result;
8386
}
8487
}

driver/src/test/java/org/neo4j/driver/internal/DirectDriverBoltKitIT.java

Lines changed: 0 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@
2222
import org.junit.jupiter.api.AfterEach;
2323
import org.junit.jupiter.api.BeforeAll;
2424
import org.junit.jupiter.api.Test;
25-
import org.mockito.ArgumentCaptor;
2625
import reactor.core.publisher.Flux;
2726
import reactor.core.publisher.Mono;
2827
import reactor.test.StepVerifier;
2928

3029
import java.net.URI;
3130
import java.util.ArrayList;
3231
import java.util.List;
33-
import java.util.Optional;
3432
import java.util.function.Consumer;
3533

3634
import org.neo4j.driver.AccessMode;
@@ -39,8 +37,6 @@
3937
import org.neo4j.driver.Config;
4038
import org.neo4j.driver.Driver;
4139
import org.neo4j.driver.GraphDatabase;
42-
import org.neo4j.driver.Logger;
43-
import org.neo4j.driver.Record;
4440
import org.neo4j.driver.Result;
4541
import org.neo4j.driver.Session;
4642
import org.neo4j.driver.Transaction;
@@ -67,18 +63,11 @@
6763
import static org.junit.jupiter.api.Assertions.assertNull;
6864
import static org.junit.jupiter.api.Assertions.assertThrows;
6965
import static org.junit.jupiter.api.Assertions.assertTrue;
70-
import static org.mockito.ArgumentMatchers.any;
71-
import static org.mockito.Mockito.atLeastOnce;
72-
import static org.mockito.Mockito.mock;
73-
import static org.mockito.Mockito.verify;
74-
import static org.mockito.Mockito.when;
7566
import static org.neo4j.driver.SessionConfig.builder;
7667
import static org.neo4j.driver.SessionConfig.forDatabase;
77-
import static org.neo4j.driver.Values.parameters;
7868
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
7969
import static org.neo4j.driver.util.StubServer.INSECURE_CONFIG;
8070
import static org.neo4j.driver.util.StubServer.insecureBuilder;
81-
import static org.neo4j.driver.util.TestUtil.asOrderedSet;
8271
import static org.neo4j.driver.util.TestUtil.await;
8372

8473
class DirectDriverBoltKitIT
@@ -97,140 +86,6 @@ public void killServers()
9786
stubController.reset();
9887
}
9988

100-
@Test
101-
void shouldBeAbleRunCypher() throws Exception
102-
{
103-
StubServer server = stubController.startStub( "return_x.script", 9001 );
104-
URI uri = URI.create( "bolt://127.0.0.1:9001" );
105-
int x;
106-
107-
try ( Driver driver = GraphDatabase.driver( uri, INSECURE_CONFIG ) )
108-
{
109-
try ( Session session = driver.session() )
110-
{
111-
Record record = session.run( "RETURN {x}", parameters( "x", 1 ) ).single();
112-
x = record.get( 0 ).asInt();
113-
}
114-
}
115-
116-
assertThat( x, equalTo( 1 ) );
117-
assertThat( server.exitStatus(), equalTo( 0 ) );
118-
}
119-
120-
@Test
121-
void shouldSendMultipleBookmarks() throws Exception
122-
{
123-
StubServer server = stubController.startStub( "multiple_bookmarks.script", 9001 );
124-
125-
Bookmark bookmarks = InternalBookmark.parse( asOrderedSet( "neo4j:bookmark:v1:tx5", "neo4j:bookmark:v1:tx29",
126-
"neo4j:bookmark:v1:tx94", "neo4j:bookmark:v1:tx56", "neo4j:bookmark:v1:tx16", "neo4j:bookmark:v1:tx68" ) );
127-
128-
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
129-
Session session = driver.session( builder().withBookmarks( bookmarks ).build() ) )
130-
{
131-
try ( Transaction tx = session.beginTransaction() )
132-
{
133-
tx.run( "CREATE (n {name:'Bob'})" );
134-
tx.commit();
135-
}
136-
137-
assertEquals( InternalBookmark.parse( "neo4j:bookmark:v1:tx95" ), session.lastBookmark() );
138-
}
139-
finally
140-
{
141-
assertEquals( 0, server.exitStatus() );
142-
}
143-
}
144-
145-
@Test
146-
void shouldSendNullRoutingContextForBoltUri() throws Exception
147-
{
148-
StubServer server = StubServer.start( "hello_with_routing_context_bolt.script", 9001 );
149-
150-
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
151-
Session session = driver.session() )
152-
{
153-
List<String> names = session.run( "MATCH (n) RETURN n.name" ).list( record -> record.get( 0 ).asString() );
154-
assertEquals( asList( "Foo", "Bar" ), names );
155-
156-
}
157-
finally
158-
{
159-
assertEquals( 0, server.exitStatus() );
160-
}
161-
}
162-
163-
@Test
164-
void shouldLogConnectionIdInDebugMode() throws Exception
165-
{
166-
StubServer server = stubController.startStub( "hello_run_exit.script", 9001 );
167-
168-
Logger logger = mock( Logger.class );
169-
when( logger.isDebugEnabled() ).thenReturn( true );
170-
171-
Config config = Config.builder()
172-
.withLogging( ignore -> logger )
173-
.withoutEncryption()
174-
.build();
175-
176-
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", config );
177-
Session session = driver.session() )
178-
{
179-
List<String> names = session.run( "MATCH (n) RETURN n.name" ).list( record -> record.get( 0 ).asString() );
180-
assertEquals( asList( "Foo", "Bar" ), names );
181-
182-
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass( String.class );
183-
verify( logger, atLeastOnce() ).debug( messageCaptor.capture(), any( Object[].class ) );
184-
185-
Optional<String> logMessageWithConnectionId = messageCaptor.getAllValues()
186-
.stream()
187-
.filter( line -> line.contains( "bolt-123456789" ) )
188-
.findAny();
189-
190-
assertTrue( logMessageWithConnectionId.isPresent(),
191-
"Expected log call did not happen. All debug log calls:\n" + String.join( "\n", messageCaptor.getAllValues() ) );
192-
}
193-
finally
194-
{
195-
assertEquals( 0, server.exitStatus() );
196-
}
197-
}
198-
199-
@Test
200-
void shouldSendReadAccessModeInQueryMetadata() throws Exception
201-
{
202-
StubServer server = stubController.startStub( "hello_run_exit_read.script", 9001 );
203-
204-
205-
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
206-
Session session = driver.session( builder().withDefaultAccessMode( AccessMode.READ ).build() ) )
207-
{
208-
List<String> names = session.run( "MATCH (n) RETURN n.name" ).list( record -> record.get( 0 ).asString() );
209-
assertEquals( asList( "Foo", "Bar" ), names );
210-
}
211-
finally
212-
{
213-
assertEquals( 0, server.exitStatus() );
214-
}
215-
}
216-
217-
@Test
218-
void shouldNotSendWriteAccessModeInQueryMetadata() throws Exception
219-
{
220-
StubServer server = stubController.startStub( "hello_run_exit.script", 9001 );
221-
222-
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", INSECURE_CONFIG );
223-
Session session = driver.session( builder().withDefaultAccessMode( AccessMode.WRITE ).build() ) )
224-
{
225-
List<String> names = session.run( "MATCH (n) RETURN n.name" ).list( record -> record.get( 0 ).asString() );
226-
assertEquals( asList( "Foo", "Bar" ), names );
227-
}
228-
finally
229-
{
230-
assertEquals( 0, server.exitStatus() );
231-
}
232-
}
233-
23489
@Test
23590
void shouldCloseChannelWhenResetFails() throws Exception
23691
{
@@ -597,25 +452,6 @@ void shouldBeAbleHandleNOOPsDuringRunCypher() throws Exception
597452
assertThat( server.exitStatus(), equalTo( 0 ) );
598453
}
599454

600-
@Test
601-
void shouldSendCustomerUserAgentInHelloMessage() throws Exception
602-
{
603-
StubServer server = stubController.startStub( "hello_with_custom_user_agent.script", 9001 );
604-
605-
Config config = Config.builder().withUserAgent( "AwesomeClient" ).build();
606-
607-
try ( Driver driver = GraphDatabase.driver( "bolt://localhost:9001", config );
608-
Session session = driver.session( builder().withDefaultAccessMode( AccessMode.WRITE ).build() ) )
609-
{
610-
List<String> names = session.run( "MATCH (n) RETURN n.name" ).list( record -> record.get( 0 ).asString() );
611-
assertEquals( asList( "Foo", "Bar" ), names );
612-
}
613-
finally
614-
{
615-
assertEquals( 0, server.exitStatus() );
616-
}
617-
}
618-
619455
private static void testTxCloseErrorPropagation( String script, Consumer<Transaction> txAction, String expectedErrorMessage )
620456
throws Exception
621457
{

driver/src/test/java/org/neo4j/driver/internal/async/inbound/InboundMessageDispatcherTest.java

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,38 @@
2323
import io.netty.channel.DefaultChannelId;
2424
import io.netty.util.Attribute;
2525
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.ValueSource;
2628
import org.mockito.ArgumentCaptor;
2729
import org.mockito.InOrder;
2830

2931
import java.util.HashMap;
3032
import java.util.Map;
3133

32-
import org.neo4j.driver.internal.spi.ResponseHandler;
33-
import org.neo4j.driver.internal.value.IntegerValue;
34+
import org.neo4j.driver.Logger;
35+
import org.neo4j.driver.Logging;
3436
import org.neo4j.driver.Value;
37+
import org.neo4j.driver.Values;
3538
import org.neo4j.driver.exceptions.ClientException;
3639
import org.neo4j.driver.exceptions.Neo4jException;
40+
import org.neo4j.driver.internal.logging.ChannelActivityLogger;
41+
import org.neo4j.driver.internal.messaging.Message;
42+
import org.neo4j.driver.internal.messaging.response.FailureMessage;
43+
import org.neo4j.driver.internal.messaging.response.IgnoredMessage;
44+
import org.neo4j.driver.internal.messaging.response.RecordMessage;
45+
import org.neo4j.driver.internal.messaging.response.SuccessMessage;
46+
import org.neo4j.driver.internal.spi.ResponseHandler;
47+
import org.neo4j.driver.internal.value.IntegerValue;
3748

3849
import static java.util.Collections.emptyMap;
3950
import static org.junit.jupiter.api.Assertions.assertEquals;
4051
import static org.junit.jupiter.api.Assertions.assertNull;
4152
import static org.junit.jupiter.api.Assertions.assertThrows;
53+
import static org.junit.jupiter.api.Assertions.assertTrue;
54+
import static org.junit.jupiter.api.Assertions.fail;
4255
import static org.mockito.ArgumentMatchers.any;
4356
import static org.mockito.ArgumentMatchers.anyBoolean;
57+
import static org.mockito.ArgumentMatchers.anyString;
4458
import static org.mockito.ArgumentMatchers.argThat;
4559
import static org.mockito.ArgumentMatchers.eq;
4660
import static org.mockito.Mockito.inOrder;
@@ -49,9 +63,9 @@
4963
import static org.mockito.Mockito.only;
5064
import static org.mockito.Mockito.verify;
5165
import static org.mockito.Mockito.when;
66+
import static org.neo4j.driver.Values.value;
5267
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
5368
import static org.neo4j.driver.internal.messaging.request.ResetMessage.RESET;
54-
import static org.neo4j.driver.Values.value;
5569

5670
class InboundMessageDispatcherTest
5771
{
@@ -354,6 +368,69 @@ void shouldReEnableAutoReadWhenAutoReadManagingHandlerIsRemoved()
354368
verify( channel.config() ).setAutoRead( anyBoolean() );
355369
}
356370

371+
@ParameterizedTest
372+
@ValueSource( classes = {SuccessMessage.class, FailureMessage.class, RecordMessage.class, IgnoredMessage.class} )
373+
void shouldCreateChannelActivityLoggerAndLogDebugMessageOnMessageHandling( Class<? extends Message> message )
374+
{
375+
// GIVEN
376+
Channel channel = newChannelMock();
377+
Logging logging = mock( Logging.class );
378+
Logger logger = mock( Logger.class );
379+
when( logger.isDebugEnabled() ).thenReturn( true );
380+
when( logging.getLog( InboundMessageDispatcher.class.getSimpleName() ) ).thenReturn( logger );
381+
InboundMessageDispatcher dispatcher = new InboundMessageDispatcher( channel, logging );
382+
ResponseHandler handler = mock( ResponseHandler.class );
383+
dispatcher.enqueue( handler );
384+
385+
// WHEN
386+
if ( SuccessMessage.class.isAssignableFrom( message ) )
387+
{
388+
dispatcher.handleSuccessMessage( new HashMap<>() );
389+
}
390+
else if ( FailureMessage.class.isAssignableFrom( message ) )
391+
{
392+
dispatcher.handleFailureMessage( FAILURE_CODE, FAILURE_MESSAGE );
393+
}
394+
else if ( RecordMessage.class.isAssignableFrom( message ) )
395+
{
396+
dispatcher.handleRecordMessage( Values.values() );
397+
}
398+
else if ( IgnoredMessage.class.isAssignableFrom( message ) )
399+
{
400+
dispatcher.handleIgnoredMessage();
401+
}
402+
else
403+
{
404+
fail( "Unexpected message type parameter provided" );
405+
}
406+
407+
// THEN
408+
assertTrue( dispatcher.getLog() instanceof ChannelActivityLogger );
409+
verify( logger ).debug( anyString(), any( Object.class ) );
410+
}
411+
412+
@Test
413+
void shouldCreateChannelActivityLoggerAndLogDebugMessageOnChannelError()
414+
{
415+
// GIVEN
416+
Channel channel = newChannelMock();
417+
Logging logging = mock( Logging.class );
418+
Logger logger = mock( Logger.class );
419+
when( logger.isDebugEnabled() ).thenReturn( true );
420+
when( logging.getLog( InboundMessageDispatcher.class.getSimpleName() ) ).thenReturn( logger );
421+
InboundMessageDispatcher dispatcher = new InboundMessageDispatcher( channel, logging );
422+
ResponseHandler handler = mock( ResponseHandler.class );
423+
dispatcher.enqueue( handler );
424+
Throwable throwable = mock( Throwable.class );
425+
426+
// WHEN
427+
dispatcher.handleChannelError( throwable );
428+
429+
// THEN
430+
assertTrue( dispatcher.getLog() instanceof ChannelActivityLogger );
431+
verify( logger ).debug( anyString(), eq( throwable ) );
432+
}
433+
357434
private static void verifyFailure( ResponseHandler handler )
358435
{
359436
ArgumentCaptor<Neo4jException> captor = ArgumentCaptor.forClass( Neo4jException.class );

driver/src/test/java/org/neo4j/driver/internal/messaging/encode/HelloMessageEncoderTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,18 @@
2121
import org.junit.jupiter.api.Test;
2222
import org.mockito.InOrder;
2323

24-
import java.util.Collections;
2524
import java.util.HashMap;
2625
import java.util.Map;
2726

27+
import org.neo4j.driver.Value;
2828
import org.neo4j.driver.internal.messaging.ValuePacker;
2929
import org.neo4j.driver.internal.messaging.request.HelloMessage;
30-
import org.neo4j.driver.Value;
3130

3231
import static org.junit.jupiter.api.Assertions.assertThrows;
3332
import static org.mockito.Mockito.inOrder;
3433
import static org.mockito.Mockito.mock;
35-
import static org.neo4j.driver.Values.NULL;
36-
import static org.neo4j.driver.internal.messaging.request.PullAllMessage.PULL_ALL;
3734
import static org.neo4j.driver.Values.value;
35+
import static org.neo4j.driver.internal.messaging.request.PullAllMessage.PULL_ALL;
3836

3937
class HelloMessageEncoderTest
4038
{
@@ -55,7 +53,6 @@ void shouldEncodeHelloMessage() throws Exception
5553

5654
Map<String,Value> expectedMetadata = new HashMap<>( authToken );
5755
expectedMetadata.put( "user_agent", value( "MyDriver" ) );
58-
expectedMetadata.put( "routing", NULL );
5956
order.verify( packer ).pack( expectedMetadata );
6057
}
6158

0 commit comments

Comments
 (0)