|
| 1 | +package com.databricks.sdk.core.utils; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import com.databricks.sdk.core.DatabricksConfig; |
| 6 | +import com.databricks.sdk.core.DatabricksException; |
| 7 | +import com.databricks.sdk.core.FixtureServer; |
| 8 | +import com.databricks.sdk.core.commons.CommonsHttpClient; |
| 9 | +import java.io.IOException; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +public class AzureUtilsTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + public void testInferTenantId404() throws IOException { |
| 16 | + try (FixtureServer server = new FixtureServer().with("GET", "/aad/auth", "", 404)) { |
| 17 | + DatabricksConfig config = new DatabricksConfig(); |
| 18 | + config.setHost(server.getUrl()); |
| 19 | + config.setAzureWorkspaceResourceId( |
| 20 | + "/subscriptions/123/resourceGroups/rg/providers/Microsoft.Databricks/workspaces/ws"); |
| 21 | + config.setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()); |
| 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 | + |
| 40 | + assertNull(config.getAzureTenantId()); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testInferTenantIdNoLocationHeader() throws IOException { |
| 46 | + try (FixtureServer server = new FixtureServer().with("GET", "/aad/auth", "", 302)) { |
| 47 | + DatabricksConfig config = new DatabricksConfig(); |
| 48 | + config.setHost(server.getUrl()); |
| 49 | + config.setAzureWorkspaceResourceId( |
| 50 | + "/subscriptions/123/resourceGroups/rg/providers/Microsoft.Databricks/workspaces/ws"); |
| 51 | + config.setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()); |
| 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 | + |
| 70 | + assertNull(config.getAzureTenantId()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testInferTenantIdUnparsableLocationHeader() throws IOException { |
| 76 | + FixtureServer.FixtureMapping fixture = |
| 77 | + new FixtureServer.FixtureMapping.Builder() |
| 78 | + .validateMethod("GET") |
| 79 | + .validatePath("/aad/auth") |
| 80 | + .withRedirect("https://unexpected-location", 302) |
| 81 | + .build(); |
| 82 | + |
| 83 | + try (FixtureServer server = new FixtureServer().with(fixture)) { |
| 84 | + DatabricksConfig config = new DatabricksConfig(); |
| 85 | + config.setHost(server.getUrl()); |
| 86 | + config.setAzureWorkspaceResourceId( |
| 87 | + "/subscriptions/123/resourceGroups/rg/providers/Microsoft.Databricks/workspaces/ws"); |
| 88 | + config.setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()); |
| 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 | + |
| 106 | + assertNull(config.getAzureTenantId()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testInferTenantIdHappyPath() throws IOException { |
| 112 | + FixtureServer.FixtureMapping fixture = |
| 113 | + new FixtureServer.FixtureMapping.Builder() |
| 114 | + .validateMethod("GET") |
| 115 | + .validatePath("/aad/auth") |
| 116 | + .withRedirect("https://login.microsoftonline.com/test-tenant-id/oauth2/authorize", 302) |
| 117 | + .build(); |
| 118 | + |
| 119 | + try (FixtureServer server = new FixtureServer().with(fixture)) { |
| 120 | + DatabricksConfig config = new DatabricksConfig(); |
| 121 | + config.setHost(server.getUrl()); |
| 122 | + config.setAzureWorkspaceResourceId( |
| 123 | + "/subscriptions/123/resourceGroups/rg/providers/Microsoft.Databricks/workspaces/ws"); |
| 124 | + config.setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()); |
| 125 | + String result = AzureUtils.inferTenantId(config); |
| 126 | + assertEquals("test-tenant-id", result); |
| 127 | + assertNull(config.getAzureTenantId()); // Config should remain unchanged |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testInferTenantIdSkipsWhenNotAzure() { |
| 133 | + DatabricksConfig config = new DatabricksConfig(); |
| 134 | + config.setHost("https://my-workspace.cloud.databricks.com"); // non-azure host |
| 135 | + config.setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()); |
| 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()); |
| 144 | + assertNull(config.getAzureTenantId()); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void testInferTenantIdSkipsWhenAlreadySet() { |
| 149 | + DatabricksConfig config = new DatabricksConfig(); |
| 150 | + config.setHost("https://adb-123.0.azuredatabricks.net"); |
| 151 | + config.setAzureTenantId("existing-tenant-id"); |
| 152 | + config.setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()); |
| 153 | + String result = AzureUtils.inferTenantId(config); |
| 154 | + assertEquals("existing-tenant-id", result); |
| 155 | + assertEquals("existing-tenant-id", config.getAzureTenantId()); // Config should remain unchanged |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testInferTenantIdSkipsWhenNoHost() { |
| 160 | + DatabricksConfig config = new DatabricksConfig(); |
| 161 | + config.setHttpClient(new CommonsHttpClient.Builder().withTimeoutSeconds(30).build()); |
| 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()); |
| 170 | + assertNull(config.getAzureTenantId()); |
| 171 | + } |
| 172 | +} |
0 commit comments