From 0b602cf067b2fe46ce225e5290827b89b7248d80 Mon Sep 17 00:00:00 2001 From: Jonas Dyrlie Date: Thu, 12 Dec 2024 13:38:39 +0100 Subject: [PATCH 01/10] Add GET and PUT endpoints for datamodel metadata --- .../Controllers/DatamodelsController.cs | 22 +++++++++++++++++++ .../Implementation/SchemaModelService.cs | 21 +++++++++++++++++- .../Interfaces/ISchemaModelService.cs | 11 ++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/backend/src/Designer/Controllers/DatamodelsController.cs b/backend/src/Designer/Controllers/DatamodelsController.cs index f7c57a88525..b81dd7f311d 100644 --- a/backend/src/Designer/Controllers/DatamodelsController.cs +++ b/backend/src/Designer/Controllers/DatamodelsController.cs @@ -6,6 +6,7 @@ using System.Text.Json.Nodes; using System.Threading; using System.Threading.Tasks; +using Altinn.Platform.Storage.Interface.Models; using Altinn.Studio.DataModeling.Validator.Json; using Altinn.Studio.Designer.Filters; using Altinn.Studio.Designer.Helpers; @@ -248,6 +249,27 @@ public async Task UseXsdFromRepo(string org, string repository, s } } + /// + /// Gets the model metadata for a given model. + /// + [HttpGet("datamodel/{modelName}/metadata")] + [UseSystemTextJson] + public async Task GetDataTypeConfiguration(string org, string repository, string modelName) + { + var dataTypeConfiguration = await _schemaModelService.GetModelMetadata(org, repository, modelName); + return Ok(dataTypeConfiguration); + } + + /// + /// Updates the model metadata for a given model. + /// + [HttpPut("datamodel/{modelName}/metadata")] + [UseSystemTextJson] + public async Task PostDataTypeConfiguration(string org, string repository, string modelName, [FromBody] DataType dataType) + { + await _schemaModelService.UpdateModelMetadata(org, repository, modelName, dataType); + } + private static string GetFileNameFromUploadedFile(IFormFile thefile) { return ContentDispositionHeaderValue.Parse(new StringSegment(thefile.ContentDisposition)).FileName.ToString(); diff --git a/backend/src/Designer/Services/Implementation/SchemaModelService.cs b/backend/src/Designer/Services/Implementation/SchemaModelService.cs index 3067a31d98d..9c5cf7da8c7 100644 --- a/backend/src/Designer/Services/Implementation/SchemaModelService.cs +++ b/backend/src/Designer/Services/Implementation/SchemaModelService.cs @@ -40,6 +40,7 @@ public class SchemaModelService : ISchemaModelService private readonly IXmlSchemaToJsonSchemaConverter _xmlSchemaToJsonSchemaConverter; private readonly IJsonSchemaToXmlSchemaConverter _jsonSchemaToXmlSchemaConverter; private readonly IModelMetadataToCsharpConverter _modelMetadataToCsharpConverter; + private readonly IApplicationMetadataService _applicationMetadataService; /// /// Initializes a new instance of the class. @@ -59,13 +60,15 @@ public class SchemaModelService : ISchemaModelService /// /// Class for converting Json schemas to Xml schemas. /// C# model generator + /// public SchemaModelService( IAltinnGitRepositoryFactory altinnGitRepositoryFactory, ILoggerFactory loggerFactory, ServiceRepositorySettings serviceRepositorySettings, IXmlSchemaToJsonSchemaConverter xmlSchemaToJsonSchemaConverter, IJsonSchemaToXmlSchemaConverter jsonSchemaToXmlSchemaConverter, - IModelMetadataToCsharpConverter modelMetadataToCsharpConverter) + IModelMetadataToCsharpConverter modelMetadataToCsharpConverter, + IApplicationMetadataService applicationMetadataService) { _altinnGitRepositoryFactory = altinnGitRepositoryFactory; _loggerFactory = loggerFactory; @@ -73,6 +76,7 @@ public SchemaModelService( _xmlSchemaToJsonSchemaConverter = xmlSchemaToJsonSchemaConverter; _jsonSchemaToXmlSchemaConverter = jsonSchemaToXmlSchemaConverter; _modelMetadataToCsharpConverter = modelMetadataToCsharpConverter; + _applicationMetadataService = applicationMetadataService; } /// @@ -431,5 +435,20 @@ private bool NamespaceNeedsToBeSeparated(ApplicationMetadata application, { return application.DataTypes.All(d => d.AppLogic?.ClassRef != $"Altinn.App.Models.{csharpModelName}"); } + + public async Task GetModelMetadata(string org, string app, string modelName) + { + ApplicationMetadata applicationMetadata = await _applicationMetadataService.GetApplicationMetadataFromRepository(org, app); + DataType dataType = applicationMetadata.DataTypes.Find((dataType) => dataType.Id == modelName); + return dataType; + } + + public async Task UpdateModelMetadata(string org, string app, string modelName, DataType dataType) + { + ApplicationMetadata applicationMetadata = await _applicationMetadataService.GetApplicationMetadataFromRepository(org, app); + applicationMetadata.DataTypes.RemoveAll((dataType) => dataType.Id == modelName); + applicationMetadata.DataTypes.Add(dataType); + await _applicationMetadataService.UpdateApplicationMetaDataLocally(org, app, applicationMetadata); + } } } diff --git a/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs b/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs index c043f7fa8cd..9f7a0ebd238 100644 --- a/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs +++ b/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Altinn.Platform.Storage.Interface.Models; using Altinn.Studio.DataModeling.Metamodel; +using Altinn.Studio.Designer.Infrastructure.GitRepository; using Altinn.Studio.Designer.Models; namespace Altinn.Studio.Designer.Services.Interfaces @@ -84,5 +85,15 @@ public interface ISchemaModelService /// An that observes if operation is cancelled. /// Returns the model metadata Task GenerateModelMetadataFromJsonSchema(AltinnRepoEditingContext altinnRepoEditingContext, string relativeFilePath, CancellationToken cancellationToken = default); + + /// + /// Gets the model metadata for a given model. + /// + Task GetModelMetadata(string org, string app, string modelName); + + /// + /// Updates the model metadata for a given model. + /// + Task UpdateModelMetadata(string org, string app, string modelName, DataType dataType); } } From a8fadc62bb595064fca7c8d10c2039d75eb8b48d Mon Sep 17 00:00:00 2001 From: Jonas Dyrlie Date: Thu, 12 Dec 2024 13:40:24 +0100 Subject: [PATCH 02/10] Change naming of controller endpoints --- backend/src/Designer/Controllers/DatamodelsController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/Designer/Controllers/DatamodelsController.cs b/backend/src/Designer/Controllers/DatamodelsController.cs index b81dd7f311d..21f1fe8e6a7 100644 --- a/backend/src/Designer/Controllers/DatamodelsController.cs +++ b/backend/src/Designer/Controllers/DatamodelsController.cs @@ -254,7 +254,7 @@ public async Task UseXsdFromRepo(string org, string repository, s /// [HttpGet("datamodel/{modelName}/metadata")] [UseSystemTextJson] - public async Task GetDataTypeConfiguration(string org, string repository, string modelName) + public async Task GetModelMetadata(string org, string repository, string modelName) { var dataTypeConfiguration = await _schemaModelService.GetModelMetadata(org, repository, modelName); return Ok(dataTypeConfiguration); @@ -265,7 +265,7 @@ public async Task GetDataTypeConfiguration(string org, string rep /// [HttpPut("datamodel/{modelName}/metadata")] [UseSystemTextJson] - public async Task PostDataTypeConfiguration(string org, string repository, string modelName, [FromBody] DataType dataType) + public async Task UpdateModelMetadata(string org, string repository, string modelName, [FromBody] DataType dataType) { await _schemaModelService.UpdateModelMetadata(org, repository, modelName, dataType); } From cabcf5dcd2e7b9eefd51393c3af395700080196d Mon Sep 17 00:00:00 2001 From: Jonas Dyrlie Date: Thu, 12 Dec 2024 14:20:53 +0100 Subject: [PATCH 03/10] Refactor schemaModelServiceTests --- .../Services/SchemaModelServiceTests.cs | 96 +++++++------------ 1 file changed, 35 insertions(+), 61 deletions(-) diff --git a/backend/tests/Designer.Tests/Services/SchemaModelServiceTests.cs b/backend/tests/Designer.Tests/Services/SchemaModelServiceTests.cs index a4febf21116..344f341d896 100644 --- a/backend/tests/Designer.Tests/Services/SchemaModelServiceTests.cs +++ b/backend/tests/Designer.Tests/Services/SchemaModelServiceTests.cs @@ -15,6 +15,7 @@ using Altinn.Studio.Designer.Services.Interfaces; using Designer.Tests.Utils; using FluentAssertions; +using Moq; using SharedResources.Tests; using Xunit; @@ -22,6 +23,17 @@ namespace Designer.Tests.Services { public class SchemaModelServiceTests { + private readonly Mock _applicationMetadataService; + private readonly AltinnGitRepositoryFactory _altinnGitRepositoryFactory; + private readonly ISchemaModelService _schemaModelService; + + public SchemaModelServiceTests() + { + _applicationMetadataService = new Mock(); + _altinnGitRepositoryFactory = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory()); + _schemaModelService = new SchemaModelService(_altinnGitRepositoryFactory, TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings, TestDataHelper.XmlSchemaToJsonSchemaConverter, TestDataHelper.JsonSchemaToXmlSchemaConverter, TestDataHelper.ModelMetadataToCsharpConverter, _applicationMetadataService.Object); + } + [Fact] public async Task DeleteSchema_AppRepo_ShouldDelete() { @@ -35,22 +47,19 @@ public async Task DeleteSchema_AppRepo_ShouldDelete() 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 schemaFiles = schemaModelService.GetSchemaFiles(editingContext); + var schemaFiles = _schemaModelService.GetSchemaFiles(editingContext); schemaFiles.Should().HaveCount(7); - var altinnAppGitRepository = altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, targetRepository, developer); + var altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, targetRepository, developer); var applicationMetadata = await altinnAppGitRepository.GetApplicationMetadata(); applicationMetadata.DataTypes.Should().HaveCount(2); // Act var schemaToDelete = schemaFiles.First(s => s.FileName == "Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.schema.json"); - await schemaModelService.DeleteSchema(editingContext, schemaToDelete.RepositoryRelativeUrl); + await _schemaModelService.DeleteSchema(editingContext, schemaToDelete.RepositoryRelativeUrl); // Assert - schemaFiles = schemaModelService.GetSchemaFiles(editingContext); + schemaFiles = _schemaModelService.GetSchemaFiles(editingContext); schemaFiles.Should().HaveCount(6); applicationMetadata = await altinnAppGitRepository.GetApplicationMetadata(); applicationMetadata.DataTypes.Should().HaveCount(1); @@ -75,22 +84,20 @@ public async Task DeleteSchema_AppRepoWithLayoutSets_ShouldDelete() try { string dataModelName = "datamodel"; - var altinnGitRepositoryFactory = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory()); - ISchemaModelService schemaModelService = new SchemaModelService(altinnGitRepositoryFactory, TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings, TestDataHelper.XmlSchemaToJsonSchemaConverter, TestDataHelper.JsonSchemaToXmlSchemaConverter, TestDataHelper.ModelMetadataToCsharpConverter); - var schemaFiles = schemaModelService.GetSchemaFiles(editingContext); + var schemaFiles = _schemaModelService.GetSchemaFiles(editingContext); schemaFiles.Should().HaveCount(1); - var altinnAppGitRepository = altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, targetRepository, developer); + var altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, targetRepository, developer); var applicationMetadataBefore = await altinnAppGitRepository.GetApplicationMetadata(); var layoutSetsBefore = await altinnAppGitRepository.GetLayoutSetsFile(); // Act var schemaToDelete = schemaFiles.First(s => s.FileName == $"{dataModelName}.schema.json"); - await schemaModelService.DeleteSchema(editingContext, schemaToDelete.RepositoryRelativeUrl); + await _schemaModelService.DeleteSchema(editingContext, schemaToDelete.RepositoryRelativeUrl); // Assert - schemaFiles = schemaModelService.GetSchemaFiles(editingContext); + schemaFiles = _schemaModelService.GetSchemaFiles(editingContext); schemaFiles.Should().HaveCount(0); var applicationMetadataAfter = await altinnAppGitRepository.GetApplicationMetadata(); applicationMetadataAfter.DataTypes.Should().HaveCount(applicationMetadataBefore.DataTypes.Count - 1); @@ -117,18 +124,16 @@ public async Task DeleteSchema_ModelsRepo_ShouldDelete() 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 schemaFiles = schemaModelService.GetSchemaFiles(editingContext); + var schemaFiles = _schemaModelService.GetSchemaFiles(editingContext); schemaFiles.Should().HaveCount(6); // Act var schemaToDelete = schemaFiles.First(s => s.FileName == "Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.schema.json"); - await schemaModelService.DeleteSchema(editingContext, schemaToDelete.RepositoryRelativeUrl); + await _schemaModelService.DeleteSchema(editingContext, schemaToDelete.RepositoryRelativeUrl); // Assert - schemaFiles = schemaModelService.GetSchemaFiles(editingContext); + schemaFiles = _schemaModelService.GetSchemaFiles(editingContext); schemaFiles.Should().HaveCount(5); } finally @@ -150,15 +155,13 @@ public async Task UpdateSchema_AppRepo_ShouldUpdate() await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository); try { - var altinnGitRepositoryFactory = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory()); // Act - ISchemaModelService schemaModelService = new SchemaModelService(altinnGitRepositoryFactory, TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings, TestDataHelper.XmlSchemaToJsonSchemaConverter, TestDataHelper.JsonSchemaToXmlSchemaConverter, TestDataHelper.ModelMetadataToCsharpConverter); var expectedSchemaUpdates = @"{""properties"":{""rootType1"":{""$ref"":""#/definitions/rootType""}},""definitions"":{""rootType"":{""properties"":{""keyword"":{""type"":""string""}}}}}"; - await schemaModelService.UpdateSchema(editingContext, "App/models/HvemErHvem_SERES.schema.json", expectedSchemaUpdates); + await _schemaModelService.UpdateSchema(editingContext, "App/models/HvemErHvem_SERES.schema.json", expectedSchemaUpdates); // Assert - var altinnGitRepository = altinnGitRepositoryFactory.GetAltinnGitRepository(org, targetRepository, developer); + var altinnGitRepository = _altinnGitRepositoryFactory.GetAltinnGitRepository(org, targetRepository, developer); var updatedSchema = await altinnGitRepository.ReadTextByRelativePathAsync("App/models/HvemErHvem_SERES.schema.json"); string serializedExpectedSchemaUpdates = FormatJsonString(updatedSchema); @@ -199,13 +202,11 @@ public async Task UpdateSchema_ModelMetadataExistForModelInRepo_ShouldDeleteMode 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); + 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); + 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 @@ -227,13 +228,11 @@ public async Task UpdateSchema_NoModelMetadataForModelInRepo_ShouldOnlyUpdate() 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); + 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); + 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); @@ -257,37 +256,18 @@ public async Task UpdateSchema_InvalidJsonSchema_ShouldThrowException() await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository); - var altinnGitRepositoryFactory = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory()); - - ISchemaModelService schemaModelService = new SchemaModelService(altinnGitRepositoryFactory, - TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings, - TestDataHelper.XmlSchemaToJsonSchemaConverter, TestDataHelper.JsonSchemaToXmlSchemaConverter, - TestDataHelper.ModelMetadataToCsharpConverter); var invalidSchema = @"{""properties"":{""root"":{""$ref"":""#/definitions/rootType""}},""definitions"":{""rootType"":{""properties"":{""keyword"":{""type"":""string""}}}}}"; var exception = await Assert.ThrowsAsync(async () => { - await schemaModelService.UpdateSchema(editingContext, "App/models/HvemErHvem_SERES.schema.json", invalidSchema); + await _schemaModelService.UpdateSchema(editingContext, "App/models/HvemErHvem_SERES.schema.json", invalidSchema); }); Assert.NotNull(exception.CustomErrorMessages); exception.CustomErrorMessages.Should().ContainSingle(c => c.Contains("root': member names cannot be the same as their enclosing type")); } - [Theory] - [InlineData("ttd", "apprepo", "test", "", "http://studio.localhost/repos")] - [InlineData("ttd", "apprepo", "test", "/path/to/folder/", "http://studio.localhost/repos")] - public void GetSchemaUri_ValidNameProvided_ShouldReturnUri(string org, string repository, string schemaName, string relativePath, string repositoryBaseUrl) - { - var altinnGitRepositoryFactory = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory()); - var schemaModelService = new SchemaModelService(altinnGitRepositoryFactory, TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings, TestDataHelper.XmlSchemaToJsonSchemaConverter, TestDataHelper.JsonSchemaToXmlSchemaConverter, TestDataHelper.ModelMetadataToCsharpConverter); - - var schemaUri = schemaModelService.GetSchemaUri(org, repository, schemaName, relativePath); - - schemaUri.AbsoluteUri.Should().Be($"{repositoryBaseUrl}/{org}/{repository}{(string.IsNullOrEmpty(relativePath) ? "/" : relativePath)}{schemaName}.schema.json"); - } - [Fact] public async Task UploadSchemaFromXsd_InvalidXsd_ThrowsException() { @@ -301,13 +281,11 @@ public async Task UploadSchemaFromXsd_InvalidXsd_ThrowsException() 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 xsdStream = SharedResourcesHelper.LoadTestData("Model/XmlSchema/General/SimpleInvalidNonSeresSchema.xsd"); var schemaName = "SimpleInvalidNonSeresSchema"; var fileName = $"{schemaName}.xsd"; - Func action = () => schemaModelService.BuildSchemaFromXsd(editingContext, fileName, xsdStream); + Func action = () => _schemaModelService.BuildSchemaFromXsd(editingContext, fileName, xsdStream); // Act/assert await action.Should().ThrowAsync(); @@ -333,8 +311,6 @@ public async Task UploadSchemaFromXsd_ValidNonSeresXsd_ModelsCreated() 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 xsdStream = SharedResourcesHelper.LoadTestData("Model/XmlSchema/General/SimpleValidNonSeresSchema.xsd"); var schemaName = "SimpleValidNonSeresSchema"; var fileName = $"{schemaName}.xsd"; @@ -342,10 +318,10 @@ public async Task UploadSchemaFromXsd_ValidNonSeresXsd_ModelsCreated() var relativeFilePath = $"{relativeDirectory}/{fileName}"; // Act - await schemaModelService.BuildSchemaFromXsd(editingContext, fileName, xsdStream); + await _schemaModelService.BuildSchemaFromXsd(editingContext, fileName, xsdStream); // Assert - var altinnAppGitRepository = altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, targetRepository, developer); + var altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, targetRepository, developer); altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.metadata.json").Should().BeFalse(); altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.schema.json").Should().BeTrue(); altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.cs").Should().BeTrue(); @@ -371,8 +347,6 @@ public async Task UploadSchemaFromXsd_OED_ModelsCreated() 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 xsdStream = SharedResourcesHelper.LoadTestData("Model/XmlSchema/Gitea/OED.xsd"); var schemaName = "OED_M"; var fileName = $"{schemaName}.xsd"; @@ -380,10 +354,10 @@ public async Task UploadSchemaFromXsd_OED_ModelsCreated() var relativeFilePath = $"{relativeDirectory}/{fileName}"; // Act - await schemaModelService.BuildSchemaFromXsd(editingContext, fileName, xsdStream); + await _schemaModelService.BuildSchemaFromXsd(editingContext, fileName, xsdStream); // Assert - var altinnAppGitRepository = altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, targetRepository, developer); + var altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, targetRepository, developer); altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.metadata.json").Should().BeFalse(); altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.schema.json").Should().BeTrue(); altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.xsd").Should().BeTrue(); From 991015f4a80b0c96a1227be3ff1fc217f22861cf Mon Sep 17 00:00:00 2001 From: Jonas Dyrlie Date: Thu, 12 Dec 2024 14:40:29 +0100 Subject: [PATCH 04/10] Remove unnecessary import --- backend/src/Designer/Services/Interfaces/ISchemaModelService.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs b/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs index 9f7a0ebd238..358df6d24a9 100644 --- a/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs +++ b/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using Altinn.Platform.Storage.Interface.Models; using Altinn.Studio.DataModeling.Metamodel; -using Altinn.Studio.Designer.Infrastructure.GitRepository; using Altinn.Studio.Designer.Models; namespace Altinn.Studio.Designer.Services.Interfaces From d67154f4ac1270a3ced8e006ce30f3c95be25a5b Mon Sep 17 00:00:00 2001 From: Jonas Dyrlie Date: Wed, 18 Dec 2024 10:24:13 +0100 Subject: [PATCH 05/10] Rework naming, remove usage of `metadata` in favor of `dataType` to be more explicit --- .../Designer/Controllers/DatamodelsController.cs | 16 ++++++++-------- .../Implementation/SchemaModelService.cs | 8 ++++---- .../Services/Interfaces/ISchemaModelService.cs | 8 ++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/backend/src/Designer/Controllers/DatamodelsController.cs b/backend/src/Designer/Controllers/DatamodelsController.cs index 21f1fe8e6a7..00d25445b8b 100644 --- a/backend/src/Designer/Controllers/DatamodelsController.cs +++ b/backend/src/Designer/Controllers/DatamodelsController.cs @@ -250,24 +250,24 @@ public async Task UseXsdFromRepo(string org, string repository, s } /// - /// Gets the model metadata for a given model. + /// Gets the dataType for a given datamodel. /// - [HttpGet("datamodel/{modelName}/metadata")] + [HttpGet("datamodel/{modelName}/dataType")] [UseSystemTextJson] - public async Task GetModelMetadata(string org, string repository, string modelName) + public async Task GetModelDataType(string org, string repository, string modelName) { - var dataTypeConfiguration = await _schemaModelService.GetModelMetadata(org, repository, modelName); + DataType dataTypeConfiguration = await _schemaModelService.GetModelDataType(org, repository, modelName); return Ok(dataTypeConfiguration); } /// - /// Updates the model metadata for a given model. + /// Updates the dataType for a given model. /// - [HttpPut("datamodel/{modelName}/metadata")] + [HttpPut("datamodel/{modelName}/dataType")] [UseSystemTextJson] - public async Task UpdateModelMetadata(string org, string repository, string modelName, [FromBody] DataType dataType) + public async Task SetModelDataType(string org, string repository, string modelName, [FromBody] DataType dataType) { - await _schemaModelService.UpdateModelMetadata(org, repository, modelName, dataType); + await _schemaModelService.SetModelDataType(org, repository, modelName, dataType); } private static string GetFileNameFromUploadedFile(IFormFile thefile) diff --git a/backend/src/Designer/Services/Implementation/SchemaModelService.cs b/backend/src/Designer/Services/Implementation/SchemaModelService.cs index 9c5cf7da8c7..fbb7d83534b 100644 --- a/backend/src/Designer/Services/Implementation/SchemaModelService.cs +++ b/backend/src/Designer/Services/Implementation/SchemaModelService.cs @@ -436,17 +436,17 @@ private bool NamespaceNeedsToBeSeparated(ApplicationMetadata application, return application.DataTypes.All(d => d.AppLogic?.ClassRef != $"Altinn.App.Models.{csharpModelName}"); } - public async Task GetModelMetadata(string org, string app, string modelName) + public async Task GetModelDataType(string org, string app, string modelId) { ApplicationMetadata applicationMetadata = await _applicationMetadataService.GetApplicationMetadataFromRepository(org, app); - DataType dataType = applicationMetadata.DataTypes.Find((dataType) => dataType.Id == modelName); + DataType dataType = applicationMetadata.DataTypes.Find((dataType) => dataType.Id == modelId); return dataType; } - public async Task UpdateModelMetadata(string org, string app, string modelName, DataType dataType) + public async Task SetModelDataType(string org, string app, string modelId, DataType dataType) { ApplicationMetadata applicationMetadata = await _applicationMetadataService.GetApplicationMetadataFromRepository(org, app); - applicationMetadata.DataTypes.RemoveAll((dataType) => dataType.Id == modelName); + applicationMetadata.DataTypes.RemoveAll((dataType) => dataType.Id == modelId); applicationMetadata.DataTypes.Add(dataType); await _applicationMetadataService.UpdateApplicationMetaDataLocally(org, app, applicationMetadata); } diff --git a/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs b/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs index 358df6d24a9..16cc0a91f34 100644 --- a/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs +++ b/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs @@ -86,13 +86,13 @@ public interface ISchemaModelService Task GenerateModelMetadataFromJsonSchema(AltinnRepoEditingContext altinnRepoEditingContext, string relativeFilePath, CancellationToken cancellationToken = default); /// - /// Gets the model metadata for a given model. + /// Gets the dataType for a given model. /// - Task GetModelMetadata(string org, string app, string modelName); + Task GetModelDataType(string org, string app, string modelId); /// - /// Updates the model metadata for a given model. + /// Updates the dataType for a given model. /// - Task UpdateModelMetadata(string org, string app, string modelName, DataType dataType); + Task SetModelDataType(string org, string app, string modelId, DataType dataType); } } From 66d32d27bc72b7127bb2eee7f726e8a70037d6a2 Mon Sep 17 00:00:00 2001 From: Jonas Dyrlie Date: Wed, 18 Dec 2024 13:46:21 +0100 Subject: [PATCH 06/10] Remove modelId parameter from setModelDataType method --- backend/src/Designer/Controllers/DatamodelsController.cs | 4 ++-- .../Designer/Services/Implementation/SchemaModelService.cs | 4 ++-- .../src/Designer/Services/Interfaces/ISchemaModelService.cs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/backend/src/Designer/Controllers/DatamodelsController.cs b/backend/src/Designer/Controllers/DatamodelsController.cs index 00d25445b8b..456dd725d8b 100644 --- a/backend/src/Designer/Controllers/DatamodelsController.cs +++ b/backend/src/Designer/Controllers/DatamodelsController.cs @@ -265,9 +265,9 @@ public async Task GetModelDataType(string org, string repository, /// [HttpPut("datamodel/{modelName}/dataType")] [UseSystemTextJson] - public async Task SetModelDataType(string org, string repository, string modelName, [FromBody] DataType dataType) + public async Task SetModelDataType(string org, string repository, [FromBody] DataType dataType) { - await _schemaModelService.SetModelDataType(org, repository, modelName, dataType); + await _schemaModelService.SetModelDataType(org, repository, dataType); } private static string GetFileNameFromUploadedFile(IFormFile thefile) diff --git a/backend/src/Designer/Services/Implementation/SchemaModelService.cs b/backend/src/Designer/Services/Implementation/SchemaModelService.cs index fbb7d83534b..8cbf12ea32f 100644 --- a/backend/src/Designer/Services/Implementation/SchemaModelService.cs +++ b/backend/src/Designer/Services/Implementation/SchemaModelService.cs @@ -443,10 +443,10 @@ public async Task GetModelDataType(string org, string app, string mode return dataType; } - public async Task SetModelDataType(string org, string app, string modelId, DataType dataType) + public async Task SetModelDataType(string org, string app, DataType dataType) { ApplicationMetadata applicationMetadata = await _applicationMetadataService.GetApplicationMetadataFromRepository(org, app); - applicationMetadata.DataTypes.RemoveAll((dataType) => dataType.Id == modelId); + applicationMetadata.DataTypes.RemoveAll((dt) => dt.Id == dataType.Id); applicationMetadata.DataTypes.Add(dataType); await _applicationMetadataService.UpdateApplicationMetaDataLocally(org, app, applicationMetadata); } diff --git a/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs b/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs index 16cc0a91f34..ee90cb10e22 100644 --- a/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs +++ b/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs @@ -93,6 +93,6 @@ public interface ISchemaModelService /// /// Updates the dataType for a given model. /// - Task SetModelDataType(string org, string app, string modelId, DataType dataType); + Task SetModelDataType(string org, string app, DataType dataType); } } From 464758993696b4b144405851035d49a1a1691bc5 Mon Sep 17 00:00:00 2001 From: Jonas Dyrlie Date: Wed, 18 Dec 2024 14:11:46 +0100 Subject: [PATCH 07/10] Add tests for datamodel datatype endpoints --- .../DataModelsController/GetTests.cs | 11 +++++++++ .../DataModelsController/PutDatamodelTests.cs | 24 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/backend/tests/Designer.Tests/Controllers/DataModelsController/GetTests.cs b/backend/tests/Designer.Tests/Controllers/DataModelsController/GetTests.cs index bac7b5943e7..6484a7b371d 100644 --- a/backend/tests/Designer.Tests/Controllers/DataModelsController/GetTests.cs +++ b/backend/tests/Designer.Tests/Controllers/DataModelsController/GetTests.cs @@ -26,4 +26,15 @@ public async Task GetDatamodel_ValidPath_ShouldReturnContent(string modelPath, s using var response = await HttpClient.SendAsync(httpRequestMessage); response.StatusCode.Should().Be(HttpStatusCode.OK); } + + [Theory] + [InlineData("test", "ttd", "hvem-er-hvem")] + public async Task GetDatamodelDataType_ShouldReturnContent(string modelName, string org, string repo) + { + string url = $"{VersionPrefix(org, repo)}/datamodel/{modelName}/dataType"; + using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, url); + + using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage); + response.StatusCode.Should().Be(HttpStatusCode.OK); + } } diff --git a/backend/tests/Designer.Tests/Controllers/DataModelsController/PutDatamodelTests.cs b/backend/tests/Designer.Tests/Controllers/DataModelsController/PutDatamodelTests.cs index 0ee22606477..8f92c9939b0 100644 --- a/backend/tests/Designer.Tests/Controllers/DataModelsController/PutDatamodelTests.cs +++ b/backend/tests/Designer.Tests/Controllers/DataModelsController/PutDatamodelTests.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Net; using System.Net.Http; +using System.Net.Http.Json; using System.Net.Mime; using System.Text; using System.Text.Json; @@ -11,6 +12,7 @@ using System.Web; using System.Xml; using System.Xml.Schema; +using Altinn.Platform.Storage.Interface.Models; using Altinn.Studio.DataModeling.Converter.Json; using Altinn.Studio.DataModeling.Converter.Json.Strategy; using Altinn.Studio.DataModeling.Converter.Metadata; @@ -144,6 +146,28 @@ public async Task IncompatibleSchema_ShouldReturn422(string modelPath, string sc } } + [Theory] + [InlineData("test", "ttd", "hvem-er-hvem")] + public async Task PutDatamodelDataType_ShouldReturnWithoutErrors(string datamodelName, string org, string repo) + { + string url = $"{VersionPrefix(org, repo)}/datamodel/{datamodelName}/dataType"; + await CopyRepositoryForTest(org, repo, "testUser", TargetTestRepository); + + DataType dataType = new() + { + Id = datamodelName, + MaxCount = 1, + MinCount = 1, + }; + using var request = new HttpRequestMessage(HttpMethod.Get, url) + { + Content = JsonContent.Create(dataType) + }; + + HttpResponseMessage response = await HttpClient.SendAsync(request); + response.StatusCode.Should().Be(HttpStatusCode.OK); + } + private async Task FilesWithCorrectNameAndContentShouldBeCreated(string modelName) { var location = Path.GetFullPath(Path.Combine(TestRepoPath, "App", "models")); From 4086911f7375994c00bc676bd86c56232502b29f Mon Sep 17 00:00:00 2001 From: Jonas Dyrlie Date: Thu, 19 Dec 2024 12:13:40 +0100 Subject: [PATCH 08/10] Rework tests --- .../Controllers/DatamodelsController.cs | 17 +++++++--- .../Implementation/SchemaModelService.cs | 6 +++- .../Interfaces/ISchemaModelService.cs | 2 +- .../DataModelsController/GetTests.cs | 34 +++++++++++++++++-- .../DataModelsController/PutDatamodelTests.cs | 34 ++++++++++++++++--- 5 files changed, 80 insertions(+), 13 deletions(-) diff --git a/backend/src/Designer/Controllers/DatamodelsController.cs b/backend/src/Designer/Controllers/DatamodelsController.cs index 456dd725d8b..152eae5238c 100644 --- a/backend/src/Designer/Controllers/DatamodelsController.cs +++ b/backend/src/Designer/Controllers/DatamodelsController.cs @@ -254,10 +254,10 @@ public async Task UseXsdFromRepo(string org, string repository, s /// [HttpGet("datamodel/{modelName}/dataType")] [UseSystemTextJson] - public async Task GetModelDataType(string org, string repository, string modelName) + public async Task> GetModelDataType(string org, string repository, string modelName) { - DataType dataTypeConfiguration = await _schemaModelService.GetModelDataType(org, repository, modelName); - return Ok(dataTypeConfiguration); + DataType dataType = await _schemaModelService.GetModelDataType(org, repository, modelName); + return Ok(dataType); } /// @@ -265,9 +265,16 @@ public async Task GetModelDataType(string org, string repository, /// [HttpPut("datamodel/{modelName}/dataType")] [UseSystemTextJson] - public async Task SetModelDataType(string org, string repository, [FromBody] DataType dataType) + public async Task SetModelDataType(string org, string repository, string modelName, [FromBody] DataType dataType) { - await _schemaModelService.SetModelDataType(org, repository, dataType); + if (!Equals(modelName, dataType.Id)) + { + return BadRequest("Model name in path and request body does not match"); + } + + await _schemaModelService.SetModelDataType(org, repository, modelName, dataType); + DataType updatedDataType = await _schemaModelService.GetModelDataType(org, repository, modelName); + return Ok(updatedDataType); } private static string GetFileNameFromUploadedFile(IFormFile thefile) diff --git a/backend/src/Designer/Services/Implementation/SchemaModelService.cs b/backend/src/Designer/Services/Implementation/SchemaModelService.cs index 8cbf12ea32f..556440a7e3c 100644 --- a/backend/src/Designer/Services/Implementation/SchemaModelService.cs +++ b/backend/src/Designer/Services/Implementation/SchemaModelService.cs @@ -443,8 +443,12 @@ public async Task GetModelDataType(string org, string app, string mode return dataType; } - public async Task SetModelDataType(string org, string app, DataType dataType) + public async Task SetModelDataType(string org, string app, string modelId, DataType dataType) { + if (dataType.Id != modelId) + { + throw new ArgumentException("Provided modelId does not match the DataType's Id"); + } ApplicationMetadata applicationMetadata = await _applicationMetadataService.GetApplicationMetadataFromRepository(org, app); applicationMetadata.DataTypes.RemoveAll((dt) => dt.Id == dataType.Id); applicationMetadata.DataTypes.Add(dataType); diff --git a/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs b/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs index ee90cb10e22..16cc0a91f34 100644 --- a/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs +++ b/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs @@ -93,6 +93,6 @@ public interface ISchemaModelService /// /// Updates the dataType for a given model. /// - Task SetModelDataType(string org, string app, DataType dataType); + Task SetModelDataType(string org, string app, string modelId, DataType dataType); } } diff --git a/backend/tests/Designer.Tests/Controllers/DataModelsController/GetTests.cs b/backend/tests/Designer.Tests/Controllers/DataModelsController/GetTests.cs index 6484a7b371d..31c013aa15f 100644 --- a/backend/tests/Designer.Tests/Controllers/DataModelsController/GetTests.cs +++ b/backend/tests/Designer.Tests/Controllers/DataModelsController/GetTests.cs @@ -1,6 +1,9 @@ -using System.Net; +using System.Collections.Generic; +using System.Net; using System.Net.Http; +using System.Net.Http.Json; using System.Threading.Tasks; +using Altinn.Platform.Storage.Interface.Models; using Designer.Tests.Controllers.ApiTests; using FluentAssertions; using Microsoft.AspNetCore.Mvc.Testing; @@ -28,7 +31,7 @@ public async Task GetDatamodel_ValidPath_ShouldReturnContent(string modelPath, s } [Theory] - [InlineData("test", "ttd", "hvem-er-hvem")] + [InlineData("Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES", "ttd", "hvem-er-hvem")] public async Task GetDatamodelDataType_ShouldReturnContent(string modelName, string org, string repo) { string url = $"{VersionPrefix(org, repo)}/datamodel/{modelName}/dataType"; @@ -36,5 +39,32 @@ public async Task GetDatamodelDataType_ShouldReturnContent(string modelName, str using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage); response.StatusCode.Should().Be(HttpStatusCode.OK); + DataType dataTypeResponse = await response.Content.ReadFromJsonAsync(); + dataTypeResponse.Should().NotBeNull(); + dataTypeResponse.Should().BeEquivalentTo(new DataType + { + Id = "Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES", + AllowedContentTypes = new List { "application/xml" }, + AppLogic = new ApplicationLogic + { + AutoCreate = true, + ClassRef = "Altinn.App.Models.HvemErHvem_M" + }, + TaskId = "Task_1", + MaxCount = 1, + MinCount = 1 + }); + } + + [Theory] + [InlineData("notfound", "ttd", "hvem-er-hvem")] + public async Task GetDatamodelDataType_ShouldNullWhenNotFound(string modelName, string org, string repo) + { + string url = $"{VersionPrefix(org, repo)}/datamodel/{modelName}/dataType"; + using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, url); + + using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage); + response.StatusCode.Should().Be(HttpStatusCode.OK); + (await response.Content.ReadAsStringAsync()).Should().Be("null"); } } diff --git a/backend/tests/Designer.Tests/Controllers/DataModelsController/PutDatamodelTests.cs b/backend/tests/Designer.Tests/Controllers/DataModelsController/PutDatamodelTests.cs index 8f92c9939b0..4db5c7cf72f 100644 --- a/backend/tests/Designer.Tests/Controllers/DataModelsController/PutDatamodelTests.cs +++ b/backend/tests/Designer.Tests/Controllers/DataModelsController/PutDatamodelTests.cs @@ -147,10 +147,10 @@ public async Task IncompatibleSchema_ShouldReturn422(string modelPath, string sc } [Theory] - [InlineData("test", "ttd", "hvem-er-hvem")] + [InlineData("testmodelname", "ttd", "hvem-er-hvem")] public async Task PutDatamodelDataType_ShouldReturnWithoutErrors(string datamodelName, string org, string repo) { - string url = $"{VersionPrefix(org, repo)}/datamodel/{datamodelName}/dataType"; + string url = $"{VersionPrefix(org, TargetTestRepository)}/datamodel/{datamodelName}/dataType"; await CopyRepositoryForTest(org, repo, "testUser", TargetTestRepository); DataType dataType = new() @@ -159,15 +159,41 @@ public async Task PutDatamodelDataType_ShouldReturnWithoutErrors(string datamode MaxCount = 1, MinCount = 1, }; - using var request = new HttpRequestMessage(HttpMethod.Get, url) + using var putRequest = new HttpRequestMessage(HttpMethod.Put, url) { Content = JsonContent.Create(dataType) }; - HttpResponseMessage response = await HttpClient.SendAsync(request); + HttpResponseMessage response = await HttpClient.SendAsync(putRequest); response.StatusCode.Should().Be(HttpStatusCode.OK); + DataType dataTypeResponse = await response.Content.ReadFromJsonAsync(); + dataTypeResponse.Should().NotBeNull(); + dataTypeResponse.Should().BeEquivalentTo(dataType); } + [Theory] + [InlineData("testmodelname", "ttd", "hvem-er-hvem")] + public async Task PutDatamodelDataType_FailsIfDatamodelNameMismatchesObjectId(string datamodelName, string org, string repo) + { + string url = $"{VersionPrefix(org, TargetTestRepository)}/datamodel/{datamodelName}/dataType"; + await CopyRepositoryForTest(org, repo, "testUser", TargetTestRepository); + + DataType dataType = new() + { + Id = "wrongId", + MaxCount = 1, + MinCount = 1, + }; + using var putRequest = new HttpRequestMessage(HttpMethod.Put, url) + { + Content = JsonContent.Create(dataType) + }; + + HttpResponseMessage response = await HttpClient.SendAsync(putRequest); + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); + } + + private async Task FilesWithCorrectNameAndContentShouldBeCreated(string modelName) { var location = Path.GetFullPath(Path.Combine(TestRepoPath, "App", "models")); From 7fb6153ce591f02bf069f23daf94ca67536f54d0 Mon Sep 17 00:00:00 2001 From: Jonas Dyrlie Date: Thu, 19 Dec 2024 14:56:29 +0100 Subject: [PATCH 09/10] Update backend/src/Designer/Controllers/DatamodelsController.cs Co-authored-by: andreastanderen <71079896+standeren@users.noreply.github.com> --- backend/src/Designer/Controllers/DatamodelsController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/Designer/Controllers/DatamodelsController.cs b/backend/src/Designer/Controllers/DatamodelsController.cs index 152eae5238c..3d9195750ed 100644 --- a/backend/src/Designer/Controllers/DatamodelsController.cs +++ b/backend/src/Designer/Controllers/DatamodelsController.cs @@ -261,7 +261,7 @@ public async Task> GetModelDataType(string org, string re } /// - /// Updates the dataType for a given model. + /// Updates the dataType for a given data model. /// [HttpPut("datamodel/{modelName}/dataType")] [UseSystemTextJson] From ebeceae22315a376bc878903bc5b2dffab42fa81 Mon Sep 17 00:00:00 2001 From: Jonas Dyrlie Date: Thu, 19 Dec 2024 14:56:35 +0100 Subject: [PATCH 10/10] Update backend/src/Designer/Controllers/DatamodelsController.cs Co-authored-by: andreastanderen <71079896+standeren@users.noreply.github.com> --- backend/src/Designer/Controllers/DatamodelsController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/Designer/Controllers/DatamodelsController.cs b/backend/src/Designer/Controllers/DatamodelsController.cs index 3d9195750ed..6417218debf 100644 --- a/backend/src/Designer/Controllers/DatamodelsController.cs +++ b/backend/src/Designer/Controllers/DatamodelsController.cs @@ -250,7 +250,7 @@ public async Task UseXsdFromRepo(string org, string repository, s } /// - /// Gets the dataType for a given datamodel. + /// Gets the dataType for a given data model. /// [HttpGet("datamodel/{modelName}/dataType")] [UseSystemTextJson]