From aa0d8fe59a6e41226f6ed4d9b827fa5f9b3dc6fe Mon Sep 17 00:00:00 2001 From: Tom Gardham-Pallister Date: Wed, 22 Feb 2017 07:48:49 +0000 Subject: [PATCH] all tests passing, now to do authentication config provider --- configuration.json | 1 + .../File/FileGlobalConfiguration.cs | 4 +- .../File/FileServiceDiscoveryProvider.cs | 1 + .../Provider/FileConfigurationProvider.cs | 25 +++++ .../Provider}/IFileConfigurationProvider.cs | 2 +- .../Repository/FileConfigurationRepository.cs | 42 ++++++++ .../IFileConfigurationRepository.cs | 11 +++ .../Setter/FileConfigurationSetter.cs | 12 ++- .../FileConfigurationController.cs | 4 +- .../ServiceCollectionExtensions.cs | 4 +- .../Services/FileConfigurationProvider.cs | 19 ---- .../AdministrationTests.cs | 95 ++++++++++++++++++- .../configuration.json | 0 .../FileConfigurationProviderTests.cs | 75 ++++++--------- .../FileConfigurationRepositoryTests.cs} | 78 +++++++++++++-- .../FileConfigurationSetterTests.cs | 29 +++++- .../OcelotConfigurationProviderTests.cs | 77 +++++++++++++++ .../FileConfigurationControllerTests.cs | 2 +- 18 files changed, 399 insertions(+), 82 deletions(-) create mode 100755 configuration.json create mode 100644 src/Ocelot/Configuration/Provider/FileConfigurationProvider.cs rename src/Ocelot/{Services => Configuration/Provider}/IFileConfigurationProvider.cs (80%) create mode 100644 src/Ocelot/Configuration/Repository/FileConfigurationRepository.cs create mode 100644 src/Ocelot/Configuration/Repository/IFileConfigurationRepository.cs delete mode 100644 src/Ocelot/Services/FileConfigurationProvider.cs mode change 100644 => 100755 test/Ocelot.IntegrationTests/configuration.json rename test/Ocelot.UnitTests/{Services/FileConfigurationGetterTests.cs => Configuration/FileConfigurationRepositoryTests.cs} (51%) create mode 100644 test/Ocelot.UnitTests/Configuration/OcelotConfigurationProviderTests.cs diff --git a/configuration.json b/configuration.json new file mode 100755 index 00000000..a2784d8c --- /dev/null +++ b/configuration.json @@ -0,0 +1 @@ +{"ReRoutes":[{"DownstreamPathTemplate":"/","UpstreamPathTemplate":"/","UpstreamHttpMethod":"get","AuthenticationOptions":{"Provider":null,"ProviderRootUrl":null,"ScopeName":null,"RequireHttps":false,"AdditionalScopes":[],"ScopeSecret":null},"AddHeadersToRequest":{},"AddClaimsToRequest":{},"RouteClaimsRequirement":{},"AddQueriesToRequest":{},"RequestIdKey":null,"FileCacheOptions":{"TtlSeconds":0},"ReRouteIsCaseSensitive":false,"ServiceName":null,"DownstreamScheme":"https","DownstreamHost":"localhost","DownstreamPort":80,"QoSOptions":{"ExceptionsAllowedBeforeBreaking":0,"DurationOfBreak":0,"TimeoutValue":0},"LoadBalancer":null},{"DownstreamPathTemplate":"/","UpstreamPathTemplate":"/test","UpstreamHttpMethod":"get","AuthenticationOptions":{"Provider":null,"ProviderRootUrl":null,"ScopeName":null,"RequireHttps":false,"AdditionalScopes":[],"ScopeSecret":null},"AddHeadersToRequest":{},"AddClaimsToRequest":{},"RouteClaimsRequirement":{},"AddQueriesToRequest":{},"RequestIdKey":null,"FileCacheOptions":{"TtlSeconds":0},"ReRouteIsCaseSensitive":false,"ServiceName":null,"DownstreamScheme":"https","DownstreamHost":"localhost","DownstreamPort":80,"QoSOptions":{"ExceptionsAllowedBeforeBreaking":0,"DurationOfBreak":0,"TimeoutValue":0},"LoadBalancer":null}],"GlobalConfiguration":{"RequestIdKey":"RequestId","ServiceDiscoveryProvider":{"Provider":null,"Host":null,"Port":0},"AdministrationPath":"/administration"}} \ No newline at end of file diff --git a/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs b/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs index fd0f41a9..95e56f95 100644 --- a/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs +++ b/src/Ocelot/Configuration/File/FileGlobalConfiguration.cs @@ -1,4 +1,5 @@ -namespace Ocelot.Configuration.File + +namespace Ocelot.Configuration.File { public class FileGlobalConfiguration { @@ -6,6 +7,7 @@ { ServiceDiscoveryProvider = new FileServiceDiscoveryProvider(); } + public string RequestIdKey { get; set; } public FileServiceDiscoveryProvider ServiceDiscoveryProvider {get;set;} public string AdministrationPath {get;set;} diff --git a/src/Ocelot/Configuration/File/FileServiceDiscoveryProvider.cs b/src/Ocelot/Configuration/File/FileServiceDiscoveryProvider.cs index 2f26b6ea..70a3c42f 100644 --- a/src/Ocelot/Configuration/File/FileServiceDiscoveryProvider.cs +++ b/src/Ocelot/Configuration/File/FileServiceDiscoveryProvider.cs @@ -2,6 +2,7 @@ namespace Ocelot.Configuration.File { public class FileServiceDiscoveryProvider { + public string Provider {get;set;} public string Host {get;set;} public int Port { get; set; } diff --git a/src/Ocelot/Configuration/Provider/FileConfigurationProvider.cs b/src/Ocelot/Configuration/Provider/FileConfigurationProvider.cs new file mode 100644 index 00000000..5f6dbe08 --- /dev/null +++ b/src/Ocelot/Configuration/Provider/FileConfigurationProvider.cs @@ -0,0 +1,25 @@ +using System; +using System.IO; +using Newtonsoft.Json; +using Ocelot.Configuration.File; +using Ocelot.Configuration.Repository; +using Ocelot.Responses; + +namespace Ocelot.Configuration.Provider +{ + public class FileConfigurationProvider : IFileConfigurationProvider + { + private IFileConfigurationRepository _repo; + + public FileConfigurationProvider(IFileConfigurationRepository repo) + { + _repo = repo; + } + + public Response Get() + { + var fileConfig = _repo.Get(); + return new OkResponse(fileConfig.Data); + } + } +} \ No newline at end of file diff --git a/src/Ocelot/Services/IFileConfigurationProvider.cs b/src/Ocelot/Configuration/Provider/IFileConfigurationProvider.cs similarity index 80% rename from src/Ocelot/Services/IFileConfigurationProvider.cs rename to src/Ocelot/Configuration/Provider/IFileConfigurationProvider.cs index 725f7463..3a4dca14 100644 --- a/src/Ocelot/Services/IFileConfigurationProvider.cs +++ b/src/Ocelot/Configuration/Provider/IFileConfigurationProvider.cs @@ -1,7 +1,7 @@ using Ocelot.Configuration.File; using Ocelot.Responses; -namespace Ocelot.Services +namespace Ocelot.Configuration.Provider { public interface IFileConfigurationProvider { diff --git a/src/Ocelot/Configuration/Repository/FileConfigurationRepository.cs b/src/Ocelot/Configuration/Repository/FileConfigurationRepository.cs new file mode 100644 index 00000000..f32dcaec --- /dev/null +++ b/src/Ocelot/Configuration/Repository/FileConfigurationRepository.cs @@ -0,0 +1,42 @@ +using System; +using Newtonsoft.Json; +using Ocelot.Configuration.File; +using Ocelot.Responses; + +namespace Ocelot.Configuration.Repository +{ + public class FileConfigurationRepository : IFileConfigurationRepository + { + private static readonly object _lock = new object(); + public Response Get() + { + var configFilePath = $"{AppContext.BaseDirectory}/configuration.json"; + string json = string.Empty; + lock(_lock) + { + json = System.IO.File.ReadAllText(configFilePath); + } + var fileConfiguration = JsonConvert.DeserializeObject(json); + return new OkResponse(fileConfiguration); + } + + public Response Set(FileConfiguration fileConfiguration) + { + var configurationPath = $"{AppContext.BaseDirectory}/configuration.json"; + + var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration); + + lock(_lock) + { + if (System.IO.File.Exists(configurationPath)) + { + System.IO.File.Delete(configurationPath); + } + + System.IO.File.WriteAllText(configurationPath, jsonConfiguration); + } + + return new OkResponse(); + } + } +} \ No newline at end of file diff --git a/src/Ocelot/Configuration/Repository/IFileConfigurationRepository.cs b/src/Ocelot/Configuration/Repository/IFileConfigurationRepository.cs new file mode 100644 index 00000000..26726b46 --- /dev/null +++ b/src/Ocelot/Configuration/Repository/IFileConfigurationRepository.cs @@ -0,0 +1,11 @@ +using Ocelot.Configuration.File; +using Ocelot.Responses; + +namespace Ocelot.Configuration.Repository +{ + public interface IFileConfigurationRepository + { + Response Get(); + Response Set(FileConfiguration fileConfiguration); + } +} \ No newline at end of file diff --git a/src/Ocelot/Configuration/Setter/FileConfigurationSetter.cs b/src/Ocelot/Configuration/Setter/FileConfigurationSetter.cs index 28957cc7..93dd4d85 100644 --- a/src/Ocelot/Configuration/Setter/FileConfigurationSetter.cs +++ b/src/Ocelot/Configuration/Setter/FileConfigurationSetter.cs @@ -10,15 +10,25 @@ namespace Ocelot.Configuration.Setter { private readonly IOcelotConfigurationRepository _configRepo; private readonly IOcelotConfigurationCreator _configCreator; + private readonly IFileConfigurationRepository _repo; - public FileConfigurationSetter(IOcelotConfigurationRepository configRepo, IOcelotConfigurationCreator configCreator) + public FileConfigurationSetter(IOcelotConfigurationRepository configRepo, + IOcelotConfigurationCreator configCreator, IFileConfigurationRepository repo) { _configRepo = configRepo; _configCreator = configCreator; + _repo = repo; } public async Task Set(FileConfiguration fileConfig) { + var response = _repo.Set(fileConfig); + + if(response.IsError) + { + return new ErrorResponse(response.Errors); + } + var config = await _configCreator.Create(fileConfig); if(!config.IsError) diff --git a/src/Ocelot/Controllers/FileConfigurationController.cs b/src/Ocelot/Controllers/FileConfigurationController.cs index a378f530..5e36c64b 100644 --- a/src/Ocelot/Controllers/FileConfigurationController.cs +++ b/src/Ocelot/Controllers/FileConfigurationController.cs @@ -2,8 +2,8 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Ocelot.Configuration.File; +using Ocelot.Configuration.Provider; using Ocelot.Configuration.Setter; -using Ocelot.Services; namespace Ocelot.Controllers { @@ -34,7 +34,7 @@ namespace Ocelot.Controllers } [HttpPost] - public async Task Post(FileConfiguration fileConfiguration) + public async Task Post([FromBody]FileConfiguration fileConfiguration) { var response = await _configSetter.Set(fileConfiguration); diff --git a/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs b/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs index 19b13e31..fdc78bd1 100644 --- a/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs @@ -34,7 +34,6 @@ using Ocelot.Requester; using Ocelot.Requester.QoS; using Ocelot.Responder; using Ocelot.ServiceDiscovery; -using Ocelot.Services; namespace Ocelot.DependencyInjection { @@ -112,8 +111,9 @@ namespace Ocelot.DependencyInjection .AddAuthorization() .AddJsonFormatters(); services.AddLogging(); + services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/src/Ocelot/Services/FileConfigurationProvider.cs b/src/Ocelot/Services/FileConfigurationProvider.cs deleted file mode 100644 index abac93f9..00000000 --- a/src/Ocelot/Services/FileConfigurationProvider.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.IO; -using Newtonsoft.Json; -using Ocelot.Configuration.File; -using Ocelot.Responses; - -namespace Ocelot.Services -{ - public class FileConfigurationProvider : IFileConfigurationProvider - { - public Response Get() - { - var configFilePath = $"{AppContext.BaseDirectory}/configuration.json"; - var json = File.ReadAllText(configFilePath); - var fileConfiguration = JsonConvert.DeserializeObject(json); - return new OkResponse(fileConfiguration); - } - } -} \ No newline at end of file diff --git a/test/Ocelot.IntegrationTests/AdministrationTests.cs b/test/Ocelot.IntegrationTests/AdministrationTests.cs index 6ea1aa32..6661a2cc 100644 --- a/test/Ocelot.IntegrationTests/AdministrationTests.cs +++ b/test/Ocelot.IntegrationTests/AdministrationTests.cs @@ -74,7 +74,14 @@ namespace Ocelot.IntegrationTests { GlobalConfiguration = new FileGlobalConfiguration { - AdministrationPath = "/administration" + AdministrationPath = "/administration", + RequestIdKey = "RequestId", + ServiceDiscoveryProvider = new FileServiceDiscoveryProvider + { + Host = "127.0.0.1", + Provider = "test" + } + }, ReRoutes = new List() { @@ -109,6 +116,88 @@ namespace Ocelot.IntegrationTests .BDDfy(); } + [Fact] + public void should_get_file_configuration_edit_and_post_updated_version() + { + var initialConfiguration = new FileConfiguration + { + GlobalConfiguration = new FileGlobalConfiguration + { + AdministrationPath = "/administration" + }, + ReRoutes = new List() + { + new FileReRoute() + { + DownstreamHost = "localhost", + DownstreamPort = 80, + DownstreamScheme = "https", + DownstreamPathTemplate = "/", + UpstreamHttpMethod = "get", + UpstreamPathTemplate = "/" + }, + new FileReRoute() + { + DownstreamHost = "localhost", + DownstreamPort = 80, + DownstreamScheme = "https", + DownstreamPathTemplate = "/", + UpstreamHttpMethod = "get", + UpstreamPathTemplate = "/test" + } + } + }; + + var updatedConfiguration = new FileConfiguration + { + GlobalConfiguration = new FileGlobalConfiguration + { + AdministrationPath = "/administration" + }, + ReRoutes = new List() + { + new FileReRoute() + { + DownstreamHost = "127.0.0.1", + DownstreamPort = 80, + DownstreamScheme = "http", + DownstreamPathTemplate = "/geoffrey", + UpstreamHttpMethod = "get", + UpstreamPathTemplate = "/" + }, + new FileReRoute() + { + DownstreamHost = "123.123.123", + DownstreamPort = 443, + DownstreamScheme = "https", + DownstreamPathTemplate = "/blooper/{productId}", + UpstreamHttpMethod = "post", + UpstreamPathTemplate = "/test" + } + } + }; + + this.Given(x => GivenThereIsAConfiguration(initialConfiguration)) + .And(x => GivenOcelotIsRunning()) + .And(x => GivenIHaveAnOcelotToken("/administration")) + .And(x => GivenIHaveAddedATokenToMyRequest()) + .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) + .When(x => WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration)) + .Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) + .And(x => ThenTheResponseShouldBe(updatedConfiguration)) + .When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration")) + .And(x => ThenTheResponseShouldBe(updatedConfiguration)) + .BDDfy(); + } + + private void WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration) + { + var json = JsonConvert.SerializeObject(updatedConfiguration); + var content = new StringContent(json); + content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + _response = _httpClient.PostAsync(url, content).Result; + } + private void ThenTheResponseShouldBe(FileConfiguration expected) { var response = JsonConvert.DeserializeObject(_response.Content.ReadAsStringAsync().Result); @@ -180,6 +269,8 @@ namespace Ocelot.IntegrationTests File.WriteAllText(configurationPath, jsonConfiguration); + var text = File.ReadAllText(configurationPath); + configurationPath = $"{AppContext.BaseDirectory}/configuration.json"; if (File.Exists(configurationPath)) @@ -188,6 +279,8 @@ namespace Ocelot.IntegrationTests } File.WriteAllText(configurationPath, jsonConfiguration); + + text = File.ReadAllText(configurationPath); } private void WhenIGetUrlOnTheApiGateway(string url) diff --git a/test/Ocelot.IntegrationTests/configuration.json b/test/Ocelot.IntegrationTests/configuration.json old mode 100644 new mode 100755 diff --git a/test/Ocelot.UnitTests/Configuration/FileConfigurationProviderTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationProviderTests.cs index c7d240f8..a1ea4af7 100644 --- a/test/Ocelot.UnitTests/Configuration/FileConfigurationProviderTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationProviderTests.cs @@ -1,77 +1,62 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Moq; using Ocelot.Configuration; -using Ocelot.Configuration.Creator; -using Ocelot.Configuration.Provider; -using Ocelot.Configuration.Repository; -using Ocelot.Errors; +using Ocelot.Configuration.File; using Ocelot.Responses; using Shouldly; using TestStack.BDDfy; using Xunit; +using Newtonsoft.Json; +using System.IO; +using Ocelot.Configuration.Provider; +using Ocelot.Configuration.Repository; namespace Ocelot.UnitTests.Configuration { public class FileConfigurationProviderTests { - private readonly IOcelotConfigurationProvider _ocelotConfigurationProvider; - private readonly Mock _configurationRepository; - private Response _result; + private readonly IFileConfigurationProvider _provider; + private Mock _repo; + private FileConfiguration _result; + private FileConfiguration _fileConfiguration; public FileConfigurationProviderTests() { - _configurationRepository = new Mock(); - _ocelotConfigurationProvider = new OcelotConfigurationProvider(_configurationRepository.Object); + _repo = new Mock(); + _provider = new FileConfigurationProvider(_repo.Object); } [Fact] - public void should_get_config() + public void should_return_file_configuration() { - this.Given(x => x.GivenTheRepoReturns(new OkResponse(new OcelotConfiguration(new List(), string.Empty)))) - .When(x => x.WhenIGetTheConfig()) - .Then(x => x.TheFollowingIsReturned(new OkResponse(new OcelotConfiguration(new List(), string.Empty)))) + var config = new FileConfiguration(); + + this.Given(x => x.GivenTheConfigurationIs(config)) + .When(x => x.WhenIGetTheReRoutes()) + .Then(x => x.ThenTheRepoIsCalledCorrectly()) .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) + private void GivenTheConfigurationIs(FileConfiguration fileConfiguration) { - _configurationRepository + _fileConfiguration = fileConfiguration; + _repo .Setup(x => x.Get()) - .Returns(config); + .Returns(new OkResponse(fileConfiguration)); } - private void WhenIGetTheConfig() + private void WhenIGetTheReRoutes() { - _result = _ocelotConfigurationProvider.Get(); + _result = _provider.Get().Data; } - private void TheFollowingIsReturned(Response expected) + private void ThenTheRepoIsCalledCorrectly() { - _result.IsError.ShouldBe(expected.IsError); - } - - class AnyError : Error - { - public AnyError() - : base("blamo", OcelotErrorCode.UnknownError) - { - } + _repo + .Verify(x => x.Get(), Times.Once); } } -} +} \ No newline at end of file diff --git a/test/Ocelot.UnitTests/Services/FileConfigurationGetterTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationRepositoryTests.cs similarity index 51% rename from test/Ocelot.UnitTests/Services/FileConfigurationGetterTests.cs rename to test/Ocelot.UnitTests/Configuration/FileConfigurationRepositoryTests.cs index 32266c44..dfa8f67a 100644 --- a/test/Ocelot.UnitTests/Services/FileConfigurationGetterTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationRepositoryTests.cs @@ -7,20 +7,21 @@ using Ocelot.Responses; using Shouldly; using TestStack.BDDfy; using Xunit; -using Ocelot.Services; using Newtonsoft.Json; using System.IO; +using Ocelot.Configuration.Repository; -namespace Ocelot.UnitTests.Services +namespace Ocelot.UnitTests.Configuration { - public class GetFileConfigurationTests + public class FileConfigurationRepositoryTests { - private readonly IFileConfigurationProvider _getReRoutes; + private readonly IFileConfigurationRepository _repo; private FileConfiguration _result; + private FileConfiguration _fileConfiguration; - public GetFileConfigurationTests() + public FileConfigurationRepositoryTests() { - _getReRoutes = new FileConfigurationProvider(); + _repo = new FileConfigurationRepository(); } [Fact] @@ -58,6 +59,69 @@ namespace Ocelot.UnitTests.Services .BDDfy(); } + [Fact] + public void should_set_file_configuration() + { + var reRoutes = new List + { + new FileReRoute + { + DownstreamHost = "123.12.12.12", + DownstreamPort = 80, + DownstreamScheme = "https", + DownstreamPathTemplate = "/asdfs/test/{test}" + } + }; + + var globalConfiguration = new FileGlobalConfiguration + { + AdministrationPath = "asdas", + ServiceDiscoveryProvider = new FileServiceDiscoveryProvider + { + Provider = "consul", + Port = 198, + Host = "blah" + } + }; + + var config = new FileConfiguration(); + config.GlobalConfiguration = globalConfiguration; + config.ReRoutes.AddRange(reRoutes); + + this.Given(x => GivenIHaveAConfiguration(config)) + .When(x => WhenISetTheConfiguration()) + .Then(x => ThenTheConfigurationIsStoredAs(config)) + .BDDfy(); + } + + private void GivenIHaveAConfiguration(FileConfiguration fileConfiguration) + { + _fileConfiguration = fileConfiguration; + } + + private void WhenISetTheConfiguration() + { + _repo.Set(_fileConfiguration); + _result = _repo.Get().Data; + } + + private void ThenTheConfigurationIsStoredAs(FileConfiguration expected) + { + _result.GlobalConfiguration.AdministrationPath.ShouldBe(expected.GlobalConfiguration.AdministrationPath); + _result.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey); + _result.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host); + _result.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port); + _result.GlobalConfiguration.ServiceDiscoveryProvider.Provider.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Provider); + + for(var i = 0; i < _result.ReRoutes.Count; i++) + { + _result.ReRoutes[i].DownstreamHost.ShouldBe(expected.ReRoutes[i].DownstreamHost); + _result.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expected.ReRoutes[i].DownstreamPathTemplate); + _result.ReRoutes[i].DownstreamPort.ShouldBe(expected.ReRoutes[i].DownstreamPort); + _result.ReRoutes[i].DownstreamScheme.ShouldBe(expected.ReRoutes[i].DownstreamScheme); + } + } + private void GivenTheConfigurationIs(FileConfiguration fileConfiguration) { var configurationPath = $"{AppContext.BaseDirectory}/configuration.json"; @@ -74,7 +138,7 @@ namespace Ocelot.UnitTests.Services private void WhenIGetTheReRoutes() { - _result = _getReRoutes.Get().Data; + _result = _repo.Get().Data; } private void ThenTheFollowingIsReturned(FileConfiguration expected) diff --git a/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs index aa9b935a..bf4804f6 100644 --- a/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationSetterTests.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Moq; using Ocelot.Configuration; @@ -21,12 +22,14 @@ namespace Ocelot.UnitTests.Configuration private Mock _configCreator; private Response _configuration; private object _result; + private Mock _repo; public FileConfigurationSetterTests() { + _repo = new Mock(); _configRepo = new Mock(); _configCreator = new Mock(); - _configSetter = new FileConfigurationSetter(_configRepo.Object, _configCreator.Object); + _configSetter = new FileConfigurationSetter(_configRepo.Object, _configCreator.Object, _repo.Object); } [Fact] @@ -36,24 +39,46 @@ namespace Ocelot.UnitTests.Configuration var config = new OcelotConfiguration(new List(), string.Empty); this.Given(x => GivenTheFollowingConfiguration(fileConfig)) + .And(x => GivenTheRepoReturns(new OkResponse())) .And(x => GivenTheCreatorReturns(new OkResponse(config))) .When(x => WhenISetTheConfiguration()) .Then(x => ThenTheConfigurationRepositoryIsCalledCorrectly()) .BDDfy(); } + [Fact] - public void should_return_error_if_unable_to_set_configuration() + public void should_return_error_if_unable_to_set_file_configuration() { var fileConfig = new FileConfiguration(); this.Given(x => GivenTheFollowingConfiguration(fileConfig)) + .And(x => GivenTheRepoReturns(new ErrorResponse(It.IsAny()))) + .When(x => WhenISetTheConfiguration()) + .And(x => ThenAnErrorResponseIsReturned()) + .BDDfy(); + } + + [Fact] + public void should_return_error_if_unable_to_set_ocelot_configuration() + { + var fileConfig = new FileConfiguration(); + + this.Given(x => GivenTheFollowingConfiguration(fileConfig)) + .And(x => GivenTheRepoReturns(new OkResponse())) .And(x => GivenTheCreatorReturns(new ErrorResponse(It.IsAny()))) .When(x => WhenISetTheConfiguration()) .And(x => ThenAnErrorResponseIsReturned()) .BDDfy(); } + private void GivenTheRepoReturns(Response response) + { + _repo + .Setup(x => x.Set(It.IsAny())) + .Returns(response); + } + private void ThenAnErrorResponseIsReturned() { _result.ShouldBeOfType(); diff --git a/test/Ocelot.UnitTests/Configuration/OcelotConfigurationProviderTests.cs b/test/Ocelot.UnitTests/Configuration/OcelotConfigurationProviderTests.cs new file mode 100644 index 00000000..9be55087 --- /dev/null +++ b/test/Ocelot.UnitTests/Configuration/OcelotConfigurationProviderTests.cs @@ -0,0 +1,77 @@ +using System.Collections.Generic; +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 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() + { + this.Given(x => x.GivenTheRepoReturns(new OkResponse(new OcelotConfiguration(new List(), string.Empty)))) + .When(x => x.WhenIGetTheConfig()) + .Then(x => x.TheFollowingIsReturned(new OkResponse(new OcelotConfiguration(new List(), string.Empty)))) + .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()) + .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) + { + } + } + } +} diff --git a/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs b/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs index 2638b4a5..7f4d7b87 100644 --- a/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs +++ b/test/Ocelot.UnitTests/Controllers/FileConfigurationControllerTests.cs @@ -7,10 +7,10 @@ using Ocelot.Configuration.Setter; using Ocelot.Controllers; using Ocelot.Errors; using Ocelot.Responses; -using Ocelot.Services; using TestStack.BDDfy; using Xunit; using Shouldly; +using Ocelot.Configuration.Provider; namespace Ocelot.UnitTests.Controllers {