-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fluent methods when configuring
FluxzySetting
alteration rule (#…
…112) * Add extension methods structure to add rules in a fluent way * Lower modifiers for internal implementation * Make SetupRule immediate method * Add unit tests --------- Co-authored-by: fluxzy-ci <[email protected]>
- Loading branch information
Showing
12 changed files
with
273 additions
and
35 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
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
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
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
30 changes: 30 additions & 0 deletions
30
src/Fluxzy.Core/Rules/Extensions/ConfigureActionBuilder.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,30 @@ | ||
using System; | ||
using System.Linq; | ||
using Fluxzy.Rules.Filters; | ||
|
||
namespace Fluxzy.Rules.Extensions | ||
{ | ||
internal class ConfigureActionBuilder : IConfigureActionBuilder | ||
{ | ||
public FluxzySetting Setting { get; } | ||
|
||
private readonly Filter _filter; | ||
|
||
public ConfigureActionBuilder(FluxzySetting setting, Filter filter) | ||
{ | ||
Setting = setting; | ||
_filter = filter; | ||
} | ||
|
||
public IConfigureFilterBuilder Do(Action action, params Action [] actions) | ||
{ | ||
if (action == null) | ||
throw new ArgumentNullException(nameof(action)); | ||
|
||
Setting.AddAlterationRules(new Rule(action, _filter)); | ||
Setting.AddAlterationRules(actions.Select(a => new Rule(a, _filter))); | ||
|
||
return new ConfigureFilterBuilderBuilder(Setting); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Fluxzy.Core/Rules/Extensions/ConfigureFilterBuilderBuilder.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,37 @@ | ||
using System; | ||
using System.Linq; | ||
using Fluxzy.Rules.Filters; | ||
|
||
namespace Fluxzy.Rules.Extensions | ||
{ | ||
internal class ConfigureFilterBuilderBuilder : IConfigureFilterBuilder | ||
{ | ||
public ConfigureFilterBuilderBuilder(FluxzySetting fluxzySetting) | ||
{ | ||
FluxzySetting = fluxzySetting; | ||
} | ||
|
||
public FluxzySetting FluxzySetting { get; } | ||
|
||
public IConfigureActionBuilder When(Filter filter) | ||
{ | ||
if (filter == null) | ||
throw new ArgumentNullException(nameof(filter)); | ||
|
||
return new ConfigureActionBuilder(FluxzySetting, filter); | ||
} | ||
|
||
public IConfigureActionBuilder WhenAny(params Filter[] filters) | ||
{ | ||
return new ConfigureActionBuilder(FluxzySetting, | ||
filters.Any() ? new FilterCollection(filters) { Operation = SelectorCollectionOperation.Or }: AnyFilter.Default); | ||
} | ||
|
||
public IConfigureActionBuilder WhenAll(params Filter[] filters) | ||
{ | ||
return new ConfigureActionBuilder(FluxzySetting, filters.Any() ? | ||
new FilterCollection(filters) { Operation = SelectorCollectionOperation.And } | ||
: NoFilter.Default); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Fluxzy.Core/Rules/Extensions/IConfigureActionBuilder.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,21 @@ | ||
namespace Fluxzy.Rules.Extensions | ||
{ | ||
/// <summary> | ||
/// Helper to build alteration rules in a fluent way | ||
/// </summary> | ||
public interface IConfigureActionBuilder | ||
{ | ||
/// <summary> | ||
/// The current setting | ||
/// </summary> | ||
FluxzySetting Setting { get; } | ||
|
||
/// <summary> | ||
/// Add one or more actions to the rule | ||
/// </summary> | ||
/// <param name="action"></param> | ||
/// <param name="actions"></param> | ||
/// <returns></returns> | ||
IConfigureFilterBuilder Do(Action action, params Action [] actions); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Fluxzy.Core/Rules/Extensions/IConfigureFilterBuilder.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,37 @@ | ||
using Fluxzy.Rules.Filters; | ||
|
||
namespace Fluxzy.Rules.Extensions | ||
{ | ||
/// <summary> | ||
/// Helper to build alteration rules in a fluent way | ||
/// </summary> | ||
public interface IConfigureFilterBuilder | ||
{ | ||
/// <summary> | ||
/// The current fluxzy setting | ||
/// </summary> | ||
FluxzySetting FluxzySetting { get; } | ||
|
||
/// <summary> | ||
/// Create a rule that will be applied when the filter passes | ||
/// </summary> | ||
/// <param name="filter"></param> | ||
/// <returns></returns> | ||
|
||
IConfigureActionBuilder When(Filter filter); | ||
|
||
/// <summary> | ||
/// Create a rule that will be applied when any of the filters passes. If no filters are provided, the rule will be applied always. | ||
/// </summary> | ||
/// <param name="filters"></param> | ||
/// <returns></returns> | ||
IConfigureActionBuilder WhenAny(params Filter[] filters); | ||
|
||
/// <summary> | ||
/// Create a rule that will be applied when all of the filters passes | ||
/// </summary> | ||
/// <param name="filters"></param> | ||
/// <returns></returns> | ||
IConfigureActionBuilder WhenAll(params Filter[] filters); | ||
} | ||
} |
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
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
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
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,90 @@ | ||
using System.Linq; | ||
using Fluxzy.Rules.Actions; | ||
using Fluxzy.Rules.Filters; | ||
using Fluxzy.Rules.Filters.RequestFilters; | ||
using Fluxzy.Rules.Filters.ResponseFilters; | ||
using Xunit; | ||
|
||
namespace Fluxzy.Tests.UnitTests | ||
{ | ||
public class RuleExtensions | ||
{ | ||
[Fact] | ||
public void Test_When_Any() | ||
{ | ||
var setting = FluxzySetting.CreateDefault(); | ||
|
||
var action = new AddRequestHeaderAction("x", "y"); | ||
|
||
setting.ClearAlterationRules(); | ||
|
||
setting.ConfigureRule().WhenAny().Do(action); | ||
|
||
Assert.Single(setting.AlterationRules); | ||
Assert.Equal(action, setting.AlterationRules.First().Action); | ||
Assert.Equal(AnyFilter.Default, setting.AlterationRules.First().Filter); | ||
} | ||
|
||
[Fact] | ||
public void Test_When_Multiple_Actions() | ||
{ | ||
var setting = FluxzySetting.CreateDefault(); | ||
|
||
var filter = new HostFilter("myhost.com", StringSelectorOperation.Contains); | ||
var actionA = new AddRequestHeaderAction("x", "y"); | ||
var actionB = new ForceHttp11Action(); | ||
|
||
setting.ClearAlterationRules(); | ||
|
||
setting.ConfigureRule().When(filter).Do(actionA, actionB); | ||
|
||
Assert.Equal(2, setting.AlterationRules.Count); | ||
Assert.Equal(actionA, setting.AlterationRules.First().Action); | ||
Assert.Equal(actionB, setting.AlterationRules.Last().Action); | ||
} | ||
|
||
[Fact] | ||
public void Test_When_Any_Multiple_Actions() | ||
{ | ||
var setting = FluxzySetting.CreateDefault(); | ||
|
||
var filterA = new HostFilter("myhost.com", StringSelectorOperation.Contains); | ||
var filterB = new StatusCodeSuccessFilter(); | ||
|
||
var actionA = new AddRequestHeaderAction("x", "y"); | ||
var actionB = new ForceHttp11Action(); | ||
|
||
setting.ClearAlterationRules(); | ||
|
||
setting.ConfigureRule().WhenAny(filterA, filterB).Do(actionA, actionB); | ||
|
||
Assert.Equal(2, setting.AlterationRules.Count); | ||
Assert.Equal(actionA, setting.AlterationRules.First().Action); | ||
Assert.Equal(actionB, setting.AlterationRules.Last().Action); | ||
Assert.Equal(typeof(FilterCollection), setting.AlterationRules.Last().Filter.GetType()); | ||
Assert.Equal(SelectorCollectionOperation.Or, ((FilterCollection) setting.AlterationRules.Last().Filter).Operation); | ||
} | ||
|
||
[Fact] | ||
public void Test_When_All() | ||
{ | ||
var setting = FluxzySetting.CreateDefault(); | ||
|
||
var filterA = new HostFilter("myhost.com", StringSelectorOperation.Contains); | ||
var filterB = new StatusCodeSuccessFilter(); | ||
|
||
var actionA = new AddRequestHeaderAction("x", "y"); | ||
var actionB = new ForceHttp11Action(); | ||
|
||
setting.ClearAlterationRules(); | ||
|
||
setting.ConfigureRule().WhenAll(filterA, filterB).Do(actionA, actionB); | ||
|
||
Assert.Equal(2, setting.AlterationRules.Count); | ||
Assert.Equal(actionA, setting.AlterationRules.First().Action); | ||
Assert.Equal(actionB, setting.AlterationRules.Last().Action); | ||
Assert.Equal(typeof(FilterCollection), setting.AlterationRules.Last().Filter.GetType()); | ||
Assert.Equal(SelectorCollectionOperation.And, ((FilterCollection)setting.AlterationRules.Last().Filter).Operation); | ||
} | ||
} | ||
} |