Skip to content

Commit

Permalink
pr-fix: add unit tests for success path when registering sink
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnmoreels committed Oct 27, 2023
1 parent 6332f74 commit 27734e2
Showing 1 changed file with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Azure.Identity;
using Bogus;
using Microsoft.ApplicationInsights;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Configuration;
Expand All @@ -11,8 +12,55 @@ namespace Arcus.Observability.Tests.Unit.Serilog.Sinks.ApplicationInsights
{
public class LoggerConfigurationExtensionsTests
{
private const string ExampleConnectionString = "InstrumentationKey=<key>";

private static readonly Faker BogusGenerator = new Faker();

[Fact]
public void AzureApplicationInsightsUsingManagedIdentity_WithDefault_Succeeds()
{
// Arrange
IServiceProvider provider = CreateServiceProviderWithTelemetryClient();
string clientId = BogusGenerator.Random.Guid().ToString().OrNull(BogusGenerator);

var config = new LoggerConfiguration();

// Act
config.WriteTo.AzureApplicationInsightsUsingManagedIdentity(provider, ExampleConnectionString, clientId);
}

[Fact]
public void AzureApplicationInsightsUsingAuthentication_WithDefault_Succeeds()
{
// Arrange
IServiceProvider provider = CreateServiceProviderWithTelemetryClient();
var credential = new DefaultAzureCredential();

var config = new LoggerConfiguration();

// Act
config.WriteTo.AzureApplicationInsightsUsingAuthentication(provider, ExampleConnectionString, credential);
}

[Fact]
public void AzureApplicationInsightsWithConnectionString_WithDefault_Succeeds()
{
// Arrange
IServiceProvider provider = CreateServiceProviderWithTelemetryClient();
var config = new LoggerConfiguration();

// Act
config.WriteTo.AzureApplicationInsightsWithConnectionString(provider, ExampleConnectionString);
}

private static IServiceProvider CreateServiceProviderWithTelemetryClient()
{
var services = new ServiceCollection();
services.AddSingleton<TelemetryClient>();

return services.BuildServiceProvider();
}

[Theory]
[ClassData(typeof(Blanks))]
public void AzureApplicationInsightsUsingManagedIdentity_WithoutConnectionString_Fails(string connectionString)
Expand Down Expand Up @@ -67,7 +115,7 @@ public void AzureApplicationInsightsUsingManagedIdentity_WithoutTelemetryClient_

// Act / Assert
Assert.ThrowsAny<InvalidOperationException>(
() => config.WriteTo.AzureApplicationInsightsUsingManagedIdentity(provider, "<connection-string>", "<client-id>"));
() => config.WriteTo.AzureApplicationInsightsUsingManagedIdentity(provider, ExampleConnectionString, "<client-id>"));
}

[Fact]
Expand All @@ -80,7 +128,7 @@ public void AzureApplicationInsightsUsingAuthentication_WithoutTokenCredential_F

// Act / Assert
Assert.ThrowsAny<ArgumentException>(
() => config.WriteTo.AzureApplicationInsightsUsingAuthentication(provider, "<connection-string>", tokenCredential: null));
() => config.WriteTo.AzureApplicationInsightsUsingAuthentication(provider, ExampleConnectionString, tokenCredential: null));
}

[Fact]
Expand All @@ -94,7 +142,7 @@ public void AzureApplicationInsightsUsingAuthenticationWithLogLevel_WithoutToken

// Act / Assert
Assert.ThrowsAny<ArgumentException>(
() => config.WriteTo.AzureApplicationInsightsUsingAuthentication(provider, "<connection-string>", tokenCredential: null, level));
() => config.WriteTo.AzureApplicationInsightsUsingAuthentication(provider, ExampleConnectionString, tokenCredential: null, level));
}

[Fact]
Expand All @@ -108,7 +156,7 @@ public void AzureApplicationInsightsUsingAuthenticationWithOptions_WithoutTokenC

// Act / Assert
Assert.ThrowsAny<ArgumentException>(
() => config.WriteTo.AzureApplicationInsightsUsingAuthentication(provider, "<connection-string>", tokenCredential: null, level, opt => { }));
() => config.WriteTo.AzureApplicationInsightsUsingAuthentication(provider, ExampleConnectionString, tokenCredential: null, level, opt => { }));
}

[Theory]
Expand Down Expand Up @@ -170,7 +218,7 @@ public void AzureApplicationInsightsUsingAuthentication_WithoutTelemetryClient_F

// Act / Assert
Assert.ThrowsAny<InvalidOperationException>(
() => config.WriteTo.AzureApplicationInsightsUsingAuthentication(provider, "<connection-string>", credential));
() => config.WriteTo.AzureApplicationInsightsUsingAuthentication(provider, ExampleConnectionString, credential));
}

[Fact]
Expand Down Expand Up @@ -237,7 +285,7 @@ public void AzureApplicationInsightsWithConnectionString_WithoutTelemetryClient_

// Act / Assert
Assert.ThrowsAny<InvalidOperationException>(
() => config.WriteTo.AzureApplicationInsightsWithConnectionString(provider, "<connection-string>"));
() => config.WriteTo.AzureApplicationInsightsWithConnectionString(provider, ExampleConnectionString));
}

[Fact]
Expand All @@ -251,7 +299,7 @@ public void AzureApplicationInsightsWithConnectionStringWithLogLevel_WithoutTele

// Act / Assert
Assert.ThrowsAny<InvalidOperationException>(
() => config.WriteTo.AzureApplicationInsightsWithConnectionString(provider, "<connection-string>", level));
() => config.WriteTo.AzureApplicationInsightsWithConnectionString(provider, ExampleConnectionString, level));
}

[Fact]
Expand All @@ -278,7 +326,7 @@ public void AzureApplicationInsightsWithConnectionStringWithLevelAndOptions_With

// Act / Assert
Assert.ThrowsAny<InvalidOperationException>(
() => config.WriteTo.AzureApplicationInsightsWithConnectionString(provider, "<connection-string>", level, options => { }));
() => config.WriteTo.AzureApplicationInsightsWithConnectionString(provider, ExampleConnectionString, level, options => { }));
}

[Theory]
Expand Down

0 comments on commit 27734e2

Please sign in to comment.