Skip to content

Commit

Permalink
Add test case for layoutSetDeletion with referecing component
Browse files Browse the repository at this point in the history
  • Loading branch information
Jondyr committed Oct 2, 2024
1 parent 7bdb17d commit c2e7124
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();

Check warning

Code scanning / CodeQL

Dereferenced variable may be null Warning test

Variable
layout
may be null at this access because of
this
assignment.
layout
.Where(jsonNode => jsonNode["layoutSet"] != null)
.Should()
.NotContain(jsonNode => jsonNode["layoutSet"].GetValue<string>() == deletedComponentId,
$"No components should reference the deleted layout set {deletedComponentId}");
}

private async Task<LayoutSets> GetLayoutSetsFile(string org, string app, string developer)
{
AltinnGitRepositoryFactory altinnGitRepositoryFactory =
Expand All @@ -143,6 +170,16 @@ private async Task<LayoutSets> GetLayoutSetsFile(string org, string app, string
return await altinnAppGitRepository.GetLayoutSetsFile();
}

private async Task<Dictionary<string, JsonNode>> GetFormLayouts(string org, string app, string developer, string layoutSetName)
{
AltinnGitRepositoryFactory altinnGitRepositoryFactory =
new(TestDataHelper.GetTestDataRepositoriesRootDirectory());
AltinnAppGitRepository altinnAppGitRepository =
altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, app, developer);
Dictionary<string, JsonNode> formLayouts = await altinnAppGitRepository.GetFormLayouts(layoutSetName);
return formLayouts;
}

private async Task<Application> GetApplicationMetadataFile(string org, string app, string developer)
{
AltinnGitRepositoryFactory altinnGitRepositoryFactory =
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}

0 comments on commit c2e7124

Please sign in to comment.