Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/CalculationConfigurationImporterTest.cs =================================================================== diff -u -r82af27f82c4dc2248e03c0e22bbe46b5e01e88bb -rb80222df5e484e90e255f10116a7e88ead10a289 --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/CalculationConfigurationImporterTest.cs (.../CalculationConfigurationImporterTest.cs) (revision 82af27f82c4dc2248e03c0e22bbe46b5e01e88bb) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/CalculationConfigurationImporterTest.cs (.../CalculationConfigurationImporterTest.cs) (revision b80222df5e484e90e255f10116a7e88ead10a289) @@ -25,6 +25,7 @@ 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; @@ -112,7 +113,7 @@ // Assert TestHelper.AssertLogMessages(call, messages => { - var msgs = messages.ToArray(); + 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]); }); @@ -241,7 +242,7 @@ calculationGroup); // Call - var valid = importer.PublicValidateWaveReduction(null, null, calculationName); + bool valid = importer.PublicValidateWaveReduction(null, null, calculationName); // Assert Assert.IsTrue(valid); @@ -260,7 +261,7 @@ calculationGroup); // Call - var valid = importer.PublicValidateWaveReduction(new WaveReductionConfiguration(), null, calculationName); + bool valid = importer.PublicValidateWaveReduction(new WaveReductionConfiguration(), null, calculationName); // Assert Assert.IsTrue(valid); @@ -304,7 +305,7 @@ Action validate = () => valid = importer.PublicValidateWaveReduction(waveReductionConfiguration, null, calculationName); // Assert - var expectedMessage = $"Er is geen voorlandprofiel opgegeven om golfreductie parameters aan toe te voegen. Berekening '{calculationName}' is overgeslagen."; + string expectedMessage = $"Er is geen voorlandprofiel opgegeven om golfreductie parameters aan toe te voegen. Berekening '{calculationName}' is overgeslagen."; TestHelper.AssertLogMessageWithLevelIsGenerated(validate, Tuple.Create(expectedMessage, LogLevelConstant.Error)); Assert.IsFalse(valid); } @@ -363,12 +364,136 @@ Action validate = () => valid = importer.PublicValidateWaveReduction(waveReductionConfiguration, new TestForeshoreProfile(profileName), calculationName); // Assert - var expectedMessage = $"Het opgegeven voorlandprofiel '{profileName}' heeft geen voorlandgeometrie en kan daarom niet gebruikt worden. Berekening '{calculationName}' is overgeslagen."; + string expectedMessage = $"Het opgegeven voorlandprofiel '{profileName}' heeft geen voorlandgeometrie en kan daarom niet gebruikt worden. Berekening '{calculationName}' is overgeslagen."; TestHelper.AssertLogMessageWithLevelIsGenerated(validate, Tuple.Create(expectedMessage, LogLevelConstant.Error)); Assert.IsFalse(valid); } [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 @@ -424,7 +549,7 @@ HydraulicBoundaryLocation location; // Call - var valid = importer.PublicTryReadHydraulicBoundaryLocation(null, "name", Enumerable.Empty(), out location); + bool valid = importer.PublicTryReadHydraulicBoundaryLocation(null, "name", Enumerable.Empty(), out location); // Assert Assert.IsTrue(valid); @@ -443,7 +568,7 @@ calculationGroup); HydraulicBoundaryLocation location = null; - bool valid = true; + var valid = true; const string locationName = "someName"; const string calculationName = "name"; @@ -452,7 +577,7 @@ Action validate = () => valid = importer.PublicTryReadHydraulicBoundaryLocation(locationName, calculationName, Enumerable.Empty(), out location); // Assert - var expectedMessage = $"De locatie met hydraulische randvoorwaarden '{locationName}' bestaat niet. Berekening '{calculationName}' is overgeslagen."; + 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); @@ -547,7 +672,7 @@ ForeshoreProfile profile; // Call - var valid = importer.PublicTryReadForeshoreProfile(null, "name", Enumerable.Empty(), out profile); + bool valid = importer.PublicTryReadForeshoreProfile(null, "name", Enumerable.Empty(), out profile); // Assert Assert.IsTrue(valid); @@ -566,7 +691,7 @@ calculationGroup); ForeshoreProfile profile = null; - bool valid = true; + var valid = true; const string profileName = "someName"; const string calculationName = "name"; @@ -575,7 +700,7 @@ Action validate = () => valid = importer.PublicTryReadForeshoreProfile(profileName, calculationName, Enumerable.Empty(), out profile); // Assert - var expectedMessage = $"Het voorlandprofiel '{profileName}' bestaat niet. Berekening '{calculationName}' is overgeslagen."; + 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); @@ -670,7 +795,7 @@ StructureBase structure; // Call - var valid = importer.PublicTryReadStructure(null, "name", Enumerable.Empty(), out structure); + bool valid = importer.PublicTryReadStructure(null, "name", Enumerable.Empty(), out structure); // Assert Assert.IsTrue(valid); @@ -689,7 +814,7 @@ calculationGroup); StructureBase profile = null; - bool valid = true; + var valid = true; const string structureName = "someName"; const string calculationName = "name"; @@ -698,7 +823,7 @@ Action validate = () => valid = importer.PublicTryReadStructure(structureName, calculationName, Enumerable.Empty(), out profile); // Assert - var expectedMessage = $"Het kunstwerk '{structureName}' bestaat niet. Berekening '{calculationName}' is overgeslagen."; + 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); @@ -718,7 +843,7 @@ var importer = new CalculationConfigurationImporter(filePath, calculationGroup); - TestStructure expectedProfile = new TestStructure(structureName); + var expectedProfile = new TestStructure(structureName); StructureBase structure; // Call @@ -751,7 +876,7 @@ const string message = "an error"; const string calculationName = "calculationA"; const string innerMessage = "Inner message"; - ArgumentOutOfRangeException exception = new ArgumentOutOfRangeException(null, innerMessage); + var exception = new ArgumentOutOfRangeException(null, innerMessage); // Call Action log = () => importer.PublicLogOutOfRangeException(message, calculationName, exception); @@ -765,19 +890,6 @@ public CalculationConfigurationImporter(string filePath, CalculationGroup importTarget) : base(filePath, importTarget) {} - protected override CalculationConfigurationReader CreateCalculationConfigurationReader(string xmlFilePath) - { - return new CalculationConfigurationReader(xmlFilePath); - } - - protected override ICalculation ParseReadCalculation(ReadCalculation readCalculation) - { - return new TestCalculation - { - Name = readCalculation.Name - }; - } - public void PublicLogOutOfRangeException(string errorMessage, string calculationName, ArgumentOutOfRangeException e) { LogOutOfRangeException(errorMessage, calculationName, e); @@ -788,6 +900,12 @@ return ValidateWaveReduction(waveReduction, foreshoreProfile, calculationName); } + 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); @@ -802,6 +920,19 @@ { return TryReadStructure(locationName, calculationName, structures, out location); } + + 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 @@ -924,7 +1055,27 @@ private class TestStructure : StructureBase { - public TestStructure(string name) : base(name, "id", new Point2D(0, 0), 2) { } + 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(); + } + } + } } } \ No newline at end of file