From 76fe21d6574391f8e7950c7d1dd81bb591c7c55c Mon Sep 17 00:00:00 2001 From: Dave Tillman Date: Tue, 5 Jan 2016 15:47:39 -0800 Subject: [PATCH] Unit/integration test updates/additions - Add Integration test verifying Options dependency injection mechanism works with Config Server data - Fix some unit test so they run on OSX --- ...rConfigurationExtensionsIntegrationTest.cs | 38 +++++++++++- .../ConfigServerOptions.cs | 22 +++++++ .../HomeController.cs | 25 ++++++++ .../TestServerStartup.cs | 59 +++++++++++++++++++ .../project.json | 4 +- ...ConfigServerConfigurationExtensionsTest.cs | 22 ++++++- .../ConfigServerConfigurationProviderTest.cs | 4 +- .../project.json | 2 +- 8 files changed, 168 insertions(+), 8 deletions(-) create mode 100644 test/Spring.Extensions.Configuration.Server.IntegrationTest/ConfigServerOptions.cs create mode 100644 test/Spring.Extensions.Configuration.Server.IntegrationTest/HomeController.cs create mode 100644 test/Spring.Extensions.Configuration.Server.IntegrationTest/TestServerStartup.cs diff --git a/test/Spring.Extensions.Configuration.Server.IntegrationTest/ConfigServerConfigurationExtensionsIntegrationTest.cs b/test/Spring.Extensions.Configuration.Server.IntegrationTest/ConfigServerConfigurationExtensionsIntegrationTest.cs index b125121..90a3f62 100644 --- a/test/Spring.Extensions.Configuration.Server.IntegrationTest/ConfigServerConfigurationExtensionsIntegrationTest.cs +++ b/test/Spring.Extensions.Configuration.Server.IntegrationTest/ConfigServerConfigurationExtensionsIntegrationTest.cs @@ -17,9 +17,23 @@ using Spring.Extensions.Configuration.Server.Test; using Xunit; using Microsoft.Extensions.Configuration; +using Microsoft.AspNet.TestHost; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; namespace Spring.Extensions.Configuration.Server.IntegrationTest { + // + // NOTE: Some of the tests assume an running Java config server is started + // with repository data for application: foo, profile: development + // + // The easiest way to get that to happen is clone the spring-cloud-config + // repo and run the config-server. + // eg. git clone https://github.com/spring-cloud/spring-cloud-config.git + // cd spring-cloud-config\spring-cloud-config-server + // mvn spring-boot:run + // + public class ConfigServerConfigurationExtensionsIntegrationTest { public ConfigServerConfigurationExtensionsIntegrationTest() @@ -27,9 +41,10 @@ namespace Spring.Extensions.Configuration.Server.IntegrationTest } [Fact] - public void SpringCloudConfigServer_DefaultData() + public void SpringCloudConfigServer_ReturnsExpectedDefaultData() { - // Arrange + + // Arrange var appsettings = @" { 'spring': { @@ -47,7 +62,7 @@ namespace Spring.Extensions.Configuration.Server.IntegrationTest var configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddJsonFile(path); - // Act and Assert + // Act and Assert (expects Spring Cloud Config server to be running) configurationBuilder.AddConfigServer(); IConfigurationRoot root = configurationBuilder.Build(); @@ -59,6 +74,23 @@ namespace Spring.Extensions.Configuration.Server.IntegrationTest } + [Fact] + public async void SpringCloudConfigServer_ReturnsExpectedDefaultData_AsInjectedOptions() + { + // Arrange + var builder = new WebHostBuilder().UseStartup(); + + // Act and Assert (TestServer expects Spring Cloud Config server to be running) + using (var server = new TestServer(builder)) { + var client = server.CreateClient(); + string result = await client.GetStringAsync("http://localhost/Home/VerifyAsInjectedOptions"); + + Assert.Equal("spam" + + "bar"+ + "Spring Cloud Samples" + + "https://github.com/spring-cloud-samples", result); + } + } } } diff --git a/test/Spring.Extensions.Configuration.Server.IntegrationTest/ConfigServerOptions.cs b/test/Spring.Extensions.Configuration.Server.IntegrationTest/ConfigServerOptions.cs new file mode 100644 index 0000000..f2506af --- /dev/null +++ b/test/Spring.Extensions.Configuration.Server.IntegrationTest/ConfigServerOptions.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Spring.Extensions.Configuration.Server.IntegrationTest +{ + public class ConfigServerOptions + { + public string Bar { get; set; } + public string Foo { get; set; } + public Info Info { get; set; } + + } + + public class Info + { + public string Description { get; set; } + public string Url { get; set; } + } + +} diff --git a/test/Spring.Extensions.Configuration.Server.IntegrationTest/HomeController.cs b/test/Spring.Extensions.Configuration.Server.IntegrationTest/HomeController.cs new file mode 100644 index 0000000..a5c5469 --- /dev/null +++ b/test/Spring.Extensions.Configuration.Server.IntegrationTest/HomeController.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNet.Mvc; +using Microsoft.Extensions.OptionsModel; + +namespace Spring.Extensions.Configuration.Server.IntegrationTest +{ + + public class HomeController : Controller + { + private ConfigServerOptions _options; + public HomeController(IOptions options) + { + _options = options.Value; + } + [HttpGet] + public string VerifyAsInjectedOptions() + { + return _options.Bar + _options.Foo + _options.Info.Description + _options.Info.Url; + } + + } +} diff --git a/test/Spring.Extensions.Configuration.Server.IntegrationTest/TestServerStartup.cs b/test/Spring.Extensions.Configuration.Server.IntegrationTest/TestServerStartup.cs new file mode 100644 index 0000000..96276a6 --- /dev/null +++ b/test/Spring.Extensions.Configuration.Server.IntegrationTest/TestServerStartup.cs @@ -0,0 +1,59 @@ +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; +using Microsoft.AspNet.Http; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Spring.Extensions.Configuration.Server.Test; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Spring.Extensions.Configuration.Server.IntegrationTest +{ + class TestServerStartup + { + public IConfiguration Configuration { get; set; } + + + public TestServerStartup() + { + // These settings match the default java config server + var appsettings = @" +{ + 'spring': { + 'cloud': { + 'config': { + 'uri': 'http://localhost:8888', + 'name': 'foo', + 'environment': 'development' + } + } + } +}"; + var path = ConfigServerTestHelpers.CreateTempFile(appsettings); + var builder = new ConfigurationBuilder() + .AddJsonFile(path) + .AddConfigServer(); + Configuration = builder.Build(); + + } + + public void ConfigureServices(IServiceCollection services) + { + services.AddOptions(); + services.Configure(Configuration); + + services.AddMvc(); + } + public void Configure(IApplicationBuilder app) + { + + app.UseMvc(routes => + routes.MapRoute( + name: "VerifyAsInjectedOptions", + template: "{controller=Home}/{action=VerifyAsInjectedOptions}") + ); + } + } +} diff --git a/test/Spring.Extensions.Configuration.Server.IntegrationTest/project.json b/test/Spring.Extensions.Configuration.Server.IntegrationTest/project.json index 6861f9f..3a69dd8 100644 --- a/test/Spring.Extensions.Configuration.Server.IntegrationTest/project.json +++ b/test/Spring.Extensions.Configuration.Server.IntegrationTest/project.json @@ -1,4 +1,4 @@ -{ +{ "version": "1.0.0-*", "description": "Integration test project for Spring.Extensions.Configuration.Test", "authors": [ "dtillman" ], @@ -13,6 +13,8 @@ } }, "dependencies": { + "Microsoft.AspNet.Hosting": "1.0.0-*", + "Microsoft.AspNet.Mvc": "6.0.0-*", "Microsoft.Extensions.Configuration": "1.0.0-*", "Microsoft.Extensions.Configuration.Json": "1.0.0-*", "Spring.Extensions.Configuration.Server": "1.0.0-*", diff --git a/test/Spring.Extensions.Configuration.Server.Test/ConfigServerConfigurationExtensionsTest.cs b/test/Spring.Extensions.Configuration.Server.Test/ConfigServerConfigurationExtensionsTest.cs index fa665ae..bedb1c0 100644 --- a/test/Spring.Extensions.Configuration.Server.Test/ConfigServerConfigurationExtensionsTest.cs +++ b/test/Spring.Extensions.Configuration.Server.Test/ConfigServerConfigurationExtensionsTest.cs @@ -30,7 +30,27 @@ namespace Spring.Extensions.Configuration.Server.Test // Act and Assert var ex = Assert.Throws(() => ConfigServerConfigurationExtensions.AddConfigServer(configurationBuilder)); - Assert.Equal("Value cannot be null.\r\nParameter name: " + nameof(configurationBuilder), ex.Message); + Assert.Contains(nameof(configurationBuilder), ex.Message); + + } + + [Fact] + public void AddConfigService_AddsConfigServerProviderToProvidersList() + { + // Arrange + var configurationBuilder = new ConfigurationBuilder(); + + // Act and Assert + configurationBuilder.AddConfigServer(); + + ConfigServerConfigurationProvider configServerProvider = null; + foreach (IConfigurationProvider provider in configurationBuilder.Providers) + { + configServerProvider = provider as ConfigServerConfigurationProvider; + if (configServerProvider != null) + break; + } + Assert.NotNull(configServerProvider); } diff --git a/test/Spring.Extensions.Configuration.Server.Test/ConfigServerConfigurationProviderTest.cs b/test/Spring.Extensions.Configuration.Server.Test/ConfigServerConfigurationProviderTest.cs index 339e9bf..25614e8 100644 --- a/test/Spring.Extensions.Configuration.Server.Test/ConfigServerConfigurationProviderTest.cs +++ b/test/Spring.Extensions.Configuration.Server.Test/ConfigServerConfigurationProviderTest.cs @@ -35,7 +35,7 @@ namespace Spring.Extensions.Configuration.Server.Test // Act and Assert var ex = Assert.Throws(() => new ConfigServerConfigurationProvider(settings)); - Assert.Equal("Value cannot be null.\r\nParameter name: " + nameof(settings), ex.Message); + Assert.Contains(nameof(settings), ex.Message); } [Fact] @@ -47,7 +47,7 @@ namespace Spring.Extensions.Configuration.Server.Test // Act and Assert var ex = Assert.Throws(() => new ConfigServerConfigurationProvider(settings, httpClient)); - Assert.Equal("Value cannot be null.\r\nParameter name: " + nameof(httpClient), ex.Message); + Assert.Contains(nameof(httpClient), ex.Message); } [Fact] diff --git a/test/Spring.Extensions.Configuration.Server.Test/project.json b/test/Spring.Extensions.Configuration.Server.Test/project.json index bf5c188..640d147 100644 --- a/test/Spring.Extensions.Configuration.Server.Test/project.json +++ b/test/Spring.Extensions.Configuration.Server.Test/project.json @@ -1,4 +1,4 @@ -{ +{ "version": "1.0.0-*", "description": "Unit test project for Spring.Extensions.Configuration.Server", "authors": [ "dtillman" ],