1616
1717package com .google .cloud .spanner ;
1818
19- import static com .google .common .base .Preconditions .checkNotNull ;
2019import static com .google .common .truth .Truth .assertThat ;
20+ import static org .junit .Assert .fail ;
2121
2222import com .google .api .core .ApiFunction ;
2323import com .google .api .gax .longrunning .OperationFuture ;
5656import java .util .concurrent .CancellationException ;
5757import java .util .concurrent .ExecutionException ;
5858import java .util .concurrent .TimeUnit ;
59- import org .hamcrest .BaseMatcher ;
60- import org .hamcrest .Description ;
6159import org .junit .After ;
6260import org .junit .AfterClass ;
6361import org .junit .Before ;
6462import org .junit .BeforeClass ;
65- import org .junit .Rule ;
6663import org .junit .Test ;
67- import org .junit .rules .ExpectedException ;
6864import org .junit .runner .RunWith ;
6965import org .junit .runners .JUnit4 ;
7066import org .threeten .bp .Duration ;
7167
7268@ RunWith (JUnit4 .class )
7369public class DatabaseAdminClientTest {
74- private static class SpannerExecutionExceptionMatcher extends BaseMatcher <Throwable > {
75- private final ErrorCode expectedCode ;
76-
77- private static SpannerExecutionExceptionMatcher forCode (ErrorCode code ) {
78- return new SpannerExecutionExceptionMatcher (code );
79- }
80-
81- private SpannerExecutionExceptionMatcher (ErrorCode code ) {
82- this .expectedCode = checkNotNull (code );
83- }
84-
85- @ Override
86- public boolean matches (Object item ) {
87- if (item instanceof ExecutionException ) {
88- ExecutionException e = (ExecutionException ) item ;
89- if (e .getCause () instanceof SpannerException ) {
90- SpannerException se = (SpannerException ) e .getCause ();
91- return se .getErrorCode () == expectedCode ;
92- }
93- }
94- return false ;
95- }
96-
97- @ Override
98- public void describeTo (Description description ) {
99- description .appendText ("SpannerException[" + expectedCode + "]" );
100- }
101- }
102-
10370 private static final String PROJECT_ID = "my-project" ;
10471 private static final String INSTANCE_ID = "my-instance" ;
10572 private static final String DB_ID = "test-db" ;
@@ -117,7 +84,6 @@ public void describeTo(Description description) {
11784
11885 private Spanner spanner ;
11986 private DatabaseAdminClient client ;
120- @ Rule public ExpectedException exception = ExpectedException .none ();
12187 private OperationFuture <Database , CreateDatabaseMetadata > createDatabaseOperation ;
12288 private OperationFuture <Backup , CreateBackupMetadata > createBackupOperation ;
12389 private OperationFuture <Database , RestoreDatabaseMetadata > restoreDatabaseOperation ;
@@ -150,8 +116,8 @@ public void setUp() throws IOException {
150116 SpannerOptions .Builder builder = SpannerOptions .newBuilder ();
151117 RetrySettings longRunningInitialRetrySettings =
152118 RetrySettings .newBuilder ()
153- .setInitialRpcTimeout (Duration .ofMillis (60L ))
154- .setMaxRpcTimeout (Duration .ofMillis (600L ))
119+ .setInitialRpcTimeout (Duration .ofMillis (600L ))
120+ .setMaxRpcTimeout (Duration .ofMillis (6000L ))
155121 .setInitialRetryDelay (Duration .ofMillis (20L ))
156122 .setMaxRetryDelay (Duration .ofMillis (45L ))
157123 .setRetryDelayMultiplier (1.5 )
@@ -348,8 +314,14 @@ public void databaseBackup() throws InterruptedException, ExecutionException {
348314 public void dbAdminCreateBackupAlreadyExists () throws InterruptedException , ExecutionException {
349315 OperationFuture <Backup , CreateBackupMetadata > op =
350316 client .createBackup (INSTANCE_ID , BCK_ID , DB_ID , after7Days ());
351- exception .expect (SpannerExecutionExceptionMatcher .forCode (ErrorCode .ALREADY_EXISTS ));
352- op .get ();
317+ try {
318+ op .get ();
319+ fail ("missing expected exception" );
320+ } catch (ExecutionException e ) {
321+ assertThat (e .getCause ()).isInstanceOf (SpannerException .class );
322+ assertThat (((SpannerException ) e .getCause ()).getErrorCode ())
323+ .isEqualTo (ErrorCode .ALREADY_EXISTS );
324+ }
353325 }
354326
355327 @ Test
@@ -360,9 +332,14 @@ public void backupCreateAlreadyExists() throws InterruptedException, ExecutionEx
360332 .setDatabase (DatabaseId .of (PROJECT_ID , INSTANCE_ID , DB_ID ))
361333 .setExpireTime (after7Days ())
362334 .build ();
363- OperationFuture <Backup , CreateBackupMetadata > op = backup .create ();
364- exception .expect (SpannerExecutionExceptionMatcher .forCode (ErrorCode .ALREADY_EXISTS ));
365- op .get ();
335+ try {
336+ backup .create ().get ();
337+ fail ("missing expected exception" );
338+ } catch (ExecutionException e ) {
339+ assertThat (e .getCause ()).isInstanceOf (SpannerException .class );
340+ assertThat (((SpannerException ) e .getCause ()).getErrorCode ())
341+ .isEqualTo (ErrorCode .ALREADY_EXISTS );
342+ }
366343 }
367344
368345 @ Test
@@ -374,17 +351,28 @@ public void databaseBackupAlreadyExists() throws InterruptedException, Execution
374351 .newBackupBuilder (BackupId .of (PROJECT_ID , INSTANCE_ID , BCK_ID ))
375352 .setExpireTime (after7Days ())
376353 .build ());
377- exception .expect (SpannerExecutionExceptionMatcher .forCode (ErrorCode .ALREADY_EXISTS ));
378- op .get ();
354+ try {
355+ op .get ();
356+ fail ("missing expected exception" );
357+ } catch (ExecutionException e ) {
358+ assertThat (e .getCause ()).isInstanceOf (SpannerException .class );
359+ assertThat (((SpannerException ) e .getCause ()).getErrorCode ())
360+ .isEqualTo (ErrorCode .ALREADY_EXISTS );
361+ }
379362 }
380363
381364 @ Test
382365 public void dbAdminCreateBackupDbNotFound () throws InterruptedException , ExecutionException {
383366 final String backupId = "other-backup-id" ;
384367 OperationFuture <Backup , CreateBackupMetadata > op =
385368 client .createBackup (INSTANCE_ID , backupId , "does-not-exist" , after7Days ());
386- exception .expect (SpannerExecutionExceptionMatcher .forCode (ErrorCode .NOT_FOUND ));
387- op .get ();
369+ try {
370+ op .get ();
371+ fail ("missing expected exception" );
372+ } catch (ExecutionException e ) {
373+ assertThat (e .getCause ()).isInstanceOf (SpannerException .class );
374+ assertThat (((SpannerException ) e .getCause ()).getErrorCode ()).isEqualTo (ErrorCode .NOT_FOUND );
375+ }
388376 }
389377
390378 @ Test
@@ -396,9 +384,13 @@ public void backupCreateDbNotFound() throws InterruptedException, ExecutionExcep
396384 .setDatabase (DatabaseId .of (PROJECT_ID , INSTANCE_ID , "does-not-exist" ))
397385 .setExpireTime (after7Days ())
398386 .build ();
399- OperationFuture <Backup , CreateBackupMetadata > op = backup .create ();
400- exception .expect (SpannerExecutionExceptionMatcher .forCode (ErrorCode .NOT_FOUND ));
401- op .get ();
387+ try {
388+ backup .create ().get ();
389+ fail ("missing expected exception" );
390+ } catch (ExecutionException e ) {
391+ assertThat (e .getCause ()).isInstanceOf (SpannerException .class );
392+ assertThat (((SpannerException ) e .getCause ()).getErrorCode ()).isEqualTo (ErrorCode .NOT_FOUND );
393+ }
402394 }
403395
404396 @ Test
@@ -413,8 +405,13 @@ public void databaseBackupDbNotFound() throws InterruptedException, ExecutionExc
413405 .newBackupBuilder (BackupId .of (PROJECT_ID , INSTANCE_ID , backupId ))
414406 .setExpireTime (after7Days ())
415407 .build ());
416- exception .expect (SpannerExecutionExceptionMatcher .forCode (ErrorCode .NOT_FOUND ));
417- op .get ();
408+ try {
409+ op .get ();
410+ fail ("missing expected exception" );
411+ } catch (ExecutionException e ) {
412+ assertThat (e .getCause ()).isInstanceOf (SpannerException .class );
413+ assertThat (((SpannerException ) e .getCause ()).getErrorCode ()).isEqualTo (ErrorCode .NOT_FOUND );
414+ }
418415 }
419416
420417 @ Test
@@ -435,16 +432,24 @@ public void backupDelete() {
435432
436433 @ Test
437434 public void dbAdminDeleteBackupNotFound () {
438- exception .expect (SpannerMatchers .isSpannerException (ErrorCode .NOT_FOUND ));
439- client .deleteBackup (INSTANCE_ID , "does-not-exist" );
435+ try {
436+ client .deleteBackup (INSTANCE_ID , "does-not-exist" );
437+ fail ("missing expected exception" );
438+ } catch (SpannerException e ) {
439+ assertThat (e .getErrorCode ()).isEqualTo (ErrorCode .NOT_FOUND );
440+ }
440441 }
441442
442443 @ Test
443444 public void backupDeleteNotFound () {
444445 Backup backup =
445446 client .newBackupBuilder (BackupId .of (PROJECT_ID , INSTANCE_ID , "does-not-exist" )).build ();
446- exception .expect (SpannerMatchers .isSpannerException (ErrorCode .NOT_FOUND ));
447- backup .delete ();
447+ try {
448+ backup .delete ();
449+ fail ("missing expected exception" );
450+ } catch (SpannerException e ) {
451+ assertThat (e .getErrorCode ()).isEqualTo (ErrorCode .NOT_FOUND );
452+ }
448453 }
449454
450455 @ Test
@@ -463,16 +468,22 @@ public void backupReload() {
463468
464469 @ Test
465470 public void dbAdminGetBackupNotFound () {
466- exception .expect (SpannerMatchers .isSpannerException (ErrorCode .NOT_FOUND ));
467- client .getBackup (INSTANCE_ID , "does-not-exist" );
471+ try {
472+ client .getBackup (INSTANCE_ID , "does-not-exist" );
473+ } catch (SpannerException e ) {
474+ assertThat (e .getErrorCode ()).isEqualTo (ErrorCode .NOT_FOUND );
475+ }
468476 }
469477
470478 @ Test
471479 public void backupReloadNotFound () {
472480 Backup backup =
473481 client .newBackupBuilder (BackupId .of (PROJECT_ID , INSTANCE_ID , "does-not-exist" )).build ();
474- exception .expect (SpannerMatchers .isSpannerException (ErrorCode .NOT_FOUND ));
475- backup .reload ();
482+ try {
483+ backup .reload ();
484+ } catch (SpannerException e ) {
485+ assertThat (e .getErrorCode ()).isEqualTo (ErrorCode .NOT_FOUND );
486+ }
476487 }
477488
478489 @ Test
0 commit comments