Skip to content

Commit aae9607

Browse files
authored
Migrate tests from Java Driver to Testkit (#981)
Replaced tests: - boltSchemeShouldInstantiateDirectDriver -> shouldCreateAppropriateDriverType (DriverFactoryTest unit test) - boltPlusDiscoverySchemeShouldInstantiateClusterDriver -> shouldCreateAppropriateDriverType (DriverFactoryTest unit test) - shouldLogWhenUnableToCreateRoutingDriver -> shouldLogWhenUnableToCreateRoutingDriver (converted to unit test) Migrated tests: - shouldLogWhenUnableToCreateRoutingDriver -> test_should_fail_discovery_when_router_fails_with_procedure_not_found_code (covers the actual logic when error occurs, the logging verification has been converted to unit test) - shouldOnlyPullRecordsWhenNeededSimpleSession -> test_should_accept_custom_fetch_size_using_session_configuration (existing test)
1 parent 377a48f commit aae9607

File tree

7 files changed

+73
-93
lines changed

7 files changed

+73
-93
lines changed

driver/src/main/java/org/neo4j/driver/GraphDatabase.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,24 @@ public static Driver driver( String uri, AuthToken authToken, Config config )
122122
/**
123123
* Return a driver for a Neo4j instance with custom configuration.
124124
*
125-
* @param uri the URL to a Neo4j instance
125+
* @param uri the URL to a Neo4j instance
126126
* @param authToken authentication to use, see {@link AuthTokens}
127-
* @param config user defined configuration
127+
* @param config user defined configuration
128128
* @return a new driver to the database instance specified by the URL
129129
*/
130130
public static Driver driver( URI uri, AuthToken authToken, Config config )
131+
{
132+
return driver( uri, authToken, config, new DriverFactory() );
133+
}
134+
135+
static Driver driver( URI uri, AuthToken authToken, Config config, DriverFactory driverFactory )
131136
{
132137
config = getOrDefault( config );
133138
RoutingSettings routingSettings = config.routingSettings();
134139
RetrySettings retrySettings = config.retrySettings();
135140
SecuritySettings securitySettings = config.securitySettings();
136141
SecurityPlan securityPlan = securitySettings.createSecurityPlan( uri.getScheme() );
137-
return new DriverFactory().newInstance( uri, authToken, routingSettings, retrySettings, config, securityPlan );
142+
return driverFactory.newInstance( uri, authToken, routingSettings, retrySettings, config, securityPlan );
138143
}
139144

140145
/**
@@ -149,13 +154,18 @@ public static Driver driver( URI uri, AuthToken authToken, Config config )
149154
* @return a new driver instance
150155
*/
151156
public static Driver routingDriver( Iterable<URI> routingUris, AuthToken authToken, Config config )
157+
{
158+
return routingDriver( routingUris, authToken, config, new DriverFactory() );
159+
}
160+
161+
static Driver routingDriver( Iterable<URI> routingUris, AuthToken authToken, Config config, DriverFactory driverFactory )
152162
{
153163
assertRoutingUris( routingUris );
154164
Logger log = createLogger( config );
155165

156166
for ( URI uri : routingUris )
157167
{
158-
final Driver driver = driver( uri, authToken, config );
168+
final Driver driver = driver( uri, authToken, config, driverFactory );
159169
try
160170
{
161171
driver.verifyConnectivity();

driver/src/test/java/org/neo4j/driver/GraphDatabaseTest.java

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.neo4j.driver;
2020

21+
import io.netty.util.concurrent.EventExecutorGroup;
2122
import org.junit.jupiter.api.Test;
2223

2324
import java.io.IOException;
@@ -26,102 +27,63 @@
2627
import java.util.List;
2728

2829
import org.neo4j.driver.exceptions.ServiceUnavailableException;
29-
import org.neo4j.driver.util.StubServer;
30+
import org.neo4j.driver.internal.BoltServerAddress;
31+
import org.neo4j.driver.internal.DriverFactory;
32+
import org.neo4j.driver.internal.InternalDriver;
33+
import org.neo4j.driver.internal.cluster.RoutingSettings;
34+
import org.neo4j.driver.internal.metrics.MetricsProvider;
35+
import org.neo4j.driver.internal.retry.RetryLogic;
36+
import org.neo4j.driver.internal.security.SecurityPlan;
37+
import org.neo4j.driver.internal.spi.ConnectionPool;
3038
import org.neo4j.driver.util.TestUtil;
3139

3240
import static java.util.Arrays.asList;
3341
import static java.util.concurrent.TimeUnit.MILLISECONDS;
3442
import static org.hamcrest.Matchers.containsString;
35-
import static org.hamcrest.Matchers.is;
36-
import static org.hamcrest.core.IsEqual.equalTo;
3743
import static org.hamcrest.junit.MatcherAssert.assertThat;
3844
import static org.junit.jupiter.api.Assertions.assertEquals;
3945
import static org.junit.jupiter.api.Assertions.assertThrows;
4046
import static org.mockito.ArgumentMatchers.any;
4147
import static org.mockito.ArgumentMatchers.eq;
48+
import static org.mockito.Mockito.doThrow;
4249
import static org.mockito.Mockito.mock;
4350
import static org.mockito.Mockito.verify;
4451
import static org.mockito.Mockito.when;
4552
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
46-
import static org.neo4j.driver.internal.util.Matchers.clusterDriver;
47-
import static org.neo4j.driver.internal.util.Matchers.directDriver;
4853
import static org.neo4j.driver.util.StubServer.INSECURE_CONFIG;
4954

5055
class GraphDatabaseTest
5156
{
52-
@Test
53-
void boltSchemeShouldInstantiateDirectDriver() throws Exception
54-
{
55-
// Given
56-
StubServer server = StubServer.start( "dummy_connection.script", 9001 );
57-
URI uri = URI.create( "bolt://localhost:9001" );
58-
59-
// When
60-
Driver driver = GraphDatabase.driver( uri, INSECURE_CONFIG );
61-
driver.verifyConnectivity();
62-
63-
// Then
64-
assertThat( driver, is( directDriver() ) );
65-
66-
// Finally
67-
driver.close();
68-
assertThat( server.exitStatus(), equalTo( 0 ) );
69-
}
70-
71-
@Test
72-
void boltPlusDiscoverySchemeShouldInstantiateClusterDriver() throws Exception
73-
{
74-
// Given
75-
StubServer server = StubServer.start( "discover_servers.script", 9001 );
76-
URI uri = URI.create( "neo4j://127.0.0.1:9001" );
77-
78-
// When
79-
Driver driver = GraphDatabase.driver( uri, INSECURE_CONFIG );
80-
driver.verifyConnectivity();
81-
82-
// Then
83-
assertThat( driver, is( clusterDriver() ) );
84-
85-
// Finally
86-
driver.close();
87-
assertThat( server.exitStatus(), equalTo( 0 ) );
88-
}
89-
9057
@Test
9158
void throwsWhenBoltSchemeUsedWithRoutingParams()
9259
{
9360
assertThrows( IllegalArgumentException.class, () -> GraphDatabase.driver( "bolt://localhost:7687/?policy=my_policy" ) );
9461
}
9562

9663
@Test
97-
void shouldLogWhenUnableToCreateRoutingDriver() throws Exception
64+
void shouldLogWhenUnableToCreateRoutingDriver()
9865
{
99-
StubServer server1 = StubServer.start( "discover_not_supported_9001.script", 9001 );
100-
StubServer server2 = StubServer.start( "discover_not_supported_9002.script", 9002 );
101-
10266
Logging logging = mock( Logging.class );
10367
Logger logger = mock( Logger.class );
10468
when( logging.getLog( any( Class.class ) ) ).thenReturn( logger );
105-
69+
InternalDriver driver = mock( InternalDriver.class );
70+
doThrow( ServiceUnavailableException.class ).when( driver ).verifyConnectivity();
71+
DriverFactory driverFactory = new MockSupplyingDriverFactory( driver );
10672
Config config = Config.builder()
107-
.withoutEncryption()
108-
.withLogging( logging )
109-
.build();
73+
.withLogging( logging )
74+
.build();
11075

11176
List<URI> routingUris = asList(
11277
URI.create( "neo4j://localhost:9001" ),
11378
URI.create( "neo4j://localhost:9002" ) );
11479

115-
assertThrows( ServiceUnavailableException.class, () -> GraphDatabase.routingDriver( routingUris, AuthTokens.none(), config ) );
80+
assertThrows( ServiceUnavailableException.class, () -> GraphDatabase.routingDriver( routingUris, AuthTokens.none(), config, driverFactory ) );
11681

11782
verify( logger ).warn( eq( "Unable to create routing driver for URI: neo4j://localhost:9001" ),
11883
any( Throwable.class ) );
11984

12085
verify( logger ).warn( eq( "Unable to create routing driver for URI: neo4j://localhost:9002" ),
12186
any( Throwable.class ) );
122-
123-
assertEquals( 0, server1.exitStatus() );
124-
assertEquals( 0, server2.exitStatus() );
12587
}
12688

12789
@Test
@@ -214,4 +176,22 @@ private static Config createConfig( boolean encrypted, int timeoutMillis )
214176

215177
return configBuilder.build();
216178
}
179+
180+
private static class MockSupplyingDriverFactory extends DriverFactory
181+
{
182+
private final InternalDriver driver;
183+
184+
private MockSupplyingDriverFactory( InternalDriver driver )
185+
{
186+
this.driver = driver;
187+
}
188+
189+
@Override
190+
protected InternalDriver createRoutingDriver( SecurityPlan securityPlan, BoltServerAddress address, ConnectionPool connectionPool,
191+
EventExecutorGroup eventExecutorGroup, RoutingSettings routingSettings, RetryLogic retryLogic,
192+
MetricsProvider metricsProvider, Config config )
193+
{
194+
return driver;
195+
}
196+
}
217197
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
5858
import static org.junit.jupiter.api.Assertions.assertNotNull;
5959
import static org.junit.jupiter.api.Assertions.assertThrows;
60+
import static org.junit.jupiter.api.Assertions.fail;
6061
import static org.mockito.Mockito.any;
6162
import static org.mockito.Mockito.mock;
6263
import static org.mockito.Mockito.never;
@@ -66,6 +67,8 @@
6667
import static org.neo4j.driver.internal.metrics.MetricsProvider.METRICS_DISABLED_PROVIDER;
6768
import static org.neo4j.driver.internal.util.Futures.completedWithNull;
6869
import static org.neo4j.driver.internal.util.Futures.failedFuture;
70+
import static org.neo4j.driver.internal.util.Matchers.clusterDriver;
71+
import static org.neo4j.driver.internal.util.Matchers.directDriver;
6972

7073
class DriverFactoryTest
7174
{
@@ -168,6 +171,27 @@ void shouldCreateDriverMetricsIfMonitoringEnabled()
168171
assertThat( provider instanceof InternalMetricsProvider, is( true ) );
169172
}
170173

174+
@ParameterizedTest
175+
@MethodSource( "testUris" )
176+
void shouldCreateAppropriateDriverType( String uri )
177+
{
178+
DriverFactory driverFactory = new DriverFactory();
179+
Driver driver = createDriver( uri, driverFactory );
180+
181+
if ( uri.startsWith( "bolt://" ) )
182+
{
183+
assertThat( driver, is( directDriver() ) );
184+
}
185+
else if ( uri.startsWith( "neo4j://" ) )
186+
{
187+
assertThat( driver, is( clusterDriver() ) );
188+
}
189+
else
190+
{
191+
fail( "Unexpected scheme provided in argument" );
192+
}
193+
}
194+
171195
private Driver createDriver( String uri, DriverFactory driverFactory )
172196
{
173197
return createDriver( uri, driverFactory, defaultConfig() );

driver/src/test/resources/discover_not_supported_9001.script

Lines changed: 0 additions & 10 deletions
This file was deleted.

driver/src/test/resources/discover_not_supported_9002.script

Lines changed: 0 additions & 10 deletions
This file was deleted.

driver/src/test/resources/discover_servers.script

Lines changed: 0 additions & 10 deletions
This file was deleted.

driver/src/test/resources/dummy_connection.script

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)