Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/CalculationConfigurationImporterTest.cs =================================================================== diff -u -rcc1268d7cb906524d4cabcd4cbd9ae16676cf059 -r82af27f82c4dc2248e03c0e22bbe46b5e01e88bb --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/CalculationConfigurationImporterTest.cs (.../CalculationConfigurationImporterTest.cs) (revision cc1268d7cb906524d4cabcd4cbd9ae16676cf059) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/CalculationConfigurationImporterTest.cs (.../CalculationConfigurationImporterTest.cs) (revision 82af27f82c4dc2248e03c0e22bbe46b5e01e88bb) @@ -25,11 +25,15 @@ using System.Linq; using System.Xml.Linq; using Core.Common.Base; +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.TestUtil; using Ringtoets.Common.IO.Configurations; using Ringtoets.Common.IO.FileImporters; using Ringtoets.Common.IO.Readers; @@ -205,6 +209,557 @@ AssertCalculationGroup(GetExpectedNestedData(), calculationGroup); } + [Test] + public void ValidateWaveReduction_NoCalculationName_ThrowsArgumentNullException() + { + // Setup + string filePath = Path.Combine(readerPath, "validConfiguration.xml"); + + var calculationGroup = new CalculationGroup(); + + var importer = new CalculationConfigurationImporter(filePath, + calculationGroup); + + // Call + TestDelegate test = () => importer.PublicValidateWaveReduction(null, null, null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("calculationName", exception.ParamName); + } + + [Test] + public void ValidateWaveReduction_NoForeshoreProfileNoParameters_ReturnsTrue() + { + // Setup + string filePath = Path.Combine(readerPath, "validConfiguration.xml"); + const string calculationName = "calculation"; + + var calculationGroup = new CalculationGroup(); + + var importer = new CalculationConfigurationImporter(filePath, + calculationGroup); + + // Call + var valid = importer.PublicValidateWaveReduction(null, null, calculationName); + + // Assert + Assert.IsTrue(valid); + } + + [Test] + public void ValidateWaveReduction_NoForeshoreProfileWaveReductionWithoutParameters_ReturnsTrue() + { + // Setup + string filePath = Path.Combine(readerPath, "validConfiguration.xml"); + const string calculationName = "calculation"; + + var calculationGroup = new CalculationGroup(); + + var importer = new CalculationConfigurationImporter(filePath, + calculationGroup); + + // Call + var valid = importer.PublicValidateWaveReduction(new WaveReductionConfiguration(), null, calculationName); + + // Assert + Assert.IsTrue(valid); + } + + [Test] + public void ValidateWaveReduction_NoForeshoreProfileWaveReductionWithParameter_LogsErrorReturnsFalse([Values(0, 1, 2, 3)] int propertyToSet) + { + // Setup + string filePath = Path.Combine(readerPath, "validConfiguration.xml"); + const string calculationName = "calculation"; + + var calculationGroup = new CalculationGroup(); + + var importer = new CalculationConfigurationImporter(filePath, + calculationGroup); + + var waveReductionConfiguration = new WaveReductionConfiguration(); + var random = new Random(21); + + switch (propertyToSet) + { + case 0: + waveReductionConfiguration.BreakWaterType = random.NextEnumValue(); + break; + case 1: + waveReductionConfiguration.BreakWaterHeight = random.NextDouble(); + break; + case 2: + waveReductionConfiguration.UseBreakWater = random.NextBoolean(); + break; + case 3: + waveReductionConfiguration.UseForeshoreProfile = random.NextBoolean(); + break; + } + ; + + var valid = true; + + // Call + 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."; + TestHelper.AssertLogMessageWithLevelIsGenerated(validate, Tuple.Create(expectedMessage, LogLevelConstant.Error)); + Assert.IsFalse(valid); + } + + [Test] + public void ValidateWaveReduction_ForeshoreProfileWithGeometryForeshoreProfileUsed_ReturnsTrue() + { + // Setup + string filePath = Path.Combine(readerPath, "validConfiguration.xml"); + const string calculationName = "calculation"; + + var calculationGroup = new CalculationGroup(); + + var importer = new CalculationConfigurationImporter(filePath, + calculationGroup); + + var waveReductionConfiguration = new WaveReductionConfiguration + { + UseForeshoreProfile = true + }; + + // Call + bool valid = importer.PublicValidateWaveReduction( + waveReductionConfiguration, + new TestForeshoreProfile("voorland", new[] + { + new Point2D(0, 2) + }), + calculationName); + + // Assert + Assert.IsTrue(valid); + } + + [Test] + public void ValidateWaveReduction_ForeshoreProfileWithoutGeometryForeshoreProfileUsed_LogsErrorReturnsFalse() + { + // Setup + string filePath = Path.Combine(readerPath, "validConfiguration.xml"); + const string calculationName = "calculation"; + + var calculationGroup = new CalculationGroup(); + + var importer = new CalculationConfigurationImporter(filePath, + calculationGroup); + + var waveReductionConfiguration = new WaveReductionConfiguration + { + UseForeshoreProfile = true + }; + + var valid = true; + var profileName = "voorland"; + + // Call + 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."; + TestHelper.AssertLogMessageWithLevelIsGenerated(validate, Tuple.Create(expectedMessage, LogLevelConstant.Error)); + Assert.IsFalse(valid); + } + + [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 + var 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; + bool valid = true; + + const string locationName = "someName"; + const string calculationName = "name"; + + // Call + 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."; + 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 + var 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; + bool valid = true; + + const string profileName = "someName"; + const string calculationName = "name"; + + // Call + Action validate = () => valid = importer.PublicTryReadForeshoreProfile(profileName, calculationName, Enumerable.Empty(), out profile); + + // Assert + var 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 + var 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; + bool valid = true; + + const string structureName = "someName"; + const string calculationName = "name"; + + // Call + Action validate = () => valid = importer.PublicTryReadStructure(structureName, calculationName, Enumerable.Empty(), out profile); + + // Assert + var 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); + + TestStructure 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] + public void LogOutOfRangeException_Always_LogMessage() + { + // Setup + string filePath = Path.Combine(readerPath, "validConfiguration.xml"); + + var calculationGroup = new CalculationGroup(); + + var importer = new CalculationConfigurationImporter(filePath, + calculationGroup); + + const string message = "an error"; + const string calculationName = "calculationA"; + const string innerMessage = "Inner message"; + ArgumentOutOfRangeException exception = new ArgumentOutOfRangeException(null, innerMessage); + + // Call + Action log = () => importer.PublicLogOutOfRangeException(message, calculationName, exception); + + // Assert + TestHelper.AssertLogMessageWithLevelIsGenerated(log, Tuple.Create($"{message} {innerMessage} Berekening '{calculationName}' is overgeslagen.", LogLevelConstant.Error)); + } + private class CalculationConfigurationImporter : CalculationConfigurationImporter { public CalculationConfigurationImporter(string filePath, CalculationGroup importTarget) @@ -222,6 +777,31 @@ Name = readCalculation.Name }; } + + public void PublicLogOutOfRangeException(string errorMessage, string calculationName, ArgumentOutOfRangeException e) + { + LogOutOfRangeException(errorMessage, calculationName, e); + } + + public bool PublicValidateWaveReduction(WaveReductionConfiguration waveReduction, ForeshoreProfile foreshoreProfile, string calculationName) + { + return ValidateWaveReduction(waveReduction, foreshoreProfile, calculationName); + } + + 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); + } } private class CalculationConfigurationReader : CalculationConfigurationReader @@ -256,6 +836,7 @@ public string Name { get; set; } public bool HasOutput { get; } public Comment Comments { get; } + public void ClearOutput() { throw new NotImplementedException(); @@ -340,5 +921,10 @@ } } } + + private class TestStructure : StructureBase + { + public TestStructure(string name) : base(name, "id", new Point2D(0, 0), 2) { } + } } } \ No newline at end of file