24
24
import org .apache .zookeeper .ZooDefs ;
25
25
import org .apache .zookeeper .client .ZKClientConfig ;
26
26
import org .apache .zookeeper .common .ClientX509Util ;
27
- import org .hamcrest .Matcher ;
28
- import org .hamcrest .core .IsNull ;
29
27
import org .junit .jupiter .api .BeforeEach ;
30
28
import org .junit .jupiter .api .Test ;
31
29
import org .mockito .ArgumentCaptor ;
35
33
36
34
import java .util .Arrays ;
37
35
38
- import static org .hamcrest .CoreMatchers .anyOf ;
39
- import static org .hamcrest .CoreMatchers .containsString ;
40
- import static org .hamcrest .CoreMatchers .is ;
41
- import static org .hamcrest .MatcherAssert .assertThat ;
36
+ import static org .assertj .core .api .Assertions .assertThat ;
42
37
import static org .junit .jupiter .api .Assertions .assertThrows ;
43
38
import static org .mockito .ArgumentCaptor .forClass ;
44
39
import static org .mockito .ArgumentMatchers .isA ;
@@ -219,14 +214,18 @@ public void testSSLConfiguration() {
219
214
verify (cfBuilder ).zkClientConfig (clientConfCaptor .capture ());
220
215
ZKClientConfig conf = clientConfCaptor .getValue ();
221
216
222
- assertThat (conf .getProperty (ZKClientConfig .SECURE_CLIENT ), is ("true" ) );
223
- assertThat (conf .getProperty (ZKClientConfig .ZOOKEEPER_CLIENT_CNXN_SOCKET ),
224
- is ("org.apache.zookeeper.ClientCnxnSocketNetty" ) );
217
+ assertThat (conf .getProperty (ZKClientConfig .SECURE_CLIENT )). isEqualTo ("true" );
218
+ assertThat (conf .getProperty (ZKClientConfig .ZOOKEEPER_CLIENT_CNXN_SOCKET ))
219
+ . isEqualTo ("org.apache.zookeeper.ClientCnxnSocketNetty" );
225
220
try (ClientX509Util sslOpts = new ClientX509Util ()) {
226
- assertThat (conf .getProperty (sslOpts .getSslKeystoreLocationProperty ()), is ("keystoreLoc" ));
227
- assertThat (conf .getProperty (sslOpts .getSslKeystorePasswdProperty ()), is ("ksPass" ));
228
- assertThat (conf .getProperty (sslOpts .getSslTruststoreLocationProperty ()), is ("truststoreLoc" ));
229
- assertThat (conf .getProperty (sslOpts .getSslTruststorePasswdProperty ()), is ("tsPass" ));
221
+ assertThat (conf .getProperty (sslOpts .getSslKeystoreLocationProperty ()))
222
+ .isEqualTo ("keystoreLoc" );
223
+ assertThat (conf .getProperty (sslOpts .getSslKeystorePasswdProperty ()))
224
+ .isEqualTo ("ksPass" );
225
+ assertThat (conf .getProperty (sslOpts .getSslTruststoreLocationProperty ()))
226
+ .isEqualTo ("truststoreLoc" );
227
+ assertThat (conf .getProperty (sslOpts .getSslTruststorePasswdProperty ()))
228
+ .isEqualTo ("tsPass" );
230
229
}
231
230
232
231
verifyDummyConnectionString ();
@@ -244,39 +243,39 @@ public void testNoConnectionString(){
244
243
clientConfigurer .withConnectionString (null );
245
244
246
245
Throwable t = assertThrows (NullPointerException .class , () -> clientConfigurer .create ());
247
- assertThat (t .getMessage (), containsString ("Zookeeper connection string cannot be null!" ) );
246
+ assertThat (t .getMessage ()). contains ("Zookeeper connection string cannot be null!" );
248
247
}
249
248
250
249
@ Test
251
250
public void testNoRetryPolicy () {
252
251
clientConfigurer .withRetryPolicy (null );
253
252
254
253
Throwable t = assertThrows (NullPointerException .class , () -> clientConfigurer .create ());
255
- assertThat (t .getMessage (), containsString ("Zookeeper connection retry policy cannot be null!" ) );
254
+ assertThat (t .getMessage ()). contains ("Zookeeper connection retry policy cannot be null!" );
256
255
}
257
256
258
257
@ Test
259
258
public void testNoAuthType () {
260
259
clientConfigurer .withAuthType (null );
261
260
262
261
Throwable t = assertThrows (NullPointerException .class , () -> clientConfigurer .create ());
263
- assertThat (t .getMessage (), containsString ("Zookeeper authType cannot be null!" ) );
262
+ assertThat (t .getMessage ()). contains ("Zookeeper authType cannot be null!" );
264
263
}
265
264
266
265
@ Test
267
266
public void testUnrecognizedAuthType () {
268
267
clientConfigurer .withAuthType ("something" );
269
268
270
269
Throwable t = assertThrows (IllegalArgumentException .class , () -> clientConfigurer .create ());
271
- assertThat (t .getMessage (), is ("Zookeeper authType must be one of [none, sasl]!" ) );
270
+ assertThat (t .getMessage ()). isEqualTo ("Zookeeper authType must be one of [none, sasl]!" );
272
271
}
273
272
274
273
@ Test
275
274
public void testSaslAuthTypeWithoutKeytab () {
276
275
clientConfigurer .withAuthType ("sasl" );
277
276
278
277
Throwable t = assertThrows (IllegalArgumentException .class , () -> clientConfigurer .create ());
279
- assertThat (t .getMessage (), is ("Zookeeper client's Kerberos Keytab must be specified!" ) );
278
+ assertThat (t .getMessage ()). isEqualTo ("Zookeeper client's Kerberos Keytab must be specified!" );
280
279
}
281
280
282
281
@ Test
@@ -286,7 +285,8 @@ public void testSaslAuthTypeWithEmptyKeytab() {
286
285
.withKeytab ("" );
287
286
288
287
Throwable t = assertThrows (IllegalArgumentException .class , () -> clientConfigurer .create ());
289
- assertThat (t .getMessage (), is ("Zookeeper client's Kerberos Keytab must be specified!" ));
288
+
289
+ assertThat (t .getMessage ()).isEqualTo ("Zookeeper client's Kerberos Keytab must be specified!" );
290
290
}
291
291
292
292
@ Test
@@ -296,7 +296,8 @@ public void testSaslAuthTypeWithoutPrincipal() {
296
296
.withKeytab ("keytabLoc" );
297
297
298
298
Throwable t = assertThrows (IllegalArgumentException .class , () -> clientConfigurer .create ());
299
- assertThat (t .getMessage (), is ("Zookeeper client's Kerberos Principal must be specified!" ));
299
+ assertThat (t .getMessage ()).isEqualTo (
300
+ "Zookeeper client's Kerberos Principal must be specified!" );
300
301
}
301
302
302
303
@ Test
@@ -307,7 +308,8 @@ public void testSaslAuthTypeWithEmptyPrincipal() {
307
308
.withPrincipal ("" );
308
309
309
310
Throwable t = assertThrows (IllegalArgumentException .class , () -> clientConfigurer .create ());
310
- assertThat (t .getMessage (), is ("Zookeeper client's Kerberos Principal must be specified!" ));
311
+ assertThat (t .getMessage ()).isEqualTo (
312
+ "Zookeeper client's Kerberos Principal must be specified!" );
311
313
}
312
314
313
315
@ Test
@@ -319,7 +321,7 @@ public void testSaslAuthTypeWithoutJaasLoginEntryName() {
319
321
.withJaasLoginEntryName (null );
320
322
321
323
Throwable t = assertThrows (IllegalArgumentException .class , () -> clientConfigurer .create ());
322
- assertThat (t .getMessage (), is ("JAAS Login Entry name must be specified!" ) );
324
+ assertThat (t .getMessage ()). isEqualTo ("JAAS Login Entry name must be specified!" );
323
325
}
324
326
325
327
@ Test
@@ -331,7 +333,7 @@ public void testSaslAuthTypeWithEmptyJaasLoginEntryName() {
331
333
.withJaasLoginEntryName ("" );
332
334
333
335
Throwable t = assertThrows (IllegalArgumentException .class , () -> clientConfigurer .create ());
334
- assertThat (t .getMessage (), is ("JAAS Login Entry name must be specified!" ) );
336
+ assertThat (t .getMessage ()). isEqualTo ("JAAS Login Entry name must be specified!" );
335
337
}
336
338
337
339
@ Test
@@ -340,8 +342,8 @@ public void testSSLWithoutKeystore() {
340
342
.enableSSL (true );
341
343
342
344
Throwable t = assertThrows (IllegalArgumentException .class , () -> clientConfigurer .create ());
343
- assertThat (t .getMessage (),
344
- is ( "The keystore location parameter is empty for the ZooKeeper client connection." ) );
345
+ assertThat (t .getMessage ()). isEqualTo (
346
+ "The keystore location parameter is empty for the ZooKeeper client connection." );
345
347
}
346
348
347
349
@ Test
@@ -351,8 +353,8 @@ public void testSSLWithEmptyKeystore() {
351
353
.withKeystore ("" );
352
354
353
355
Throwable t = assertThrows (IllegalArgumentException .class , () -> clientConfigurer .create ());
354
- assertThat (t .getMessage (),
355
- is ( "The keystore location parameter is empty for the ZooKeeper client connection." ) );
356
+ assertThat (t .getMessage ()). isEqualTo (
357
+ "The keystore location parameter is empty for the ZooKeeper client connection." );
356
358
}
357
359
358
360
@ Test
@@ -362,8 +364,8 @@ public void testSSLWithoutTruststore() {
362
364
.withKeystore ("keyStoreLoc" );
363
365
364
366
Throwable t = assertThrows (IllegalArgumentException .class , () -> clientConfigurer .create ());
365
- assertThat (t .getMessage (),
366
- is ( "The truststore location parameter is empty for the ZooKeeper client connection." ) );
367
+ assertThat (t .getMessage ()). isEqualTo (
368
+ "The truststore location parameter is empty for the ZooKeeper client connection." );
367
369
}
368
370
369
371
@ Test
@@ -374,8 +376,8 @@ public void testSSLWithEmptyTruststore() {
374
376
.withTruststore ("" );
375
377
376
378
Throwable t = assertThrows (IllegalArgumentException .class , () -> clientConfigurer .create ());
377
- assertThat (t .getMessage (),
378
- is ( "The truststore location parameter is empty for the ZooKeeper client connection." ) );
379
+ assertThat (t .getMessage ()). isEqualTo (
380
+ "The truststore location parameter is empty for the ZooKeeper client connection." );
379
381
}
380
382
381
383
private void testSaslAuthType (String vendor ) {
@@ -395,36 +397,39 @@ private void testSaslAuthType(String vendor) {
395
397
verify (cfBuilder ).aclProvider (aclProviderCaptor .capture ());
396
398
SASLOwnerACLProvider aclProvider = aclProviderCaptor .getValue ();
397
399
398
- assertThat (aclProvider .getDefaultAcl ().size (), is ( 1 ) );
399
- assertThat (aclProvider .getDefaultAcl ().get (0 ).getId ().getScheme (), is ("sasl" ) );
400
- assertThat (aclProvider .getDefaultAcl ().get (0 ).getId ().getId (), is ("principal" ) );
401
- assertThat (aclProvider .getDefaultAcl ().get (0 ).getPerms (), is (ZooDefs .Perms .ALL ) );
400
+ assertThat (aclProvider .getDefaultAcl ().size ()). isEqualTo ( 1 );
401
+ assertThat (aclProvider .getDefaultAcl ().get (0 ).getId ().getScheme ()). isEqualTo ("sasl" );
402
+ assertThat (aclProvider .getDefaultAcl ().get (0 ).getId ().getId ()). isEqualTo ("principal" );
403
+ assertThat (aclProvider .getDefaultAcl ().get (0 ).getPerms ()). isEqualTo (ZooDefs .Perms .ALL );
402
404
403
405
Arrays .stream (new String [] {"/" , "/foo" , "/foo/bar/baz" , "/random/path" })
404
406
.forEach (s -> {
405
- assertThat (aclProvider .getAclForPath (s ).size (), is ( 1 ) );
406
- assertThat (aclProvider .getAclForPath (s ).get (0 ).getId ().getScheme (), is ("sasl" ) );
407
- assertThat (aclProvider .getAclForPath (s ).get (0 ).getId ().getId (), is ("principal" ) );
408
- assertThat (aclProvider .getAclForPath (s ).get (0 ).getPerms (), is (ZooDefs .Perms .ALL ) );
407
+ assertThat (aclProvider .getAclForPath (s ).size ()). isEqualTo ( 1 );
408
+ assertThat (aclProvider .getAclForPath (s ).get (0 ).getId ().getScheme ()). isEqualTo ("sasl" );
409
+ assertThat (aclProvider .getAclForPath (s ).get (0 ).getId ().getId ()). isEqualTo ("principal" );
410
+ assertThat (aclProvider .getAclForPath (s ).get (0 ).getPerms ()). isEqualTo (ZooDefs .Perms .ALL );
409
411
});
410
412
411
- assertThat (System .getProperty (ZKClientConfig .LOGIN_CONTEXT_NAME_KEY ), is ("TestEntry" ) );
412
- assertThat (System .getProperty ("zookeeper.authProvider.1" ),
413
- is ( "org.apache.zookeeper.server.auth.SASLAuthenticationProvider" ) );
413
+ assertThat (System .getProperty (ZKClientConfig .LOGIN_CONTEXT_NAME_KEY )). isEqualTo ("TestEntry" );
414
+ assertThat (System .getProperty ("zookeeper.authProvider.1" )). isEqualTo (
415
+ "org.apache.zookeeper.server.auth.SASLAuthenticationProvider" );
414
416
415
417
Configuration config = Configuration .getConfiguration ();
416
- assertThat (config .getAppConfigurationEntry ("TestEntry" ).length , is ( 1 ) );
418
+ assertThat (config .getAppConfigurationEntry ("TestEntry" ).length ). isEqualTo ( 1 );
417
419
AppConfigurationEntry entry = config .getAppConfigurationEntry ("TestEntry" )[0 ];
418
- assertThat (entry .getOptions ().get ("keyTab" ), is ("keytabLoc" ));
419
- assertThat (
entry .
getOptions ().
get (
"principal" ),
is (
"[email protected] /SOME.REALM" ));
420
- assertThat (entry .getOptions ().get ("useKeyTab" ), is ("true" ));
421
- assertThat (entry .getOptions ().get ("storeKey" ), is ("true" ));
422
- assertThat (entry .getOptions ().get ("useTicketCache" ), is ("false" ));
423
- assertThat (entry .getOptions ().get ("refreshKrb5Config" ), is ("true" ));
420
+ assertThat (entry .getOptions ().get ("keyTab" )).isEqualTo ("keytabLoc" );
421
+ assertThat (
entry .
getOptions ().
get (
"principal" )).
isEqualTo (
"[email protected] /SOME.REALM" );
422
+ assertThat (entry .getOptions ().get ("useKeyTab" )).isEqualTo ("true" );
423
+ assertThat (entry .getOptions ().get ("storeKey" )).isEqualTo ("true" );
424
+ assertThat (entry .getOptions ().get ("useTicketCache" )).isEqualTo ("false" );
425
+ assertThat (entry .getOptions ().get ("refreshKrb5Config" )).isEqualTo ("true" );
426
+
424
427
if (System .getProperty ("java.vendor" ).contains ("IBM" )){
425
- assertThat (entry .getLoginModuleName (), is ("com.ibm.security.auth.module.Krb5LoginModule" ));
428
+ assertThat (entry .getLoginModuleName ()).isEqualTo (
429
+ "com.ibm.security.auth.module.Krb5LoginModule" );
426
430
} else {
427
- assertThat (entry .getLoginModuleName (), is ("com.sun.security.auth.module.Krb5LoginModule" ));
431
+ assertThat (entry .getLoginModuleName ()).isEqualTo (
432
+ "com.sun.security.auth.module.Krb5LoginModule" );
428
433
}
429
434
} finally {
430
435
Configuration .setConfiguration (origConf );
@@ -465,8 +470,8 @@ private void verifyDefaultRetryPolicy() {
465
470
verify (cfBuilder ).retryPolicy (retry .capture ());
466
471
ExponentialBackoffRetry policy = retry .getValue ();
467
472
468
- assertThat (policy .getBaseSleepTimeMs (), is (1000 ) );
469
- assertThat (policy .getN (), is ( 3 ) );
473
+ assertThat (policy .getBaseSleepTimeMs ()). isEqualTo (1000 );
474
+ assertThat (policy .getN ()). isEqualTo ( 3 );
470
475
}
471
476
472
477
private void verifyDefaultAclProvider () {
@@ -478,21 +483,16 @@ private void verifyDefaultZKClientConfig() {
478
483
verify (cfBuilder ).zkClientConfig (clientConfCaptor .capture ());
479
484
ZKClientConfig conf = clientConfCaptor .getValue ();
480
485
481
- assertThat (conf .getProperty (ZKClientConfig .SECURE_CLIENT ), isEmptyOrFalse ());
486
+ assertThat (conf .getProperty (ZKClientConfig .SECURE_CLIENT ))
487
+ .satisfiesAnyOf (value -> assertThat (value ).isNullOrEmpty (),
488
+ value -> assertThat (value ).isEqualTo ("false" ));
489
+
482
490
try (ClientX509Util sslOpts = new ClientX509Util ()) {
483
- assertThat (conf .getProperty (sslOpts .getSslKeystoreLocationProperty ()), isEmpty () );
484
- assertThat (conf .getProperty (sslOpts .getSslKeystorePasswdProperty ()), isEmpty () );
485
- assertThat (conf .getProperty (sslOpts .getSslTruststoreLocationProperty ()), isEmpty () );
486
- assertThat (conf .getProperty (sslOpts .getSslTruststorePasswdProperty ()), isEmpty () );
491
+ assertThat (conf .getProperty (sslOpts .getSslKeystoreLocationProperty ())). isNullOrEmpty ( );
492
+ assertThat (conf .getProperty (sslOpts .getSslKeystorePasswdProperty ())). isNullOrEmpty ( );
493
+ assertThat (conf .getProperty (sslOpts .getSslTruststoreLocationProperty ())). isNullOrEmpty ( );
494
+ assertThat (conf .getProperty (sslOpts .getSslTruststorePasswdProperty ())). isNullOrEmpty ( );
487
495
}
488
496
}
489
497
490
- private Matcher <String > isEmptyOrFalse () {
491
- return anyOf (isEmpty (), is ("false" ));
492
- }
493
-
494
- private Matcher <String > isEmpty () {
495
- return anyOf (new IsNull <>(), is ("" ));
496
- }
497
-
498
498
}
0 commit comments