Skip to content

Commit 40c9b0b

Browse files
committed
test: Fix sonatype complaints in the tests (#1870)
* chore: migrate tests from JUnit 4 to JUnit 5 * fix: move @test annotations to separate lines * chore: Fix JUnit annotations being on the same line * chore: Fix samples imports to use new junit and truth * chore: Fix sonatype complaints in tests * chore: Address sonatype complaints * chore: Address the next batch of complaints * chore: Fix all the tests * refactor(TokenVerifierTest): use assertThrows for expected exceptions * chore: Fix line issues * chore: Fix remaining sonatype complaints * chore: Revert Junit5 changes in samples directory * chore: Revert Junit5 changes in samples directory * chore: Remove end of file whitespace * chore: Remove whitespace * chore: Fix lint issues Original-PR: googleapis/google-auth-library-java#1870
1 parent d2abcd6 commit 40c9b0b

File tree

49 files changed

+2192
-2731
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2192
-2731
lines changed

google-auth-library-java/appengine/javatests/com/google/auth/appengine/AppEngineCredentialsTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
import static org.junit.jupiter.api.Assertions.assertNotEquals;
3737
import static org.junit.jupiter.api.Assertions.assertNotNull;
3838
import static org.junit.jupiter.api.Assertions.assertNotSame;
39+
import static org.junit.jupiter.api.Assertions.assertThrows;
3940
import static org.junit.jupiter.api.Assertions.assertTrue;
40-
import static org.junit.jupiter.api.Assertions.fail;
4141

4242
import com.google.auth.Credentials;
4343
import com.google.auth.oauth2.AccessToken;
@@ -135,11 +135,7 @@ void createScoped_clonesWithScopes() throws IOException {
135135
.setAppIdentityService(appIdentity)
136136
.build();
137137
assertTrue(credentials.createScopedRequired());
138-
try {
139-
credentials.getRequestMetadata(CALL_URI);
140-
fail("Should not be able to use credential without scopes.");
141-
} catch (Exception expected) {
142-
}
138+
assertThrows(IOException.class, () -> credentials.getRequestMetadata(CALL_URI));
143139
assertEquals(0, appIdentity.getGetAccessTokenCallCount());
144140

145141
GoogleCredentials scopedCredentials = credentials.createScoped(SCOPES);

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
import java.io.ByteArrayInputStream;
4747
import java.io.IOException;
4848
import java.io.InputStream;
49-
import java.io.UnsupportedEncodingException;
5049
import java.net.URI;
5150
import java.net.URLDecoder;
51+
import java.nio.charset.StandardCharsets;
5252
import java.text.SimpleDateFormat;
5353
import java.util.Calendar;
5454
import java.util.Date;
@@ -90,15 +90,11 @@ private static boolean hasBearerToken(Map<String, List<String>> metadata, String
9090
public static InputStream jsonToInputStream(GenericJson json) throws IOException {
9191
json.setFactory(JSON_FACTORY);
9292
String text = json.toPrettyString();
93-
return new ByteArrayInputStream(text.getBytes("UTF-8"));
93+
return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
9494
}
9595

9696
public static InputStream stringToInputStream(String text) {
97-
try {
98-
return new ByteArrayInputStream(text.getBytes("UTF-8"));
99-
} catch (UnsupportedEncodingException e) {
100-
throw new RuntimeException("Unexpected encoding exception", e);
101-
}
97+
return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
10298
}
10399

104100
public static Map<String, String> parseQuery(String query) throws IOException {

google-auth-library-java/oauth2_http/javatests/com/google/auth/http/HttpCredentialsAdapterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void initialize_populatesOAuth2Credentials() throws IOException {
7979

8080
HttpHeaders requestHeaders = request.getHeaders();
8181
String authorizationHeader = requestHeaders.getAuthorization();
82-
assertEquals(authorizationHeader, expectedAuthorization);
82+
assertEquals(expectedAuthorization, authorizationHeader);
8383
}
8484

8585
@Test

google-auth-library-java/oauth2_http/javatests/com/google/auth/mtls/SecureConnectProviderTest.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.io.IOException;
3939
import java.io.InputStream;
4040
import java.io.OutputStream;
41-
import java.security.GeneralSecurityException;
4241
import java.util.List;
4342
import org.junit.jupiter.api.Test;
4443

@@ -70,7 +69,7 @@ public InputStream getErrorStream() {
7069
}
7170

7271
@Override
73-
public int waitFor() throws InterruptedException {
72+
public int waitFor() {
7473
return 0;
7574
}
7675

@@ -83,7 +82,9 @@ public int exitValue() {
8382
}
8483

8584
@Override
86-
public void destroy() {}
85+
public void destroy() {
86+
// Nothing was initialized and nothing needs to be destroyed
87+
}
8788
}
8889

8990
static class TestProcessProvider implements SecureConnectProvider.ProcessProvider {
@@ -102,19 +103,22 @@ public Process createProcess(InputStream metadata) throws IOException {
102103

103104
@Test
104105
void testGetKeyStoreNonZeroExitCode() {
105-
InputStream metadata =
106+
try (InputStream metadata =
106107
this.getClass()
107108
.getClassLoader()
108-
.getResourceAsStream("com/google/api/gax/rpc/mtls/mtlsCertAndKey.pem");
109-
IOException actual =
110-
assertThrows(
111-
IOException.class,
112-
() -> SecureConnectProvider.getKeyStore(metadata, new TestProcessProvider(1)));
113-
assertTrue(
114-
actual
115-
.getMessage()
116-
.contains("SecureConnect: Cert provider command failed with exit code: 1"),
117-
"expected to fail with nonzero exit code");
109+
.getResourceAsStream("com/google/api/gax/rpc/mtls/mtlsCertAndKey.pem")) {
110+
IOException actual =
111+
assertThrows(
112+
IOException.class,
113+
() -> SecureConnectProvider.getKeyStore(metadata, new TestProcessProvider(1)));
114+
assertTrue(
115+
actual
116+
.getMessage()
117+
.contains("SecureConnect: Cert provider command failed with exit code: 1"),
118+
"expected to fail with nonzero exit code");
119+
} catch (IOException e) {
120+
throw new RuntimeException(e);
121+
}
118122
}
119123

120124
@Test
@@ -147,8 +151,7 @@ void testRunCertificateProviderCommandTimeout() {
147151
}
148152

149153
@Test
150-
void testGetKeyStore_FileNotFoundException()
151-
throws IOException, GeneralSecurityException, InterruptedException {
154+
void testGetKeyStore_FileNotFoundException() {
152155
SecureConnectProvider provider =
153156
new SecureConnectProvider(new TestProcessProvider(0), "/invalid/metadata/path.json");
154157

google-auth-library-java/oauth2_http/javatests/com/google/auth/mtls/WorkloadCertificateConfigurationTest.java

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -58,71 +58,77 @@ void workloadCertificateConfig_fromStream_Succeeds() throws IOException {
5858
void workloadCertificateConfig_fromStreamMissingCertPath_Fails() throws IOException {
5959
String certPath = "";
6060
String privateKeyPath = "key.crt";
61-
InputStream configStream = writeWorkloadCertificateConfigStream(certPath, privateKeyPath);
62-
63-
IllegalArgumentException exception =
64-
assertThrows(
65-
IllegalArgumentException.class,
66-
() ->
67-
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream));
68-
assertTrue(
69-
exception
70-
.getMessage()
71-
.contains(
72-
"The cert_path field must be provided in the workload certificate configuration."));
61+
try (InputStream configStream =
62+
writeWorkloadCertificateConfigStream(certPath, privateKeyPath)) {
63+
IllegalArgumentException exception =
64+
assertThrows(
65+
IllegalArgumentException.class,
66+
() ->
67+
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(
68+
configStream));
69+
assertTrue(
70+
exception
71+
.getMessage()
72+
.contains(
73+
"The cert_path field must be provided in the workload certificate configuration."));
74+
}
7375
}
7476

7577
@Test
7678
void workloadCertificateConfig_fromStreamMissingPrivateKeyPath_Fails() throws IOException {
7779
String certPath = "cert.crt";
7880
String privateKeyPath = "";
79-
InputStream configStream = writeWorkloadCertificateConfigStream(certPath, privateKeyPath);
80-
81-
IllegalArgumentException exception =
82-
assertThrows(
83-
IllegalArgumentException.class,
84-
() ->
85-
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream));
86-
assertTrue(
87-
exception
88-
.getMessage()
89-
.contains(
90-
"The key_path field must be provided in the workload certificate configuration."));
81+
try (InputStream configStream =
82+
writeWorkloadCertificateConfigStream(certPath, privateKeyPath)) {
83+
IllegalArgumentException exception =
84+
assertThrows(
85+
IllegalArgumentException.class,
86+
() ->
87+
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(
88+
configStream));
89+
assertTrue(
90+
exception
91+
.getMessage()
92+
.contains(
93+
"The key_path field must be provided in the workload certificate configuration."));
94+
}
9195
}
9296

9397
@Test
9498
void workloadCertificateConfig_fromStreamMissingWorkload_Fails() throws IOException {
9599
GenericJson json = new GenericJson();
96100
json.put("cert_configs", new GenericJson());
97-
InputStream configStream = TestUtils.jsonToInputStream(json);
98-
99-
CertificateSourceUnavailableException exception =
100-
assertThrows(
101-
CertificateSourceUnavailableException.class,
102-
() ->
103-
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream));
104-
assertTrue(
105-
exception
106-
.getMessage()
107-
.contains(
108-
"A workload certificate configuration must be provided in the cert_configs object."));
101+
try (InputStream configStream = TestUtils.jsonToInputStream(json)) {
102+
CertificateSourceUnavailableException exception =
103+
assertThrows(
104+
CertificateSourceUnavailableException.class,
105+
() ->
106+
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(
107+
configStream));
108+
assertTrue(
109+
exception
110+
.getMessage()
111+
.contains(
112+
"A workload certificate configuration must be provided in the cert_configs object."));
113+
}
109114
}
110115

111116
@Test
112117
void workloadCertificateConfig_fromStreamMissingCertConfig_Fails() throws IOException {
113118
GenericJson json = new GenericJson();
114-
InputStream configStream = TestUtils.jsonToInputStream(json);
115-
116-
IllegalArgumentException exception =
117-
assertThrows(
118-
IllegalArgumentException.class,
119-
() ->
120-
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream));
121-
assertTrue(
122-
exception
123-
.getMessage()
124-
.contains(
125-
"The cert_configs object must be provided in the certificate configuration file."));
119+
try (InputStream configStream = TestUtils.jsonToInputStream(json)) {
120+
IllegalArgumentException exception =
121+
assertThrows(
122+
IllegalArgumentException.class,
123+
() ->
124+
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(
125+
configStream));
126+
assertTrue(
127+
exception
128+
.getMessage()
129+
.contains(
130+
"The cert_configs object must be provided in the certificate configuration file."));
131+
}
126132
}
127133

128134
static InputStream writeWorkloadCertificateConfigStream(String certPath, String privateKeyPath)

google-auth-library-java/oauth2_http/javatests/com/google/auth/mtls/X509ProviderTest.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,24 @@ void x509Provider_succeeds_withEnvVariable()
140140
String certConfigPath = "certConfig.txt";
141141
String certPath = "cert.crt";
142142
String keyPath = "key.crt";
143-
InputStream certConfigStream =
143+
try (InputStream certConfigStream =
144144
WorkloadCertificateConfigurationTest.writeWorkloadCertificateConfigStream(
145-
certPath, keyPath);
146-
147-
TestX509Provider testProvider = new TestX509Provider();
148-
testProvider.setEnv("GOOGLE_API_CERTIFICATE_CONFIG", certConfigPath);
149-
testProvider.addFile(certConfigPath, certConfigStream);
150-
testProvider.addFile(certPath, new ByteArrayInputStream(TEST_CERT.getBytes()));
151-
testProvider.addFile(keyPath, new ByteArrayInputStream(TEST_PRIVATE_KEY.getBytes()));
152-
153-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
154-
Certificate expectedCert =
155-
cf.generateCertificate(new ByteArrayInputStream(TEST_CERT.getBytes()));
156-
157-
// Assert that the store has the expected certificate and only the expected certificate.
158-
KeyStore store = testProvider.getKeyStore();
159-
assertEquals(1, store.size());
160-
assertNotNull(store.getCertificateAlias(expectedCert));
145+
certPath, keyPath)) {
146+
TestX509Provider testProvider = new TestX509Provider();
147+
testProvider.setEnv(certConfigPath);
148+
testProvider.addFile(certConfigPath, certConfigStream);
149+
testProvider.addFile(certPath, new ByteArrayInputStream(TEST_CERT.getBytes()));
150+
testProvider.addFile(keyPath, new ByteArrayInputStream(TEST_PRIVATE_KEY.getBytes()));
151+
152+
CertificateFactory cf = CertificateFactory.getInstance("X.509");
153+
Certificate expectedCert =
154+
cf.generateCertificate(new ByteArrayInputStream(TEST_CERT.getBytes()));
155+
156+
// Assert that the store has the expected certificate and only the expected certificate.
157+
KeyStore store = testProvider.getKeyStore();
158+
assertEquals(1, store.size());
159+
assertNotNull(store.getCertificateAlias(expectedCert));
160+
}
161161
}
162162

163163
@Test
@@ -166,24 +166,24 @@ void x509Provider_succeeds_withWellKnownPath()
166166
String certConfigPath = "certConfig.txt";
167167
String certPath = "cert.crt";
168168
String keyPath = "key.crt";
169-
InputStream certConfigStream =
169+
try (InputStream certConfigStream =
170170
WorkloadCertificateConfigurationTest.writeWorkloadCertificateConfigStream(
171-
certPath, keyPath);
172-
173-
TestX509Provider testProvider = new TestX509Provider();
174-
testProvider.setEnv("GOOGLE_API_CERTIFICATE_CONFIG", certConfigPath);
175-
testProvider.addFile(certConfigPath, certConfigStream);
176-
testProvider.addFile(certPath, new ByteArrayInputStream(TEST_CERT.getBytes()));
177-
testProvider.addFile(keyPath, new ByteArrayInputStream(TEST_PRIVATE_KEY.getBytes()));
178-
179-
CertificateFactory cf = CertificateFactory.getInstance("X.509");
180-
Certificate expectedCert =
181-
cf.generateCertificate(new ByteArrayInputStream(TEST_CERT.getBytes()));
182-
183-
// Assert that the store has the expected certificate and only the expected certificate.
184-
KeyStore store = testProvider.getKeyStore();
185-
assertEquals(1, store.size());
186-
assertNotNull(store.getCertificateAlias(expectedCert));
171+
certPath, keyPath)) {
172+
TestX509Provider testProvider = new TestX509Provider();
173+
testProvider.setEnv(certConfigPath);
174+
testProvider.addFile(certConfigPath, certConfigStream);
175+
testProvider.addFile(certPath, new ByteArrayInputStream(TEST_CERT.getBytes()));
176+
testProvider.addFile(keyPath, new ByteArrayInputStream(TEST_PRIVATE_KEY.getBytes()));
177+
178+
CertificateFactory cf = CertificateFactory.getInstance("X.509");
179+
Certificate expectedCert =
180+
cf.generateCertificate(new ByteArrayInputStream(TEST_CERT.getBytes()));
181+
182+
// Assert that the store has the expected certificate and only the expected certificate.
183+
KeyStore store = testProvider.getKeyStore();
184+
assertEquals(1, store.size());
185+
assertNotNull(store.getCertificateAlias(expectedCert));
186+
}
187187
}
188188

189189
static class TestX509Provider extends X509Provider {
@@ -211,8 +211,8 @@ String getEnv(String name) {
211211
return variables.get(name);
212212
}
213213

214-
void setEnv(String name, String value) {
215-
variables.put(name, value);
214+
void setEnv(String value) {
215+
variables.put("GOOGLE_API_CERTIFICATE_CONFIG", value);
216216
}
217217

218218
@Override

0 commit comments

Comments
 (0)