diff --git a/MainProject.Tests/AppSettingsUtils_Tests.cs b/MainProject.Tests/AppSettingsUtils_Tests.cs deleted file mode 100644 index 4e58c38..0000000 --- a/MainProject.Tests/AppSettingsUtils_Tests.cs +++ /dev/null @@ -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); - } - } -} diff --git a/MainProject.Tests/Program_Tests.cs b/MainProject.Tests/Program_Tests.cs new file mode 100644 index 0000000..dcbab1d --- /dev/null +++ b/MainProject.Tests/Program_Tests.cs @@ -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}"); + } + } +} diff --git a/MainProject/Controllers/BaseController.cs b/MainProject/Controllers/BaseController.cs index 2127fd3..09e4ce3 100644 --- a/MainProject/Controllers/BaseController.cs +++ b/MainProject/Controllers/BaseController.cs @@ -1,5 +1,6 @@ using System.Net; using Microsoft.AspNetCore.Mvc; +using BasicDotnetTemplate.MainProject.Models.Api.Response; using BasicDotnetTemplate.MainProject.Models.Settings; [Controller] @@ -17,7 +18,41 @@ public abstract class BaseController : ControllerBase _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 } \ No newline at end of file diff --git a/MainProject/Controllers/RootController.cs b/MainProject/Controllers/RootController.cs new file mode 100644 index 0000000..ca2bb3e --- /dev/null +++ b/MainProject/Controllers/RootController.cs @@ -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"); + } + } +} \ No newline at end of file diff --git a/MainProject/Controllers/VersionController.cs b/MainProject/Controllers/VersionController.cs index 6866cf0..88b527b 100644 --- a/MainProject/Controllers/VersionController.cs +++ b/MainProject/Controllers/VersionController.cs @@ -16,7 +16,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers [Route("version")] public IActionResult GetVersion() { - return Ok(new { version = _appSettings.Settings.Version }); + return Success(String.Empty, _appSettings?.Settings?.Version); } } } \ No newline at end of file diff --git a/MainProject/Models/Api/Response/BaseResponse.cs b/MainProject/Models/Api/Response/BaseResponse.cs new file mode 100644 index 0000000..619d2ad --- /dev/null +++ b/MainProject/Models/Api/Response/BaseResponse.cs @@ -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 +} \ No newline at end of file diff --git a/MainProject/Program.cs b/MainProject/Program.cs index 7c738e8..1ed6985 100644 --- a/MainProject/Program.cs +++ b/MainProject/Program.cs @@ -1,16 +1,43 @@ using System; +using System.Runtime.CompilerServices; using Microsoft.OpenApi.Models; using NLog; using BasicDotnetTemplate.MainProject.Models.Settings; using BasicDotnetTemplate.MainProject.Utils; +using System.Reflection; 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 { 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"); var builder = WebApplication.CreateBuilder(args); @@ -86,7 +113,7 @@ internal static class Program options.SwaggerDoc("v1", openApiInfo); }); - var app = builder.Build(); + app = builder.Build(); // REGISTER MIDDLEWARE HERE app.UseRouting(); @@ -110,6 +137,13 @@ internal static class Program } Logger.Info("[Program][Main] Launching app"); + } + + + public static void Main(string[] args) + { + ReflectionProgram.LaunchConfiguration(); + Initialize(args); app.Run(); diff --git a/README.md b/README.md index 27eeedb..fb9751d 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ # MainProject +