33import static org .junit .jupiter .api .Assertions .*;
44
55import com .databricks .sdk .core .DatabricksConfig ;
6+ import com .databricks .sdk .core .DatabricksException ;
67import com .databricks .sdk .core .FixtureServer ;
78import com .databricks .sdk .core .commons .CommonsHttpClient ;
89import java .io .IOException ;
@@ -15,9 +16,27 @@ public void testInferTenantId404() throws IOException {
1516 try (FixtureServer server = new FixtureServer ().with ("GET" , "/aad/auth" , "" , 404 )) {
1617 DatabricksConfig config = new DatabricksConfig ();
1718 config .setHost (server .getUrl ());
19+ config .setAzureWorkspaceResourceId (
20+ "/subscriptions/123/resourceGroups/rg/providers/Microsoft.Databricks/workspaces/ws" );
1821 config .setHttpClient (new CommonsHttpClient .Builder ().withTimeoutSeconds (30 ).build ());
19- String result = AzureUtils .inferTenantId (config );
20- assertNull (result );
22+
23+ DatabricksException exception =
24+ assertThrows (
25+ DatabricksException .class ,
26+ () -> {
27+ AzureUtils .inferTenantId (config );
28+ });
29+ assertEquals (
30+ "Failed to infer Azure tenant ID from " + server .getUrl () + "/aad/auth" ,
31+ exception .getMessage ());
32+
33+ assertNotNull (exception .getCause ());
34+ assertInstanceOf (DatabricksException .class , exception .getCause ());
35+ DatabricksException cause = (DatabricksException ) exception .getCause ();
36+ assertEquals (
37+ "Expected redirect (302) from " + server .getUrl () + "/aad/auth, got status code: 404" ,
38+ cause .getMessage ());
39+
2140 assertNull (config .getAzureTenantId ());
2241 }
2342 }
@@ -27,9 +46,27 @@ public void testInferTenantIdNoLocationHeader() throws IOException {
2746 try (FixtureServer server = new FixtureServer ().with ("GET" , "/aad/auth" , "" , 302 )) {
2847 DatabricksConfig config = new DatabricksConfig ();
2948 config .setHost (server .getUrl ());
49+ config .setAzureWorkspaceResourceId (
50+ "/subscriptions/123/resourceGroups/rg/providers/Microsoft.Databricks/workspaces/ws" );
3051 config .setHttpClient (new CommonsHttpClient .Builder ().withTimeoutSeconds (30 ).build ());
31- String result = AzureUtils .inferTenantId (config );
32- assertNull (result );
52+
53+ DatabricksException exception =
54+ assertThrows (
55+ DatabricksException .class ,
56+ () -> {
57+ AzureUtils .inferTenantId (config );
58+ });
59+ assertEquals (
60+ "Failed to infer Azure tenant ID from " + server .getUrl () + "/aad/auth" ,
61+ exception .getMessage ());
62+
63+ assertNotNull (exception .getCause ());
64+ assertInstanceOf (DatabricksException .class , exception .getCause ());
65+ DatabricksException cause = (DatabricksException ) exception .getCause ();
66+ assertEquals (
67+ "No Location header in redirect response from " + server .getUrl () + "/aad/auth" ,
68+ cause .getMessage ());
69+
3370 assertNull (config .getAzureTenantId ());
3471 }
3572 }
@@ -46,9 +83,26 @@ public void testInferTenantIdUnparsableLocationHeader() throws IOException {
4683 try (FixtureServer server = new FixtureServer ().with (fixture )) {
4784 DatabricksConfig config = new DatabricksConfig ();
4885 config .setHost (server .getUrl ());
86+ config .setAzureWorkspaceResourceId (
87+ "/subscriptions/123/resourceGroups/rg/providers/Microsoft.Databricks/workspaces/ws" );
4988 config .setHttpClient (new CommonsHttpClient .Builder ().withTimeoutSeconds (30 ).build ());
50- String result = AzureUtils .inferTenantId (config );
51- assertNull (result );
89+
90+ DatabricksException exception =
91+ assertThrows (
92+ DatabricksException .class ,
93+ () -> {
94+ AzureUtils .inferTenantId (config );
95+ });
96+ assertEquals (
97+ "Failed to infer Azure tenant ID from " + server .getUrl () + "/aad/auth" ,
98+ exception .getMessage ());
99+
100+ assertNotNull (exception .getCause ());
101+ assertInstanceOf (DatabricksException .class , exception .getCause ());
102+ DatabricksException cause = (DatabricksException ) exception .getCause ();
103+ assertEquals (
104+ "Failed to parse tenant ID from URL https://unexpected-location" , cause .getMessage ());
105+
52106 assertNull (config .getAzureTenantId ());
53107 }
54108 }
@@ -79,8 +133,14 @@ public void testInferTenantIdSkipsWhenNotAzure() {
79133 DatabricksConfig config = new DatabricksConfig ();
80134 config .setHost ("https://my-workspace.cloud.databricks.com" ); // non-azure host
81135 config .setHttpClient (new CommonsHttpClient .Builder ().withTimeoutSeconds (30 ).build ());
82- String result = AzureUtils .inferTenantId (config );
83- assertNull (result );
136+
137+ DatabricksException exception =
138+ assertThrows (
139+ DatabricksException .class ,
140+ () -> {
141+ AzureUtils .inferTenantId (config );
142+ });
143+ assertEquals ("Cannot infer tenant ID: workspace is not Azure" , exception .getMessage ());
84144 assertNull (config .getAzureTenantId ());
85145 }
86146
@@ -99,8 +159,14 @@ public void testInferTenantIdSkipsWhenAlreadySet() {
99159 public void testInferTenantIdSkipsWhenNoHost () {
100160 DatabricksConfig config = new DatabricksConfig ();
101161 config .setHttpClient (new CommonsHttpClient .Builder ().withTimeoutSeconds (30 ).build ());
102- String result = AzureUtils .inferTenantId (config );
103- assertNull (result );
162+
163+ DatabricksException exception =
164+ assertThrows (
165+ DatabricksException .class ,
166+ () -> {
167+ AzureUtils .inferTenantId (config );
168+ });
169+ assertEquals ("Cannot infer tenant ID: host is missing" , exception .getMessage ());
104170 assertNull (config .getAzureTenantId ());
105171 }
106172}
0 commit comments