Skip to content

Commit 6271345

Browse files
committed
Refactor tests for info endpoint
- Fixed: adjust contributor nullability to allow null dictionary values - Fixed: do not skip lines in git.properties file that contain = character in their value
1 parent 9b792e2 commit 6271345

25 files changed

+473
-730
lines changed

src/Management/src/Endpoint/Actuators/Info/Contributors/AppSettingsInfoContributor.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66

77
namespace Steeltoe.Management.Endpoint.Actuators.Info.Contributors;
88

9-
internal sealed class AppSettingsInfoContributor(IConfiguration? configuration)
10-
: ConfigurationContributor(configuration), IInfoContributor
9+
internal sealed class AppSettingsInfoContributor : ConfigurationContributor, IInfoContributor
1110
{
1211
private const string AppSettingsPrefix = "info";
1312

13+
public AppSettingsInfoContributor(IConfiguration configuration)
14+
: base(configuration)
15+
{
16+
ArgumentNullException.ThrowIfNull(configuration);
17+
}
18+
1419
public Task ContributeAsync(InfoBuilder builder, CancellationToken cancellationToken)
1520
{
1621
Contribute(builder, AppSettingsPrefix, false);

src/Management/src/Endpoint/Actuators/Info/Contributors/ConfigurationContributor.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,51 @@ protected void Contribute(InfoBuilder builder, string prefix, bool keepPrefix)
1515
ArgumentNullException.ThrowIfNull(builder);
1616
ArgumentException.ThrowIfNullOrEmpty(prefix);
1717

18-
Dictionary<string, object> dictionary = CreateDictionary(prefix, keepPrefix);
18+
Dictionary<string, object?> dictionary = CreateDictionary(prefix, keepPrefix);
1919
builder.WithInfo(dictionary);
2020
}
2121

22-
private Dictionary<string, object> CreateDictionary(string prefix, bool keepPrefix)
22+
private Dictionary<string, object?> CreateDictionary(string prefix, bool keepPrefix)
2323
{
24-
var result = new Dictionary<string, object>();
24+
var result = new Dictionary<string, object?>();
2525

2626
if (Configuration != null)
2727
{
28-
Dictionary<string, object> dictionary = result;
28+
Dictionary<string, object?> dictionary = result;
2929

3030
IConfigurationSection section = Configuration.GetSection(prefix);
31-
IEnumerable<IConfigurationSection> children = section.GetChildren();
31+
IEnumerable<IConfigurationSection> childSections = section.GetChildren();
3232

3333
if (keepPrefix)
3434
{
3535
dictionary = [];
3636
result[prefix] = dictionary;
3737
}
3838

39-
AddChildren(dictionary, children);
39+
AddChildren(dictionary, childSections);
4040
}
4141

4242
return result;
4343
}
4444

45-
private void AddChildren(Dictionary<string, object> dictionary, IEnumerable<IConfigurationSection> sections)
45+
private void AddChildren(Dictionary<string, object?> dictionary, IEnumerable<IConfigurationSection> sections)
4646
{
4747
foreach (IConfigurationSection section in sections)
4848
{
49-
string key = section.Key;
50-
string? value = section.Value;
51-
52-
if (value == null)
49+
if (section.Value == null)
5350
{
54-
var emptyDictionary = new Dictionary<string, object>();
55-
dictionary[key] = emptyDictionary;
56-
AddChildren(emptyDictionary, section.GetChildren());
51+
var subDictionary = new Dictionary<string, object?>();
52+
dictionary[section.Key] = subDictionary;
53+
AddChildren(subDictionary, section.GetChildren());
5754
}
5855
else
5956
{
60-
AddKeyValue(dictionary, key, value);
57+
AddKeyValue(dictionary, section.Key, section.Value);
6158
}
6259
}
6360
}
6461

65-
protected virtual void AddKeyValue(IDictionary<string, object> dictionary, string key, string value)
62+
protected virtual void AddKeyValue(IDictionary<string, object?> dictionary, string key, object? value)
6663
{
6764
ArgumentNullException.ThrowIfNull(dictionary);
6865
ArgumentException.ThrowIfNullOrEmpty(key);

src/Management/src/Endpoint/Actuators/Info/Contributors/GitInfoContributor.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal sealed class GitInfoContributor : ConfigurationContributor, IInfoContri
1313
private const string GitSettingsPrefix = "git";
1414
private const string GitPropertiesFileName = "git.properties";
1515

16-
private static readonly List<string> DatetimeInputKeys = ["time"];
16+
private static readonly List<string> DateTimeInputKeys = ["time"];
1717

1818
private readonly string _propertiesPath;
1919
private readonly ILogger _logger;
@@ -58,7 +58,7 @@ public async Task ContributeAsync(InfoBuilder builder, CancellationToken cancell
5858
continue;
5959
}
6060

61-
string[] keyValuePair = line.Split('=');
61+
string[] keyValuePair = line.Split('=', 2);
6262

6363
if (keyValuePair.Length != 2)
6464
{
@@ -78,23 +78,23 @@ public async Task ContributeAsync(InfoBuilder builder, CancellationToken cancell
7878
}
7979
else
8080
{
81-
_logger.LogWarning("Unable to locate GitInfo at {GitInfoLocation}", _propertiesPath);
81+
_logger.LogWarning("File '{Path}' does not exist.", _propertiesPath);
8282
}
8383

8484
return null;
8585
}
8686

87-
protected override void AddKeyValue(IDictionary<string, object> dictionary, string key, string value)
87+
protected override void AddKeyValue(IDictionary<string, object?> dictionary, string key, object? value)
8888
{
8989
ArgumentNullException.ThrowIfNull(dictionary);
9090
ArgumentException.ThrowIfNullOrEmpty(key);
9191

92-
object valueToInsert = value;
92+
object? valueToInsert = value;
9393

94-
if (DatetimeInputKeys.Contains(key))
94+
if (DateTimeInputKeys.Contains(key) && value is string stringValue)
9595
{
9696
// Normalize datetime values to ISO8601 format
97-
valueToInsert = DateTime.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
97+
valueToInsert = DateTime.Parse(stringValue, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
9898
}
9999

100100
dictionary[key] = valueToInsert;

src/Management/src/Endpoint/Actuators/Info/EndpointServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static IServiceCollection AddInfoActuator(this IServiceCollection service
4242
ArgumentNullException.ThrowIfNull(services);
4343

4444
services.AddCoreActuatorServices<InfoEndpointOptions, ConfigureInfoEndpointOptions, InfoEndpointMiddleware, IInfoEndpointHandler,
45-
InfoEndpointHandler, object?, IDictionary<string, object>>(configureMiddleware);
45+
InfoEndpointHandler, object?, IDictionary<string, object?>>(configureMiddleware);
4646

4747
RegisterDefaultInfoContributors(services);
4848

src/Management/src/Endpoint/Actuators/Info/IInfoEndpointHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
namespace Steeltoe.Management.Endpoint.Actuators.Info;
66

7-
public interface IInfoEndpointHandler : IEndpointHandler<object?, IDictionary<string, object>>;
7+
public interface IInfoEndpointHandler : IEndpointHandler<object?, IDictionary<string, object?>>;

src/Management/src/Endpoint/Actuators/Info/InfoBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ namespace Steeltoe.Management.Endpoint.Actuators.Info;
66

77
public sealed class InfoBuilder
88
{
9-
private readonly Dictionary<string, object> _info = [];
9+
private readonly Dictionary<string, object?> _info = [];
1010

11-
public IDictionary<string, object> Build()
11+
public IDictionary<string, object?> Build()
1212
{
1313
return _info;
1414
}
1515

16-
public InfoBuilder WithInfo(string key, object value)
16+
public InfoBuilder WithInfo(string key, object? value)
1717
{
1818
ArgumentException.ThrowIfNullOrWhiteSpace(key);
1919

@@ -22,13 +22,13 @@ public InfoBuilder WithInfo(string key, object value)
2222
return this;
2323
}
2424

25-
public InfoBuilder WithInfo(IDictionary<string, object> details)
25+
public InfoBuilder WithInfo(IDictionary<string, object?> details)
2626
{
2727
ArgumentNullException.ThrowIfNull(details);
2828

29-
foreach (KeyValuePair<string, object> pair in details)
29+
foreach ((string key, object? value) in details)
3030
{
31-
_info[pair.Key] = pair.Value;
31+
_info[key] = value;
3232
}
3333

3434
return this;

src/Management/src/Endpoint/Actuators/Info/InfoEndpointHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public InfoEndpointHandler(IOptionsMonitor<InfoEndpointOptions> optionsMonitor,
3232
_logger = loggerFactory.CreateLogger<InfoEndpointHandler>();
3333
}
3434

35-
public async Task<IDictionary<string, object>> InvokeAsync(object? argument, CancellationToken cancellationToken)
35+
public async Task<IDictionary<string, object?>> InvokeAsync(object? argument, CancellationToken cancellationToken)
3636
{
3737
var builder = new InfoBuilder();
3838

@@ -44,7 +44,8 @@ public async Task<IDictionary<string, object>> InvokeAsync(object? argument, Can
4444
}
4545
catch (Exception exception) when (!exception.IsCancellation())
4646
{
47-
_logger.LogError(exception, "Operation failed.");
47+
_logger.LogWarning(exception, "Exception thrown by contributor '{ContributorTypeName}' while contributing to info endpoint.",
48+
contributor.GetType());
4849
}
4950
}
5051

src/Management/src/Endpoint/Actuators/Info/InfoEndpointMiddleware.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ namespace Steeltoe.Management.Endpoint.Actuators.Info;
1111

1212
internal sealed class InfoEndpointMiddleware(
1313
IInfoEndpointHandler endpointHandler, IOptionsMonitor<ManagementOptions> managementOptionsMonitor, ILoggerFactory loggerFactory)
14-
: EndpointMiddleware<object?, IDictionary<string, object>>(endpointHandler, managementOptionsMonitor, loggerFactory)
14+
: EndpointMiddleware<object?, IDictionary<string, object?>>(endpointHandler, managementOptionsMonitor, loggerFactory)
1515
{
16-
protected override async Task<IDictionary<string, object>> InvokeEndpointHandlerAsync(object? request, CancellationToken cancellationToken)
16+
protected override async Task<IDictionary<string, object?>> InvokeEndpointHandlerAsync(object? request, CancellationToken cancellationToken)
1717
{
1818
return await EndpointHandler.InvokeAsync(request, cancellationToken);
1919
}

src/Management/src/Endpoint/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,10 @@ Steeltoe.Management.Endpoint.Actuators.Info.IInfoContributor
263263
Steeltoe.Management.Endpoint.Actuators.Info.IInfoContributor.ContributeAsync(Steeltoe.Management.Endpoint.Actuators.Info.InfoBuilder! builder, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task!
264264
Steeltoe.Management.Endpoint.Actuators.Info.IInfoEndpointHandler
265265
Steeltoe.Management.Endpoint.Actuators.Info.InfoBuilder
266-
Steeltoe.Management.Endpoint.Actuators.Info.InfoBuilder.Build() -> System.Collections.Generic.IDictionary<string!, object!>!
266+
Steeltoe.Management.Endpoint.Actuators.Info.InfoBuilder.Build() -> System.Collections.Generic.IDictionary<string!, object?>!
267267
Steeltoe.Management.Endpoint.Actuators.Info.InfoBuilder.InfoBuilder() -> void
268-
Steeltoe.Management.Endpoint.Actuators.Info.InfoBuilder.WithInfo(string! key, object! value) -> Steeltoe.Management.Endpoint.Actuators.Info.InfoBuilder!
269-
Steeltoe.Management.Endpoint.Actuators.Info.InfoBuilder.WithInfo(System.Collections.Generic.IDictionary<string!, object!>! details) -> Steeltoe.Management.Endpoint.Actuators.Info.InfoBuilder!
268+
Steeltoe.Management.Endpoint.Actuators.Info.InfoBuilder.WithInfo(string! key, object? value) -> Steeltoe.Management.Endpoint.Actuators.Info.InfoBuilder!
269+
Steeltoe.Management.Endpoint.Actuators.Info.InfoBuilder.WithInfo(System.Collections.Generic.IDictionary<string!, object?>! details) -> Steeltoe.Management.Endpoint.Actuators.Info.InfoBuilder!
270270
Steeltoe.Management.Endpoint.Actuators.Info.InfoEndpointOptions
271271
Steeltoe.Management.Endpoint.Actuators.Info.InfoEndpointOptions.InfoEndpointOptions() -> void
272272
Steeltoe.Management.Endpoint.Actuators.Loggers.EndpointServiceCollectionExtensions

src/Management/test/Endpoint.Test/Actuators/Info/AppSettingsInfoContributorTest.cs

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)