@@ -480,6 +480,105 @@ public void testGetHostMetadataRaisesOnHttpError() throws IOException {
480480 }
481481 }
482482
483+ // --- resolveHostMetadata tests ---
484+
485+ @ Test
486+ public void testResolveHostMetadataWorkspacePopulatesAllFields () throws IOException {
487+ String response =
488+ "{\" oidc_endpoint\" :\" https://ws.databricks.com/oidc\" ,"
489+ + "\" account_id\" :\" "
490+ + DUMMY_ACCOUNT_ID
491+ + "\" ,"
492+ + "\" workspace_id\" :\" "
493+ + DUMMY_WORKSPACE_ID
494+ + "\" }" ;
495+ try (FixtureServer server =
496+ new FixtureServer ().with ("GET" , "/.well-known/databricks-config" , response , 200 )) {
497+ DatabricksConfig config = new DatabricksConfig ().setHost (server .getUrl ());
498+ config .resolve (emptyEnv ());
499+ config .resolveHostMetadata ();
500+ assertEquals (DUMMY_ACCOUNT_ID , config .getAccountId ());
501+ assertEquals (DUMMY_WORKSPACE_ID , config .getWorkspaceId ());
502+ assertEquals ("https://ws.databricks.com/oidc" , config .getDiscoveryUrl ());
503+ }
504+ }
505+
506+ @ Test
507+ public void testResolveHostMetadataAccountSubstitutesAccountId () throws IOException {
508+ String response =
509+ "{\" oidc_endpoint\" :\" https://acc.databricks.com/oidc/accounts/{account_id}\" }" ;
510+ try (FixtureServer server =
511+ new FixtureServer ().with ("GET" , "/.well-known/databricks-config" , response , 200 )) {
512+ DatabricksConfig config =
513+ new DatabricksConfig ().setHost (server .getUrl ()).setAccountId (DUMMY_ACCOUNT_ID );
514+ config .resolve (emptyEnv ());
515+ config .resolveHostMetadata ();
516+ assertEquals (
517+ "https://acc.databricks.com/oidc/accounts/" + DUMMY_ACCOUNT_ID , config .getDiscoveryUrl ());
518+ }
519+ }
520+
521+ @ Test
522+ public void testResolveHostMetadataDoesNotOverwriteExistingFields () throws IOException {
523+ String existingAccountId = "existing-account-id" ;
524+ String existingWorkspaceId = "existing-workspace-id" ;
525+ String response =
526+ "{\" oidc_endpoint\" :\" https://ws.databricks.com/oidc\" ,"
527+ + "\" account_id\" :\" other-account\" ,"
528+ + "\" workspace_id\" :\" other-ws\" }" ;
529+ try (FixtureServer server =
530+ new FixtureServer ().with ("GET" , "/.well-known/databricks-config" , response , 200 )) {
531+ DatabricksConfig config =
532+ new DatabricksConfig ()
533+ .setHost (server .getUrl ())
534+ .setAccountId (existingAccountId )
535+ .setWorkspaceId (existingWorkspaceId );
536+ config .resolve (emptyEnv ());
537+ config .resolveHostMetadata ();
538+ assertEquals (existingAccountId , config .getAccountId ());
539+ assertEquals (existingWorkspaceId , config .getWorkspaceId ());
540+ }
541+ }
542+
543+ @ Test
544+ public void testResolveHostMetadataRaisesWhenAccountIdUnresolvable () throws IOException {
545+ String response =
546+ "{\" oidc_endpoint\" :\" https://acc.databricks.com/oidc/accounts/{account_id}\" }" ;
547+ try (FixtureServer server =
548+ new FixtureServer ().with ("GET" , "/.well-known/databricks-config" , response , 200 )) {
549+ DatabricksConfig config = new DatabricksConfig ().setHost (server .getUrl ());
550+ config .resolve (emptyEnv ());
551+ DatabricksException ex =
552+ assertThrows (DatabricksException .class , () -> config .resolveHostMetadata ());
553+ assertTrue (ex .getMessage ().contains ("account_id is not configured" ));
554+ }
555+ }
556+
557+ @ Test
558+ public void testResolveHostMetadataRaisesWhenOidcEndpointMissing () throws IOException {
559+ String response = "{\" account_id\" :\" " + DUMMY_ACCOUNT_ID + "\" }" ;
560+ try (FixtureServer server =
561+ new FixtureServer ().with ("GET" , "/.well-known/databricks-config" , response , 200 )) {
562+ DatabricksConfig config = new DatabricksConfig ().setHost (server .getUrl ());
563+ config .resolve (emptyEnv ());
564+ DatabricksException ex =
565+ assertThrows (DatabricksException .class , () -> config .resolveHostMetadata ());
566+ assertTrue (ex .getMessage ().contains ("discovery_url is not configured" ));
567+ }
568+ }
569+
570+ @ Test
571+ public void testResolveHostMetadataRaisesOnHttpError () throws IOException {
572+ try (FixtureServer server =
573+ new FixtureServer ().with ("GET" , "/.well-known/databricks-config" , "{}" , 500 )) {
574+ DatabricksConfig config = new DatabricksConfig ().setHost (server .getUrl ());
575+ config .resolve (emptyEnv ());
576+ DatabricksException ex =
577+ assertThrows (DatabricksException .class , () -> config .resolveHostMetadata ());
578+ assertTrue (ex .getMessage ().contains ("Failed to fetch host metadata" ));
579+ }
580+ }
581+
483582 // --- discoveryUrl / OIDC endpoint tests ---
484583
485584 @ Test
0 commit comments