Index: Riskeer/Integration/src/Riskeer.Integration.IO/Exporters/HydraulicBoundaryLocationCalculationsExporter.cs =================================================================== diff -u -rf6640bf5857d4318252c06c2ed62c66e4ce4e552 -rbcda3536acc16028971ad92dce17aa5472bd0590 --- Riskeer/Integration/src/Riskeer.Integration.IO/Exporters/HydraulicBoundaryLocationCalculationsExporter.cs (.../HydraulicBoundaryLocationCalculationsExporter.cs) (revision f6640bf5857d4318252c06c2ed62c66e4ce4e552) +++ Riskeer/Integration/src/Riskeer.Integration.IO/Exporters/HydraulicBoundaryLocationCalculationsExporter.cs (.../HydraulicBoundaryLocationCalculationsExporter.cs) (revision bcda3536acc16028971ad92dce17aa5472bd0590) @@ -29,8 +29,7 @@ using log4net; using Riskeer.Common.Data.AssessmentSection; using Riskeer.Common.Data.Hydraulics; -using Riskeer.Common.Forms.Helpers; -using Riskeer.Integration.IO.Properties; +using Riskeer.Integration.IO.Helpers; using RiskeerCommonFormsResources = Riskeer.Common.Forms.Properties.Resources; using RiskeerCommonIOResources = Riskeer.Common.IO.Properties.Resources; @@ -138,39 +137,8 @@ { var exportedCalculations = new Dictionary, string>(); return calculationsForTargetProbabilities.All( - calculationsForTargetProbability => ExportLocationCalculationsForTargetProbability( + calculationsForTargetProbability => HydraulicBoundaryLocationCalculationsExportHelper.ExportLocationCalculationsForTargetProbability( calculationsForTargetProbability, exportedCalculations, calculationsType, folderPath)); } - - private static bool ExportLocationCalculationsForTargetProbability( - Tuple, double> calculationsForTargetProbability, - IDictionary, string> exportedCalculations, - HydraulicBoundaryLocationCalculationsType calculationsType, - string folderPath) - { - IEnumerable calculations = calculationsForTargetProbability.Item1; - double targetProbability = calculationsForTargetProbability.Item2; - - string exportType = calculationsType == HydraulicBoundaryLocationCalculationsType.WaterLevel - ? Resources.WaterLevels_DisplayName - : Resources.WaveHeights_DisplayName; - - string uniqueName = NamingHelper.GetUniqueName( - exportedCalculations, $"{exportType}_{ReturnPeriodFormattingHelper.FormatFromProbability(targetProbability)}", - c => c.Value); - - string tempFilePath = Path.Combine(folderPath, $"{uniqueName}.{RiskeerCommonIOResources.Shape_file_filter_Extension}"); - - var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilityExporter( - calculations, tempFilePath, calculationsType); - - if (!exporter.Export()) - { - return false; - } - - exportedCalculations.Add(calculations, uniqueName); - return true; - } } } \ No newline at end of file Index: Riskeer/Integration/src/Riskeer.Integration.IO/Exporters/HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter.cs =================================================================== diff -u -r4789e92d306ad94826e9348ac4ffd6c805cdf5b1 -rbcda3536acc16028971ad92dce17aa5472bd0590 --- Riskeer/Integration/src/Riskeer.Integration.IO/Exporters/HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter.cs (.../HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter.cs) (revision 4789e92d306ad94826e9348ac4ffd6c805cdf5b1) +++ Riskeer/Integration/src/Riskeer.Integration.IO/Exporters/HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter.cs (.../HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter.cs) (revision bcda3536acc16028971ad92dce17aa5472bd0590) @@ -29,10 +29,7 @@ using Core.Common.Util; using log4net; using Riskeer.Common.Data.Hydraulics; -using Riskeer.Common.Forms.Helpers; -using Riskeer.Integration.IO.Properties; -using CoreCommonUtilResources = Core.Common.Util.Properties.Resources; -using CoreGuiResources = Core.Gui.Properties.Resources; +using Riskeer.Integration.IO.Helpers; using RiskeerCommonIOResources = Riskeer.Common.IO.Properties.Resources; namespace Riskeer.Integration.IO.Exporters @@ -92,9 +89,8 @@ { var exportedCalculations = new Dictionary, string>(); if (locationCalculationsForTargetProbabilities.Any( - locationCalculationsForTargetProbability => !ExportLocationCalculationsForTargetProbability( - locationCalculationsForTargetProbability, - exportedCalculations))) + locationCalculationsForTargetProbability => !HydraulicBoundaryLocationCalculationsExportHelper.ExportLocationCalculationsForTargetProbability( + locationCalculationsForTargetProbability, exportedCalculations, calculationsType, tempFolderPath))) { return false; } @@ -117,34 +113,5 @@ } } } - - private bool ExportLocationCalculationsForTargetProbability( - Tuple, double> calculationsForTargetProbability, - IDictionary, string> exportedCalculations) - { - IEnumerable calculations = calculationsForTargetProbability.Item1; - double targetProbability = calculationsForTargetProbability.Item2; - - string exportType = calculationsType == HydraulicBoundaryLocationCalculationsType.WaterLevel - ? Resources.WaterLevels_DisplayName - : Resources.WaveHeights_DisplayName; - - string uniqueName = NamingHelper.GetUniqueName( - exportedCalculations, $"{exportType}_{ReturnPeriodFormattingHelper.FormatFromProbability(targetProbability)}", - c => c.Value); - - string tempFilePath = Path.Combine(tempFolderPath, $"{uniqueName}.{RiskeerCommonIOResources.Shape_file_filter_Extension}"); - - var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilityExporter( - calculations, tempFilePath, calculationsType); - - if (!exporter.Export()) - { - return false; - } - - exportedCalculations.Add(calculations, uniqueName); - return true; - } } } \ No newline at end of file Index: Riskeer/Integration/src/Riskeer.Integration.IO/Helpers/HydraulicBoundaryLocationCalculationsExportHelper.cs =================================================================== diff -u --- Riskeer/Integration/src/Riskeer.Integration.IO/Helpers/HydraulicBoundaryLocationCalculationsExportHelper.cs (revision 0) +++ Riskeer/Integration/src/Riskeer.Integration.IO/Helpers/HydraulicBoundaryLocationCalculationsExportHelper.cs (revision bcda3536acc16028971ad92dce17aa5472bd0590) @@ -0,0 +1,99 @@ +// Copyright (C) Stichting Deltares 2021. All rights reserved. +// +// This file is part of Riskeer. +// +// Riskeer 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.ComponentModel; +using System.IO; +using Riskeer.Common.Data.Hydraulics; +using Riskeer.Common.Forms.Helpers; +using Riskeer.Integration.IO.Exporters; +using Riskeer.Integration.IO.Properties; +using RiskeerCommonIOResources = Riskeer.Common.IO.Properties.Resources; + +namespace Riskeer.Integration.IO.Helpers +{ + /// + /// Helper class for exporting hydraulic boundary location calculations. + /// + public static class HydraulicBoundaryLocationCalculationsExportHelper + { + /// + /// Exports the location calculations for the target probability. + /// + /// The calculations to export. + /// The already exported calculations. + /// The type of the calculations. + /// The path of the folder to export to. + /// true when the export was successful; false otherwise. + /// Thrown when + /// or is null. + /// Thrown when + /// is invalid. + public static bool ExportLocationCalculationsForTargetProbability( + Tuple, double> calculationsForTargetProbability, + IDictionary, string> exportedCalculations, + HydraulicBoundaryLocationCalculationsType calculationsType, + string folderPath) + { + if (calculationsForTargetProbability == null) + { + throw new ArgumentNullException(nameof(calculationsForTargetProbability)); + } + + if (exportedCalculations == null) + { + throw new ArgumentNullException(nameof(exportedCalculations)); + } + + if (!Enum.IsDefined(typeof(HydraulicBoundaryLocationCalculationsType), calculationsType)) + { + throw new InvalidEnumArgumentException(nameof(calculationsType), + (int) calculationsType, + typeof(HydraulicBoundaryLocationCalculationsType)); + } + + IEnumerable calculations = calculationsForTargetProbability.Item1; + double targetProbability = calculationsForTargetProbability.Item2; + + string exportType = calculationsType == HydraulicBoundaryLocationCalculationsType.WaterLevel + ? Resources.WaterLevels_DisplayName + : Resources.WaveHeights_DisplayName; + + string uniqueName = NamingHelper.GetUniqueName( + exportedCalculations, $"{exportType}_{ReturnPeriodFormattingHelper.FormatFromProbability(targetProbability)}", + c => c.Value); + + string tempFilePath = Path.Combine(folderPath, $"{uniqueName}.{RiskeerCommonIOResources.Shape_file_filter_Extension}"); + + var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilityExporter( + calculations, tempFilePath, calculationsType); + + if (!exporter.Export()) + { + return false; + } + + exportedCalculations.Add(calculations, uniqueName); + return true; + } + } +} \ No newline at end of file Index: Riskeer/Integration/test/Riskeer.Integration.IO.Test/Helpers/HydraulicBoundaryLocationCalculationsExportHelperTest.cs =================================================================== diff -u --- Riskeer/Integration/test/Riskeer.Integration.IO.Test/Helpers/HydraulicBoundaryLocationCalculationsExportHelperTest.cs (revision 0) +++ Riskeer/Integration/test/Riskeer.Integration.IO.Test/Helpers/HydraulicBoundaryLocationCalculationsExportHelperTest.cs (revision bcda3536acc16028971ad92dce17aa5472bd0590) @@ -0,0 +1,161 @@ +// Copyright (C) Stichting Deltares 2021. All rights reserved. +// +// This file is part of Riskeer. +// +// Riskeer 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.ComponentModel; +using System.IO; +using System.Linq; +using System.Security.AccessControl; +using Core.Common.TestUtil; +using NUnit.Framework; +using Riskeer.Common.Data.Hydraulics; +using Riskeer.Common.IO.TestUtil; +using Riskeer.Integration.IO.Exporters; +using Riskeer.Integration.IO.Helpers; + +namespace Riskeer.Integration.IO.Test.Helpers +{ + [TestFixture] + public class HydraulicBoundaryLocationCalculationsExportHelperTest + { + [Test] + public void ExportLocationCalculationsForTargetProbabilityConstructor_CalculationsForTargetProbabilityNull_ThrowsArgumentNullException() + { + // Call + void Call() => HydraulicBoundaryLocationCalculationsExportHelper.ExportLocationCalculationsForTargetProbability( + null, new Dictionary, string>(), + HydraulicBoundaryLocationCalculationsType.WaterLevel, string.Empty); + + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual("calculationsForTargetProbability", exception.ParamName); + } + + [Test] + public void ExportLocationCalculationsForTargetProbability_ExportedCalculationsNull_ThrowsArgumentNullException() + { + // Call + void Call() => HydraulicBoundaryLocationCalculationsExportHelper.ExportLocationCalculationsForTargetProbability( + new Tuple, double>(Enumerable.Empty(), double.NaN), + null, HydraulicBoundaryLocationCalculationsType.WaterLevel, string.Empty); + + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual("exportedCalculations", exception.ParamName); + } + + [Test] + public void ExportLocationCalculationsForTargetProbability_InvalidCalculationsType_ThrowsInvalidEnumArgumentException() + { + // Setup + const HydraulicBoundaryLocationCalculationsType calculationsType = (HydraulicBoundaryLocationCalculationsType) 99; + + // Call + void Call() => HydraulicBoundaryLocationCalculationsExportHelper.ExportLocationCalculationsForTargetProbability( + new Tuple, double>(Enumerable.Empty(), double.NaN), + new Dictionary, string>(), calculationsType, string.Empty); + + // Assert + string expectedMessage = $"The value of argument 'calculationsType' ({calculationsType}) " + + $"is invalid for Enum type '{nameof(HydraulicBoundaryLocationCalculationsType)}'."; + var exception = TestHelper.AssertThrowsArgumentExceptionAndTestMessage(Call, expectedMessage); + Assert.AreEqual("calculationsType", exception.ParamName); + } + + [Test] + [TestCase(HydraulicBoundaryLocationCalculationsType.WaterLevel, "ExpectedWaterLevelExport")] + [TestCase(HydraulicBoundaryLocationCalculationsType.WaveHeight, "ExpectedWaveHeightExport")] + public void ExportLocationCalculationsForTargetProbability_ValidData_ReturnsTrueAndWritesCorrectData(HydraulicBoundaryLocationCalculationsType calculationsType, + string expectedExportFileName) + { + // Setup + const string fileName = "test"; + + string directoryPath = TestHelper.GetScratchPadPath(nameof(ExportLocationCalculationsForTargetProbability_ValidData_ReturnsTrueAndWritesCorrectData)); + Directory.CreateDirectory(directoryPath); + string filePath = Path.Combine(directoryPath, $"{fileName}.shp"); + + var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilityExporter(new[] + { + new HydraulicBoundaryLocationCalculation(new HydraulicBoundaryLocation(123, "aName", 1.1, 2.2)) + }, filePath, calculationsType); + + // Precondition + FileTestHelper.AssertEssentialShapefilesExist(directoryPath, fileName, false); + + try + { + // Call + bool isExported = exporter.Export(); + + // Assert + FileTestHelper.AssertEssentialShapefilesExist(directoryPath, fileName, true); + FileTestHelper.AssertEssentialShapefileMd5Hashes( + directoryPath, fileName, + Path.Combine(TestHelper.GetTestDataPath(TestDataPath.Riskeer.Integration.IO), + nameof(HydraulicBoundaryLocationCalculationsExportHelper)), + expectedExportFileName, 28, 8, 628); + Assert.IsTrue(isExported); + } + finally + { + Directory.Delete(directoryPath, true); + } + } + + [Test] + public void ExportLocationCalculationsForTargetProbability_InvalidDirectoryRights_LogErrorAndReturnFalse() + { + // Setup + const string fileName = "test"; + + string directoryPath = TestHelper.GetScratchPadPath(nameof(ExportLocationCalculationsForTargetProbability_InvalidDirectoryRights_LogErrorAndReturnFalse)); + Directory.CreateDirectory(directoryPath); + string filePath = Path.Combine(directoryPath, $"{fileName}.shp"); + + var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilityExporter(new[] + { + new HydraulicBoundaryLocationCalculation(new HydraulicBoundaryLocation(123, "aName", 1.1, 2.2)) + }, filePath, HydraulicBoundaryLocationCalculationsType.WaterLevel); + + try + { + using (new DirectoryPermissionsRevoker(directoryPath, FileSystemRights.Write)) + { + // Call + var isExported = true; + void Call() => isExported = exporter.Export(); + + // Assert + string expectedMessage = $"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'. " + + "Er zijn geen hydraulische belastingenlocaties geëxporteerd."; + TestHelper.AssertLogMessageIsGenerated(Call, expectedMessage); + Assert.IsFalse(isExported); + } + } + finally + { + Directory.Delete(directoryPath, true); + } + } + } +} \ No newline at end of file Index: Riskeer/Integration/test/Riskeer.Integration.IO.Test/test-data/HydraulicBoundaryLocationCalculationsExportHelper/ExpectedWaterLevelExport.dbf =================================================================== diff -u Binary files differ Index: Riskeer/Integration/test/Riskeer.Integration.IO.Test/test-data/HydraulicBoundaryLocationCalculationsExportHelper/ExpectedWaterLevelExport.shp =================================================================== diff -u Binary files differ Index: Riskeer/Integration/test/Riskeer.Integration.IO.Test/test-data/HydraulicBoundaryLocationCalculationsExportHelper/ExpectedWaterLevelExport.shx =================================================================== diff -u Binary files differ Index: Riskeer/Integration/test/Riskeer.Integration.IO.Test/test-data/HydraulicBoundaryLocationCalculationsExportHelper/ExpectedWaveHeightExport.dbf =================================================================== diff -u Binary files differ Index: Riskeer/Integration/test/Riskeer.Integration.IO.Test/test-data/HydraulicBoundaryLocationCalculationsExportHelper/ExpectedWaveHeightExport.shp =================================================================== diff -u Binary files differ Index: Riskeer/Integration/test/Riskeer.Integration.IO.Test/test-data/HydraulicBoundaryLocationCalculationsExportHelper/ExpectedWaveHeightExport.shx =================================================================== diff -u Binary files differ