|
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.anyString;
|
42 | 48 | import static org.mockito.ArgumentMatchers.eq;
|
| 49 | +import static org.mockito.Mockito.doThrow; |
43 | 50 | import static org.mockito.Mockito.mock;
|
44 | 51 | import static org.mockito.Mockito.verify;
|
45 | 52 | import static org.mockito.Mockito.when;
|
46 | 53 | 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; |
49 | 54 | import static org.neo4j.driver.util.StubServer.INSECURE_CONFIG;
|
50 | 55 |
|
51 | 56 | class GraphDatabaseTest
|
52 | 57 | {
|
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 |
| - |
91 | 58 | @Test
|
92 | 59 | void throwsWhenBoltSchemeUsedWithRoutingParams()
|
93 | 60 | {
|
94 | 61 | assertThrows( IllegalArgumentException.class, () -> GraphDatabase.driver( "bolt://localhost:7687/?policy=my_policy" ) );
|
95 | 62 | }
|
96 | 63 |
|
97 | 64 | @Test
|
98 |
| - void shouldLogWhenUnableToCreateRoutingDriver() throws Exception |
| 65 | + void shouldLogWhenUnableToCreateRoutingDriver() |
99 | 66 | {
|
100 |
| - StubServer server1 = StubServer.start( "discover_not_supported_9001.script", 9001 ); |
101 |
| - StubServer server2 = StubServer.start( "discover_not_supported_9002.script", 9002 ); |
102 |
| - |
103 | 67 | Logging logging = mock( Logging.class );
|
104 | 68 | Logger logger = mock( Logger.class );
|
105 | 69 | 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 ); |
107 | 73 | Config config = Config.builder()
|
108 |
| - .withoutEncryption() |
109 |
| - .withLogging( logging ) |
110 |
| - .build(); |
| 74 | + .withLogging( logging ) |
| 75 | + .build(); |
111 | 76 |
|
112 | 77 | List<URI> routingUris = asList(
|
113 | 78 | URI.create( "neo4j://localhost:9001" ),
|
114 | 79 | URI.create( "neo4j://localhost:9002" ) );
|
115 | 80 |
|
116 |
| - assertThrows( ServiceUnavailableException.class, () -> GraphDatabase.routingDriver( routingUris, AuthTokens.none(), config ) ); |
| 81 | + assertThrows( ServiceUnavailableException.class, () -> GraphDatabase.routingDriver( routingUris, AuthTokens.none(), config, driverFactory ) ); |
117 | 82 |
|
118 | 83 | verify( logger ).warn( eq( "Unable to create routing driver for URI: neo4j://localhost:9001" ),
|
119 | 84 | any( Throwable.class ) );
|
120 | 85 |
|
121 | 86 | verify( logger ).warn( eq( "Unable to create routing driver for URI: neo4j://localhost:9002" ),
|
122 | 87 | any( Throwable.class ) );
|
123 |
| - |
124 |
| - assertEquals( 0, server1.exitStatus() ); |
125 |
| - assertEquals( 0, server2.exitStatus() ); |
126 | 88 | }
|
127 | 89 |
|
128 | 90 | @Test
|
@@ -215,4 +177,22 @@ private static Config createConfig( boolean encrypted, int timeoutMillis )
|
215 | 177 |
|
216 | 178 | return configBuilder.build();
|
217 | 179 | }
|
| 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 | + } |
218 | 198 | }
|
0 commit comments