4
4
package net .snowflake .client .jdbc ;
5
5
6
6
import static net .snowflake .client .jdbc .SnowflakeUtil .systemGetProperty ;
7
- import static org .hamcrest .CoreMatchers .instanceOf ;
8
7
import static org .junit .jupiter .api .Assertions .assertEquals ;
9
8
import static org .junit .jupiter .api .Assertions .assertFalse ;
9
+ import static org .junit .jupiter .api .Assertions .assertInstanceOf ;
10
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
10
11
import static org .junit .jupiter .api .Assertions .assertTrue ;
11
12
import static org .junit .jupiter .api .Assertions .fail ;
12
13
@@ -174,13 +175,11 @@ public void testGetObjectMetadataFileNotFoundWithGCS() throws Exception {
174
175
int idx = location .indexOf ('/' );
175
176
String remoteStageLocation = location .substring (0 , idx );
176
177
String path = location .substring (idx + 1 ) + "wrong_file.csv.gz" ;
177
- client .getObjectMetadata (remoteStageLocation , path );
178
- fail ("should raise exception" );
179
- } catch (Exception ex ) {
180
- assertTrue (
181
- ex instanceof StorageProviderException ,
182
- "Wrong type of exception. Message: " + ex .getMessage ());
183
- assertTrue (ex .getMessage ().matches (".*Blob.*not found in bucket.*" ));
178
+ StorageProviderException thrown =
179
+ assertThrows (
180
+ StorageProviderException .class ,
181
+ () -> client .getObjectMetadata (remoteStageLocation , path ));
182
+ assertTrue (thrown .getMessage ().matches (".*Blob.*not found in bucket.*" ));
184
183
} finally {
185
184
statement .execute ("DROP STAGE if exists " + OBJ_META_STAGE );
186
185
}
@@ -212,13 +211,11 @@ public void testGetObjectMetadataStorageExceptionWithGCS() throws Exception {
212
211
String location = info .getLocation ();
213
212
int idx = location .indexOf ('/' );
214
213
String remoteStageLocation = location .substring (0 , idx );
215
- client .getObjectMetadata (remoteStageLocation , "" );
216
- fail ("should raise exception" );
217
- } catch (Exception ex ) {
218
- assertTrue (
219
- ex instanceof StorageProviderException ,
220
- "Wrong type of exception. Message: " + ex .getMessage ());
221
- assertTrue (ex .getMessage ().matches (".*Permission.*denied.*" ));
214
+ StorageProviderException thrown =
215
+ assertThrows (
216
+ StorageProviderException .class ,
217
+ () -> client .getObjectMetadata (remoteStageLocation , "" ));
218
+ assertTrue (thrown .getMessage ().matches (".*Permission.*denied.*" ));
222
219
} finally {
223
220
statement .execute ("DROP STAGE if exists " + OBJ_META_STAGE );
224
221
}
@@ -248,12 +245,13 @@ public void testNullCommand() throws SQLException {
248
245
try {
249
246
statement .execute ("create or replace stage testStage" );
250
247
SFSession sfSession = con .unwrap (SnowflakeConnectionV1 .class ).getSfSession ();
251
- SnowflakeFileTransferAgent sfAgent =
252
- new SnowflakeFileTransferAgent ( null , sfSession , new SFStatement ( sfSession ));
253
- } catch ( SnowflakeSQLException err ) {
254
- assertEquals (( long ) ErrorCode . INTERNAL_ERROR . getMessageCode (), err . getErrorCode ( ));
248
+ SnowflakeSQLException thrown =
249
+ assertThrows (
250
+ SnowflakeSQLException . class ,
251
+ () -> new SnowflakeFileTransferAgent ( null , sfSession , new SFStatement ( sfSession ) ));
255
252
assertTrue (
256
- err .getMessage ()
253
+ thrown
254
+ .getMessage ()
257
255
.contains ("JDBC driver internal error: Missing sql for statement execution" ));
258
256
} finally {
259
257
statement .execute ("drop stage if exists testStage" );
@@ -281,20 +279,24 @@ public void testCompressStreamWithGzipException() throws Exception {
281
279
282
280
String srcPath = getFullPathFileInResource (TEST_DATA_FILE );
283
281
InputStream inputStream = new FileInputStream (srcPath );
284
- SnowflakeFileTransferAgent .uploadWithoutConnection (
285
- SnowflakeFileTransferConfig .Builder .newInstance ()
286
- .setSnowflakeFileTransferMetadata (metadata )
287
- .setUploadStream (inputStream )
288
- .setRequireCompress (true )
289
- .setNetworkTimeoutInMilli (0 )
290
- .setOcspMode (OCSPMode .FAIL_OPEN )
291
- .setSFSession (sfSession )
292
- .setCommand (PUT_COMMAND )
293
- .build ());
294
- } catch (SnowflakeSQLException err ) {
295
- assertEquals ((long ) ErrorCode .INTERNAL_ERROR .getMessageCode (), err .getErrorCode ());
282
+ SnowflakeSQLException thrown =
283
+ assertThrows (
284
+ SnowflakeSQLException .class ,
285
+ () ->
286
+ SnowflakeFileTransferAgent .uploadWithoutConnection (
287
+ SnowflakeFileTransferConfig .Builder .newInstance ()
288
+ .setSnowflakeFileTransferMetadata (metadata )
289
+ .setUploadStream (inputStream )
290
+ .setRequireCompress (true )
291
+ .setNetworkTimeoutInMilli (0 )
292
+ .setOcspMode (OCSPMode .FAIL_OPEN )
293
+ .setSFSession (sfSession )
294
+ .setCommand (PUT_COMMAND )
295
+ .build ()));
296
+ assertEquals ((long ) ErrorCode .INTERNAL_ERROR .getMessageCode (), thrown .getErrorCode ());
296
297
assertTrue (
297
- err .getMessage ()
298
+ thrown
299
+ .getMessage ()
298
300
.contains ("JDBC driver internal error: error encountered for compression" ));
299
301
} finally {
300
302
statement .execute ("DROP STAGE if exists testStage" );
@@ -325,20 +327,24 @@ public void testCompressStreamWithGzipNoDigestException() throws Exception {
325
327
String srcPath = getFullPathFileInResource (TEST_DATA_FILE );
326
328
327
329
InputStream inputStream = new FileInputStream (srcPath );
328
- SnowflakeFileTransferAgent .uploadWithoutConnection (
329
- SnowflakeFileTransferConfig .Builder .newInstance ()
330
- .setSnowflakeFileTransferMetadata (metadata )
331
- .setUploadStream (inputStream )
332
- .setRequireCompress (true )
333
- .setNetworkTimeoutInMilli (0 )
334
- .setOcspMode (OCSPMode .FAIL_OPEN )
335
- .setSFSession (sfSession )
336
- .setCommand (PUT_COMMAND )
337
- .build ());
338
- } catch (SnowflakeSQLException err ) {
339
- assertEquals ((long ) ErrorCode .INTERNAL_ERROR .getMessageCode (), err .getErrorCode ());
330
+ SnowflakeSQLException thrown =
331
+ assertThrows (
332
+ SnowflakeSQLException .class ,
333
+ () ->
334
+ SnowflakeFileTransferAgent .uploadWithoutConnection (
335
+ SnowflakeFileTransferConfig .Builder .newInstance ()
336
+ .setSnowflakeFileTransferMetadata (metadata )
337
+ .setUploadStream (inputStream )
338
+ .setRequireCompress (true )
339
+ .setNetworkTimeoutInMilli (0 )
340
+ .setOcspMode (OCSPMode .FAIL_OPEN )
341
+ .setSFSession (sfSession )
342
+ .setCommand (PUT_COMMAND )
343
+ .build ()));
344
+ assertEquals ((long ) ErrorCode .INTERNAL_ERROR .getMessageCode (), thrown .getErrorCode ());
340
345
assertTrue (
341
- err .getMessage ()
346
+ thrown
347
+ .getMessage ()
342
348
.contains ("JDBC driver internal error: error encountered for compression" ));
343
349
} finally {
344
350
statement .execute ("DROP STAGE if exists testStage" );
@@ -369,19 +375,23 @@ public void testUploadWithoutConnectionException() throws Exception {
369
375
String srcPath = getFullPathFileInResource (TEST_DATA_FILE );
370
376
371
377
InputStream inputStream = new FileInputStream (srcPath );
372
- SnowflakeFileTransferAgent .uploadWithoutConnection (
373
- SnowflakeFileTransferConfig .Builder .newInstance ()
374
- .setSnowflakeFileTransferMetadata (metadata )
375
- .setUploadStream (inputStream )
376
- .setRequireCompress (true )
377
- .setNetworkTimeoutInMilli (0 )
378
- .setOcspMode (OCSPMode .FAIL_OPEN )
379
- .setSFSession (sfSession )
380
- .setCommand (PUT_COMMAND )
381
- .build ());
382
- } catch (Exception err ) {
378
+ Exception thrown =
379
+ assertThrows (
380
+ Exception .class ,
381
+ () ->
382
+ SnowflakeFileTransferAgent .uploadWithoutConnection (
383
+ SnowflakeFileTransferConfig .Builder .newInstance ()
384
+ .setSnowflakeFileTransferMetadata (metadata )
385
+ .setUploadStream (inputStream )
386
+ .setRequireCompress (true )
387
+ .setNetworkTimeoutInMilli (0 )
388
+ .setOcspMode (OCSPMode .FAIL_OPEN )
389
+ .setSFSession (sfSession )
390
+ .setCommand (PUT_COMMAND )
391
+ .build ()));
383
392
assertTrue (
384
- err .getMessage ()
393
+ thrown
394
+ .getMessage ()
385
395
.contains (
386
396
"Exception encountered during file upload: failed to push to remote store" ));
387
397
} finally {
@@ -401,9 +411,8 @@ public void testInitFileMetadataFileNotFound() throws Exception {
401
411
SnowflakeFileTransferAgent sfAgent =
402
412
new SnowflakeFileTransferAgent (PUT_COMMAND , sfSession , new SFStatement (sfSession ));
403
413
404
- sfAgent .execute ();
405
- } catch (SnowflakeSQLException err ) {
406
- assertEquals (200008 , err .getErrorCode ());
414
+ SnowflakeSQLException thrown = assertThrows (SnowflakeSQLException .class , sfAgent ::execute );
415
+ assertEquals (ErrorCode .FILE_NOT_FOUND .getMessageCode (), thrown .getErrorCode ());
407
416
} finally {
408
417
statement .execute ("DROP STAGE if exists testStage" );
409
418
}
@@ -422,9 +431,8 @@ public void testInitFileMetadataFileIsDirectory() throws Exception {
422
431
String command = "put file://" + srcPath + " @testStage" ;
423
432
SnowflakeFileTransferAgent sfAgent =
424
433
new SnowflakeFileTransferAgent (command , sfSession , new SFStatement (sfSession ));
425
- sfAgent .execute ();
426
- } catch (SnowflakeSQLException err ) {
427
- assertEquals (200009 , err .getErrorCode ());
434
+ SnowflakeSQLException thrown = assertThrows (SnowflakeSQLException .class , sfAgent ::execute );
435
+ assertEquals (ErrorCode .FILE_IS_DIRECTORY .getMessageCode (), thrown .getErrorCode ());
428
436
} finally {
429
437
statement .execute ("DROP STAGE if exists testStage" );
430
438
}
@@ -445,10 +453,10 @@ public void testCompareAndSkipFilesException() throws Exception {
445
453
SnowflakeFileTransferAgent sfAgent =
446
454
new SnowflakeFileTransferAgent (command , sfSession , new SFStatement (sfSession ));
447
455
448
- sfAgent . execute ( );
449
- } catch ( SnowflakeSQLException err ) {
450
- assertEquals (( long ) ErrorCode .INTERNAL_ERROR .getMessageCode (), err .getErrorCode ());
451
- assertTrue ( err . getMessage ().contains ( "Error reading:" ));
456
+ SnowflakeSQLException thrown = assertThrows ( SnowflakeSQLException . class , sfAgent :: execute );
457
+ assertEquals (
458
+ ( long ) ErrorCode .FILE_OPERATION_UPLOAD_ERROR .getMessageCode (), thrown .getErrorCode ());
459
+ assertInstanceOf ( NoSuchAlgorithmException . class , thrown . getCause ().getCause ( ));
452
460
} finally {
453
461
statement .execute ("DROP STAGE if exists testStage" );
454
462
}
@@ -466,12 +474,14 @@ public void testParseCommandException() throws SQLException {
466
474
try {
467
475
statement .execute ("create or replace stage testStage" );
468
476
SFSession sfSession = con .unwrap (SnowflakeConnectionV1 .class ).getSfSession ();
469
- SnowflakeFileTransferAgent sfAgent =
470
- new SnowflakeFileTransferAgent (PUT_COMMAND , sfSession , new SFStatement (sfSession ));
471
-
472
- } catch (SnowflakeSQLException err ) {
473
- assertEquals ((long ) ErrorCode .INTERNAL_ERROR .getMessageCode (), err .getErrorCode ());
474
- assertTrue (err .getMessage ().contains ("Failed to parse the locations" ));
477
+ SnowflakeSQLException thrown =
478
+ assertThrows (
479
+ SnowflakeSQLException .class ,
480
+ () ->
481
+ new SnowflakeFileTransferAgent (
482
+ PUT_COMMAND , sfSession , new SFStatement (sfSession )));
483
+ assertEquals ((long ) ErrorCode .INTERNAL_ERROR .getMessageCode (), thrown .getErrorCode ());
484
+ assertTrue (thrown .getMessage ().contains ("Failed to parse the locations" ));
475
485
} finally {
476
486
statement .execute ("DROP STAGE if exists testStage" );
477
487
}
@@ -530,10 +540,9 @@ public void testListObjectsStorageException() throws Exception {
530
540
SnowflakeFileTransferAgent sfAgent =
531
541
new SnowflakeFileTransferAgent (command , sfSession , new SFStatement (sfSession ));
532
542
533
- sfAgent .execute ();
534
- } catch (SnowflakeSQLException err ) {
535
- assertEquals (200016 , err .getErrorCode ());
536
- assertTrue (err .getMessage ().contains ("Encountered exception during listObjects" ));
543
+ SnowflakeSQLException thrown = assertThrows (SnowflakeSQLException .class , sfAgent ::execute );
544
+ assertEquals (ErrorCode .IO_ERROR .getMessageCode (), thrown .getErrorCode ());
545
+ assertTrue (thrown .getMessage ().contains ("Encountered exception during listObjects" ));
537
546
} finally {
538
547
statement .execute ("DROP STAGE if exists testStage" );
539
548
}
@@ -555,13 +564,19 @@ public void testUploadStreamInterruptedException() throws IOException, SQLExcept
555
564
outputStream .flush ();
556
565
557
566
// upload the data to user stage under testUploadStream with name hello.txt
558
- connection
559
- .unwrap (SnowflakeConnection .class )
560
- .uploadStream (
561
- "~" , DEST_PREFIX , outputStream .asByteSource ().openStream (), "hello.txt" , false );
562
-
563
- } catch (SnowflakeSQLLoggedException err ) {
564
- assertEquals (200003 , err .getErrorCode ());
567
+ SnowflakeSQLException thrown =
568
+ assertThrows (
569
+ SnowflakeSQLException .class ,
570
+ () ->
571
+ connection
572
+ .unwrap (SnowflakeConnection .class )
573
+ .uploadStream (
574
+ "~" ,
575
+ DEST_PREFIX ,
576
+ outputStream .asByteSource ().openStream (),
577
+ "hello.txt" ,
578
+ false ));
579
+ assertEquals (ErrorCode .INTERRUPTED .getMessageCode (), thrown .getErrorCode ());
565
580
} finally {
566
581
statement .execute ("rm @~/" + DEST_PREFIX );
567
582
}
@@ -632,9 +647,8 @@ public void testUploadFileCallableFileNotFound() throws Exception {
632
647
String command = "PUT file://" + getFullPathFileInResource (TEST_DATA_FILE ) + " @testStage" ;
633
648
SnowflakeFileTransferAgent sfAgent =
634
649
new SnowflakeFileTransferAgent (command , sfSession , new SFStatement (sfSession ));
635
- sfAgent .execute ();
636
- } catch (Exception err ) {
637
- assertEquals (err .getCause (), instanceOf (FileNotFoundException .class ));
650
+ Exception thrown = assertThrows (Exception .class , sfAgent ::execute );
651
+ assertInstanceOf (FileNotFoundException .class , thrown .getCause ());
638
652
} finally {
639
653
statement .execute ("DROP STAGE if exists testStage" );
640
654
}
0 commit comments