diff --git a/src/Ocelot/Configuration/Repository/FileConfigurationRepository.cs b/src/Ocelot/Configuration/Repository/FileConfigurationRepository.cs index c7614c30..54bb36d7 100644 --- a/src/Ocelot/Configuration/Repository/FileConfigurationRepository.cs +++ b/src/Ocelot/Configuration/Repository/FileConfigurationRepository.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; using Newtonsoft.Json; using Ocelot.Configuration.File; using Ocelot.Responses; @@ -8,33 +9,41 @@ namespace Ocelot.Configuration.Repository { public class FileConfigurationRepository : IFileConfigurationRepository { + private readonly string _configFilePath; + private static readonly object _lock = new object(); + + public FileConfigurationRepository(IHostingEnvironment hostingEnvironment) + { + _configFilePath = $"{AppContext.BaseDirectory}/configuration{(string.IsNullOrEmpty(hostingEnvironment.EnvironmentName) ? string.Empty : ".")}{hostingEnvironment.EnvironmentName}.json"; + } + public async Task> Get() { - var configFilePath = $"{AppContext.BaseDirectory}/configuration.json"; - string json = string.Empty; + string jsonConfiguration; + lock(_lock) { - json = System.IO.File.ReadAllText(configFilePath); + jsonConfiguration = System.IO.File.ReadAllText(_configFilePath); } - var fileConfiguration = JsonConvert.DeserializeObject(json); + + var fileConfiguration = JsonConvert.DeserializeObject(jsonConfiguration); + return new OkResponse(fileConfiguration); } public async Task Set(FileConfiguration fileConfiguration) { - var configurationPath = $"{AppContext.BaseDirectory}/configuration.json"; - - var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration); + string jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration); lock(_lock) { - if (System.IO.File.Exists(configurationPath)) + if (System.IO.File.Exists(_configFilePath)) { - System.IO.File.Delete(configurationPath); + System.IO.File.Delete(_configFilePath); } - System.IO.File.WriteAllText(configurationPath, jsonConfiguration); + System.IO.File.WriteAllText(_configFilePath, jsonConfiguration); } return new OkResponse(); diff --git a/test/Ocelot.UnitTests/Configuration/FileConfigurationRepositoryTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationRepositoryTests.cs index 13a078b2..63841953 100644 --- a/test/Ocelot.UnitTests/Configuration/FileConfigurationRepositoryTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationRepositoryTests.cs @@ -1,56 +1,35 @@ using System; using System.Collections.Generic; using Moq; -using Ocelot.Configuration; using Ocelot.Configuration.File; -using Ocelot.Responses; using Shouldly; using TestStack.BDDfy; using Xunit; using Newtonsoft.Json; using System.IO; +using Microsoft.AspNetCore.Hosting; using Ocelot.Configuration.Repository; namespace Ocelot.UnitTests.Configuration { public class FileConfigurationRepositoryTests { - private readonly IFileConfigurationRepository _repo; + private readonly Mock _hostingEnvironment = new Mock(); + private IFileConfigurationRepository _repo; private FileConfiguration _result; private FileConfiguration _fileConfiguration; + private string _environmentName = "DEV"; public FileConfigurationRepositoryTests() { - _repo = new FileConfigurationRepository(); + _hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName); + _repo = new FileConfigurationRepository(_hostingEnvironment.Object); } [Fact] public void should_return_file_configuration() { - var reRoutes = new List - { - new FileReRoute - { - DownstreamHost = "localhost", - DownstreamPort = 80, - DownstreamScheme = "https", - DownstreamPathTemplate = "/test/test/{test}" - } - }; - - var globalConfiguration = new FileGlobalConfiguration - { - AdministrationPath = "testy", - ServiceDiscoveryProvider = new FileServiceDiscoveryProvider - { - Port = 198, - Host = "blah" - } - }; - - var config = new FileConfiguration(); - config.GlobalConfiguration = globalConfiguration; - config.ReRoutes.AddRange(reRoutes); + var config = FakeFileConfigurationForGet(); this.Given(x => x.GivenTheConfigurationIs(config)) .When(x => x.WhenIGetTheReRoutes()) @@ -58,33 +37,22 @@ namespace Ocelot.UnitTests.Configuration .BDDfy(); } + [Fact] + public void should_return_file_configuration_if_environment_name_is_unavailable() + { + var config = FakeFileConfigurationForGet(); + + this.Given(x => x.GivenTheEnvironmentNameIsUnavailable()) + .And(x => x.GivenTheConfigurationIs(config)) + .When(x => x.WhenIGetTheReRoutes()) + .Then(x => x.ThenTheFollowingIsReturned(config)) + .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 - { - Port = 198, - Host = "blah" - } - }; - - var config = new FileConfiguration(); - config.GlobalConfiguration = globalConfiguration; - config.ReRoutes.AddRange(reRoutes); + var config = FakeFileConfigurationForSet(); this.Given(x => GivenIHaveAConfiguration(config)) .When(x => WhenISetTheConfiguration()) @@ -92,6 +60,24 @@ namespace Ocelot.UnitTests.Configuration .BDDfy(); } + [Fact] + public void should_set_file_configuration_if_environment_name_is_unavailable() + { + var config = FakeFileConfigurationForSet(); + this.Given(x => GivenIHaveAConfiguration(config)) + .And(x => GivenTheEnvironmentNameIsUnavailable()) + .When(x => WhenISetTheConfiguration()) + .Then(x => ThenTheConfigurationIsStoredAs(config)) + .BDDfy(); + } + + private void GivenTheEnvironmentNameIsUnavailable() + { + _environmentName = null; + _hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName); + _repo = new FileConfigurationRepository(_hostingEnvironment.Object); + } + private void GivenIHaveAConfiguration(FileConfiguration fileConfiguration) { _fileConfiguration = fileConfiguration; @@ -119,9 +105,9 @@ namespace Ocelot.UnitTests.Configuration } } - private void GivenTheConfigurationIs(FileConfiguration fileConfiguration) + private void GivenTheConfigurationIs(FileConfiguration fileConfiguration) { - var configurationPath = $"{AppContext.BaseDirectory}/configuration.json"; + var configurationPath = $"{AppContext.BaseDirectory}/configuration{(string.IsNullOrEmpty(_environmentName) ? string.Empty : ".")}{_environmentName}.json"; var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration); @@ -153,5 +139,65 @@ namespace Ocelot.UnitTests.Configuration _result.ReRoutes[i].DownstreamScheme.ShouldBe(expected.ReRoutes[i].DownstreamScheme); } } + + private FileConfiguration FakeFileConfigurationForSet() + { + 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 + { + Port = 198, + Host = "blah" + } + }; + + return new FileConfiguration + { + GlobalConfiguration = globalConfiguration, + ReRoutes = reRoutes + }; + } + + private FileConfiguration FakeFileConfigurationForGet() + { + var reRoutes = new List + { + new FileReRoute + { + DownstreamHost = "localhost", + DownstreamPort = 80, + DownstreamScheme = "https", + DownstreamPathTemplate = "/test/test/{test}" + } + }; + + var globalConfiguration = new FileGlobalConfiguration + { + AdministrationPath = "testy", + ServiceDiscoveryProvider = new FileServiceDiscoveryProvider + { + Port = 198, + Host = "blah" + } + }; + + return new FileConfiguration + { + GlobalConfiguration = globalConfiguration, + ReRoutes = reRoutes + }; + } } } \ No newline at end of file