Exemplo n.º 1
0
        public async Task ConvertSeresXsd_SeresGeneratedXsd_ShouldConvertToJsonSchemaAndBackToXsd(string xsdSchemaPath, string xmlPath)
        {
            JsonSchemaKeywords.RegisterXsdKeywords();

            XmlSchema originalXsd = ResourceHelpers.LoadXmlSchemaTestData(xsdSchemaPath);

            // Convert the XSD to JSON Schema
            var        xsdToJsonConverter        = new XmlSchemaToJsonSchemaConverter();
            JsonSchema convertedJsonSchema       = xsdToJsonConverter.Convert(originalXsd);
            var        convertedJsonSchemaString = JsonSerializer.Serialize(convertedJsonSchema, new JsonSerializerOptions()
            {
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Latin1Supplement), WriteIndented = true
            });

            // Convert the converted JSON Schema back to XSD
            var jsonToXsdConverter = new JsonSchemaToXmlSchemaConverter(new JsonSchemaNormalizer());
            var convertedXsd       = jsonToXsdConverter.Convert(convertedJsonSchema);

            var convertedXsdString = await Serialize(convertedXsd);

            var originalXsdString = await Serialize(originalXsd);

            // The two XSD's should be structural equal, but there might be minor differences if you compare the text
            XmlSchemaAssertions.IsEquivalentTo(originalXsd, convertedXsd);

            if (!string.IsNullOrEmpty(xmlPath))
            {
                // The XML should validate against both XSD's
                var xml = ResourceHelpers.LoadTestDataAsString(xmlPath);
                Assert.True(ValidateXml(originalXsd, xml));
                Assert.True(ValidateXml(convertedXsd, xml));
            }
        }
Exemplo n.º 2
0
        private static async Task TestFiles(string schemaPath, string expectedPath, string uri)
        {
            // Arrange
            JsonSchemaKeywords.RegisterXsdKeywords();

            JsonSchemaToXmlSchemaConverter converter = new JsonSchemaToXmlSchemaConverter(new JsonSchemaNormalizer());

            JsonSchema jsonSchema = await ResourceHelpers.LoadJsonSchemaTestData(schemaPath);

            XmlSchema expected = ResourceHelpers.LoadXmlSchemaTestData(expectedPath);

            // Act
            var       schemaUri = new Uri(uri, UriKind.RelativeOrAbsolute);
            XmlSchema actual    = converter.Convert(jsonSchema, schemaUri);

            StringBuilder xmlStringBuilder = new StringBuilder();

            await using (XmlWriter xmlWriter = XmlWriter.Create(xmlStringBuilder, new XmlWriterSettings
            {
                Async = true,
                CheckCharacters = true,
                ConformanceLevel = ConformanceLevel.Document,
                Indent = true,
                Encoding = SafeUtf8,
                OmitXmlDeclaration = false
            }))
            {
                actual.Write(xmlWriter);
            }

            string xsd = xmlStringBuilder.ToString();

            // Assert
            XmlSchemaAssertions.IsEquivalentTo(expected, actual);
        }
Exemplo n.º 3
0
        public JsonSchemaToMetamodelConverterTests(ITestOutputHelper outputHelper)
        {
            _outputHelper = outputHelper;

            JsonSchemaKeywords.RegisterXsdKeywords();
            JsonSchemaFormats.RegisterFormats();
        }
Exemplo n.º 4
0
        public async Task Analyze_SimpleContent_Extension(string path)
        {
            JsonSchemaKeywords.RegisterXsdKeywords();

            var schema = await ResourceHelpers.LoadJsonSchemaTestData(path);

            var analyzer = new SeresJsonSchemaAnalyzer();

            var metadata = analyzer.AnalyzeSchema(schema, new Uri("dummy", UriKind.Relative));

            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/myBase")).Should().Contain(new[] { CompatibleXsdType.ComplexType, CompatibleXsdType.SimpleContentExtension });
        }
