Skip to content

Commit 6ab7b3d

Browse files
committed
Add unit tests for X-Databricks-Workspace-Id header gating
Mirrors the Go SDK's `workspace_functions_test.go` coverage: * `AppsImplTest.testWorkspaceIdHeaderSentWhenConfigured` — a workspace-scoped service forwards the configured workspace ID on the `X-Databricks-Workspace-Id` request header. * `AppsImplTest.testWorkspaceIdHeaderOmittedWhenUnset` — when no workspace ID is configured, the header is not sent (so calls to legacy single-workspace hosts don't pick up an empty value). * `BudgetsImplTest.testWorkspaceIdHeaderNotSentForAccountScopedService` — an account-scoped service never forwards the header, even if a workspace ID is configured. Locks in the generator gate (`if not .Service.IsAccounts`). Signed-off-by: Divyansh Vijayvergia <divyansh.vijayvergia@databricks.com>
1 parent 328e300 commit 6ab7b3d

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

databricks-sdk-java/src/test/java/com/databricks/sdk/service/apps/AppsImplTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,42 @@ public void testCreateAppIncludesNoComputeParameter() throws IOException {
4141
}),
4242
eq(App.class));
4343
}
44+
45+
// On unified ("SPOG") hosts, workspace-scoped requests must carry an
46+
// X-Databricks-Workspace-Id header so the gateway can route them to the
47+
// correct workspace. When ApiClient#workspaceId() returns a value, every
48+
// generated *Impl must forward it on the request; when it returns null,
49+
// the header must be omitted (so calls to legacy single-workspace hosts
50+
// don't pick up an empty value).
51+
@Test
52+
public void testWorkspaceIdHeaderSentWhenConfigured() throws IOException {
53+
ApiClient apiClient = Mockito.mock(ApiClient.class);
54+
when(apiClient.workspaceId()).thenReturn("7474644166319138");
55+
when(apiClient.execute(any(), any())).thenReturn(null);
56+
57+
AppsService apps = new AppsImpl(apiClient);
58+
apps.list(new ListAppsRequest());
59+
60+
verify(apiClient)
61+
.execute(
62+
argThat(
63+
(Request req) ->
64+
"7474644166319138".equals(req.getHeaders().get("X-Databricks-Workspace-Id"))),
65+
any());
66+
}
67+
68+
@Test
69+
public void testWorkspaceIdHeaderOmittedWhenUnset() throws IOException {
70+
ApiClient apiClient = Mockito.mock(ApiClient.class);
71+
when(apiClient.workspaceId()).thenReturn(null);
72+
when(apiClient.execute(any(), any())).thenReturn(null);
73+
74+
AppsService apps = new AppsImpl(apiClient);
75+
apps.list(new ListAppsRequest());
76+
77+
verify(apiClient)
78+
.execute(
79+
argThat((Request req) -> !req.getHeaders().containsKey("X-Databricks-Workspace-Id")),
80+
any());
81+
}
4482
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.databricks.sdk.service.billing;
2+
3+
import static org.mockito.ArgumentMatchers.*;
4+
import static org.mockito.Mockito.verify;
5+
import static org.mockito.Mockito.when;
6+
7+
import com.databricks.sdk.core.ApiClient;
8+
import com.databricks.sdk.core.http.Request;
9+
import java.io.IOException;
10+
import org.junit.jupiter.api.Test;
11+
import org.mockito.Mockito;
12+
13+
public class BudgetsImplTest {
14+
// Account-scoped services must not forward the workspace routing header even
15+
// when ApiClient#workspaceId() happens to be set. This locks in the generator
16+
// gate (`if not .Service.IsAccounts`) so a regression that drops it would be
17+
// caught here.
18+
@Test
19+
public void testWorkspaceIdHeaderNotSentForAccountScopedService() throws IOException {
20+
ApiClient apiClient = Mockito.mock(ApiClient.class);
21+
when(apiClient.workspaceId()).thenReturn("7474644166319138");
22+
when(apiClient.configuredAccountID()).thenReturn("account-abc");
23+
when(apiClient.execute(any(), any())).thenReturn(null);
24+
25+
BudgetsService budgets = new BudgetsImpl(apiClient);
26+
budgets.list(new ListBudgetConfigurationsRequest());
27+
28+
verify(apiClient)
29+
.execute(
30+
argThat((Request req) -> !req.getHeaders().containsKey("X-Databricks-Workspace-Id")),
31+
any());
32+
}
33+
}

0 commit comments

Comments
 (0)