From 6018bc85e7f7f02a4d33035912c96a37dbb6eafa Mon Sep 17 00:00:00 2001 From: andreastanderen Date: Wed, 10 Apr 2024 15:58:07 +0200 Subject: [PATCH] Delete modelMetadata on model updates --- .../GitRepository/AltinnAppGitRepository.cs | 27 ++------- .../Implementation/SchemaModelService.cs | 11 ++-- .../Services/SchemaModelServiceTests.cs | 59 +++++++++++++++++++ 3 files changed, 71 insertions(+), 26 deletions(-) diff --git a/backend/src/Designer/Infrastructure/GitRepository/AltinnAppGitRepository.cs b/backend/src/Designer/Infrastructure/GitRepository/AltinnAppGitRepository.cs index 3cd3012f8f2..9a8c781a17a 100644 --- a/backend/src/Designer/Infrastructure/GitRepository/AltinnAppGitRepository.cs +++ b/backend/src/Designer/Infrastructure/GitRepository/AltinnAppGitRepository.cs @@ -8,7 +8,6 @@ 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.Exceptions.AppDevelopment; using Altinn.Studio.Designer.Helpers; @@ -142,30 +141,16 @@ public async Task GetAppMetadataConfig() } /// - /// Gets the model metadata content based on model name. If no model metadata found for the model name an empty model metadata is returned. + /// Deletes model metadata file as it is generated on the fly and does not need to be in the repo. /// - /// The model metadata as string - public async Task GetModelMetadata(string modelName) + /// The full relative path to the model metadata + public void DeleteModelMetadata(string modelMetadataFilePath) { - string modelMetadataFileName = GetPathToModelMetadata(modelName); - if (!FileExistsByRelativePath(modelMetadataFileName)) + if (FileExistsByRelativePath(modelMetadataFilePath)) { - ModelMetadata emptyModel = JsonSerializer.Deserialize("{}"); - return JsonSerializer.Serialize(emptyModel); + string absolutePath = GetAbsoluteFileOrDirectoryPathSanitized(modelMetadataFilePath); + File.Delete(absolutePath); } - return await ReadTextByRelativePathAsync(modelMetadataFileName); - } - - /// - /// Saves the model metadata model for the application (a JSON where the model hierarchy is flatten, - /// in order to easier generate the C# class) to disk. - /// - /// Model metadata to persist. - /// The name of the model. - public async Task SaveModelMetadata(string modelMetadata, string modelName) - { - string modelMetadataRelativeFilePath = GetPathToModelMetadata(modelName); - await WriteTextByRelativePathAsync(modelMetadataRelativeFilePath, modelMetadata, true); } /// diff --git a/backend/src/Designer/Services/Implementation/SchemaModelService.cs b/backend/src/Designer/Services/Implementation/SchemaModelService.cs index 6342907301a..bed9abd511e 100644 --- a/backend/src/Designer/Services/Implementation/SchemaModelService.cs +++ b/backend/src/Designer/Services/Implementation/SchemaModelService.cs @@ -96,24 +96,25 @@ public async Task GetSchema(AltinnRepoEditingContext altinnRepoEditingCo public async Task UpdateSchema(AltinnRepoEditingContext altinnRepoEditingContext, string relativeFilePath, string jsonContent, bool saveOnly = false, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); - var altinnGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository(altinnRepoEditingContext.Org, altinnRepoEditingContext.Repo, altinnRepoEditingContext.Developer); + var altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository(altinnRepoEditingContext.Org, altinnRepoEditingContext.Repo, altinnRepoEditingContext.Developer); var jsonSchema = Json.Schema.JsonSchema.FromText(jsonContent); var serializedJsonContent = SerializeJson(jsonSchema); + altinnAppGitRepository.DeleteModelMetadata(relativeFilePath.Replace(".schema.json", ".metadata.json")); if (saveOnly) { // Only save updated JSON schema - no model file generation - await altinnGitRepository.WriteTextByRelativePathAsync(relativeFilePath, serializedJsonContent, true, cancellationToken); + await altinnAppGitRepository.WriteTextByRelativePathAsync(relativeFilePath, serializedJsonContent, true, cancellationToken); return; } - var repositoryType = await altinnGitRepository.GetRepositoryType(); + var repositoryType = await altinnAppGitRepository.GetRepositoryType(); if (repositoryType == AltinnRepositoryType.Datamodels) { // Datamodels repository - save JSON and update XSD - await altinnGitRepository.WriteTextByRelativePathAsync(relativeFilePath, serializedJsonContent, true, cancellationToken); + await altinnAppGitRepository.WriteTextByRelativePathAsync(relativeFilePath, serializedJsonContent, true, cancellationToken); XmlSchema xsd = _jsonSchemaToXmlSchemaConverter.Convert(jsonSchema); - await altinnGitRepository.SaveXsd(xsd, relativeFilePath.Replace(".schema.json", ".xsd")); + await altinnAppGitRepository.SaveXsd(xsd, relativeFilePath.Replace(".schema.json", ".xsd")); return; } diff --git a/backend/tests/Designer.Tests/Services/SchemaModelServiceTests.cs b/backend/tests/Designer.Tests/Services/SchemaModelServiceTests.cs index f36f0de1ae6..954e224a2cd 100644 --- a/backend/tests/Designer.Tests/Services/SchemaModelServiceTests.cs +++ b/backend/tests/Designer.Tests/Services/SchemaModelServiceTests.cs @@ -143,6 +143,65 @@ public async Task UpdateSchema_AppRepo_ShouldUpdate() } } + [Fact] + public async Task UpdateSchema_ModelMetadataExistForModelInRepo_ShouldDeleteModelMetadata() + { + // Arrange + var org = "ttd"; + var sourceRepository = "hvem-er-hvem"; + var developer = "testUser"; + var targetRepository = TestDataHelper.GenerateTestRepoName(); + var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, targetRepository, developer); + + await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository); + try + { + var altinnGitRepositoryFactory = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory()); + ISchemaModelService schemaModelService = new SchemaModelService(altinnGitRepositoryFactory, TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings, TestDataHelper.XmlSchemaToJsonSchemaConverter, TestDataHelper.JsonSchemaToXmlSchemaConverter, TestDataHelper.ModelMetadataToCsharpConverter); + var updatedSchema = @"{""properties"":{""rootType1"":{""$ref"":""#/definitions/rootType""}},""definitions"":{""rootType"":{""properties"":{""keyword"":{""type"":""string""}}}}}"; + + var altinnGitRepository = altinnGitRepositoryFactory.GetAltinnGitRepository(org, targetRepository, developer); + Assert.True(altinnGitRepository.FileExistsByRelativePath("App/models/Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.metadata.json")); + await schemaModelService.UpdateSchema(editingContext, "App/models/Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.schema.json", updatedSchema); + Assert.False(altinnGitRepository.FileExistsByRelativePath("App/models/Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.metadata.json")); + } + finally + { + TestDataHelper.DeleteAppRepository(org, targetRepository, developer); + } + } + + [Fact] + public async Task UpdateSchema_NoModelMetadataForModelInRepo_ShouldOnlyUpdate() + { + // Arrange + var org = "ttd"; + var sourceRepository = "hvem-er-hvem"; + var developer = "testUser"; + var targetRepository = TestDataHelper.GenerateTestRepoName(); + var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, targetRepository, developer); + + await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository); + try + { + var altinnGitRepositoryFactory = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory()); + ISchemaModelService schemaModelService = new SchemaModelService(altinnGitRepositoryFactory, TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings, TestDataHelper.XmlSchemaToJsonSchemaConverter, TestDataHelper.JsonSchemaToXmlSchemaConverter, TestDataHelper.ModelMetadataToCsharpConverter); + var expectedUpdatedSchema = @"{""properties"":{""rootType1"":{""$ref"":""#/definitions/rootType""}},""definitions"":{""rootType"":{""properties"":{""keyword"":{""type"":""string""}}}}}"; + var altinnGitRepository = altinnGitRepositoryFactory.GetAltinnGitRepository(org, targetRepository, developer); + Assert.False(altinnGitRepository.FileExistsByRelativePath("App/models/HvemErHvem_SERES.metadata.json")); + + await schemaModelService.UpdateSchema(editingContext, "App/models/HvemErHvem_SERES.schema.json", expectedUpdatedSchema); + Assert.False(altinnGitRepository.FileExistsByRelativePath("App/models/HvemErHvem_SERES.metadata.json")); + var updatedSchema = await altinnGitRepository.ReadTextByRelativePathAsync("App/models/HvemErHvem_SERES.schema.json"); + string serializedExpectedSchemaUpdates = FormatJsonString(updatedSchema); + updatedSchema.Should().BeEquivalentTo(serializedExpectedSchemaUpdates); + } + finally + { + TestDataHelper.DeleteAppRepository(org, targetRepository, developer); + } + } + [Fact] public async Task UpdateSchema_InvalidJsonSchema_ShouldThrowException() {