diff --git a/MainProject.Tests/Models/Api/Response/ApiResponse_Tests.cs b/MainProject.Tests/Models/Api/Response/ApiResponse_Tests.cs new file mode 100644 index 0000000..31e41d6 --- /dev/null +++ b/MainProject.Tests/Models/Api/Response/ApiResponse_Tests.cs @@ -0,0 +1,80 @@ +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; + + +namespace BasicDotnetTemplate.MainProject.Tests; + +[TestClass] +public class ApiResponse_Tests +{ + [TestMethod] + public void IstantiateBaseResponse_OnlyStatus_Valid() + { + try + { + var baseResponse = new BaseResponse(200, null, null); + Assert.IsTrue(baseResponse.Status == 200 && String.IsNullOrEmpty(baseResponse.Message) && baseResponse.Data == null); + } + catch (Exception ex) + { + Console.WriteLine(ex.InnerException); + Assert.Fail($"An exception was thrown: {ex.Message}"); + } + } + + [TestMethod] + public void IstantiateBaseResponse_OnlyStatus_IsInvalid() + { + try + { + var baseResponse = new BaseResponse(201, null, null); + Assert.IsFalse(baseResponse.Status == 200); + } + catch (Exception ex) + { + Console.WriteLine(ex.InnerException); + Assert.Fail($"An exception was thrown: {ex.Message}"); + } + } + + [TestMethod] + public void IstantiateBaseResponse_StatusAndMessage_Valid() + { + try + { + var baseResponse = new BaseResponse(200, "This is a test message", null); + Assert.IsTrue(baseResponse.Status == 200 && baseResponse.Message == "This is a test message" && baseResponse.Data == null); + } + catch (Exception ex) + { + Console.WriteLine(ex.InnerException); + Assert.Fail($"An exception was thrown: {ex.Message}"); + } + } + + [TestMethod] + public void IstantiateBaseResponse_AllFields_Valid() + { + try + { + string[] data = { "Volvo", "BMW", "Ford", "Mazda" }; + var baseResponse = new BaseResponse(200, "This is a test message", data); + Assert.IsTrue(baseResponse.Status == 200 && baseResponse.Message == "This is a test message" && baseResponse.Data == data); + } + 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 2fa623a..0d3efa8 100644 --- a/MainProject/Controllers/BaseController.cs +++ b/MainProject/Controllers/BaseController.cs @@ -22,12 +22,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers #nullable enable private static BaseResponse CreateResponse(HttpStatusCode status, string message, object? data = null) { - return new BaseResponse() - { - Status = (int)status, - Message = message, - Data = data - }; + return new BaseResponse((int)status, message, data); } protected new IActionResult Created(string message, object? data = null) diff --git a/MainProject/Models/Api/Response/BaseResponse.cs b/MainProject/Models/Api/Response/BaseResponse.cs index 040427b..2d408d1 100644 --- a/MainProject/Models/Api/Response/BaseResponse.cs +++ b/MainProject/Models/Api/Response/BaseResponse.cs @@ -2,10 +2,18 @@ namespace BasicDotnetTemplate.MainProject.Models.Api.Response; public class BaseResponse { +#nullable enable + public BaseResponse(int status, string? message, dynamic? data) + { + this.Status = status; + this.Message = message; + this.Data = data; + } +#nullable disable public int Status { get; set; } #nullable enable public string? Message { get; set; } - public object? Data { get; set; } + public virtual dynamic? Data { get; set; } #nullable disable } \ No newline at end of file diff --git a/MainProject/Program.cs b/MainProject/Program.cs index 84b66de..77a9d03 100644 --- a/MainProject/Program.cs +++ b/MainProject/Program.cs @@ -3,7 +3,6 @@ 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; diff --git a/MainProject/Utils/AppSettingsUtils.cs b/MainProject/Utils/AppSettingsUtils.cs deleted file mode 100644 index d8189de..0000000 --- a/MainProject/Utils/AppSettingsUtils.cs +++ /dev/null @@ -1,12 +0,0 @@ -using BasicDotnetTemplate.MainProject.Models.Settings; - -namespace BasicDotnetTemplate.MainProject.Utils; - -public static class AppSettingsUtils -{ - static AppSettingsUtils() { } - public static bool CheckAppSettings(AppSettings appSetting) - { - return true; - } -} \ No newline at end of file