Skip to content

Commit d7edc45

Browse files
authored
chore: migrate tests in oauth2_http module from JUnit4 to JUnit5 - fourth iteration (#756) (#766)
1 parent ec29abc commit d7edc45

File tree

4 files changed

+87
-92
lines changed

4 files changed

+87
-92
lines changed

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131

3232
package com.google.auth.oauth2;
3333

34-
import static org.junit.Assert.assertEquals;
35-
import static org.junit.Assert.assertTrue;
36-
import static org.junit.Assert.fail;
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertThrows;
36+
import static org.junit.jupiter.api.Assertions.assertTrue;
3737

3838
import com.google.api.client.http.GenericUrl;
3939
import com.google.api.client.http.HttpRequest;
@@ -46,7 +46,7 @@
4646
import com.google.auth.Credentials;
4747
import com.google.auth.http.HttpCredentialsAdapter;
4848
import java.io.IOException;
49-
import org.junit.Test;
49+
import org.junit.jupiter.api.Test;
5050

5151
/**
5252
* Integration tests for Downscoping with Credential Access Boundaries via {@link
@@ -56,7 +56,7 @@
5656
* GOOGLE_APPLICATION_CREDENTIALS to point to the same service account configured in the setup
5757
* script (downscoping-with-cab-setup.sh).
5858
*/
59-
public final class ITDownscopingTest {
59+
class ITDownscopingTest {
6060

6161
// Output copied from the setup script (downscoping-with-cab-setup.sh).
6262
private static final String GCS_BUCKET_NAME = "cab-int-bucket-cbi3qrv5";
@@ -93,7 +93,7 @@ public final class ITDownscopingTest {
9393
* in the same bucket.
9494
*/
9595
@Test
96-
public void downscoping_serviceAccountSourceWithRefresh() throws IOException {
96+
void downscoping_serviceAccountSourceWithRefresh() throws IOException {
9797
OAuth2CredentialsWithRefresh.OAuth2RefreshHandler refreshHandler =
9898
new OAuth2CredentialsWithRefresh.OAuth2RefreshHandler() {
9999
@Override
@@ -122,12 +122,12 @@ public AccessToken refreshAccessToken() throws IOException {
122122

123123
// Attempt to retrieve the object that the downscoped token does not have access to. This should
124124
// fail.
125-
try {
126-
retrieveObjectFromGcs(credentials, GCS_OBJECT_NAME_WITHOUT_PERMISSION);
127-
fail("Call to GCS should have failed.");
128-
} catch (HttpResponseException e) {
129-
assertEquals(403, e.getStatusCode());
130-
}
125+
HttpResponseException exception =
126+
assertThrows(
127+
HttpResponseException.class,
128+
() -> retrieveObjectFromGcs(credentials, GCS_OBJECT_NAME_WITHOUT_PERMISSION),
129+
"Call to GCS should have failed.");
130+
assertEquals(403, exception.getStatusCode());
131131
}
132132

133133
private void retrieveObjectFromGcs(Credentials credentials, String objectName)

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

+10-12
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131

3232
package com.google.auth.oauth2;
3333

34-
import static org.junit.Assert.assertTrue;
35-
import static org.junit.Assert.fail;
34+
import static org.junit.jupiter.api.Assertions.assertNotNull;
35+
import static org.junit.jupiter.api.Assertions.assertTrue;
3636

3737
import com.google.api.client.http.GenericUrl;
3838
import com.google.api.client.http.HttpRequest;
@@ -51,8 +51,8 @@
5151
import java.io.FileInputStream;
5252
import java.io.IOException;
5353
import java.nio.charset.StandardCharsets;
54-
import org.junit.Before;
55-
import org.junit.Test;
54+
import org.junit.jupiter.api.BeforeEach;
55+
import org.junit.jupiter.api.Test;
5656

5757
/**
5858
* Integration tests for Workload Identity Federation.
@@ -62,7 +62,7 @@
6262
* (workloadidentityfederation-setup). These tests call GCS to get bucket information. The bucket
6363
* name must be provided through the GCS_BUCKET environment variable.
6464
*/
65-
public final class ITWorkloadIdentityFederationTest {
65+
class ITWorkloadIdentityFederationTest {
6666

6767
// Copy output from workloadidentityfederation-setup.
6868
private static final String AUDIENCE_PREFIX =
@@ -75,8 +75,8 @@ public final class ITWorkloadIdentityFederationTest {
7575

7676
private String clientEmail;
7777

78-
@Before
79-
public void setup() throws IOException {
78+
@BeforeEach
79+
void setup() throws IOException {
8080
GenericJson keys = getServiceAccountKeyFileAsJson();
8181
clientEmail = (String) keys.get("client_email");
8282
}
@@ -89,7 +89,7 @@ public void setup() throws IOException {
8989
* service account key.
9090
*/
9191
@Test
92-
public void identityPoolCredentials() throws IOException {
92+
void identityPoolCredentials() throws IOException {
9393
IdentityPoolCredentials identityPoolCredentials =
9494
(IdentityPoolCredentials)
9595
ExternalAccountCredentials.fromJson(
@@ -108,7 +108,7 @@ public void identityPoolCredentials() throws IOException {
108108
* service account key.
109109
*/
110110
@Test
111-
public void awsCredentials() throws Exception {
111+
void awsCredentials() throws Exception {
112112
String idToken = generateGoogleIdToken(AWS_AUDIENCE);
113113

114114
String url =
@@ -202,9 +202,7 @@ private GenericJson buildAwsCredentialConfig() {
202202

203203
private void callGcs(GoogleCredentials credentials) throws IOException {
204204
String bucketName = System.getenv("GCS_BUCKET");
205-
if (bucketName == null) {
206-
fail("GCS bucket name not set through GCS_BUCKET env variable.");
207-
}
205+
assertNotNull(bucketName, "GCS bucket name not set through GCS_BUCKET env variable.");
208206

209207
String url = "https://storage.googleapis.com/storage/v1/b/" + bucketName;
210208

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@
3131

3232
package com.google.auth.oauth2;
3333

34-
import static org.junit.Assert.*;
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertNotNull;
36+
import static org.junit.jupiter.api.Assertions.assertNull;
37+
import static org.junit.jupiter.api.Assertions.assertTrue;
3538

3639
import java.util.Collections;
3740
import java.util.Map;
38-
import org.junit.Test;
39-
import org.junit.runner.RunWith;
40-
import org.junit.runners.JUnit4;
41+
import org.junit.jupiter.api.Test;
4142

42-
@RunWith(JUnit4.class)
4343
public class JwtClaimsTest {
4444

4545
@Test
46-
public void testMergeOverwritesFields() {
46+
void testMergeOverwritesFields() {
4747
JwtClaims claims1 =
4848
JwtClaims.newBuilder()
4949
.setAudience("audience-1")
@@ -64,7 +64,7 @@ public void testMergeOverwritesFields() {
6464
}
6565

6666
@Test
67-
public void testMergeDefaultValues() {
67+
void testMergeDefaultValues() {
6868
JwtClaims claims1 =
6969
JwtClaims.newBuilder()
7070
.setAudience("audience-1")
@@ -80,7 +80,7 @@ public void testMergeDefaultValues() {
8080
}
8181

8282
@Test
83-
public void testMergeNull() {
83+
void testMergeNull() {
8484
JwtClaims claims1 = JwtClaims.newBuilder().build();
8585
JwtClaims claims2 = JwtClaims.newBuilder().build();
8686
JwtClaims merged = claims1.merge(claims2);
@@ -93,7 +93,7 @@ public void testMergeNull() {
9393
}
9494

9595
@Test
96-
public void testEquals() {
96+
void testEquals() {
9797
JwtClaims claims1 =
9898
JwtClaims.newBuilder()
9999
.setAudience("audience-1")
@@ -111,14 +111,14 @@ public void testEquals() {
111111
}
112112

113113
@Test
114-
public void testAdditionalClaimsDefaults() {
114+
void testAdditionalClaimsDefaults() {
115115
JwtClaims claims = JwtClaims.newBuilder().build();
116116
assertNotNull(claims.getAdditionalClaims());
117117
assertTrue(claims.getAdditionalClaims().isEmpty());
118118
}
119119

120120
@Test
121-
public void testMergeAdditionalClaims() {
121+
void testMergeAdditionalClaims() {
122122
JwtClaims claims1 =
123123
JwtClaims.newBuilder().setAdditionalClaims(Collections.singletonMap("foo", "bar")).build();
124124
JwtClaims claims2 =
@@ -138,7 +138,7 @@ public void testMergeAdditionalClaims() {
138138
}
139139

140140
@Test
141-
public void testIsComplete() {
141+
void testIsComplete() {
142142
// Test JwtClaim is complete if audience is not set but scope is provided.
143143
JwtClaims claims =
144144
JwtClaims.newBuilder()

0 commit comments

Comments
 (0)