24
24
import java .util .HashSet ;
25
25
import java .util .Map ;
26
26
import java .util .Set ;
27
+ import java .util .concurrent .CompletionStage ;
27
28
28
29
import org .neo4j .driver .internal .InternalRecord ;
30
+ import org .neo4j .driver .internal .async .AsyncConnection ;
29
31
import org .neo4j .driver .internal .net .BoltServerAddress ;
30
- import org .neo4j .driver .internal .spi .PooledConnection ;
31
32
import org .neo4j .driver .internal .util .Clock ;
32
33
import org .neo4j .driver .internal .value .StringValue ;
33
34
import org .neo4j .driver .v1 .Record ;
37
38
import org .neo4j .driver .v1 .exceptions .ServiceUnavailableException ;
38
39
39
40
import static java .util .Arrays .asList ;
41
+ import static java .util .concurrent .CompletableFuture .completedFuture ;
40
42
import static org .hamcrest .MatcherAssert .assertThat ;
41
43
import static org .hamcrest .Matchers .containsString ;
42
44
import static org .hamcrest .Matchers .instanceOf ;
45
47
import static org .mockito .Mockito .doThrow ;
46
48
import static org .mockito .Mockito .mock ;
47
49
import static org .mockito .Mockito .when ;
50
+ import static org .neo4j .driver .internal .async .Futures .getBlocking ;
48
51
import static org .neo4j .driver .internal .logging .DevNullLogger .DEV_NULL_LOGGER ;
49
52
import static org .neo4j .driver .v1 .Values .value ;
50
53
51
- public class ClusterCompositionProviderTest
54
+ public class RoutingProcedureClusterCompositionProviderTest
52
55
{
53
56
@ Test
54
- public void shouldProtocolErrorWhenNoRecord () throws Throwable
57
+ public void shouldProtocolErrorWhenNoRecord ()
55
58
{
56
59
// Given
57
60
RoutingProcedureRunner mockedRunner = newProcedureRunnerMock ();
58
61
ClusterCompositionProvider provider = new RoutingProcedureClusterCompositionProvider ( mock ( Clock .class ),
59
62
DEV_NULL_LOGGER , mockedRunner );
60
63
61
- PooledConnection mockedConn = mock ( PooledConnection .class );
64
+ CompletionStage < AsyncConnection > connectionStage = completedFuture ( mock ( AsyncConnection .class ) );
62
65
RoutingProcedureResponse noRecordsResponse = newRoutingResponse ();
63
- when ( mockedRunner .run ( mockedConn ) ).thenReturn ( noRecordsResponse );
66
+ when ( mockedRunner .run ( connectionStage ) ).thenReturn ( completedFuture ( noRecordsResponse ) );
64
67
65
68
// When
66
- ClusterCompositionResponse response = provider .getClusterComposition ( mockedConn );
69
+ ClusterCompositionResponse response = getBlocking ( provider .getClusterComposition ( connectionStage ) );
67
70
68
71
// Then
69
72
assertThat ( response , instanceOf ( ClusterCompositionResponse .Failure .class ) );
@@ -80,20 +83,20 @@ public void shouldProtocolErrorWhenNoRecord() throws Throwable
80
83
}
81
84
82
85
@ Test
83
- public void shouldProtocolErrorWhenMoreThanOneRecord () throws Throwable
86
+ public void shouldProtocolErrorWhenMoreThanOneRecord ()
84
87
{
85
88
// Given
86
89
RoutingProcedureRunner mockedRunner = newProcedureRunnerMock ();
87
90
ClusterCompositionProvider provider = new RoutingProcedureClusterCompositionProvider ( mock ( Clock .class ),
88
91
DEV_NULL_LOGGER , mockedRunner );
89
92
90
- PooledConnection mockedConn = mock ( PooledConnection .class );
93
+ CompletionStage < AsyncConnection > connectionStage = completedFuture ( mock ( AsyncConnection .class ) );
91
94
Record aRecord = new InternalRecord ( asList ( "key1" , "key2" ), new Value []{ new StringValue ( "a value" ) } );
92
95
RoutingProcedureResponse routingResponse = newRoutingResponse ( aRecord , aRecord );
93
- when ( mockedRunner .run ( mockedConn ) ).thenReturn ( routingResponse );
96
+ when ( mockedRunner .run ( connectionStage ) ).thenReturn ( completedFuture ( routingResponse ) );
94
97
95
98
// When
96
- ClusterCompositionResponse response = provider .getClusterComposition ( mockedConn );
99
+ ClusterCompositionResponse response = getBlocking ( provider .getClusterComposition ( connectionStage ) );
97
100
98
101
// Then
99
102
assertThat ( response , instanceOf ( ClusterCompositionResponse .Failure .class ) );
@@ -110,20 +113,20 @@ public void shouldProtocolErrorWhenMoreThanOneRecord() throws Throwable
110
113
}
111
114
112
115
@ Test
113
- public void shouldProtocolErrorWhenUnparsableRecord () throws Throwable
116
+ public void shouldProtocolErrorWhenUnparsableRecord ()
114
117
{
115
118
// Given
116
119
RoutingProcedureRunner mockedRunner = newProcedureRunnerMock ();
117
120
ClusterCompositionProvider provider = new RoutingProcedureClusterCompositionProvider ( mock ( Clock .class ),
118
121
DEV_NULL_LOGGER , mockedRunner );
119
122
120
- PooledConnection mockedConn = mock ( PooledConnection .class );
123
+ CompletionStage < AsyncConnection > connectionStage = completedFuture ( mock ( AsyncConnection .class ) );
121
124
Record aRecord = new InternalRecord ( asList ( "key1" , "key2" ), new Value []{ new StringValue ( "a value" ) } );
122
125
RoutingProcedureResponse routingResponse = newRoutingResponse ( aRecord );
123
- when ( mockedRunner .run ( mockedConn ) ).thenReturn ( routingResponse );
126
+ when ( mockedRunner .run ( connectionStage ) ).thenReturn ( completedFuture ( routingResponse ) );
124
127
125
128
// When
126
- ClusterCompositionResponse response = provider .getClusterComposition ( mockedConn );
129
+ ClusterCompositionResponse response = getBlocking ( provider .getClusterComposition ( connectionStage ) );
127
130
128
131
// Then
129
132
assertThat ( response , instanceOf ( ClusterCompositionResponse .Failure .class ) );
@@ -140,26 +143,26 @@ public void shouldProtocolErrorWhenUnparsableRecord() throws Throwable
140
143
}
141
144
142
145
@ Test
143
- public void shouldProtocolErrorWhenNoRouters () throws Throwable
146
+ public void shouldProtocolErrorWhenNoRouters ()
144
147
{
145
148
// Given
146
149
RoutingProcedureRunner mockedRunner = newProcedureRunnerMock ();
147
150
Clock mockedClock = mock ( Clock .class );
148
151
ClusterCompositionProvider provider = new RoutingProcedureClusterCompositionProvider ( mockedClock ,
149
152
DEV_NULL_LOGGER , mockedRunner );
150
153
151
- PooledConnection mockedConn = mock ( PooledConnection .class );
154
+ CompletionStage < AsyncConnection > connectionStage = completedFuture ( mock ( AsyncConnection .class ) );
152
155
Record record = new InternalRecord ( asList ( "ttl" , "servers" ), new Value []{
153
156
value ( 100 ), value ( asList (
154
157
serverInfo ( "READ" , "one:1337" , "two:1337" ),
155
158
serverInfo ( "WRITE" , "one:1337" ) ) )
156
159
} );
157
160
RoutingProcedureResponse routingResponse = newRoutingResponse ( record );
158
- when ( mockedRunner .run ( mockedConn ) ).thenReturn ( routingResponse );
161
+ when ( mockedRunner .run ( connectionStage ) ).thenReturn ( completedFuture ( routingResponse ) );
159
162
when ( mockedClock .millis () ).thenReturn ( 12345L );
160
163
161
164
// When
162
- ClusterCompositionResponse response = provider .getClusterComposition ( mockedConn );
165
+ ClusterCompositionResponse response = getBlocking ( provider .getClusterComposition ( connectionStage ) );
163
166
164
167
// Then
165
168
assertThat ( response , instanceOf ( ClusterCompositionResponse .Failure .class ) );
@@ -176,26 +179,26 @@ public void shouldProtocolErrorWhenNoRouters() throws Throwable
176
179
}
177
180
178
181
@ Test
179
- public void shouldProtocolErrorWhenNoReaders () throws Throwable
182
+ public void shouldProtocolErrorWhenNoReaders ()
180
183
{
181
184
// Given
182
185
RoutingProcedureRunner mockedRunner = newProcedureRunnerMock ();
183
186
Clock mockedClock = mock ( Clock .class );
184
187
ClusterCompositionProvider provider = new RoutingProcedureClusterCompositionProvider ( mockedClock ,
185
188
DEV_NULL_LOGGER , mockedRunner );
186
189
187
- PooledConnection mockedConn = mock ( PooledConnection .class );
190
+ CompletionStage < AsyncConnection > connectionStage = completedFuture ( mock ( AsyncConnection .class ) );
188
191
Record record = new InternalRecord ( asList ( "ttl" , "servers" ), new Value []{
189
192
value ( 100 ), value ( asList (
190
193
serverInfo ( "WRITE" , "one:1337" ),
191
194
serverInfo ( "ROUTE" , "one:1337" , "two:1337" ) ) )
192
195
} );
193
196
RoutingProcedureResponse routingResponse = newRoutingResponse ( record );
194
- when ( mockedRunner .run ( mockedConn ) ).thenReturn ( routingResponse );
197
+ when ( mockedRunner .run ( connectionStage ) ).thenReturn ( completedFuture ( routingResponse ) );
195
198
when ( mockedClock .millis () ).thenReturn ( 12345L );
196
199
197
200
// When
198
- ClusterCompositionResponse response = provider .getClusterComposition ( mockedConn );
201
+ ClusterCompositionResponse response = getBlocking ( provider .getClusterComposition ( connectionStage ) );
199
202
200
203
// Then
201
204
assertThat ( response , instanceOf ( ClusterCompositionResponse .Failure .class ) );
@@ -213,26 +216,26 @@ public void shouldProtocolErrorWhenNoReaders() throws Throwable
213
216
214
217
215
218
@ Test
216
- public void shouldPropagateConnectionFailureExceptions () throws Exception
219
+ public void shouldPropagateConnectionFailureExceptions ()
217
220
{
218
221
// Given
219
222
RoutingProcedureRunner mockedRunner = newProcedureRunnerMock ();
220
223
ClusterCompositionProvider provider = new RoutingProcedureClusterCompositionProvider ( mock ( Clock .class ),
221
224
DEV_NULL_LOGGER , mockedRunner );
222
225
223
- PooledConnection mockedConn = mock ( PooledConnection .class );
226
+ CompletionStage < AsyncConnection > connectionStage = completedFuture ( mock ( AsyncConnection .class ) );
224
227
Record record = new InternalRecord ( asList ( "ttl" , "servers" ), new Value []{
225
228
value ( 100 ), value ( asList (
226
229
serverInfo ( "WRITE" , "one:1337" ),
227
230
serverInfo ( "ROUTE" , "one:1337" , "two:1337" ) ) )
228
231
} );
229
232
doThrow ( new ServiceUnavailableException ( "Connection breaks during cypher execution" ) )
230
- .when ( mockedRunner ).run ( mockedConn );
233
+ .when ( mockedRunner ).run ( connectionStage );
231
234
232
235
// When & Then
233
236
try
234
237
{
235
- provider .getClusterComposition ( mockedConn );
238
+ provider .getClusterComposition ( connectionStage );
236
239
fail ( "Expecting a failure but not triggered." );
237
240
}
238
241
catch ( Exception e )
@@ -243,27 +246,27 @@ public void shouldPropagateConnectionFailureExceptions() throws Exception
243
246
}
244
247
245
248
@ Test
246
- public void shouldReturnSuccessResultWhenNoError () throws Throwable
249
+ public void shouldReturnSuccessResultWhenNoError ()
247
250
{
248
251
// Given
249
252
Clock mockedClock = mock ( Clock .class );
250
253
RoutingProcedureRunner mockedRunner = newProcedureRunnerMock ();
251
254
ClusterCompositionProvider provider = new RoutingProcedureClusterCompositionProvider ( mockedClock ,
252
255
DEV_NULL_LOGGER , mockedRunner );
253
256
254
- PooledConnection mockedConn = mock ( PooledConnection .class );
257
+ CompletionStage < AsyncConnection > connectionStage = completedFuture ( mock ( AsyncConnection .class ) );
255
258
Record record = new InternalRecord ( asList ( "ttl" , "servers" ), new Value []{
256
259
value ( 100 ), value ( asList (
257
260
serverInfo ( "READ" , "one:1337" , "two:1337" ),
258
261
serverInfo ( "WRITE" , "one:1337" ),
259
262
serverInfo ( "ROUTE" , "one:1337" , "two:1337" ) ) )
260
263
} );
261
264
RoutingProcedureResponse routingResponse = newRoutingResponse ( record );
262
- when ( mockedRunner .run ( mockedConn ) ).thenReturn ( routingResponse );
265
+ when ( mockedRunner .run ( connectionStage ) ).thenReturn ( completedFuture ( routingResponse ) );
263
266
when ( mockedClock .millis () ).thenReturn ( 12345L );
264
267
265
268
// When
266
- ClusterCompositionResponse response = provider .getClusterComposition ( mockedConn );
269
+ ClusterCompositionResponse response = getBlocking ( provider .getClusterComposition ( connectionStage ) );
267
270
268
271
// Then
269
272
assertThat ( response , instanceOf ( ClusterCompositionResponse .Success .class ) );
0 commit comments