Index: Riskeer/Common/src/Riskeer.Common.IO/Configurations/Import/CalculationConfigurationMigrator.cs =================================================================== diff -u --- Riskeer/Common/src/Riskeer.Common.IO/Configurations/Import/CalculationConfigurationMigrator.cs (revision 0) +++ Riskeer/Common/src/Riskeer.Common.IO/Configurations/Import/CalculationConfigurationMigrator.cs (revision 09f8bc173387fe5ca6b8735595d5a4b485623585) @@ -0,0 +1,78 @@ +// Copyright (C) Stichting Deltares 2019. All rights reserved. +// +// This file is part of Riskeer. +// +// Riskeer is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.IO; +using System.Text; +using System.Xml; +using System.Xml.Linq; +using System.Xml.Xsl; + +namespace Riskeer.Common.IO.Configurations.Import +{ + /// + /// Class to migrate different calculation configuration versions. + /// + public static class CalculationConfigurationMigrator + { + /// + /// Migrates the given with the . + /// + /// The to migrate. + /// The migration script. + /// The migrated . + /// Thrown when + /// is null. + public static XDocument Migrate(XDocument xmlDocument, string migrationScriptDefinition) + { + if (xmlDocument == null) + { + throw new ArgumentNullException(nameof(xmlDocument)); + } + + var stringBuilder = new StringBuilder(); + + XslCompiledTransform transformer = CreateTransformer(migrationScriptDefinition); + + using (var writer = XmlWriter.Create(stringBuilder)) + { + transformer.Transform(xmlDocument.CreateReader(ReaderOptions.None), writer); + writer.Close(); + writer.Flush(); + } + + return XDocument.Parse(stringBuilder.ToString()); + } + + private static XslCompiledTransform CreateTransformer(string migrationScriptDefinition) + { + var xslCompiledTransform = new XslCompiledTransform(); + + using (var stringReader = new StringReader(migrationScriptDefinition)) + using (var xmlReader = XmlReader.Create(stringReader)) + { + xslCompiledTransform.Load(xmlReader); + } + + return xslCompiledTransform; + } + } +} \ No newline at end of file Fisheye: Tag 09f8bc173387fe5ca6b8735595d5a4b485623585 refers to a dead (removed) revision in file `Riskeer/Common/src/Riskeer.Common.IO/Configurations/Import/XmlMigrator.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Riskeer/Common/test/Riskeer.Common.IO.Test/Configurations/Import/CalculationConfigurationMigratorTest.cs =================================================================== diff -u --- Riskeer/Common/test/Riskeer.Common.IO.Test/Configurations/Import/CalculationConfigurationMigratorTest.cs (revision 0) +++ Riskeer/Common/test/Riskeer.Common.IO.Test/Configurations/Import/CalculationConfigurationMigratorTest.cs (revision 09f8bc173387fe5ca6b8735595d5a4b485623585) @@ -0,0 +1,89 @@ +// Copyright (C) Stichting Deltares 2019. All rights reserved. +// +// This file is part of Riskeer. +// +// Riskeer is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.IO; +using System.Xml.Linq; +using Core.Common.TestUtil; +using NUnit.Framework; +using Riskeer.Common.IO.Configurations.Import; + +namespace Riskeer.Common.IO.Test.Configurations.Import +{ + [TestFixture] + public class CalculationConfigurationMigratorTest + { + private readonly string testDirectoryPath = TestHelper.GetTestDataPath(TestDataPath.Riskeer.Common.IO, + nameof(CalculationConfigurationMigrator)); + [Test] + public void Migrate_XmlDocumentNull_ThrowsArgumentNullException() + { + // Call + void Call() => CalculationConfigurationMigrator.Migrate(null, ""); + + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual("xmlDocument", exception.ParamName); + } + + [Test] + public void Migrate_EmptyMigrationScript_MigratesXmlDocument() + { + // Setup + string xslt = File.ReadAllText(Path.Combine(testDirectoryPath, "EmptyMigrationScript.xslt")); + XDocument xmlDocument = GetDefaultXDocument(); + + // Call + XDocument migratedXDocument = CalculationConfigurationMigrator.Migrate(xmlDocument, xslt); + + // Assert + Assert.IsTrue(XNode.DeepEquals(xmlDocument, migratedXDocument)); + } + + [Test] + public void Migrate_WithMigrationScript_MigratesXmlDocument() + { + // Setup + string xslt = File.ReadAllText(Path.Combine(testDirectoryPath, "MigrationScript.xslt")); + XDocument xmlDocument = GetDefaultXDocument(); + + // Call + XDocument migratedXDocument = CalculationConfigurationMigrator.Migrate(xmlDocument, xslt); + + // Assert + XDocument expectedXmlDocument = GetDefaultXDocument(); + var attribute = new XAttribute("test", true); + expectedXmlDocument.Root.Add(attribute); + var newNode = new XElement("newNode"); + expectedXmlDocument.Root.Add(newNode); + + Assert.IsTrue(XNode.DeepEquals(expectedXmlDocument, migratedXDocument)); + } + + private static XDocument GetDefaultXDocument() + { + var xDocument = new XDocument(); + var root = new XElement("root"); + xDocument.Add(root); + return xDocument; + } + } +} \ No newline at end of file Index: Riskeer/Common/test/Riskeer.Common.IO.Test/Configurations/Import/CalculationConfigurationReaderTest.cs =================================================================== diff -u -rde0a6dcc11a22e488cc61fbebc2dfee1b6eef2c5 -r09f8bc173387fe5ca6b8735595d5a4b485623585 --- Riskeer/Common/test/Riskeer.Common.IO.Test/Configurations/Import/CalculationConfigurationReaderTest.cs (.../CalculationConfigurationReaderTest.cs) (revision de0a6dcc11a22e488cc61fbebc2dfee1b6eef2c5) +++ Riskeer/Common/test/Riskeer.Common.IO.Test/Configurations/Import/CalculationConfigurationReaderTest.cs (.../CalculationConfigurationReaderTest.cs) (revision 09f8bc173387fe5ca6b8735595d5a4b485623585) @@ -40,7 +40,7 @@ private string validMainSchemaDefinition; private readonly string testDirectoryPath = TestHelper.GetTestDataPath(TestDataPath.Riskeer.Common.IO, - "CalculationConfigurationReader"); + nameof(CalculationConfigurationReader)); private static IEnumerable InvalidConfigurations { Index: Riskeer/Common/test/Riskeer.Common.IO.Test/test-data/CalculationConfigurationMigrator/EmptyMigrationScript.xslt =================================================================== diff -u --- Riskeer/Common/test/Riskeer.Common.IO.Test/test-data/CalculationConfigurationMigrator/EmptyMigrationScript.xslt (revision 0) +++ Riskeer/Common/test/Riskeer.Common.IO.Test/test-data/CalculationConfigurationMigrator/EmptyMigrationScript.xslt (revision 09f8bc173387fe5ca6b8735595d5a4b485623585) @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file Index: Riskeer/Common/test/Riskeer.Common.IO.Test/test-data/CalculationConfigurationMigrator/MigrationScript.xslt =================================================================== diff -u --- Riskeer/Common/test/Riskeer.Common.IO.Test/test-data/CalculationConfigurationMigrator/MigrationScript.xslt (revision 0) +++ Riskeer/Common/test/Riskeer.Common.IO.Test/test-data/CalculationConfigurationMigrator/MigrationScript.xslt (revision 09f8bc173387fe5ca6b8735595d5a4b485623585) @@ -0,0 +1,18 @@ + + + + + + + + + + + + + true + + + + + \ No newline at end of file