Various changes (Controller and test)
- Added RootController - Added new methods in BaseController - Added BaseResponse - Changed Program structure - Added first basic test for Program
This commit is contained in:
@@ -1,25 +0,0 @@
|
|||||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
|
||||||
using BasicDotnetTemplate.MainProject.Utils;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject.Tests
|
|
||||||
{
|
|
||||||
[TestClass]
|
|
||||||
public class AppSettingsUtils_Tests
|
|
||||||
{
|
|
||||||
private readonly AppSettings _appSettings;
|
|
||||||
|
|
||||||
public AppSettingsUtils_Tests()
|
|
||||||
{
|
|
||||||
_appSettings = new AppSettings();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void AppSettingsUtils_Tests_IsValid()
|
|
||||||
{
|
|
||||||
bool result = AppSettingsUtils.CheckAppSettings(_appSettings);
|
|
||||||
|
|
||||||
Assert.IsTrue(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
67
MainProject.Tests/Program_Tests.cs
Normal file
67
MainProject.Tests/Program_Tests.cs
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using BasicDotnetTemplate.MainProject;
|
||||||
|
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Tests;
|
||||||
|
|
||||||
|
[TestClass]
|
||||||
|
public class Program_Tests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public async Task Program_Configuration_IsValid()
|
||||||
|
{
|
||||||
|
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var reflectionType = typeof(ReflectionProgram);
|
||||||
|
|
||||||
|
if (reflectionType != null)
|
||||||
|
{
|
||||||
|
MethodInfo[] methods = reflectionType.GetMethods(); //Using BindingFlags.NonPublic does not show any results
|
||||||
|
|
||||||
|
MethodInfo? execute = null;
|
||||||
|
|
||||||
|
foreach (MethodInfo m in methods)
|
||||||
|
{
|
||||||
|
if (m.Name == "LaunchConfiguration")
|
||||||
|
{
|
||||||
|
execute = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (execute != null)
|
||||||
|
{
|
||||||
|
object initializeObj = execute.Invoke(null, new object[] { });
|
||||||
|
MethodInfo initialize = (MethodInfo)initializeObj;
|
||||||
|
if (initialize != null)
|
||||||
|
{
|
||||||
|
initialize.Invoke(null, new object[] { new string[] { } });
|
||||||
|
Assert.IsTrue(true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail("Initialize is null.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail("Initialize method not found in Program class.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.Fail("Program class not found in the assembly.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.InnerException);
|
||||||
|
Assert.Fail($"An exception was thrown: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
|
||||||
[Controller]
|
[Controller]
|
||||||
@@ -17,7 +18,41 @@ public abstract class BaseController : ControllerBase
|
|||||||
_configuration.GetSection("AppSettings").Bind(_appSettings);
|
_configuration.GetSection("AppSettings").Bind(_appSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#nullable enable
|
||||||
|
private BaseResponse CreateResponse(HttpStatusCode status, string message, object? data = null)
|
||||||
|
{
|
||||||
|
return new BaseResponse()
|
||||||
|
{
|
||||||
|
Status = (int)status,
|
||||||
|
Message = message,
|
||||||
|
Data = data
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
protected new IActionResult Created(string message, object? data = null)
|
||||||
|
{
|
||||||
|
return StatusCode((int)HttpStatusCode.Created, CreateResponse(HttpStatusCode.Created, message, data));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected IActionResult Success(string message, object? data = null)
|
||||||
|
{
|
||||||
|
return StatusCode((int)HttpStatusCode.OK, CreateResponse(HttpStatusCode.OK, message, data));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected IActionResult NotFound(string message, object? data = null)
|
||||||
|
{
|
||||||
|
return StatusCode((int)HttpStatusCode.NotFound, CreateResponse(HttpStatusCode.NotFound, message, data));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected IActionResult BadRequest(string message, object? data = null)
|
||||||
|
{
|
||||||
|
return StatusCode((int)HttpStatusCode.BadRequest, CreateResponse(HttpStatusCode.BadRequest, message, data));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected IActionResult InternalServerError(string message)
|
||||||
|
{
|
||||||
|
return StatusCode((int)HttpStatusCode.InternalServerError, CreateResponse(HttpStatusCode.InternalServerError, message));
|
||||||
|
}
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
}
|
}
|
||||||
22
MainProject/Controllers/RootController.cs
Normal file
22
MainProject/Controllers/RootController.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
|
|
||||||
|
namespace BasicDotnetTemplate.MainProject.Controllers
|
||||||
|
{
|
||||||
|
public class RootController : BaseController
|
||||||
|
{
|
||||||
|
public RootController(
|
||||||
|
IConfiguration configuration
|
||||||
|
) : base(configuration)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
[Route("")]
|
||||||
|
public IActionResult GetVersion()
|
||||||
|
{
|
||||||
|
return Success(String.Empty, "Success");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,7 +16,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
|||||||
[Route("version")]
|
[Route("version")]
|
||||||
public IActionResult GetVersion()
|
public IActionResult GetVersion()
|
||||||
{
|
{
|
||||||
return Ok(new { version = _appSettings.Settings.Version });
|
return Success(String.Empty, _appSettings?.Settings?.Version);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
10
MainProject/Models/Api/Response/BaseResponse.cs
Normal file
10
MainProject/Models/Api/Response/BaseResponse.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
namespace BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||||
|
|
||||||
|
public class BaseResponse
|
||||||
|
{
|
||||||
|
public int Status { get; set; }
|
||||||
|
public string Message { get; set; }
|
||||||
|
#nullable enable
|
||||||
|
public object? Data { get; set; }
|
||||||
|
#nullable disable
|
||||||
|
}
|
||||||
@@ -1,16 +1,43 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using NLog;
|
using NLog;
|
||||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||||
using BasicDotnetTemplate.MainProject.Utils;
|
using BasicDotnetTemplate.MainProject.Utils;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace BasicDotnetTemplate.MainProject;
|
namespace BasicDotnetTemplate.MainProject;
|
||||||
|
|
||||||
|
public class ReflectionProgram
|
||||||
|
{
|
||||||
|
public static MethodInfo LaunchConfiguration()
|
||||||
|
{
|
||||||
|
var a = typeof(Program);
|
||||||
|
|
||||||
|
MethodInfo[] methods = a.GetMethods(); //Using BindingFlags.NonPublic does not show any results
|
||||||
|
MethodInfo? initialize = null;
|
||||||
|
|
||||||
|
foreach (MethodInfo m in methods)
|
||||||
|
{
|
||||||
|
if (m.Name == "Initialize")
|
||||||
|
{
|
||||||
|
|
||||||
|
initialize = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return initialize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
|
internal static WebApplication app;
|
||||||
|
|
||||||
public static void Main(string[] args)
|
public static void Initialize(string[] args)
|
||||||
{
|
{
|
||||||
Logger.Info("[Program][Main] Start building");
|
Logger.Info("[Program][Main] Start building");
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
@@ -86,7 +113,7 @@ internal static class Program
|
|||||||
options.SwaggerDoc("v1", openApiInfo);
|
options.SwaggerDoc("v1", openApiInfo);
|
||||||
});
|
});
|
||||||
|
|
||||||
var app = builder.Build();
|
app = builder.Build();
|
||||||
|
|
||||||
// REGISTER MIDDLEWARE HERE
|
// REGISTER MIDDLEWARE HERE
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
@@ -110,6 +137,13 @@ internal static class Program
|
|||||||
}
|
}
|
||||||
|
|
||||||
Logger.Info("[Program][Main] Launching app");
|
Logger.Info("[Program][Main] Launching app");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
ReflectionProgram.LaunchConfiguration();
|
||||||
|
Initialize(args);
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user