Skip to content

Commit 1c5e7bb

Browse files
committed
test: refactor to use assertThrows in GdchCredentialsTest and remove host OS check in DefaultCredentialsProviderTest
1 parent dcd97cb commit 1c5e7bb

2 files changed

Lines changed: 93 additions & 107 deletions

File tree

oauth2_http/javatests/com/google/auth/oauth2/DefaultCredentialsProviderTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,6 @@ void getDefaultCredentials_static_unsupportedPlatform_notGce() {
201201
}
202202

203203
private void checkStaticGceDetection(String osName, String productContent, boolean expected) {
204-
assumeTrue(
205-
System.getProperty("os.name").toLowerCase().startsWith("linux"),
206-
"This test only runs on Linux.");
207204
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
208205
testProvider.setProperty("os.name", osName);
209206
String productFilePath = SMBIOS_PATH_LINUX;

oauth2_http/javatests/com/google/auth/oauth2/GdchCredentialsTest.java

Lines changed: 93 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,17 @@ void fromJSON_nullFormatVersion() throws IOException {
164164
CA_CERT_PATH,
165165
TOKEN_SERVER_URI);
166166

167-
try {
168-
GdchCredentials.fromJson(json);
169-
fail("Should not be able to create GDCH credential without exception.");
170-
} catch (IOException ex) {
171-
assertTrue(
172-
ex.getMessage()
173-
.contains(
174-
String.format(
175-
"Error reading GDCH service account credential from JSON, "
176-
+ "%s is misconfigured.",
177-
"format_version")));
178-
}
167+
IOException ex =
168+
assertThrows(
169+
IOException.class,
170+
() -> GdchCredentials.fromJson(json));
171+
assertTrue(
172+
ex.getMessage()
173+
.contains(
174+
String.format(
175+
"Error reading GDCH service account credential from JSON, "
176+
+ "%s is misconfigured.",
177+
"format_version")));
179178
}
180179

181180
@Test
@@ -190,18 +189,17 @@ void fromJSON_nullProjectId() throws IOException {
190189
CA_CERT_PATH,
191190
TOKEN_SERVER_URI);
192191

193-
try {
194-
GdchCredentials.fromJson(json);
195-
fail("Should not be able to create GDCH credential without exception.");
196-
} catch (IOException ex) {
197-
assertTrue(
198-
ex.getMessage()
199-
.contains(
200-
String.format(
201-
"Error reading GDCH service account credential from JSON, "
202-
+ "%s is misconfigured.",
203-
"project")));
204-
}
192+
IOException ex =
193+
assertThrows(
194+
IOException.class,
195+
() -> GdchCredentials.fromJson(json));
196+
assertTrue(
197+
ex.getMessage()
198+
.contains(
199+
String.format(
200+
"Error reading GDCH service account credential from JSON, "
201+
+ "%s is misconfigured.",
202+
"project")));
205203
}
206204

207205
@Test
@@ -216,18 +214,17 @@ void fromJSON_nullPrivateKeyId() throws IOException {
216214
CA_CERT_PATH,
217215
TOKEN_SERVER_URI);
218216

219-
try {
220-
GdchCredentials.fromJson(json);
221-
fail("Should not be able to create GDCH credential without exception.");
222-
} catch (IOException ex) {
223-
assertTrue(
224-
ex.getMessage()
225-
.contains(
226-
String.format(
227-
"Error reading GDCH service account credential from JSON, "
228-
+ "%s is misconfigured.",
229-
"private_key_id")));
230-
}
217+
IOException ex =
218+
assertThrows(
219+
IOException.class,
220+
() -> GdchCredentials.fromJson(json));
221+
assertTrue(
222+
ex.getMessage()
223+
.contains(
224+
String.format(
225+
"Error reading GDCH service account credential from JSON, "
226+
+ "%s is misconfigured.",
227+
"private_key_id")));
231228
}
232229

233230
@Test
@@ -242,18 +239,17 @@ void fromJSON_nullPrivateKey() throws IOException {
242239
CA_CERT_PATH,
243240
TOKEN_SERVER_URI);
244241

245-
try {
246-
GdchCredentials.fromJson(json);
247-
fail("Should not be able to create GDCH credential without exception.");
248-
} catch (IOException ex) {
249-
assertTrue(
250-
ex.getMessage()
251-
.contains(
252-
String.format(
253-
"Error reading GDCH service account credential from JSON, "
254-
+ "%s is misconfigured.",
255-
"private_key")));
256-
}
242+
IOException ex =
243+
assertThrows(
244+
IOException.class,
245+
() -> GdchCredentials.fromJson(json));
246+
assertTrue(
247+
ex.getMessage()
248+
.contains(
249+
String.format(
250+
"Error reading GDCH service account credential from JSON, "
251+
+ "%s is misconfigured.",
252+
"private_key")));
257253
}
258254

259255
@Test
@@ -268,18 +264,17 @@ void fromJSON_nullServiceIdentityName() throws IOException {
268264
CA_CERT_PATH,
269265
TOKEN_SERVER_URI);
270266

