6666import java .util .stream .Collectors ;
6767import java .util .stream .IntStream ;
6868import java .util .stream .Stream ;
69+ import org .junit .Assert ;
6970import org .junit .Test ;
7071import org .junit .runner .RunWith ;
7172import org .junit .runners .JUnit4 ;
@@ -645,7 +646,7 @@ public LowLevelHttpResponse execute() throws IOException {
645646 }
646647
647648 @ Test
648- public void sign_sameAs () throws IOException {
649+ public void sign_sameAs () {
649650 MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory ();
650651 String defaultAccountEmail =
"[email protected] " ;
651652 byte [] expectedSignature = {0xD , 0xE , 0xA , 0xD };
@@ -659,21 +660,36 @@ public void sign_sameAs() throws IOException {
659660 }
660661
661662 @ Test
662- public void sign_getAccountFails () throws IOException {
663+ public void sign_getUniverseException () {
664+ MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory ();
665+
666+ String defaultAccountEmail =
"[email protected] " ;
667+ transportFactory .transport .setServiceAccountEmail (defaultAccountEmail );
668+ ComputeEngineCredentials credentials =
669+ ComputeEngineCredentials .newBuilder ().setHttpTransportFactory (transportFactory ).build ();
670+
671+ transportFactory .transport .setRequestStatusCode (501 );
672+ Assert .assertThrows (IOException .class , credentials ::getUniverseDomain );
673+
674+ byte [] expectedSignature = {0xD , 0xE , 0xA , 0xD };
675+ SigningException signingException =
676+ Assert .assertThrows (SigningException .class , () -> credentials .sign (expectedSignature ));
677+ assertEquals ("Failed to sign: Error obtaining universe domain" , signingException .getMessage ());
678+ }
679+
680+ @ Test
681+ public void sign_getAccountFails () {
663682 MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory ();
664683 byte [] expectedSignature = {0xD , 0xE , 0xA , 0xD };
665684
666685 transportFactory .transport .setSignature (expectedSignature );
667686 ComputeEngineCredentials credentials =
668687 ComputeEngineCredentials .newBuilder ().setHttpTransportFactory (transportFactory ).build ();
669688
670- try {
671- credentials .sign (expectedSignature );
672- fail ("Should not be able to use credential without exception." );
673- } catch (SigningException ex ) {
674- assertNotNull (ex .getMessage ());
675- assertNotNull (ex .getCause ());
676- }
689+ SigningException exception =
690+ Assert .assertThrows (SigningException .class , () -> credentials .sign (expectedSignature ));
691+ assertNotNull (exception .getMessage ());
692+ assertNotNull (exception .getCause ());
677693 }
678694
679695 @ Test
@@ -705,15 +721,13 @@ public LowLevelHttpResponse execute() throws IOException {
705721 ComputeEngineCredentials credentials =
706722 ComputeEngineCredentials .newBuilder ().setHttpTransportFactory (transportFactory ).build ();
707723
708- try {
709- byte [] bytes = {0xD , 0xE , 0xA , 0xD };
710- credentials .sign (bytes );
711- fail ("Signing should have failed" );
712- } catch (SigningException e ) {
713- assertEquals ("Failed to sign the provided bytes" , e .getMessage ());
714- assertNotNull (e .getCause ());
715- assertTrue (e .getCause ().getMessage ().contains ("403" ));
716- }
724+ byte [] bytes = {0xD , 0xE , 0xA , 0xD };
725+
726+ SigningException exception =
727+ Assert .assertThrows (SigningException .class , () -> credentials .sign (bytes ));
728+ assertEquals ("Failed to sign the provided bytes" , exception .getMessage ());
729+ assertNotNull (exception .getCause ());
730+ assertTrue (exception .getCause ().getMessage ().contains ("403" ));
717731 }
718732
719733 @ Test
@@ -745,15 +759,13 @@ public LowLevelHttpResponse execute() throws IOException {
745759 ComputeEngineCredentials credentials =
746760 ComputeEngineCredentials .newBuilder ().setHttpTransportFactory (transportFactory ).build ();
747761
748- try {
749- byte [] bytes = {0xD , 0xE , 0xA , 0xD };
750- credentials .sign (bytes );
751- fail ("Signing should have failed" );
752- } catch (SigningException e ) {
753- assertEquals ("Failed to sign the provided bytes" , e .getMessage ());
754- assertNotNull (e .getCause ());
755- assertTrue (e .getCause ().getMessage ().contains ("500" ));
756- }
762+ byte [] bytes = {0xD , 0xE , 0xA , 0xD };
763+
764+ SigningException exception =
765+ Assert .assertThrows (SigningException .class , () -> credentials .sign (bytes ));
766+ assertEquals ("Failed to sign the provided bytes" , exception .getMessage ());
767+ assertNotNull (exception .getCause ());
768+ assertTrue (exception .getCause ().getMessage ().contains ("500" ));
757769 }
758770
759771 @ Test
@@ -778,14 +790,11 @@ public LowLevelHttpResponse execute() throws IOException {
778790 ComputeEngineCredentials credentials =
779791 ComputeEngineCredentials .newBuilder ().setHttpTransportFactory (transportFactory ).build ();
780792
781- try {
782- credentials .refreshAccessToken ();
783- fail ("Should have failed" );
784- } catch (IOException e ) {
785- assertTrue (e .getCause ().getMessage ().contains ("503" ));
786- assertTrue (e instanceof GoogleAuthException );
787- assertTrue (((GoogleAuthException ) e ).isRetryable ());
788- }
793+ IOException exception =
794+ Assert .assertThrows (IOException .class , () -> credentials .refreshAccessToken ());
795+ assertTrue (exception .getCause ().getMessage ().contains ("503" ));
796+ assertTrue (exception instanceof GoogleAuthException );
797+ assertTrue (((GoogleAuthException ) exception ).isRetryable ());
789798 }
790799
791800 @ Test
@@ -818,12 +827,9 @@ public LowLevelHttpResponse execute() throws IOException {
818827 ComputeEngineCredentials credentials =
819828 ComputeEngineCredentials .newBuilder ().setHttpTransportFactory (transportFactory ).build ();
820829
821- try {
822- credentials .refreshAccessToken ();
823- fail ("Should have failed" );
824- } catch (IOException e ) {
825- assertFalse (e instanceof GoogleAuthException );
826- }
830+ IOException exception =
831+ Assert .assertThrows (IOException .class , () -> credentials .refreshAccessToken ());
832+ assertFalse (exception instanceof GoogleAuthException );
827833 }
828834 }
829835
@@ -993,15 +999,13 @@ public LowLevelHttpResponse execute() throws IOException {
993999 ComputeEngineCredentials credentials =
9941000 ComputeEngineCredentials .newBuilder ().setHttpTransportFactory (transportFactory ).build ();
9951001
996- try {
997- byte [] bytes = {0xD , 0xE , 0xA , 0xD };
998- credentials .sign (bytes );
999- fail ("Signing should have failed" );
1000- } catch (SigningException e ) {
1001- assertEquals ("Failed to sign the provided bytes" , e .getMessage ());
1002- assertNotNull (e .getCause ());
1003- assertTrue (e .getCause ().getMessage ().contains ("Empty content" ));
1004- }
1002+ byte [] bytes = {0xD , 0xE , 0xA , 0xD };
1003+
1004+ SigningException exception =
1005+ Assert .assertThrows (SigningException .class , () -> credentials .sign (bytes ));
1006+ assertEquals ("Failed to sign the provided bytes" , exception .getMessage ());
1007+ assertNotNull (exception .getCause ());
1008+ assertTrue (exception .getCause ().getMessage ().contains ("Empty content" ));
10051009 }
10061010
10071011 @ Test
0 commit comments