diff --git a/MainProject.Tests/JsonData/appsettings.json b/MainProject.Tests/JsonData/appsettings.json new file mode 100644 index 0000000..94c400b --- /dev/null +++ b/MainProject.Tests/JsonData/appsettings.json @@ -0,0 +1,22 @@ +{ + "AppSettings" : + { + "Settings": { + "Name": "MainProject", + "Version": "v1.0", + "Description": "This template contains basic configuration for a .Net 8 backend" + }, + "OpenApiSettings": { + "TermsOfServiceUrl": "", + "OpenApiContact": { + "Name": "", + "Url": "" + }, + "OpenApiLicense": { + "Name": "", + "Url": "" + } + } + } + +} \ No newline at end of file diff --git a/MainProject.Tests/JsonData/completeAppSettings.json b/MainProject.Tests/JsonData/completeAppSettings.json new file mode 100644 index 0000000..38b0a1d --- /dev/null +++ b/MainProject.Tests/JsonData/completeAppSettings.json @@ -0,0 +1,22 @@ +{ + "AppSettings" : + { + "Settings": { + "Name": "MainProject", + "Version": "v1.0", + "Description": "This template contains basic configuration for a .Net 8 backend" + }, + "OpenApiSettings": { + "TermsOfServiceUrl": "https://github.com/csimonapastore/BasicDotnetTemplate/blob/main/LICENSE.md", + "OpenApiContact": { + "Name": "README", + "Url": "https://github.com/csimonapastore/BasicDotnetTemplate/blob/main/README.md" + }, + "OpenApiLicense": { + "Name": "MIT License", + "Url": "https://github.com/csimonapastore/BasicDotnetTemplate/blob/main/LICENSE.md" + } + } + } + +} \ No newline at end of file diff --git a/MainProject.Tests/Utils/ProgramUtils_Tests.cs b/MainProject.Tests/Utils/ProgramUtils_Tests.cs new file mode 100644 index 0000000..4e2ba0d --- /dev/null +++ b/MainProject.Tests/Utils/ProgramUtils_Tests.cs @@ -0,0 +1,100 @@ +using System; +using System.Reflection; +using System.Net; +using System.Net.Http; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using BasicDotnetTemplate.MainProject; +using BasicDotnetTemplate.MainProject.Models.Api.Response; +using Microsoft.Extensions.DependencyModel.Resolution; +using BasicDotnetTemplate.MainProject.Models.Settings; +using Microsoft.AspNetCore.Builder; +using BasicDotnetTemplate.MainProject.Utils; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Configuration; +using Microsoft.OpenApi.Models; +using Microsoft.OpenApi.Interfaces; + + +namespace BasicDotnetTemplate.MainProject.Tests; + +[TestClass] +public class ProgramUtils_Tests +{ + [TestMethod] + public void IstantiateAppSettingsOpenApi_NoOpenApiConfig_Valid() + { + try + { + OpenApiInfo expectedOpenApiInfo = new OpenApiInfo() + { + Title = "MainProject", + Description = "This template contains basic configuration for a .Net 8 backend", + Version = "v1.0", + Contact = null, + TermsOfService = null, + License = null, + Extensions = new Dictionary() + }; + + WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty()); + AppSettings realAppSettings = ProgramUtils.AddConfiguration(ref builder, "D:\\Users\\Simona\\Documents\\Projects\\BasicDotnetTemplate\\MainProject.Tests\\JsonData"); + OpenApiInfo realOpenApiInfo = ProgramUtils.CreateOpenApiInfo(realAppSettings); + + var areEquals = expectedOpenApiInfo.Title == realOpenApiInfo.Title && + expectedOpenApiInfo.Description == realOpenApiInfo.Description && + expectedOpenApiInfo.Version == realOpenApiInfo.Version && + expectedOpenApiInfo.Contact == realOpenApiInfo.Contact && + expectedOpenApiInfo.TermsOfService == realOpenApiInfo.TermsOfService && + expectedOpenApiInfo.License == realOpenApiInfo.License; + + Assert.IsTrue(areEquals); + } + catch (Exception ex) + { + Console.WriteLine(ex.InnerException); + Assert.Fail($"An exception was thrown: {ex.Message}"); + } + } + + [TestMethod] + public void IstantiateAppSettingsOpenApi_OpenApiConfig_NotNull() + { + try + { + OpenApiInfo expectedOpenApiInfo = new OpenApiInfo() + { + Title = "MainProject", + Description = "This template contains basic configuration for a .Net 8 backend", + Version = "v1.0", + Contact = null, + TermsOfService = null, + License = null, + Extensions = new Dictionary() + }; + + WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty()); + AppSettings realAppSettings = ProgramUtils.AddConfiguration(ref builder, "D:\\Users\\Simona\\Documents\\Projects\\BasicDotnetTemplate\\MainProject.Tests\\JsonData", "completeAppSettings.json"); + OpenApiInfo realOpenApiInfo = ProgramUtils.CreateOpenApiInfo(realAppSettings); + + var areEquals = expectedOpenApiInfo.Title == realOpenApiInfo.Title && + expectedOpenApiInfo.Description == realOpenApiInfo.Description && + expectedOpenApiInfo.Version == realOpenApiInfo.Version && + expectedOpenApiInfo.Contact == realOpenApiInfo.Contact && + expectedOpenApiInfo.TermsOfService == realOpenApiInfo.TermsOfService && + expectedOpenApiInfo.License == realOpenApiInfo.License; + + Assert.IsFalse(areEquals); + } + catch (Exception ex) + { + Console.WriteLine(ex.InnerException); + Assert.Fail($"An exception was thrown: {ex.Message}"); + } + } + + +} + + + + diff --git a/MainProject/Program.cs b/MainProject/Program.cs index 77a9d03..9edcba3 100644 --- a/MainProject/Program.cs +++ b/MainProject/Program.cs @@ -4,6 +4,7 @@ using Microsoft.OpenApi.Models; using NLog; using BasicDotnetTemplate.MainProject.Models.Settings; using System.Reflection; +using BasicDotnetTemplate.MainProject.Utils; namespace BasicDotnetTemplate.MainProject; @@ -32,128 +33,16 @@ public static class ReflectionProgram internal static class Program { private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); - - private static AppSettings AddConfiguration(ref WebApplicationBuilder builder) - { - Logger.Info("[Program][AddConfiguration] Adding configuration"); - - var _configuration = new ConfigurationBuilder() - .SetBasePath(System.AppDomain.CurrentDomain.BaseDirectory) - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true) - .AddEnvironmentVariables() - .Build(); - - builder.Services.AddSingleton(_configuration); - - PrivateSettings privateSettings = new PrivateSettings(); - _configuration.GetSection("PrivateSettings").Bind(privateSettings); - - builder.Services.Configure(_configuration.GetSection("AppSettings")); - builder.Services.Configure(_configuration.GetSection("PrivateSettings")); - - AppSettings appSettings = new AppSettings(); - appSettings.PrivateSettings = privateSettings; - _configuration.GetSection("AppSettings").Bind(appSettings); - - Logger.Info("[Program][AddConfiguration] Ended configuration"); - - return appSettings; - } - - private static void AddOpenApi(ref WebApplicationBuilder builder, AppSettings appSettings) - { - Logger.Info("[Program][AddOpenApi] Adding configuration"); - - OpenApiInfo openApiInfo = new() - { - Version = appSettings.Settings?.Version, - Title = appSettings.Settings?.Name, - Description = appSettings.Settings?.Description - }; - - if (!String.IsNullOrEmpty(appSettings.OpenApiSettings?.TermsOfServiceUrl)) - { - openApiInfo.TermsOfService = new Uri(appSettings.OpenApiSettings.TermsOfServiceUrl); - } - - if (!String.IsNullOrEmpty(appSettings.OpenApiSettings?.OpenApiContact?.Name) && !String.IsNullOrEmpty(appSettings.OpenApiSettings?.OpenApiContact?.Url)) - { - openApiInfo.Contact = new OpenApiContact - { - Name = appSettings.OpenApiSettings.OpenApiContact.Name, - Url = new Uri(appSettings.OpenApiSettings.OpenApiContact.Url) - }; - } - - if (!String.IsNullOrEmpty(appSettings.OpenApiSettings?.OpenApiLicense?.Name) && !String.IsNullOrEmpty(appSettings.OpenApiSettings?.OpenApiLicense?.Url)) - { - openApiInfo.License = new OpenApiLicense - { - Name = appSettings.OpenApiSettings.OpenApiLicense.Name, - Url = new Uri(appSettings.OpenApiSettings.OpenApiLicense.Url) - }; - } - - builder.Services.AddSwaggerGen(options => - { - options.SwaggerDoc("v1", openApiInfo); - }); - - Logger.Info("[Program][AddOpenApi] Ended configuration"); - - return; - } - - private static void AddServices(ref WebApplicationBuilder builder) - { - Logger.Info("[Program][AddServices] Adding services"); - - builder.Services.AddAuthentication(); - builder.Services.AddAuthorization(); - builder.Services.AddControllers(); - builder.Services.AddEndpointsApiExplorer(); - - Logger.Info("[Program][AddServices] Done services"); - } - - private static void AddMiddlewares(ref WebApplication app) - { - Logger.Info("[Program][AddMiddlewares] Adding middlewares"); - - app.UseRouting(); - app.UseAuthentication(); - app.UseAuthorization(); - - app.UseHttpsRedirection(); - app.UseStaticFiles(); - - app.MapControllers(); // This maps all controllers - - if (app.Environment.IsDevelopment()) - { - app.UseStaticFiles(); - app.UseSwagger(); - app.UseSwaggerUI(options => - { - options.InjectStylesheet("/swagger-ui/custom.css"); - options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1"); - }); - } - - Logger.Info("[Program][AddMiddlewares] Done middlewares"); - } - public static WebApplication Initialize(string[] args) { Logger.Info("[Program][Initialize] Start building"); var builder = WebApplication.CreateBuilder(args); - AppSettings appSettings = Program.AddConfiguration(ref builder); - Program.AddServices(ref builder); - Program.AddOpenApi(ref builder, appSettings); + AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder); + ProgramUtils.AddServices(ref builder); + ProgramUtils.AddOpenApi(ref builder, appSettings); WebApplication app = builder.Build(); - Program.AddMiddlewares(ref app); + ProgramUtils.AddMiddlewares(ref app); Logger.Info("[Program][Initialize] End building"); return app; } diff --git a/MainProject/Utils/ProgramUtils.cs b/MainProject/Utils/ProgramUtils.cs new file mode 100644 index 0000000..d2bc078 --- /dev/null +++ b/MainProject/Utils/ProgramUtils.cs @@ -0,0 +1,128 @@ +using System; +using System.Runtime.CompilerServices; +using Microsoft.OpenApi.Models; +using NLog; +using BasicDotnetTemplate.MainProject.Models.Settings; +using System.Reflection; + +namespace BasicDotnetTemplate.MainProject.Utils; + +public static class ProgramUtils +{ + private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); + + public static AppSettings AddConfiguration(ref WebApplicationBuilder builder, string? path = "", string? filename = "") + { + Logger.Info("[ProgramUtils][AddConfiguration] Adding configuration"); + + string appSettingsPath = String.IsNullOrEmpty(path) ? System.AppDomain.CurrentDomain.BaseDirectory : path; + var _configuration = new ConfigurationBuilder() + .SetBasePath(appSettingsPath) + .AddJsonFile(String.IsNullOrEmpty(filename) ? "appsettings.json" : filename, optional: false, reloadOnChange: true) + .AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true) + .AddEnvironmentVariables() + .Build(); + + builder.Services.AddSingleton(_configuration); + + PrivateSettings privateSettings = new PrivateSettings(); + _configuration.GetSection("PrivateSettings").Bind(privateSettings); + + builder.Services.Configure(_configuration.GetSection("AppSettings")); + builder.Services.Configure(_configuration.GetSection("PrivateSettings")); + + AppSettings appSettings = new AppSettings(); + appSettings.PrivateSettings = privateSettings; + _configuration.GetSection("AppSettings").Bind(appSettings); + + Logger.Info("[ProgramUtils][AddConfiguration] Ended configuration"); + + return appSettings; + } + + public static OpenApiInfo CreateOpenApiInfo(AppSettings appSettings) + { + OpenApiInfo openApiInfo = new() + { + Version = appSettings.Settings?.Version, + Title = appSettings.Settings?.Name, + Description = appSettings.Settings?.Description + }; + + if (!String.IsNullOrEmpty(appSettings.OpenApiSettings?.TermsOfServiceUrl)) + { + openApiInfo.TermsOfService = new Uri(appSettings.OpenApiSettings.TermsOfServiceUrl); + } + + if (!String.IsNullOrEmpty(appSettings.OpenApiSettings?.OpenApiContact?.Name) && !String.IsNullOrEmpty(appSettings.OpenApiSettings?.OpenApiContact?.Url)) + { + openApiInfo.Contact = new OpenApiContact + { + Name = appSettings.OpenApiSettings.OpenApiContact.Name, + Url = new Uri(appSettings.OpenApiSettings.OpenApiContact.Url) + }; + } + + if (!String.IsNullOrEmpty(appSettings.OpenApiSettings?.OpenApiLicense?.Name) && !String.IsNullOrEmpty(appSettings.OpenApiSettings?.OpenApiLicense?.Url)) + { + openApiInfo.License = new OpenApiLicense + { + Name = appSettings.OpenApiSettings.OpenApiLicense.Name, + Url = new Uri(appSettings.OpenApiSettings.OpenApiLicense.Url) + }; + } + + return openApiInfo; + } + public static void AddOpenApi(ref WebApplicationBuilder builder, AppSettings appSettings) + { + Logger.Info("[ProgramUtils][AddOpenApi] Adding swagger doc"); + + builder.Services.AddSwaggerGen(options => + { + options.SwaggerDoc("v1", CreateOpenApiInfo(appSettings)); + }); + + Logger.Info("[ProgramUtils][AddOpenApi] Ended swagger doc"); + } + + public static void AddServices(ref WebApplicationBuilder builder) + { + Logger.Info("[ProgramUtils][AddServices] Adding services"); + + builder.Services.AddAuthentication(); + builder.Services.AddAuthorization(); + builder.Services.AddControllers(); + builder.Services.AddEndpointsApiExplorer(); + + Logger.Info("[ProgramUtils][AddServices] Done services"); + } + + public static void AddMiddlewares(ref WebApplication app) + { + Logger.Info("[ProgramUtils][AddMiddlewares] Adding middlewares"); + + app.UseRouting(); + app.UseAuthentication(); + app.UseAuthorization(); + + app.UseHttpsRedirection(); + app.UseStaticFiles(); + + app.MapControllers(); // This maps all controllers + + if (app.Environment.IsDevelopment()) + { + app.UseStaticFiles(); + app.UseSwagger(); + app.UseSwaggerUI(options => + { + options.InjectStylesheet("/swagger-ui/custom.css"); + options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1"); + }); + } + + Logger.Info("[ProgramUtils][AddMiddlewares] Done middlewares"); + } + +} \ No newline at end of file