From ef71c16c1e70214b4322ab9ce21593678f059594 Mon Sep 17 00:00:00 2001 From: Chris Cheetham Date: Mon, 21 Jun 2021 12:50:46 -0400 Subject: [PATCH] Simplify UI config service interface --- .../Configuration/UiConfigFile.cs | 40 ++++++------------- .../Configuration/UiConfigService.cs | 21 ++-------- .../Controllers/ProjectController.cs | 32 +++++++-------- .../Controllers/RootController.cs | 12 +++--- .../Controllers/UiConfigController.cs | 22 +++++----- src/InitializrApi/Program.cs | 6 +-- src/InitializrApi/Services/IInitializeable.cs | 18 --------- .../Services/IUiConfigService.cs | 5 +-- .../HttpTests.cs | 3 -- .../InitializrApiOptionsTests.cs | 18 ++++++--- .../Controllers/ProjectControllerTests.cs | 2 +- .../Controllers/RootControllerTests.cs | 2 +- .../Controllers/UiConfigControllerTests.cs | 2 +- 13 files changed, 64 insertions(+), 119 deletions(-) delete mode 100644 src/InitializrApi/Services/IInitializeable.cs diff --git a/src/InitializrApi/Configuration/UiConfigFile.cs b/src/InitializrApi/Configuration/UiConfigFile.cs index 9bd88b6..75b821f 100644 --- a/src/InitializrApi/Configuration/UiConfigFile.cs +++ b/src/InitializrApi/Configuration/UiConfigFile.cs @@ -17,14 +17,6 @@ namespace Steeltoe.InitializrApi.Configuration /// public class UiConfigFile : InitializrApiServiceBase, IUiConfigService { - /* ----------------------------------------------------------------- * - * fields * - * ----------------------------------------------------------------- */ - - private readonly InitializrApiOptions _apiOptions; - - private UiConfig _uiConfig; - /* ----------------------------------------------------------------- * * constructors * * ----------------------------------------------------------------- */ @@ -37,37 +29,29 @@ namespace Steeltoe.InitializrApi.Configuration public UiConfigFile(IOptions options, ILogger logger) : base(logger) { - _apiOptions = options.Value; - } - - /* ----------------------------------------------------------------- * - * methods * - * ----------------------------------------------------------------- */ - - /// - public void Initialize() - { - Logger.LogInformation("loading configuration: {Path}", _apiOptions.UiConfigPath); + var apiOptions = options.Value; + Logger.LogInformation("loading configuration: {Path}", apiOptions.UiConfigPath); try { - var configJson = File.ReadAllText(_apiOptions.UiConfig["Path"]); - _uiConfig = Serializer.DeserializeJson(configJson); + var configJson = File.ReadAllText(apiOptions.UiConfig["Path"]); + UiConfig = Serializer.DeserializeJson(configJson); } catch (FileNotFoundException) { - throw new ArgumentException($"UI configuration file path does not exist: {_apiOptions.UiConfigPath}"); + throw new ArgumentException($"UI configuration file path does not exist: {apiOptions.UiConfigPath}"); } catch (UnauthorizedAccessException) { throw new ArgumentException( - $"UI configuration file path is not a file or cannot be read: {_apiOptions.UiConfigPath}"); + $"UI configuration file path is not a file or cannot be read: {apiOptions.UiConfigPath}"); } } - /// - public UiConfig GetUiConfig() - { - return _uiConfig; - } + /* ----------------------------------------------------------------- * + * properties * + * ----------------------------------------------------------------- */ + + /// + public UiConfig UiConfig { get; } } } diff --git a/src/InitializrApi/Configuration/UiConfigService.cs b/src/InitializrApi/Configuration/UiConfigService.cs index c806f38..12b9da9 100644 --- a/src/InitializrApi/Configuration/UiConfigService.cs +++ b/src/InitializrApi/Configuration/UiConfigService.cs @@ -15,12 +15,6 @@ namespace Steeltoe.InitializrApi.Configuration /// public class UiConfigService : InitializrApiServiceBase, IUiConfigService { - /* ----------------------------------------------------------------- * - * fields * - * ----------------------------------------------------------------- */ - - private readonly UiConfig _uiConfig; - /* ----------------------------------------------------------------- * * constructors * * ----------------------------------------------------------------- */ @@ -37,7 +31,7 @@ namespace Steeltoe.InitializrApi.Configuration ILogger logger) : base(logger) { - _uiConfig = configuration.Value; + UiConfig = configuration.Value; Logger.LogInformation( "Config Server: uri={ConfigServer},env={Environment},label={Label}", settings.Value.Uri, @@ -46,19 +40,10 @@ namespace Steeltoe.InitializrApi.Configuration } /* ----------------------------------------------------------------- * - * methods * + * properties * * ----------------------------------------------------------------- */ - /// - public void Initialize() - { - Logger.LogInformation("Initializing Initializr configuration"); - } - /// - public UiConfig GetUiConfig() - { - return _uiConfig; - } + public UiConfig UiConfig { get; } } } diff --git a/src/InitializrApi/Controllers/ProjectController.cs b/src/InitializrApi/Controllers/ProjectController.cs index 165bcdd..a558777 100644 --- a/src/InitializrApi/Controllers/ProjectController.cs +++ b/src/InitializrApi/Controllers/ProjectController.cs @@ -9,6 +9,7 @@ using Steeltoe.InitializrApi.Models; using Steeltoe.InitializrApi.Services; using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; namespace Steeltoe.InitializrApi.Controllers @@ -24,7 +25,7 @@ namespace Steeltoe.InitializrApi.Controllers * fields * * ----------------------------------------------------------------- */ - private readonly IUiConfigService _configService; + private readonly IUiConfigService _uiConfigService; private readonly IProjectGenerator _projectGenerator; @@ -35,16 +36,16 @@ namespace Steeltoe.InitializrApi.Controllers /// /// Initializes a new instance of the class. /// - /// Injected Initializr configuration service. + /// Injected Initializr configuration service. /// Injected project generator. /// Injected logger. public ProjectController( - IUiConfigService configService, + IUiConfigService uiConfigService, IProjectGenerator projectGenerator, ILogger logger) : base(logger) { - _configService = configService; + _uiConfigService = uiConfigService; _projectGenerator = projectGenerator; } @@ -60,7 +61,7 @@ namespace Steeltoe.InitializrApi.Controllers [AcceptVerbs("GET")] public async Task GetProjectArchive([FromQuery] ProjectSpec spec) { - var defaults = _configService.GetUiConfig(); + var defaults = _uiConfigService.UiConfig; var normalizedSpec = new ProjectSpec() { Name = spec.Name ?? defaults?.Name?.Default, @@ -85,26 +86,21 @@ namespace Steeltoe.InitializrApi.Controllers var caseSensitiveDeps = new List(); if (defaults.Dependencies?.Values != null) { - foreach (var group in defaults.Dependencies.Values) - { - foreach (var dep in group.Values) - { - caseSensitiveDeps.Add(dep.Id); - } - } + caseSensitiveDeps.AddRange( + from @group in defaults.Dependencies.Values + from dep in @group.Values + select dep.Id); } var deps = normalizedSpec.Dependencies.Split(','); for (int i = 0; i < deps.Length; ++i) { var found = false; - foreach (var caseSensitiveDep in caseSensitiveDeps) + foreach (var caseSensitiveDep in caseSensitiveDeps.Where( + caseSensitiveDep => caseSensitiveDep.Equals(deps[i], StringComparison.OrdinalIgnoreCase))) { - if (caseSensitiveDep.Equals(deps[i], StringComparison.OrdinalIgnoreCase)) - { - deps[i] = caseSensitiveDep; - found = true; - } + deps[i] = caseSensitiveDep; + found = true; } if (!found) diff --git a/src/InitializrApi/Controllers/RootController.cs b/src/InitializrApi/Controllers/RootController.cs index 4fcc437..85213a8 100644 --- a/src/InitializrApi/Controllers/RootController.cs +++ b/src/InitializrApi/Controllers/RootController.cs @@ -27,7 +27,7 @@ namespace Steeltoe.InitializrApi.Controllers private readonly InitializrApiOptions _apiOptions; - private readonly IUiConfigService _configService; + private readonly IUiConfigService _uiConfigService; /* ----------------------------------------------------------------- * * constructors * @@ -37,16 +37,16 @@ namespace Steeltoe.InitializrApi.Controllers /// Initializes a new instance of the class. /// /// Injected Initializr options. - /// Injected Initializr configuration service. + /// Injected Initializr configuration service. /// Injected logger. public RootController( IOptions options, - IUiConfigService configService, + IUiConfigService uiConfigService, ILogger logger) : base(logger) { _apiOptions = options.Value; - _configService = configService; + _uiConfigService = uiConfigService; } /* ----------------------------------------------------------------- * @@ -61,7 +61,7 @@ namespace Steeltoe.InitializrApi.Controllers public IActionResult GetHelp() { var help = new List(); - if (!(_apiOptions?.Logo is null)) + if (_apiOptions?.Logo is not null) { try { @@ -79,7 +79,7 @@ namespace Steeltoe.InitializrApi.Controllers help.Add(" :: Steeltoe Initializr :: https://start.steeltoe.io"); help.Add(string.Empty); - var uiConfig = _configService.GetUiConfig(); + var uiConfig = _uiConfigService.UiConfig; help.Add("This service generates quickstart projects that can be easily customized."); help.Add("Possible customizations include a project's dependencies and .NET target framework."); help.Add(string.Empty); diff --git a/src/InitializrApi/Controllers/UiConfigController.cs b/src/InitializrApi/Controllers/UiConfigController.cs index 872f19c..ae1bbaf 100644 --- a/src/InitializrApi/Controllers/UiConfigController.cs +++ b/src/InitializrApi/Controllers/UiConfigController.cs @@ -19,7 +19,7 @@ namespace Steeltoe.InitializrApi.Controllers * fields * * ----------------------------------------------------------------- */ - private readonly IUiConfigService _configService; + private readonly IUiConfigService _uiConfigService; /* ----------------------------------------------------------------- * * constructors * @@ -28,14 +28,14 @@ namespace Steeltoe.InitializrApi.Controllers /// /// Initializes a new instance of the class. /// - /// Injected configuration repository. + /// Injected configuration repository. /// Injected logger. public UiConfigController( - IUiConfigService configService, + IUiConfigService uiConfigService, ILogger logger) : base(logger) { - _configService = configService; + _uiConfigService = uiConfigService; } /* ----------------------------------------------------------------- * @@ -49,7 +49,7 @@ namespace Steeltoe.InitializrApi.Controllers [HttpGet] public IActionResult GetUiConfig() { - return Ok(_configService.GetUiConfig()); + return Ok(_uiConfigService.UiConfig); } /// @@ -60,7 +60,7 @@ namespace Steeltoe.InitializrApi.Controllers [Route("steeltoeVersions")] public IActionResult GetSteeltoeVersions() { - return Ok(_configService.GetUiConfig().SteeltoeVersion.Values); + return Ok(_uiConfigService.UiConfig.SteeltoeVersion.Values); } /// @@ -71,7 +71,7 @@ namespace Steeltoe.InitializrApi.Controllers [Route("dotNetFrameworks")] public IActionResult GetDotNetFrameworks() { - return Ok(_configService.GetUiConfig().DotNetFramework.Values); + return Ok(_uiConfigService.UiConfig.DotNetFramework.Values); } /// @@ -82,7 +82,7 @@ namespace Steeltoe.InitializrApi.Controllers [Route("dotNetTemplates")] public IActionResult GetDotNetTemplates() { - return Ok(_configService.GetUiConfig().DotNetTemplate.Values); + return Ok(_uiConfigService.UiConfig.DotNetTemplate.Values); } /// @@ -93,7 +93,7 @@ namespace Steeltoe.InitializrApi.Controllers [Route("languages")] public IActionResult GetLanguages() { - return Ok(_configService.GetUiConfig().Language.Values); + return Ok(_uiConfigService.UiConfig.Language.Values); } /// @@ -104,7 +104,7 @@ namespace Steeltoe.InitializrApi.Controllers [Route("archiveTypes")] public IActionResult GetArchiveTypes() { - return Ok(_configService.GetUiConfig().Packaging.Values); + return Ok(_uiConfigService.UiConfig.Packaging.Values); } /// @@ -115,7 +115,7 @@ namespace Steeltoe.InitializrApi.Controllers [Route("dependencies")] public IActionResult GetDependencies() { - return Ok(_configService.GetUiConfig().Dependencies.Values); + return Ok(_uiConfigService.UiConfig.Dependencies.Values); } } } diff --git a/src/InitializrApi/Program.cs b/src/InitializrApi/Program.cs index 0cbe7a6..63c6374 100644 --- a/src/InitializrApi/Program.cs +++ b/src/InitializrApi/Program.cs @@ -3,11 +3,9 @@ // See the LICENSE file in the project root for more information. using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Steeltoe.Extensions.Configuration.ConfigServer; using Steeltoe.InitializrApi.Models; -using Steeltoe.InitializrApi.Services; using System.Diagnostics.CodeAnalysis; using System.Reflection; @@ -61,9 +59,7 @@ namespace Steeltoe.InitializrApi /// public static void Main(string[] args) { - var host = CreateHostBuilder(args).Build(); - host.Services.GetRequiredService().Initialize(); - host.Run(); + CreateHostBuilder(args).Build().Run(); } /// diff --git a/src/InitializrApi/Services/IInitializeable.cs b/src/InitializrApi/Services/IInitializeable.cs deleted file mode 100644 index f98032d..0000000 --- a/src/InitializrApi/Services/IInitializeable.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the Apache 2.0 License. -// See the LICENSE file in the project root for more information. - -namespace Steeltoe.InitializrApi.Services -{ - /// - /// Contract for services that can be initialized and reinitialized. - /// - public interface IInitializeable - { - /// - /// Perform initialization. - /// Called when started or reconfigured. - /// - void Initialize(); - } -} diff --git a/src/InitializrApi/Services/IUiConfigService.cs b/src/InitializrApi/Services/IUiConfigService.cs index cc12e89..a6fe927 100644 --- a/src/InitializrApi/Services/IUiConfigService.cs +++ b/src/InitializrApi/Services/IUiConfigService.cs @@ -9,12 +9,11 @@ namespace Steeltoe.InitializrApi.Services /// /// Contract for Initializr configuration service implementations. /// - public interface IUiConfigService : IInitializeable + public interface IUiConfigService { /// /// Gets the Initializr configuration. /// - /// Returns an Initializr configuration. - public UiConfig GetUiConfig(); + public UiConfig UiConfig { get; } } } diff --git a/test/InitializrApi.Test.Integration/HttpTests.cs b/test/InitializrApi.Test.Integration/HttpTests.cs index 7d0f458..27f5d71 100644 --- a/test/InitializrApi.Test.Integration/HttpTests.cs +++ b/test/InitializrApi.Test.Integration/HttpTests.cs @@ -4,9 +4,7 @@ using System.Net.Http.Json; using System.Threading.Tasks; using FluentAssertions; using Microsoft.AspNetCore.Mvc.Testing; -using Microsoft.Extensions.DependencyInjection; using Steeltoe.InitializrApi.Models; -using Steeltoe.InitializrApi.Services; using Xunit; namespace Steeltoe.InitializrApi.Test.Integration @@ -189,7 +187,6 @@ namespace Steeltoe.InitializrApi.Test.Integration public HttpTests(WebApplicationFactory fixture) { - fixture.Services.GetRequiredService().Initialize(); HttpClient = fixture.CreateClient(); } } diff --git a/test/InitializrApi.Test.Unit/Configuration/InitializrApiOptionsTests.cs b/test/InitializrApi.Test.Unit/Configuration/InitializrApiOptionsTests.cs index 06b3339..cecbe0b 100644 --- a/test/InitializrApi.Test.Unit/Configuration/InitializrApiOptionsTests.cs +++ b/test/InitializrApi.Test.Unit/Configuration/InitializrApiOptionsTests.cs @@ -31,10 +31,12 @@ namespace Steeltoe.InitializrApi.Test.Unit.Configuration options.Setup(opts => opts.Value).Returns(new InitializrApiOptions { UiConfig = new Dictionary { { "Path", "no_such_path" } } }); var logger = new NullLogger(); - var config = new UiConfigFile(options.Object, logger); // Act - Action act = () => config.Initialize(); + Action act = () => + { + var _ = new UiConfigFile(options.Object, logger); + }; // Assert act.Should().Throw() @@ -49,10 +51,12 @@ namespace Steeltoe.InitializrApi.Test.Unit.Configuration options.Setup(opts => opts.Value).Returns(new InitializrApiOptions { UiConfig = new Dictionary { { "Path", "." } } }); var logger = new NullLogger(); - var config = new UiConfigFile(options.Object, logger); // Act - Action act = () => config.Initialize(); + Action act = () => + { + var _ = new UiConfigFile(options.Object, logger); + }; // Assert act.Should().Throw() @@ -67,10 +71,12 @@ namespace Steeltoe.InitializrApi.Test.Unit.Configuration options.Setup(opts => opts.Value).Returns(new InitializrApiOptions { UiConfig = new Dictionary { { "Path", "Steeltoe.InitializrApi.dll" } } }); var logger = new NullLogger(); - var config = new UiConfigFile(options.Object, logger); // Act - Action act = () => config.Initialize(); + Action act = () => + { + var _ = new UiConfigFile(options.Object, logger); + }; // Assert act.Should().Throw(); diff --git a/test/InitializrApi.Test.Unit/Controllers/ProjectControllerTests.cs b/test/InitializrApi.Test.Unit/Controllers/ProjectControllerTests.cs index ba0efc1..8cb0014 100644 --- a/test/InitializrApi.Test.Unit/Controllers/ProjectControllerTests.cs +++ b/test/InitializrApi.Test.Unit/Controllers/ProjectControllerTests.cs @@ -188,7 +188,7 @@ namespace Steeltoe.InitializrApi.Test.Unit.Controllers _generator ??= new TestProjectGenerator(); var configurationService = new Mock(); - configurationService.Setup(svc => svc.GetUiConfig()).Returns(_uiConfig); + configurationService.Setup(svc => svc.UiConfig).Returns(_uiConfig); var logger = new NullLogger(); var projectController = new ProjectController(configurationService.Object, _generator, logger) { diff --git a/test/InitializrApi.Test.Unit/Controllers/RootControllerTests.cs b/test/InitializrApi.Test.Unit/Controllers/RootControllerTests.cs index 02ffc20..26fbbab 100644 --- a/test/InitializrApi.Test.Unit/Controllers/RootControllerTests.cs +++ b/test/InitializrApi.Test.Unit/Controllers/RootControllerTests.cs @@ -103,7 +103,7 @@ namespace Steeltoe.InitializrApi.Test.Unit.Controllers } }; var configService = new Mock(); - configService.Setup(repo => repo.GetUiConfig()).Returns(config); + configService.Setup(svc => svc.UiConfig).Returns(config); var controller = new RootController(initializrCfg.Object, configService.Object, new NullLogger()); diff --git a/test/InitializrApi.Test.Unit/Controllers/UiConfigControllerTests.cs b/test/InitializrApi.Test.Unit/Controllers/UiConfigControllerTests.cs index 1587475..313f504 100644 --- a/test/InitializrApi.Test.Unit/Controllers/UiConfigControllerTests.cs +++ b/test/InitializrApi.Test.Unit/Controllers/UiConfigControllerTests.cs @@ -25,7 +25,7 @@ namespace Steeltoe.InitializrApi.Test.Unit.Controllers // Arrange var uiConfig = new UiConfig(); var configService = new Mock(); - configService.Setup(repo => repo.GetUiConfig()).Returns(uiConfig); + configService.Setup(svc => svc.UiConfig).Returns(uiConfig); var controller = new UiConfigController(configService.Object, new NullLogger()); // Act