Skip to content

Commit 5b4cfa7

Browse files
authored
Migrate tests from Java Driver to Testkit (#981) (#984)
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 cb3b34e commit 5b4cfa7

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

+14-4
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,24 @@ public static Driver driver( String uri, AuthToken authToken, Config config )
124124
/**
125125
* Return a driver for a Neo4j instance with custom configuration.
126126
*
127-
* @param uri the URL to a Neo4j instance
127+
* @param uri the URL to a Neo4j instance
128128
* @param authToken authentication to use, see {@link AuthTokens}
129-
* @param config user defined configuration
129+
* @param config user defined configuration
130130
* @return a new driver to the database instance specified by the URL
131131
*/
132132
public static Driver driver( URI uri, AuthToken authToken, Config config )
133+
{
134+
return driver( uri, authToken, config, new DriverFactory() );
135+
}
136+
137+
static Driver driver( URI uri, AuthToken authToken, Config config, DriverFactory driverFactory )
133138
{
134139
config = getOrDefault( config );
135140
RoutingSettings routingSettings = config.routingSettings();
136141
RetrySettings retrySettings = config.retrySettings();
137142
SecuritySettings securitySettings = config.securitySettings();
138143
SecurityPlan securityPlan = securitySettings.createSecurityPlan( uri.getScheme() );
139-
return new DriverFactory().newInstance( uri, authToken, routingSettings, retrySettings, config, securityPlan );
144+
return driverFactory.newInstance( uri, authToken, routingSettings, retrySettings, config, securityPlan );
140145
}
141146

142147
/**
@@ -151,13 +156,18 @@ public static Driver driver( URI uri, AuthToken authToken, Config config )
151156
* @return a new driver instance
152157
*/
153158
public static Driver routingDriver( Iterable<URI> routingUris, AuthToken authToken, Config config )
159+
{
160+
return routingDriver( routingUris, authToken, config, new DriverFactory() );
161+
}
162+
163+
static Driver routingDriver( Iterable<URI> routingUris, AuthToken authToken, Config config, DriverFactory driverFactory )
154164
{
155165
assertRoutingUris( routingUris );
156166
Logger log = createLogger( config );
157167

158168
for ( URI uri : routingUris )
159169
{
160-
final Driver driver = driver( uri, authToken, config );
170+
final Driver driver = driver( uri, authToken, config, driverFactory );
161171
try
162172
{
163173
driver.verifyConnectivity();

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

+35-55
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,103 +27,64 @@
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.anyString;
4248
import static org.mockito.ArgumentMatchers.eq;
49+
import static org.mockito.Mockito.doThrow;
4350
import static org.mockito.Mockito.mock;
4451
import static org.mockito.Mockito.verify;
4552
import static org.mockito.Mockito.when;
4653
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
47-
import static org.neo4j.driver.internal.util.Matchers.clusterDriver;
48-
import static org.neo4j.driver.internal.util.Matchers.directDriver;
4954
import static org.neo4j.driver.util.StubServer.INSECURE_CONFIG;
5055

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

9764
@Test
98-
void shouldLogWhenUnableToCreateRoutingDriver() throws Exception
65+
void shouldLogWhenUnableToCreateRoutingDriver()
9966
{
100-
StubServer server1 = StubServer.start( "discover_not_supported_9001.script", 9001 );
101-
StubServer server2 = StubServer.start( "discover_not_supported_9002.script", 9002 );
102-
10367
Logging logging = mock( Logging.class );
10468
Logger logger = mock( Logger.class );
10569
when( logging.getLog( anyString() ) ).thenReturn( logger );
106-
70+
InternalDriver driver = mock( InternalDriver.class );
71+
doThrow( ServiceUnavailableException.class ).when( driver ).verifyConnectivity();
72+
DriverFactory driverFactory = new MockSupplyingDriverFactory( driver );
10773
Config config = Config.builder()
108-
.withoutEncryption()
109-
.withLogging( logging )
110-
.build();
74+
.withLogging( logging )
75+
.build();
11176

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

116-
assertThrows( ServiceUnavailableException.class, () -> GraphDatabase.routingDriver( routingUris, AuthTokens.none(), config ) );
81+
assertThrows( ServiceUnavailableException.class, () -> GraphDatabase.routingDriver( routingUris, AuthTokens.none(), config, driverFactory ) );
11782

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

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

12890
@Test
@@ -215,4 +177,22 @@ private static Config createConfig( boolean encrypted, int timeoutMillis )
215177

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

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

+24
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

-10
This file was deleted.

driver/src/test/resources/discover_not_supported_9002.script

-10
This file was deleted.

driver/src/test/resources/discover_servers.script

-10
This file was deleted.

driver/src/test/resources/dummy_connection.script

-4
This file was deleted.

0 commit comments

Comments
 (0)