Skip to content

Commit b46d2a1

Browse files
committed
formatting and changing the tests to use assertThrows
1 parent 889015c commit b46d2a1

File tree

4 files changed

+28
-55
lines changed

4 files changed

+28
-55
lines changed

oauth2_http/java/com/google/auth/mtls/WorkloadCertificateConfiguration.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,9 @@ String getPrivateKeyPath() {
6262
return privateKeyPath;
6363
}
6464

65-
66-
6765
static WorkloadCertificateConfiguration fromCertificateConfigurationStream(
6866
InputStream certConfigStream) throws IOException {
69-
if (certConfigStream == null){
67+
if (certConfigStream == null) {
7068
throw new IllegalArgumentException("certConfigStream must not be null.");
7169
}
7270

oauth2_http/java/com/google/auth/mtls/X509Provider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,21 @@ public X509Provider(String certConfigPathOverride) {
5252
this.certConfigPathOverride = certConfigPathOverride;
5353
}
5454

55-
public X509Provider(){
55+
public X509Provider() {
5656
this(null);
5757
}
5858

5959
/**
6060
* Finds the certificate configuration file, then builds a Keystore using the X.509 certificate
6161
* and private key pointed to by the configuration. This will check the following locations in
6262
* order.
63+
*
6364
* <ul>
6465
* <li>The certificate config override path, if set.
6566
* <li>The path pointed to by the "GOOGLE_API_CERTIFICATE_CONFIG" environment variable
6667
* <li>The well known gcloud location for the certificate configuration file.
6768
* </ul>
69+
*
6870
* @return a KeyStore containing the X.509 certificate specified by the certificate configuration.
6971
* @throws IOException if there is an error retrieving the certificate configuration.
7072
*/

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

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333

3434
import static org.junit.Assert.assertEquals;
3535
import static org.junit.Assert.assertNotNull;
36+
import static org.junit.Assert.assertTrue;
3637
import static org.junit.Assert.fail;
3738

3839
import com.google.api.client.json.GenericJson;
3940
import com.google.auth.TestUtils;
4041
import java.io.IOException;
4142
import java.io.InputStream;
43+
import org.junit.Assert;
4244
import org.junit.Test;
4345

4446
public class WorkloadCertificateConfigurationTest {
@@ -60,15 +62,9 @@ public void workloadCertificateConfig_fromStreamMissingCertPath_Fails() throws I
6062
String privateKeyPath = "key.crt";
6163
InputStream configStream = writeWorkloadCertificateConfigStream(certPath, privateKeyPath);
6264

63-
try {
64-
WorkloadCertificateConfiguration config =
65-
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream);
66-
fail("Expected an error due to missing the certificate path field.");
67-
} catch (IllegalArgumentException e) {
68-
assertEquals(
69-
"The cert_path field must be provided in the workload certificate configuration.",
70-
e.getMessage());
71-
}
65+
IllegalArgumentException exception =
66+
Assert.assertThrows(IllegalArgumentException.class, () -> WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream));
67+
assertTrue(exception.getMessage().contains("The cert_path field must be provided in the workload certificate configuration."));
7268
}
7369

7470
@Test
@@ -77,15 +73,9 @@ public void workloadCertificateConfig_fromStreamMissingPrivateKeyPath_Fails() th
7773
String privateKeyPath = "";
7874
InputStream configStream = writeWorkloadCertificateConfigStream(certPath, privateKeyPath);
7975

80-
try {
81-
WorkloadCertificateConfiguration config =
82-
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream);
83-
fail("Expected an error due to missing the private key path field.");
84-
} catch (IllegalArgumentException e) {
85-
assertEquals(
86-
"The key_path field must be provided in the workload certificate configuration.",
87-
e.getMessage());
88-
}
76+
IllegalArgumentException exception =
77+
Assert.assertThrows(IllegalArgumentException.class, () -> WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream));
78+
assertTrue(exception.getMessage().contains("The key_path field must be provided in the workload certificate configuration."));
8979
}
9080

9181
@Test
@@ -94,31 +84,19 @@ public void workloadCertificateConfig_fromStreamMissingWorkload_Fails() throws I
9484
json.put("cert_configs", new GenericJson());
9585
InputStream configStream = TestUtils.jsonToInputStream(json);
9686

97-
try {
98-
WorkloadCertificateConfiguration config =
99-
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream);
100-
fail("Expected an error due to missing the workload field.");
101-
} catch (IllegalArgumentException e) {
102-
assertEquals(
103-
"A workload certificate configuration must be provided in the cert_configs object.",
104-
e.getMessage());
105-
}
87+
IllegalArgumentException exception =
88+
Assert.assertThrows(IllegalArgumentException.class, () -> WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream));
89+
assertTrue(exception.getMessage().contains("A workload certificate configuration must be provided in the cert_configs object."));
10690
}
10791

10892
@Test
10993
public void workloadCertificateConfig_fromStreamMissingCertConfig_Fails() throws IOException {
11094
GenericJson json = new GenericJson();
11195
InputStream configStream = TestUtils.jsonToInputStream(json);
11296

113-
try {
114-
WorkloadCertificateConfiguration config =
115-
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream);
116-
fail("Expected an error due to missing the cert_config field.");
117-
} catch (IllegalArgumentException e) {
118-
assertEquals(
119-
"The cert_configs object must be provided in the certificate configuration file.",
120-
e.getMessage());
121-
}
97+
IllegalArgumentException exception =
98+
Assert.assertThrows(IllegalArgumentException.class, () -> WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream));
99+
assertTrue(exception.getMessage().contains("The cert_configs object must be provided in the certificate configuration file."));
122100
}
123101

124102
static InputStream writeWorkloadCertificateConfigStream(String certPath, String privateKeyPath)

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.security.cert.CertificateFactory;
4747
import java.util.HashMap;
4848
import java.util.Map;
49+
import org.junit.Assert;
4950
import org.junit.Test;
5051

5152
public class X509ProviderTest {
@@ -93,13 +94,9 @@ public void x509Provider_fileDoesntExist_throws() {
9394
"Error reading certificate configuration file value '%s': File does not exist.",
9495
certConfigPath);
9596

96-
try {
97-
testProvider.getKeyStore();
98-
fail("No key stores expected.");
99-
} catch (IOException e) {
100-
String message = e.getMessage();
101-
assertTrue(message.equals(expectedErrorMessage));
102-
}
97+
IOException exception =
98+
Assert.assertThrows(IOException.class, () -> testProvider.getKeyStore());
99+
assertTrue(exception.getMessage().contains(expectedErrorMessage));
103100
}
104101

105102
@Test
@@ -113,13 +110,9 @@ public void x509Provider_emptyFile_throws() {
113110
"Error reading certificate configuration file value '%s': no JSON input found",
114111
certConfigPath);
115112

116-
try {
117-
testProvider.getKeyStore();
118-
fail("No key store expected.");
119-
} catch (IOException e) {
120-
String message = e.getMessage();
121-
assertTrue(message.equals(expectedErrorMessage));
122-
}
113+
IOException exception =
114+
Assert.assertThrows(IOException.class, () -> testProvider.getKeyStore());
115+
assertTrue(exception.getMessage().contains(expectedErrorMessage));
123116
}
124117

125118
@Test
@@ -203,7 +196,9 @@ static class TestX509Provider extends X509Provider {
203196
private Map<String, String> variables;
204197
private Map<String, String> properties;
205198

206-
TestX509Provider() {this(null);}
199+
TestX509Provider() {
200+
this(null);
201+
}
207202

208203
TestX509Provider(String filePathOverride) {
209204
super(filePathOverride);

0 commit comments

Comments
 (0)