Beispiel #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);
        }
Beispiel #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));
            }
        }
Beispiel #3
0
        public async Task Convert_SeresBasicSchema(string jsonPath, string xsdPath)
        {
            var expectedXsd = ResourceHelpers.LoadXmlSchemaTestData(xsdPath);

            XmlSchema actualXsd = await ConvertJsonSchema(jsonPath);

            string actualXml = await Serialize(actualXsd);

            XmlSchemaAssertions.IsEquivalentTo(expectedXsd, actualXsd);
        }