#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Api.IntegrationTests.Common.Setup;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.IntegrationTests.Core.Health;
///
/// Contains integration tests for the /health/live and /health/ready probe routes.
///
[ExcludeFromCodeCoverage]
public class HealthEndpointTests : IClassFixture
{
private readonly HttpClient _client;
private readonly JsonSerializerOptions _jsonOptions = new()
{
PropertyNameCaseInsensitive = true
};
///
/// Initializes a new instance of the class.
///
/// Injected in-memory API factory.
public HealthEndpointTests(LuminaApiFactory apiFactory)
{
_client = apiFactory.CreateClient();
}
[Fact]
public async Task HealthLive_WhenCalled_ShouldReturnOk()
{
// Act
HttpResponseMessage response = await _client.GetAsync("/health/live");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
[Fact]
public async Task HealthReady_WhenDatabaseIsReachable_ShouldReturnHealthyReport()
{
// Act
HttpResponseMessage response = await _client.GetAsync("/health/ready");
string content = await response.Content.ReadAsStringAsync();
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
HealthReportDto report = JsonSerializer.Deserialize(content, _jsonOptions)!;
Assert.Equal("Healthy", report.Status);
HealthCheckReportDto databaseCheck = Assert.Single(report.Checks, check => check.Name == "database");
Assert.Equal("Healthy", databaseCheck.Status);
}
///
/// Deserialized shape of the health report JSON written by the API.
///
private sealed record HealthReportDto(string Status, List Checks);
///
/// Deserialized shape of a single health check entry within the health report.
///
private sealed record HealthCheckReportDto(string Name, string Status, string? Description);
}