@@ -3249,6 +3249,57 @@ public void testExecuteSelectDefaultConnectionSettings() throws SQLException {
3249
3249
assertEquals (42 , bigQueryResult .getTotalRows ());
3250
3250
}
3251
3251
3252
+ @ Test
3253
+ public void testExecuteSelectWithCredentials () throws SQLException {
3254
+ // This test validate that executeSelect uses the same credential provided by the BigQuery
3255
+ // object used to create the Connection client.
3256
+ // This is done the following scenarios:
3257
+ // 1. Validate that setting a valid credential executes the query.
3258
+ // 2. Validate that setting an invalid credential causes failure.
3259
+
3260
+ // Scenario 1.
3261
+ // Create a new bigQuery object but explicitly set the credentials.
3262
+ RemoteBigQueryHelper bigqueryHelper = RemoteBigQueryHelper .create ();
3263
+ BigQueryOptions bigQueryOptions =
3264
+ bigqueryHelper
3265
+ .getOptions ()
3266
+ .toBuilder ()
3267
+ .setCredentials (bigquery .getOptions ().getCredentials ())
3268
+ .build ();
3269
+ BigQuery bigQueryGoodCredentials = bigQueryOptions .getService ();
3270
+
3271
+ ConnectionSettings connectionSettings =
3272
+ ConnectionSettings .newBuilder ()
3273
+ .setJobTimeoutMs (10L ) // Force non-fast query to use BigQueryReadClient.
3274
+ .setDefaultDataset (DatasetId .of (DATASET ))
3275
+ .build ();
3276
+ Connection connectionGoodCredentials =
3277
+ bigQueryGoodCredentials .createConnection (connectionSettings );
3278
+ String query =
3279
+ "SELECT * FROM "
3280
+ + TABLE_ID_LARGE .getTable (); // Large query result is needed to use BigQueryReadClient.
3281
+ BigQueryResult bigQueryResult = connectionGoodCredentials .executeSelect (query );
3282
+ assertEquals (313348 , bigQueryResult .getTotalRows ());
3283
+
3284
+ // Scenario 2.
3285
+ // Create a new bigQuery object but explicitly an invalid credential.
3286
+ BigQueryOptions bigQueryOptionsBadCredentials =
3287
+ bigqueryHelper
3288
+ .getOptions ()
3289
+ .toBuilder ()
3290
+ .setCredentials (loadCredentials (FAKE_JSON_CRED_WITH_GOOGLE_DOMAIN ))
3291
+ .build ();
3292
+ BigQuery bigQueryBadCredentials = bigQueryOptionsBadCredentials .getService ();
3293
+ Connection connectionBadCredentials =
3294
+ bigQueryBadCredentials .createConnection (connectionSettings );
3295
+ try {
3296
+ connectionBadCredentials .executeSelect (query );
3297
+ fail (); // this line should not be reached
3298
+ } catch (BigQuerySQLException e ) {
3299
+ assertNotNull (e );
3300
+ }
3301
+ }
3302
+
3252
3303
/* TODO(prasmish): replicate the entire test case for executeSelect */
3253
3304
@ Test
3254
3305
public void testQueryTimeStamp () throws InterruptedException {
0 commit comments