Skip to content

Commit 1efce2d

Browse files
committed
assertThrows changes. Fixed TestUtils getDefaultExpireTime to represent time in UTC.
1 parent fe772d8 commit 1efce2d

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

oauth2_http/javatests/com/google/auth/TestUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import java.util.HashMap;
5757
import java.util.List;
5858
import java.util.Map;
59+
import java.util.TimeZone;
5960
import javax.annotation.Nullable;
6061

6162
/** Utilities for test code under com.google.auth. */
@@ -150,8 +151,10 @@ public static HttpResponseException buildHttpResponseException(
150151
public static String getDefaultExpireTime() {
151152
Calendar calendar = Calendar.getInstance();
152153
calendar.setTime(new Date());
153-
calendar.add(Calendar.SECOND, 3000);
154-
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(calendar.getTime());
154+
calendar.add(Calendar.SECOND, 300);
155+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
156+
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
157+
return dateFormat.format(calendar.getTime());
155158
}
156159

157160
private TestUtils() {}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import static org.junit.Assert.assertNotNull;
3939
import static org.junit.Assert.assertNull;
4040
import static org.junit.Assert.assertSame;
41+
import static org.junit.Assert.assertThrows;
4142
import static org.junit.Assert.assertTrue;
4243
import static org.junit.Assert.fail;
4344

@@ -1243,7 +1244,6 @@ public void testRefresh_trustBoundarySuccess() throws IOException {
12431244
TrustBoundary trustBoundary = credentials.getTrustBoundary();
12441245
assertNotNull(trustBoundary);
12451246
assertEquals(TestUtils.TRUST_BOUNDARY_ENCODED_LOCATION, trustBoundary.getEncodedLocations());
1246-
TrustBoundary.setEnvironmentProviderForTest(null);
12471247
}
12481248

12491249
@Test
@@ -1262,15 +1262,15 @@ public void testRefresh_trustBoundaryFails_incorrectAudience() throws IOExceptio
12621262
.setTokenUrl(TOKEN_URL)
12631263
.build();
12641264

1265-
try {
1266-
credentials.refresh();
1267-
fail("Expected IOException to be thrown.");
1268-
} catch (IOException e) {
1269-
assertEquals(
1270-
"The provided audience is not in the correct format for a workforce pool.",
1271-
e.getMessage());
1272-
}
1273-
TrustBoundary.setEnvironmentProviderForTest(null);
1265+
IOException exception =
1266+
assertThrows(
1267+
IOException.class,
1268+
() -> {
1269+
credentials.refresh();
1270+
});
1271+
assertEquals(
1272+
"The provided audience is not in the correct format for a workforce pool.",
1273+
exception.getMessage());
12741274
}
12751275

12761276
@Test

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

Lines changed: 14 additions & 3 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

@@ -1287,15 +1288,25 @@ public void getTrustBoundaryUrl_workforce() throws IOException {
12871288
assertEquals(expectedUrl, credentials.getTrustBoundaryUrl());
12881289
}
12891290

1290-
@Test(expected = IOException.class)
1291-
public void getTrustBoundaryUrl_invalidAudience_throws() throws IOException {
1291+
@Test
1292+
public void getTrustBoundaryUrl_invalidAudience_throws() {
12921293
ExternalAccountCredentials credentials =
12931294
TestExternalAccountCredentials.newBuilder()
12941295
.setAudience("invalid-audience")
12951296
.setSubjectTokenType("subject_token_type")
12961297
.setCredentialSource(new TestCredentialSource(FILE_CREDENTIAL_SOURCE_MAP))
12971298
.build();
1298-
credentials.getTrustBoundaryUrl();
1299+
1300+
IOException exception =
1301+
assertThrows(
1302+
IOException.class,
1303+
() -> {
1304+
credentials.getTrustBoundaryUrl();
1305+
});
1306+
1307+
assertEquals(
1308+
"The provided audience is not in a valid format for either a workload identity pool or a workforce pool.",
1309+
exception.getMessage());
12991310
}
13001311

13011312
@Test

0 commit comments

Comments
 (0)