Skip to content

Commit b60ed0e

Browse files
committed
Updated tests
1 parent 79e360d commit b60ed0e

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/DataPlaneTokenSourceTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import java.net.URL;
1111
import java.time.LocalDateTime;
1212
import java.util.stream.Stream;
13+
import org.junit.jupiter.api.Test;
1314
import org.junit.jupiter.params.ParameterizedTest;
1415
import org.junit.jupiter.params.provider.Arguments;
1516
import org.junit.jupiter.params.provider.MethodSource;
17+
import org.mockito.MockedConstruction;
1618

1719
public class DataPlaneTokenSourceTest {
1820
private static final String TEST_ENDPOINT_1 = "https://endpoint1.databricks.com/";
@@ -197,4 +199,38 @@ void testDataPlaneTokenSource(
197199
assertTrue(token.isValid());
198200
}
199201
}
202+
203+
@Test
204+
void testEndpointTokenSourceConstructionCount() throws Exception {
205+
Token cpToken = new Token(TEST_CP_TOKEN, TEST_TOKEN_TYPE, null, LocalDateTime.now().plusSeconds(3600));
206+
DatabricksOAuthTokenSource mockCpTokenSource = mock(DatabricksOAuthTokenSource.class);
207+
when(mockCpTokenSource.getToken()).thenReturn(cpToken);
208+
209+
String successJson = "{\"access_token\":\"dp-access-token\",\"token_type\":\"Bearer\",\"refresh_token\":\"refresh-token\",\"expires_in\":3600}";
210+
HttpClient mockHttpClient = mock(HttpClient.class);
211+
when(mockHttpClient.execute(any())).thenReturn(new Response(successJson, 200, "OK", new URL(TEST_ENDPOINT_1)));
212+
213+
try (MockedConstruction<EndpointTokenSource> mockedConstruction = mockConstruction(EndpointTokenSource.class)) {
214+
DataPlaneTokenSource source = new DataPlaneTokenSource(mockHttpClient, mockCpTokenSource, TEST_HOST);
215+
216+
// First call - should create new EndpointTokenSource
217+
source.getToken(TEST_ENDPOINT_1, TEST_AUTH_DETAILS_1);
218+
assertEquals(1, mockedConstruction.constructed().size(), "First call should create one EndpointTokenSource");
219+
220+
// Second call with same endpoint and auth details - should reuse existing EndpointTokenSource
221+
source.getToken(TEST_ENDPOINT_1, TEST_AUTH_DETAILS_1);
222+
assertEquals(1, mockedConstruction.constructed().size(), "This call should reuse the existing EndpointTokenSource");
223+
224+
// Call with different endpoint - should create new EndpointTokenSource
225+
source.getToken(TEST_ENDPOINT_2, TEST_AUTH_DETAILS_2);
226+
assertEquals(2, mockedConstruction.constructed().size(), "Different endpoint should create new EndpointTokenSource");
227+
228+
// Call with different auth details - should create new EndpointTokenSource
229+
source.getToken(TEST_ENDPOINT_1, TEST_AUTH_DETAILS_2);
230+
assertEquals(3, mockedConstruction.constructed().size(), "Different auth details should create new EndpointTokenSource");
231+
232+
source.getToken(TEST_ENDPOINT_2, TEST_AUTH_DETAILS_2);
233+
assertEquals(3, mockedConstruction.constructed().size(), "This call should reuse the existing EndpointTokenSource");
234+
}
235+
}
200236
}

0 commit comments

Comments
 (0)