Esempio n. 1
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);
        }
Esempio n. 2
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));
            }
        }
Esempio n. 3
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);
        }