-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IHealthChecksBuilder AddIf and AddIfElse extension methods
- Loading branch information
1 parent
18d87d0
commit 1350b12
Showing
2 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
78
Tests/Boxed.AspNetCore.Test/HealthChecksBuilderExtensionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |