Skip to content

Commit d56154a

Browse files
authored
Merge pull request #604 from ali-ince/1.7-add-neo4j-scheme
Introduce neo4j scheme as a direct alias for bolt+routing
2 parents b57b339 + e28a995 commit d56154a

File tree

7 files changed

+43
-20
lines changed

7 files changed

+43
-20
lines changed

driver/src/main/java/org/neo4j/driver/internal/DriverFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public class DriverFactory
7171
{
7272
public static final String BOLT_URI_SCHEME = "bolt";
7373
public static final String BOLT_ROUTING_URI_SCHEME = "bolt+routing";
74+
public static final String NEO4J_URI_SCHEME = "neo4j";
7475

7576
public final Driver newInstance( URI uri, AuthToken authToken, RoutingSettings routingSettings,
7677
RetrySettings retrySettings, Config config )
@@ -138,6 +139,7 @@ private InternalDriver createDriver( URI uri, SecurityPlan securityPlan, BoltSer
138139
assertNoRoutingContext( uri, routingSettings );
139140
return createDirectDriver( securityPlan, address, connectionPool, retryLogic, metrics, config );
140141
case BOLT_ROUTING_URI_SCHEME:
142+
case NEO4J_URI_SCHEME:
141143
return createRoutingDriver( securityPlan, address, connectionPool, eventExecutorGroup, routingSettings, retryLogic, metrics, config );
142144
default:
143145
throw new ClientException( format( "Unsupported URI scheme: %s", scheme ) );
@@ -168,7 +170,7 @@ protected InternalDriver createDirectDriver( SecurityPlan securityPlan, BoltServ
168170
}
169171

170172
/**
171-
* Creates new a new driver for "bolt+routing" scheme.
173+
* Creates new a new driver for "bolt+routing" or "neo4j" scheme.
172174
* <p>
173175
* <b>This method is protected only for testing</b>
174176
*/

driver/src/main/java/org/neo4j/driver/v1/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ public ConfigBuilder withMaxConnectionLifetime( long value, TimeUnit unit )
488488
* Configure maximum amount of connections in the connection pool towards a single database. This setting
489489
* limits total amount of connections in the pool when used in direct driver, created for URI with 'bolt'
490490
* scheme. It will limit amount of connections per cluster member when used with routing driver, created for
491-
* URI with 'bolt+routing' scheme.
491+
* URI with 'bolt+routing' or 'neo4j' scheme.
492492
* <p>
493493
* Acquisition will be attempted for at most configured timeout
494494
* {@link #withConnectionAcquisitionTimeout(long, TimeUnit)} when limit is reached.

driver/src/main/java/org/neo4j/driver/v1/Driver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
* <td>Direct driver: connects directly to the host and port specified in the URI.</td>
4848
* </tr>
4949
* <tr>
50-
* <td><code>bolt+routing</code></td>
50+
* <td><code>bolt+routing</code> or <code>neo4j</code></td>
5151
* <td>Routing driver: can automatically discover members of a Causal Cluster and route {@link Session sessions} based on {@link AccessMode}.</td>
5252
* </tr>
5353
* </tbody>

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,27 @@
1919
package org.neo4j.driver.v1;
2020

2121
import java.net.URI;
22+
import java.util.Arrays;
23+
import java.util.List;
2224

2325
import org.neo4j.driver.internal.DriverFactory;
2426
import org.neo4j.driver.internal.cluster.RoutingSettings;
2527
import org.neo4j.driver.internal.retry.RetrySettings;
2628
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
2729

2830
import static org.neo4j.driver.internal.DriverFactory.BOLT_ROUTING_URI_SCHEME;
31+
import static org.neo4j.driver.internal.DriverFactory.NEO4J_URI_SCHEME;
2932

3033
/**
3134
* Creates {@link Driver drivers}, optionally letting you {@link #driver(URI, Config)} to configure them.
35+
*
3236
* @see Driver
3337
* @since 1.0
3438
*/
3539
public class GraphDatabase
3640
{
3741
private static final String LOGGER_NAME = GraphDatabase.class.getSimpleName();
42+
private static final List<String> VALID_ROUTING_URIS = Arrays.asList( BOLT_ROUTING_URI_SCHEME, NEO4J_URI_SCHEME );
3843

3944
/**
4045
* Return a driver for a Neo4j instance with the default configuration settings
@@ -137,12 +142,12 @@ public static Driver driver( URI uri, AuthToken authToken, Config config )
137142
}
138143

139144
/**
140-
* Try to create a bolt+routing driver from the <b>first</b> available address.
145+
* Try to create a routing driver from the <b>first</b> available address.
141146
* This is wrapper for the {@link #driver} method that finds the <b>first</b>
142147
* server to respond positively.
143148
*
144149
* @param routingUris an {@link Iterable} of server {@link URI}s for Neo4j instances. All given URIs should
145-
* have 'bolt+routing' scheme.
150+
* have 'bolt+routing' or 'neo4j' scheme.
146151
* @param authToken authentication to use, see {@link AuthTokens}
147152
* @param config user defined configuration
148153
* @return a new driver instance
@@ -171,10 +176,11 @@ private static void assertRoutingUris( Iterable<URI> uris )
171176
{
172177
for ( URI uri : uris )
173178
{
174-
if ( !BOLT_ROUTING_URI_SCHEME.equals( uri.getScheme() ) )
179+
if ( !VALID_ROUTING_URIS.contains( uri.getScheme() ) )
175180
{
176181
throw new IllegalArgumentException(
177-
"Illegal URI scheme, expected '" + BOLT_ROUTING_URI_SCHEME + "' in '" + uri + "'" );
182+
String.format(
183+
"Illegal URI scheme, expected URI scheme '%s' to be among '%s'", uri.getScheme(), VALID_ROUTING_URIS.toString() ) );
178184
}
179185
}
180186
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class DriverFactoryTest
6767
{
6868
private static Stream<String> testUris()
6969
{
70-
return Stream.of( "bolt://localhost:7687", "bolt+routing://localhost:7687" );
70+
return Stream.of( "bolt://localhost:7687", "bolt+routing://localhost:7687", "neo4j://localhost:7687" );
7171
}
7272

7373
@ParameterizedTest

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package org.neo4j.driver.v1;
2020

2121
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.params.ParameterizedTest;
23+
import org.junit.jupiter.params.provider.ValueSource;
2224

2325
import java.io.File;
2426
import java.io.IOException;
@@ -69,15 +71,16 @@ void boltSchemeShouldInstantiateDirectDriver() throws Exception
6971
assertThat( server.exitStatus(), equalTo( 0 ) );
7072
}
7173

72-
@Test
73-
void boltPlusDiscoverySchemeShouldInstantiateClusterDriver() throws Exception
74+
@ParameterizedTest
75+
@ValueSource( strings = {"bolt+routing://127.0.0.1:9001", "neo4j://127.0.0.1:9001"} )
76+
void boltPlusDiscoverySchemeShouldInstantiateClusterDriver( String uri ) throws Exception
7477
{
7578
// Given
7679
StubServer server = StubServer.start( "discover_servers.script", 9001 );
77-
URI uri = URI.create( "bolt+routing://127.0.0.1:9001" );
80+
URI driverUri = URI.create( uri );
7881

7982
// When
80-
Driver driver = GraphDatabase.driver( uri, INSECURE_CONFIG );
83+
Driver driver = GraphDatabase.driver( driverUri, INSECURE_CONFIG );
8184

8285
// Then
8386
assertThat( driver, is( clusterDriver() ) );
@@ -107,8 +110,9 @@ void throwsWhenBoltSchemeUsedWithRoutingParams()
107110
assertThrows( IllegalArgumentException.class, () -> GraphDatabase.driver( "bolt://localhost:7687/?policy=my_policy" ) );
108111
}
109112

110-
@Test
111-
void shouldLogWhenUnableToCreateRoutingDriver() throws Exception
113+
@ParameterizedTest
114+
@ValueSource( strings = {"bolt+routing", "neo4j"} )
115+
void shouldLogWhenUnableToCreateRoutingDriver( String scheme ) throws Exception
112116
{
113117
StubServer server1 = StubServer.start( "non_discovery_server.script", 9001 );
114118
StubServer server2 = StubServer.start( "non_discovery_server.script", 9002 );
@@ -123,21 +127,31 @@ void shouldLogWhenUnableToCreateRoutingDriver() throws Exception
123127
.build();
124128

125129
List<URI> routingUris = asList(
126-
URI.create( "bolt+routing://localhost:9001" ),
127-
URI.create( "bolt+routing://localhost:9002" ) );
130+
URI.create( scheme + "://localhost:9001" ),
131+
URI.create( scheme + "://localhost:9002" ) );
128132

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

131-
verify( logger ).warn( eq( "Unable to create routing driver for URI: bolt+routing://localhost:9001" ),
135+
verify( logger ).warn( eq( String.format( "Unable to create routing driver for URI: %s://localhost:9001", scheme ) ),
132136
any( Throwable.class ) );
133137

134-
verify( logger ).warn( eq( "Unable to create routing driver for URI: bolt+routing://localhost:9002" ),
138+
verify( logger ).warn( eq( String.format( "Unable to create routing driver for URI: %s://localhost:9002", scheme ) ),
135139
any( Throwable.class ) );
136140

137141
assertEquals( 0, server1.exitStatus() );
138142
assertEquals( 0, server2.exitStatus() );
139143
}
140144

145+
@Test
146+
void shouldFailWhenUnsupportedSchemeIsProvidedToRoutingDriver()
147+
{
148+
IllegalArgumentException exc =
149+
assertThrows( IllegalArgumentException.class, () ->
150+
GraphDatabase.routingDriver( asList( URI.create( "xscheme://localhost:7687" ) ), AuthTokens.none(), Config.defaultConfig() ) );
151+
152+
assertThat( exc.getMessage(), equalTo( "Illegal URI scheme, expected URI scheme 'xscheme' to be among '[bolt+routing, neo4j]'" ) );
153+
}
154+
141155
@Test
142156
void shouldRespondToInterruptsWhenConnectingToUnresponsiveServer() throws Exception
143157
{

driver/src/test/java/org/neo4j/driver/v1/util/cc/LocalOrRemoteClusterExtension.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.neo4j.driver.v1.util.TestUtil;
3333

3434
import static org.neo4j.driver.internal.DriverFactory.BOLT_ROUTING_URI_SCHEME;
35+
import static org.neo4j.driver.internal.DriverFactory.NEO4J_URI_SCHEME;
3536
import static org.neo4j.driver.v1.Config.defaultConfig;
3637

3738
public class LocalOrRemoteClusterExtension implements BeforeAllCallback, AfterEachCallback, AfterAllCallback
@@ -118,9 +119,9 @@ private static void assertValidSystemPropertiesDefined()
118119
"Both cluster uri and 'neo4j' user password system properties should be set. " +
119120
"Uri: '" + uri + "', Password: '" + password + "'" );
120121
}
121-
if ( uri != null && !BOLT_ROUTING_URI_SCHEME.equals( uri.getScheme() ) )
122+
if ( uri != null && !BOLT_ROUTING_URI_SCHEME.equals( uri.getScheme() ) && !NEO4J_URI_SCHEME.equals( uri.getScheme() ) )
122123
{
123-
throw new IllegalStateException( "Cluster uri should have bolt+routing scheme: '" + uri + "'" );
124+
throw new IllegalStateException( "Cluster uri should have bolt+routing or neo4j scheme: '" + uri + "'" );
124125
}
125126
}
126127

0 commit comments

Comments
 (0)