|
18 | 18 | */
|
19 | 19 | package org.neo4j.driver;
|
20 | 20 |
|
| 21 | +import io.netty.util.concurrent.EventExecutorGroup; |
21 | 22 | import org.junit.jupiter.api.Test;
|
22 | 23 |
|
23 | 24 | import java.io.IOException;
|
|
26 | 27 | import java.util.List;
|
27 | 28 |
|
28 | 29 | 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; |
30 | 38 | import org.neo4j.driver.util.TestUtil;
|
31 | 39 |
|
32 | 40 | import static java.util.Arrays.asList;
|
33 | 41 | import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
34 | 42 | import static org.hamcrest.Matchers.containsString;
|
35 |
| -import static org.hamcrest.Matchers.is; |
36 |
| -import static org.hamcrest.core.IsEqual.equalTo; |
37 | 43 | import static org.hamcrest.junit.MatcherAssert.assertThat;
|
38 | 44 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
39 | 45 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
40 | 46 | import static org.mockito.ArgumentMatchers.any;
|
41 | 47 | import static org.mockito.ArgumentMatchers.eq;
|
| 48 | +import static org.mockito.Mockito.doThrow; |
42 | 49 | import static org.mockito.Mockito.mock;
|
43 | 50 | import static org.mockito.Mockito.verify;
|
44 | 51 | import static org.mockito.Mockito.when;
|
45 | 52 | 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; |
48 | 53 | import static org.neo4j.driver.util.StubServer.INSECURE_CONFIG;
|
49 | 54 |
|
50 | 55 | class GraphDatabaseTest
|
51 | 56 | {
|
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 |
| - |
90 | 57 | @Test
|
91 | 58 | void throwsWhenBoltSchemeUsedWithRoutingParams()
|
92 | 59 | {
|
93 | 60 | assertThrows( IllegalArgumentException.class, () -> GraphDatabase.driver( "bolt://localhost:7687/?policy=my_policy" ) );
|
94 | 61 | }
|
95 | 62 |
|
96 | 63 | @Test
|
97 |
| - void shouldLogWhenUnableToCreateRoutingDriver() throws Exception |
| 64 | + void shouldLogWhenUnableToCreateRoutingDriver() |
98 | 65 | {
|
99 |
| - StubServer server1 = StubServer.start( "discover_not_supported_9001.script", 9001 ); |
100 |
| - StubServer server2 = StubServer.start( "discover_not_supported_9002.script", 9002 ); |
101 |
| - |
102 | 66 | Logging logging = mock( Logging.class );
|
103 | 67 | Logger logger = mock( Logger.class );
|
104 | 68 | 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 ); |
106 | 72 | Config config = Config.builder()
|
107 |
| - .withoutEncryption() |
108 |
| - .withLogging( logging ) |
109 |
| - .build(); |
| 73 | + .withLogging( logging ) |
| 74 | + .build(); |
110 | 75 |
|
111 | 76 | List<URI> routingUris = asList(
|
112 | 77 | URI.create( "neo4j://localhost:9001" ),
|
113 | 78 | URI.create( "neo4j://localhost:9002" ) );
|
114 | 79 |
|
115 |
| - assertThrows( ServiceUnavailableException.class, () -> GraphDatabase.routingDriver( routingUris, AuthTokens.none(), config ) ); |
| 80 | + assertThrows( ServiceUnavailableException.class, () -> GraphDatabase.routingDriver( routingUris, AuthTokens.none(), config, driverFactory ) ); |
116 | 81 |
|
117 | 82 | verify( logger ).warn( eq( "Unable to create routing driver for URI: neo4j://localhost:9001" ),
|
118 | 83 | any( Throwable.class ) );
|
119 | 84 |
|
120 | 85 | verify( logger ).warn( eq( "Unable to create routing driver for URI: neo4j://localhost:9002" ),
|
121 | 86 | any( Throwable.class ) );
|
122 |
| - |
123 |
| - assertEquals( 0, server1.exitStatus() ); |
124 |
| - assertEquals( 0, server2.exitStatus() ); |
125 | 87 | }
|
126 | 88 |
|
127 | 89 | @Test
|
@@ -214,4 +176,22 @@ private static Config createConfig( boolean encrypted, int timeoutMillis )
|
214 | 176 |
|
215 | 177 | return configBuilder.build();
|
216 | 178 | }
|
| 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 | + } |
217 | 197 | }
|
0 commit comments