Пример #1
0
        public async Task CreateSchemaFromXsd_DatamodelsRepo_ShouldStoreModel()
        {
            // Arrange
            var org = "ttd";
            var sourceRepository = "empty-datamodels";
            var developer        = "testUser";
            var targetRepository = Guid.NewGuid().ToString();

            await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);

            try
            {
                var altinnGitRepositoryFactory         = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory());
                ISchemaModelService schemaModelService = new SchemaModelService(altinnGitRepositoryFactory, TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings);
                var xsdStream = TestDataHelper.LoadDataFromEmbeddedResource("Designer.Tests._TestData.Model.Xsd.Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.xsd");
                xsdStream.Seek(0, System.IO.SeekOrigin.Begin);
                var schemaName        = "Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES";
                var fileName          = $"{schemaName}.xsd";
                var relativeDirectory = "App/models";
                var relativeFilePath  = $"{relativeDirectory}/{fileName}";

                // Act
                await schemaModelService.CreateSchemaFromXsd(org, targetRepository, developer, relativeFilePath, xsdStream);

                // Assert
                var altinnAppGitRepository = altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, targetRepository, developer);
                altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.schema.json").Should().BeTrue();
                altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.original.xsd").Should().BeTrue();
            }
            finally
            {
                TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
            }
        }
Пример #2
0
        public async Task DeleteSchema_ModelsRepo_ShouldDelete()
        {
            // Arrange
            var org = "ttd";
            var sourceRepository = "xyz-datamodels";
            var developer        = "testUser";
            var targetRepository = Guid.NewGuid().ToString();

            await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);

            try
            {
                var altinnGitRepositoryFactory = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory());

                ISchemaModelService schemaModelService = new SchemaModelService(altinnGitRepositoryFactory, TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings);
                var schemaFiles = schemaModelService.GetSchemaFiles(org, targetRepository, developer);
                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(org, targetRepository, developer, schemaToDelete.RepositoryRelativeUrl);

                // Assert
                schemaFiles = schemaModelService.GetSchemaFiles(org, targetRepository, developer);
                schemaFiles.Should().HaveCount(5);
            }
            finally
            {
                TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
            }
        }
Пример #3
0
        public async Task UploadSchemaFromXsd_InvalidXsd_ThrowsException()
        {
            // Arrange
            var org = "ttd";
            var sourceRepository = "empty-app-pref-json";
            var developer        = "testUser";
            var targetRepository = Guid.NewGuid().ToString();

            await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);

            try
            {
                var altinnGitRepositoryFactory         = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory());
                ISchemaModelService schemaModelService = new SchemaModelService(altinnGitRepositoryFactory, TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings);
                var xsdStream  = TestDataHelper.LoadDataFromEmbeddedResource("Designer.Tests._TestData.Model.Xsd.SimpleInvalidNonSeresSchema.xsd");
                var schemaName = "SimpleInvalidNonSeresSchema";
                var fileName   = $"{schemaName}.xsd";

                Func <Task> action = () => schemaModelService.BuildSchemaFromXsd(org, targetRepository, developer, fileName, xsdStream);

                // Act/assert
                await action.Should().ThrowAsync <XmlSchemaException>();
            }
            finally
            {
                TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
            }
        }
Пример #4
0
        public async Task PutDatamodel_ValidInput_ShouldUpdateFile(string modelPath)
        {
            var org = "ttd";
            var sourceRepository = "hvem-er-hvem";
            var developer        = "testUser";
            var targetRepository = Guid.NewGuid().ToString();

            await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);

            var    client = GetTestClient();
            var    url    = $"{_versionPrefix}/{org}/{targetRepository}/Datamodels/?modelPath={modelPath}";
            var    minimumValidJsonSchema = @"{""properties"":{""root"":{""$ref"":""#/definitions/rootType""}},""definitions"":{""rootType"":{""properties"":{""keyword"":{""type"":""string""}}}}}";
            string requestBody            = minimumValidJsonSchema;
            var    httpRequestMessage     = new HttpRequestMessage(HttpMethod.Put, url)
            {
                Content = new StringContent(requestBody, Encoding.UTF8, "application/json")
            };
            await AuthenticationUtil.AddAuthenticateAndAuthAndXsrFCookieToRequest(client, httpRequestMessage);

            try
            {
                var response = await client.SendAsync(httpRequestMessage);

                Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
            }
            finally
            {
                TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
            }
        }
