diff --git a/Masa.Framework.sln b/Masa.Framework.sln index 49a91b091..b9c36795c 100644 --- a/Masa.Framework.sln +++ b/Masa.Framework.sln @@ -637,6 +637,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Models ", "Models ", "{6FB6 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Utils.Models.Config", "src\Utils\Models\Masa.Utils.Models.Config\Masa.Utils.Models.Config.csproj", "{29125077-70C0-456A-BACD-E614FD71B16E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Service.Caller.Configuration.Tests", "src\Contrib\Service\Caller\Tests\Scenes\Masa.Contrib.Service.Caller.Configuration.Tests\Masa.Contrib.Service.Caller.Configuration.Tests.csproj", "{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -2261,6 +2263,14 @@ Global {29125077-70C0-456A-BACD-E614FD71B16E}.Release|Any CPU.Build.0 = Release|Any CPU {29125077-70C0-456A-BACD-E614FD71B16E}.Release|x64.ActiveCfg = Release|Any CPU {29125077-70C0-456A-BACD-E614FD71B16E}.Release|x64.Build.0 = Release|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Debug|x64.ActiveCfg = Debug|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Debug|x64.Build.0 = Debug|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|Any CPU.Build.0 = Release|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|x64.ActiveCfg = Release|Any CPU + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -2575,6 +2585,7 @@ Global {AE12559A-5C8F-4590-9B18-B935155B15C5} = {F83E3E53-2DFE-4B1F-B988-204CA4A42572} {6FB69510-B616-4F6D-82CC-36AD52CEF4D3} = {5944A182-13B8-4DA6-AEE2-0A01E64A9648} {29125077-70C0-456A-BACD-E614FD71B16E} = {6FB69510-B616-4F6D-82CC-36AD52CEF4D3} + {BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1} = {E8681596-D4BF-484A-A428-06D749FD4C5D} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03} diff --git a/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.DaprClient/DefaultCallerProvider.cs b/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.DaprClient/DefaultCallerProvider.cs index 479a075bc..fbe3e71de 100644 --- a/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.DaprClient/DefaultCallerProvider.cs +++ b/src/Contrib/Service/Caller/Masa.Contrib.Service.Caller.DaprClient/DefaultCallerProvider.cs @@ -7,13 +7,15 @@ public class DefaultCallerProvider : ICallerProvider { private readonly IOptionsMonitor<DaprOptions> _daprOptions; private readonly IConfiguration? _configuration; + private readonly IMasaConfiguration? _masaConfiguration; public DefaultCallerProvider(IOptionsMonitor<DaprOptions> daprOptions, IConfiguration? configuration = null, IMasaConfiguration? masaConfiguration = null) { _daprOptions = daprOptions; - _configuration = masaConfiguration?.Local ?? configuration; + _configuration = configuration; + _masaConfiguration = masaConfiguration; } public string CompletionAppId(string appId) @@ -21,7 +23,11 @@ public string CompletionAppId(string appId) var daprOptions = _daprOptions.CurrentValue; if (daprOptions.AppPort > 0 && daprOptions.IsIncompleteAppId()) appId = $"{appId}{daprOptions.AppIdDelimiter}{daprOptions.AppIdSuffix ?? NetworkUtils.GetPhysicalAddress()}"; + var value = _configuration?.GetSection(appId).Value; + if (value.IsNullOrWhiteSpace()) + value = _masaConfiguration?.Local.GetSection(appId).Value; + if (value.IsNullOrWhiteSpace()) return appId; return value; diff --git a/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/DefaultCallerProviderTest.cs b/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/DefaultCallerProviderTest.cs index 8955518d5..a639bbda9 100644 --- a/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/DefaultCallerProviderTest.cs +++ b/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/DefaultCallerProviderTest.cs @@ -1,8 +1,6 @@ // Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. -using Microsoft.Extensions.Configuration; - namespace Masa.Contrib.Service.Caller.Tests; [TestClass] @@ -58,6 +56,8 @@ public void TestCompletionAppId3() masaConfiguration.Setup(configuration => configuration.Local.GetSection($"{appId}-{NetworkUtils.GetPhysicalAddress()}").Value) .Returns(() => expectedAppId); Mock<IConfiguration> configuration = new(); + configuration.Setup(c => c.GetSection($"{appId}-{NetworkUtils.GetPhysicalAddress()}").Value) + .Returns(() => expectedAppId); var serviceProvider = services.BuildServiceProvider(); var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>(); @@ -83,6 +83,8 @@ public void TestCompletionAppId4() masaConfiguration.Setup(configuration => configuration.Local.GetSection($"{appId}-suffix").Value) .Returns(() => expectedAppId); Mock<IConfiguration> configuration = new(); + configuration.Setup(c => c.GetSection($"{appId}-suffix").Value) + .Returns(() => expectedAppId); var serviceProvider = services.BuildServiceProvider(); var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>(); @@ -114,4 +116,26 @@ public void TestCompletionAppId5() string actualAppId = callerProvider.CompletionAppId(appId); Assert.AreEqual(expectedAppId, actualAppId); } + + [TestMethod] + public void TestCompletionAppId6() + { + string appId = "appid"; + string expectedAppId = $"{appId}-{Guid.NewGuid()}"; + Environment.SetEnvironmentVariable($"{appId}-suffix", expectedAppId); + var builder = WebApplication.CreateBuilder(); + builder.Services.Configure<DaprOptions>(options => + { + options.AppPort = 5000; + options.AppIdSuffix = "suffix"; + }); + + var serviceProvider = builder.Services.BuildServiceProvider(); + + var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>(); + + var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration); + string actualAppId = callerProvider.CompletionAppId(appId); + Assert.AreEqual(expectedAppId, actualAppId); + } } diff --git a/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/_Imports.cs b/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/_Imports.cs index 0709e12b5..4a690947b 100644 --- a/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/_Imports.cs +++ b/src/Contrib/Service/Caller/Tests/Masa.Contrib.Service.Caller.Tests/_Imports.cs @@ -11,6 +11,7 @@ global using Masa.Contrib.Service.Caller.Tests.Requesties; global using Masa.Contrib.Service.Caller.Tests.Response; global using Microsoft.AspNetCore.Builder; +global using Microsoft.Extensions.Configuration; global using Microsoft.Extensions.DependencyInjection; global using Microsoft.Extensions.Logging; global using Microsoft.Extensions.Options; diff --git a/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/DefaultCallerProviderTest.cs b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/DefaultCallerProviderTest.cs new file mode 100644 index 000000000..8bda88da2 --- /dev/null +++ b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/DefaultCallerProviderTest.cs @@ -0,0 +1,106 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +namespace Masa.Contrib.Service.Caller.Configuration.Tests; + +[TestClass] +public class DefaultCallerProviderTest +{ + private readonly string _appid = "appid"; + + [TestInitialize] + public void InitializeData() + { + Environment.SetEnvironmentVariable($"{_appid}-suffix", ""); + } + + [TestMethod] + public void TestCompletionAppId() + { + string expectedAppId = "expected-appid"; + var builder = WebApplication.CreateBuilder(); + builder.Configuration.AddJsonFile("customAppsettings.json"); + builder.Services.Configure<DaprOptions>(options => + { + options.AppPort = 5000; + options.AppIdSuffix = "suffix"; + }); + + var serviceProvider = builder.Services.BuildServiceProvider(); + + var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>(); + + var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration); + string actualAppId = callerProvider.CompletionAppId(_appid); + Assert.AreEqual(expectedAppId, actualAppId); + } + + [TestMethod] + public void TestCompletionAppId2() + { + string expectedAppId = $"{_appid}-{Guid.NewGuid()}"; + + Environment.SetEnvironmentVariable($"{_appid}-suffix", expectedAppId); + var builder = WebApplication.CreateBuilder(); + + //If the newly added data source exists, the environment variable data source will no longer be queried + builder.Configuration.AddJsonFile("customAppsettings.json"); + builder.Services.Configure<DaprOptions>(options => + { + options.AppPort = 5000; + options.AppIdSuffix = "suffix"; + }); + + var serviceProvider = builder.Services.BuildServiceProvider(); + + var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>(); + + var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration); + string actualAppId = callerProvider.CompletionAppId(_appid); + Assert.AreEqual("expected-appid", actualAppId); + } + + [TestMethod] + public void TestCompletionAppId3() + { + string expectedAppId = "expected-appid"; + var builder = WebApplication.CreateBuilder(); + builder.Services.AddMasaConfiguration(options => options.AddJsonFile("customAppsettings.json")); + builder.Services.Configure<DaprOptions>(options => + { + options.AppPort = 5000; + options.AppIdSuffix = "suffix"; + }); + + var serviceProvider = builder.Services.BuildServiceProvider(); + + var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>(); + + var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration, builder.Services.GetMasaConfiguration()); + string actualAppId = callerProvider.CompletionAppId(_appid); + Assert.AreEqual(expectedAppId, actualAppId); + } + + [TestMethod] + public void TestCompletionAppId4() + { + string expectedAppId = $"{_appid}-{Guid.NewGuid()}"; + + Environment.SetEnvironmentVariable($"{_appid}-suffix", expectedAppId); + var builder = WebApplication.CreateBuilder(); + builder.Services.AddMasaConfiguration(options => options.AddJsonFile("customAppsettings.json")); + builder.Services.Configure<DaprOptions>(options => + { + options.AppPort = 5000; + options.AppIdSuffix = "suffix"; + }); + + var serviceProvider = builder.Services.BuildServiceProvider(); + + var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>(); + + var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration, builder.Services.GetMasaConfiguration()); + string actualAppId = callerProvider.CompletionAppId(_appid); + Assert.AreEqual(expectedAppId, actualAppId); + } +} diff --git a/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/Masa.Contrib.Service.Caller.Configuration.Tests.csproj b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/Masa.Contrib.Service.Caller.Configuration.Tests.csproj new file mode 100644 index 000000000..8d0d7f40f --- /dev/null +++ b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/Masa.Contrib.Service.Caller.Configuration.Tests.csproj @@ -0,0 +1,37 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + <IsPackable>false</IsPackable> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftTeskSdkPackageVersion)" /> + <PackageReference Include="MSTest.TestAdapter" Version="$(MSTestPackageVersion)" /> + <PackageReference Include="MSTest.TestFramework" Version="$(MSTestPackageVersion)" /> + <PackageReference Include="coverlet.collector" Version="$(CoverletPackageVersion)"> + <PrivateAssets>all</PrivateAssets> + <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> + </PackageReference> + <PackageReference Include="coverlet.msbuild" Version="$(CoverletPackageVersion)"> + <PrivateAssets>all</PrivateAssets> + <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> + </PackageReference> + <PackageReference Include="Moq" Version="$(MoqPackageVersion)" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\..\..\..\..\Configuration\Masa.Contrib.Configuration\Masa.Contrib.Configuration.csproj" /> + <ProjectReference Include="..\..\..\Masa.Contrib.Service.Caller.DaprClient\Masa.Contrib.Service.Caller.DaprClient.csproj" /> + </ItemGroup> + + <ItemGroup> + <None Remove="customAppsettings.json" /> + <Content Include="customAppsettings.json"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </Content> + </ItemGroup> + +</Project> diff --git a/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/_Imports.cs b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/_Imports.cs new file mode 100644 index 000000000..6e6088bba --- /dev/null +++ b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/_Imports.cs @@ -0,0 +1,25 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +global using Masa.BuildingBlocks.Configuration; +global using Masa.BuildingBlocks.Development.DaprStarter; +global using Masa.BuildingBlocks.Service.Caller; +global using Masa.BuildingBlocks.Service.Caller.Options; +global using Masa.Contrib.Service.Caller.DaprClient; +global using Microsoft.AspNetCore.Builder; +global using Microsoft.Extensions.Configuration; +global using Microsoft.Extensions.DependencyInjection; +global using Microsoft.Extensions.Logging; +global using Microsoft.Extensions.Options; +global using Microsoft.VisualStudio.TestTools.UnitTesting; +global using Moq; +global using Moq.Protected; +global using System.Net; +global using System.Net.Http.Json; +global using System.Net.NetworkInformation; +global using System.Reflection; +global using System.Runtime.ExceptionServices; +global using System.Text; +global using System.Text.Json; +global using System.Text.Json.Serialization; +global using System.Xml.Serialization; diff --git a/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/customAppsettings.json b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/customAppsettings.json new file mode 100644 index 000000000..b5eb3a6b7 --- /dev/null +++ b/src/Contrib/Service/Caller/Tests/Scenes/Masa.Contrib.Service.Caller.Configuration.Tests/customAppsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + }, + "appid-suffix": "expected-appid" +} \ No newline at end of file