19
19
20
20
import static org .assertj .core .api .Assertions .assertThat ;
21
21
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
22
+ import static org .mockito .Mockito .mock ;
22
23
import static org .mockito .Mockito .when ;
23
24
import static org .mockito .MockitoAnnotations .initMocks ;
24
25
28
29
import com .datastax .oss .driver .api .core .config .DriverConfig ;
29
30
import com .datastax .oss .driver .api .core .config .DriverConfigLoader ;
30
31
import com .datastax .oss .driver .api .core .config .DriverExecutionProfile ;
32
+ import com .datastax .oss .driver .api .core .loadbalancing .LoadBalancingPolicy ;
33
+ import com .datastax .oss .driver .api .core .loadbalancing .LocalDcAwareLoadBalancingPolicy ;
31
34
import com .datastax .oss .driver .api .core .session .ProgrammaticArguments ;
32
35
import com .datastax .oss .driver .api .core .session .Session ;
33
36
import com .datastax .oss .driver .api .core .uuid .Uuids ;
34
37
import com .datastax .oss .driver .internal .core .context .DefaultDriverContext ;
35
38
import com .datastax .oss .driver .internal .core .context .StartupOptionsBuilder ;
39
+ import com .datastax .oss .driver .shaded .guava .common .collect .ImmutableMap ;
36
40
import com .datastax .oss .protocol .internal .request .Startup ;
37
41
import com .tngtech .java .junit .dataprovider .DataProvider ;
38
42
import com .tngtech .java .junit .dataprovider .DataProviderRunner ;
43
+ import edu .umd .cs .findbugs .annotations .NonNull ;
44
+ import java .util .Map ;
39
45
import java .util .UUID ;
40
46
import org .junit .Before ;
41
47
import org .junit .Test ;
@@ -61,16 +67,34 @@ public void before() {
61
67
}
62
68
63
69
private void buildContext (
64
- UUID clientId , String applicationName , String applicationVersion , String localDc ) {
70
+ UUID clientId ,
71
+ String applicationName ,
72
+ String applicationVersion ,
73
+ Map <String , String > localDcPerProfile ) {
65
74
ProgrammaticArguments .Builder builder =
66
75
ProgrammaticArguments .builder ()
67
76
.withStartupClientId (clientId )
68
77
.withStartupApplicationName (applicationName )
69
78
.withStartupApplicationVersion (applicationVersion );
70
- if (localDc != null ) {
71
- builder .withLocalDatacenter (DriverExecutionProfile .DEFAULT_NAME , localDc );
72
- }
73
- this .driverContext = new DefaultDriverContext (configLoader , builder .build ());
79
+ this .driverContext =
80
+ new DefaultDriverContext (configLoader , builder .build ()) {
81
+ @ NonNull
82
+ @ Override
83
+ public Map <String , LoadBalancingPolicy > getLoadBalancingPolicies () {
84
+ if (localDcPerProfile != null ) {
85
+ ImmutableMap .Builder <String , LoadBalancingPolicy > map = ImmutableMap .builder ();
86
+ localDcPerProfile .forEach (
87
+ (profile , dc ) -> {
88
+ LocalDcAwareLoadBalancingPolicy loadBalancingPolicy =
89
+ mock (LocalDcAwareLoadBalancingPolicy .class );
90
+ when (loadBalancingPolicy .getLocalDatacenter ()).thenReturn (dc );
91
+ map .put (profile , loadBalancingPolicy );
92
+ });
93
+ return map .build ();
94
+ }
95
+ return super .getLoadBalancingPolicies ();
96
+ }
97
+ };
74
98
}
75
99
76
100
private void assertDefaultStartupOptions (Startup startup ) {
@@ -157,13 +181,14 @@ public void should_build_startup_options_with_all_options() {
157
181
158
182
UUID customClientId = Uuids .random ();
159
183
160
- buildContext (customClientId , "Custom_App_Name" , "Custom_App_Version" , "dc6" );
184
+ buildContext (
185
+ customClientId , "Custom_App_Name" , "Custom_App_Version" , ImmutableMap .of ("default" , "dc6" ));
161
186
Startup startup = new Startup (driverContext .getStartupOptions ());
162
187
assertThat (startup .options )
163
188
.containsEntry (StartupOptionsBuilder .CLIENT_ID_KEY , customClientId .toString ())
164
189
.containsEntry (StartupOptionsBuilder .APPLICATION_NAME_KEY , "Custom_App_Name" )
165
190
.containsEntry (StartupOptionsBuilder .APPLICATION_VERSION_KEY , "Custom_App_Version" )
166
- .containsEntry (StartupOptionsBuilder .DRIVER_LOCAL_DC , "dc6" );
191
+ .containsEntry (StartupOptionsBuilder .DRIVER_LOCAL_DC , "default: dc6" );
167
192
assertThat (startup .options ).containsEntry (Startup .COMPRESSION_KEY , "snappy" );
168
193
assertDefaultStartupOptions (startup );
169
194
}
@@ -176,8 +201,6 @@ public void should_use_configuration_when_no_programmatic_values_provided() {
176
201
.thenReturn ("Config_App_Version" );
177
202
when (defaultProfile .getString (DefaultDriverOption .PROTOCOL_COMPRESSION , "none" ))
178
203
.thenReturn ("none" );
179
- when (defaultProfile .getString (DefaultDriverOption .LOAD_BALANCING_LOCAL_DATACENTER ))
180
- .thenReturn ("Config_DC_1" );
181
204
when (defaultProfile .isDefined (DefaultDriverOption .LOAD_BALANCING_LOCAL_DATACENTER ))
182
205
.thenReturn (true );
183
206
when (defaultProfile .getName ()).thenReturn (DriverExecutionProfile .DEFAULT_NAME );
@@ -187,8 +210,7 @@ public void should_use_configuration_when_no_programmatic_values_provided() {
187
210
188
211
assertThat (startup .options )
189
212
.containsEntry (StartupOptionsBuilder .APPLICATION_NAME_KEY , "Config_App_Name" )
190
- .containsEntry (StartupOptionsBuilder .APPLICATION_VERSION_KEY , "Config_App_Version" )
191
- .containsEntry (StartupOptionsBuilder .DRIVER_LOCAL_DC , "Config_DC_1" );
213
+ .containsEntry (StartupOptionsBuilder .APPLICATION_VERSION_KEY , "Config_App_Version" );
192
214
}
193
215
194
216
@ Test
@@ -197,12 +219,34 @@ public void should_ignore_configuration_when_programmatic_values_provided() {
197
219
.thenReturn ("none" );
198
220
when (defaultProfile .getName ()).thenReturn (DriverExecutionProfile .DEFAULT_NAME );
199
221
200
- buildContext (null , "Custom_App_Name" , "Custom_App_Version" , "us-west-2" );
222
+ buildContext (
223
+ null , "Custom_App_Name" , "Custom_App_Version" , ImmutableMap .of ("default" , "us-west-2" ));
201
224
Startup startup = new Startup (driverContext .getStartupOptions ());
202
225
203
226
assertThat (startup .options )
204
227
.containsEntry (StartupOptionsBuilder .APPLICATION_NAME_KEY , "Custom_App_Name" )
205
228
.containsEntry (StartupOptionsBuilder .APPLICATION_VERSION_KEY , "Custom_App_Version" )
206
- .containsEntry (StartupOptionsBuilder .DRIVER_LOCAL_DC , "us-west-2" );
229
+ .containsEntry (StartupOptionsBuilder .DRIVER_LOCAL_DC , "default: us-west-2" );
230
+ }
231
+
232
+ @ Test
233
+ public void should_include_all_local_dc_in_startup_message () {
234
+ when (defaultProfile .getString (DefaultDriverOption .PROTOCOL_COMPRESSION , "none" ))
235
+ .thenReturn ("none" );
236
+ when (defaultProfile .getName ()).thenReturn (DriverExecutionProfile .DEFAULT_NAME );
237
+
238
+ buildContext (
239
+ null ,
240
+ "Custom_App_Name" ,
241
+ "Custom_App_Version" ,
242
+ ImmutableMap .of ("default" , "us-west-2" , "oltp" , "us-east-2" , "olap" , "eu-central-1" ));
243
+ Startup startup = new Startup (driverContext .getStartupOptions ());
244
+
245
+ assertThat (startup .options )
246
+ .containsEntry (StartupOptionsBuilder .APPLICATION_NAME_KEY , "Custom_App_Name" )
247
+ .containsEntry (StartupOptionsBuilder .APPLICATION_VERSION_KEY , "Custom_App_Version" )
248
+ .containsEntry (
249
+ StartupOptionsBuilder .DRIVER_LOCAL_DC ,
250
+ "default: us-west-2, oltp: us-east-2, olap: eu-central-1" );
207
251
}
208
252
}
0 commit comments