|
| 1 | +// Copyright (c) MASA Stack All rights reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. |
| 3 | + |
| 4 | +namespace Masa.Contrib.Service.Caller.Configuration.Tests; |
| 5 | + |
| 6 | +[TestClass] |
| 7 | +public class DefaultCallerProviderTest |
| 8 | +{ |
| 9 | + private readonly string _appid = "appid"; |
| 10 | + |
| 11 | + [TestInitialize] |
| 12 | + public void InitializeData() |
| 13 | + { |
| 14 | + Environment.SetEnvironmentVariable($"{_appid}-suffix", ""); |
| 15 | + } |
| 16 | + |
| 17 | + [TestMethod] |
| 18 | + public void TestCompletionAppId() |
| 19 | + { |
| 20 | + string expectedAppId = "expected-appid"; |
| 21 | + var builder = WebApplication.CreateBuilder(); |
| 22 | + builder.Configuration.AddJsonFile("customAppsettings.json"); |
| 23 | + builder.Services.Configure<DaprOptions>(options => |
| 24 | + { |
| 25 | + options.AppPort = 5000; |
| 26 | + options.AppIdSuffix = "suffix"; |
| 27 | + }); |
| 28 | + |
| 29 | + var serviceProvider = builder.Services.BuildServiceProvider(); |
| 30 | + |
| 31 | + var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>(); |
| 32 | + |
| 33 | + var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration); |
| 34 | + string actualAppId = callerProvider.CompletionAppId(_appid); |
| 35 | + Assert.AreEqual(expectedAppId, actualAppId); |
| 36 | + } |
| 37 | + |
| 38 | + [TestMethod] |
| 39 | + public void TestCompletionAppId2() |
| 40 | + { |
| 41 | + string expectedAppId = $"{_appid}-{Guid.NewGuid()}"; |
| 42 | + |
| 43 | + Environment.SetEnvironmentVariable($"{_appid}-suffix", expectedAppId); |
| 44 | + var builder = WebApplication.CreateBuilder(); |
| 45 | + |
| 46 | + //If the newly added data source exists, the environment variable data source will no longer be queried |
| 47 | + builder.Configuration.AddJsonFile("customAppsettings.json"); |
| 48 | + builder.Services.Configure<DaprOptions>(options => |
| 49 | + { |
| 50 | + options.AppPort = 5000; |
| 51 | + options.AppIdSuffix = "suffix"; |
| 52 | + }); |
| 53 | + |
| 54 | + var serviceProvider = builder.Services.BuildServiceProvider(); |
| 55 | + |
| 56 | + var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>(); |
| 57 | + |
| 58 | + var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration); |
| 59 | + string actualAppId = callerProvider.CompletionAppId(_appid); |
| 60 | + Assert.AreEqual("expected-appid", actualAppId); |
| 61 | + } |
| 62 | + |
| 63 | + [TestMethod] |
| 64 | + public void TestCompletionAppId3() |
| 65 | + { |
| 66 | + string expectedAppId = "expected-appid"; |
| 67 | + var builder = WebApplication.CreateBuilder(); |
| 68 | + builder.Services.AddMasaConfiguration(options => options.AddJsonFile("customAppsettings.json")); |
| 69 | + builder.Services.Configure<DaprOptions>(options => |
| 70 | + { |
| 71 | + options.AppPort = 5000; |
| 72 | + options.AppIdSuffix = "suffix"; |
| 73 | + }); |
| 74 | + |
| 75 | + var serviceProvider = builder.Services.BuildServiceProvider(); |
| 76 | + |
| 77 | + var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>(); |
| 78 | + |
| 79 | + var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration, builder.Services.GetMasaConfiguration()); |
| 80 | + string actualAppId = callerProvider.CompletionAppId(_appid); |
| 81 | + Assert.AreEqual(expectedAppId, actualAppId); |
| 82 | + } |
| 83 | + |
| 84 | + [TestMethod] |
| 85 | + public void TestCompletionAppId4() |
| 86 | + { |
| 87 | + string expectedAppId = $"{_appid}-{Guid.NewGuid()}"; |
| 88 | + |
| 89 | + Environment.SetEnvironmentVariable($"{_appid}-suffix", expectedAppId); |
| 90 | + var builder = WebApplication.CreateBuilder(); |
| 91 | + builder.Services.AddMasaConfiguration(options => options.AddJsonFile("customAppsettings.json")); |
| 92 | + builder.Services.Configure<DaprOptions>(options => |
| 93 | + { |
| 94 | + options.AppPort = 5000; |
| 95 | + options.AppIdSuffix = "suffix"; |
| 96 | + }); |
| 97 | + |
| 98 | + var serviceProvider = builder.Services.BuildServiceProvider(); |
| 99 | + |
| 100 | + var daprOptions = serviceProvider.GetRequiredService<IOptionsMonitor<DaprOptions>>(); |
| 101 | + |
| 102 | + var callerProvider = new DefaultCallerProvider(daprOptions, builder.Configuration, builder.Services.GetMasaConfiguration()); |
| 103 | + string actualAppId = callerProvider.CompletionAppId(_appid); |
| 104 | + Assert.AreEqual(expectedAppId, actualAppId); |
| 105 | + } |
| 106 | +} |
0 commit comments