-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathApplicationInstanceInfoTest.cs
More file actions
79 lines (63 loc) · 2.6 KB
/
Copy pathApplicationInstanceInfoTest.cs
File metadata and controls
79 lines (63 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Steeltoe.Common.Extensions;
using Steeltoe.Common.TestResources;
namespace Steeltoe.Common.Test;
public sealed class ApplicationInstanceInfoTest
{
[Fact]
public void ConstructorSetsDefaults()
{
var options = new ApplicationInstanceInfo();
options.ApplicationName.Should().BeNull();
}
[Fact]
public async Task ReadsApplicationConfiguration()
{
const string appSettings = """
{
"Spring": {
"Application": {
"Name": "my-app"
}
}
}
""";
var fileProvider = new MemoryFileProvider();
fileProvider.IncludeAppSettingsJsonFile(appSettings);
var builder = new ConfigurationBuilder();
builder.AddInMemoryAppSettingsJsonFile(fileProvider);
IConfiguration configuration = builder.Build();
var services = new ServiceCollection();
services.AddSingleton(configuration);
services.AddApplicationInstanceInfo();
await using ServiceProvider serviceProvider = services.BuildServiceProvider(true);
var options = serviceProvider.GetRequiredService<IApplicationInstanceInfo>();
options.ApplicationName.Should().Be("my-app");
}
[Fact]
public void Resolves_application_name_from_configuration()
{
var appSettings = new Dictionary<string, string?>();
IApplicationInstanceInfo info = FromConfiguration(appSettings);
info.ApplicationName.Should().Be(Assembly.GetEntryAssembly()!.GetName().Name);
appSettings.Add("spring:application:name", "SpringAppName");
info = FromConfiguration(appSettings);
info.ApplicationName.Should().Be("SpringAppName");
}
private IApplicationInstanceInfo FromConfiguration(IDictionary<string, string?> appSettings)
{
var builder = new ConfigurationBuilder();
builder.AddInMemoryCollection(appSettings);
IConfiguration configuration = builder.Build();
var services = new ServiceCollection();
services.AddSingleton(configuration);
services.AddApplicationInstanceInfo();
using ServiceProvider serviceProvider = services.BuildServiceProvider(true);
return serviceProvider.GetRequiredService<IApplicationInstanceInfo>();
}
}