using System.Collections.Generic; using Moq; using Ocelot.Configuration; using Ocelot.Configuration.Builder; using Ocelot.Configuration.Creator; using Ocelot.Configuration.Provider; using Ocelot.Configuration.Repository; using Ocelot.Errors; using Ocelot.Responses; using Shouldly; using TestStack.BDDfy; using Xunit; namespace Ocelot.UnitTests.Configuration { public class OcelotConfigurationProviderTests { private readonly IOcelotConfigurationProvider _ocelotConfigurationProvider; private readonly Mock _configurationRepository; private Response _result; public OcelotConfigurationProviderTests() { _configurationRepository = new Mock(); _ocelotConfigurationProvider = new OcelotConfigurationProvider(_configurationRepository.Object); } [Fact] public void should_get_config() { var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build(); this.Given(x => x.GivenTheRepoReturns(new OkResponse(new OcelotConfiguration(new List(), string.Empty, serviceProviderConfig, "")))) .When(x => x.WhenIGetTheConfig()) .Then(x => x.TheFollowingIsReturned(new OkResponse(new OcelotConfiguration(new List(), string.Empty, serviceProviderConfig, "")))) .BDDfy(); } [Fact] public void should_return_error() { this.Given(x => x.GivenTheRepoReturns(new ErrorResponse(new List { new AnyError() }))) .When(x => x.WhenIGetTheConfig()) .Then(x => x.TheFollowingIsReturned( new ErrorResponse(new List { new AnyError() }))) .BDDfy(); } private void GivenTheRepoReturns(Response config) { _configurationRepository .Setup(x => x.Get()) .ReturnsAsync(config); } private void WhenIGetTheConfig() { _result = _ocelotConfigurationProvider.Get().Result; } private void TheFollowingIsReturned(Response expected) { _result.IsError.ShouldBe(expected.IsError); } class AnyError : Error { public AnyError() : base("blamo", OcelotErrorCode.UnknownError) { } } } }