Skip to content

Commit 5ccda63

Browse files
committed
AssertNull and AssertThrows changes. Documentation changes.
1 parent f502c38 commit 5ccda63

11 files changed

+44
-55
lines changed

oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ public String getAccount() {
707707
}
708708

709709
@Override
710-
Boolean supportsTrustBoundary() {
710+
boolean supportsTrustBoundary() {
711711
return true;
712712
}
713713

oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ TrustBoundary getTrustBoundary() {
344344
*
345345
* @return {@code true} if the credentials support trust boundary, {@code false} otherwise.
346346
*/
347-
Boolean supportsTrustBoundary() {
347+
boolean supportsTrustBoundary() {
348348
return false;
349349
}
350350

@@ -433,7 +433,7 @@ protected boolean isExplicitUniverseDomain() {
433433
* @return true if universe domain equals to {@link Credentials#GOOGLE_DEFAULT_UNIVERSE}, false
434434
* otherwise
435435
*/
436-
public boolean isDefaultUniverseDomain() throws IOException {
436+
boolean isDefaultUniverseDomain() throws IOException {
437437
return getUniverseDomain().equals(Credentials.GOOGLE_DEFAULT_UNIVERSE);
438438
}
439439

oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public GoogleCredentials getSourceCredentials() {
326326
}
327327

328328
@Override
329-
Boolean supportsTrustBoundary() {
329+
boolean supportsTrustBoundary() {
330330
return true;
331331
}
332332

oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public class OAuth2Utils {
9494

9595
static final URI TOKEN_REVOKE_URI = URI.create("https://oauth2.googleapis.com/revoke");
9696

97-
public static final String IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_SERVICE_ACCOUNT =
97+
static final String IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_SERVICE_ACCOUNT =
9898
"https://iamcredentials.%s/v1/projects/-/serviceAccounts/%s/allowedLocations";
9999
static final URI USER_AUTH_URI = URI.create("https://accounts.google.com/o/oauth2/auth");
100100

oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ public boolean getUseJwtAccessWithScope() {
824824
}
825825

826826
@Override
827-
Boolean supportsTrustBoundary() {
827+
boolean supportsTrustBoundary() {
828828
return true;
829829
}
830830

oauth2_http/java/com/google/auth/oauth2/TrustBoundary.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ static TrustBoundary refresh(
211211
throw new IOException("TrustBoundary: Failure while getting trust boundaries:", e);
212212
}
213213
String encodedLocations = json.getEncodedLocations();
214+
// The encodedLocations is the value attached to the x-allowed-locations header and
215+
// it should always have a value. In case of NO_OP the lookup endpoint returns
216+
// encodedLocations as '0x0' and locations as null. That is why we only check for
217+
// encodedLocations.
214218
if (encodedLocations == null) {
215219
throw new IOException(
216220
"TrustBoundary: Malformed response from lookup endpoint - `encodedLocations` was null.");

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,14 +1183,12 @@ public void refresh_trustBoundaryFails_throwsIOException() throws IOException {
11831183
ComputeEngineCredentials credentials =
11841184
ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build();
11851185

1186-
try {
1187-
credentials.refresh();
1188-
} catch (IOException e) {
1189-
assertTrue(
1190-
"The exception message should explain why the refresh failed.",
1191-
e.getMessage()
1192-
.contains("Failed to refresh trust boundary and no cached value is available."));
1193-
}
1186+
IOException exception = assertThrows(IOException.class, () -> credentials.refresh());
1187+
assertTrue(
1188+
"The exception message should explain why the refresh failed.",
1189+
exception
1190+
.getMessage()
1191+
.contains("Failed to refresh trust boundary and no cached value is available."));
11941192
}
11951193

11961194
static class MockMetadataServerTransportFactory implements HttpTransportFactory {

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static org.junit.Assert.assertNotNull;
3737
import static org.junit.Assert.assertNull;
3838
import static org.junit.Assert.assertSame;
39+
import static org.junit.Assert.assertThrows;
3940
import static org.junit.Assert.assertTrue;
4041
import static org.junit.Assert.fail;
4142

@@ -960,7 +961,7 @@ public void trustBoundary_shouldNotCallLookupEndpointWhenDisabled() throws IOExc
960961
.build();
961962

962963
credentials.getRequestMetadata();
963-
assertEquals(credentials.getTrustBoundary(), null);
964+
assertNull(credentials.getTrustBoundary());
964965
}
965966

966967
@Test
@@ -1076,12 +1077,9 @@ public void trustBoundary_refreshShouldThrowWhenNoValidAccessTokenIsPassed() thr
10761077
.setScopes(SCOPES)
10771078
.build();
10781079

1079-
try {
1080-
credentials.getRequestMetadata();
1081-
fail("Should have thrown an IOException.");
1082-
} catch (IllegalArgumentException e) {
1083-
assertEquals("The provided access token is expired.", e.getMessage());
1084-
}
1080+
IllegalArgumentException exception =
1081+
assertThrows(IllegalArgumentException.class, () -> credentials.getRequestMetadata());
1082+
assertEquals("The provided access token is expired.", exception.getMessage());
10851083
}
10861084

10871085
@Test
@@ -1194,15 +1192,11 @@ public void trustBoundary_refreshShouldThrowIfCallToLookupFailsAndNoCachedTb()
11941192
.setHttpTransportFactory(() -> transport)
11951193
.setScopes(SCOPES)
11961194
.build();
1197-
1198-
try {
1199-
credentials.refresh();
1200-
fail("Should have thrown an IOException.");
1201-
} catch (IOException e) {
1202-
assertTrue(
1203-
e.getMessage()
1204-
.contains("Failed to refresh trust boundary and no cached value is available."));
1205-
}
1195+
IOException exception = assertThrows(IOException.class, () -> credentials.refresh());
1196+
assertTrue(
1197+
exception
1198+
.getMessage()
1199+
.contains("Failed to refresh trust boundary and no cached value is available."));
12061200
}
12071201

12081202
@Test
@@ -1225,14 +1219,11 @@ public void trustBoundary_refreshShouldThrowInCaseOfMalformedResponse() throws I
12251219
.setScopes(SCOPES)
12261220
.build();
12271221

1228-
try {
1229-
credentials.refresh();
1230-
fail("Should have thrown an IOException.");
1231-
} catch (IOException e) {
1232-
assertTrue(
1233-
e.getMessage()
1234-
.contains("Failed to refresh trust boundary and no cached value is available."));
1235-
}
1222+
IOException exception = assertThrows(IOException.class, () -> credentials.refresh());
1223+
assertTrue(
1224+
exception
1225+
.getMessage()
1226+
.contains("Failed to refresh trust boundary and no cached value is available."));
12361227
}
12371228

12381229
@Test

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,14 +1334,12 @@ public void refresh_trustBoundaryFails_throwsIOException() throws IOException {
13341334
VALID_LIFETIME,
13351335
mockTransportFactory);
13361336

1337-
try {
1338-
targetCredentials.refresh();
1339-
} catch (IOException e) {
1340-
assertTrue(
1341-
"The exception message should explain why the refresh failed.",
1342-
e.getMessage()
1343-
.contains("Failed to refresh trust boundary and no cached value is available."));
1344-
}
1337+
IOException exception = assertThrows(IOException.class, () -> targetCredentials.refresh());
1338+
assertTrue(
1339+
"The exception message should explain why the refresh failed.",
1340+
exception
1341+
.getMessage()
1342+
.contains("Failed to refresh trust boundary and no cached value is available."));
13451343
}
13461344

13471345
public static String getDefaultExpireTime() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ public void getRequestMetadata_hasAccessToken() throws IOException {
441441

442442
TestUtils.assertContainsBearerToken(metadata, ACCESS_TOKEN);
443443

444-
assertEquals(6, testAppender.events.size());
444+
assertEquals(8, testAppender.events.size());
445445

446446
ILoggingEvent defaultServiceAccountRequest = testAppender.events.get(0);
447447
assertEquals(

0 commit comments

Comments
 (0)