Skip to content

Commit

Permalink
Migrate tests from Java Driver to Testkit (#981) (#984)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
injectives authored Aug 16, 2021
1 parent cb3b34e commit 5b4cfa7
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 93 deletions.
18 changes: 14 additions & 4 deletions driver/src/main/java/org/neo4j/driver/GraphDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,24 @@ public static Driver driver( String uri, AuthToken authToken, Config config )
/**
* Return a driver for a Neo4j instance with custom configuration.
*
* @param uri the URL to a Neo4j instance
* @param uri the URL to a Neo4j instance
* @param authToken authentication to use, see {@link AuthTokens}
* @param config user defined configuration
* @param config user defined configuration
* @return a new driver to the database instance specified by the URL
*/
public static Driver driver( URI uri, AuthToken authToken, Config config )
{
return driver( uri, authToken, config, new DriverFactory() );
}

static Driver driver( URI uri, AuthToken authToken, Config config, DriverFactory driverFactory )
{
config = getOrDefault( config );
RoutingSettings routingSettings = config.routingSettings();
RetrySettings retrySettings = config.retrySettings();
SecuritySettings securitySettings = config.securitySettings();
SecurityPlan securityPlan = securitySettings.createSecurityPlan( uri.getScheme() );
return new DriverFactory().newInstance( uri, authToken, routingSettings, retrySettings, config, securityPlan );
return driverFactory.newInstance( uri, authToken, routingSettings, retrySettings, config, securityPlan );
}

/**
Expand All @@ -151,13 +156,18 @@ public static Driver driver( URI uri, AuthToken authToken, Config config )
* @return a new driver instance
*/
public static Driver routingDriver( Iterable<URI> routingUris, AuthToken authToken, Config config )
{
return routingDriver( routingUris, authToken, config, new DriverFactory() );
}

static Driver routingDriver( Iterable<URI> routingUris, AuthToken authToken, Config config, DriverFactory driverFactory )
{
assertRoutingUris( routingUris );
Logger log = createLogger( config );

for ( URI uri : routingUris )
{
final Driver driver = driver( uri, authToken, config );
final Driver driver = driver( uri, authToken, config, driverFactory );
try
{
driver.verifyConnectivity();
Expand Down
90 changes: 35 additions & 55 deletions driver/src/test/java/org/neo4j/driver/GraphDatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.neo4j.driver;

import io.netty.util.concurrent.EventExecutorGroup;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand All @@ -26,103 +27,64 @@
import java.util.List;

import org.neo4j.driver.exceptions.ServiceUnavailableException;
import org.neo4j.driver.util.StubServer;
import org.neo4j.driver.internal.BoltServerAddress;
import org.neo4j.driver.internal.DriverFactory;
import org.neo4j.driver.internal.InternalDriver;
import org.neo4j.driver.internal.cluster.RoutingSettings;
import org.neo4j.driver.internal.metrics.MetricsProvider;
import org.neo4j.driver.internal.retry.RetryLogic;
import org.neo4j.driver.internal.security.SecurityPlan;
import org.neo4j.driver.internal.spi.ConnectionPool;
import org.neo4j.driver.util.TestUtil;

import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.junit.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
import static org.neo4j.driver.internal.util.Matchers.clusterDriver;
import static org.neo4j.driver.internal.util.Matchers.directDriver;
import static org.neo4j.driver.util.StubServer.INSECURE_CONFIG;

class GraphDatabaseTest
{
@Test
void boltSchemeShouldInstantiateDirectDriver() throws Exception
{
// Given
StubServer server = StubServer.start( "dummy_connection.script", 9001 );
URI uri = URI.create( "bolt://localhost:9001" );

// When
Driver driver = GraphDatabase.driver( uri, INSECURE_CONFIG );
driver.verifyConnectivity();

// Then
assertThat( driver, is( directDriver() ) );

// Finally
driver.close();
assertThat( server.exitStatus(), equalTo( 0 ) );
}

@Test
void boltPlusDiscoverySchemeShouldInstantiateClusterDriver() throws Exception
{
// Given
StubServer server = StubServer.start( "discover_servers.script", 9001 );
URI uri = URI.create( "neo4j://127.0.0.1:9001" );

// When
Driver driver = GraphDatabase.driver( uri, INSECURE_CONFIG );
driver.verifyConnectivity();

// Then
assertThat( driver, is( clusterDriver() ) );

// Finally
driver.close();
assertThat( server.exitStatus(), equalTo( 0 ) );
}

@Test
void throwsWhenBoltSchemeUsedWithRoutingParams()
{
assertThrows( IllegalArgumentException.class, () -> GraphDatabase.driver( "bolt://localhost:7687/?policy=my_policy" ) );
}

@Test
void shouldLogWhenUnableToCreateRoutingDriver() throws Exception
void shouldLogWhenUnableToCreateRoutingDriver()
{
StubServer server1 = StubServer.start( "discover_not_supported_9001.script", 9001 );
StubServer server2 = StubServer.start( "discover_not_supported_9002.script", 9002 );

Logging logging = mock( Logging.class );
Logger logger = mock( Logger.class );
when( logging.getLog( anyString() ) ).thenReturn( logger );

InternalDriver driver = mock( InternalDriver.class );
doThrow( ServiceUnavailableException.class ).when( driver ).verifyConnectivity();
DriverFactory driverFactory = new MockSupplyingDriverFactory( driver );
Config config = Config.builder()
.withoutEncryption()
.withLogging( logging )
.build();
.withLogging( logging )
.build();

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

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

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

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

assertEquals( 0, server1.exitStatus() );
assertEquals( 0, server2.exitStatus() );
}

@Test
Expand Down Expand Up @@ -215,4 +177,22 @@ private static Config createConfig( boolean encrypted, int timeoutMillis )

return configBuilder.build();
}

private static class MockSupplyingDriverFactory extends DriverFactory
{
private final InternalDriver driver;

private MockSupplyingDriverFactory( InternalDriver driver )
{
this.driver = driver;
}

@Override
protected InternalDriver createRoutingDriver( SecurityPlan securityPlan, BoltServerAddress address, ConnectionPool connectionPool,
EventExecutorGroup eventExecutorGroup, RoutingSettings routingSettings, RetryLogic retryLogic,
MetricsProvider metricsProvider, Config config )
{
return driver;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
Expand All @@ -66,6 +67,8 @@
import static org.neo4j.driver.internal.metrics.MetricsProvider.METRICS_DISABLED_PROVIDER;
import static org.neo4j.driver.internal.util.Futures.completedWithNull;
import static org.neo4j.driver.internal.util.Futures.failedFuture;
import static org.neo4j.driver.internal.util.Matchers.clusterDriver;
import static org.neo4j.driver.internal.util.Matchers.directDriver;

class DriverFactoryTest
{
Expand Down Expand Up @@ -168,6 +171,27 @@ void shouldCreateDriverMetricsIfMonitoringEnabled()
assertThat( provider instanceof InternalMetricsProvider, is( true ) );
}

@ParameterizedTest
@MethodSource( "testUris" )
void shouldCreateAppropriateDriverType( String uri )
{
DriverFactory driverFactory = new DriverFactory();
Driver driver = createDriver( uri, driverFactory );

if ( uri.startsWith( "bolt://" ) )
{
assertThat( driver, is( directDriver() ) );
}
else if ( uri.startsWith( "neo4j://" ) )
{
assertThat( driver, is( clusterDriver() ) );
}
else
{
fail( "Unexpected scheme provided in argument" );
}
}

private Driver createDriver( String uri, DriverFactory driverFactory )
{
return createDriver( uri, driverFactory, defaultConfig() );
Expand Down
10 changes: 0 additions & 10 deletions driver/src/test/resources/discover_not_supported_9001.script

This file was deleted.

10 changes: 0 additions & 10 deletions driver/src/test/resources/discover_not_supported_9002.script

This file was deleted.

10 changes: 0 additions & 10 deletions driver/src/test/resources/discover_servers.script

This file was deleted.

4 changes: 0 additions & 4 deletions driver/src/test/resources/dummy_connection.script

This file was deleted.

0 comments on commit 5b4cfa7

Please sign in to comment.