using System; using System.Collections.Generic; using System.Diagnostics; using Moq; using Ocelot.Configuration.File; using Ocelot.Configuration.Repository; using Ocelot.Configuration.Setter; using Ocelot.Logging; using Ocelot.Responses; using TestStack.BDDfy; using Xunit; using Shouldly; using static Ocelot.UnitTests.Wait; namespace Ocelot.UnitTests.Configuration { public class ConsulFileConfigurationPollerTests : IDisposable { private ConsulFileConfigurationPoller _poller; private Mock _factory; private Mock _repo; private Mock _setter; private FileConfiguration _fileConfig; public ConsulFileConfigurationPollerTests() { var logger = new Mock(); _factory = new Mock(); _factory.Setup(x => x.CreateLogger()).Returns(logger.Object); _repo = new Mock(); _setter = new Mock(); _fileConfig = new FileConfiguration(); _repo.Setup(x => x.Get()).ReturnsAsync(new OkResponse(_fileConfig)); _poller = new ConsulFileConfigurationPoller(_factory.Object, _repo.Object, _setter.Object); } public void Dispose() { _poller.Dispose(); } [Fact] public void should_start() { this.Given(x => ThenTheSetterIsCalled(_fileConfig)) .BDDfy(); } [Fact] public void should_call_setter_when_gets_new_config() { var newConfig = new FileConfiguration { ReRoutes = new List { new FileReRoute { DownstreamHostAndPorts = new List { new FileHostAndPort { Host = "test" } }, } } }; this.Given(x => WhenTheConfigIsChangedInConsul(newConfig)) .Then(x => ThenTheSetterIsCalled(newConfig)) .BDDfy(); } private void WhenTheConfigIsChangedInConsul(FileConfiguration newConfig) { _repo.Setup(x => x.Get()).ReturnsAsync(new OkResponse(newConfig)); } private void ThenTheSetterIsCalled(FileConfiguration fileConfig) { var result = WaitFor(2000).Until(() => { try { _setter.Verify(x => x.Set(fileConfig), Times.Once); return true; } catch(Exception ex) { return false; } }); result.ShouldBeTrue(); } } }