11package com .databricks .jdbc .auth ;
22
3+ import static com .databricks .jdbc .TestConstants .TEST_AUTH_URL ;
4+ import static com .databricks .jdbc .TestConstants .TEST_TOKEN_URL ;
35import static org .junit .jupiter .api .Assertions .*;
46import static org .mockito .ArgumentMatchers .any ;
57import static org .mockito .Mockito .*;
1113import com .databricks .sdk .core .HeaderFactory ;
1214import com .databricks .sdk .core .commons .CommonsHttpClient ;
1315import com .databricks .sdk .core .http .Response ;
16+ import com .databricks .sdk .core .oauth .OAuthResponse ;
1417import com .databricks .sdk .core .oauth .OpenIDConnectEndpoints ;
1518import com .databricks .sdk .core .oauth .Token ;
16- import java .io .ByteArrayInputStream ;
1719import java .io .IOException ;
18- import java .io . InputStream ;
20+ import java .net . URL ;
1921import java .util .Map ;
2022import java .util .Properties ;
2123import org .apache .http .HttpHeaders ;
@@ -34,6 +36,9 @@ public class OAuthRefreshCredentialsProviderTest {
3436 @ Mock DatabricksConfig databricksConfig ;
3537 @ Mock CommonsHttpClient httpClient ;
3638 @ Mock Response httpResponse ;
39+ @ Mock OAuthResponse oAuthResponse ;
40+
41+ @ Mock Response response ;
3742 private OAuthRefreshCredentialsProvider credentialsProvider ;
3843 private static final String REFRESH_TOKEN_URL_DEFAULT =
3944 "jdbc:databricks://host:4423/default;transportMode=http;ssl=1;AuthMech=11;AuthFlow=0;httpPath=/sql/1.0/warehouses/erg6767gg;OAuthRefreshToken=refresh-token" ;
@@ -89,27 +94,21 @@ void testRefreshSuccess(String refreshTokenUrl) throws Exception {
8994 boolean isDefaultEndpointPath = connectionContext .getTokenEndpoint () == null ;
9095 if (isDefaultEndpointPath ) {
9196 when (databricksConfig .getOidcEndpoints ())
92- .thenReturn (
93- new OpenIDConnectEndpoints (
94- "https://oauth.example.com/oidc/v1/token" ,
95- "https://oauth.example.com/oidc/v1/authorize" ));
97+ .thenReturn (new OpenIDConnectEndpoints (TEST_TOKEN_URL , TEST_AUTH_URL ));
9698 }
9799 credentialsProvider = new OAuthRefreshCredentialsProvider (connectionContext , databricksConfig );
98100
99101 // Reinitialize the OAUTH_RESPONSE InputStream for each test run
100- InputStream oauthResponse =
101- new ByteArrayInputStream (
102- new JSONObject ()
103- .put ("access_token" , "access-token" )
104- .put ("token_type" , "token-type" )
105- .put ("expires_in" , 360 )
106- .put ("refresh_token" , "refresh-token" )
107- .toString ()
108- .getBytes ());
102+ String jsonResponse =
103+ new JSONObject ()
104+ .put ("access_token" , "access-token" )
105+ .put ("token_type" , "token-type" )
106+ .put ("expires_in" , 360 )
107+ .put ("refresh_token" , "refresh-token" )
108+ .toString ();
109109
110110 when (databricksConfig .getHttpClient ()).thenReturn (httpClient );
111- when (httpClient .execute (any ())).thenReturn (httpResponse );
112- when (httpResponse .getBody ()).thenReturn (oauthResponse );
111+ when (httpClient .execute (any ())).thenReturn (new Response (jsonResponse , new URL (TEST_TOKEN_URL )));
113112 HeaderFactory headerFactory = credentialsProvider .configure (databricksConfig );
114113 Map <String , String > headers = headerFactory .headers ();
115114 assertNotNull (headers .get (HttpHeaders .AUTHORIZATION ));
0 commit comments