// 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.Linq; using System.Xml.Linq; using Core.Common.Base; using Core.Common.Base.Data; using Core.Common.Base.Geometry; using Core.Common.Base.IO; using Core.Common.TestUtil; using NUnit.Framework; using Ringtoets.Common.Data; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.DikeProfiles; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.Data.Probabilistics; using Ringtoets.Common.Data.TestUtil; using Ringtoets.Common.IO.Configurations; using Ringtoets.Common.IO.FileImporters; using Ringtoets.Common.IO.Readers; using Ringtoets.Common.IO.Schema; namespace Ringtoets.Common.IO.Test.FileImporters { [TestFixture] public class CalculationConfigurationImporterTest { private readonly string readerPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, nameof(CalculationConfigurationReader)); [Test] public void Constructor_ExpectedValues() { // Call var importer = new CalculationConfigurationImporter("", new CalculationGroup()); // Assert Assert.IsInstanceOf>(importer); } [Test] public void Import_FilePathIsDirectory_CancelImportWithErrorMessage() { // Setup string filePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, Path.DirectorySeparatorChar.ToString()); var importer = new CalculationConfigurationImporter(filePath, new CalculationGroup()); // Call var importSuccessful = true; Action call = () => importSuccessful = importer.Import(); // Assert string expectedMessage = $"Fout bij het lezen van bestand '{filePath}': bestandspad mag niet verwijzen naar een lege bestandsnaam. " + Environment.NewLine + "Er is geen berekeningenconfiguratie geïmporteerd."; TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); Assert.IsFalse(importSuccessful); } [Test] public void Import_FileDoesNotExist_CancelImportWithErrorMessage() { // Setup string filePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "I_dont_exist"); var importer = new CalculationConfigurationImporter(filePath, new CalculationGroup()); // Call var importSuccessful = true; Action call = () => importSuccessful = importer.Import(); // Assert string expectedMessage = $"Fout bij het lezen van bestand '{filePath}': het bestand bestaat niet. " + Environment.NewLine + "Er is geen berekeningenconfiguratie geïmporteerd."; TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); Assert.IsFalse(importSuccessful); } [Test] public void Import_InvalidFile_CancelImportWithErrorMessage() { // Setup string filePath = Path.Combine(readerPath, "invalidFolderNoName.xml"); var importer = new CalculationConfigurationImporter(filePath, new CalculationGroup()); // Call var importSuccessful = true; Action call = () => importSuccessful = importer.Import(); // Assert TestHelper.AssertLogMessages(call, messages => { string[] msgs = messages.ToArray(); Assert.AreEqual(1, msgs.Length); StringAssert.StartsWith($"Fout bij het lezen van bestand '{filePath}': het XML-document dat de configuratie voor de berekeningen beschrijft is niet geldig.", msgs[0]); }); Assert.IsFalse(importSuccessful); } [Test] [TestCase("Inlezen")] [TestCase("Valideren")] public void Import_CancelingImport_CancelImportAndLog(string expectedProgressMessage) { // Setup var calculationGroup = new CalculationGroup(); string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); importer.SetProgressChanged((description, step, steps) => { if (description.Contains(expectedProgressMessage)) { importer.Cancel(); } }); // Call var importSuccessful = true; Action call = () => importSuccessful = importer.Import(); // Assert TestHelper.AssertLogMessageIsGenerated(call, "Berekeningenconfiguratie importeren afgebroken. Geen data ingelezen.", 1); CollectionAssert.IsEmpty(calculationGroup.Children); Assert.IsFalse(importSuccessful); } [Test] public void GivenImport_WhenImporting_ThenExpectedProgressMessagesGenerated() { // Given string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var importer = new CalculationConfigurationImporter(filePath, new CalculationGroup()); var expectedProgressMessages = new[] { new ExpectedProgressNotification { Text = "Inlezen berekeningenconfiguratie.", CurrentStep = 1, TotalNumberOfSteps = 3 }, new ExpectedProgressNotification { Text = "Valideren berekeningenconfiguratie.", CurrentStep = 2, TotalNumberOfSteps = 3 }, new ExpectedProgressNotification { Text = "Geïmporteerde data toevoegen aan het toetsspoor.", CurrentStep = 3, TotalNumberOfSteps = 3 } }; var progressChangedCallCount = 0; importer.SetProgressChanged((description, step, steps) => { Assert.AreEqual(expectedProgressMessages[progressChangedCallCount].Text, description); Assert.AreEqual(expectedProgressMessages[progressChangedCallCount].CurrentStep, step); Assert.AreEqual(expectedProgressMessages[progressChangedCallCount].TotalNumberOfSteps, steps); progressChangedCallCount++; }); // When importer.Import(); // Then Assert.AreEqual(expectedProgressMessages.Length, progressChangedCallCount); } [Test] public void Import_ValidConfigurationWithValidData_DataAddedToModel() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); // Call bool successful = importer.Import(); // Assert Assert.IsTrue(successful); AssertCalculationGroup(GetExpectedNestedData(), calculationGroup); } [Test] [TestCase(true, true, 1.2, ConfigurationBreakWaterType.Wall, BreakWaterType.Wall)] [TestCase(false, false, 2.2, ConfigurationBreakWaterType.Caisson, BreakWaterType.Caisson)] [TestCase(false, true, 11.332, ConfigurationBreakWaterType.Wall, BreakWaterType.Wall)] [TestCase(true, false, 9.3, ConfigurationBreakWaterType.Dam, BreakWaterType.Dam)] public void ReadWaveReduction_DifferentScenarios_CorrectParametersSet(bool useForeshoreProfile, bool useBreakWater, double height, ConfigurationBreakWaterType type, BreakWaterType expectedType) { // Setup var testInput = new TestInputWithForeshoreProfileAndBreakWater(new BreakWater(BreakWaterType.Caisson, 0.0)); string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); var waveReductionConfiguration = new WaveReductionConfiguration { UseForeshoreProfile = useForeshoreProfile, UseBreakWater = useBreakWater, BreakWaterHeight = height, BreakWaterType = type }; // Call importer.PublicReadWaveReductionParameters(waveReductionConfiguration, testInput); // Assert Assert.AreEqual(testInput.UseForeshore, useForeshoreProfile); Assert.AreEqual(testInput.UseBreakWater, useBreakWater); Assert.AreEqual(testInput.BreakWater.Height, height, testInput.BreakWater.Height.GetAccuracy()); Assert.AreEqual(testInput.BreakWater.Type, expectedType); } [Test] public void ReadWaveReduction_WithoutConfiguration_ParametersUnchanged() { // Setup var random = new Random(21); bool useForeshoreProfile = random.NextBoolean(); bool useBreakWater = random.NextBoolean(); double height = random.NextDouble(); var breakWaterType = random.NextEnumValue(); var testInput = new TestInputWithForeshoreProfileAndBreakWater(new BreakWater(breakWaterType, height)) { UseBreakWater = useBreakWater, UseForeshore = useForeshoreProfile }; string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); // Call importer.PublicReadWaveReductionParameters(null, testInput); // Assert Assert.AreEqual(testInput.UseForeshore, useForeshoreProfile); Assert.AreEqual(testInput.UseBreakWater, useBreakWater); Assert.AreEqual(testInput.BreakWater.Height, height, testInput.BreakWater.Height.GetAccuracy()); Assert.AreEqual(testInput.BreakWater.Type, breakWaterType); } [Test] public void ReadWaveReduction_WithConfigurationWithMissingParameter_MissingParameterUnchanged([Values(0, 1, 2, 3)] int parameterNotSet) { // Setup const bool useForeshoreProfile = false; const bool useBreakWater = false; const double height = 2.55; const BreakWaterType breakWaterType = BreakWaterType.Dam; const bool newUseForeshoreProfile = true; const bool newUseBreakWater = true; const double newheight = 11.1; const ConfigurationBreakWaterType newBreakWaterType = ConfigurationBreakWaterType.Wall; const BreakWaterType expectedNewBreakWaterType = BreakWaterType.Wall; var testInput = new TestInputWithForeshoreProfileAndBreakWater(new BreakWater(breakWaterType, height)) { UseBreakWater = useBreakWater, UseForeshore = useForeshoreProfile }; var waveReductionConfiguration = new WaveReductionConfiguration(); if (parameterNotSet != 0) { waveReductionConfiguration.UseForeshoreProfile = newUseForeshoreProfile; } if (parameterNotSet != 1) { waveReductionConfiguration.UseBreakWater = newUseBreakWater; } if (parameterNotSet != 2) { waveReductionConfiguration.BreakWaterHeight = newheight; } if (parameterNotSet != 3) { waveReductionConfiguration.BreakWaterType = newBreakWaterType; } string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); // Call importer.PublicReadWaveReductionParameters(waveReductionConfiguration, testInput); // Assert Assert.AreEqual(testInput.UseForeshore, parameterNotSet == 0 ? useForeshoreProfile : newUseForeshoreProfile); Assert.AreEqual(testInput.UseBreakWater, parameterNotSet == 1 ? useBreakWater : newUseBreakWater); Assert.AreEqual(testInput.BreakWater.Height, parameterNotSet == 2 ? height : newheight, testInput.BreakWater.Height.GetAccuracy()); Assert.AreEqual(testInput.BreakWater.Type, parameterNotSet == 3 ? breakWaterType : expectedNewBreakWaterType); } [Test] public void TryReadHydraulicBoundaryLocation_NoCalculationName_ThrowsArgumentNullException() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); HydraulicBoundaryLocation location; // Call TestDelegate test = () => importer.PublicTryReadHydraulicBoundaryLocation(null, null, Enumerable.Empty(), out location); // Assert var exception = Assert.Throws(test); Assert.AreEqual("calculationName", exception.ParamName); } [Test] public void TryReadHydraulicBoundaryLocation_NoHydraulicBoundaryLocations_ThrowsArgumentNullException() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); HydraulicBoundaryLocation location; // Call TestDelegate test = () => importer.PublicTryReadHydraulicBoundaryLocation(null, "name", null, out location); // Assert var exception = Assert.Throws(test); Assert.AreEqual("hydraulicBoundaryLocations", exception.ParamName); } [Test] public void TryReadHydraulicBoundaryLocation_NoHydraulicBoundaryLocationToFindHydraulicBoundaryLocationsEmpty_ReturnsTrue() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); HydraulicBoundaryLocation location; // Call bool valid = importer.PublicTryReadHydraulicBoundaryLocation(null, "name", Enumerable.Empty(), out location); // Assert Assert.IsTrue(valid); Assert.IsNull(location); } [Test] public void TryReadHydraulicBoundaryLocation_WithHydraulicBoundaryLocationToFindHydraulicBoundaryLocationsEmpty_LogsErrorReturnsFalse() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); HydraulicBoundaryLocation location = null; var valid = true; const string locationName = "someName"; const string calculationName = "name"; // Call Action validate = () => valid = importer.PublicTryReadHydraulicBoundaryLocation(locationName, calculationName, Enumerable.Empty(), out location); // Assert string expectedMessage = $"De locatie met hydraulische randvoorwaarden '{locationName}' bestaat niet. Berekening '{calculationName}' is overgeslagen."; TestHelper.AssertLogMessageWithLevelIsGenerated(validate, Tuple.Create(expectedMessage, LogLevelConstant.Error)); Assert.IsFalse(valid); Assert.IsNull(location); } [Test] public void TryReadHydraulicBoundaryLocation_WithHydraulicBoundaryLocationToFindHydraulicBoundaryLocationsContainsLocation_ReturnsTrue() { // Setup const string locationName = "someName"; const string calculationName = "name"; string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); HydraulicBoundaryLocation expectedLocation = new TestHydraulicBoundaryLocation(locationName); HydraulicBoundaryLocation location; // Call bool valid = importer.PublicTryReadHydraulicBoundaryLocation(locationName, calculationName, new[] { new TestHydraulicBoundaryLocation("otherNameA"), expectedLocation, new TestHydraulicBoundaryLocation("otherNameB") }, out location); // Assert Assert.IsTrue(valid); Assert.AreSame(expectedLocation, location); } [Test] public void TryReadForeshoreProfile_NoCalculationName_ThrowsArgumentNullException() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); ForeshoreProfile profile; // Call TestDelegate test = () => importer.PublicTryReadForeshoreProfile(null, null, Enumerable.Empty(), out profile); // Assert var exception = Assert.Throws(test); Assert.AreEqual("calculationName", exception.ParamName); } [Test] public void TryReadForeshoreProfile_NoForeshoreProfiles_ThrowsArgumentNullException() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); ForeshoreProfile profile; // Call TestDelegate test = () => importer.PublicTryReadForeshoreProfile(null, "name", null, out profile); // Assert var exception = Assert.Throws(test); Assert.AreEqual("foreshoreProfiles", exception.ParamName); } [Test] public void TryReadForeshoreProfile_NoForeshoreProfileToFindForeshoreProfilesEmpty_ReturnsTrue() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); ForeshoreProfile profile; // Call bool valid = importer.PublicTryReadForeshoreProfile(null, "name", Enumerable.Empty(), out profile); // Assert Assert.IsTrue(valid); Assert.IsNull(profile); } [Test] public void TryReadForeshoreProfile_WithForeshoreProfileToFindForeshoreProfilesEmpty_LogsErrorReturnsFalse() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); ForeshoreProfile profile = null; var valid = true; const string profileName = "someName"; const string calculationName = "name"; // Call Action validate = () => valid = importer.PublicTryReadForeshoreProfile(profileName, calculationName, Enumerable.Empty(), out profile); // Assert string expectedMessage = $"Het voorlandprofiel '{profileName}' bestaat niet. Berekening '{calculationName}' is overgeslagen."; TestHelper.AssertLogMessageWithLevelIsGenerated(validate, Tuple.Create(expectedMessage, LogLevelConstant.Error)); Assert.IsFalse(valid); Assert.IsNull(profile); } [Test] public void TryReadForeshoreProfile_WithForeshoreProfileToFindForeshoreProfilesContainsProfile_ReturnsTrue() { // Setup const string profileName = "someName"; const string calculationName = "name"; string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); ForeshoreProfile expectedProfile = new TestForeshoreProfile(profileName); ForeshoreProfile profile; // Call bool valid = importer.PublicTryReadForeshoreProfile(profileName, calculationName, new[] { new TestForeshoreProfile("otherNameA"), expectedProfile, new TestForeshoreProfile("otherNameB") }, out profile); // Assert Assert.IsTrue(valid); Assert.AreSame(expectedProfile, profile); } [Test] public void TryReadStructure_NoCalculationName_ThrowsArgumentNullException() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); StructureBase structure; // Call TestDelegate test = () => importer.PublicTryReadStructure(null, null, Enumerable.Empty(), out structure); // Assert var exception = Assert.Throws(test); Assert.AreEqual("calculationName", exception.ParamName); } [Test] public void TryReadStructure_NoStructures_ThrowsArgumentNullException() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); StructureBase structure; // Call TestDelegate test = () => importer.PublicTryReadStructure(null, "name", null, out structure); // Assert var exception = Assert.Throws(test); Assert.AreEqual("structures", exception.ParamName); } [Test] public void TryReadStructure_NoStructureToFindStructuresEmpty_ReturnsTrue() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); StructureBase structure; // Call bool valid = importer.PublicTryReadStructure(null, "name", Enumerable.Empty(), out structure); // Assert Assert.IsTrue(valid); Assert.IsNull(structure); } [Test] public void TryReadStructure_WithStructureToFindStructuresEmpty_LogsErrorReturnsFalse() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); StructureBase profile = null; var valid = true; const string structureName = "someName"; const string calculationName = "name"; // Call Action validate = () => valid = importer.PublicTryReadStructure(structureName, calculationName, Enumerable.Empty(), out profile); // Assert string expectedMessage = $"Het kunstwerk '{structureName}' bestaat niet. Berekening '{calculationName}' is overgeslagen."; TestHelper.AssertLogMessageWithLevelIsGenerated(validate, Tuple.Create(expectedMessage, LogLevelConstant.Error)); Assert.IsFalse(valid); Assert.IsNull(profile); } [Test] public void TryReadStructure_WithStructureToFindStructuresContainsStructure_ReturnsTrue() { // Setup const string structureName = "someName"; const string calculationName = "name"; string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); var expectedProfile = new TestStructure(structureName); StructureBase structure; // Call bool valid = importer.PublicTryReadStructure(structureName, calculationName, new[] { new TestStructure("otherNameA"), expectedProfile, new TestStructure("otherNameB") }, out structure); // Assert Assert.IsTrue(valid); Assert.AreSame(expectedProfile, structure); } [Test] [TestCase(true, false)] [TestCase(false, true)] [TestCase(true, true)] [TestCase(false, false)] public void TryReadStandardDeviationStochast_ValidStochastConfiguration_ReturnsTrueParametersSet(bool setMean, bool setStandardDeviation) { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); var configuration = new StochastConfiguration(); var random = new Random(21); double mean = random.NextDouble(); double standardDeviation = random.NextDouble(); if (setMean) { configuration.Mean = mean; } if (setStandardDeviation) { configuration.StandardDeviation = standardDeviation; } var input = new TestInputWithStochasts(); // Call var valid = importer.PublicTryReadStandardDeviationStochast( "some stochast name", "some calculation name", input, configuration, i => i.Distribution, (i, s) => i.Distribution = s); // Assert Assert.IsTrue(valid); var defaultLogNormal = new LogNormalDistribution(); Assert.AreEqual( setMean ? mean : defaultLogNormal.Mean, input.Distribution.Mean, input.Distribution.Mean.GetAccuracy()); Assert.AreEqual( setStandardDeviation ? standardDeviation : defaultLogNormal.StandardDeviation, input.Distribution.StandardDeviation, input.Distribution.StandardDeviation.GetAccuracy()); } [Test] public void TryReadStandardDeviationStochast_StochastConfigurationWithStandardDeviation_LogsErrorReturnsFalse() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); var configuration = new StochastConfiguration { VariationCoefficient = new Random(21).NextDouble() }; var input = new TestInputWithStochasts(); var valid = true; const string stochastName = "some stochast name"; const string calculationName = "some calculation name"; // Call Action validate = () => valid = importer.PublicTryReadStandardDeviationStochast( stochastName, calculationName, input, configuration, i => i.Distribution, (i, s) => i.Distribution = s); // Assert var expectedError = $"Indien voor parameter '{stochastName}' de spreiding wordt opgegeven, moet dit door middel van een standaardafwijking. " + $"Voor berekening '{calculationName}' is een variatiecoëfficiënt gevonden. Berekening '{calculationName}' is overgeslagen."; TestHelper.AssertLogMessageWithLevelIsGenerated(validate, Tuple.Create(expectedError, LogLevelConstant.Error)); Assert.IsFalse(valid); } [Test] [TestCase(true, false)] [TestCase(false, true)] [TestCase(true, true)] [TestCase(false, false)] public void TryReadVariationCoefficientStochast_ValidStochastConfiguration_ReturnsTrueParametersSet(bool setMean, bool setVariationCoefficient) { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); var configuration = new StochastConfiguration(); var random = new Random(21); double mean = random.NextDouble(); double variationCoefficient = random.NextDouble(); if (setMean) { configuration.Mean = mean; } if (setVariationCoefficient) { configuration.VariationCoefficient = variationCoefficient; } var input = new TestInputWithStochasts(); // Call var valid = importer.PublicTryReadVariationCoefficientStochast( "some stochast name", "some calculation name", input, configuration, i => i.VariationCoefficientDistribution, (i, s) => i.VariationCoefficientDistribution = s); // Assert Assert.IsTrue(valid); var defaultLogNormal = new VariationCoefficientLogNormalDistribution(); Assert.AreEqual( setMean ? mean : defaultLogNormal.Mean, input.VariationCoefficientDistribution.Mean, input.VariationCoefficientDistribution.Mean.GetAccuracy()); Assert.AreEqual( setVariationCoefficient ? variationCoefficient : defaultLogNormal.CoefficientOfVariation, input.VariationCoefficientDistribution.CoefficientOfVariation, input.VariationCoefficientDistribution.CoefficientOfVariation.GetAccuracy()); } [Test] public void TryReadVariationCoefficientStochast_StochastConfigurationWithStandardDeviation_LogsErrorReturnsFalse() { // Setup string filePath = Path.Combine(readerPath, "validConfiguration.xml"); var calculationGroup = new CalculationGroup(); var importer = new CalculationConfigurationImporter(filePath, calculationGroup); var configuration = new StochastConfiguration { StandardDeviation = new Random(21).NextDouble() }; var input = new TestInputWithStochasts(); var valid = true; const string stochastName = "some stochast name"; const string calculationName = "some calculation name"; // Call Action validate = () => valid = importer.PublicTryReadVariationCoefficientStochast( stochastName, calculationName, input, configuration, i => i.VariationCoefficientDistribution, (i, s) => i.VariationCoefficientDistribution = s); // Assert var expectedError = $"Indien voor parameter '{stochastName}' de spreiding wordt opgegeven, moet dit door middel van een variatiecoëfficiënt. " + $"Voor berekening '{calculationName}' is een standaardafwijking gevonden. Berekening '{calculationName}' is overgeslagen."; TestHelper.AssertLogMessageWithLevelIsGenerated(validate, Tuple.Create(expectedError, LogLevelConstant.Error)); Assert.IsFalse(valid); } private class CalculationConfigurationImporter : CalculationConfigurationImporter { public CalculationConfigurationImporter(string filePath, CalculationGroup importTarget) : base(filePath, importTarget) {} public void PublicReadWaveReductionParameters(WaveReductionConfiguration waveReduction, T input) where T : IUseBreakWater, IUseForeshore { ReadWaveReductionParameters(waveReduction, input); } public bool PublicTryReadHydraulicBoundaryLocation(string locationName, string calculationName, IEnumerable hydraulicBoundaryLocations, out HydraulicBoundaryLocation location) { return TryReadHydraulicBoundaryLocation(locationName, calculationName, hydraulicBoundaryLocations, out location); } public bool PublicTryReadForeshoreProfile(string locationName, string calculationName, IEnumerable foreshoreProfiles, out ForeshoreProfile location) { return TryReadForeshoreProfile(locationName, calculationName, foreshoreProfiles, out location); } public bool PublicTryReadStructure(string locationName, string calculationName, IEnumerable structures, out StructureBase location) { return TryReadStructure(locationName, calculationName, structures, out location); } public bool PublicTryReadStandardDeviationStochast( string stochastName, string calculationName, TestInputWithStochasts input, StochastConfiguration stochastConfiguration, Func getStochast, Action setStochast) where TDistribution : IDistribution { return TryReadStandardDeviationStochast(stochastName, calculationName, input, stochastConfiguration, getStochast, setStochast); } public bool PublicTryReadVariationCoefficientStochast( string stochastName, string calculationName, TestInputWithStochasts input, StochastConfiguration stochastConfiguration, Func getStochast, Action setStochast) where TDistribution : IVariationCoefficientDistribution { return TryReadVariationCoefficientStochast(stochastName, calculationName, input, stochastConfiguration, getStochast, setStochast); } protected override CalculationConfigurationReader CreateCalculationConfigurationReader(string xmlFilePath) { return new CalculationConfigurationReader(xmlFilePath); } protected override ICalculation ParseReadCalculation(ReadCalculation readCalculation) { return new TestCalculation { Name = readCalculation.Name }; } } private class CalculationConfigurationReader : CalculationConfigurationReader { private static readonly string mainSchemaDefinition = File.ReadAllText(Path.Combine(TestHelper.GetTestDataPath( TestDataPath.Ringtoets.Common.IO, "CalculationConfigurationReader"), "validConfigurationSchema.xsd")); public CalculationConfigurationReader(string xmlFilePath) : base(xmlFilePath, mainSchemaDefinition, new Dictionary()) {} protected override ReadCalculation ParseCalculationElement(XElement calculationElement) { return new ReadCalculation(calculationElement.Attribute(ConfigurationSchemaIdentifiers.NameAttribute)?.Value); } } private class ReadCalculation : IConfigurationItem { public ReadCalculation(string name) { Name = name; } public string Name { get; } } private class TestCalculation : Observable, ICalculation { public string Name { get; set; } public bool HasOutput { get; } public Comment Comments { get; } public void ClearOutput() { throw new NotImplementedException(); } } private class ExpectedProgressNotification { public string Text { get; set; } public int CurrentStep { get; set; } public int TotalNumberOfSteps { get; set; } } private static CalculationGroup GetExpectedNestedData() { return new CalculationGroup("Root", false) { Children = { new CalculationGroup("Group 1", false) { Children = { new TestCalculation { Name = "Calculation 3" } } }, new TestCalculation { Name = "Calculation 1" }, new CalculationGroup("Group 2", false) { Children = { new CalculationGroup("Group 4", false) { Children = { new TestCalculation { Name = "Calculation 5" } } }, new TestCalculation { Name = "Calculation 4" } } }, new TestCalculation { Name = "Calculation 2" }, new CalculationGroup("Group 3", false) } }; } private static void AssertCalculationGroup(CalculationGroup expectedCalculationGroup, CalculationGroup actualCalculationGroup) { Assert.AreEqual(expectedCalculationGroup.Children.Count, actualCalculationGroup.Children.Count); Assert.IsTrue(actualCalculationGroup.IsNameEditable); for (var i = 0; i < expectedCalculationGroup.Children.Count; i++) { Assert.AreEqual(expectedCalculationGroup.Children[i].Name, actualCalculationGroup.Children[i].Name); var innerCalculationgroup = expectedCalculationGroup.Children[i] as CalculationGroup; var innerCalculation = expectedCalculationGroup.Children[i] as TestCalculation; if (innerCalculationgroup != null) { AssertCalculationGroup(innerCalculationgroup, (CalculationGroup) actualCalculationGroup.Children[i]); } if (innerCalculation != null) { Assert.AreEqual(innerCalculation.Name, ((TestCalculation) actualCalculationGroup.Children[i]).Name); } } } private class TestStructure : StructureBase { public TestStructure(string name) : base(name, "id", new Point2D(0, 0), 2) {} } private class TestInputWithForeshoreProfileAndBreakWater : Observable, IUseBreakWater, IUseForeshore { public TestInputWithForeshoreProfileAndBreakWater(BreakWater breakWater) { BreakWater = breakWater; } public bool UseBreakWater { get; set; } public BreakWater BreakWater { get; } public bool UseForeshore { get; set; } public RoundedPoint2DCollection ForeshoreGeometry { get { throw new NotImplementedException(); } } } private class TestInputWithStochasts : Observable, ICalculationInput { public TestInputWithStochasts() { Distribution = new LogNormalDistribution(); VariationCoefficientDistribution = new VariationCoefficientLogNormalDistribution(); } public IDistribution Distribution { get; set; } public IVariationCoefficientDistribution VariationCoefficientDistribution { get; set; } } } }