diff --git a/backend/src/Designer/Controllers/DatamodelsController.cs b/backend/src/Designer/Controllers/DatamodelsController.cs index f7c57a88525..6417218debf 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,34 @@ public async Task UseXsdFromRepo(string org, string repository, s } } + /// + /// Gets the dataType for a given data model. + /// + [HttpGet("datamodel/{modelName}/dataType")] + [UseSystemTextJson] + public async Task> GetModelDataType(string org, string repository, string modelName) + { + DataType dataType = await _schemaModelService.GetModelDataType(org, repository, modelName); + return Ok(dataType); + } + + /// + /// Updates the dataType for a given data model. + /// + [HttpPut("datamodel/{modelName}/dataType")] + [UseSystemTextJson] + public async Task SetModelDataType(string org, string repository, string modelName, [FromBody] DataType 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) { 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..556440a7e3c 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,24 @@ private bool NamespaceNeedsToBeSeparated(ApplicationMetadata application, { return application.DataTypes.All(d => d.AppLogic?.ClassRef != $"Altinn.App.Models.{csharpModelName}"); } + + 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 == modelId); + return 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); + 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..16cc0a91f34 100644 --- a/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs +++ b/backend/src/Designer/Services/Interfaces/ISchemaModelService.cs @@ -84,5 +84,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 dataType for a given model. + /// + Task GetModelDataType(string org, string app, string modelId); + + /// + /// Updates the dataType for a given model. + /// + 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 bac7b5943e7..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; @@ -26,4 +29,42 @@ public async Task GetDatamodel_ValidPath_ShouldReturnContent(string modelPath, s using var response = await HttpClient.SendAsync(httpRequestMessage); response.StatusCode.Should().Be(HttpStatusCode.OK); } + + [Theory] + [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"; + using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, url); + + 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 0ee22606477..4db5c7cf72f 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,54 @@ public async Task IncompatibleSchema_ShouldReturn422(string modelPath, string sc } } + [Theory] + [InlineData("testmodelname", "ttd", "hvem-er-hvem")] + public async Task PutDatamodelDataType_ShouldReturnWithoutErrors(string datamodelName, string org, string repo) + { + string url = $"{VersionPrefix(org, TargetTestRepository)}/datamodel/{datamodelName}/dataType"; + await CopyRepositoryForTest(org, repo, "testUser", TargetTestRepository); + + DataType dataType = new() + { + Id = datamodelName, + 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.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")); 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();