namespace Ocelot.UnitTests.Configuration { using Microsoft.Extensions.DependencyInjection; using Moq; using Ocelot.Configuration; using Ocelot.Configuration.Creator; using Ocelot.Configuration.File; using Ocelot.DependencyInjection; using Shouldly; using System.Collections.Generic; using TestStack.BDDfy; using Xunit; public class ConfigurationCreatorTests { private ConfigurationCreator _creator; private InternalConfiguration _result; private readonly Mock _spcCreator; private readonly Mock _qosCreator; private readonly Mock _hhoCreator; private readonly Mock _lboCreator; private readonly Mock _vCreator; private FileConfiguration _fileConfig; private List _routes; private ServiceProviderConfiguration _spc; private LoadBalancerOptions _lbo; private QoSOptions _qoso; private HttpHandlerOptions _hho; private AdministrationPath _adminPath; private readonly ServiceCollection _serviceCollection; public ConfigurationCreatorTests() { _vCreator = new Mock(); _lboCreator = new Mock(); _hhoCreator = new Mock(); _qosCreator = new Mock(); _spcCreator = new Mock(); _serviceCollection = new ServiceCollection(); } [Fact] public void should_build_configuration_with_no_admin_path() { this.Given(_ => GivenTheDependenciesAreSetUp()) .When(_ => WhenICreate()) .Then(_ => ThenTheDepdenciesAreCalledCorrectly()) .And(_ => ThenThePropertiesAreSetCorrectly()) .And(_ => ThenTheAdminPathIsNull()) .BDDfy(); } [Fact] public void should_build_configuration_with_admin_path() { this.Given(_ => GivenTheDependenciesAreSetUp()) .And(_ => GivenTheAdminPath()) .When(_ => WhenICreate()) .Then(_ => ThenTheDepdenciesAreCalledCorrectly()) .And(_ => ThenThePropertiesAreSetCorrectly()) .And(_ => ThenTheAdminPathIsSet()) .BDDfy(); } private void ThenTheAdminPathIsNull() { _result.AdministrationPath.ShouldBeNull(); } private void ThenThePropertiesAreSetCorrectly() { _result.ShouldNotBeNull(); _result.ServiceProviderConfiguration.ShouldBe(_spc); _result.LoadBalancerOptions.ShouldBe(_lbo); _result.QoSOptions.ShouldBe(_qoso); _result.HttpHandlerOptions.ShouldBe(_hho); _result.Routes.ShouldBe(_routes); _result.RequestId.ShouldBe(_fileConfig.GlobalConfiguration.RequestIdKey); _result.DownstreamScheme.ShouldBe(_fileConfig.GlobalConfiguration.DownstreamScheme); } private void ThenTheAdminPathIsSet() { _result.AdministrationPath.ShouldBe("wooty"); } private void ThenTheDepdenciesAreCalledCorrectly() { _spcCreator.Verify(x => x.Create(_fileConfig.GlobalConfiguration), Times.Once); _lboCreator.Verify(x => x.Create(_fileConfig.GlobalConfiguration.LoadBalancerOptions), Times.Once); _qosCreator.Verify(x => x.Create(_fileConfig.GlobalConfiguration.QoSOptions), Times.Once); _hhoCreator.Verify(x => x.Create(_fileConfig.GlobalConfiguration.HttpHandlerOptions), Times.Once); } private void GivenTheAdminPath() { _adminPath = new AdministrationPath("wooty"); _serviceCollection.AddSingleton(_adminPath); } private void GivenTheDependenciesAreSetUp() { _fileConfig = new FileConfiguration { GlobalConfiguration = new FileGlobalConfiguration() }; _routes = new List(); _spc = new ServiceProviderConfiguration("", "", "", 1, "", "", 1); _lbo = new LoadBalancerOptionsBuilder().Build(); _qoso = new QoSOptions(1, 1, 1, ""); _hho = new HttpHandlerOptionsBuilder().Build(); _spcCreator.Setup(x => x.Create(It.IsAny())).Returns(_spc); _lboCreator.Setup(x => x.Create(It.IsAny())).Returns(_lbo); _qosCreator.Setup(x => x.Create(It.IsAny())).Returns(_qoso); _hhoCreator.Setup(x => x.Create(It.IsAny())).Returns(_hho); } private void WhenICreate() { var serviceProvider = _serviceCollection.BuildServiceProvider(); _creator = new ConfigurationCreator(_spcCreator.Object, _qosCreator.Object, _hhoCreator.Object, serviceProvider, _lboCreator.Object, _vCreator.Object); _result = _creator.Create(_fileConfig, _routes); } } }