-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate namespace for new models (#12402)
* separate namespace for new models * tests added
- Loading branch information
1 parent
fb00671
commit 923b009
Showing
6 changed files
with
145 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
backend/src/DataModeling/Metamodel/ModelMetadataExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System.Linq; | ||
|
||
namespace Altinn.Studio.DataModeling.Metamodel; | ||
|
||
public static class ModelMetadataExtensions | ||
{ | ||
public static ElementMetadata GetRootElement(this ModelMetadata modelMetadata) => modelMetadata.Elements.Values.First(e => e.ParentElement == null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
backend/tests/Designer.Tests/Controllers/DataModelsController/AddXsd_CsharpNamespaceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Altinn.App.Core.Models; | ||
using Designer.Tests.Controllers.ApiTests; | ||
using Designer.Tests.Utils; | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using SharedResources.Tests; | ||
using Xunit; | ||
|
||
namespace Designer.Tests.Controllers.DataModelsController; | ||
|
||
public class AddXsd_CsharpNamespaceTests : DisagnerEndpointsTestsBase<AddXsd_CsharpNamespaceTests>, IClassFixture<WebApplicationFactory<Program>> | ||
{ | ||
private static string VersionPrefix(string org, string repository) => $"/designer/api/{org}/{repository}/datamodels"; | ||
|
||
public AddXsd_CsharpNamespaceTests(WebApplicationFactory<Program> factory) : base(factory) | ||
{ | ||
} | ||
|
||
[Theory] | ||
[InlineData("Model/XmlSchema/Gitea/aal-vedlegg.xsd", "ttd", "empty-app", "testUser", "App/models/aal-vedlegg.cs", "Altinn.App.Models.vedlegg", "vedlegg", "Altinn.App.Models")] | ||
[InlineData("Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.xsd", "ttd", "hvem-er-hvem", "testUser", "App/models/Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.cs", "Altinn.App.Models", "HvemErHvem_M", "Altinn.App.Models.HvemErHvem_M")] | ||
public async Task GivenApp_ShouldProduce_CorrectNamespace(string xsdPath, string org, string repo, string developer, string expectedModelPath, string expectedNamespace, string expectedTypeName, string notExpectedNamespace) | ||
{ | ||
string targetRepository = TestDataHelper.GenerateTestRepoName(); | ||
await CopyRepositoryForTest(org, repo, developer, targetRepository); | ||
string url = $"{VersionPrefix(org, targetRepository)}/upload"; | ||
|
||
var fileStream = SharedResourcesHelper.LoadTestData(xsdPath); | ||
var formData = new MultipartFormDataContent(); | ||
var streamContent = new StreamContent(fileStream); | ||
streamContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data"); | ||
formData.Add(streamContent, "file", Path.GetFileName(xsdPath)); | ||
|
||
using var response = await HttpClient.PostAsync(url, formData); | ||
Assert.Equal(HttpStatusCode.Created, response.StatusCode); | ||
|
||
// get the csharp model from repo | ||
string csharpModel = TestDataHelper.GetFileFromRepo(org, targetRepository, developer, expectedModelPath); | ||
csharpModel.Should().Contain($"namespace {expectedNamespace}\n{{"); | ||
|
||
string applicationMetadataContent = TestDataHelper.GetFileFromRepo(org, targetRepository, developer, "App/config/applicationmetadata.json"); | ||
var applicationMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(applicationMetadataContent, JsonSerializerOptions); | ||
|
||
applicationMetadata.DataTypes.Should().Contain(x => x.AppLogic != null && x.AppLogic.ClassRef == | ||
$"{expectedNamespace}.{expectedTypeName}"); | ||
|
||
applicationMetadata.DataTypes.Should().NotContain(x => x.AppLogic != null && x.AppLogic.ClassRef == | ||
$"{notExpectedNamespace}.{expectedTypeName}"); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...ests/Designer.Tests/Controllers/DataModelsController/PutDatamodel_CsharpNamespaceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Mime; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Altinn.App.Core.Models; | ||
using Altinn.Studio.DataModeling.Templates; | ||
using Designer.Tests.Controllers.ApiTests; | ||
using Designer.Tests.Utils; | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Xunit; | ||
|
||
namespace Designer.Tests.Controllers.DataModelsController; | ||
|
||
public class PutDatamodel_CsharpNamespaceTests : DisagnerEndpointsTestsBase<PutDatamodel_CsharpNamespaceTests>, IClassFixture<WebApplicationFactory<Program>> | ||
{ | ||
private static string VersionPrefix(string org, string repository) => $"/designer/api/{org}/{repository}/datamodels"; | ||
|
||
public PutDatamodel_CsharpNamespaceTests(WebApplicationFactory<Program> factory) : base(factory) | ||
{ | ||
} | ||
|
||
[Theory] | ||
[InlineData("App/models/Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.schema.json", "ttd", "hvem-er-hvem", "testUser", "HvemErHvem_M", "App/models/Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.cs", "Altinn.App.Models", "Altinn.App.Models.HvemErHvem_M")] | ||
[InlineData("App/models/newmodel.schema.json", "ttd", "empty-app", "testUser", "newmodel", "App/models/newmodel.cs", "Altinn.App.Models.newmodel", "Altinn.App.Models")] | ||
public async Task GivenApp_ShouldProduce_CorrectNamespace(string modelPath, string org, string repo, string developer, string expectedModelName, string expectedModelPath, string expectedNamespace, string notExpectedNamespace) | ||
{ | ||
string targetRepo = TestDataHelper.GenerateTestRepoName(); | ||
await CopyRepositoryForTest(org, repo, developer, targetRepo); | ||
string url = $"{VersionPrefix(org, targetRepo)}/datamodel?modelPath={modelPath}"; | ||
|
||
string schema = new GeneralJsonTemplate(new Uri("http://altinn-testschema.json"), expectedModelName).GetJsonString(); | ||
|
||
using var request = new HttpRequestMessage(HttpMethod.Put, url) | ||
{ | ||
Content = new StringContent(schema, Encoding.UTF8, MediaTypeNames.Application.Json) | ||
}; | ||
|
||
using var response = await HttpClient.SendAsync(request); | ||
response.StatusCode.Should().Be(HttpStatusCode.NoContent); | ||
// get the csharp model from repo | ||
string csharpModel = TestDataHelper.GetFileFromRepo(org, targetRepo, developer, expectedModelPath); | ||
csharpModel.Should().Contain($"namespace {expectedNamespace}\n{{"); | ||
|
||
string applicationMetadataContent = TestDataHelper.GetFileFromRepo(org, targetRepo, developer, "App/config/applicationmetadata.json"); | ||
var applicationMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(applicationMetadataContent, JsonSerializerOptions); | ||
|
||
applicationMetadata.DataTypes.Should().Contain(x => x.AppLogic != null && x.AppLogic.ClassRef == | ||
$"{expectedNamespace}.{expectedModelName}"); | ||
|
||
applicationMetadata.DataTypes.Should().NotContain(x => x.AppLogic != null && x.AppLogic.ClassRef == | ||
$"{notExpectedNamespace}.{expectedModelName}"); | ||
} | ||
} |