mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 03:58:15 +08:00
Feature/refactoring internal config creation stack (#600)
* tidy up some code so I can understand it * broke al the things that make config out into their own classes etc..sigh * fix the things i broked * #597 test for issue, works on this branch :E * #597 removed comments * added tests for new load balancer creator..basic * added tests for lb creator and aggregates creator * added tests for config creator * boiler plate for tests * added dynamics tests * wip * finished refactoring for now
This commit is contained in:
126
test/Ocelot.UnitTests/Configuration/DynamicsCreatorTests.cs
Normal file
126
test/Ocelot.UnitTests/Configuration/DynamicsCreatorTests.cs
Normal file
@ -0,0 +1,126 @@
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class DynamicsCreatorTests
|
||||
{
|
||||
private readonly DynamicsCreator _creator;
|
||||
private readonly Mock<IRateLimitOptionsCreator> _rloCreator;
|
||||
private List<ReRoute> _result;
|
||||
private FileConfiguration _fileConfig;
|
||||
private RateLimitOptions _rlo1;
|
||||
private RateLimitOptions _rlo2;
|
||||
|
||||
public DynamicsCreatorTests()
|
||||
{
|
||||
_rloCreator = new Mock<IRateLimitOptionsCreator>();
|
||||
_creator = new DynamicsCreator(_rloCreator.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_nothing()
|
||||
{
|
||||
var fileConfig = new FileConfiguration();
|
||||
|
||||
this.Given(_ => GivenThe(fileConfig))
|
||||
.When(_ => WhenICreate())
|
||||
.Then(_ => ThenNothingIsReturned())
|
||||
.And(_ => ThenTheRloCreatorIsNotCalled())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_re_routes()
|
||||
{
|
||||
var fileConfig = new FileConfiguration
|
||||
{
|
||||
DynamicReRoutes = new List<FileDynamicReRoute>
|
||||
{
|
||||
new FileDynamicReRoute
|
||||
{
|
||||
ServiceName = "1",
|
||||
RateLimitRule = new FileRateLimitRule
|
||||
{
|
||||
EnableRateLimiting = false
|
||||
}
|
||||
},
|
||||
new FileDynamicReRoute
|
||||
{
|
||||
ServiceName = "2",
|
||||
RateLimitRule = new FileRateLimitRule
|
||||
{
|
||||
EnableRateLimiting = true
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(_ => GivenThe(fileConfig))
|
||||
.And(_ => GivenTheRloCreatorReturns())
|
||||
.When(_ => WhenICreate())
|
||||
.Then(_ => ThenTheReRoutesAreReturned())
|
||||
.And(_ => ThenTheRloCreatorIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheRloCreatorIsCalledCorrectly()
|
||||
{
|
||||
_rloCreator.Verify(x => x.Create(_fileConfig.DynamicReRoutes[0].RateLimitRule,
|
||||
_fileConfig.GlobalConfiguration), Times.Once);
|
||||
|
||||
_rloCreator.Verify(x => x.Create(_fileConfig.DynamicReRoutes[1].RateLimitRule,
|
||||
_fileConfig.GlobalConfiguration), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenTheReRoutesAreReturned()
|
||||
{
|
||||
_result.Count.ShouldBe(2);
|
||||
_result[0].DownstreamReRoute[0].EnableEndpointEndpointRateLimiting.ShouldBeFalse();
|
||||
_result[0].DownstreamReRoute[0].RateLimitOptions.ShouldBe(_rlo1);
|
||||
_result[0].DownstreamReRoute[0].ServiceName.ShouldBe(_fileConfig.DynamicReRoutes[0].ServiceName);
|
||||
|
||||
_result[1].DownstreamReRoute[0].EnableEndpointEndpointRateLimiting.ShouldBeTrue();
|
||||
_result[1].DownstreamReRoute[0].RateLimitOptions.ShouldBe(_rlo2);
|
||||
_result[1].DownstreamReRoute[0].ServiceName.ShouldBe(_fileConfig.DynamicReRoutes[1].ServiceName);
|
||||
}
|
||||
|
||||
private void GivenTheRloCreatorReturns()
|
||||
{
|
||||
_rlo1 = new RateLimitOptionsBuilder().Build();
|
||||
_rlo2 = new RateLimitOptionsBuilder().WithEnableRateLimiting(true).Build();
|
||||
|
||||
_rloCreator
|
||||
.SetupSequence(x => x.Create(It.IsAny<FileRateLimitRule>(), It.IsAny<FileGlobalConfiguration>()))
|
||||
.Returns(_rlo1)
|
||||
.Returns(_rlo2);
|
||||
}
|
||||
|
||||
private void ThenTheRloCreatorIsNotCalled()
|
||||
{
|
||||
_rloCreator.Verify(x => x.Create(It.IsAny<FileRateLimitRule>(), It.IsAny<FileGlobalConfiguration>()), Times.Never);
|
||||
}
|
||||
|
||||
private void ThenNothingIsReturned()
|
||||
{
|
||||
_result.Count.ShouldBe(0);
|
||||
}
|
||||
|
||||
private void WhenICreate()
|
||||
{
|
||||
_result = _creator.Create(_fileConfig);
|
||||
}
|
||||
|
||||
private void GivenThe(FileConfiguration fileConfig)
|
||||
{
|
||||
_fileConfig = fileConfig;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user