From 7a57d060021bb93a99323ede34e26d2e6cc6c6e3 Mon Sep 17 00:00:00 2001 From: Ivar Nesje Date: Fri, 15 Nov 2024 22:02:29 +0100 Subject: [PATCH] Fix sonar issues and improve test coverage --- .../Validation/Default/ExpressionValidator.cs | 5 +-- .../Wrappers/DataElementValidatorWrapper.cs | 8 +--- .../Implementation/AppResourcesSI.cs | 4 +- .../Internal/Validation/IValidatorFactory.cs | 3 +- .../Controllers/ActionsControllerTests.cs | 4 +- ...dator_ReturnsValidationErrors.verified.txt | 41 +++++++++++++++-- ...sNext_PdfFails_DataIsUnlocked.verified.txt | 45 ++++++++++++++++--- .../models/default.validation.json | 18 ++++++++ 8 files changed, 105 insertions(+), 23 deletions(-) create mode 100644 test/Altinn.App.Api.Tests/Data/apps/tdd/contributer-restriction/models/default.validation.json diff --git a/src/Altinn.App.Core/Features/Validation/Default/ExpressionValidator.cs b/src/Altinn.App.Core/Features/Validation/Default/ExpressionValidator.cs index e0094da84..58ec71910 100644 --- a/src/Altinn.App.Core/Features/Validation/Default/ExpressionValidator.cs +++ b/src/Altinn.App.Core/Features/Validation/Default/ExpressionValidator.cs @@ -54,12 +54,11 @@ IAppMetadata appMetadata public bool ShouldRunForTask(string taskId) => _appMetadata .GetApplicationMetadata() - .Result.DataTypes.Where(dt => + .Result.DataTypes.Exists(dt => dt.TaskId == taskId && dt.AppLogic?.ClassRef is not null && _appResourceService.GetValidationConfiguration(dt.Id) is not null - ) - .Any(); + ); /// /// This validator has the code "Expression" and this is known by the frontend, who may request this validator to not run for incremental validation. diff --git a/src/Altinn.App.Core/Features/Validation/Wrappers/DataElementValidatorWrapper.cs b/src/Altinn.App.Core/Features/Validation/Wrappers/DataElementValidatorWrapper.cs index 5e24d6241..e9ca6f06d 100644 --- a/src/Altinn.App.Core/Features/Validation/Wrappers/DataElementValidatorWrapper.cs +++ b/src/Altinn.App.Core/Features/Validation/Wrappers/DataElementValidatorWrapper.cs @@ -11,17 +11,11 @@ internal class DataElementValidatorWrapper : IValidator { private readonly IDataElementValidator _dataElementValidator; private readonly string _taskId; - private readonly List _dataTypes; - public DataElementValidatorWrapper( - IDataElementValidator dataElementValidator, - string taskId, - List dataTypes - ) + public DataElementValidatorWrapper(IDataElementValidator dataElementValidator, string taskId) { _dataElementValidator = dataElementValidator; _taskId = taskId; - _dataTypes = dataTypes; } /// diff --git a/src/Altinn.App.Core/Implementation/AppResourcesSI.cs b/src/Altinn.App.Core/Implementation/AppResourcesSI.cs index 3310e7961..1046be0aa 100644 --- a/src/Altinn.App.Core/Implementation/AppResourcesSI.cs +++ b/src/Altinn.App.Core/Implementation/AppResourcesSI.cs @@ -498,8 +498,8 @@ private static byte[] ReadFileContentsFromLegalPath(string legalPath, string fil public string? GetValidationConfiguration(string dataTypeId) { using var activity = _telemetry?.StartGetValidationConfigurationActivity(); - string legalPath = $"{_settings.AppBasePath}{_settings.ModelsFolder}"; - string filename = $"{legalPath}{dataTypeId}.{_settings.ValidationConfigurationFileName}"; + string legalPath = Path.Join(_settings.AppBasePath, _settings.ModelsFolder); + string filename = Path.Join(legalPath, $"{dataTypeId}.{_settings.ValidationConfigurationFileName}"); PathHelper.EnsureLegalPath(legalPath, filename); string? filedata = null; diff --git a/src/Altinn.App.Core/Internal/Validation/IValidatorFactory.cs b/src/Altinn.App.Core/Internal/Validation/IValidatorFactory.cs index 7979013c3..1b9480fb9 100644 --- a/src/Altinn.App.Core/Internal/Validation/IValidatorFactory.cs +++ b/src/Altinn.App.Core/Internal/Validation/IValidatorFactory.cs @@ -132,8 +132,7 @@ public IEnumerable GetValidators(string taskId) var dataTypes = _appMetadata.GetApplicationMetadata().Result.DataTypes; validators.AddRange( - GetDataElementValidators(taskId, dataTypes) - .Select(dev => new DataElementValidatorWrapper(dev, taskId, dataTypes)) + GetDataElementValidators(taskId, dataTypes).Select(dev => new DataElementValidatorWrapper(dev, taskId)) ); validators.AddRange( GetFormDataValidators(taskId, dataTypes).Select(fdv => new FormDataValidatorWrapper(fdv, taskId)) diff --git a/test/Altinn.App.Api.Tests/Controllers/ActionsControllerTests.cs b/test/Altinn.App.Api.Tests/Controllers/ActionsControllerTests.cs index 065ad6e4f..c45192e09 100644 --- a/test/Altinn.App.Api.Tests/Controllers/ActionsControllerTests.cs +++ b/test/Altinn.App.Api.Tests/Controllers/ActionsControllerTests.cs @@ -580,7 +580,9 @@ public async Task HandleAction(UserActionContext context) ); break; case "update": - var originalDataElement = context.DataMutator.GetDataElementsForType("Scheme").First(); + var dataType = + context.DataMutator.GetDataType("Scheme") ?? throw new Exception("DataType \"Scheme\" not found"); + var originalDataElement = context.DataMutator.GetDataElementsForType(dataType).First(); var data = await context.DataMutator.GetFormData(originalDataElement); data.TestCustomButtonReadOnlyInput = "Her kommer det data fra backend"; diff --git a/test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.RunProcessNext_FailingValidator_ReturnsValidationErrors.verified.txt b/test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.RunProcessNext_FailingValidator_ReturnsValidationErrors.verified.txt index 59102fb39..baa654b72 100644 --- a/test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.RunProcessNext_FailingValidator_ReturnsValidationErrors.verified.txt +++ b/test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.RunProcessNext_FailingValidator_ReturnsValidationErrors.verified.txt @@ -4,6 +4,18 @@ ActivityName: ApplicationMetadata.Service.GetLayoutModel, IdFormat: W3C }, + { + ActivityName: ApplicationMetadata.Service.GetLayoutModel, + IdFormat: W3C + }, + { + ActivityName: ApplicationMetadata.Service.GetLayoutSet, + IdFormat: W3C + }, + { + ActivityName: ApplicationMetadata.Service.GetLayoutSet, + IdFormat: W3C + }, { ActivityName: ApplicationMetadata.Service.GetLayoutSet, IdFormat: W3C @@ -28,20 +40,28 @@ ActivityName: ApplicationMetadata.Service.GetLayoutSets, IdFormat: W3C }, + { + ActivityName: ApplicationMetadata.Service.GetLayoutSets, + IdFormat: W3C + }, + { + ActivityName: ApplicationMetadata.Service.GetLayoutSets, + IdFormat: W3C + }, { ActivityName: ApplicationMetadata.Service.GetLayoutSetsForTask, IdFormat: W3C }, { - ActivityName: ApplicationMetadata.Service.GetLayoutSettingsForSet, + ActivityName: ApplicationMetadata.Service.GetLayoutSetsForTask, IdFormat: W3C }, { - ActivityName: ApplicationMetadata.Service.GetValidationConfiguration, + ActivityName: ApplicationMetadata.Service.GetLayoutSettingsForSet, IdFormat: W3C }, { - ActivityName: ApplicationMetadata.Service.GetValidationConfiguration, + ActivityName: ApplicationMetadata.Service.GetLayoutSettingsForSet, IdFormat: W3C }, { @@ -153,6 +173,21 @@ ], IdFormat: W3C }, + { + ActivityName: Validation.RunValidator, + Tags: [ + { + validation.issue_count: 0 + }, + { + validator.source: Expression + }, + { + validator.type: ExpressionValidator + } + ], + IdFormat: W3C + }, { ActivityName: Validation.RunValidator, Tags: [ diff --git a/test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.RunProcessNext_PdfFails_DataIsUnlocked.verified.txt b/test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.RunProcessNext_PdfFails_DataIsUnlocked.verified.txt index 27cd52551..3471d54fa 100644 --- a/test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.RunProcessNext_PdfFails_DataIsUnlocked.verified.txt +++ b/test/Altinn.App.Api.Tests/Controllers/ProcessControllerTests.RunProcessNext_PdfFails_DataIsUnlocked.verified.txt @@ -4,6 +4,18 @@ ActivityName: ApplicationMetadata.Service.GetLayoutModel, IdFormat: W3C }, + { + ActivityName: ApplicationMetadata.Service.GetLayoutModel, + IdFormat: W3C + }, + { + ActivityName: ApplicationMetadata.Service.GetLayoutSet, + IdFormat: W3C + }, + { + ActivityName: ApplicationMetadata.Service.GetLayoutSet, + IdFormat: W3C + }, { ActivityName: ApplicationMetadata.Service.GetLayoutSet, IdFormat: W3C @@ -28,28 +40,36 @@ ActivityName: ApplicationMetadata.Service.GetLayoutSets, IdFormat: W3C }, + { + ActivityName: ApplicationMetadata.Service.GetLayoutSets, + IdFormat: W3C + }, + { + ActivityName: ApplicationMetadata.Service.GetLayoutSets, + IdFormat: W3C + }, { ActivityName: ApplicationMetadata.Service.GetLayoutSetsForTask, IdFormat: W3C }, { - ActivityName: ApplicationMetadata.Service.GetLayoutSettingsForSet, + ActivityName: ApplicationMetadata.Service.GetLayoutSetsForTask, IdFormat: W3C }, { - ActivityName: ApplicationMetadata.Service.GetTexts, + ActivityName: ApplicationMetadata.Service.GetLayoutSettingsForSet, IdFormat: W3C }, { - ActivityName: ApplicationMetadata.Service.GetTexts, + ActivityName: ApplicationMetadata.Service.GetLayoutSettingsForSet, IdFormat: W3C }, { - ActivityName: ApplicationMetadata.Service.GetValidationConfiguration, + ActivityName: ApplicationMetadata.Service.GetTexts, IdFormat: W3C }, { - ActivityName: ApplicationMetadata.Service.GetValidationConfiguration, + ActivityName: ApplicationMetadata.Service.GetTexts, IdFormat: W3C }, { @@ -283,6 +303,21 @@ ], IdFormat: W3C }, + { + ActivityName: Validation.RunValidator, + Tags: [ + { + validation.issue_count: 0 + }, + { + validator.source: Expression + }, + { + validator.type: ExpressionValidator + } + ], + IdFormat: W3C + }, { ActivityName: Validation.RunValidator, Tags: [ diff --git a/test/Altinn.App.Api.Tests/Data/apps/tdd/contributer-restriction/models/default.validation.json b/test/Altinn.App.Api.Tests/Data/apps/tdd/contributer-restriction/models/default.validation.json new file mode 100644 index 000000000..73f38c259 --- /dev/null +++ b/test/Altinn.App.Api.Tests/Data/apps/tdd/contributer-restriction/models/default.validation.json @@ -0,0 +1,18 @@ +{ + "$schema": "https://altinncdn.no/toolkits/altinn-app-frontend/4/schemas/json/validation/validation.schema.v1.json", + "validations": { + "melding.name": [ + { + "condition": [ + "equals", + [ + "dataModel", + "melding.name" + ], + "Model" + ], + "message": "The file must be of type Model" + } + ] + } +}