31
31
32
32
package com .google .auth .oauth2 ;
33
33
34
- import static org .junit .Assert .assertEquals ;
35
- import static org .junit .Assert .assertFalse ;
36
- import static org .junit .Assert .assertNotNull ;
37
- import static org .junit .Assert .assertNull ;
38
- import static org .junit .Assert .assertSame ;
39
- import static org .junit .Assert . assertTrue ;
40
- import static org .junit .Assert . fail ;
34
+ import static org .junit .jupiter . api . Assertions .assertEquals ;
35
+ import static org .junit .jupiter . api . Assertions .assertFalse ;
36
+ import static org .junit .jupiter . api . Assertions .assertNotNull ;
37
+ import static org .junit .jupiter . api . Assertions .assertNull ;
38
+ import static org .junit .jupiter . api . Assertions .assertSame ;
39
+ import static org .junit .jupiter . api . Assertions . assertThrows ;
40
+ import static org .junit .jupiter . api . Assertions . assertTrue ;
41
41
42
42
import com .google .api .client .http .HttpTransport ;
43
43
import com .google .api .client .http .LowLevelHttpRequest ;
65
65
import java .util .logging .Level ;
66
66
import java .util .logging .LogRecord ;
67
67
import java .util .logging .Logger ;
68
- import org .junit .Test ;
69
- import org .junit .runner .RunWith ;
70
- import org .junit .runners .JUnit4 ;
68
+ import org .junit .jupiter .api .Test ;
71
69
72
70
/** Test case for {@link DefaultCredentialsProvider}. */
73
- @ RunWith (JUnit4 .class )
74
- public class DefaultCredentialsProviderTest {
71
+ class DefaultCredentialsProviderTest {
75
72
76
73
private static final String USER_CLIENT_SECRET = "jakuaL9YyieakhECKL2SwZcu" ;
77
74
private static final String USER_CLIENT_ID = "ya29.1.AADtN_UtlxN3PuGAxrN2XQnZTVRvDyVWnYq4I6dws" ;
@@ -101,36 +98,36 @@ public HttpTransport create() {
101
98
}
102
99
103
100
@ Test
104
- public void getDefaultCredentials_noCredentials_throws () throws Exception {
101
+ void getDefaultCredentials_noCredentials_throws () {
105
102
MockHttpTransportFactory transportFactory = new MockHttpTransportFactory ();
106
103
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider ();
107
104
108
- try {
109
- testProvider . getDefaultCredentials ( transportFactory );
110
- fail ( "No credential expected." );
111
- } catch ( IOException e ) {
112
- String message = e . getMessage ( );
113
- assertTrue ( message . contains ( DefaultCredentialsProvider . HELP_PERMALINK ) );
114
- }
105
+ IOException exception =
106
+ assertThrows (
107
+ IOException . class ,
108
+ () -> testProvider . getDefaultCredentials ( transportFactory ),
109
+ "No credential expected." );
110
+ String message = exception . getMessage ( );
111
+ assertTrue ( message . contains ( DefaultCredentialsProvider . HELP_PERMALINK ));
115
112
}
116
113
117
114
@ Test
118
- public void getDefaultCredentials_noCredentialsSandbox_throwsNonSecurity () throws Exception {
115
+ void getDefaultCredentials_noCredentialsSandbox_throwsNonSecurity () {
119
116
MockHttpTransportFactory transportFactory = new MockHttpTransportFactory ();
120
117
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider ();
121
118
testProvider .setFileSandbox (true );
122
119
123
- try {
124
- testProvider . getDefaultCredentials ( transportFactory );
125
- fail ( "No credential expected." );
126
- } catch ( IOException e ) {
127
- String message = e . getMessage ( );
128
- assertTrue ( message . contains ( DefaultCredentialsProvider . HELP_PERMALINK ) );
129
- }
120
+ IOException exception =
121
+ assertThrows (
122
+ IOException . class ,
123
+ () -> testProvider . getDefaultCredentials ( transportFactory ),
124
+ "No credential expected." );
125
+ String message = exception . getMessage ( );
126
+ assertTrue ( message . contains ( DefaultCredentialsProvider . HELP_PERMALINK ));
130
127
}
131
128
132
129
@ Test
133
- public void getDefaultCredentials_envValidSandbox_throwsNonSecurity () throws Exception {
130
+ void getDefaultCredentials_envValidSandbox_throwsNonSecurity () throws Exception {
134
131
MockHttpTransportFactory transportFactory = new MockHttpTransportFactory ();
135
132
InputStream userStream =
136
133
UserCredentialsTest .writeUserStream (
@@ -141,43 +138,39 @@ public void getDefaultCredentials_envValidSandbox_throwsNonSecurity() throws Exc
141
138
testProvider .addFile (userPath , userStream );
142
139
testProvider .setEnv (DefaultCredentialsProvider .CREDENTIAL_ENV_VAR , userPath );
143
140
144
- try {
145
- testProvider . getDefaultCredentials ( transportFactory );
146
- fail ( "No credential expected." );
147
- } catch ( IOException e ) {
148
- String message = e . getMessage ( );
149
- assertTrue ( message . contains ( DefaultCredentialsProvider . HELP_PERMALINK ) );
150
- }
141
+ IOException exception =
142
+ assertThrows (
143
+ IOException . class ,
144
+ () -> testProvider . getDefaultCredentials ( transportFactory ),
145
+ "No credential expected." );
146
+ String message = exception . getMessage ( );
147
+ assertTrue ( message . contains ( DefaultCredentialsProvider . HELP_PERMALINK ));
151
148
}
152
149
153
150
@ Test
154
- public void getDefaultCredentials_noCredentials_singleGceTestRequest () {
151
+ void getDefaultCredentials_noCredentials_singleGceTestRequest () {
155
152
MockRequestCountingTransportFactory transportFactory =
156
153
new MockRequestCountingTransportFactory ();
157
154
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider ();
158
155
159
- try {
160
- testProvider .getDefaultCredentials (transportFactory );
161
- fail ("No credential expected." );
162
- } catch (IOException expected ) {
163
- // Expected
164
- }
156
+ assertThrows (
157
+ IOException .class ,
158
+ () -> testProvider .getDefaultCredentials (transportFactory ),
159
+ "No credential expected." );
165
160
assertEquals (
166
161
transportFactory .transport .getRequestCount (),
167
162
ComputeEngineCredentials .MAX_COMPUTE_PING_TRIES );
168
- try {
169
- testProvider .getDefaultCredentials (transportFactory );
170
- fail ("No credential expected." );
171
- } catch (IOException expected ) {
172
- // Expected
173
- }
163
+ assertThrows (
164
+ IOException .class ,
165
+ () -> testProvider .getDefaultCredentials (transportFactory ),
166
+ "No credential expected." );
174
167
assertEquals (
175
168
transportFactory .transport .getRequestCount (),
176
169
ComputeEngineCredentials .MAX_COMPUTE_PING_TRIES );
177
170
}
178
171
179
172
@ Test
180
- public void getDefaultCredentials_caches () throws IOException {
173
+ void getDefaultCredentials_caches () throws IOException {
181
174
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory ();
182
175
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider ();
183
176
@@ -189,43 +182,42 @@ public void getDefaultCredentials_caches() throws IOException {
189
182
}
190
183
191
184
@ Test
192
- public void getDefaultCredentials_appEngineClassWithoutRuntime_NotFoundError () {
185
+ void getDefaultCredentials_appEngineClassWithoutRuntime_NotFoundError () {
193
186
MockHttpTransportFactory transportFactory = new MockHttpTransportFactory ();
194
187
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider ();
195
188
testProvider .addType (
196
189
DefaultCredentialsProvider .APP_ENGINE_SIGNAL_CLASS , MockOffAppEngineSystemProperty .class );
197
190
testProvider .setProperty ("isOnGAEStandard7" , "true" );
198
191
199
- try {
200
- testProvider . getDefaultCredentials ( transportFactory );
201
- fail ( "No credential expected when not on App Engine." );
202
- } catch ( IOException e ) {
203
- String message = e . getMessage ( );
204
- assertTrue ( message . contains ( DefaultCredentialsProvider . HELP_PERMALINK ) );
205
- }
192
+ IOException exception =
193
+ assertThrows (
194
+ IOException . class ,
195
+ () -> testProvider . getDefaultCredentials ( transportFactory ),
196
+ "No credential expected when not on App Engine." );
197
+ String message = exception . getMessage ( );
198
+ assertTrue ( message . contains ( DefaultCredentialsProvider . HELP_PERMALINK ));
206
199
}
207
200
208
201
@ Test
209
- public void getDefaultCredentials_appEngineRuntimeWithoutClass_throwsHelpfulLoadError () {
202
+ void getDefaultCredentials_appEngineRuntimeWithoutClass_throwsHelpfulLoadError () {
210
203
MockHttpTransportFactory transportFactory = new MockHttpTransportFactory ();
211
204
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider ();
212
205
testProvider .addType (
213
206
DefaultCredentialsProvider .APP_ENGINE_SIGNAL_CLASS , MockAppEngineSystemProperty .class );
214
207
testProvider .setProperty ("isOnGAEStandard7" , "true" );
215
208
216
- try {
217
- testProvider . getDefaultCredentials ( transportFactory );
218
- fail ( "Credential expected to fail to load if credential class not present." );
219
- } catch ( IOException e ) {
220
- String message = e . getMessage ( );
221
- assertFalse ( message . contains ( DefaultCredentialsProvider . HELP_PERMALINK ) );
222
- assertTrue (message .contains ("Check that the App Engine SDK is deployed." ));
223
- }
209
+ IOException exception =
210
+ assertThrows (
211
+ IOException . class ,
212
+ () -> testProvider . getDefaultCredentials ( transportFactory ),
213
+ "Credential expected to fail to load if credential class not present." );
214
+ String message = exception . getMessage ( );
215
+ assertFalse (message .contains (DefaultCredentialsProvider . HELP_PERMALINK ));
216
+ assertTrue ( message . contains ( "Check that the App Engine SDK is deployed." ));
224
217
}
225
218
226
219
@ Test
227
- public void getDefaultCredentials_appEngineSkipWorks_retrievesCloudShellCredential ()
228
- throws IOException {
220
+ void getDefaultCredentials_appEngineSkipWorks_retrievesCloudShellCredential () throws IOException {
229
221
MockHttpTransportFactory transportFactory = new MockHttpTransportFactory ();
230
222
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider ();
231
223
testProvider .addType (
@@ -239,7 +231,7 @@ public void getDefaultCredentials_appEngineSkipWorks_retrievesCloudShellCredenti
239
231
}
240
232
241
233
@ Test
242
- public void getDefaultCredentials_compute_providesToken () throws IOException {
234
+ void getDefaultCredentials_compute_providesToken () throws IOException {
243
235
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory ();
244
236
transportFactory .transport .setAccessToken (ACCESS_TOKEN );
245
237
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider ();
@@ -252,7 +244,7 @@ public void getDefaultCredentials_compute_providesToken() throws IOException {
252
244
}
253
245
254
246
@ Test
255
- public void getDefaultCredentials_cloudshell () throws IOException {
247
+ void getDefaultCredentials_cloudshell () throws IOException {
256
248
MockHttpTransportFactory transportFactory = new MockHttpTransportFactory ();
257
249
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider ();
258
250
testProvider .setEnv (DefaultCredentialsProvider .CLOUD_SHELL_ENV_VAR , "4" );
@@ -264,7 +256,7 @@ public void getDefaultCredentials_cloudshell() throws IOException {
264
256
}
265
257
266
258
@ Test
267
- public void getDefaultCredentials_cloudshell_withComputCredentialsPresent () throws IOException {
259
+ void getDefaultCredentials_cloudshell_withComputCredentialsPresent () throws IOException {
268
260
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory ();
269
261
transportFactory .transport .setAccessToken (ACCESS_TOKEN );
270
262
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider ();
@@ -277,24 +269,24 @@ public void getDefaultCredentials_cloudshell_withComputCredentialsPresent() thro
277
269
}
278
270
279
271
@ Test
280
- public void getDefaultCredentials_envMissingFile_throws () {
272
+ void getDefaultCredentials_envMissingFile_throws () {
281
273
final String invalidPath = "/invalid/path" ;
282
274
MockHttpTransportFactory transportFactory = new MockHttpTransportFactory ();
283
275
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider ();
284
276
testProvider .setEnv (DefaultCredentialsProvider .CREDENTIAL_ENV_VAR , invalidPath );
285
277
286
- try {
287
- testProvider . getDefaultCredentials ( transportFactory );
288
- fail ( "Non existent credential should throw exception" );
289
- } catch ( IOException e ) {
290
- String message = e . getMessage ( );
291
- assertTrue ( message . contains ( DefaultCredentialsProvider . CREDENTIAL_ENV_VAR ) );
292
- assertTrue (message .contains (invalidPath ));
293
- }
278
+ IOException exception =
279
+ assertThrows (
280
+ IOException . class ,
281
+ () -> testProvider . getDefaultCredentials ( transportFactory ),
282
+ "Non existent credential should throw exception" );
283
+ String message = exception . getMessage ( );
284
+ assertTrue (message .contains (DefaultCredentialsProvider . CREDENTIAL_ENV_VAR ));
285
+ assertTrue ( message . contains ( invalidPath ));
294
286
}
295
287
296
288
@ Test
297
- public void getDefaultCredentials_envServiceAccount_providesToken () throws IOException {
289
+ void getDefaultCredentials_envServiceAccount_providesToken () throws IOException {
298
290
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
299
291
transportFactory .transport .addServiceAccount (SA_CLIENT_EMAIL , ACCESS_TOKEN );
300
292
InputStream serviceAccountStream =
@@ -314,7 +306,7 @@ public void getDefaultCredentials_envServiceAccount_providesToken() throws IOExc
314
306
}
315
307
316
308
@ Test
317
- public void getDefaultCredentials_envUser_providesToken () throws IOException {
309
+ void getDefaultCredentials_envUser_providesToken () throws IOException {
318
310
InputStream userStream =
319
311
UserCredentialsTest .writeUserStream (
320
312
USER_CLIENT_ID , USER_CLIENT_SECRET , REFRESH_TOKEN , QUOTA_PROJECT );
@@ -327,31 +319,29 @@ public void getDefaultCredentials_envUser_providesToken() throws IOException {
327
319
}
328
320
329
321
@ Test
330
- public void getDefaultCredentials_envNoGceCheck_noGceRequest () throws IOException {
322
+ void getDefaultCredentials_envNoGceCheck_noGceRequest () {
331
323
MockRequestCountingTransportFactory transportFactory =
332
324
new MockRequestCountingTransportFactory ();
333
325
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider ();
334
326
testProvider .setEnv (DefaultCredentialsProvider .NO_GCE_CHECK_ENV_VAR , "true" );
335
327
336
- try {
337
- testProvider .getDefaultCredentials (transportFactory );
338
- fail ("No credential expected." );
339
- } catch (IOException expected ) {
340
- // Expected
341
- }
328
+ assertThrows (
329
+ IOException .class ,
330
+ () -> testProvider .getDefaultCredentials (transportFactory ),
331
+ "No credential expected." );
342
332
assertEquals (transportFactory .transport .getRequestCount (), 0 );
343
333
}
344
334
345
335
@ Test
346
- public void getDefaultCredentials_envGceMetadataHost_setsMetadataServerUrl () {
336
+ void getDefaultCredentials_envGceMetadataHost_setsMetadataServerUrl () {
347
337
String testUrl = "192.0.2.0" ;
348
338
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider ();
349
339
testProvider .setEnv (DefaultCredentialsProvider .GCE_METADATA_HOST_ENV_VAR , testUrl );
350
340
assertEquals (ComputeEngineCredentials .getMetadataServerUrl (testProvider ), "http://" + testUrl );
351
341
}
352
342
353
343
@ Test
354
- public void getDefaultCredentials_envGceMetadataHost_setsTokenServerUrl () {
344
+ void getDefaultCredentials_envGceMetadataHost_setsTokenServerUrl () {
355
345
String testUrl = "192.0.2.0" ;
356
346
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider ();
357
347
testProvider .setEnv (DefaultCredentialsProvider .GCE_METADATA_HOST_ENV_VAR , testUrl );
@@ -361,7 +351,7 @@ public void getDefaultCredentials_envGceMetadataHost_setsTokenServerUrl() {
361
351
}
362
352
363
353
@ Test
364
- public void getDefaultCredentials_wellKnownFileEnv_providesToken () throws IOException {
354
+ void getDefaultCredentials_wellKnownFileEnv_providesToken () throws IOException {
365
355
File cloudConfigDir = getTempDirectory ();
366
356
InputStream userStream =
367
357
UserCredentialsTest .writeUserStream (
@@ -376,7 +366,7 @@ public void getDefaultCredentials_wellKnownFileEnv_providesToken() throws IOExce
376
366
}
377
367
378
368
@ Test
379
- public void getDefaultCredentials_wellKnownFileNonWindows_providesToken () throws IOException {
369
+ void getDefaultCredentials_wellKnownFileNonWindows_providesToken () throws IOException {
380
370
File homeDir = getTempDirectory ();
381
371
File configDir = new File (homeDir , ".config" );
382
372
File cloudConfigDir = new File (configDir , DefaultCredentialsProvider .CLOUDSDK_CONFIG_DIRECTORY );
@@ -394,7 +384,7 @@ public void getDefaultCredentials_wellKnownFileNonWindows_providesToken() throws
394
384
}
395
385
396
386
@ Test
397
- public void getDefaultCredentials_wellKnownFileWindows_providesToken () throws IOException {
387
+ void getDefaultCredentials_wellKnownFileWindows_providesToken () throws IOException {
398
388
File homeDir = getTempDirectory ();
399
389
File cloudConfigDir = new File (homeDir , DefaultCredentialsProvider .CLOUDSDK_CONFIG_DIRECTORY );
400
390
InputStream userStream =
@@ -411,7 +401,7 @@ public void getDefaultCredentials_wellKnownFileWindows_providesToken() throws IO
411
401
}
412
402
413
403
@ Test
414
- public void getDefaultCredentials_envAndWellKnownFile_envPrecedence () throws IOException {
404
+ void getDefaultCredentials_envAndWellKnownFile_envPrecedence () throws IOException {
415
405
final String refreshTokenEnv = "2/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY" ;
416
406
final String accessTokenEnv = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
417
407
final String refreshTokenWkf = "3/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY" ;
@@ -466,15 +456,15 @@ public void flush() {}
466
456
}
467
457
468
458
@ Test
469
- public void getDefaultCredentials_wellKnownFile_logsGcloudWarning () throws IOException {
459
+ void getDefaultCredentials_wellKnownFile_logsGcloudWarning () throws IOException {
470
460
LogRecord message = getCredentialsAndReturnLogMessage (false );
471
461
assertNotNull (message );
472
462
assertEquals (Level .WARNING , message .getLevel ());
473
463
assertTrue (message .getMessage ().contains ("end user credentials from Google Cloud SDK" ));
474
464
}
475
465
476
466
@ Test
477
- public void getDefaultCredentials_wellKnownFile_suppressGcloudWarning () throws IOException {
467
+ void getDefaultCredentials_wellKnownFile_suppressGcloudWarning () throws IOException {
478
468
LogRecord message = getCredentialsAndReturnLogMessage (true );
479
469
assertNull (message );
480
470
}
0 commit comments