22
22
23
23
import java .net .URI ;
24
24
import java .util .List ;
25
+ import java .util .concurrent .CompletionStage ;
25
26
27
+ import org .neo4j .driver .internal .async .AsyncConnection ;
26
28
import org .neo4j .driver .internal .net .BoltServerAddress ;
27
29
import org .neo4j .driver .internal .spi .Connection ;
28
30
import org .neo4j .driver .internal .summary .InternalServerInfo ;
29
31
import org .neo4j .driver .v1 .Record ;
30
32
import org .neo4j .driver .v1 .Statement ;
31
33
import org .neo4j .driver .v1 .Value ;
34
+ import org .neo4j .driver .v1 .exceptions .ClientException ;
32
35
36
+ import static java .util .Arrays .asList ;
33
37
import static java .util .Collections .EMPTY_MAP ;
34
- import static org .hamcrest .MatcherAssert .assertThat ;
38
+ import static java .util .Collections .singletonList ;
39
+ import static java .util .concurrent .CompletableFuture .completedFuture ;
35
40
import static org .hamcrest .core .IsEqual .equalTo ;
41
+ import static org .junit .Assert .assertEquals ;
42
+ import static org .junit .Assert .assertFalse ;
43
+ import static org .junit .Assert .assertThat ;
44
+ import static org .junit .Assert .assertTrue ;
45
+ import static org .junit .Assert .fail ;
36
46
import static org .mockito .Mockito .mock ;
37
47
import static org .mockito .Mockito .when ;
48
+ import static org .neo4j .driver .internal .async .Futures .failedFuture ;
49
+ import static org .neo4j .driver .internal .async .Futures .getBlocking ;
38
50
import static org .neo4j .driver .internal .cluster .RoutingProcedureRunner .GET_ROUTING_TABLE ;
39
51
import static org .neo4j .driver .internal .cluster .RoutingProcedureRunner .GET_ROUTING_TABLE_PARAM ;
40
52
import static org .neo4j .driver .internal .cluster .RoutingProcedureRunner .GET_SERVERS ;
@@ -58,6 +70,20 @@ public void shouldCallGetRoutingTableWithEmptyMap() throws Throwable
58
70
new Statement ( "CALL " + GET_ROUTING_TABLE , parameters ( GET_ROUTING_TABLE_PARAM , EMPTY_MAP ) ) ) );
59
71
}
60
72
73
+ @ Test
74
+ public void shouldCallGetRoutingTableWithEmptyMapAsync ()
75
+ {
76
+ RoutingProcedureRunner runner = new TestRoutingProcedureRunner ( RoutingContext .EMPTY ,
77
+ completedFuture ( asList ( mock ( Record .class ), mock ( Record .class ) ) ) );
78
+
79
+ RoutingProcedureResponse response = getBlocking ( runner .run ( connectionStage ( "Neo4j/3.2.1" ) ) );
80
+
81
+ assertTrue ( response .isSuccess () );
82
+ assertEquals ( 2 , response .records ().size () );
83
+ assertEquals ( new Statement ( "CALL " + GET_ROUTING_TABLE , parameters ( GET_ROUTING_TABLE_PARAM , EMPTY_MAP ) ),
84
+ response .procedure () );
85
+ }
86
+
61
87
@ Test
62
88
public void shouldCallGetRoutingTableWithParam () throws Throwable
63
89
{
@@ -77,6 +103,23 @@ public void shouldCallGetRoutingTableWithParam() throws Throwable
77
103
new Statement ( "CALL " + GET_ROUTING_TABLE , expectedParams ) ) );
78
104
}
79
105
106
+ @ Test
107
+ public void shouldCallGetRoutingTableWithParamAsync ()
108
+ {
109
+ URI uri = URI .create ( "bolt+routing://localhost/?key1=value1&key2=value2" );
110
+ RoutingContext context = new RoutingContext ( uri );
111
+
112
+ RoutingProcedureRunner runner = new TestRoutingProcedureRunner ( context ,
113
+ completedFuture ( singletonList ( mock ( Record .class ) ) ) );
114
+
115
+ RoutingProcedureResponse response = getBlocking ( runner .run ( connectionStage ( "Neo4j/3.2.1" ) ) );
116
+
117
+ assertTrue ( response .isSuccess () );
118
+ assertEquals ( 1 , response .records ().size () );
119
+ Value expectedParams = parameters ( GET_ROUTING_TABLE_PARAM , context .asMap () );
120
+ assertEquals ( new Statement ( "CALL " + GET_ROUTING_TABLE , expectedParams ), response .procedure () );
121
+ }
122
+
80
123
@ Test
81
124
public void shouldCallGetServers () throws Throwable
82
125
{
@@ -95,11 +138,72 @@ public void shouldCallGetServers() throws Throwable
95
138
new Statement ( "CALL " + GET_SERVERS ) ) );
96
139
}
97
140
141
+ @ Test
142
+ public void shouldCallGetServersAsync ()
143
+ {
144
+ URI uri = URI .create ( "bolt+routing://localhost/?key1=value1&key2=value2" );
145
+ RoutingContext context = new RoutingContext ( uri );
146
+
147
+ RoutingProcedureRunner runner = new TestRoutingProcedureRunner ( context ,
148
+ completedFuture ( asList ( mock ( Record .class ), mock ( Record .class ) ) ) );
149
+
150
+ RoutingProcedureResponse response = getBlocking ( runner .run ( connectionStage ( "Neo4j/3.1.8" ) ) );
151
+
152
+ assertTrue ( response .isSuccess () );
153
+ assertEquals ( 2 , response .records ().size () );
154
+ assertEquals ( new Statement ( "CALL " + GET_SERVERS ), response .procedure () );
155
+ }
156
+
157
+ @ Test
158
+ public void shouldReturnFailedResponseOnClientException ()
159
+ {
160
+ ClientException error = new ClientException ( "Hi" );
161
+ RoutingProcedureRunner runner = new TestRoutingProcedureRunner ( RoutingContext .EMPTY , failedFuture ( error ) );
162
+
163
+ RoutingProcedureResponse response = getBlocking ( runner .run ( connectionStage ( "Neo4j/3.2.2" ) ) );
164
+
165
+ assertFalse ( response .isSuccess () );
166
+ assertEquals ( error , response .error () );
167
+ }
168
+
169
+ @ Test
170
+ public void shouldReturnFailedStageOnError ()
171
+ {
172
+ Exception error = new Exception ( "Hi" );
173
+ RoutingProcedureRunner runner = new TestRoutingProcedureRunner ( RoutingContext .EMPTY , failedFuture ( error ) );
174
+
175
+ try
176
+ {
177
+ getBlocking ( runner .run ( connectionStage ( "Neo4j/3.2.2" ) ) );
178
+ fail ( "Exception expected" );
179
+ }
180
+ catch ( Exception e )
181
+ {
182
+ assertEquals ( error , e );
183
+ }
184
+ }
185
+
186
+ private static CompletionStage <AsyncConnection > connectionStage ( String serverVersion )
187
+ {
188
+ AsyncConnection connection = mock ( AsyncConnection .class );
189
+ InternalServerInfo serverInfo = new InternalServerInfo ( new BoltServerAddress ( "123:45" ), serverVersion );
190
+ when ( connection .serverInfo () ).thenReturn ( serverInfo );
191
+ return completedFuture ( connection );
192
+ }
193
+
98
194
private static class TestRoutingProcedureRunner extends RoutingProcedureRunner
99
195
{
196
+ final CompletionStage <List <Record >> runProcedureResult ;
197
+
100
198
TestRoutingProcedureRunner ( RoutingContext context )
199
+ {
200
+ this ( context , null );
201
+ }
202
+
203
+ TestRoutingProcedureRunner ( RoutingContext context , CompletionStage <List <Record >> runProcedureResult )
101
204
{
102
205
super ( context );
206
+ this .runProcedureResult = runProcedureResult ;
103
207
}
104
208
105
209
@ Override
@@ -108,6 +212,12 @@ List<Record> runProcedure( Connection connection, Statement procedure )
108
212
// I do not want any network traffic
109
213
return null ;
110
214
}
215
+
216
+ @ Override
217
+ CompletionStage <List <Record >> runProcedure ( AsyncConnection connection , Statement procedure )
218
+ {
219
+ return runProcedureResult ;
220
+ }
111
221
}
112
222
113
223
}
0 commit comments