diff --git a/source/ProcessManager.Components.Tests/Unit/Extensions/DependencyInjection/DatabricksWorkspaceExtensionsTests.cs b/source/ProcessManager.Components.Tests/Unit/Extensions/DependencyInjection/DatabricksWorkspaceExtensionsTests.cs index e51297ea..8b8da8b6 100644 --- a/source/ProcessManager.Components.Tests/Unit/Extensions/DependencyInjection/DatabricksWorkspaceExtensionsTests.cs +++ b/source/ProcessManager.Components.Tests/Unit/Extensions/DependencyInjection/DatabricksWorkspaceExtensionsTests.cs @@ -13,6 +13,8 @@ // limitations under the License. using FluentAssertions; +using FluentAssertions.Common; +using FluentAssertions.Execution; using Microsoft.Azure.Databricks.Client; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -76,6 +78,38 @@ public void AddDatabricksJobs_WhenCustomSectionName_RegistrationsArePerformedUsi actualJobsClient.Should().NotBeNull(); } + [Fact] + public void AddDatabricksJobs_WhenConfiguringMultipleWorkspaces_CanUseFromKeyedServicesToInjectClients() + { + // Arrange + var wholesaleSectionName = "Wholesale"; + var measurementsSectionName = "Measurements"; + AddInMemoryConfigurations(new Dictionary() + { + [$"{wholesaleSectionName}:{nameof(DatabricksWorkspaceOptions.BaseUrl)}"] = "https://www.wholesale.com", + [$"{wholesaleSectionName}:{nameof(DatabricksWorkspaceOptions.Token)}"] = TokenFake, + [$"{measurementsSectionName}:{nameof(DatabricksWorkspaceOptions.BaseUrl)}"] = "https://www.measurements.com", + [$"{measurementsSectionName}:{nameof(DatabricksWorkspaceOptions.Token)}"] = TokenFake, + }); + + Services.AddDatabricksJobs(configSectionPath: wholesaleSectionName); + Services.AddDatabricksJobs(configSectionPath: measurementsSectionName); + + // Act + Services.AddTransient(); + Services.AddTransient(); + + // Assert + var assertionScope = new AssertionScope(); + var serviceProvider = Services.BuildServiceProvider(); + + var actualWholesaleClient = serviceProvider.GetRequiredService(); + actualWholesaleClient.Client.Should().NotBeNull(); + + var actualMeasurementsClient = serviceProvider.GetRequiredService(); + actualMeasurementsClient.Client.Should().NotBeNull(); + } + private void AddInMemoryConfigurations(Dictionary configurations) { Services.AddScoped(_ => @@ -85,4 +119,30 @@ private void AddInMemoryConfigurations(Dictionary configuration .Build(); }); } + + public class WholesaleClientStub + { + /// + /// The "key" must match the configuration section name give during registration. + /// + public WholesaleClientStub([FromKeyedServices("Wholesale")] IDatabricksJobsClient client) + { + Client = client; + } + + public IDatabricksJobsClient Client { get; } + } + + public class MeasurementsClientStub + { + /// + /// The "key" must match the configuration section name give during registration. + /// + public MeasurementsClientStub([FromKeyedServices("Measurements")] IDatabricksJobsClient client) + { + Client = client; + } + + public IDatabricksJobsClient Client { get; } + } }