Пример #5
0
        public async Task Put_ValidRepositorySettings_ShouldUpdate()
        {
            var org = "ttd";
            var sourceRepository = "xyz-datamodels";
            var developer        = "testUser";
            var targetRepository = $"{Guid.NewGuid()}-datamodels";
            await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);

            var                httpClient         = GetTestClient();
            string             requestUrl         = $"/designer/api/v1/{org}/{targetRepository}/repositorysettings";
            var                requestBody        = @"{""repoType"": ""Datamodels"", ""datamodelling.preference"": ""JsonSchema""}";
            HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, requestUrl)
            {
                Content = new StringContent(requestBody, Encoding.UTF8, "application/json")
            };
            await AuthenticationUtil.AddAuthenticateAndAuthAndXsrFCookieToRequest(httpClient, httpRequestMessage);

            try
            {
                HttpResponseMessage response = await httpClient.SendAsync(httpRequestMessage);

                var altinnStudioSettings = await response.Content.ReadAsAsync <AltinnStudioSettings>();

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal(DatamodellingPreference.JsonSchema, altinnStudioSettings.DatamodellingPreference);
                Assert.Equal(AltinnRepositoryType.Datamodels, altinnStudioSettings.RepoType);
            }
            finally
            {
                TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
            }
        }
Пример #6
0
        public async Task PostDatamodel_FromFormPost_ShouldReturnCreatedFromTemplate(string relativeDirectory, bool altinn2Compatible, string sourceRepository)
        {
            // Arrange
            var org              = "ttd";
            var developer        = "testUser";
            var targetRepository = Guid.NewGuid().ToString();

            await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);

            var client = GetTestClient();
            var url    = $"{_versionPrefix}/{org}/{targetRepository}/Datamodels/Post";

            var createViewModel = new CreateModelViewModel()
            {
                ModelName = "test", RelativeDirectory = relativeDirectory, Altinn2Compatible = altinn2Compatible
            };
            var postRequestMessage = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = JsonContent.Create(createViewModel, null, new System.Text.Json.JsonSerializerOptions()
                {
                    PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase
                })
            };

            await AuthenticationUtil.AddAuthenticateAndAuthAndXsrFCookieToRequest(client, postRequestMessage);

            // Act / Assert
            try
            {
                var postResponse = await client.SendAsync(postRequestMessage);

                Assert.Equal(HttpStatusCode.Created, postResponse.StatusCode);

                Assert.Equal("application/json", postResponse.Content.Headers.ContentType.MediaType);

                var postContent = await postResponse.Content.ReadAsStringAsync();

                Json.Schema.JsonSchema postJsonSchema = Json.Schema.JsonSchema.FromText(postContent);
                Assert.NotNull(postJsonSchema);

                // Try to read back the created schema to verify it's stored
                // at the location provided in the post response
                var location          = postResponse.Headers.Location;
                var getRequestMessage = new HttpRequestMessage(HttpMethod.Get, location);
                var getResponse       = await client.SendAsync(getRequestMessage);

                var getContent = await getResponse.Content.ReadAsStringAsync();

                var getJsonSchema = Json.Schema.JsonSchema.FromText(getContent);
                Assert.NotNull(getJsonSchema);
                Assert.Equal(postContent, getContent);
            }
            finally
            {
                TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
            }
        }
