Skip to content

Commit 0bb88f2

Browse files
CopilotJFenderson
andcommitted
Add tests for NoOpTelemetryService and conditional registration
Co-authored-by: JFenderson <24466206+JFenderson@users.noreply.github.com>
1 parent 1013eba commit 0bb88f2

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using Xunit;
2+
using FluentAssertions;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Configuration;
5+
using Podium.API.Extensions;
6+
using Podium.Core.Interfaces;
7+
using Podium.Infrastructure.Telemetry;
8+
9+
namespace Podium.Tests.Unit.Extensions
10+
{
11+
/// <summary>
12+
/// Unit tests for MonitoringExtensions to verify conditional telemetry service registration.
13+
/// </summary>
14+
public class MonitoringExtensionsTests
15+
{
16+
[Fact]
17+
public void AddPodiumTelemetryServices_WithApplicationInsightsDisabled_RegistersNoOpService()
18+
{
19+
// Arrange
20+
var services = new ServiceCollection();
21+
services.AddLogging(); // Add logging infrastructure
22+
var configuration = new ConfigurationBuilder()
23+
.AddInMemoryCollection(new Dictionary<string, string?>
24+
{
25+
{ "ENABLE_APPLICATION_INSIGHTS", "false" }
26+
})
27+
.Build();
28+
29+
// Act
30+
services.AddPodiumTelemetryServices(configuration);
31+
var serviceProvider = services.BuildServiceProvider();
32+
33+
// Assert
34+
var telemetryService = serviceProvider.GetService<ITelemetryService>();
35+
telemetryService.Should().NotBeNull();
36+
telemetryService.Should().BeOfType<NoOpTelemetryService>();
37+
}
38+
39+
[Fact]
40+
public void AddPodiumTelemetryServices_WithNoConnectionString_RegistersNoOpService()
41+
{
42+
// Arrange
43+
var services = new ServiceCollection();
44+
services.AddLogging(); // Add logging infrastructure
45+
var configuration = new ConfigurationBuilder()
46+
.AddInMemoryCollection(new Dictionary<string, string?>())
47+
.Build();
48+
49+
// Act
50+
services.AddPodiumTelemetryServices(configuration);
51+
var serviceProvider = services.BuildServiceProvider();
52+
53+
// Assert
54+
var telemetryService = serviceProvider.GetService<ITelemetryService>();
55+
telemetryService.Should().NotBeNull();
56+
telemetryService.Should().BeOfType<NoOpTelemetryService>();
57+
}
58+
59+
[Fact]
60+
public void AddPodiumTelemetryServices_WithEmptyConnectionString_RegistersNoOpService()
61+
{
62+
// Arrange
63+
var services = new ServiceCollection();
64+
services.AddLogging(); // Add logging infrastructure
65+
var configuration = new ConfigurationBuilder()
66+
.AddInMemoryCollection(new Dictionary<string, string?>
67+
{
68+
{ "APPLICATIONINSIGHTS_CONNECTION_STRING", "" },
69+
{ "ENABLE_APPLICATION_INSIGHTS", "true" }
70+
})
71+
.Build();
72+
73+
// Act
74+
services.AddPodiumTelemetryServices(configuration);
75+
var serviceProvider = services.BuildServiceProvider();
76+
77+
// Assert
78+
var telemetryService = serviceProvider.GetService<ITelemetryService>();
79+
telemetryService.Should().NotBeNull();
80+
telemetryService.Should().BeOfType<NoOpTelemetryService>();
81+
}
82+
83+
[Fact]
84+
public void AddPodiumTelemetryServices_WithApplicationInsightsDisabled_DoesNotRegisterMetricsService()
85+
{
86+
// Arrange
87+
var services = new ServiceCollection();
88+
services.AddLogging(); // Add logging infrastructure
89+
var configuration = new ConfigurationBuilder()
90+
.AddInMemoryCollection(new Dictionary<string, string?>
91+
{
92+
{ "ENABLE_APPLICATION_INSIGHTS", "false" }
93+
})
94+
.Build();
95+
96+
// Act
97+
services.AddPodiumTelemetryServices(configuration);
98+
var serviceProvider = services.BuildServiceProvider();
99+
100+
// Assert
101+
var metricsService = serviceProvider.GetService<MetricsService>();
102+
metricsService.Should().BeNull();
103+
}
104+
}
105+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using Xunit;
2+
using Moq;
3+
using FluentAssertions;
4+
using Microsoft.Extensions.Logging;
5+
using Podium.Infrastructure.Telemetry;
6+
7+
namespace Podium.Tests.Unit.Telemetry
8+
{
9+
/// <summary>
10+
/// Unit tests for NoOpTelemetryService to ensure it doesn't throw exceptions
11+
/// and properly logs debug messages when Application Insights is disabled.
12+
/// </summary>
13+
public class NoOpTelemetryServiceTests
14+
{
15+
private readonly Mock<ILogger<NoOpTelemetryService>> _mockLogger;
16+
private readonly NoOpTelemetryService _service;
17+
18+
public NoOpTelemetryServiceTests()
19+
{
20+
_mockLogger = new Mock<ILogger<NoOpTelemetryService>>();
21+
_service = new NoOpTelemetryService(_mockLogger.Object);
22+
}
23+
24+
[Fact]
25+
public void TrackAuthenticationAttempt_DoesNotThrowException()
26+
{
27+
// Arrange
28+
var userId = "test-user";
29+
var success = true;
30+
var reason = "Valid credentials";
31+
32+
// Act
33+
var action = () => _service.TrackAuthenticationAttempt(userId, success, reason);
34+
35+
// Assert
36+
action.Should().NotThrow();
37+
}
38+
39+
[Fact]
40+
public void TrackVideoUpload_DoesNotThrowException()
41+
{
42+
// Arrange
43+
var userId = "test-user";
44+
var fileSizeBytes = 1024L;
45+
var duration = TimeSpan.FromSeconds(30);
46+
var success = true;
47+
48+
// Act
49+
var action = () => _service.TrackVideoUpload(userId, fileSizeBytes, duration, success);
50+
51+
// Assert
52+
action.Should().NotThrow();
53+
}
54+
55+
[Fact]
56+
public void TrackDatabaseQuery_DoesNotThrowException()
57+
{
58+
// Arrange
59+
var queryName = "GetStudentById";
60+
var duration = TimeSpan.FromMilliseconds(50);
61+
62+
// Act
63+
var action = () => _service.TrackDatabaseQuery(queryName, duration);
64+
65+
// Assert
66+
action.Should().NotThrow();
67+
}
68+
69+
[Fact]
70+
public void TrackApiEndpoint_DoesNotThrowException()
71+
{
72+
// Arrange
73+
var endpoint = "/api/students";
74+
var responseTime = TimeSpan.FromMilliseconds(100);
75+
var statusCode = 200;
76+
77+
// Act
78+
var action = () => _service.TrackApiEndpoint(endpoint, responseTime, statusCode);
79+
80+
// Assert
81+
action.Should().NotThrow();
82+
}
83+
84+
[Fact]
85+
public void TrackException_DoesNotThrowException()
86+
{
87+
// Arrange
88+
var exception = new Exception("Test exception");
89+
var properties = new Dictionary<string, string> { { "Key", "Value" } };
90+
91+
// Act
92+
var action = () => _service.TrackException(exception, properties);
93+
94+
// Assert
95+
action.Should().NotThrow();
96+
}
97+
98+
[Fact]
99+
public void TrackAuthenticationAttempt_LogsDebugMessage()
100+
{
101+
// Arrange
102+
var userId = "test-user";
103+
var success = true;
104+
var reason = "Valid credentials";
105+
106+
// Act
107+
_service.TrackAuthenticationAttempt(userId, success, reason);
108+
109+
// Assert
110+
_mockLogger.Verify(
111+
x => x.Log(
112+
LogLevel.Debug,
113+
It.IsAny<EventId>(),
114+
It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains("Application Insights disabled")),
115+
It.IsAny<Exception>(),
116+
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
117+
Times.Once);
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)