From d5d5baf7b69ba3a07dc5611560acde1522f32a78 Mon Sep 17 00:00:00 2001 From: Ivar Nesje Date: Sun, 6 Oct 2024 00:19:19 +0200 Subject: [PATCH] Run analyzers that sonar cloud runs It is annoying to not get warnings in local dev, that I have to fix when creating a PR --- .editorconfig | 10 ++- .../Controllers/ProfileController.cs | 4 +- .../Extensions/WebHostBuilderExtensions.cs | 2 - .../Converters/JsonWebKeyConverter.cs | 1 - .../Telemetry/Telemetry.Validation.cs | 2 +- .../Helpers/DataModel/DataModel.cs | 9 +- .../Implementation/AppResourcesSI.cs | 9 +- .../Implementation/PrefillSI.cs | 2 +- .../Events/EventsSubscriptionClient.cs | 2 - .../Clients/Register/PersonClient.cs | 2 +- .../Internal/Language/ApplicationLanguage.cs | 11 +-- .../Internal/Patch/IPatchService.cs | 1 - .../Models/Layout/PageComponentConverter.cs | 15 ++-- .../InstancesController_PostNewInstance.cs | 2 +- .../Helpers/StartupHelperTests.cs | 2 +- .../Telemetry/TelemetryConfigurationTests.cs | 2 +- .../SwaggerIncludeXmlCommentsTestDouble.cs | 2 +- test/Altinn.App.Common.Tests/TelemetrySink.cs | 4 +- .../Features/Action/SigningUserActionTests.cs | 2 +- .../Action/UniqueSignatureAuthorizerTests.cs | 2 +- .../Default/DataAnnotationValidatorTests.cs | 4 +- .../Default/ExpressionValidatorTests.cs | 2 +- .../Validators/ValidationServiceTests.cs | 18 ++-- .../Helpers/JsonDataModel.cs | 14 ++- .../Helpers/JsonHelperTests.cs | 2 +- .../ObjectUtils_XmlSerializationTests.cs | 2 + .../Implementation/AppResourcesSITests.cs | 15 +--- .../Implementation/InstanceClientTests.cs | 2 +- .../Clients/Profile/ProfileClientTests.cs | 2 +- .../Clients/Storage/DataClientTests.cs | 2 +- .../Internal/Patch/PatchServiceTests.cs | 2 +- .../Internal/Process/ProcessEngineTest.cs | 2 +- .../LayoutModelConverterFromObject.cs | 3 +- .../CommonTests/TestFunctions.cs | 6 +- .../ExpressionEvaluatorTests/EqualsTests.cs | 87 ++++++++++++++----- .../Models/PageComponentConverterTests.cs | 4 +- 36 files changed, 144 insertions(+), 109 deletions(-) diff --git a/.editorconfig b/.editorconfig index 09294cd3e..f891a2fec 100644 --- a/.editorconfig +++ b/.editorconfig @@ -111,7 +111,7 @@ csharp_style_namespace_declarations = file_scoped:error dotnet_diagnostic.IDE1006.severity = error # Unused usings -dotnet_diagnostic.IDE0005.severity = suggestion +dotnet_diagnostic.IDE0005.severity = warning # CA1848: Use the LoggerMessage delegates dotnet_diagnostic.CA1848.severity = none @@ -123,7 +123,10 @@ dotnet_diagnostic.CA1727.severity = suggestion dotnet_diagnostic.CA2254.severity = none # CA1822: Mark members as static -dotnet_diagnostic.CA1822.severity = suggestion +dotnet_diagnostic.CA1822.severity = warning + +# IDE0052: Remove unread private members +dotnet_diagnostic.IDE0052.severity = warning # IDE0080: Remove unnecessary suppression operator dotnet_diagnostic.IDE0080.severity = error @@ -146,6 +149,9 @@ dotnet_diagnostic.CA2201.severity = suggestion # TODO: fixing this would be breaking dotnet_diagnostic.CA1720.severity = suggestion +# CA1816: Call GC.SuppressFinalize correctly +dotnet_diagnostic.CA1816.severity = warning + [Program.cs] dotnet_diagnostic.CA1050.severity = none dotnet_diagnostic.S1118.severity = none diff --git a/src/Altinn.App.Api/Controllers/ProfileController.cs b/src/Altinn.App.Api/Controllers/ProfileController.cs index 8be647500..900e8c2e7 100644 --- a/src/Altinn.App.Api/Controllers/ProfileController.cs +++ b/src/Altinn.App.Api/Controllers/ProfileController.cs @@ -14,15 +14,13 @@ namespace Altinn.App.Api.Controllers; public class ProfileController : Controller { private readonly IProfileClient _profileClient; - private readonly ILogger _logger; /// /// Initializes a new instance of the class /// - public ProfileController(IProfileClient profileClient, ILogger logger) + public ProfileController(IProfileClient profileClient) { _profileClient = profileClient; - _logger = logger; } /// diff --git a/src/Altinn.App.Api/Extensions/WebHostBuilderExtensions.cs b/src/Altinn.App.Api/Extensions/WebHostBuilderExtensions.cs index 52bf037b7..e3fb8e9bf 100644 --- a/src/Altinn.App.Api/Extensions/WebHostBuilderExtensions.cs +++ b/src/Altinn.App.Api/Extensions/WebHostBuilderExtensions.cs @@ -1,6 +1,4 @@ using Altinn.App.Core.Extensions; -using Altinn.App.Core.Features.Maskinporten; -using Altinn.App.Core.Features.Maskinporten.Models; using Microsoft.Extensions.FileProviders; namespace Altinn.App.Api.Extensions; diff --git a/src/Altinn.App.Core/Features/Maskinporten/Converters/JsonWebKeyConverter.cs b/src/Altinn.App.Core/Features/Maskinporten/Converters/JsonWebKeyConverter.cs index 90f5b20be..5b6a54ac9 100644 --- a/src/Altinn.App.Core/Features/Maskinporten/Converters/JsonWebKeyConverter.cs +++ b/src/Altinn.App.Core/Features/Maskinporten/Converters/JsonWebKeyConverter.cs @@ -1,6 +1,5 @@ using System.Text; using System.Text.Json; -using System.Text.Json.Serialization; using Altinn.App.Core.Features.Maskinporten.Exceptions; using Altinn.App.Core.Features.Maskinporten.Models; using Microsoft.IdentityModel.Tokens; diff --git a/src/Altinn.App.Core/Features/Telemetry/Telemetry.Validation.cs b/src/Altinn.App.Core/Features/Telemetry/Telemetry.Validation.cs index 9ef4c4763..f96c7f84e 100644 --- a/src/Altinn.App.Core/Features/Telemetry/Telemetry.Validation.cs +++ b/src/Altinn.App.Core/Features/Telemetry/Telemetry.Validation.cs @@ -6,7 +6,7 @@ namespace Altinn.App.Core.Features; partial class Telemetry { - private void InitValidation(InitContext context) { } + private static void InitValidation(InitContext context) { } internal Activity? StartValidateInstanceAtTaskActivity(Instance instance, string taskId) { diff --git a/src/Altinn.App.Core/Helpers/DataModel/DataModel.cs b/src/Altinn.App.Core/Helpers/DataModel/DataModel.cs index 361d9098a..ca22ce63a 100644 --- a/src/Altinn.App.Core/Helpers/DataModel/DataModel.cs +++ b/src/Altinn.App.Core/Helpers/DataModel/DataModel.cs @@ -44,7 +44,12 @@ is System.Collections.IEnumerable childEnum return null; } - private object? GetModelDataRecursive(string[] keys, int index, object currentModel, ReadOnlySpan indicies) + private static object? GetModelDataRecursive( + string[] keys, + int index, + object currentModel, + ReadOnlySpan indicies + ) { if (index == keys.Length) { @@ -404,7 +409,7 @@ public bool VerifyKey(string key) return VerifyKeyRecursive(key.Split('.'), 0, _serviceModel.GetType()); } - private bool VerifyKeyRecursive(string[] keys, int index, Type currentModel) + private static bool VerifyKeyRecursive(string[] keys, int index, Type currentModel) { if (index == keys.Length) { diff --git a/src/Altinn.App.Core/Implementation/AppResourcesSI.cs b/src/Altinn.App.Core/Implementation/AppResourcesSI.cs index 5adb31c4f..6313153a5 100644 --- a/src/Altinn.App.Core/Implementation/AppResourcesSI.cs +++ b/src/Altinn.App.Core/Implementation/AppResourcesSI.cs @@ -8,7 +8,6 @@ using Altinn.App.Core.Models.Layout; using Altinn.App.Core.Models.Layout.Components; using Altinn.Platform.Storage.Interface.Models; -using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Newtonsoft.Json; @@ -30,7 +29,6 @@ public class AppResourcesSI : IAppResources private readonly AppSettings _settings; private readonly IAppMetadata _appMetadata; - private readonly IWebHostEnvironment _hostingEnvironment; private readonly ILogger _logger; private readonly Telemetry? _telemetry; @@ -39,20 +37,17 @@ public class AppResourcesSI : IAppResources /// /// The app repository settings. /// App metadata service - /// The hosting environment /// A logger from the built in logger factory. /// Telemetry for traces and metrics. public AppResourcesSI( IOptions settings, IAppMetadata appMetadata, - IWebHostEnvironment hostingEnvironment, ILogger logger, Telemetry? telemetry = null ) { _settings = settings.Value; _appMetadata = appMetadata; - _hostingEnvironment = hostingEnvironment; _logger = logger; _telemetry = telemetry; } @@ -403,7 +398,7 @@ public byte[] GetRuleHandlerForSet(string id) return ReadFileByte(filename); } - private byte[] ReadFileByte(string fileName) + private static byte[] ReadFileByte(string fileName) { byte[]? filedata = null; if (File.Exists(fileName)) @@ -416,7 +411,7 @@ private byte[] ReadFileByte(string fileName) #nullable restore } - private byte[] ReadFileContentsFromLegalPath(string legalPath, string filePath) + private static byte[] ReadFileContentsFromLegalPath(string legalPath, string filePath) { var fullFileName = legalPath + filePath; if (!PathHelper.ValidateLegalFilePath(legalPath, fullFileName)) diff --git a/src/Altinn.App.Core/Implementation/PrefillSI.cs b/src/Altinn.App.Core/Implementation/PrefillSI.cs index 895380dc8..19b00756f 100644 --- a/src/Altinn.App.Core/Implementation/PrefillSI.cs +++ b/src/Altinn.App.Core/Implementation/PrefillSI.cs @@ -313,7 +313,7 @@ private void LoopThroughDictionaryAndAssignValuesToDataModel( } } - private Dictionary SwapKeyValuesForPrefill(Dictionary externalPrefil) + private static Dictionary SwapKeyValuesForPrefill(Dictionary externalPrefil) { return externalPrefil.ToDictionary(x => x.Value, x => x.Key); } diff --git a/src/Altinn.App.Core/Infrastructure/Clients/Events/EventsSubscriptionClient.cs b/src/Altinn.App.Core/Infrastructure/Clients/Events/EventsSubscriptionClient.cs index 8cef0e9d4..cac3cc038 100644 --- a/src/Altinn.App.Core/Infrastructure/Clients/Events/EventsSubscriptionClient.cs +++ b/src/Altinn.App.Core/Infrastructure/Clients/Events/EventsSubscriptionClient.cs @@ -17,7 +17,6 @@ public class EventsSubscriptionClient : IEventsSubscription { private static readonly JsonSerializerOptions _jsonSerializerOptions = new() { PropertyNameCaseInsensitive = true }; - private readonly PlatformSettings _platformSettings; private readonly GeneralSettings _generalSettings; private readonly HttpClient _client; private readonly IEventSecretCodeProvider _secretCodeProvider; @@ -34,7 +33,6 @@ public EventsSubscriptionClient( ILogger logger ) { - _platformSettings = platformSettings.Value; _generalSettings = generalSettings.Value; httpClient.BaseAddress = new Uri(platformSettings.Value.ApiEventsEndpoint); httpClient.DefaultRequestHeaders.Add(General.SubscriptionKeyHeaderName, platformSettings.Value.SubscriptionKey); diff --git a/src/Altinn.App.Core/Infrastructure/Clients/Register/PersonClient.cs b/src/Altinn.App.Core/Infrastructure/Clients/Register/PersonClient.cs index a34ec5890..846fdfd66 100644 --- a/src/Altinn.App.Core/Infrastructure/Clients/Register/PersonClient.cs +++ b/src/Altinn.App.Core/Infrastructure/Clients/Register/PersonClient.cs @@ -81,7 +81,7 @@ private async Task AddAuthHeaders(HttpRequestMessage request) request.Headers.Add("Authorization", "Bearer " + _userTokenProvider.GetUserToken()); } - private async Task ReadResponse(HttpResponseMessage response, CancellationToken ct) + private static async Task ReadResponse(HttpResponseMessage response, CancellationToken ct) { if (response.StatusCode == HttpStatusCode.OK) { diff --git a/src/Altinn.App.Core/Internal/Language/ApplicationLanguage.cs b/src/Altinn.App.Core/Internal/Language/ApplicationLanguage.cs index 1d1bf97df..de7829e3c 100644 --- a/src/Altinn.App.Core/Internal/Language/ApplicationLanguage.cs +++ b/src/Altinn.App.Core/Internal/Language/ApplicationLanguage.cs @@ -1,8 +1,6 @@ using System.Text.Json; using Altinn.App.Core.Configuration; using Altinn.App.Core.Features; -using Altinn.App.Core.Implementation; -using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace Altinn.App.Core.Internal.Language; @@ -16,23 +14,16 @@ public class ApplicationLanguage : IApplicationLanguage new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; private readonly AppSettings _settings; - private readonly ILogger _logger; private readonly Telemetry? _telemetry; /// /// Initializes a new instance of the class. /// /// The app repository settings. - /// A logger from the built in logger factory. /// Telemetry for traces and metrics. - public ApplicationLanguage( - IOptions settings, - ILogger logger, - Telemetry? telemetry = null - ) + public ApplicationLanguage(IOptions settings, Telemetry? telemetry = null) { _settings = settings.Value; - _logger = logger; _telemetry = telemetry; } diff --git a/src/Altinn.App.Core/Internal/Patch/IPatchService.cs b/src/Altinn.App.Core/Internal/Patch/IPatchService.cs index 6a0e74869..f257a3395 100644 --- a/src/Altinn.App.Core/Internal/Patch/IPatchService.cs +++ b/src/Altinn.App.Core/Internal/Patch/IPatchService.cs @@ -1,4 +1,3 @@ -using System.Text.Json.Nodes; using Altinn.App.Core.Models.Result; using Altinn.Platform.Storage.Interface.Models; using Json.Patch; diff --git a/src/Altinn.App.Core/Models/Layout/PageComponentConverter.cs b/src/Altinn.App.Core/Models/Layout/PageComponentConverter.cs index 0ac0596b1..45c26e264 100644 --- a/src/Altinn.App.Core/Models/Layout/PageComponentConverter.cs +++ b/src/Altinn.App.Core/Models/Layout/PageComponentConverter.cs @@ -48,7 +48,7 @@ public static void SetAsyncLocalPageName(string pageName) /// /// Similar to read, but not nullable, and no pageName hack. /// - public PageComponent ReadNotNull(ref Utf8JsonReader reader, string pageName, JsonSerializerOptions options) + public static PageComponent ReadNotNull(ref Utf8JsonReader reader, string pageName, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartObject) { @@ -87,7 +87,7 @@ public PageComponent ReadNotNull(ref Utf8JsonReader reader, string pageName, Jso return page; } - private PageComponent ReadData(ref Utf8JsonReader reader, string pageName, JsonSerializerOptions options) + private static PageComponent ReadData(ref Utf8JsonReader reader, string pageName, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartObject) { @@ -156,10 +156,11 @@ private PageComponent ReadData(ref Utf8JsonReader reader, string pageName, JsonS return new PageComponent(pageName, layout, componentLookup, hidden, required, readOnly, additionalProperties); } - private (List, Dictionary, Dictionary) ReadLayout( - ref Utf8JsonReader reader, - JsonSerializerOptions options - ) + private static ( + List, + Dictionary, + Dictionary + ) ReadLayout(ref Utf8JsonReader reader, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartArray) { @@ -248,7 +249,7 @@ Dictionary childToGroupMapping } } - private BaseComponent ReadComponent(ref Utf8JsonReader reader, JsonSerializerOptions options) + private static BaseComponent ReadComponent(ref Utf8JsonReader reader, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartObject) { diff --git a/test/Altinn.App.Api.Tests/Controllers/InstancesController_PostNewInstance.cs b/test/Altinn.App.Api.Tests/Controllers/InstancesController_PostNewInstance.cs index c4a32508f..8184bf51f 100644 --- a/test/Altinn.App.Api.Tests/Controllers/InstancesController_PostNewInstance.cs +++ b/test/Altinn.App.Api.Tests/Controllers/InstancesController_PostNewInstance.cs @@ -82,7 +82,7 @@ public async Task PostNewInstanceWithContent_EnsureDataIsPresent() readDataElementResponseParsed.Melding!.Name.Should().Be(testName); } - private async Task CreateInstanceSimplified( + private static async Task CreateInstanceSimplified( string org, string app, int instanceOwnerPartyId, diff --git a/test/Altinn.App.Api.Tests/Helpers/StartupHelperTests.cs b/test/Altinn.App.Api.Tests/Helpers/StartupHelperTests.cs index 7611fcca7..b5be9b370 100644 --- a/test/Altinn.App.Api.Tests/Helpers/StartupHelperTests.cs +++ b/test/Altinn.App.Api.Tests/Helpers/StartupHelperTests.cs @@ -34,7 +34,7 @@ public void IncludeXmlComments_calls_delegate_function_with_expected_values() public void IncludeXmlComments_discards_exceptions() { var testDouble = new SwaggerIncludeXmlCommentsTestDouble(); - StartupHelper.IncludeXmlComments(testDouble.IncludeXmlCommentsFailingTestDouble); + StartupHelper.IncludeXmlComments(SwaggerIncludeXmlCommentsTestDouble.IncludeXmlCommentsFailingTestDouble); testDouble.GetStrings().Should().HaveCount(0); testDouble.GetBools().Should().HaveCount(0); } diff --git a/test/Altinn.App.Api.Tests/Telemetry/TelemetryConfigurationTests.cs b/test/Altinn.App.Api.Tests/Telemetry/TelemetryConfigurationTests.cs index 483d72af2..68fecc729 100644 --- a/test/Altinn.App.Api.Tests/Telemetry/TelemetryConfigurationTests.cs +++ b/test/Altinn.App.Api.Tests/Telemetry/TelemetryConfigurationTests.cs @@ -344,7 +344,7 @@ public async Task OpenTelemetry_MetricReaderOptions_Override_Is_Possible_Through Assert.Equal(timeoutToUse, options.ExportTimeoutMilliseconds); } - private Sampler GetSampler(TracerProvider provider) + private static Sampler GetSampler(TracerProvider provider) { var property = provider.GetType().GetProperty("Sampler", BindingFlags.Instance | BindingFlags.NonPublic) diff --git a/test/Altinn.App.Api.Tests/TestStubs/SwaggerIncludeXmlCommentsTestDouble.cs b/test/Altinn.App.Api.Tests/TestStubs/SwaggerIncludeXmlCommentsTestDouble.cs index 6f36d8a84..d76a26172 100644 --- a/test/Altinn.App.Api.Tests/TestStubs/SwaggerIncludeXmlCommentsTestDouble.cs +++ b/test/Altinn.App.Api.Tests/TestStubs/SwaggerIncludeXmlCommentsTestDouble.cs @@ -12,7 +12,7 @@ public void IncludeXmlCommentsTestDouble(string s, bool b) _bools.Add(b); } - public void IncludeXmlCommentsFailingTestDouble(string s, bool b) + public static void IncludeXmlCommentsFailingTestDouble(string s, bool b) { throw new Exception("xUnit expected exception"); } diff --git a/test/Altinn.App.Common.Tests/TelemetrySink.cs b/test/Altinn.App.Common.Tests/TelemetrySink.cs index 9601407e2..166214b5f 100644 --- a/test/Altinn.App.Common.Tests/TelemetrySink.cs +++ b/test/Altinn.App.Common.Tests/TelemetrySink.cs @@ -77,10 +77,10 @@ public sealed record TelemetrySink : IDisposable public TelemetrySnapshot GetSnapshot() => new(CapturedActivities, CapturedMetrics); - public TelemetrySnapshot GetSnapshot(Activity activity) => + public static TelemetrySnapshot GetSnapshot(Activity activity) => new([activity], new Dictionary>()); - public TelemetrySnapshot GetSnapshot(IEnumerable activities) => + public static TelemetrySnapshot GetSnapshot(IEnumerable activities) => new(activities, new Dictionary>()); public async Task Snapshot( diff --git a/test/Altinn.App.Core.Tests/Features/Action/SigningUserActionTests.cs b/test/Altinn.App.Core.Tests/Features/Action/SigningUserActionTests.cs index 25cfc56d2..2c80fceda 100644 --- a/test/Altinn.App.Core.Tests/Features/Action/SigningUserActionTests.cs +++ b/test/Altinn.App.Core.Tests/Features/Action/SigningUserActionTests.cs @@ -296,7 +296,7 @@ private static (SigningUserAction SigningUserAction, Mock SignClien ); } - private bool AssertSigningContextAsExpected(SignatureContext s1, SignatureContext s2) + private static bool AssertSigningContextAsExpected(SignatureContext s1, SignatureContext s2) { s1.Should().BeEquivalentTo(s2); return true; diff --git a/test/Altinn.App.Core.Tests/Features/Action/UniqueSignatureAuthorizerTests.cs b/test/Altinn.App.Core.Tests/Features/Action/UniqueSignatureAuthorizerTests.cs index 7773e990d..0c82d2e93 100644 --- a/test/Altinn.App.Core.Tests/Features/Action/UniqueSignatureAuthorizerTests.cs +++ b/test/Altinn.App.Core.Tests/Features/Action/UniqueSignatureAuthorizerTests.cs @@ -14,7 +14,7 @@ namespace Altinn.App.Core.Tests.Features.Action; -public class UniqueSignatureAuthorizerTests : IDisposable +public sealed class UniqueSignatureAuthorizerTests : IDisposable { private readonly Mock _processReaderMock; private readonly Mock _instanceClientMock; diff --git a/test/Altinn.App.Core.Tests/Features/Validators/Default/DataAnnotationValidatorTests.cs b/test/Altinn.App.Core.Tests/Features/Validators/Default/DataAnnotationValidatorTests.cs index fa44e4ca3..cee5acce6 100644 --- a/test/Altinn.App.Core.Tests/Features/Validators/Default/DataAnnotationValidatorTests.cs +++ b/test/Altinn.App.Core.Tests/Features/Validators/Default/DataAnnotationValidatorTests.cs @@ -13,7 +13,7 @@ namespace Altinn.App.Core.Tests.Features.Validators.Default; -public class DataAnnotationValidatorTests : IClassFixture +public sealed class DataAnnotationValidatorTests : IClassFixture { private readonly DataAnnotationValidator _validator; @@ -168,7 +168,7 @@ public async Task ValidateFormData_RequiredProperty() /// /// A full WebApplicationFactory seemed a little overkill, so we just use a WebApplicationBuilder. /// -public class DataAnnotationsTestFixture : IAsyncDisposable +public sealed class DataAnnotationsTestFixture : IAsyncDisposable { public const string DataType = "test"; diff --git a/test/Altinn.App.Core.Tests/Features/Validators/Default/ExpressionValidatorTests.cs b/test/Altinn.App.Core.Tests/Features/Validators/Default/ExpressionValidatorTests.cs index c77f805c1..4843a88e3 100644 --- a/test/Altinn.App.Core.Tests/Features/Validators/Default/ExpressionValidatorTests.cs +++ b/test/Altinn.App.Core.Tests/Features/Validators/Default/ExpressionValidatorTests.cs @@ -48,7 +48,7 @@ public ExpressionValidatorTests() private static readonly JsonSerializerOptions _jsonSerializerOptions = new() { ReadCommentHandling = JsonCommentHandling.Skip, PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; - public ExpressionValidationTestModel LoadData(string fileName, string folder) + public static ExpressionValidationTestModel LoadData(string fileName, string folder) { var data = File.ReadAllText(Path.Join(folder, fileName)); return JsonSerializer.Deserialize(data, _jsonSerializerOptions)!; diff --git a/test/Altinn.App.Core.Tests/Features/Validators/ValidationServiceTests.cs b/test/Altinn.App.Core.Tests/Features/Validators/ValidationServiceTests.cs index a9a67f3d3..0dc0156ab 100644 --- a/test/Altinn.App.Core.Tests/Features/Validators/ValidationServiceTests.cs +++ b/test/Altinn.App.Core.Tests/Features/Validators/ValidationServiceTests.cs @@ -15,7 +15,7 @@ namespace Altinn.App.Core.Tests.Features.Validators; -public class ValidationServiceTests : IDisposable +public sealed class ValidationServiceTests : IDisposable { private class MyModel { @@ -127,13 +127,17 @@ public ValidationServiceTests() SetupFormDataValidatorType(_formDataValidatorAlwaysMock, "*", "alwaysUsedValidator"); } - private void SetupTaskValidatorType(Mock taskValidatorMock, string taskId, string validationSource) + private static void SetupTaskValidatorType( + Mock taskValidatorMock, + string taskId, + string validationSource + ) { taskValidatorMock.Setup(v => v.TaskId).Returns(taskId); taskValidatorMock.Setup(v => v.ValidationSource).Returns(validationSource); } - private void SetupTaskValidatorReturn( + private static void SetupTaskValidatorReturn( Mock taskValidatorMock, List validationIssues, Times? times = default @@ -145,7 +149,7 @@ private void SetupTaskValidatorReturn( .Verifiable(times ?? Times.Once()); } - private void SetupDataElementValidatorType( + private static void SetupDataElementValidatorType( Mock taskValidatorMock, string dataType, string validationSource @@ -155,7 +159,7 @@ string validationSource taskValidatorMock.Setup(v => v.ValidationSource).Returns(validationSource); } - private void SetupDataElementValidatorReturn( + private static void SetupDataElementValidatorReturn( Mock dataElementValidatorMock, List validationIssues, Times? times = default @@ -167,7 +171,7 @@ private void SetupDataElementValidatorReturn( .Verifiable(times ?? Times.Once()); } - private void SetupFormDataValidatorType( + private static void SetupFormDataValidatorType( Mock formDataValidatorMock, string dataType, string validationSource @@ -180,7 +184,7 @@ string validationSource formDataValidatorMock.Setup(v => v.ValidationSource).Returns(validationSource); } - private void SetupFormDataValidatorReturn( + private static void SetupFormDataValidatorReturn( Mock formDataValidatorMock, bool? hasRelevantChanges, Func> func, diff --git a/test/Altinn.App.Core.Tests/Helpers/JsonDataModel.cs b/test/Altinn.App.Core.Tests/Helpers/JsonDataModel.cs index e5f63e48f..c2946839b 100644 --- a/test/Altinn.App.Core.Tests/Helpers/JsonDataModel.cs +++ b/test/Altinn.App.Core.Tests/Helpers/JsonDataModel.cs @@ -35,7 +35,12 @@ public JsonDataModel(JsonObject? modelRoot) return GetModelDataRecursive(key.Split('.'), 0, _modelRoot, indicies); } - private object? GetModelDataRecursive(string[] keys, int index, JsonNode? currentModel, ReadOnlySpan indicies) + private static object? GetModelDataRecursive( + string[] keys, + int index, + JsonNode? currentModel, + ReadOnlySpan indicies + ) { if (currentModel is null) { @@ -114,7 +119,12 @@ currentModel is not JsonObject return GetModelDataCountRecurs(key.Split('.'), 0, _modelRoot, indicies); } - private int? GetModelDataCountRecurs(string[] keys, int index, JsonNode? currentModel, ReadOnlySpan indicies) + private static int? GetModelDataCountRecurs( + string[] keys, + int index, + JsonNode? currentModel, + ReadOnlySpan indicies + ) { if (index == keys.Length || currentModel is null) { diff --git a/test/Altinn.App.Core.Tests/Helpers/JsonHelperTests.cs b/test/Altinn.App.Core.Tests/Helpers/JsonHelperTests.cs index 36090d78c..67e17de89 100644 --- a/test/Altinn.App.Core.Tests/Helpers/JsonHelperTests.cs +++ b/test/Altinn.App.Core.Tests/Helpers/JsonHelperTests.cs @@ -13,7 +13,7 @@ public class JsonHelperTests /// /// Helper method to setup and get the dictionary of the diffs /// - public async Task?> DoTest( + public static async Task?> DoTest( TModel model, Func processDataWriteImpl ) diff --git a/test/Altinn.App.Core.Tests/Helpers/ObjectUtils_XmlSerializationTests.cs b/test/Altinn.App.Core.Tests/Helpers/ObjectUtils_XmlSerializationTests.cs index 55a682ac4..8c3c9fad1 100644 --- a/test/Altinn.App.Core.Tests/Helpers/ObjectUtils_XmlSerializationTests.cs +++ b/test/Altinn.App.Core.Tests/Helpers/ObjectUtils_XmlSerializationTests.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; @@ -15,6 +16,7 @@ namespace Altinn.App.Core.Tests.Helpers; +[SuppressMessage("Performance", "CA1822:Mark members as static")] public class ObjectUtils_XmlSerializationTests(ITestOutputHelper _output) { private readonly Mock _loggerMock = new(); diff --git a/test/Altinn.App.Core.Tests/Implementation/AppResourcesSITests.cs b/test/Altinn.App.Core.Tests/Implementation/AppResourcesSITests.cs index 089945319..99d9eb233 100644 --- a/test/Altinn.App.Core.Tests/Implementation/AppResourcesSITests.cs +++ b/test/Altinn.App.Core.Tests/Implementation/AppResourcesSITests.cs @@ -26,8 +26,7 @@ public void GetApplication_desrializes_file_from_disk() AppSettings appSettings = GetAppSettings("AppMetadata", "default.applicationmetadata.json"); var settings = Options.Create(appSettings); IAppMetadata appMetadata = SetupAppMetadata(Options.Create(appSettings)); - AppResourcesSI appResources = - new(settings, appMetadata, null, new NullLogger(), _telemetry.Object); + AppResourcesSI appResources = new(settings, appMetadata, new NullLogger(), _telemetry.Object); Application expected = new() { @@ -73,8 +72,7 @@ public void GetApplication_handles_onEntry_null() AppSettings appSettings = GetAppSettings("AppMetadata", "no-on-entry.applicationmetadata.json"); var settings = Options.Create(appSettings); IAppMetadata appMetadata = SetupAppMetadata(Options.Create(appSettings)); - AppResourcesSI appResources = - new(settings, appMetadata, null, new NullLogger(), _telemetry.Object); + AppResourcesSI appResources = new(settings, appMetadata, new NullLogger(), _telemetry.Object); Application expected = new Application() { Id = "tdd/bestilling", @@ -122,8 +120,7 @@ public void GetApplication_second_read_from_cache() .ReturnsAsync(new Dictionary() { { "footer", true } }); var settings = Options.Create(appSettings); IAppMetadata appMetadata = SetupAppMetadata(Options.Create(appSettings), appFeaturesMock.Object); - AppResourcesSI appResources = - new(settings, appMetadata, null, new NullLogger(), _telemetry.Object); + AppResourcesSI appResources = new(settings, appMetadata, new NullLogger(), _telemetry.Object); Application expected = new() { @@ -176,7 +173,6 @@ public void GetApplicationMetadata_throws_ApplicationConfigException_if_file_not IAppResources appResources = new AppResourcesSI( settings, appMetadata, - null, new NullLogger(), _telemetry.Object ); @@ -192,7 +188,6 @@ public void GetApplicationMetadata_throws_ApplicationConfigException_if_deserial IAppResources appResources = new AppResourcesSI( settings, appMetadata, - null, new NullLogger(), _telemetry.Object ); @@ -208,7 +203,6 @@ public void GetApplicationXACMLPolicy_return_policyfile_as_string() IAppResources appResources = new AppResourcesSI( settings, appMetadata, - null, new NullLogger(), _telemetry.Object ); @@ -226,7 +220,6 @@ public void GetApplicationXACMLPolicy_return_null_if_file_not_found() IAppResources appResources = new AppResourcesSI( settings, appMetadata, - null, new NullLogger(), _telemetry.Object ); @@ -243,7 +236,6 @@ public void GetApplicationBPMNProcess_return_process_as_string() IAppResources appResources = new AppResourcesSI( settings, appMetadata, - null, new NullLogger(), _telemetry.Object ); @@ -261,7 +253,6 @@ public void GetApplicationBPMNProcess_return_null_if_file_not_found() IAppResources appResources = new AppResourcesSI( settings, appMetadata, - null, new NullLogger(), _telemetry.Object ); diff --git a/test/Altinn.App.Core.Tests/Implementation/InstanceClientTests.cs b/test/Altinn.App.Core.Tests/Implementation/InstanceClientTests.cs index 1c4a9ac1a..59ad71b36 100644 --- a/test/Altinn.App.Core.Tests/Implementation/InstanceClientTests.cs +++ b/test/Altinn.App.Core.Tests/Implementation/InstanceClientTests.cs @@ -17,7 +17,7 @@ namespace Altinn.App.PlatformServices.Tests.Implementation; -public class InstanceClientTests : IDisposable +public sealed class InstanceClientTests : IDisposable { private readonly Mock> platformSettingsOptions; private readonly Mock> appSettingsOptions; diff --git a/test/Altinn.App.Core.Tests/Infrastructure/Clients/Profile/ProfileClientTests.cs b/test/Altinn.App.Core.Tests/Infrastructure/Clients/Profile/ProfileClientTests.cs index eb5067063..e4e9b84a6 100644 --- a/test/Altinn.App.Core.Tests/Infrastructure/Clients/Profile/ProfileClientTests.cs +++ b/test/Altinn.App.Core.Tests/Infrastructure/Clients/Profile/ProfileClientTests.cs @@ -27,7 +27,7 @@ private readonly record struct Fixture(ServiceProvider ServiceProvider, Mock ServiceProvider.DisposeAsync(); } - private Fixture BuildFixture(Func? userProfileFactory = null) + private static Fixture BuildFixture(Func? userProfileFactory = null) { var services = new ServiceCollection(); diff --git a/test/Altinn.App.Core.Tests/Infrastructure/Clients/Storage/DataClientTests.cs b/test/Altinn.App.Core.Tests/Infrastructure/Clients/Storage/DataClientTests.cs index b79015b0b..0a17f7510 100644 --- a/test/Altinn.App.Core.Tests/Infrastructure/Clients/Storage/DataClientTests.cs +++ b/test/Altinn.App.Core.Tests/Infrastructure/Clients/Storage/DataClientTests.cs @@ -847,7 +847,7 @@ private DataClient GetDataClient( ); } - private void AssertHttpRequest( + private static void AssertHttpRequest( HttpRequestMessage actual, Uri expectedUri, HttpMethod method, diff --git a/test/Altinn.App.Core.Tests/Internal/Patch/PatchServiceTests.cs b/test/Altinn.App.Core.Tests/Internal/Patch/PatchServiceTests.cs index 49eba51d9..2beb98e65 100644 --- a/test/Altinn.App.Core.Tests/Internal/Patch/PatchServiceTests.cs +++ b/test/Altinn.App.Core.Tests/Internal/Patch/PatchServiceTests.cs @@ -18,7 +18,7 @@ namespace Altinn.App.Core.Tests.Internal.Patch; -public class PatchServiceTests : IDisposable +public sealed class PatchServiceTests : IDisposable { // Test data private static readonly Guid DataGuid = new("12345678-1234-1234-1234-123456789123"); diff --git a/test/Altinn.App.Core.Tests/Internal/Process/ProcessEngineTest.cs b/test/Altinn.App.Core.Tests/Internal/Process/ProcessEngineTest.cs index 043239c4b..8700aac22 100644 --- a/test/Altinn.App.Core.Tests/Internal/Process/ProcessEngineTest.cs +++ b/test/Altinn.App.Core.Tests/Internal/Process/ProcessEngineTest.cs @@ -20,7 +20,7 @@ namespace Altinn.App.Core.Tests.Internal.Process; -public class ProcessEngineTest : IDisposable +public sealed class ProcessEngineTest : IDisposable { private Mock _processReaderMock; private readonly Mock _profileMock; diff --git a/test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/LayoutModelConverterFromObject.cs b/test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/LayoutModelConverterFromObject.cs index 9ec0925e8..c7d607a79 100644 --- a/test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/LayoutModelConverterFromObject.cs +++ b/test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/LayoutModelConverterFromObject.cs @@ -45,9 +45,8 @@ public class LayoutModelConverterFromObject : JsonConverter reader.Read(); PageComponentConverter.SetAsyncLocalPageName(pageName); - var converter = new PageComponentConverter(); - componentModel.Pages[pageName] = converter.ReadNotNull(ref reader, pageName, options); + componentModel.Pages[pageName] = PageComponentConverter.ReadNotNull(ref reader, pageName, options); } return componentModel; diff --git a/test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/TestFunctions.cs b/test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/TestFunctions.cs index 6395ad6b8..00d055da7 100644 --- a/test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/TestFunctions.cs +++ b/test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/TestFunctions.cs @@ -220,6 +220,7 @@ public void Ensure_tests_For_All_Folders() // This is just a way to ensure that all folders have test methods associcated. var jsonTestFolders = Directory .GetDirectories(Path.Join("LayoutExpressions", "CommonTests", "shared-tests", "functions")) + .Where(d => Directory.GetFiles(d).Length > 0) .Select(d => Path.GetFileName(d)) .OrderBy(s => s) .ToArray(); @@ -232,10 +233,9 @@ public void Ensure_tests_For_All_Folders() ) .OrderBy(s => s) .OfType() + .OrderBy(d => d) .ToArray(); - testMethods - .Should() - .BeEquivalentTo(jsonTestFolders, "Shared test folders should have a corresponding test method"); + testMethods.Should().Equal(jsonTestFolders, "Shared test folders should have a corresponding test method"); } } diff --git a/test/Altinn.App.Core.Tests/LayoutExpressions/ExpressionEvaluatorTests/EqualsTests.cs b/test/Altinn.App.Core.Tests/LayoutExpressions/ExpressionEvaluatorTests/EqualsTests.cs index 275bba8d1..3d871217f 100644 --- a/test/Altinn.App.Core.Tests/LayoutExpressions/ExpressionEvaluatorTests/EqualsTests.cs +++ b/test/Altinn.App.Core.Tests/LayoutExpressions/ExpressionEvaluatorTests/EqualsTests.cs @@ -1,53 +1,92 @@ using System.Numerics; using System.Text.Json; using Altinn.App.Core.Internal.Expressions; +using FluentAssertions; using Xunit.Abstractions; namespace Altinn.App.Core.Tests.LayoutExpressions.ExpressionEvaluatorTests; public class EqualTests(ITestOutputHelper outputHelper) { - public static TheoryData GetNumericTestData(double value) => + public static TheoryData GetNumericTestData(double value) => new() { - value, - (byte)value, - (sbyte)value, - (short)value, - (ushort)value, - (int)value, - (uint)value, - (long)value, - (ulong)value, - (float)value, - (decimal)value, + { typeof(double), value }, + { typeof(byte), (byte)Math.Abs(value) }, + { typeof(sbyte), (sbyte)value }, + { typeof(short), (short)value }, + { typeof(ushort), (ushort)value }, + { typeof(int), (int)value }, + { typeof(uint), (uint)Math.Abs(value) }, + { typeof(long), (long)value }, + { typeof(ulong), (ulong)Math.Abs(value) }, + { typeof(float), (float)value }, + { typeof(decimal), (decimal)value }, + { typeof(double?), value }, + { typeof(byte?), (byte?)Math.Abs(value) }, + { typeof(sbyte?), (sbyte?)value }, + { typeof(short?), (short?)value }, + { typeof(ushort?), (ushort?)Math.Abs(value) }, + { typeof(int?), (int?)value }, + { typeof(uint?), (uint?)Math.Abs(value) }, + { typeof(long?), (long?)value }, + { typeof(ulong?), (ulong?)Math.Abs(value) }, + { typeof(float?), (float?)value }, + { typeof(decimal?), (decimal?)value }, // (BigInteger)value, // Not supported by JsonSerializer }; - public static TheoryData GetExoticTypes => + public static TheoryData GetNullNumericData() => new() { - "123", - true, - false, - "", - DateTime.Now, - DateOnly.FromDateTime(DateTime.Now), - TimeOnly.FromDateTime(DateTime.Now), + { typeof(double?), (double?)null }, + { typeof(byte?), (byte?)null }, + { typeof(sbyte?), (sbyte?)null }, + { typeof(short?), (short?)null }, + { typeof(ushort?), (ushort?)null }, + { typeof(int?), (int?)null }, + { typeof(uint?), (uint?)null }, + { typeof(long?), (long?)null }, + { typeof(ulong?), (ulong?)null }, + { typeof(float?), (float?)null }, + { typeof(decimal?), (decimal?)null }, + }; + + public static TheoryData GetExoticTypes => + new() + { + { typeof(string), "123" }, + { typeof(bool), true }, + { typeof(bool), false }, + { typeof(string), "" }, + { typeof(DateTime), DateTime.Now }, + { typeof(DateOnly), DateOnly.FromDateTime(DateTime.Now) }, + { typeof(TimeOnly), TimeOnly.FromDateTime(DateTime.Now) }, }; [Theory] [MemberData(nameof(GetNumericTestData), 123.0)] [MemberData(nameof(GetNumericTestData), 0.5)] - [MemberData(nameof(GetNumericTestData), -123.0)] + [MemberData(nameof(GetNumericTestData), -11.0)] + [MemberData(nameof(GetNullNumericData))] [MemberData(nameof(GetExoticTypes))] - public void ToStringForEquals_AgreesWithJsonSerializer(object? value) + public void ToStringForEquals_AgreesWithJsonSerializer(Type type, object? value) { - outputHelper.WriteLine($"Object of type {value?.GetType().FullName ?? "null"}:"); + outputHelper.WriteLine($"Object of type {type.Name}:"); outputHelper.WriteLine($" value:{value}"); outputHelper.WriteLine($" json: {JsonSerializer.Serialize(value)}"); // Verify that the EqualsToString method returns the same value as the JsonSerializer. - var json = value is string ? value : JsonSerializer.Serialize(value); + // apart from the issue of: + var json = value switch + { + // null: Json returns "null", while ToStringForEquals returns C# null + null => null, + // strings: Json adds "" around the string, while ToStringForEquals does not + string => value, + // In remaining cases use JsonSerializer to get numbers ++ formatted as strings + _ => JsonSerializer.Serialize(value) + }; + var toStringForEquals = ExpressionEvaluator.ToStringForEquals(value); Assert.Equal(json, toStringForEquals); } diff --git a/test/Altinn.App.Core.Tests/Models/PageComponentConverterTests.cs b/test/Altinn.App.Core.Tests/Models/PageComponentConverterTests.cs index f486c57fe..3ebc0e41f 100644 --- a/test/Altinn.App.Core.Tests/Models/PageComponentConverterTests.cs +++ b/test/Altinn.App.Core.Tests/Models/PageComponentConverterTests.cs @@ -37,13 +37,13 @@ public void RunPageComponentConverterTest(string fileName, string folder) } } - private PageComponentConverterTestModel LoadData(string fileName, string folder) + private static PageComponentConverterTestModel LoadData(string fileName, string folder) { var data = File.ReadAllText(Path.Join(folder, fileName)); return JsonSerializer.Deserialize(data, _jsonSerializerOptions)!; } - private HierarchyTestModel[] GenerateTestHierarchy(GroupComponent group) + private static HierarchyTestModel[] GenerateTestHierarchy(GroupComponent group) { var children = new List(); foreach (var child in group.Children)