Index: Ringtoets/Common/src/Ringtoets.Common.IO/Readers/ConfigurationReaderHelper.cs =================================================================== diff -u --- Ringtoets/Common/src/Ringtoets.Common.IO/Readers/ConfigurationReaderHelper.cs (revision 0) +++ Ringtoets/Common/src/Ringtoets.Common.IO/Readers/ConfigurationReaderHelper.cs (revision 78e404373f030f9fef45733c2f6c560d6ea58b21) @@ -0,0 +1,89 @@ +// 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.Linq; +using System.Xml; +using System.Xml.Linq; + +namespace Ringtoets.Common.IO.Readers +{ + /// + /// Helper methods related to instances. + /// + public static class ConfigurationReaderHelper + { + /// + /// Gets the double value from a child element. + /// + /// The that contains the child element. + /// The name of the child element. + /// The value of the element, or null when the + /// does not have child elements of . + /// Thrown when any parameter is null. + public static double? GetDoubleValueFromChildElement(XElement parentElement, string childElementName) + { + if (parentElement == null) + { + throw new ArgumentNullException(nameof(parentElement)); + } + if (childElementName == null) + { + throw new ArgumentNullException(nameof(childElementName)); + } + + XElement childElement = GetChildElement(parentElement, childElementName); + + return childElement != null + ? (double?) XmlConvert.ToDouble(childElement.Value) + : null; + } + + /// + /// Gets the string value from a child element. + /// + /// The that contains the child element. + /// The name of the child element. + /// The value of the element, or null when the + /// does not have child elements of . + /// Thrown when any parameter is null. + public static string GetStringValueFromChildElement(XElement parentElement, string childElementName) + { + if (parentElement == null) + { + throw new ArgumentNullException(nameof(parentElement)); + } + if (childElementName == null) + { + throw new ArgumentNullException(nameof(childElementName)); + } + + XElement childElement = GetChildElement(parentElement, childElementName); + + return childElement?.Value; + } + + private static XElement GetChildElement(XElement parentElement, string childElementName) + { + return parentElement.Elements(childElementName).FirstOrDefault(); + } + } +} \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.IO/Ringtoets.Common.IO.csproj =================================================================== diff -u -rb84f4b9c37b5ce0bd49b65269c4984c0bc1543e8 -r78e404373f030f9fef45733c2f6c560d6ea58b21 --- Ringtoets/Common/src/Ringtoets.Common.IO/Ringtoets.Common.IO.csproj (.../Ringtoets.Common.IO.csproj) (revision b84f4b9c37b5ce0bd49b65269c4984c0bc1543e8) +++ Ringtoets/Common/src/Ringtoets.Common.IO/Ringtoets.Common.IO.csproj (.../Ringtoets.Common.IO.csproj) (revision 78e404373f030f9fef45733c2f6c560d6ea58b21) @@ -83,6 +83,7 @@ + Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/ConfigurationReaderHelperTest.cs =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/ConfigurationReaderHelperTest.cs (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/ConfigurationReaderHelperTest.cs (revision 78e404373f030f9fef45733c2f6c560d6ea58b21) @@ -0,0 +1,134 @@ +// 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.Xml.Linq; +using NUnit.Framework; +using Ringtoets.Common.IO.Readers; + +namespace Ringtoets.Common.IO.Test.Readers +{ + [TestFixture] + public class ConfigurationReaderHelperTest + { + [Test] + public void GetDoubleValueFromChildElement_ParentElementNull_ThrowArgumentNullException() + { + // Call + TestDelegate test = () => ConfigurationReaderHelper.GetDoubleValueFromChildElement(null, ""); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("parentElement", exception.ParamName); + } + + [Test] + public void GetDoubleValueFromChildElement_ChildElementNameNull_ThrowArgumentNullException() + { + // Call + TestDelegate test = () => ConfigurationReaderHelper.GetDoubleValueFromChildElement(new XElement("Root"), null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("childElementName", exception.ParamName); + } + + [Test] + public void GetDoubleValueFromChildElement_ValidChildElement_ReturnValue() + { + // Setup + const string childElementName = "number"; + const double childElementValue = 3; + + var element = new XElement("Root", new XElement(childElementName, childElementValue)); + + // Call + double? readValue = ConfigurationReaderHelper.GetDoubleValueFromChildElement(element, childElementName); + + // Assert + Assert.AreEqual(childElementValue, readValue.Value); + } + + [Test] + public void GetDoubleValueFromChildElement_InvalidChildElement_ReturnNull() + { + // Setup + var element = new XElement("Root", new XElement("number", (double) 3)); + + // Call + double? readValue = ConfigurationReaderHelper.GetDoubleValueFromChildElement(element, "invalidName"); + + // Assert + Assert.IsNull(readValue); + } + + [Test] + public void GetStringValueFromChildElement_ParentElementNull_ThrowArgumentNullException() + { + // Call + TestDelegate test = () => ConfigurationReaderHelper.GetStringValueFromChildElement(null, ""); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("parentElement", exception.ParamName); + } + + [Test] + public void GetStringValueFromChildElement_ChildElementNameNull_ThrowArgumentNullException() + { + // Call + TestDelegate test = () => ConfigurationReaderHelper.GetStringValueFromChildElement(new XElement("Test"), null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("childElementName", exception.ParamName); + } + + [Test] + public void GetStringValueFromChildElement_ValidChildElement_ReturnValue() + { + // Setup + const string childElementName = "text"; + const string childElementValue = "valueText"; + + var element = new XElement("Root", new XElement(childElementName, childElementValue)); + + // Call + string readValue = ConfigurationReaderHelper.GetStringValueFromChildElement(element, childElementName); + + // Assert + Assert.AreEqual(childElementValue, readValue); + } + + [Test] + public void GetStringValueFromChildElement_InvalidChildElement_ReturnNull() + { + // Setup + var element = new XElement("Root", new XElement("number", "valueText")); + + // Call + string readValue = ConfigurationReaderHelper.GetStringValueFromChildElement(element, "invalidName"); + + // Assert + Assert.IsNull(readValue); + } + } +} \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/Ringtoets.Common.IO.Test.csproj =================================================================== diff -u -rfc204f9f1958c10c27c00e43fc2dba43565f7b35 -r78e404373f030f9fef45733c2f6c560d6ea58b21 --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/Ringtoets.Common.IO.Test.csproj (.../Ringtoets.Common.IO.Test.csproj) (revision fc204f9f1958c10c27c00e43fc2dba43565f7b35) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/Ringtoets.Common.IO.Test.csproj (.../Ringtoets.Common.IO.Test.csproj) (revision 78e404373f030f9fef45733c2f6c560d6ea58b21) @@ -84,6 +84,7 @@ + Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/Readers/PipingConfigurationReader.cs =================================================================== diff -u -ra30456917268fbd9a24f24c3695b15389d4ca092 -r78e404373f030f9fef45733c2f6c560d6ea58b21 --- Ringtoets/Piping/src/Ringtoets.Piping.IO/Readers/PipingConfigurationReader.cs (.../PipingConfigurationReader.cs) (revision a30456917268fbd9a24f24c3695b15389d4ca092) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/Readers/PipingConfigurationReader.cs (.../PipingConfigurationReader.cs) (revision 78e404373f030f9fef45733c2f6c560d6ea58b21) @@ -22,7 +22,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Xml; using System.Xml.Linq; using Core.Common.IO.Exceptions; using Ringtoets.Common.IO.Readers; @@ -73,59 +72,43 @@ var constructionProperties = new ReadPipingCalculation.ConstructionProperties { Name = calculationElement.Attribute(ConfigurationSchemaIdentifiers.NameAttribute)?.Value, - AssessmentLevel = GetDoubleValueFromChildElement(calculationElement, - PipingConfigurationSchemaIdentifiers.AssessmentLevelElement), - HydraulicBoundaryLocation = GetStringValueFromChildElement(calculationElement, - ConfigurationSchemaIdentifiers.HydraulicBoundaryLocationElement), - SurfaceLine = GetStringValueFromChildElement(calculationElement, - PipingConfigurationSchemaIdentifiers.SurfaceLineElement), - EntryPointL = GetDoubleValueFromChildElement(calculationElement, - PipingConfigurationSchemaIdentifiers.EntryPointLElement), - ExitPointL = GetDoubleValueFromChildElement(calculationElement, - PipingConfigurationSchemaIdentifiers.ExitPointLElement), - StochasticSoilModel = GetStringValueFromChildElement(calculationElement, - PipingConfigurationSchemaIdentifiers.StochasticSoilModelElement), - StochasticSoilProfile = GetStringValueFromChildElement(calculationElement, - PipingConfigurationSchemaIdentifiers.StochasticSoilProfileElement) + AssessmentLevel = ConfigurationReaderHelper.GetDoubleValueFromChildElement(calculationElement, + PipingConfigurationSchemaIdentifiers.AssessmentLevelElement), + HydraulicBoundaryLocation = ConfigurationReaderHelper.GetStringValueFromChildElement(calculationElement, + ConfigurationSchemaIdentifiers.HydraulicBoundaryLocationElement), + SurfaceLine = ConfigurationReaderHelper.GetStringValueFromChildElement(calculationElement, + PipingConfigurationSchemaIdentifiers.SurfaceLineElement), + EntryPointL = ConfigurationReaderHelper.GetDoubleValueFromChildElement(calculationElement, + PipingConfigurationSchemaIdentifiers.EntryPointLElement), + ExitPointL = ConfigurationReaderHelper.GetDoubleValueFromChildElement(calculationElement, + PipingConfigurationSchemaIdentifiers.ExitPointLElement), + StochasticSoilModel = ConfigurationReaderHelper.GetStringValueFromChildElement(calculationElement, + PipingConfigurationSchemaIdentifiers.StochasticSoilModelElement), + StochasticSoilProfile = ConfigurationReaderHelper.GetStringValueFromChildElement(calculationElement, + PipingConfigurationSchemaIdentifiers.StochasticSoilProfileElement) }; XElement phreaticLevelExitElement = GetStochastChildElement(calculationElement, PipingConfigurationSchemaIdentifiers.PhreaticLevelExitStochastName); if (phreaticLevelExitElement != null) { - constructionProperties.PhreaticLevelExitMean = GetDoubleValueFromChildElement(phreaticLevelExitElement, - ConfigurationSchemaIdentifiers.MeanElement); - constructionProperties.PhreaticLevelExitStandardDeviation = GetDoubleValueFromChildElement(phreaticLevelExitElement, - ConfigurationSchemaIdentifiers.StandardDeviationElement); + constructionProperties.PhreaticLevelExitMean = ConfigurationReaderHelper.GetDoubleValueFromChildElement(phreaticLevelExitElement, + ConfigurationSchemaIdentifiers.MeanElement); + constructionProperties.PhreaticLevelExitStandardDeviation = ConfigurationReaderHelper.GetDoubleValueFromChildElement(phreaticLevelExitElement, + ConfigurationSchemaIdentifiers.StandardDeviationElement); } XElement dampingFactorExitElement = GetStochastChildElement(calculationElement, PipingConfigurationSchemaIdentifiers.DampingFactorExitStochastName); if (dampingFactorExitElement != null) { - constructionProperties.DampingFactorExitMean = GetDoubleValueFromChildElement(dampingFactorExitElement, - ConfigurationSchemaIdentifiers.MeanElement); - constructionProperties.DampingFactorExitStandardDeviation = GetDoubleValueFromChildElement(dampingFactorExitElement, - ConfigurationSchemaIdentifiers.StandardDeviationElement); + constructionProperties.DampingFactorExitMean = ConfigurationReaderHelper.GetDoubleValueFromChildElement(dampingFactorExitElement, + ConfigurationSchemaIdentifiers.MeanElement); + constructionProperties.DampingFactorExitStandardDeviation = ConfigurationReaderHelper.GetDoubleValueFromChildElement(dampingFactorExitElement, + ConfigurationSchemaIdentifiers.StandardDeviationElement); } return new ReadPipingCalculation(constructionProperties); } - private static double? GetDoubleValueFromChildElement(XElement parentElement, string childElementName) - { - XElement childElement = parentElement.Elements(childElementName).FirstOrDefault(); - - return childElement != null - ? (double?) XmlConvert.ToDouble(childElement.Value) - : null; - } - - private static string GetStringValueFromChildElement(XElement parentElement, string childElementName) - { - XElement childElement = parentElement.Elements(childElementName).FirstOrDefault(); - - return childElement?.Value; - } - private static XElement GetStochastChildElement(XElement parentElement, string stochastName) { return parentElement.Elements(ConfigurationSchemaIdentifiers.StochastsElement)