Skip to content

Commit

Permalink
removed health check validation feature
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll committed Jan 4, 2025
1 parent d79da2e commit 7edec28
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 200 deletions.
11 changes: 0 additions & 11 deletions sample/Sample.Minimal/CustomHostedService.cs

This file was deleted.

15 changes: 0 additions & 15 deletions sample/Sample.Minimal/CustomHostedServiceOptions.cs

This file was deleted.

1 change: 0 additions & 1 deletion sample/Sample.Minimal/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers().AddControllersAsServices();
builder.Services.AddHostedService<CustomHostedService>();

var app = await builder.ConfigureRocketSurgery();
app.UseExceptionHandler();
Expand Down
25 changes: 0 additions & 25 deletions sample/Sample.Restful/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using FluentValidation;

using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Options;

using Rocket.Surgery.Hosting;
using Rocket.Surgery.LaunchPad.AspNetCore;
Expand All @@ -11,7 +8,6 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers().AddControllersAsServices();
builder.Services.AddHostedService<CustomHostedService>();

var app = await builder.ConfigureRocketSurgery();
app.UseExceptionHandler();
Expand Down Expand Up @@ -53,24 +49,3 @@
app.Run();

public partial class Program;

internal class CustomHostedServiceOptions
{
public string? A { get; set; }

[UsedImplicitly]
private sealed class Validator : AbstractValidator<CustomHostedServiceOptions>
{
public Validator() => RuleFor(z => z.A).NotNull();
}
}

internal class CustomHostedService(IOptions<CustomHostedServiceOptions> options) : BackgroundService
{
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
// ReSharper disable once UnusedVariable
var v = options.Value.A;
return Task.CompletedTask;
}
}
18 changes: 0 additions & 18 deletions src/Foundation/Validation/CustomHealthCheckService.cs

This file was deleted.

16 changes: 7 additions & 9 deletions src/Foundation/Validation/FluentValidationOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using FluentValidation;

using Microsoft.Extensions.Options;

using Rocket.Surgery.LaunchPad.Foundation.Extensions;

namespace Rocket.Surgery.LaunchPad.Foundation.Validation;
Expand All @@ -8,22 +10,18 @@ namespace Rocket.Surgery.LaunchPad.Foundation.Validation;
/// This class enables fluent validators to be used for options validations!
/// </summary>
/// <typeparam name="T"></typeparam>
internal class FluentValidationOptions<T>(
ValidationHealthCheckResults? healthCheckResults = null,
IValidator<T>? validator = null
)
internal class FluentValidationOptions<T>(IValidator<T>? validator = null)
: IValidateOptions<T>
where T : class
{
public virtual ValidateOptionsResult Validate(string? name, T options)
{
if (validator == null) return ValidateOptionsResult.Skip;
if (validator is null) return ValidateOptionsResult.Skip;

var result = validator.Validate(options);
healthCheckResults?.AddResult(typeof(T).GetNestedTypeName(), name ?? Options.DefaultName, result);
if (result.IsValid) return ValidateOptionsResult.Success;

return ValidateOptionsResult.Fail(
return result.IsValid
? ValidateOptionsResult.Success
: ValidateOptionsResult.Fail(
new[] { $"Failure while validating {typeof(T).GetNestedTypeName()}{( name == Options.DefaultName ? "" : $" (Name: {name})" )}." }
.Concat(result.Errors.Select(z => z.ToString()))
);
Expand Down
30 changes: 0 additions & 30 deletions src/Foundation/Validation/HealthCheckFluentValidationOptions.cs

This file was deleted.

59 changes: 0 additions & 59 deletions src/Foundation/Validation/ValidationHealthCheckResults.cs

This file was deleted.

5 changes: 3 additions & 2 deletions src/Hosting/Conventions/SerilogTelemetryConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Rocket.Surgery.LaunchPad.Hosting.Conventions;
public partial class SerilogTelemetryConvention : ISerilogConvention
{
/// <inheritdoc />
[RequiresUnreferencedCode()]
public void Register(IConventionContext context, IConfiguration configuration, IServiceProvider services, LoggerConfiguration loggerConfiguration) => loggerConfiguration.WriteTo.OpenTelemetry(_ => { }, configuration.GetValue<string>);
[RequiresUnreferencedCode("Serilog")]
public void Register(IConventionContext context, IConfiguration configuration, IServiceProvider services, LoggerConfiguration loggerConfiguration) =>
loggerConfiguration.WriteTo.OpenTelemetry(_ => { }, configuration.GetValue<string>);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,17 @@

using Rocket.Surgery.Conventions;
using Rocket.Surgery.LaunchPad.Foundation;
using Rocket.Surgery.LaunchPad.Foundation.Validation;

namespace Extensions.Tests.Validation;

public class HealthCheckOptionsValidationTests(ITestOutputHelper outputHelper) : AutoFakeTest<XUnitTestContext>(XUnitDefaults.CreateTestContext(outputHelper)), IAsyncLifetime
{
[Fact]
public async Task Should_Validate_Options_And_Throw()
public Task Should_Validate_Options_And_Throw()
{
var a = () => Container.Resolve<IOptions<Options>>().Value;
a.Should().NotThrow();
await Verify(Container.Resolve<ValidationHealthCheckResults>().Results);
}

[Fact]
public async Task Should_Validate_Options_And_Pass()
{
var services = new ServiceCollection();
services
.AddOptions<Options>()
.Configure(
options =>
{
options.Bool = true;
options.Double = -50;
options.Int = 50;
options.String = "Hello";
}
);
Populate(services);
var a = () => Container.Resolve<IOptions<Options>>().Value;
a.Should().NotThrow();
await Verify(Container.Resolve<ValidationHealthCheckResults>().Results);
a.Should().Throw<OptionsValidationException>();
return Task.CompletedTask;
}

[Fact]
Expand All @@ -59,14 +37,12 @@ public async Task Should_Validate_Options_And_Throw_If_Out_Of_Bounds()
);
Populate(services);
var a = () => Container.Resolve<IOptions<Options>>().Value;
a.Should().NotThrow();
await Verify(Container.Resolve<ValidationHealthCheckResults>().Results);
a.Should().Throw<OptionsValidationException>();
}

[Fact]
public async Task Should_Validate_Options_And_Throw_After_Application_Has_Started()
{
Container.Resolve<ValidationHealthCheckResults>().ApplicationHasStarted = true;
var a = () => Container.Resolve<IOptions<Options>>().Value;
var failures = a
.Should()
Expand All @@ -78,7 +54,6 @@ public async Task Should_Validate_Options_And_Throw_After_Application_Has_Starte
[Fact]
public void Should_Validate_Options_And_Pass_After_Application_Has_Started()
{
Container.Resolve<ValidationHealthCheckResults>().ApplicationHasStarted = true;
var services = new ServiceCollection();
services
.AddOptions<Options>()
Expand All @@ -99,7 +74,6 @@ public void Should_Validate_Options_And_Pass_After_Application_Has_Started()
[Fact]
public async Task Should_Validate_Options_And_Throw_If_Out_Of_Bounds_After_Application_Has_Started()
{
Container.Resolve<ValidationHealthCheckResults>().ApplicationHasStarted = true;
var services = new ServiceCollection();
services
.AddOptions<Options>()
Expand Down

0 comments on commit 7edec28

Please sign in to comment.