Skip to content

Commit

Permalink
Add Unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Long Do Thanh committed Sep 29, 2023
1 parent 37e68df commit e1240f9
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Logging/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static IServiceCollection AddOmexLogging(this IServiceCollection serviceC
serviceCollection.TryAddTransient<IExecutionContext, BaseExecutionContext>();
serviceCollection.AddLogging();

const string settingName = "Monitoring:EventSourceLoggingEnabled";
const string settingName = "Monitoring:OmexLoggingEnabled";
bool isEventSourceLoggingEnabled = bool.Parse(context?.Configuration.GetSection(settingName).Get<string>() ?? "true");
if (!isEventSourceLoggingEnabled)
{
Expand Down
78 changes: 78 additions & 0 deletions tests/Logging.UnitTests/ServiceCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Omex.Extensions.Logging.Scrubbing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Omex.Extensions.Logging.UnitTests
Expand Down Expand Up @@ -64,6 +68,80 @@ private T ValidateTypeRegistration<T>(IServiceCollection collection)
return obj;
}

[TestMethod]
public void AddOmexLoggerOnLogBuilder_ContainHostBuilder_SettingOmexLoggingEnabled_False_OmexLoggerNotRegistered()
{
HostBuilderContext hostBuilderContext = new(new Dictionary<object, object>());
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
{ "Monitoring:OmexLoggingEnabled", "false"},
})
.Build();

hostBuilderContext.Configuration = configuration;
IServiceCollection collection = new ServiceCollection()
.AddOmexServiceContext<MockServiceContext>()
.AddOmexLogging(hostBuilderContext);

Assert.ThrowsException<InvalidOperationException>(() =>
{
OmexLogEventSource logEventSource = collection
.BuildServiceProvider(new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true })
.GetRequiredService<OmexLogEventSource>();
});

Assert.ThrowsException<InvalidOperationException>(() =>
{
ILogEventSender eventSender = collection
.BuildServiceProvider(new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true })
.GetRequiredService<ILogEventSender>();
});

Assert.ThrowsException<InvalidOperationException>(() =>
{
ILoggerProvider omexLoggerProvider = collection
.BuildServiceProvider(new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true })
.GetRequiredService<ILoggerProvider>();
});
}

[TestMethod]
[DataTestMethod]
[DataRow(true)]
[DataRow(null!)]
public void AddOmexLoggerOnLogBuilder_ContainHostBuilder_SettingOmexLoggingEnabled_TrueOrMissing_OmexLoggerRegistered(bool? isEnabled)
{
HostBuilderContext hostBuilderContext = new(new Dictionary<object, object>());
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddInMemoryCollection(isEnabled == null ? new Dictionary<string, string?>() : new Dictionary<string, string?>
{
{ "Monitoring:OmexLoggingEnabled", isEnabled.ToString()},
})
.Build();

hostBuilderContext.Configuration = configuration;
IServiceCollection collection = new ServiceCollection()
.AddOmexServiceContext<MockServiceContext>()
.AddOmexLogging(hostBuilderContext);

OmexLogEventSource logEventSource = collection
.BuildServiceProvider(new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true })
.GetRequiredService<OmexLogEventSource>();

ILogEventSender eventSender = collection
.BuildServiceProvider(new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true })
.GetRequiredService<ILogEventSender>();

ILoggerProvider omexLoggerProvider = collection
.BuildServiceProvider(new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true })
.GetRequiredService<ILoggerProvider>();

Assert.IsInstanceOfType<OmexLogEventSource>(logEventSource);
Assert.IsInstanceOfType<ILogEventSender>(eventSender);
Assert.IsInstanceOfType<OmexLoggerProvider>(omexLoggerProvider);
}

private class MockLoggingBuilder : ILoggingBuilder
{
public IServiceCollection Services { get; } = new ServiceCollection();
Expand Down

0 comments on commit e1240f9

Please sign in to comment.