diff --git a/MainProject.Tests/Controllers/VersionController_Tests.cs b/MainProject.Tests/Controllers/VersionController_Tests.cs index c4ad5df..ac1ff3c 100644 --- a/MainProject.Tests/Controllers/VersionController_Tests.cs +++ b/MainProject.Tests/Controllers/VersionController_Tests.cs @@ -8,6 +8,8 @@ using BasicDotnetTemplate.MainProject.Controllers; using Microsoft.Extensions.Configuration; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Infrastructure; +using BasicDotnetTemplate.MainProject.Models.Api.Response; +using BasicDotnetTemplate.MainProject.Models.Settings; namespace BasicDotnetTemplate.MainProject.Tests; @@ -25,7 +27,26 @@ public class BaseController_Tests IConfiguration configuration = TestUtils.CreateConfiguration(); VersionController versionController = new VersionController(configuration); var result = versionController.GetVersion(); - Assert.IsTrue(((IStatusCodeActionResult)result).StatusCode == 200); + var objectResult = ((ObjectResult)result).Value; + if (objectResult != null) + { + var data = (BaseResponse)objectResult; + if (data.Data != null) + { + AppSettings appSettings = new AppSettings(); + configuration.GetSection("AppSettings").Bind(appSettings); + string version = data.Data != null ? (string)data.Data : ""; + Assert.IsTrue((((IStatusCodeActionResult)result).StatusCode == 200) && (version == appSettings.Settings?.Version)); + } + else + { + Assert.Fail($"Unable to convert response value to BaseResponse because Data is null"); + } + } + else + { + Assert.Fail($"Data is null"); + } } catch (Exception ex) { diff --git a/MainProject.Tests/JsonData/appsettings.json b/MainProject.Tests/JsonData/appsettings.json index 94c400b..0e20c40 100644 --- a/MainProject.Tests/JsonData/appsettings.json +++ b/MainProject.Tests/JsonData/appsettings.json @@ -13,8 +13,8 @@ "Url": "" }, "OpenApiLicense": { - "Name": "", - "Url": "" + "Name": "MIT License", + "Url": "https://github.com/csimonapastore/BasicDotnetTemplate/blob/main/LICENSE.md" } } } diff --git a/MainProject.Tests/JsonData/noApiConfigurationAppSettings.json b/MainProject.Tests/JsonData/noApiConfigurationAppSettings.json new file mode 100644 index 0000000..94c400b --- /dev/null +++ b/MainProject.Tests/JsonData/noApiConfigurationAppSettings.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/TestsUtils/TestUtils.cs b/MainProject.Tests/TestsUtils/TestUtils.cs index f33d9f2..ce8d95e 100644 --- a/MainProject.Tests/TestsUtils/TestUtils.cs +++ b/MainProject.Tests/TestsUtils/TestUtils.cs @@ -19,27 +19,15 @@ namespace BasicDotnetTemplate.MainProject.Tests; public static class TestUtils { - public static IConfiguration? CreateConfiguration() + public static IConfiguration CreateConfiguration() { - try - { - WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty()); - AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, "D:\\Users\\Simona\\Documents\\Projects\\BasicDotnetTemplate\\MainProject.Tests\\JsonData"); - ProgramUtils.AddOpenApi(ref builder, appSettings); - AppSettings _appSettings = new AppSettings(); - builder.Configuration.GetSection("AppSettings").Bind(_appSettings); - return builder.Configuration; - } - catch (Exception ex) - { - Console.WriteLine(ex.InnerException); - return null; - } + WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty()); + AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, "D:\\Users\\Simona\\Documents\\Projects\\BasicDotnetTemplate\\MainProject.Tests\\JsonData"); + ProgramUtils.AddOpenApi(ref builder, appSettings); + AppSettings _appSettings = new AppSettings(); + builder.Configuration.GetSection("AppSettings").Bind(_appSettings); + return builder.Configuration; } - - - - } diff --git a/MainProject.Tests/Utils/ProgramUtils_Tests.cs b/MainProject.Tests/Utils/ProgramUtils_Tests.cs index 5970c3c..2cfc572 100644 --- a/MainProject.Tests/Utils/ProgramUtils_Tests.cs +++ b/MainProject.Tests/Utils/ProgramUtils_Tests.cs @@ -37,7 +37,7 @@ public class ProgramUtils_Tests }; WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty()); - AppSettings realAppSettings = ProgramUtils.AddConfiguration(ref builder, "D:\\Users\\Simona\\Documents\\Projects\\BasicDotnetTemplate\\MainProject.Tests\\JsonData"); + AppSettings realAppSettings = ProgramUtils.AddConfiguration(ref builder, "D:\\Users\\Simona\\Documents\\Projects\\BasicDotnetTemplate\\MainProject.Tests\\JsonData", "noApiConfigurationAppSettings.json"); OpenApiInfo realOpenApiInfo = ProgramUtils.CreateOpenApiInfo(realAppSettings); var areEquals = expectedOpenApiInfo.Title == realOpenApiInfo.Title && @@ -115,8 +115,6 @@ public class ProgramUtils_Tests Assert.Fail($"An exception was thrown: {ex.Message}"); } } - - } diff --git a/MainProject/appsettings.json b/MainProject/appsettings.json index 94c400b..0e20c40 100644 --- a/MainProject/appsettings.json +++ b/MainProject/appsettings.json @@ -13,8 +13,8 @@ "Url": "" }, "OpenApiLicense": { - "Name": "", - "Url": "" + "Name": "MIT License", + "Url": "https://github.com/csimonapastore/BasicDotnetTemplate/blob/main/LICENSE.md" } } }