#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.DTO.Configuration;
using Lumina.Presentation.Web.Common.HealthChecks;
using Lumina.Presentation.Web.Fixtures.Common.Api;
using Lumina.Presentation.Web.Fixtures.Common.DTO.Configuration;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Options;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Web.UnitTests.Common.HealthChecks;
///
/// Contains unit tests for the class.
///
[ExcludeFromCodeCoverage]
public class ApiReachabilityHealthCheckTests
{
private readonly ServerConfigurationDtoFixture _serverConfigurationDtoFixture = new();
[Fact]
public async Task CheckHealthAsync_WhenApiReturnsSuccess_ShouldReturnHealthyAndProbeReadyEndpoint()
{
// Arrange
TestApiHttpMessageHandler messageHandler = new(_ => new HttpResponseMessage(HttpStatusCode.OK));
ApiReachabilityHealthCheck sut = CreateSut(messageHandler);
// Act
HealthCheckResult result = await sut.CheckHealthAsync(CreateHealthCheckContext(sut), CancellationToken.None);
// Assert
Assert.Equal(HealthStatus.Healthy, result.Status);
Assert.Single(messageHandler.Requests);
Assert.Equal("http://localhost:5214/health/ready", messageHandler.Requests[0].RequestUri!.ToString());
}
[Fact]
public async Task CheckHealthAsync_WhenApiReturnsErrorStatus_ShouldReturnUnhealthyWithStatusCodeDescription()
{
// Arrange
TestApiHttpMessageHandler messageHandler = new(_ => new HttpResponseMessage(HttpStatusCode.ServiceUnavailable));
ApiReachabilityHealthCheck sut = CreateSut(messageHandler);
// Act
HealthCheckResult result = await sut.CheckHealthAsync(CreateHealthCheckContext(sut), CancellationToken.None);
// Assert
Assert.Equal(HealthStatus.Unhealthy, result.Status);
Assert.Equal("The API returned status 503.", result.Description);
}
[Fact]
public async Task CheckHealthAsync_WhenApiRequestThrowsException_ShouldReturnUnhealthyWithException()
{
// Arrange
TestApiHttpMessageHandler messageHandler = new(_ => throw new HttpRequestException("The API is down."));
ApiReachabilityHealthCheck sut = CreateSut(messageHandler);
// Act
HealthCheckResult result = await sut.CheckHealthAsync(CreateHealthCheckContext(sut), CancellationToken.None);
// Assert
Assert.Equal(HealthStatus.Unhealthy, result.Status);
Assert.Equal("The API is not reachable.", result.Description);
Assert.NotNull(result.Exception);
}
[Fact]
public async Task CheckHealthAsync_WhenCancellationIsRequestedBeforeProbe_ShouldReturnUnhealthy()
{
// Arrange
TestApiHttpMessageHandler messageHandler = new(_ => new HttpResponseMessage(HttpStatusCode.OK));
ApiReachabilityHealthCheck sut = CreateSut(messageHandler);
CancellationTokenSource cancellationTokenSource = new();
cancellationTokenSource.Cancel();
// Act
HealthCheckResult result = await sut.CheckHealthAsync(CreateHealthCheckContext(sut), cancellationTokenSource.Token);
// Assert
Assert.Equal(HealthStatus.Unhealthy, result.Status);
Assert.Equal("The API is not reachable.", result.Description);
}
///
/// Creates a whose registration points at the given health check.
///
/// The health check to register in the context.
/// The created instance.
private static HealthCheckContext CreateHealthCheckContext(IHealthCheck healthCheck)
{
// the ApiReachabilityHealthCheck never reads the registration, so a bare context is sufficient
return new HealthCheckContext();
}
///
/// Creates the system under test configured with the provided message handler and a fixed server configuration.
///
/// The message handler backing the inner .
/// The created .
private ApiReachabilityHealthCheck CreateSut(TestApiHttpMessageHandler messageHandler)
{
HttpClient httpClient = new(messageHandler);
IHttpClientFactory httpClientFactory = Substitute.For();
httpClientFactory.CreateClient().Returns(httpClient);
IOptionsSnapshot serverConfigurationOptions = Substitute.For>();
serverConfigurationOptions.Value.Returns(_serverConfigurationDtoFixture.Create(apiVersion: '1', baseAddress: "http://localhost", port: 5214));
return new ApiReachabilityHealthCheck(httpClientFactory, serverConfigurationOptions);
}
}