2323import com .google .auth .oauth2 .OAuth2CredentialsWithRefresh ;
2424import com .google .cloud .sql .AuthType ;
2525import com .google .common .util .concurrent .Futures ;
26- import com .google .common .util .concurrent .ListenableFuture ;
2726import com .google .common .util .concurrent .ListeningScheduledExecutorService ;
2827import com .google .common .util .concurrent .MoreExecutors ;
2928import java .security .GeneralSecurityException ;
@@ -55,15 +54,13 @@ public void testFetchInstanceData_returnsIpAddresses()
5554 new StubApiFetcherFactory (mockAdminApi .getHttpTransport ())
5655 .create (new StubCredentialFactory ().create ());
5756
58- ListenableFuture < InstanceData > instanceDataFuture =
57+ InstanceData instanceData =
5958 fetcher .getInstanceData (
6059 new CloudSqlInstanceName (INSTANCE_CONNECTION_NAME ),
6160 null ,
6261 AuthType .PASSWORD ,
6362 newTestExecutor (),
6463 Futures .immediateFuture (mockAdminApi .getClientKeyPair ()));
65-
66- InstanceData instanceData = instanceDataFuture .get ();
6764 assertThat (instanceData .getSslContext ()).isInstanceOf (SSLContext .class );
6865
6966 Map <String , String > ipAddrs = instanceData .getIpAddrs ();
@@ -89,20 +86,22 @@ public void testFetchInstanceData_throwsException_whenIamAuthnIsNotSupported()
8986 new StubApiFetcherFactory (mockAdminApi .getHttpTransport ())
9087 .create (new StubCredentialFactory ().create ());
9188
92- ListenableFuture <InstanceData > instanceData =
93- fetcher .getInstanceData (
94- new CloudSqlInstanceName (INSTANCE_CONNECTION_NAME ),
95- OAuth2CredentialsWithRefresh .newBuilder ()
96- .setRefreshHandler (
97- mockAdminApi .getRefreshHandler (
98- "refresh-token" , Date .from (Instant .now ().plus (1 , ChronoUnit .HOURS ))))
99- .setAccessToken (new AccessToken ("my-token" , Date .from (Instant .now ())))
100- .build (),
101- AuthType .IAM ,
102- newTestExecutor (),
103- Futures .immediateFuture (mockAdminApi .getClientKeyPair ()));
104-
105- ExecutionException ex = assertThrows (ExecutionException .class , instanceData ::get );
89+ ExecutionException ex =
90+ assertThrows (
91+ ExecutionException .class ,
92+ () -> {
93+ fetcher .getInstanceData (
94+ new CloudSqlInstanceName (INSTANCE_CONNECTION_NAME ),
95+ OAuth2CredentialsWithRefresh .newBuilder ()
96+ .setRefreshHandler (
97+ mockAdminApi .getRefreshHandler (
98+ "refresh-token" , Date .from (Instant .now ().plus (1 , ChronoUnit .HOURS ))))
99+ .setAccessToken (new AccessToken ("my-token" , Date .from (Instant .now ())))
100+ .build (),
101+ AuthType .IAM ,
102+ newTestExecutor (),
103+ Futures .immediateFuture (mockAdminApi .getClientKeyPair ()));
104+ });
106105 assertThat (ex )
107106 .hasMessageThat ()
108107 .contains ("[p:r:i] IAM Authentication is not supported for SQL Server instances" );
@@ -116,20 +115,22 @@ public void testFetchInstanceData_throwsException_whenTokenIsEmpty()
116115 new StubApiFetcherFactory (mockAdminApi .getHttpTransport ())
117116 .create (new StubCredentialFactory ().create ());
118117
119- ListenableFuture <InstanceData > instanceData =
120- fetcher .getInstanceData (
121- new CloudSqlInstanceName (INSTANCE_CONNECTION_NAME ),
122- OAuth2CredentialsWithRefresh .newBuilder ()
123- .setRefreshHandler (
124- mockAdminApi .getRefreshHandler (
125- "" , Date .from (Instant .now ().plus (1 , ChronoUnit .HOURS )) /* empty */ ))
126- .setAccessToken (new AccessToken ("" /* ignored */ , Date .from (Instant .now ())))
127- .build (),
128- AuthType .IAM ,
129- newTestExecutor (),
130- Futures .immediateFuture (mockAdminApi .getClientKeyPair ()));
131-
132- ExecutionException ex = assertThrows (ExecutionException .class , instanceData ::get );
118+ ExecutionException ex =
119+ assertThrows (
120+ ExecutionException .class ,
121+ () -> {
122+ fetcher .getInstanceData (
123+ new CloudSqlInstanceName (INSTANCE_CONNECTION_NAME ),
124+ OAuth2CredentialsWithRefresh .newBuilder ()
125+ .setRefreshHandler (
126+ mockAdminApi .getRefreshHandler (
127+ "" , Date .from (Instant .now ().plus (1 , ChronoUnit .HOURS )) /* empty */ ))
128+ .setAccessToken (new AccessToken ("" /* ignored */ , Date .from (Instant .now ())))
129+ .build (),
130+ AuthType .IAM ,
131+ newTestExecutor (),
132+ Futures .immediateFuture (mockAdminApi .getClientKeyPair ()));
133+ });
133134
134135 assertThat (ex ).hasMessageThat ().contains ("Access Token has length of zero" );
135136 }
@@ -142,21 +143,23 @@ public void testFetchInstanceData_throwsException_whenTokenIsExpired()
142143 new StubApiFetcherFactory (mockAdminApi .getHttpTransport ())
143144 .create (new StubCredentialFactory ().create ());
144145
145- ListenableFuture <InstanceData > instanceData =
146- fetcher .getInstanceData (
147- new CloudSqlInstanceName (INSTANCE_CONNECTION_NAME ),
148- OAuth2CredentialsWithRefresh .newBuilder ()
149- .setRefreshHandler (
150- mockAdminApi .getRefreshHandler (
151- "refresh-token" ,
152- Date .from (Instant .now ().minus (1 , ChronoUnit .HOURS )) /* 1 hour ago */ ))
153- .setAccessToken (new AccessToken ("original-token" , Date .from (Instant .now ())))
154- .build (),
155- AuthType .IAM ,
156- newTestExecutor (),
157- Futures .immediateFuture (mockAdminApi .getClientKeyPair ()));
158-
159- ExecutionException ex = assertThrows (ExecutionException .class , instanceData ::get );
146+ ExecutionException ex =
147+ assertThrows (
148+ ExecutionException .class ,
149+ () -> {
150+ fetcher .getInstanceData (
151+ new CloudSqlInstanceName (INSTANCE_CONNECTION_NAME ),
152+ OAuth2CredentialsWithRefresh .newBuilder ()
153+ .setRefreshHandler (
154+ mockAdminApi .getRefreshHandler (
155+ "refresh-token" ,
156+ Date .from (Instant .now ().minus (1 , ChronoUnit .HOURS )) /* 1 hour ago */ ))
157+ .setAccessToken (new AccessToken ("original-token" , Date .from (Instant .now ())))
158+ .build (),
159+ AuthType .IAM ,
160+ newTestExecutor (),
161+ Futures .immediateFuture (mockAdminApi .getClientKeyPair ()));
162+ });
160163
161164 assertThat (ex ).hasMessageThat ().contains ("Access Token expiration time is in the past" );
162165 }
@@ -169,21 +172,24 @@ public void testFetchInstanceData_throwsException_whenRequestsTimeout()
169172 new StubApiFetcherFactory (new BadConnectionFactory ())
170173 .create (new StubCredentialFactory ().create ());
171174
172- ListenableFuture <InstanceData > instanceData =
173- fetcher .getInstanceData (
174- new CloudSqlInstanceName (INSTANCE_CONNECTION_NAME ),
175- OAuth2CredentialsWithRefresh .newBuilder ()
176- .setRefreshHandler (
177- mockAdminApi .getRefreshHandler (
178- "refresh-token" ,
179- Date .from (Instant .now ().plus (1 , ChronoUnit .HOURS )) /* 1 hour from now */ ))
180- .setAccessToken (new AccessToken ("original-token" , Date .from (Instant .now ())))
181- .build (),
182- AuthType .IAM ,
183- newTestExecutor (),
184- Futures .immediateFuture (mockAdminApi .getClientKeyPair ()));
185-
186- ExecutionException ex = assertThrows (ExecutionException .class , instanceData ::get );
175+ ExecutionException ex =
176+ assertThrows (
177+ ExecutionException .class ,
178+ () -> {
179+ fetcher .getInstanceData (
180+ new CloudSqlInstanceName (INSTANCE_CONNECTION_NAME ),
181+ OAuth2CredentialsWithRefresh .newBuilder ()
182+ .setRefreshHandler (
183+ mockAdminApi .getRefreshHandler (
184+ "refresh-token" ,
185+ Date .from (
186+ Instant .now ().plus (1 , ChronoUnit .HOURS )) /* 1 hour from now */ ))
187+ .setAccessToken (new AccessToken ("original-token" , Date .from (Instant .now ())))
188+ .build (),
189+ AuthType .IAM ,
190+ newTestExecutor (),
191+ Futures .immediateFuture (mockAdminApi .getClientKeyPair ()));
192+ });
187193
188194 assertThat (ex .getCause ().getCause ()).hasMessageThat ().contains ("Fake connect timeout" );
189195 }
0 commit comments