Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions Source/Boxed.AspNetCore/HealthChecksBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
namespace Boxed.AspNetCore
{
using System;
using Microsoft.Extensions.DependencyInjection;

/// <summary>
/// <see cref="IHealthChecksBuilder"/> extension methods.
/// </summary>
public static class HealthChecksBuilderExtensions
{
/// <summary>
/// Executes the specified action if the specified <paramref name="condition"/> is <c>true</c> which can be
/// used to conditionally add to the health check builder.
/// </summary>
/// <param name="healthChecksBuilder">The health checks builder.</param>
/// <param name="condition">If set to <c>true</c> the action is executed.</param>
/// <param name="action">The action used to add to the health check builder.</param>
/// <returns>The same health checks builder.</returns>
public static IHealthChecksBuilder AddIf(
this IHealthChecksBuilder healthChecksBuilder,
bool condition,
Func<IHealthChecksBuilder, IHealthChecksBuilder> 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;
}

/// <summary>
/// Executes the specified <paramref name="ifAction"/> if the specified <paramref name="condition"/> is
/// <c>true</c>, otherwise executes the <paramref name="elseAction"/>. This can be used to conditionally add to
/// the health check builder.
/// </summary>
/// <param name="healthChecksBuilder">The health checks builder.</param>
/// <param name="condition">If set to <c>true</c> the <paramref name="ifAction"/> is executed, otherwise the
/// <paramref name="elseAction"/> is executed.</param>
/// <param name="ifAction">The action used to add to the health check builder if the condition is <c>true</c>.</param>
/// <param name="elseAction">The action used to add to the health check builder if the condition is <c>false</c>.</param>
/// <returns>The same health checks builder.</returns>
public static IHealthChecksBuilder AddIfElse(
this IHealthChecksBuilder healthChecksBuilder,
bool condition,
Func<IHealthChecksBuilder, IHealthChecksBuilder> ifAction,
Func<IHealthChecksBuilder, IHealthChecksBuilder> 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;
}
}
}
78 changes: 78 additions & 0 deletions Tests/Boxed.AspNetCore.Test/HealthChecksBuilderExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -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<IHealthChecksBuilder> healthChecksBuilderMock;

public HealthChecksBuilderExtensionsTest() =>
this.healthChecksBuilderMock = new Mock<IHealthChecksBuilder>(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);
}
}