Пример #7
0
        public async Task UpdateSchema_AppRepo_ShouldUpdate()
        {
            // Arrange
            var org = "ttd";
            var sourceRepository = "hvem-er-hvem";
            var developer        = "testUser";
            var targetRepository = Guid.NewGuid().ToString();

            await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);

            try
            {
                var altinnGitRepositoryFactory = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory());

                // Act
                ISchemaModelService schemaModelService = new SchemaModelService(altinnGitRepositoryFactory, TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings);
                var expectedSchemaUpdates = @"{""properties"":{""root"":{""$ref"":""#/definitions/rootType""}},""definitions"":{""rootType"":{""properties"":{""keyword"":{""type"":""string""}}}}}";
                await schemaModelService.UpdateSchema(org, targetRepository, developer, $"App/models/HvemErHvem_SERES.schema.json", expectedSchemaUpdates);

                // Assert
                var altinnGitRepository = altinnGitRepositoryFactory.GetAltinnGitRepository(org, targetRepository, developer);

                var updatedSchema = await altinnGitRepository.ReadTextByRelativePathAsync("App/models/HvemErHvem_SERES.schema.json");

                updatedSchema.Should().BeEquivalentTo(expectedSchemaUpdates);

                var xsd = await altinnGitRepository.ReadTextByRelativePathAsync("App/models/HvemErHvem_SERES.xsd");

                // Generated XSD included for reference
                // <?xml version="1.0"?>
                // <xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                //   <xsd:element name="root" type="rootType" />
                //   <xsd:complexType name="rootType">
                //     <xsd:sequence>
                //       <xsd:element minOccurs="0" name="keyword" nillable="true" type="xsd:string" />
                //     </xsd:sequence>
                //   </xsd:complexType>
                // </xsd:schema>
                var xsdSchema = XDocument.Parse(xsd);
                xsdSchema.Root.Should().NotBeNull();
                xsdSchema.Root.Elements().First().Attributes().First(a => a.Name.LocalName == "name").Should().HaveValue("root");

                var metadataModelJson = await altinnGitRepository.ReadTextByRelativePathAsync("App/models/HvemErHvem_SERES.metadata.json");

                var jsonSchema = JsonSerializer.Deserialize <ModelMetadata>(metadataModelJson);
                jsonSchema.Org.Should().Be(org);
            }
            finally
            {
                TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
            }
        }
Пример #8
0
        public async Task ModelPreference_SettingsFileExistsButNotDatamodellingPreference_ShouldUseCorrectDefault()
        {
            var org = "ttd";
            var sourceRepository = "xyz-datamodels";
            var developer        = "testUser";
            var targetRepository = $"{Guid.NewGuid()}-datamodels";

            await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);

            try
            {
                var altinnGitRepository = GetTestRepository(org, targetRepository, developer);
                Assert.Equal(DatamodellingPreference.JsonSchema, await altinnGitRepository.GetDatamodellingPreference());
            }
            finally
            {
                TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
            }
        }
Пример #9
0
        public async Task CreateSchemaFromXsdWithTexts_AppRepo_ShouldCreateTextResourceFiles()
        {
            // Arrange
            var org = "ttd";
            var sourceRepository = "empty-app";
            var developer        = "testUser";
            var targetRepository = Guid.NewGuid().ToString();

            await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);

            try
            {
                var altinnGitRepositoryFactory         = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory());
                ISchemaModelService schemaModelService = new SchemaModelService(altinnGitRepositoryFactory, TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings);
                var xsdStream = TestDataHelper.LoadDataFromEmbeddedResource("Designer.Tests._TestData.Model.Xsd.Skjema-1603-12392.xsd");
                xsdStream.Seek(0, System.IO.SeekOrigin.Begin);
                var schemaName        = "Skjema-1603-12392";
                var fileName          = $"{schemaName}.xsd";
                var relativeDirectory = "App/models";
                var relativeFilePath  = $"{relativeDirectory}/{fileName}";

                // Act
                await schemaModelService.CreateSchemaFromXsd(org, targetRepository, developer, relativeFilePath, xsdStream);

                // Assert
                var altinnAppGitRepository = altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, targetRepository, developer);
                altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.metadata.json").Should().BeTrue();
                altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.schema.json").Should().BeTrue();
                altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.original.xsd").Should().BeTrue();
                altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.cs").Should().BeTrue();

                var textResource = await altinnAppGitRepository.GetTextResources("nb");

                textResource.Language.Should().Be("nb");
                textResource.Resources.Should().HaveCount(9);
                textResource.Resources.First(r => r.Id == "27688.KontaktpersonEPostdatadef27688.Label").Value.Should().Be("E-post");
            }
            finally
            {
                TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
            }
        }
