Fixing coverage - 3
This commit is contained in:
22
MainProject.Tests/JsonData/appsettings.json
Normal file
22
MainProject.Tests/JsonData/appsettings.json
Normal file
@@ -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": ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
22
MainProject.Tests/JsonData/completeAppSettings.json
Normal file
22
MainProject.Tests/JsonData/completeAppSettings.json
Normal file
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
100
MainProject.Tests/Utils/ProgramUtils_Tests.cs
Normal file
100
MainProject.Tests/Utils/ProgramUtils_Tests.cs
Normal file
@@ -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<string, IOpenApiExtension>()
|
||||||
|
};
|
||||||
|
|
||||||
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||||
|
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<string, IOpenApiExtension>()
|
||||||
|
};
|
||||||
|
|
||||||
|
WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty<string>());
|
||||||
|
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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -4,6 +4,7 @@ using Microsoft.OpenApi.Models;
|
|||||||
using NLog;
|
using NLog;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject;
|
namespace BasicDotnetTemplate.MainProject;
|
||||||
|
|
||||||
@@ -32,128 +33,16 @@ public static class ReflectionProgram
|
|||||||
internal static class Program
|
internal static class Program
|
||||||
{
|
{
|
||||||
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
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<IConfiguration>(_configuration);
|
|
||||||
|
|
||||||
PrivateSettings privateSettings = new PrivateSettings();
|
|
||||||
_configuration.GetSection("PrivateSettings").Bind(privateSettings);
|
|
||||||
|
|
||||||
builder.Services.Configure<AppSettings>(_configuration.GetSection("AppSettings"));
|
|
||||||
builder.Services.Configure<PrivateSettings>(_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)
|
public static WebApplication Initialize(string[] args)
|
||||||
{
|
{
|
||||||
Logger.Info("[Program][Initialize] Start building");
|
Logger.Info("[Program][Initialize] Start building");
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
AppSettings appSettings = Program.AddConfiguration(ref builder);
|
AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder);
|
||||||
Program.AddServices(ref builder);
|
ProgramUtils.AddServices(ref builder);
|
||||||
Program.AddOpenApi(ref builder, appSettings);
|
ProgramUtils.AddOpenApi(ref builder, appSettings);
|
||||||
WebApplication app = builder.Build();
|
WebApplication app = builder.Build();
|
||||||
Program.AddMiddlewares(ref app);
|
ProgramUtils.AddMiddlewares(ref app);
|
||||||
Logger.Info("[Program][Initialize] End building");
|
Logger.Info("[Program][Initialize] End building");
|
||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
|
|||||||
128
MainProject/Utils/ProgramUtils.cs
Normal file
128
MainProject/Utils/ProgramUtils.cs
Normal file
@@ -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<IConfiguration>(_configuration);
|
||||||
|
|
||||||
|
PrivateSettings privateSettings = new PrivateSettings();
|
||||||
|
_configuration.GetSection("PrivateSettings").Bind(privateSettings);
|
||||||
|
|
||||||
|
builder.Services.Configure<AppSettings>(_configuration.GetSection("AppSettings"));
|
||||||
|
builder.Services.Configure<PrivateSettings>(_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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user