using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Moq; using Ocelot.Configuration; 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 FileConfigurationProviderTests { private readonly IOcelotConfigurationProvider _ocelotConfigurationProvider; private readonly Mock _configurationRepository; private readonly Mock _creator; private Response _result; public FileConfigurationProviderTests() { _creator = new Mock(); _configurationRepository = new Mock(); _ocelotConfigurationProvider = new OcelotConfigurationProvider(_configurationRepository.Object, _creator.Object); } [Fact] public void should_get_config() { this.Given(x => x.GivenTheRepoReturns(new OkResponse(new OcelotConfiguration(new List())))) .When(x => x.WhenIGetTheConfig()) .Then(x => x.TheFollowingIsReturned(new OkResponse(new OcelotConfiguration(new List())))) .BDDfy(); } [Fact] public void should_create_config_if_it_doesnt_exist() { this.Given(x => x.GivenTheRepoReturns(new OkResponse(null))) .And(x => x.GivenTheCreatorReturns(new OkResponse(new OcelotConfiguration(new List())))) .When(x => x.WhenIGetTheConfig()) .Then(x => x.TheFollowingIsReturned(new OkResponse(new OcelotConfiguration(new List())))) .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(); } [Fact] public void should_return_error_if_creator_errors() { this.Given(x => x.GivenTheRepoReturns(new OkResponse(null))) .And(x => x.GivenTheCreatorReturns(new ErrorResponse(new List { new AnyError() }))) .When(x => x.WhenIGetTheConfig()) .Then(x => x.TheFollowingIsReturned(new ErrorResponse(new List { new AnyError() }))) .BDDfy(); } private void GivenTheCreatorReturns(Response config) { _creator .Setup(x => x.Create()) .Returns(config); } private void GivenTheRepoReturns(Response config) { _configurationRepository .Setup(x => x.Get()) .Returns(config); } private void WhenIGetTheConfig() { _result = _ocelotConfigurationProvider.Get(); } private void TheFollowingIsReturned(Response expected) { _result.IsError.ShouldBe(expected.IsError); } class AnyError : Error { public AnyError() : base("blamo", OcelotErrorCode.UnknownError) { } } } }