Пример #10
0
        public async Task RepositoryType_SettingsDontExists_ShouldUseAppAsDefault()
        {
            var org = "ttd";
            var sourceRepository = "hvem-er-hvem";
            var developer        = "testUser";
            var targetRepository = Guid.NewGuid().ToString();

            await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);

            try
            {
                var altinnGitRepository = GetTestRepository(org, targetRepository, developer);
                Assert.Equal(AltinnRepositoryType.App, await altinnGitRepository.GetRepositoryType());
                Assert.True(altinnGitRepository.FileExistsByRelativePath(Path.Combine(".altinnstudio", "settings.json")));
            }
            finally
            {
                TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
            }
        }
Пример #11
0
        public async Task AddXsd_DatamodelsRepo_ShouldReturnCreated()
        {
            // Arrange
            var org = "ttd";
            var sourceRepository = "empty-datamodels";
            var developer        = "testUser";
            var targetRepository = Guid.NewGuid().ToString();

            await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);

            var client = GetTestClient();
            var url    = $"{_versionPrefix}/{org}/{targetRepository}/datamodels/upload";

            var fileStream    = TestDataHelper.LoadDataFromEmbeddedResource("Designer.Tests._TestData.Model.Xsd.Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.xsd");
            var formData      = new MultipartFormDataContent();
            var streamContent = new StreamContent(fileStream);

            streamContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
            formData.Add(streamContent, "file", "Kursdomene_HvemErHvem_M_2021-04-08_5742_34627_SERES.xsd");

            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = formData
            };

            await AuthenticationUtil.AddAuthenticateAndAuthAndXsrFCookieToRequest(client, httpRequestMessage);

            try
            {
                var response = await client.SendAsync(httpRequestMessage);

                Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            }
            finally
            {
                TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
            }
        }
Пример #12
0
        public async Task UploadSchemaFromXsd_OED_ModelsCreated()
        {
            // Arrange
            JsonSchemaKeywords.RegisterXsdKeywords();

            var org = "ttd";
            var sourceRepository = "empty-app";
            var developer        = "testUser";
            var targetRepository = Guid.NewGuid().ToString();

            await TestDataHelper.CopyRepositoryForTest(org, sourceRepository, developer, targetRepository);

            try
            {
                var altinnGitRepositoryFactory         = new AltinnGitRepositoryFactory(TestDataHelper.GetTestDataRepositoriesRootDirectory());
                ISchemaModelService schemaModelService = new SchemaModelService(altinnGitRepositoryFactory, TestDataHelper.LogFactory, TestDataHelper.ServiceRepositorySettings);
                var xsdStream         = TestDataHelper.LoadDataFromEmbeddedResource("Designer.Tests._TestData.Model.Xsd.OED.xsd");
                var schemaName        = "OED_M";
                var fileName          = $"{schemaName}.xsd";
                var relativeDirectory = "App/models";
                var relativeFilePath  = $"{relativeDirectory}/{fileName}";

                // Act
                var jsonSchema = await schemaModelService.BuildSchemaFromXsd(org, targetRepository, developer, fileName, xsdStream);

                // Assert
                var altinnAppGitRepository = altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, targetRepository, developer);
                altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.metadata.json").Should().BeTrue();
                altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.schema.json").Should().BeTrue();
                altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.xsd").Should().BeTrue();
                altinnAppGitRepository.FileExistsByRelativePath($"{relativeDirectory}/{schemaName}.cs").Should().BeTrue();
            }
            finally
            {
                TestDataHelper.DeleteAppRepository(org, targetRepository, developer);
            }
        }