mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 11:38:15 +08:00
refactoring configuration code so its not so crazy, still need to work on the creator class
This commit is contained in:
@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Library.Configuration;
|
||||
using Ocelot.Library.Configuration.Builder;
|
||||
using Ocelot.Library.Configuration.Repository;
|
||||
using Ocelot.Library.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class InMemoryConfigurationRepositoryTests
|
||||
{
|
||||
private readonly InMemoryOcelotConfigurationRepository _repo;
|
||||
private IOcelotConfiguration _config;
|
||||
private Response _result;
|
||||
private Response<IOcelotConfiguration> _getResult;
|
||||
|
||||
public InMemoryConfigurationRepositoryTests()
|
||||
{
|
||||
_repo = new InMemoryOcelotConfigurationRepository();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_add_config()
|
||||
{
|
||||
this.Given(x => x.GivenTheConfigurationIs(new FakeConfig("initial")))
|
||||
.When(x => x.WhenIAddOrReplaceTheConfig())
|
||||
.Then(x => x.ThenNoErrorsAreReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_get_config()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsASavedConfiguration())
|
||||
.When(x => x.WhenIGetTheConfiguration())
|
||||
.Then(x => x.ThenTheConfigurationIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// long runnnig unit test to make sure repo thread safeok on
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void repo_is_thread_safe()
|
||||
{
|
||||
var tasks = new Task[100000];
|
||||
for (int i = 0; i < tasks.Length; i++)
|
||||
{
|
||||
tasks[i] = Fire();
|
||||
}
|
||||
|
||||
Task.WaitAll(tasks);
|
||||
}
|
||||
|
||||
private async Task Fire()
|
||||
{
|
||||
var taskGuid = Guid.NewGuid().ToString();
|
||||
_repo.AddOrReplace(new FakeConfig(taskGuid));
|
||||
var configuration = _repo.Get();
|
||||
configuration.Data.ReRoutes[0].DownstreamTemplate.ShouldBe(taskGuid);
|
||||
}
|
||||
|
||||
private void ThenTheConfigurationIsReturned()
|
||||
{
|
||||
_getResult.Data.ReRoutes[0].DownstreamTemplate.ShouldBe("initial");
|
||||
}
|
||||
|
||||
private void WhenIGetTheConfiguration()
|
||||
{
|
||||
_getResult = _repo.Get();
|
||||
}
|
||||
|
||||
private void GivenThereIsASavedConfiguration()
|
||||
{
|
||||
GivenTheConfigurationIs(new FakeConfig("initial"));
|
||||
WhenIAddOrReplaceTheConfig();
|
||||
}
|
||||
|
||||
private void GivenTheConfigurationIs(IOcelotConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
}
|
||||
|
||||
private void WhenIAddOrReplaceTheConfig()
|
||||
{
|
||||
_result = _repo.AddOrReplace(_config);
|
||||
}
|
||||
|
||||
private void ThenNoErrorsAreReturned()
|
||||
{
|
||||
_result.IsError.ShouldBeFalse();
|
||||
}
|
||||
|
||||
class FakeConfig : IOcelotConfiguration
|
||||
{
|
||||
private readonly string _downstreamTemplate;
|
||||
|
||||
public FakeConfig(string downstreamTemplate)
|
||||
{
|
||||
_downstreamTemplate = downstreamTemplate;
|
||||
}
|
||||
|
||||
public List<ReRoute> ReRoutes => new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder().WithDownstreamTemplate(_downstreamTemplate).Build()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -2,6 +2,9 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using Ocelot.Library.Configuration.Builder;
|
||||
using Ocelot.Library.Configuration.Parser;
|
||||
using Ocelot.Library.Configuration.Repository;
|
||||
using Ocelot.Library.RequestBuilder;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
@ -9,26 +12,28 @@ using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
using Library.Builder;
|
||||
using Library.Configuration;
|
||||
using Library.Configuration.Yaml;
|
||||
using Library.Responses;
|
||||
|
||||
public class OcelotConfigurationTests
|
||||
public class YamlConfigurationCreatorTests
|
||||
{
|
||||
private readonly Mock<IOptions<YamlConfiguration>> _yamlConfig;
|
||||
private readonly Mock<IConfigurationValidator> _validator;
|
||||
private OcelotConfiguration _config;
|
||||
private Response<IOcelotConfiguration> _config;
|
||||
private YamlConfiguration _yamlConfiguration;
|
||||
private readonly Mock<IClaimToHeaderConfigurationParser> _configExtractor;
|
||||
private readonly Mock<ILogger<OcelotConfiguration>> _logger;
|
||||
private readonly Mock<IClaimToHeaderConfigurationParser> _configParser;
|
||||
private readonly Mock<ILogger<YamlOcelotConfigurationCreator>> _logger;
|
||||
private readonly YamlOcelotConfigurationCreator _ocelotConfigurationCreator;
|
||||
|
||||
public OcelotConfigurationTests()
|
||||
public YamlConfigurationCreatorTests()
|
||||
{
|
||||
_logger = new Mock<ILogger<OcelotConfiguration>>();
|
||||
_configExtractor = new Mock<IClaimToHeaderConfigurationParser>();
|
||||
_logger = new Mock<ILogger<YamlOcelotConfigurationCreator>>();
|
||||
_configParser = new Mock<IClaimToHeaderConfigurationParser>();
|
||||
_validator = new Mock<IConfigurationValidator>();
|
||||
_yamlConfig = new Mock<IOptions<YamlConfiguration>>();
|
||||
_ocelotConfigurationCreator = new YamlOcelotConfigurationCreator(
|
||||
_yamlConfig.Object, _validator.Object, _configParser.Object, _logger.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -47,7 +52,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheYamlConfigIsValid())
|
||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
@ -109,7 +114,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
}))
|
||||
.And(x => x.GivenTheYamlConfigIsValid())
|
||||
.And(x => x.GivenTheConfigHeaderExtractorReturns(new ClaimToHeader("CustomerId", "CustomerId", "", 0)))
|
||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
.Then(x => x.ThenTheReRoutesAre(expected))
|
||||
.And(x => x.ThenTheAuthenticationOptionsAre(expected))
|
||||
.BDDfy();
|
||||
@ -117,7 +122,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
|
||||
private void GivenTheConfigHeaderExtractorReturns(ClaimToHeader expected)
|
||||
{
|
||||
_configExtractor
|
||||
_configParser
|
||||
.Setup(x => x.Extract(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(new OkResponse<ClaimToHeader>(expected));
|
||||
}
|
||||
@ -162,7 +167,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheYamlConfigIsValid())
|
||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
.Then(x => x.ThenTheReRoutesAre(expected))
|
||||
.And(x => x.ThenTheAuthenticationOptionsAre(expected))
|
||||
.BDDfy();
|
||||
@ -184,7 +189,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheYamlConfigIsValid())
|
||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
@ -213,7 +218,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheYamlConfigIsValid())
|
||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
@ -242,7 +247,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheYamlConfigIsValid())
|
||||
.When(x => x.WhenIInstanciateTheOcelotConfig())
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
@ -270,17 +275,16 @@ namespace Ocelot.UnitTests.Configuration
|
||||
.Returns(_yamlConfiguration);
|
||||
}
|
||||
|
||||
private void WhenIInstanciateTheOcelotConfig()
|
||||
private void WhenICreateTheConfig()
|
||||
{
|
||||
_config = new OcelotConfiguration(_yamlConfig.Object, _validator.Object,
|
||||
_configExtractor.Object, _logger.Object);
|
||||
_config = _ocelotConfigurationCreator.Create();
|
||||
}
|
||||
|
||||
private void ThenTheReRoutesAre(List<ReRoute> expectedReRoutes)
|
||||
{
|
||||
for (int i = 0; i < _config.ReRoutes.Count; i++)
|
||||
for (int i = 0; i < _config.Data.ReRoutes.Count; i++)
|
||||
{
|
||||
var result = _config.ReRoutes[i];
|
||||
var result = _config.Data.ReRoutes[i];
|
||||
var expected = expectedReRoutes[i];
|
||||
|
||||
result.DownstreamTemplate.ShouldBe(expected.DownstreamTemplate);
|
||||
@ -292,9 +296,9 @@ namespace Ocelot.UnitTests.Configuration
|
||||
|
||||
private void ThenTheAuthenticationOptionsAre(List<ReRoute> expectedReRoutes)
|
||||
{
|
||||
for (int i = 0; i < _config.ReRoutes.Count; i++)
|
||||
for (int i = 0; i < _config.Data.ReRoutes.Count; i++)
|
||||
{
|
||||
var result = _config.ReRoutes[i].AuthenticationOptions;
|
||||
var result = _config.Data.ReRoutes[i].AuthenticationOptions;
|
||||
var expected = expectedReRoutes[i].AuthenticationOptions;
|
||||
|
||||
result.AdditionalScopes.ShouldBe(expected.AdditionalScopes);
|
@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Moq;
|
||||
using Ocelot.Library.Configuration;
|
||||
using Ocelot.Library.Configuration.Creator;
|
||||
using Ocelot.Library.Configuration.Provider;
|
||||
using Ocelot.Library.Configuration.Repository;
|
||||
using Ocelot.Library.Configuration.Yaml;
|
||||
using Ocelot.Library.Errors;
|
||||
using Ocelot.Library.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class YamlConfigurationProviderTests
|
||||
{
|
||||
private readonly IOcelotConfigurationProvider _ocelotConfigurationProvider;
|
||||
private readonly Mock<IOcelotConfigurationRepository> _configurationRepository;
|
||||
private readonly Mock<IOcelotConfigurationCreator> _creator;
|
||||
private Response<IOcelotConfiguration> _result;
|
||||
|
||||
public YamlConfigurationProviderTests()
|
||||
{
|
||||
_creator = new Mock<IOcelotConfigurationCreator>();
|
||||
_configurationRepository = new Mock<IOcelotConfigurationRepository>();
|
||||
_ocelotConfigurationProvider = new YamlOcelotConfigurationProvider(_configurationRepository.Object, _creator.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_config()
|
||||
{
|
||||
this.Given(x => x.GivenTheRepoReturns(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(new List<ReRoute>()))))
|
||||
.When(x => x.WhenIGetTheConfig())
|
||||
.Then(x => x.TheFollowingIsReturned(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(new List<ReRoute>()))))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_config_if_it_doesnt_exist()
|
||||
{
|
||||
this.Given(x => x.GivenTheRepoReturns(new OkResponse<IOcelotConfiguration>(null)))
|
||||
.And(x => x.GivenTheCreatorReturns(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(new List<ReRoute>()))))
|
||||
.When(x => x.WhenIGetTheConfig())
|
||||
.Then(x => x.TheFollowingIsReturned(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(new List<ReRoute>()))))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
{
|
||||
this.Given(x => x.GivenTheRepoReturns(new ErrorResponse<IOcelotConfiguration>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.When(x => x.WhenIGetTheConfig())
|
||||
.Then(x => x.TheFollowingIsReturned(
|
||||
new ErrorResponse<IOcelotConfiguration>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_if_creator_errors()
|
||||
{
|
||||
this.Given(x => x.GivenTheRepoReturns(new OkResponse<IOcelotConfiguration>(null)))
|
||||
.And(x => x.GivenTheCreatorReturns(new ErrorResponse<IOcelotConfiguration>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.When(x => x.WhenIGetTheConfig())
|
||||
.Then(x => x.TheFollowingIsReturned(new ErrorResponse<IOcelotConfiguration>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheCreatorReturns(Response<IOcelotConfiguration> config)
|
||||
{
|
||||
_creator
|
||||
.Setup(x => x.Create())
|
||||
.Returns(config);
|
||||
}
|
||||
|
||||
private void GivenTheRepoReturns(Response<IOcelotConfiguration> config)
|
||||
{
|
||||
_configurationRepository
|
||||
.Setup(x => x.Get())
|
||||
.Returns(config);
|
||||
}
|
||||
|
||||
private void WhenIGetTheConfig()
|
||||
{
|
||||
_result = _ocelotConfigurationProvider.Get();
|
||||
}
|
||||
|
||||
private void TheFollowingIsReturned(Response<IOcelotConfiguration> expected)
|
||||
{
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
}
|
||||
|
||||
class AnyError : Error
|
||||
{
|
||||
public AnyError()
|
||||
: base("blamo", OcelotErrorCode.UnknownError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user