271-
try {
272-
GdchCredentials.fromJson(json);
273-
fail("Should not be able to create GDCH credential without exception.");
274-
} catch (IOException ex) {
275-
assertTrue(
276-
ex.getMessage()
277-
.contains(
278-
String.format(
279-
"Error reading GDCH service account credential from JSON, "
280-
+ "%s is misconfigured.",
281-
"name")));
282-
}
267+
IOException ex =
268+
assertThrows(
269+
IOException.class,
270+
() -> GdchCredentials.fromJson(json));
271+
assertTrue(
272+
ex.getMessage()
273+
.contains(
274+
String.format(
275+
"Error reading GDCH service account credential from JSON, "
276+
+ "%s is misconfigured.",
277+
"name")));
283278
}
284279

285280
@Test
@@ -309,18 +304,17 @@ void fromJSON_nullTokenServerUri() throws IOException {
309304
CA_CERT_PATH,
310305
null);
311306

312-
try {
313-
GdchCredentials.fromJson(json);
314-
fail("Should not be able to create GDCH credential without exception.");
315-
} catch (IOException ex) {
316-
assertTrue(
317-
ex.getMessage()
318-
.contains(
319-
String.format(
320-
"Error reading GDCH service account credential from JSON, "
321-
+ "%s is misconfigured.",
322-
"token_uri")));
323-
}
307+
IOException ex =
308+
assertThrows(
309+
IOException.class,
310+
() -> GdchCredentials.fromJson(json));
311+
assertTrue(
312+
ex.getMessage()
313+
.contains(
314+
String.format(
315+
"Error reading GDCH service account credential from JSON, "
316+
+ "%s is misconfigured.",
317+
"token_uri")));
324318
}
325319

326320
@Test
@@ -335,14 +329,13 @@ void fromJSON_invalidFormatVersion() throws IOException {
335329
CA_CERT_PATH,
336330
TOKEN_SERVER_URI);
337331

338-
try {
339-
GdchCredentials.fromJson(json);
340-
fail("Should not be able to create GDCH credential without exception.");
341-
} catch (IOException ex) {
342-
assertTrue(
343-
ex.getMessage()
344-
.contains(String.format("Only format version %s is supported", FORMAT_VERSION)));
345-
}
332+
IOException ex =
333+
assertThrows(
334+
IOException.class,
335+
() -> GdchCredentials.fromJson(json));
336+
assertTrue(
337+
ex.getMessage()
338+
.contains(String.format("Only format version %s is supported", FORMAT_VERSION)));
346339
}
347340

348341
@Test
@@ -357,12 +350,11 @@ void fromJSON_invalidCaCertPath() throws IOException {
357350
"/path/to/missing/file",
358351
TOKEN_SERVER_URI);
359352

360-
try {
361-
GdchCredentials.fromJson(json);
362-
fail("Should not be able to create GDCH credential without exception.");
363-
} catch (IOException ex) {
364-
assertTrue(ex.getMessage().contains("Error reading certificate file from CA cert path"));
365-
}
353+
IOException ex =
354+
assertThrows(
355+
IOException.class,
356+
() -> GdchCredentials.fromJson(json));
357+
assertTrue(ex.getMessage().contains("Error reading certificate file from CA cert path"));
366358
}
367359

368360
@Test
@@ -449,12 +441,11 @@ void fromStream_invalidType() throws IOException {
449441
json.put("type", "invalid_type");
450442
InputStream stream = TestUtils.jsonToInputStream(json);
451443

452-
try {
453-
GdchCredentials.fromStream(stream);
454-
fail("Should not be able to create GDCH credential with invalid type.");
455-
} catch (IOException ex) {
456-
assertTrue(ex.getMessage().contains("not recognized"));
457-
}
444+
IOException ex =
445+
assertThrows(
446+
IOException.class,
447+
() -> GdchCredentials.fromStream(stream));
448+
assertTrue(ex.getMessage().contains("not recognized"));
458449
}
459450

460451
@Test
@@ -530,12 +521,11 @@ void createWithGdchAudience_nullApiAudience() throws IOException {
530521
TOKEN_SERVER_URI);
531522
GdchCredentials credentials = GdchCredentials.fromJson(json);
532523

533-
try {
534-
credentials.createWithGdchAudience((String) null);
535-
fail("Should not be able to create GDCH credential without exception.");
536-
} catch (IllegalArgumentException ex) {
537-
assertTrue(ex.getMessage().contains("Audience are not configured for GDCH service account"));
538-
}
524+
IllegalArgumentException ex =
525+
assertThrows(
526+
IllegalArgumentException.class,
527+
() -> credentials.createWithGdchAudience((String) null));
528+
assertTrue(ex.getMessage().contains("Audience are not configured for GDCH service account"));
539529
}
540530

541531
@Test
@@ -551,12 +541,11 @@ void createWithGdchAudience_emptyApiAudience() throws IOException {
551541
TOKEN_SERVER_URI);
552542
GdchCredentials credentials = GdchCredentials.fromJson(json);
553543

554-
try {
555-
credentials.createWithGdchAudience("");
556-
fail("Should not be able to create GDCH credential without exception.");
557-
} catch (IllegalArgumentException ex) {
558-
assertTrue(ex.getMessage().contains("Audience are not configured for GDCH service account"));
559-
}
544+
IllegalArgumentException ex =
545+
assertThrows(
546+
IllegalArgumentException.class,
547+
() -> credentials.createWithGdchAudience(""));
548+
assertTrue(ex.getMessage().contains("Audience are not configured for GDCH service account"));
560549
}
561550

562551
@Test

0 commit comments

Comments
 (0)