Exemplo n.º 5
0
        public async Task Analyze_Seres_Converted_JsonSchema(string path)
        {
            JsonSchemaKeywords.RegisterXsdKeywords();

            var schema = await ResourceHelpers.LoadJsonSchemaTestData(path);

            var analyzer = new SeresJsonSchemaAnalyzer();

            var metadata = analyzer.AnalyzeSchema(schema, new Uri("dummy", UriKind.Relative));

            metadata.GetCompatibleTypes(JsonPointer.Parse("#")).Should().Equal(CompatibleXsdType.ComplexType);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/oneOf/[0]")).Should().Equal(CompatibleXsdType.ComplexType);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/melding-modell")).Should().Equal(CompatibleXsdType.ComplexType);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/melding-modell/properties/e1")).Should().Equal(CompatibleXsdType.SimpleType);
        }
Exemplo n.º 6
0
        public void Constructor_TemplateExists_ShouldSetCorrectValues()
        {
            // Arrange
            JsonSchemaKeywords.RegisterXsdKeywords();

            var expectedId = "https://dev.altinn.studio/org/repository/app/model/model.schema.json";

            // Act
            var actualJsonTemplate = new GeneralJsonTemplate(new Uri(expectedId), "model");

            // Assert
            JsonSchema jsonSchema = JsonSchema.FromText(actualJsonTemplate.GetJsonString());
            var        idKeyword  = jsonSchema.GetKeyword <IdKeyword>();

            idKeyword.Id.Should().Be(expectedId);
            var messageType = jsonSchema.FollowReference(JsonPointer.Parse("#/$defs/model")).Should().NotBeNull();
        }
Exemplo n.º 7
0
        public async Task Analyze_SimpleContent_Restriction(string path)
        {
            JsonSchemaKeywords.RegisterXsdKeywords();

            var schema = await ResourceHelpers.LoadJsonSchemaTestData(path);

            var analyzer = new SeresJsonSchemaAnalyzer();

            var metadata = analyzer.AnalyzeSchema(schema, new Uri("dummy", UriKind.Relative));

            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/ageType")).Should().Contain(CompatibleXsdType.SimpleType);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/limitedAgeType")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/genderType")).Should().Contain(CompatibleXsdType.SimpleType);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/limitedGenderType")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/person")).Should().Contain(CompatibleXsdType.SimpleContentExtension);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/limitedPerson")).Should().Contain(CompatibleXsdType.SimpleContentRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/limitedPerson-inline")).Should().Contain(CompatibleXsdType.SimpleContentRestriction);
        }
Exemplo n.º 8
0
        public async Task XmlSchema_to_JsonSchema_Converter(string schemaPath, string expectedPath, string testCase)
        {
            _testOutputHelper.WriteLine(testCase);

            // Arrange
            JsonSchemaKeywords.RegisterXsdKeywords();
            JsonSchemaFormats.RegisterFormats();
            var converter = new XmlSchemaToJsonSchemaConverter();

            var xsd    = ResourceHelpers.LoadXmlSchemaTestData(schemaPath);
            var actual = converter.Convert(xsd);

            var actualJson = await SerializeJsonSchemaToString(actual);

            // Assert
            var expected = await ResourceHelpers.LoadJsonSchemaTestData(expectedPath);

            JsonSchemaAssertions.IsEquivalentTo(expected, actual);
        }
Exemplo n.º 9
0
        public async Task CSharpModel_SeresBasic_ShouldSerializeToValidJson()
        {
            JsonSchemaKeywords.RegisterXsdKeywords();
            var melding = new _TestData.Model.CSharp.melding()
            {
                E1 = "Yo"
            };

            var json       = JsonSerializer.Serialize(melding);
            var jsonSchema = await EmbeddedResource.LoadDataFromEmbeddedResourceAsJsonSchema(SERESBASIC_JSON_SCHEMA_RESOURCE);

            var jsonDocument = JsonDocument.Parse(json);

            var validationResults = jsonSchema.Validate(jsonDocument.RootElement, new ValidationOptions()
            {
                OutputFormat = OutputFormat.Detailed
            });

            validationResults.IsValid.Should().BeTrue();
        }
