Skip to content

Commit

Permalink
Refactor schemaModelServiceTests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jondyr committed Dec 12, 2024
1 parent cc0d6c2 commit fe7279e
Showing 1 changed file with 35 additions and 61 deletions.
96 changes: 35 additions & 61 deletions backend/tests/Designer.Tests/Services/SchemaModelServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@
using Altinn.Studio.Designer.Services.Interfaces;
using Designer.Tests.Utils;
using FluentAssertions;
using Moq;
using SharedResources.Tests;
using Xunit;

namespace Designer.Tests.Services
{
public class SchemaModelServiceTests
{
private readonly Mock<IApplicationMetadataService> _applicationMetadataService;
private readonly AltinnGitRepositoryFactory _altinnGitRepositoryFactory;
private readonly ISchemaModelService _schemaModelService;

public SchemaModelServiceTests()
{
_applicationMetadataService = new Mock<IApplicationMetadataService>();
_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()
{
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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<CsharpCompilationException>(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()
{
Expand All @@ -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<Task> action = () => schemaModelService.BuildSchemaFromXsd(editingContext, fileName, xsdStream);
Func<Task> action = () => _schemaModelService.BuildSchemaFromXsd(editingContext, fileName, xsdStream);

// Act/assert
await action.Should().ThrowAsync<XmlSchemaException>();
Expand All @@ -333,19 +311,17 @@ 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";
var relativeDirectory = "App/models";
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();
Expand All @@ -371,19 +347,17 @@ 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";
var relativeDirectory = "App/models";
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();
Expand Down

0 comments on commit fe7279e

Please sign in to comment.