public void Test(string extension)
        {
            List <string> aasxPaths = SamplesAasxDir.ListAasxPaths();

            using (var tmpDir = new TemporaryDirectory())
            {
                foreach (string aasxPath in aasxPaths)
                {
                    /*
                     * The chain is as follows:
                     * - First load from AASX (package A)
                     * - Convert package 1 to `extension` format and save as path 1
                     * - Load from the path 1 in `extension` format (package B)
                     * - Save package B in `extension` format to path 2
                     *
                     * We expect the content of the two files (path 1 and path 2, respectively) to be equal.
                     */
                    using (var packageA = new AdminShellPackageEnv(aasxPath))
                    {
                        string path1 = System.IO.Path.Combine(tmpDir.Path, $"first{extension}");
                        string path2 = System.IO.Path.Combine(tmpDir.Path, $"second{extension}");

                        packageA.SaveAs(path1, writeFreshly: true);

                        using (var packageB = new AdminShellPackageEnv(path1))
                        {
                            packageB.SaveAs(path2, writeFreshly: true);
                            AssertFilesEqual(path1, path2, aasxPath);
                        }
                    }
                }
            }
        }
예제 #2
0
        /*
         * TODO (mristin, 2020-10-05): This test has been temporary disabled so that we can merge in the branch
         * MIHO/EnhanceDocumentShelf. The test should be fixed in a future pull request and we will then re-enable it
         * again.
         *
         * Please do not forget to remove the Resharper directive at the top of this class.
         *
         * [Test]
         *
         * dead-csharp ignore this comment
         */
        public void TestLoadSaveXmlValidate()
        {
            var validator = AasSchemaValidation.NewXmlValidator();

            List <string> aasxPaths = SamplesAasxDir.ListAasxPaths();

            using (var tmpDir = new TemporaryDirectory())
            {
                string tmpDirPath = tmpDir.Path;

                foreach (string aasxPath in aasxPaths)
                {
                    using (var package = new AdminShellPackageEnv(aasxPath))
                    {
                        /*
                         * TODO (mristin, 2020-09-17): Remove autofix once XSD and Aasx library in sync
                         *
                         * Package has been loaded, now we need to do an automatic check & fix.
                         *
                         * This is necessary as Aasx library is still not conform with the XSD AASX schema and breaks
                         * certain constraints (*e.g.*, the cardinality of langString = 1..*).
                         */
                        var recs = package.AasEnv.ValidateAll();
                        if (recs != null)
                        {
                            package.AasEnv.AutoFix(recs);
                        }

                        // Save as XML
                        string name    = Path.GetFileName(aasxPath);
                        string outPath = System.IO.Path.Combine(tmpDirPath, $"{name}.converted.xml");
                        package.SaveAs(outPath, writeFreshly: true);

                        using (var fileStream = System.IO.File.OpenRead(outPath))
                        {
                            var records = new AasValidationRecordList();
                            validator.Validate(records, fileStream);
                            if (records.Count != 0)
                            {
                                var parts = new List <string>
                                {
                                    $"Failed to validate XML file exported from {aasxPath} to {outPath}:"
                                };
                                parts.AddRange(records.Select((r) => r.Message));
                                throw new AssertionException(string.Join(Environment.NewLine, parts));
                            }
                        }
                    }
                }
            }
        }
예제 #3
0
        public void TestLoadSaveXmlValidate()
        {
            // Load the schema

            var xmlSchemaSet = new Xml.Schema.XmlSchemaSet();

            xmlSchemaSet.XmlResolver = new Xml.XmlUrlResolver();

            string schemaPath = Path.Combine(
                TestContext.CurrentContext.TestDirectory,
                "Resources\\schemas\\xml\\AAS.xsd");

            xmlSchemaSet.Add(null, schemaPath);

            var schemaMessages = new List <string>();

            xmlSchemaSet.ValidationEventHandler +=
                (object sender, Xml.Schema.ValidationEventArgs e) => { schemaMessages.Add(e.Message); };
            xmlSchemaSet.Compile();

            if (schemaMessages.Count > 0)
            {
                var parts = new List <string> {
                    $"Failed to compile the schema: {schemaPath}"
                };
                parts.AddRange(schemaMessages);

                throw new InvalidOperationException(string.Join(Environment.NewLine, parts));
            }

            // Load-Save-Validate

            List <string> aasxPaths = SamplesAasxDir.ListAasxPaths();

            using (var tmpDir = new TemporaryDirectory())
            {
                string tmpDirPath = tmpDir.Path;

                foreach (string aasxPath in aasxPaths)
                {
                    using (var package = new AdminShellPackageEnv(aasxPath))
                    {
                        string name    = Path.GetFileName(aasxPath);
                        string outPath = System.IO.Path.Combine(tmpDirPath, $"{name}.converted.xml");

                        package.SaveAs(outPath, writeFreshly: true);

                        var settings = new Xml.XmlReaderSettings();
                        settings.ValidationType = Xml.ValidationType.Schema;
                        settings.Schemas        = xmlSchemaSet;

                        var messages = new List <string>();
                        settings.ValidationEventHandler +=
                            (object sender, Xml.Schema.ValidationEventArgs e) =>
                        {
                            messages.Add(e.Message);
                        };

                        using (var reader = Xml.XmlReader.Create(outPath, settings))
                        {
                            while (reader.Read())
                            {
                                // Invoke callbacks
                            }
                            ;

                            if (messages.Count > 0)
                            {
                                var parts = new List <string>
                                {
                                    $"Failed to validate XML file exported from {aasxPath} to {outPath}:"
                                };
                                parts.AddRange(messages);

                                throw new AssertionException(string.Join(Environment.NewLine, parts));
                            }
                        }
                    }
                }
            }
        }