From c2e7124ba3aa3d2317ea123f3c9dda8370656aa2 Mon Sep 17 00:00:00 2001 From: Jonas Dyrlie Date: Wed, 2 Oct 2024 14:39:05 +0200 Subject: [PATCH] Add test case for layoutSetDeletion with referecing component --- .../DeleteLayoutSetTests.cs | 41 ++++++++++++++++++- .../layoutSet2/layouts/layoutFile1InSet2.json | 28 ++++++++----- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/backend/tests/Designer.Tests/Controllers/AppDevelopmentController/DeleteLayoutSetTests.cs b/backend/tests/Designer.Tests/Controllers/AppDevelopmentController/DeleteLayoutSetTests.cs index cb15df89f79..8acf823a8a2 100644 --- a/backend/tests/Designer.Tests/Controllers/AppDevelopmentController/DeleteLayoutSetTests.cs +++ b/backend/tests/Designer.Tests/Controllers/AppDevelopmentController/DeleteLayoutSetTests.cs @@ -1,7 +1,8 @@ -using System.IO; +using System.Collections.Generic; +using System.Linq; using System.Net; using System.Net.Http; -using System.Text.Json; +using System.Text.Json.Nodes; using System.Threading.Tasks; using Altinn.Platform.Storage.Interface.Models; using Altinn.Studio.Designer.Factories; @@ -133,6 +134,32 @@ public async Task DeleteLayoutSet_AppWithoutLayoutSets_ReturnsNotFound(string or response.StatusCode.Should().Be(HttpStatusCode.NotFound); } + [Theory] + [InlineData("ttd", "app-with-layoutsets", "testUser", "layoutSet3", + "layoutSet2", "layoutFile1InSet2", "subform-component-id")] + public async Task DeleteLayoutSet_RemovesComponentsReferencingLayoutSet(string org, string app, string developer, string layoutSetToDeleteId, + string layoutSetWithRef, string layoutSetFile, string deletedComponentId) + { + string targetRepository = TestDataHelper.GenerateTestRepoName(); + await CopyRepositoryForTest(org, app, developer, targetRepository); + + string url = $"{VersionPrefix(org, targetRepository)}/layout-set/{layoutSetToDeleteId}"; + using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Delete, url); + + using var response = await HttpClient.SendAsync(httpRequestMessage); + response.StatusCode.Should().Be(HttpStatusCode.OK, await response.Content.ReadAsStringAsync()); + + JsonNode formLayout = (await GetFormLayouts(org, targetRepository, developer, layoutSetWithRef))[layoutSetFile]; + JsonArray layout = formLayout["data"]?["layout"] as JsonArray; + + layout.Should().NotBeNull(); + layout + .Where(jsonNode => jsonNode["layoutSet"] != null) + .Should() + .NotContain(jsonNode => jsonNode["layoutSet"].GetValue() == deletedComponentId, + $"No components should reference the deleted layout set {deletedComponentId}"); + } + private async Task GetLayoutSetsFile(string org, string app, string developer) { AltinnGitRepositoryFactory altinnGitRepositoryFactory = @@ -143,6 +170,16 @@ private async Task GetLayoutSetsFile(string org, string app, string return await altinnAppGitRepository.GetLayoutSetsFile(); } + private async Task> GetFormLayouts(string org, string app, string developer, string layoutSetName) + { + AltinnGitRepositoryFactory altinnGitRepositoryFactory = + new(TestDataHelper.GetTestDataRepositoriesRootDirectory()); + AltinnAppGitRepository altinnAppGitRepository = + altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, app, developer); + Dictionary formLayouts = await altinnAppGitRepository.GetFormLayouts(layoutSetName); + return formLayouts; + } + private async Task GetApplicationMetadataFile(string org, string app, string developer) { AltinnGitRepositoryFactory altinnGitRepositoryFactory = diff --git a/backend/tests/Designer.Tests/_TestData/Repositories/testUser/ttd/app-with-layoutsets/App/ui/layoutSet2/layouts/layoutFile1InSet2.json b/backend/tests/Designer.Tests/_TestData/Repositories/testUser/ttd/app-with-layoutsets/App/ui/layoutSet2/layouts/layoutFile1InSet2.json index 2e97608b86d..32c4c651167 100644 --- a/backend/tests/Designer.Tests/_TestData/Repositories/testUser/ttd/app-with-layoutsets/App/ui/layoutSet2/layouts/layoutFile1InSet2.json +++ b/backend/tests/Designer.Tests/_TestData/Repositories/testUser/ttd/app-with-layoutsets/App/ui/layoutSet2/layouts/layoutFile1InSet2.json @@ -1,13 +1,19 @@ { - "schema": "https://altinncdn.no/schemas/json/layout/layout.schema.v1.json", - "data": { - "layout": [{ - "id": "component-id", - "type": "Header", - "textResourceBindings": { - "title": "some-old-id", - "body": "another-key" - } - }] - } + "schema": "https://altinncdn.no/schemas/json/layout/layout.schema.v1.json", + "data": { + "layout": [ + { + "id": "component-id", + "type": "Header", + "textResourceBindings": { + "title": "some-old-id", + "body": "another-key" + } + }, + { + "id": "subform-component-id", + "layoutSet": "layoutSet3" + } + ] + } }