From 31cab20f41f6c98a7cb1829838245e4aae5b6b6c Mon Sep 17 00:00:00 2001 From: andreastanderen Date: Fri, 1 Mar 2024 13:30:30 +0100 Subject: [PATCH] Update tests --- backend/src/DataModeling/Metamodel/ModelMetadata.cs | 2 ++ .../src/Designer/Controllers/AppDevelopmentController.cs | 3 +-- .../GitRepository/AltinnAppGitRepository.cs | 6 +++++- .../Services/Implementation/AppDevelopmentService.cs | 7 +++---- .../Services/Interfaces/IAppDevelopmentService.cs | 4 +++- .../AppDevelopmentController/GetModelMetadataTests.cs | 9 ++++----- 6 files changed, 18 insertions(+), 13 deletions(-) diff --git a/backend/src/DataModeling/Metamodel/ModelMetadata.cs b/backend/src/DataModeling/Metamodel/ModelMetadata.cs index adf6f8ce068..80723098780 100644 --- a/backend/src/DataModeling/Metamodel/ModelMetadata.cs +++ b/backend/src/DataModeling/Metamodel/ModelMetadata.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Text.Json.Serialization; using Newtonsoft.Json; namespace Altinn.Studio.DataModeling.Metamodel @@ -41,6 +42,7 @@ public class ModelMetadata /// Gets or sets all elements for the service () /// [JsonProperty(PropertyName = "elements")] + [JsonPropertyName("elements")] public Dictionary Elements { get; set; } = new Dictionary(); } } diff --git a/backend/src/Designer/Controllers/AppDevelopmentController.cs b/backend/src/Designer/Controllers/AppDevelopmentController.cs index 215ce61bf03..19d660c997f 100644 --- a/backend/src/Designer/Controllers/AppDevelopmentController.cs +++ b/backend/src/Designer/Controllers/AppDevelopmentController.cs @@ -5,7 +5,6 @@ using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; -using Altinn.Studio.DataModeling.Metamodel; using Altinn.Studio.Designer.Configuration; using Altinn.Studio.Designer.Filters; using Altinn.Studio.Designer.Helpers; @@ -275,7 +274,7 @@ public async Task GetLayoutSettings(string org, string app, [From public async Task GetModelMetadata(string org, string app, [FromQuery] string layoutSetName, CancellationToken cancellationToken) { string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext); - ModelMetadata modelMetadata = await _appDevelopmentService.GetModelMetadata(AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer), layoutSetName, cancellationToken); + JsonNode modelMetadata = await _appDevelopmentService.GetModelMetadata(AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer), layoutSetName, cancellationToken); return Ok(modelMetadata); } diff --git a/backend/src/Designer/Infrastructure/GitRepository/AltinnAppGitRepository.cs b/backend/src/Designer/Infrastructure/GitRepository/AltinnAppGitRepository.cs index 7a2545e502b..9f897575b91 100644 --- a/backend/src/Designer/Infrastructure/GitRepository/AltinnAppGitRepository.cs +++ b/backend/src/Designer/Infrastructure/GitRepository/AltinnAppGitRepository.cs @@ -8,6 +8,7 @@ using System.Text.Json.Serialization; using System.Threading; using System.Threading.Tasks; +using Altinn.Studio.DataModeling.Metamodel; using Altinn.Studio.Designer.Configuration; using Altinn.Studio.Designer.Helpers; using Altinn.Studio.Designer.Models; @@ -141,9 +142,12 @@ public async Task GetAppMetadataConfig() public async Task GetModelMetadata(string modelName) { string modelMetadataFileName = GetPathToModelMetadata(modelName); + JsonSerializerOptions metadataOptions = new(_jsonOptions); + metadataOptions.Converters.Add(new JsonStringEnumConverter()); if (!FileExistsByRelativePath(modelMetadataFileName)) { - return "{}"; + ModelMetadata emptyModel = JsonSerializer.Deserialize("{}", metadataOptions); + return JsonSerializer.Serialize(emptyModel); } return await ReadTextByRelativePathAsync(modelMetadataFileName); } diff --git a/backend/src/Designer/Services/Implementation/AppDevelopmentService.cs b/backend/src/Designer/Services/Implementation/AppDevelopmentService.cs index 429b9ce552c..1b179cb8fc5 100644 --- a/backend/src/Designer/Services/Implementation/AppDevelopmentService.cs +++ b/backend/src/Designer/Services/Implementation/AppDevelopmentService.cs @@ -5,14 +5,12 @@ using System.Threading; using System.Threading.Tasks; using Altinn.App.Core.Models; -using Altinn.Studio.DataModeling.Metamodel; using Altinn.Studio.Designer.Helpers; using Altinn.Studio.Designer.Infrastructure.GitRepository; using Altinn.Studio.Designer.Models; using Altinn.Studio.Designer.Services.Interfaces; using JetBrains.Annotations; using Microsoft.AspNetCore.Http; -using Newtonsoft.Json; using NuGet.Versioning; using LayoutSets = Altinn.Studio.Designer.Models.LayoutSets; using PlatformStorageModels = Altinn.Platform.Storage.Interface.Models; @@ -149,7 +147,8 @@ public async Task SaveLayoutSettings(AltinnRepoEditingContext altinnRepoEditingC } /// - public async Task GetModelMetadata(AltinnRepoEditingContext altinnRepoEditingContext, string layoutSetName, CancellationToken cancellationToken = default) + public async Task GetModelMetadata(AltinnRepoEditingContext altinnRepoEditingContext, + string layoutSetName, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); AltinnAppGitRepository altinnAppGitRepository = @@ -161,7 +160,7 @@ public async Task GetModelMetadata(AltinnRepoEditingContext altin string taskId = await GetTaskIdBasedOnLayoutSet(altinnRepoEditingContext, layoutSetName, cancellationToken); string modelName = GetModelName(applicationMetadata, taskId); string fileContent = await altinnAppGitRepository.GetModelMetadata(modelName); - return JsonConvert.DeserializeObject(fileContent); + return JsonNode.Parse(fileContent); } private string GetModelName(ApplicationMetadata applicationMetadata, [CanBeNull] string taskId) diff --git a/backend/src/Designer/Services/Interfaces/IAppDevelopmentService.cs b/backend/src/Designer/Services/Interfaces/IAppDevelopmentService.cs index b2e3c1c19ab..b4681905c0a 100644 --- a/backend/src/Designer/Services/Interfaces/IAppDevelopmentService.cs +++ b/backend/src/Designer/Services/Interfaces/IAppDevelopmentService.cs @@ -75,7 +75,9 @@ public interface IAppDevelopmentService /// Name of current layoutSet in ux-editor that edited layout belongs to /// An that observes if operation is cancelled. /// The service metadata for an app. - public Task GetModelMetadata(AltinnRepoEditingContext altinnRepoEditingContext, [CanBeNull] string layoutSetName, CancellationToken cancellationToken = default); + public Task GetModelMetadata( + AltinnRepoEditingContext altinnRepoEditingContext, [CanBeNull] string layoutSetName, + CancellationToken cancellationToken = default); /// diff --git a/backend/tests/Designer.Tests/Controllers/AppDevelopmentController/GetModelMetadataTests.cs b/backend/tests/Designer.Tests/Controllers/AppDevelopmentController/GetModelMetadataTests.cs index 7bcf3969d99..393e3141ab9 100644 --- a/backend/tests/Designer.Tests/Controllers/AppDevelopmentController/GetModelMetadataTests.cs +++ b/backend/tests/Designer.Tests/Controllers/AppDevelopmentController/GetModelMetadataTests.cs @@ -56,12 +56,11 @@ public async Task GetModelMetadata_Should_Return_Empty_Model_When_No_ModelMetada using var response = await HttpClient.SendAsync(httpRequestMessage); response.StatusCode.Should().Be(HttpStatusCode.OK); - + string responseContent = await response.Content.ReadAsStringAsync(); - string responseContentLowerCase = responseContent.ToLowerInvariant(); - string expectedResposeContentLowerCase = JsonConvert - .SerializeObject(JsonConvert.DeserializeObject("{}")).ToLowerInvariant(); - responseContentLowerCase.Should().Be(expectedResposeContentLowerCase); + string expectedResponse = JsonConvert + .SerializeObject(JsonConvert.DeserializeObject("{}")); + responseContent.Should().Be(expectedResponse); } private async Task AddModelMetadataToRepo(string createdFolderPath, string expectedModelMetadataPath)