|
10 | 10 | import java.net.URL; |
11 | 11 | import java.time.LocalDateTime; |
12 | 12 | import java.util.stream.Stream; |
| 13 | +import org.junit.jupiter.api.Test; |
13 | 14 | import org.junit.jupiter.params.ParameterizedTest; |
14 | 15 | import org.junit.jupiter.params.provider.Arguments; |
15 | 16 | import org.junit.jupiter.params.provider.MethodSource; |
| 17 | +import org.mockito.MockedConstruction; |
16 | 18 |
|
17 | 19 | public class DataPlaneTokenSourceTest { |
18 | 20 | private static final String TEST_ENDPOINT_1 = "https://endpoint1.databricks.com/"; |
@@ -197,4 +199,38 @@ void testDataPlaneTokenSource( |
197 | 199 | assertTrue(token.isValid()); |
198 | 200 | } |
199 | 201 | } |
| 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 | + } |
200 | 236 | } |
0 commit comments