diff --git a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java index 220a222fd..2de7e5dca 100644 --- a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java +++ b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java @@ -240,7 +240,7 @@ private void tryResolveHostMetadata() { } try { resolveHostMetadata(); - } catch (Exception e) { + } catch (Throwable e) { LOG.debug("Failed to resolve host metadata: {}", e.getMessage()); } } diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/ExternalBrowserCredentialsProviderTest.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/ExternalBrowserCredentialsProviderTest.java index 57c08d112..c467d1626 100644 --- a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/ExternalBrowserCredentialsProviderTest.java +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/ExternalBrowserCredentialsProviderTest.java @@ -34,8 +34,14 @@ void clientAndConsentTest() throws IOException { "{\"token_endpoint\": \"tokenEndPointFromServer\", \"authorization_endpoint\": \"authEndPointFromServer\"}", 200) .build(); + FixtureServer.FixtureMapping wellKnownFixture = + new FixtureServer.FixtureMapping.Builder() + .validateMethod("GET") + .validatePath("/.well-known/databricks-config") + .withResponse("{}", 404) + .build(); try (FixtureServer fixtures = new FixtureServer()) { - fixtures.with(fixture).with(fixture); + fixtures.with(wellKnownFixture).with(fixture).with(fixture); DatabricksConfig config = new DatabricksConfig() .setAuthType("external-browser") @@ -81,8 +87,14 @@ void clientAndConsentTestWithCustomRedirectUrl() throws IOException { "{\"token_endpoint\": \"tokenEndPointFromServer\", \"authorization_endpoint\": \"authEndPointFromServer\"}", 200) .build(); + FixtureServer.FixtureMapping wellKnownFixture = + new FixtureServer.FixtureMapping.Builder() + .validateMethod("GET") + .validatePath("/.well-known/databricks-config") + .withResponse("{}", 404) + .build(); try (FixtureServer fixtures = new FixtureServer()) { - fixtures.with(fixture).with(fixture); + fixtures.with(wellKnownFixture).with(fixture).with(fixture); DatabricksConfig config = new DatabricksConfig() .setAuthType("external-browser") @@ -126,9 +138,7 @@ void openIDConnectEndPointsTestAccounts() throws IOException { new DatabricksConfig() .setAuthType("external-browser") .setHost("https://accounts.cloud.databricks.com") - .setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()) .setAccountId("testAccountId"); - config.resolve(); String prefix = "https://accounts.cloud.databricks.com/oidc/accounts/" + config.getAccountId(); assertEquals(prefix + "/v1/token", config.getDatabricksOidcEndpoints().getTokenEndpoint()); diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/integration/HostMetadataIT.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/integration/HostMetadataIT.java new file mode 100644 index 000000000..5c6784531 --- /dev/null +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/integration/HostMetadataIT.java @@ -0,0 +1,41 @@ +package com.databricks.sdk.integration; + +import static org.junit.jupiter.api.Assertions.*; + +import com.databricks.sdk.core.DatabricksConfig; +import com.databricks.sdk.integration.framework.EnvContext; +import com.databricks.sdk.integration.framework.EnvOrSkip; +import com.databricks.sdk.integration.framework.EnvTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Integration test for host metadata resolution via /.well-known/databricks-config. Port of Go SDK + * #1546. + */ +@ExtendWith(EnvTest.class) +@EnvContext("ucws") +public class HostMetadataIT { + private static final Logger LOG = LoggerFactory.getLogger(HostMetadataIT.class); + + @Test + void testResolvePopulatesFieldsFromMetadata(@EnvOrSkip("DATABRICKS_HOST") String host) { + DatabricksConfig config = new DatabricksConfig().setHost(host); + config.resolve(); + + LOG.info( + "Resolved metadata for {}: accountId={}, workspaceId={}, discoveryUrl={}", + host, + config.getAccountId(), + config.getWorkspaceId(), + config.getDiscoveryUrl()); + + // After resolve(), host metadata should have been resolved + assertNotNull(config.getDiscoveryUrl(), "Expected discoveryUrl to be populated after resolve"); + assertFalse(config.getDiscoveryUrl().isEmpty(), "Expected non-empty discoveryUrl"); + assertNotNull(config.getAccountId(), "Expected accountId to be populated after resolve"); + assertFalse(config.getAccountId().isEmpty(), "Expected non-empty accountId"); + } +} diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/service/gentesting/unittests/IdempotencyTestingAPITest.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/service/gentesting/unittests/IdempotencyTestingAPITest.java index a9a9b6faa..64d8a44db 100755 --- a/databricks-sdk-java/src/test/java/com/databricks/sdk/service/gentesting/unittests/IdempotencyTestingAPITest.java +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/service/gentesting/unittests/IdempotencyTestingAPITest.java @@ -192,13 +192,20 @@ void testIdempotencyAutoRequestID(AutoRequestIDTestCase testCase) throws Excepti TestResource result = api.createTestResource(testCase.apiRequest); assertEquals(testCase.wantResult, result, "Test case: " + testCase.name); - // Capture and verify request IDs + // Capture and verify request IDs (atLeast(2) accounts for metadata resolution call) ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(Request.class); - verify(spyClient, times(2)).execute(requestCaptor.capture()); + verify(spyClient, atLeast(2)).execute(requestCaptor.capture()); + // Filter to only the API requests (skip metadata resolution calls) List capturedRequests = requestCaptor.getAllValues(); - String firstRequestId = capturedRequests.get(0).getQuery().get("request_id").get(0); - String secondRequestId = capturedRequests.get(1).getQuery().get("request_id").get(0); + List apiRequests = new java.util.ArrayList<>(); + for (Request req : capturedRequests) { + if (!req.getUrl().contains(".well-known/databricks-config")) { + apiRequests.add(req); + } + } + String firstRequestId = apiRequests.get(0).getQuery().get("request_id").get(0); + String secondRequestId = apiRequests.get(1).getQuery().get("request_id").get(0); assertNotNull(firstRequestId, "Auto-generated request_id should not be null"); assertFalse(firstRequestId.isEmpty(), "Auto-generated request_id should not be empty");