Fisheye: Tag 82d0d520e61fceff9d0acacd0e46f07048239a6e refers to a dead (removed) revision in file `Ringtoets/Piping/src/Ringtoets.Piping.IO/Exporters/CalculationGroupWriter.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/Exporters/PipingConfigurationWriter.cs =================================================================== diff -u --- Ringtoets/Piping/src/Ringtoets.Piping.IO/Exporters/PipingConfigurationWriter.cs (revision 0) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/Exporters/PipingConfigurationWriter.cs (revision 82d0d520e61fceff9d0acacd0e46f07048239a6e) @@ -0,0 +1,168 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets 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.Globalization; +using System.Xml; +using Core.Common.Base.Data; +using Core.Common.IO.Exceptions; +using Ringtoets.Common.Data.Calculation; +using Ringtoets.Common.Data.Probabilistics; +using Ringtoets.Piping.Data; +using CoreCommonUtilsResources = Core.Common.Utils.Properties.Resources; + +namespace Ringtoets.Piping.IO.Exporters +{ + /// + /// Xml file writer for writing objects to *.xml file. + /// + internal static class PipingConfigurationWriter + { + /// + /// Writes the calculation group to a xml file. + /// + /// The root calculation group to be written to the file. + /// The path to the file. + /// Thrown when any parameter is null. + /// Thrown when unable to write to . + internal static void Write(CalculationGroup rootCalculationGroup, string filePath) + { + if (rootCalculationGroup == null) + { + throw new ArgumentNullException(nameof(rootCalculationGroup)); + } + if (filePath == null) + { + throw new ArgumentNullException(nameof(filePath)); + } + + try + { + var settings = new XmlWriterSettings + { + Indent = true, + IndentChars = "\t" + }; + + using (XmlWriter writer = XmlWriter.Create(filePath, settings)) + { + writer.WriteStartDocument(); + writer.WriteStartElement("root"); + + WriteConfiguration(rootCalculationGroup, writer); + + writer.WriteEndElement(); + writer.WriteEndDocument(); + } + } + catch (SystemException e) + { + throw new CriticalFileWriteException(string.Format(CoreCommonUtilsResources.Error_General_output_error_0, filePath), e); + } + } + + private static void WriteConfiguration(CalculationGroup calculationGroup, XmlWriter writer) + { + foreach (ICalculationBase child in calculationGroup.Children) + { + var innerGroup = child as CalculationGroup; + if (innerGroup != null) + { + WriteCalculationGroup(innerGroup, writer); + } + + var calculation = child as PipingCalculation; + if (calculation != null) + { + WriteCalculation(calculation, writer); + } + } + } + + private static void WriteCalculationGroup(CalculationGroup calculationGroup, XmlWriter writer) + { + writer.WriteStartElement("folder"); + writer.WriteAttributeString("naam", calculationGroup.Name); + + WriteConfiguration(calculationGroup, writer); + + writer.WriteEndElement(); + } + + private static void WriteCalculation(PipingCalculation calculation, XmlWriter writer) + { + writer.WriteStartElement("berekening"); + writer.WriteAttributeString("naam", calculation.Name); + + PipingInput calculationInputParameters = calculation.InputParameters; + + if (calculationInputParameters.UseAssessmentLevelManualInput) + { + writer.WriteElementString("toetspeil", ToStringInvariantCulture(calculationInputParameters.AssessmentLevel)); + } + else + { + if (calculationInputParameters.HydraulicBoundaryLocation != null) + { + writer.WriteElementString("hrlocatie", calculationInputParameters.HydraulicBoundaryLocation.Name); + } + } + + if (calculationInputParameters.SurfaceLine != null) + { + writer.WriteElementString("profielschematisatie", calculationInputParameters.SurfaceLine.Name); + writer.WriteElementString("intredepunt", ToStringInvariantCulture(calculationInputParameters.EntryPointL)); + writer.WriteElementString("uittredepunt", ToStringInvariantCulture(calculationInputParameters.ExitPointL)); + } + + if (calculationInputParameters.StochasticSoilModel != null) + { + writer.WriteElementString("ondergrondmodel", calculationInputParameters.StochasticSoilModel.Name); + + if (calculationInputParameters.StochasticSoilProfile?.SoilProfile != null) + { + writer.WriteElementString("ondergrondschematisatie", calculationInputParameters.StochasticSoilProfile.SoilProfile.Name); + } + } + + WriteDistribution(calculationInputParameters.PhreaticLevelExit, "polderpeil", writer); + WriteDistribution(calculationInputParameters.DampingFactorExit, "dempingsfactor", writer); + + writer.WriteEndElement(); + } + + private static void WriteDistribution(IDistribution distribution, string elementName, XmlWriter writer) + { + writer.WriteStartElement("stochast"); + writer.WriteAttributeString("naam", elementName); + + writer.WriteElementString("verwachtingswaarde", ToStringInvariantCulture(distribution.Mean)); + writer.WriteElementString("standaardafwijking", ToStringInvariantCulture(distribution.StandardDeviation)); + + writer.WriteEndElement(); + } + + private static string ToStringInvariantCulture(RoundedDouble roundedDouble) + { + return roundedDouble.ToString(null, CultureInfo.InvariantCulture); + } + } +} \ No newline at end of file Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/Ringtoets.Piping.IO.csproj =================================================================== diff -u -r9b940da26da12706c2b6665959e6de7f1a3dbc52 -r82d0d520e61fceff9d0acacd0e46f07048239a6e --- Ringtoets/Piping/src/Ringtoets.Piping.IO/Ringtoets.Piping.IO.csproj (.../Ringtoets.Piping.IO.csproj) (revision 9b940da26da12706c2b6665959e6de7f1a3dbc52) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/Ringtoets.Piping.IO.csproj (.../Ringtoets.Piping.IO.csproj) (revision 82d0d520e61fceff9d0acacd0e46f07048239a6e) @@ -52,7 +52,7 @@ - + Fisheye: Tag 82d0d520e61fceff9d0acacd0e46f07048239a6e refers to a dead (removed) revision in file `Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Exporters/CalculationGroupWriterTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Exporters/PipingConfigurationWriterTest.cs =================================================================== diff -u --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Exporters/PipingConfigurationWriterTest.cs (revision 0) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Exporters/PipingConfigurationWriterTest.cs (revision 82d0d520e61fceff9d0acacd0e46f07048239a6e) @@ -0,0 +1,251 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets 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.Collections.Generic; +using System.IO; +using System.Security.AccessControl; +using Core.Common.Base.Data; +using Core.Common.IO.Exceptions; +using Core.Common.TestUtil; +using NUnit.Framework; +using Ringtoets.Common.Data.Calculation; +using Ringtoets.Common.Data.Hydraulics; +using Ringtoets.Piping.Data; +using Ringtoets.Piping.Integration.TestUtils; +using Ringtoets.Piping.IO.Exporters; +using Ringtoets.Piping.Primitives; + +namespace Ringtoets.Piping.IO.Test.Exporters +{ + [TestFixture] + public class PipingConfigurationWriterTest + { + [Test] + public void Write_CalculationGroupNull_ThrowArgumentNullException() + { + // Call + TestDelegate test = () => PipingConfigurationWriter.Write(null, string.Empty); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("rootCalculationGroup", exception.ParamName); + } + + [Test] + public void Write_FilePathNull_ThrowArgumentNullException() + { + // Call + TestDelegate test = () => PipingConfigurationWriter.Write(new CalculationGroup(), null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("filePath", exception.ParamName); + } + + [Test] + [TestCase("")] + [TestCase(" ")] + [TestCase("c:\\>")] + public void Write_FilePathInvalid_ThrowCriticalFileWriteException(string filePath) + { + // Call + TestDelegate call = () => PipingConfigurationWriter.Write(new CalculationGroup(), filePath); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual($"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.", exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } + + [Test] + public void Write_FilePathTooLong_ThrowCriticalFileWriteException() + { + // Setup + var filePath = new string('a', 249); + + // Call + TestDelegate call = () => PipingConfigurationWriter.Write(new CalculationGroup(), filePath); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual($"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.", exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } + + [Test] + public void Write_InvalidDirectoryRights_ThrowCriticalFileWriteException() + { + // Setup + string directoryPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Piping.IO, + "Write_InvalidDirectoryRights_ThrowCriticalFileWriteException"); + Directory.CreateDirectory(directoryPath); + string filePath = Path.Combine(directoryPath, "test.xml"); + + // Call + TestDelegate call = () => PipingConfigurationWriter.Write(new CalculationGroup(), filePath); + + try + { + using (new DirectoryPermissionsRevoker(directoryPath, FileSystemRights.Write)) + { + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual($"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.", exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } + } + finally + { + Directory.Delete(directoryPath, true); + } + } + + [Test] + public void Write_CalculationGroupsAndCalculation_ValidFile() + { + // Setup + string directoryPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Piping.IO, + "PipingConfigurationWriter"); + Directory.CreateDirectory(directoryPath); + string filePath = Path.Combine(directoryPath, "test.xml"); + + var calculation = PipingTestDataGenerator.GetPipingCalculation(); + calculation.InputParameters.ExitPointL = (RoundedDouble) 0.2; + + var calculation2 = PipingTestDataGenerator.GetPipingCalculation(); + calculation2.Name = "PK001_0002 W1-6_4_1D1"; + calculation2.InputParameters.HydraulicBoundaryLocation = new HydraulicBoundaryLocation(1, "PUNT_SCH_17", 0, 0); + calculation2.InputParameters.SurfaceLine.Name = "PK001_0002"; + calculation2.InputParameters.ExitPointL = (RoundedDouble) 0.2; + calculation2.InputParameters.StochasticSoilModel = new StochasticSoilModel(1, "PK001_0002_Piping", string.Empty); + calculation2.InputParameters.StochasticSoilProfile = new StochasticSoilProfile(0, SoilProfileType.SoilProfile1D, 0) + { + SoilProfile = new PipingSoilProfile("W1-6_4_1D1", 0, new [] + { + new PipingSoilLayer(0) + }, SoilProfileType.SoilProfile1D, 0) + }; + + var calculationGroup2 = new CalculationGroup("PK001_0002", false) + { + Children = + { + calculation2 + } + }; + + var calculationGroup = new CalculationGroup("PK001_0001", false) + { + Children = + { + calculation, + calculationGroup2 + } + }; + + var rootGroup = new CalculationGroup("root", false) + { + Children = + { + calculationGroup + } + }; + + try + { + // Call + PipingConfigurationWriter.Write(rootGroup, filePath); + + // Assert + Assert.IsTrue(File.Exists(filePath)); + + var actualXml = File.ReadAllText(filePath); + var expectedXml = File.ReadAllText(Path.Combine(directoryPath, "folderWithSubfolderAndCalculation.xml")); + + + Assert.AreEqual(expectedXml, actualXml); + } + finally + { + File.Delete(filePath); + } + } + + [Test] + [TestCaseSource(nameof(Calculations))] + public void Write_ValidCalculationCalculation_ValidFile(string expectedFileName, PipingCalculation calculation) + { + // Setup + string directoryPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Piping.IO, + "PipingConfigurationWriter"); + Directory.CreateDirectory(directoryPath); + string filePath = Path.Combine(directoryPath, "test.xml"); + + var rootCalculationGroup = new CalculationGroup("group", false); + rootCalculationGroup.Children.Add(calculation); + + try + { + // Call + PipingConfigurationWriter.Write(rootCalculationGroup, filePath); + + // Assert + Assert.IsTrue(File.Exists(filePath)); + + var actualXml = File.ReadAllText(filePath); + var expectedXml = File.ReadAllText(Path.Combine(directoryPath, $"{expectedFileName}.xml")); + + Assert.AreEqual(expectedXml, actualXml); + } + finally + { + File.Delete(filePath); + } + } + + private static IEnumerable Calculations + { + get + { + yield return new TestCaseData("calculationWithoutHydraulicLocation", + PipingTestDataGenerator.GetPipingCalculationWithoutHydraulicLocationAndAssessmentLevel()) + .SetName("calculationWithoutHydraulicLocation"); + + yield return new TestCaseData("calculationWithAssessmentLevel", + PipingTestDataGenerator.GetPipingCalculationWithAssessmentLevel()) + .SetName("calculationWithAssessmentLevel"); + + yield return new TestCaseData("calculationWithoutSurfaceLine", + PipingTestDataGenerator.GetPipingCalculationWithoutSurfaceLine()) + .SetName("calculationWithoutSurfaceLine"); + + yield return new TestCaseData("calculationWithoutSoilModel", + PipingTestDataGenerator.GetPipingCalculationWithoutSoilModel()) + .SetName("calculationWithoutSoilModel"); + + yield return new TestCaseData("calculationWithoutSoilProfile", + PipingTestDataGenerator.GetPipingCalculationWithoutSoilProfile()) + .SetName("calculationWithoutSoilProfile"); + } + } + } +} \ No newline at end of file Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Ringtoets.Piping.IO.Test.csproj =================================================================== diff -u -r9b940da26da12706c2b6665959e6de7f1a3dbc52 -r82d0d520e61fceff9d0acacd0e46f07048239a6e --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Ringtoets.Piping.IO.Test.csproj (.../Ringtoets.Piping.IO.Test.csproj) (revision 9b940da26da12706c2b6665959e6de7f1a3dbc52) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Ringtoets.Piping.IO.Test.csproj (.../Ringtoets.Piping.IO.Test.csproj) (revision 82d0d520e61fceff9d0acacd0e46f07048239a6e) @@ -72,7 +72,7 @@ - + Fisheye: Tag 82d0d520e61fceff9d0acacd0e46f07048239a6e refers to a dead (removed) revision in file `Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/CalculationGroupWriter/calculationWithAssessmentLevel.xml'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 82d0d520e61fceff9d0acacd0e46f07048239a6e refers to a dead (removed) revision in file `Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/CalculationGroupWriter/calculationWithoutHydraulicLocation.xml'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 82d0d520e61fceff9d0acacd0e46f07048239a6e refers to a dead (removed) revision in file `Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/CalculationGroupWriter/calculationWithoutSoilProfile.xml'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 82d0d520e61fceff9d0acacd0e46f07048239a6e refers to a dead (removed) revision in file `Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/CalculationGroupWriter/calculationWithoutSoilmodel.xml'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 82d0d520e61fceff9d0acacd0e46f07048239a6e refers to a dead (removed) revision in file `Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/CalculationGroupWriter/calculationWithoutSurfaceline.xml'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 82d0d520e61fceff9d0acacd0e46f07048239a6e refers to a dead (removed) revision in file `Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/CalculationGroupWriter/folderWithSubfolderAndCalculation.xml'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 82d0d520e61fceff9d0acacd0e46f07048239a6e refers to a dead (removed) revision in file `Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/CalculationGroupWriter/validation.xsd'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithAssessmentLevel.xml =================================================================== diff -u --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithAssessmentLevel.xml (revision 0) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithAssessmentLevel.xml (revision 82d0d520e61fceff9d0acacd0e46f07048239a6e) @@ -0,0 +1,19 @@ + + + + 3.00 + PK001_0001 + 0.00 + 10.00 + PK001_0001_Piping + W1-6_0_1D1 + + 0.000 + 0.100 + + + 0.700 + 0.100 + + + \ No newline at end of file Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithoutHydraulicLocation.xml =================================================================== diff -u --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithoutHydraulicLocation.xml (revision 0) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithoutHydraulicLocation.xml (revision 82d0d520e61fceff9d0acacd0e46f07048239a6e) @@ -0,0 +1,18 @@ + + + + PK001_0001 + 0.00 + 10.00 + PK001_0001_Piping + W1-6_0_1D1 + + 0.000 + 0.100 + + + 0.700 + 0.100 + + + \ No newline at end of file Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithoutSoilProfile.xml =================================================================== diff -u --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithoutSoilProfile.xml (revision 0) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithoutSoilProfile.xml (revision 82d0d520e61fceff9d0acacd0e46f07048239a6e) @@ -0,0 +1,18 @@ + + + + PUNT_KAT_18 + PK001_0001 + 0.00 + 10.00 + PK001_0001_Piping + + 0.000 + 0.100 + + + 0.700 + 0.100 + + + \ No newline at end of file Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithoutSoilmodel.xml =================================================================== diff -u --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithoutSoilmodel.xml (revision 0) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithoutSoilmodel.xml (revision 82d0d520e61fceff9d0acacd0e46f07048239a6e) @@ -0,0 +1,17 @@ + + + + PUNT_KAT_18 + PK001_0001 + 0.00 + 10.00 + + 0.000 + 0.100 + + + 0.700 + 0.100 + + + \ No newline at end of file Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithoutSurfaceline.xml =================================================================== diff -u --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithoutSurfaceline.xml (revision 0) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/calculationWithoutSurfaceline.xml (revision 82d0d520e61fceff9d0acacd0e46f07048239a6e) @@ -0,0 +1,16 @@ + + + + PUNT_KAT_18 + PK001_0001_Piping + W1-6_0_1D1 + + 0.000 + 0.100 + + + 0.700 + 0.100 + + + \ No newline at end of file Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/folderWithSubfolderAndCalculation.xml =================================================================== diff -u --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/folderWithSubfolderAndCalculation.xml (revision 0) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/PipingConfigurationWriter/folderWithSubfolderAndCalculation.xml (revision 82d0d520e61fceff9d0acacd0e46f07048239a6e) @@ -0,0 +1,39 @@ + + + + + PUNT_KAT_18 + PK001_0001 + 0.00 + 0.20 + PK001_0001_Piping + W1-6_0_1D1 + + 0.000 + 0.100 + + + 0.700 + 0.100 + + + + + PUNT_SCH_17 + PK001_0002 + 0.00 + 0.20 + PK001_0002_Piping + W1-6_4_1D1 + + 0.000 + 0.100 + + + 0.700 + 0.100 + + + + + \ No newline at end of file