3333
3434import static org .junit .Assert .assertEquals ;
3535import static org .junit .Assert .assertNotNull ;
36+ import static org .junit .Assert .assertTrue ;
3637import static org .junit .Assert .fail ;
3738
3839import com .google .api .client .json .GenericJson ;
3940import com .google .auth .TestUtils ;
4041import java .io .IOException ;
4142import java .io .InputStream ;
43+ import org .junit .Assert ;
4244import org .junit .Test ;
4345
4446public 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 )
0 commit comments