Esempio 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));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Parses the XSD, generates Json Schema and generates the meta model using
        /// the new classes.
        /// </summary>
        private static ModelMetadata CreateMetamodelNewWay(string xsdResource, string org, string app)
        {
            Stream    xsdStream = TestDataHelper.LoadDataFromEmbeddedResource(xsdResource);
            XmlReader xmlReader = XmlReader.Create(xsdStream, new XmlReaderSettings {
                IgnoreWhitespace = true
            });
            var xmlSchema = XmlSchema.Read(xmlReader, (_, _) => { });
            var schemaSet = new XmlSchemaSet();

            schemaSet.Add(xmlSchema);
            schemaSet.Compile();

            var xsdToJsonConverter = new XmlSchemaToJsonSchemaConverter();

            Json.Schema.JsonSchema convertedJsonSchema = xsdToJsonConverter.Convert(xmlSchema);
            var convertedJsonSchemaString = JsonSerializer.Serialize(convertedJsonSchema, new JsonSerializerOptions()
            {
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Latin1Supplement), WriteIndented = true
            });

            var metamodelConverter = new JsonSchemaToMetamodelConverter(new SeresJsonSchemaAnalyzer());

            ModelMetadata actualMetamodel = metamodelConverter.Convert("melding", convertedJsonSchemaString);

            return(actualMetamodel);
        }
Esempio n. 3
0
        private Json.Schema.JsonSchema GenerateJsonSchemaFromXsd(Stream xsdStream)
        {
            XmlSchema originalXsd = XmlSchema.Read(xsdStream, (_, _) => { });

            var xsdToJsonConverter = new XmlSchemaToJsonSchemaConverter();

            Json.Schema.JsonSchema convertedJsonSchema = xsdToJsonConverter.Convert(originalXsd);

            return(convertedJsonSchema);
        }
Esempio n. 4
0
        public void Convert_FromSeresSchema_ShouldConvert(string xsdSchemaPath, string expectedMetamodelPath)
        {
            // Arrange
            // Convert the Seres XSD to JSON Schema
            XmlSchema  originalXsd               = TestDataHelper.LoadXmlSchemaTestData(xsdSchemaPath);
            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 to Metadata model
            var metamodelConverter = new JsonSchemaToMetamodelConverter(new SeresJsonSchemaAnalyzer());

            metamodelConverter.KeywordProcessed   += KeywordProcessedHandler;
            metamodelConverter.SubSchemaProcessed += SubSchemaProcessedHandler;

            // Act
            var actualMetamodel = metamodelConverter.Convert("melding", convertedJsonSchemaString);

            // Leaving this for easy access/manual verification
            var actualMetamodelJson = JsonSerializer.Serialize(actualMetamodel, new JsonSerializerOptions()
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true, Converters = { new JsonStringEnumConverter() }
            });

            // Assert
            var expectedMetamodelJson = TestDataHelper.LoadTestDataFromFileAsString(expectedMetamodelPath);
            var expectedMetamodel     = JsonSerializer.Deserialize <ModelMetadata>(expectedMetamodelJson, new JsonSerializerOptions()
            {
                PropertyNameCaseInsensitive = true, Converters = { new JsonStringEnumConverter() }
            });

            MetadataAssertions.IsEquivalentTo(expectedMetamodel, actualMetamodel);

            actualMetamodel.Elements.Values.Where(e => e.ParentElement == null).ToList().Count.Should().Be(1);

            // Compile the generated class to verify it compiles without errors
            var classes          = new JsonMetadataParser().CreateModelFromMetadata(actualMetamodel);
            var compiledAssembly = Compiler.CompileToAssembly(classes);

            compiledAssembly.Should().NotBeNull();
        }
Esempio n. 5
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);
        }