Exemplo n.º 10
0
        public async Task Normalize_WithNormalization_ShouldRemoveSingleAllOfs(string jsonSchemaTestdata, string expectedNormalizedSchemaTestdata)
        {
            JsonSchemaKeywords.RegisterXsdKeywords();

            var jsonSchema = await ResourceHelpers.LoadJsonSchemaTestData(jsonSchemaTestdata);

            var jsonSchemaNormalizer     = new JsonSchemaNormalizer();
            var normalizedJsonSchema     = jsonSchemaNormalizer.Normalize(jsonSchema);
            var normalizedJsonSchemaText = JsonSerializer.Serialize(normalizedJsonSchema);

            var expectedNormalizedJsonSchema = await ResourceHelpers.LoadJsonSchemaTestData(expectedNormalizedSchemaTestdata);

            var expectedNormalizedJsonSchemaText = JsonSerializer.Serialize(expectedNormalizedJsonSchema);

            var json = JsonSerializer.Serialize(normalizedJsonSchema, new JsonSerializerOptions {
                WriteIndented = true
            });

            normalizedJsonSchemaText.Should().BeEquivalentTo(expectedNormalizedJsonSchemaText);
        }
Exemplo n.º 11
0
        public async Task Analyze_SimpleType_Restriction(string path)
        {
            JsonSchemaKeywords.RegisterXsdKeywords();

            var schema = await ResourceHelpers.LoadJsonSchemaTestData(path);

            var analyzer = new SeresJsonSchemaAnalyzer();

            var metadata = analyzer.AnalyzeSchema(schema, new Uri("dummy", UriKind.Relative));

            metadata.GetCompatibleTypes(JsonPointer.Parse("#/properties/t1")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/properties/t2")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/properties/t3")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/properties/t4")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/properties/n1")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/properties/n2")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/properties/f1")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/properties/f2")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/properties/f3")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/properties/f4")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/properties/f5")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/properties/f6")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/properties/c0")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);

            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/simpleString")).Should().Contain(CompatibleXsdType.SimpleType);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/SeresType")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/simpleString")).Should().NotContain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/stringMinMaxLengthRestrictions")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/stringLengthRestrictions")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/stringEnumRestrictions")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/stringPatternRestrictions")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/numberRestrictions")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/numberRestrictions2")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/numberRestrictionsFractional0")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/numberRestrictionsFractional1")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/numberRestrictionsFractional2")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/numberRestrictionsFractional3")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/numberRestrictionsFractional4")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/numberRestrictionsFractional5")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
            metadata.GetCompatibleTypes(JsonPointer.Parse("#/$defs/complexStructure")).Should().Contain(CompatibleXsdType.SimpleTypeRestriction);
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 13
0
        public async Task Normalize_NoNormalization_ShouldEqualSourceSchema(string jsonSchemaTestdata)
        {
            JsonSchemaKeywords.RegisterXsdKeywords();

            var jsonSchema = await ResourceHelpers.LoadJsonSchemaTestData(jsonSchemaTestdata);

            var jsonSchemaText = JsonSerializer.Serialize(jsonSchema, new JsonSerializerOptions()
            {
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Latin1Supplement)
            });

            var jsonSchemaNormalizer = new JsonSchemaNormalizer()
            {
                PerformNormalization = false
            };

            var normalizedJsonSchema     = jsonSchemaNormalizer.Normalize(jsonSchema);
            var normalizedJsonSchemaText = JsonSerializer.Serialize(normalizedJsonSchema, new JsonSerializerOptions()
            {
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Latin1Supplement)
            });

            normalizedJsonSchemaText.Should().BeEquivalentTo(jsonSchemaText);
        }
Exemplo n.º 14
0
 public SeresConverterTests()
 {
     JsonSchemaKeywords.RegisterXsdKeywords();
 }
Exemplo n.º 15
0
 public JsonSchemaSeresAnalyzerTests(ITestOutputHelper testOutputHelper)
 {
     _testOutputHelper = testOutputHelper;
     JsonSchemaKeywords.RegisterXsdKeywords();
 }