diff --git a/Source/Boxed.AspNetCore/HealthChecksBuilderExtensions.cs b/Source/Boxed.AspNetCore/HealthChecksBuilderExtensions.cs new file mode 100644 index 00000000..c2b9478c --- /dev/null +++ b/Source/Boxed.AspNetCore/HealthChecksBuilderExtensions.cs @@ -0,0 +1,86 @@ +namespace Boxed.AspNetCore +{ + using System; + using Microsoft.Extensions.DependencyInjection; + + /// + /// extension methods. + /// + public static class HealthChecksBuilderExtensions + { + /// + /// Executes the specified action if the specified is true which can be + /// used to conditionally add to the health check builder. + /// + /// The health checks builder. + /// If set to true the action is executed. + /// The action used to add to the health check builder. + /// The same health checks builder. + public static IHealthChecksBuilder AddIf( + this IHealthChecksBuilder healthChecksBuilder, + bool condition, + Func action) + { + if (healthChecksBuilder is null) + { + throw new ArgumentNullException(nameof(healthChecksBuilder)); + } + + if (action is null) + { + throw new ArgumentNullException(nameof(action)); + } + + if (condition) + { + healthChecksBuilder = action(healthChecksBuilder); + } + + return healthChecksBuilder; + } + + /// + /// Executes the specified if the specified is + /// true, otherwise executes the . This can be used to conditionally add to + /// the health check builder. + /// + /// The health checks builder. + /// If set to true the is executed, otherwise the + /// is executed. + /// The action used to add to the health check builder if the condition is true. + /// The action used to add to the health check builder if the condition is false. + /// The same health checks builder. + public static IHealthChecksBuilder AddIfElse( + this IHealthChecksBuilder healthChecksBuilder, + bool condition, + Func ifAction, + Func elseAction) + { + if (healthChecksBuilder is null) + { + throw new ArgumentNullException(nameof(healthChecksBuilder)); + } + + if (ifAction is null) + { + throw new ArgumentNullException(nameof(ifAction)); + } + + if (elseAction is null) + { + throw new ArgumentNullException(nameof(elseAction)); + } + + if (condition) + { + healthChecksBuilder = ifAction(healthChecksBuilder); + } + else + { + healthChecksBuilder = elseAction(healthChecksBuilder); + } + + return healthChecksBuilder; + } + } +} diff --git a/Tests/Boxed.AspNetCore.Test/HealthChecksBuilderExtensionsTest.cs b/Tests/Boxed.AspNetCore.Test/HealthChecksBuilderExtensionsTest.cs new file mode 100644 index 00000000..94a53add --- /dev/null +++ b/Tests/Boxed.AspNetCore.Test/HealthChecksBuilderExtensionsTest.cs @@ -0,0 +1,78 @@ +namespace Boxed.AspNetCore.Test +{ + using System; + using Boxed.AspNetCore; + using Microsoft.Extensions.DependencyInjection; + using Moq; + using Xunit; + + public sealed class HealthChecksBuilderExtensionsTest : IDisposable + { + private readonly Mock healthChecksBuilderMock; + + public HealthChecksBuilderExtensionsTest() => + this.healthChecksBuilderMock = new Mock(MockBehavior.Strict); + + [Fact] + public void AddIf_TrueCondition_ActionCalled() + { + var actionCalled = false; + + this.healthChecksBuilderMock.Object.AddIf( + true, + healthChecksBuilder => + { + Assert.Same(this.healthChecksBuilderMock.Object, healthChecksBuilder); + actionCalled = true; + return healthChecksBuilder; + }); + + Assert.True(actionCalled); + } + + [Fact] + public void AddIf_FalseCondition_ActionCalled() + { + var actionCalled = false; + + this.healthChecksBuilderMock.Object.AddIf( + false, + healthChecksBuilder => + { + actionCalled = true; + return healthChecksBuilder; + }); + + Assert.False(actionCalled); + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void AddIfElse_TrueCondition_ActionCalled(bool condition) + { + var ifActionCalled = false; + var elseActionCalled = false; + + this.healthChecksBuilderMock.Object.AddIfElse( + condition, + healthChecksBuilder => + { + Assert.Same(this.healthChecksBuilderMock.Object, healthChecksBuilder); + ifActionCalled = true; + return healthChecksBuilder; + }, + healthChecksBuilder => + { + Assert.Same(this.healthChecksBuilderMock.Object, healthChecksBuilder); + elseActionCalled = true; + return healthChecksBuilder; + }); + + Assert.Equal(ifActionCalled, condition); + Assert.NotEqual(elseActionCalled, condition); + } + + public void Dispose() => Mock.VerifyAll(this.healthChecksBuilderMock); + } +}