Index: Riskeer/Integration/src/Riskeer.Integration.IO/Exporters/HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter.cs =================================================================== diff -u -r4eea139ce2a04e1f095eb98c18e283311d94f7d4 -r63b9c8a671b9f3d9eeb74f9a2cf93e0b8d63d84e --- Riskeer/Integration/src/Riskeer.Integration.IO/Exporters/HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter.cs (.../HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter.cs) (revision 4eea139ce2a04e1f095eb98c18e283311d94f7d4) +++ Riskeer/Integration/src/Riskeer.Integration.IO/Exporters/HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter.cs (.../HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter.cs) (revision 63b9c8a671b9f3d9eeb74f9a2cf93e0b8d63d84e) @@ -22,6 +22,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.IO.Compression; using System.Linq; using Core.Common.Base.IO; using Core.Common.Util; @@ -31,7 +32,7 @@ using Riskeer.Integration.IO.Properties; using CoreCommonUtilResources = Core.Common.Util.Properties.Resources; using CoreGuiResources = Core.Gui.Properties.Resources; -using RiskeerCommonIOResources = Riskeer.Common.IO.Properties.Resources ; +using RiskeerCommonIOResources = Riskeer.Common.IO.Properties.Resources; namespace Riskeer.Integration.IO.Exporters { @@ -41,81 +42,96 @@ public class HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter : IFileExporter { private static readonly ILog log = LogManager.GetLogger(typeof(HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter)); - - private readonly IEnumerable> locationCalculationsForTargetProbabilities; - private readonly string folderPath; + private readonly IEnumerable, double, HydraulicBoundaryLocationCalculationsType>> locationCalculationsForTargetProbabilities; + private readonly string filePath; + private readonly string tempFolderPath; + /// /// Creates a new instance of . /// /// The collection of /// to export. - /// The folder path to export to. + /// The path of the file to export to. /// Thrown when /// is null. - /// Thrown when is invalid. - /// A valid path: - /// is not empty or null, - /// does not consist out of only whitespace characters, - /// does not contain an invalid character, - /// is not too long. - /// + /// Thrown when is invalid. public HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter( - IEnumerable> locationCalculationsForTargetProbabilities, - string folderPath) + IEnumerable, double, HydraulicBoundaryLocationCalculationsType>> locationCalculationsForTargetProbabilities, + string filePath) { if (locationCalculationsForTargetProbabilities == null) { throw new ArgumentNullException(nameof(locationCalculationsForTargetProbabilities)); } - IOUtils.ValidateFolderPath(folderPath); + IOUtils.ValidateFilePath(filePath); this.locationCalculationsForTargetProbabilities = locationCalculationsForTargetProbabilities; - this.folderPath = folderPath; + this.filePath = filePath; + string folderPath = Path.GetDirectoryName(filePath); + tempFolderPath = Path.Combine(folderPath, "~temp"); } public bool Export() { - var exportedCalculations = new Dictionary(); - return locationCalculationsForTargetProbabilities.All( - locationCalculationsForTargetProbability => ExportLocationCalculationsForTargetProbability( - locationCalculationsForTargetProbability.Item1, - locationCalculationsForTargetProbability.Item2, - exportedCalculations)); + try + { + var exportedCalculations = new Dictionary, string>(); + if (locationCalculationsForTargetProbabilities.Any( + locationCalculationsForTargetProbability => !ExportLocationCalculationsForTargetProbability( + locationCalculationsForTargetProbability, + exportedCalculations))) + { + return false; + } + + ZipFile.CreateFromDirectory(tempFolderPath, filePath); + + return true; + } + catch (Exception e) + { + log.ErrorFormat(RiskeerCommonIOResources.HydraulicBoundaryLocationsExporter_Error_Exception_0_no_HydraulicBoundaryLocations_exported, + e.Message); + return false; + } + finally + { + if (Directory.Exists(tempFolderPath)) + { + Directory.Delete(tempFolderPath, true); + } + } } private bool ExportLocationCalculationsForTargetProbability( - HydraulicBoundaryLocationCalculationsForTargetProbability calculationsForTargetProbability, - HydraulicBoundaryLocationCalculationsType calculationsType, - IDictionary exportedCalculations) + Tuple, double, HydraulicBoundaryLocationCalculationsType> calculationsForTargetProbability, + IDictionary, string> exportedCalculations) { + IEnumerable calculations = calculationsForTargetProbability.Item1; + double targetProbability = calculationsForTargetProbability.Item2; + HydraulicBoundaryLocationCalculationsType calculationsType = calculationsForTargetProbability.Item3; + string exportType = calculationsType == HydraulicBoundaryLocationCalculationsType.WaterLevel ? Resources.WaterLevels_DisplayName : Resources.WaveHeights_DisplayName; - double targetProbability = calculationsForTargetProbability.TargetProbability; - var exportName = $"{exportType} {ProbabilityFormattingHelper.Format(targetProbability)}"; - - log.InfoFormat(CoreGuiResources.GuiExportHandler_ExportItemUsingDialog_Start_exporting_DataType_0_, exportName); - string uniqueName = NamingHelper.GetUniqueName( exportedCalculations, $"{exportType}_{ReturnPeriodFormattingHelper.FormatFromProbability(targetProbability)}", c => c.Value); - string filePath = Path.Combine(folderPath, $"{uniqueName}.{RiskeerCommonIOResources.Shape_file_filter_Extension}"); + string tempFilePath = Path.Combine(tempFolderPath, $"{uniqueName}.{RiskeerCommonIOResources.Shape_file_filter_Extension}"); var exporter = new HydraulicBoundaryLocationCalculationsExporter( - calculationsForTargetProbability.HydraulicBoundaryLocationCalculations, - filePath, calculationsType); + calculations, tempFilePath, calculationsType); if (!exporter.Export()) { return false; } - log.InfoFormat(CoreGuiResources.GuiExportHandler_ExportItemUsingDialog_Data_from_0_exported_to_file_1, exportName, filePath); - exportedCalculations.Add(calculationsForTargetProbability, uniqueName); + exportedCalculations.Add(calculations, uniqueName); return true; } } Index: Riskeer/Integration/test/Riskeer.Integration.IO.Test/Exporters/HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporterTest.cs =================================================================== diff -u -rd22318d66f73e412d80e69398a515c9347f25c8e -r63b9c8a671b9f3d9eeb74f9a2cf93e0b8d63d84e --- Riskeer/Integration/test/Riskeer.Integration.IO.Test/Exporters/HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporterTest.cs (.../HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporterTest.cs) (revision d22318d66f73e412d80e69398a515c9347f25c8e) +++ Riskeer/Integration/test/Riskeer.Integration.IO.Test/Exporters/HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporterTest.cs (.../HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporterTest.cs) (revision 63b9c8a671b9f3d9eeb74f9a2cf93e0b8d63d84e) @@ -20,7 +20,9 @@ // All rights reserved. using System; +using System.Collections.Generic; using System.IO; +using System.IO.Compression; using System.Linq; using System.Security.AccessControl; using Core.Common.Base.IO; @@ -47,16 +49,11 @@ } [Test] - [TestCase(null)] - [TestCase("")] - [TestCase(" ")] - [TestCase("C:\\Not:Valid")] - public void Constructor_InvalidFolderPath_ThrowsArgumentException(string folderPath) + public void Constructor_FilePathNull_ThrowsArgumentException() { // Call void Call() => new HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter( - Enumerable.Empty>(), - folderPath); + Enumerable.Empty, double, HydraulicBoundaryLocationCalculationsType>>(), null); // Assert Assert.Throws(Call); @@ -65,10 +62,13 @@ [Test] public void Constructor_ExpectedValues() { + // Setup + string filePath = TestHelper.GetScratchPadPath(Path.Combine("export", "test.shp")); + // Call var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter( - Enumerable.Empty>(), - "test"); + Enumerable.Empty, double, HydraulicBoundaryLocationCalculationsType>>(), + filePath); // Assert Assert.IsInstanceOf(exporter); @@ -78,23 +78,23 @@ public void Export_HydraulicBoundaryLocationCalculationsExporterReturnsFalse_ReturnsFalse() { // Setup - string folderPath = TestHelper.GetScratchPadPath($"{nameof(HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporterTest)}.{nameof(Export_HydraulicBoundaryLocationCalculationsExporterReturnsFalse_ReturnsFalse)}"); - Directory.CreateDirectory(folderPath); + string directoryPath = TestHelper.GetScratchPadPath($"{nameof(Export_HydraulicBoundaryLocationCalculationsExporterReturnsFalse_ReturnsFalse)}"); + Directory.CreateDirectory(directoryPath); + string filePath = Path.Combine(directoryPath, "export.zip"); var calculationsForTargetProbabilities = new[] { - new Tuple( - new HydraulicBoundaryLocationCalculationsForTargetProbability(0.1), - HydraulicBoundaryLocationCalculationsType.WaterLevel) + new Tuple, double, HydraulicBoundaryLocationCalculationsType>( + Enumerable.Empty(), 0.1, HydraulicBoundaryLocationCalculationsType.WaterLevel) }; - var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter(calculationsForTargetProbabilities, folderPath); + var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter(calculationsForTargetProbabilities, filePath); - string expectedFilePath = Path.Combine(folderPath, "Waterstanden_10.shp"); + string expectedFilePath = Path.Combine(directoryPath, "~temp", "Waterstanden_10.shp"); try { - using (new DirectoryPermissionsRevoker(folderPath, FileSystemRights.Write)) + using (new DirectoryPermissionsRevoker(directoryPath, FileSystemRights.CreateDirectories)) { // Call var isExported = true; @@ -109,30 +109,31 @@ } finally { - Directory.Delete(folderPath, true); + Directory.Delete(directoryPath, true); } } [Test] public void Export_WithHydraulicBoundaryLocationCalculationsForTargetProbabilities_WritesFilesAndReturnsTrue() { // Setup - string folderPath = TestHelper.GetScratchPadPath($"{nameof(HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporterTest)}.{nameof(Export_WithHydraulicBoundaryLocationCalculationsForTargetProbabilities_WritesFilesAndReturnsTrue)}"); - Directory.CreateDirectory(folderPath); + string directoryPath = TestHelper.GetScratchPadPath($"{nameof(Export_WithHydraulicBoundaryLocationCalculationsForTargetProbabilities_WritesFilesAndReturnsTrue)}"); + Directory.CreateDirectory(directoryPath); + string filePath = Path.Combine(directoryPath, "export.zip"); var random = new Random(21); var calculationsForTargetProbabilities = new[] { - new Tuple( - new HydraulicBoundaryLocationCalculationsForTargetProbability(random.NextDouble(0, 0.1)), + new Tuple, double, HydraulicBoundaryLocationCalculationsType>( + new List(), random.NextDouble(0, 0.1), HydraulicBoundaryLocationCalculationsType.WaterLevel), - new Tuple( - new HydraulicBoundaryLocationCalculationsForTargetProbability(random.NextDouble(0, 0.1)), + new Tuple, double, HydraulicBoundaryLocationCalculationsType>( + new List(), random.NextDouble(0, 0.1), HydraulicBoundaryLocationCalculationsType.WaveHeight) }; - var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter(calculationsForTargetProbabilities, folderPath); + var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter(calculationsForTargetProbabilities, filePath); try { @@ -142,43 +143,44 @@ // Assert Assert.IsTrue(isExported); - string[] expectedFilePaths = + string[] expectedFiles = { - Path.Combine(folderPath, $"Waterstanden_{GetReturnPeriodText(calculationsForTargetProbabilities.First().Item1.TargetProbability)}.shp"), - Path.Combine(folderPath, $"Golfhoogten_{GetReturnPeriodText(calculationsForTargetProbabilities.Last().Item1.TargetProbability)}.shp") + $"Waterstanden_{GetReturnPeriodText(calculationsForTargetProbabilities.First().Item2)}.shp", + $"Golfhoogten_{GetReturnPeriodText(calculationsForTargetProbabilities.Last().Item2)}.shp" }; - Assert.IsTrue(expectedFilePaths.All(File.Exists)); + + using (ZipArchive zipArchive = ZipFile.OpenRead(filePath)) + { + CollectionAssert.IsSubsetOf(expectedFiles, zipArchive.Entries.Select(e => e.FullName)); + } } finally { - Directory.Delete(folderPath, true); + Directory.Delete(directoryPath, true); } } [Test] public void Export_WithDoubleHydraulicBoundaryLocationCalculationsForTargetProbabilities_WritesFilesAndReturnsTrue() { // Setup - string folderPath = TestHelper.GetScratchPadPath($"{nameof(HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporterTest)}.{nameof(Export_WithDoubleHydraulicBoundaryLocationCalculationsForTargetProbabilities_WritesFilesAndReturnsTrue)}"); - Directory.CreateDirectory(folderPath); + string directoryPath = TestHelper.GetScratchPadPath($"{nameof(Export_WithDoubleHydraulicBoundaryLocationCalculationsForTargetProbabilities_WritesFilesAndReturnsTrue)}"); + Directory.CreateDirectory(directoryPath); + string filePath = Path.Combine(directoryPath, "export.zip"); var calculationsForTargetProbabilities = new[] { - new Tuple( - new HydraulicBoundaryLocationCalculationsForTargetProbability(0.1), - HydraulicBoundaryLocationCalculationsType.WaterLevel), - new Tuple( - new HydraulicBoundaryLocationCalculationsForTargetProbability(0.1), - HydraulicBoundaryLocationCalculationsType.WaterLevel), - new Tuple( - new HydraulicBoundaryLocationCalculationsForTargetProbability(0.001), - HydraulicBoundaryLocationCalculationsType.WaveHeight), - new Tuple( - new HydraulicBoundaryLocationCalculationsForTargetProbability(0.001), - HydraulicBoundaryLocationCalculationsType.WaveHeight) + new Tuple, double, HydraulicBoundaryLocationCalculationsType>( + new List(), 0.1, HydraulicBoundaryLocationCalculationsType.WaterLevel), + new Tuple, double, HydraulicBoundaryLocationCalculationsType>( + new List(), 0.1, HydraulicBoundaryLocationCalculationsType.WaterLevel), + new Tuple, double, HydraulicBoundaryLocationCalculationsType>( + new List(), 0.001, HydraulicBoundaryLocationCalculationsType.WaveHeight), + new Tuple, double, HydraulicBoundaryLocationCalculationsType>( + new List(), 0.001, HydraulicBoundaryLocationCalculationsType.WaveHeight) }; - var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter(calculationsForTargetProbabilities, folderPath); + var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter(calculationsForTargetProbabilities, filePath); try { @@ -188,74 +190,22 @@ // Assert Assert.IsTrue(isExported); - string[] expectedFilePaths = + string[] expectedFiles = { - Path.Combine(folderPath, "Waterstanden_10.shp"), - Path.Combine(folderPath, "Waterstanden_10 (1).shp"), - Path.Combine(folderPath, "Golfhoogten_1.000.shp"), - Path.Combine(folderPath, "Golfhoogten_1.000 (1).shp") + "Waterstanden_10.shp", + "Waterstanden_10 (1).shp", + "Golfhoogten_1.000.shp", + "Golfhoogten_1.000 (1).shp" }; - Assert.IsTrue(expectedFilePaths.All(File.Exists)); - } - finally - { - Directory.Delete(folderPath, true); - } - } - [Test] - public void Export_WithHydraulicBoundaryLocationCalculationsForTargetProbabilities_LogsMessages() - { - // Setup - string folderPath = TestHelper.GetScratchPadPath($"{nameof(HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporterTest)}.{nameof(Export_WithHydraulicBoundaryLocationCalculationsForTargetProbabilities_LogsMessages)}"); - Directory.CreateDirectory(folderPath); - - var random = new Random(21); - - var calculationsForTargetProbabilities = new[] - { - new Tuple( - new HydraulicBoundaryLocationCalculationsForTargetProbability(random.NextDouble(0, 0.1)), - HydraulicBoundaryLocationCalculationsType.WaterLevel), - new Tuple( - new HydraulicBoundaryLocationCalculationsForTargetProbability(random.NextDouble(0, 0.1)), - HydraulicBoundaryLocationCalculationsType.WaveHeight) - }; - - var exporter = new HydraulicBoundaryLocationCalculationsForTargetProbabilitiesExporter(calculationsForTargetProbabilities, folderPath); - - try - { - // Call - var isExported = false; - void Call() => isExported = exporter.Export(); - - // Assert - string[] expectedCalculationNames = + using (ZipArchive zipArchive = ZipFile.OpenRead(filePath)) { - $"Waterstanden {ProbabilityFormattingHelper.Format(calculationsForTargetProbabilities.First().Item1.TargetProbability)}", - $"Golfhoogten {ProbabilityFormattingHelper.Format(calculationsForTargetProbabilities.Last().Item1.TargetProbability)}" - }; - - string[] expectedFilePaths = - { - Path.Combine(folderPath, $"Waterstanden_{GetReturnPeriodText(calculationsForTargetProbabilities.First().Item1.TargetProbability)}.shp"), - Path.Combine(folderPath, $"Golfhoogten_{GetReturnPeriodText(calculationsForTargetProbabilities.Last().Item1.TargetProbability)}.shp") - }; - - TestHelper.AssertLogMessagesAreGenerated(Call, new[] - { - $"Exporteren van '{expectedCalculationNames[0]}' is gestart.", - $"Gegevens van '{expectedCalculationNames[0]}' zijn geƫxporteerd naar bestand '{expectedFilePaths[0]}'.", - $"Exporteren van '{expectedCalculationNames[1]}' is gestart.", - $"Gegevens van '{expectedCalculationNames[1]}' zijn geƫxporteerd naar bestand '{expectedFilePaths[1]}'." - }); - Assert.IsTrue(isExported); - Assert.IsTrue(expectedFilePaths.All(File.Exists)); + CollectionAssert.IsSubsetOf(expectedFiles, zipArchive.Entries.Select(e => e.FullName)); + } } finally { - Directory.Delete(folderPath, true); + Directory.Delete(directoryPath, true); } } Index: Riskeer/Integration/test/Riskeer.Integration.IO.Test/Riskeer.Integration.IO.Test.csproj =================================================================== diff -u -r08e8d26a0715f0f3db57c1d3e86256aa06934db4 -r63b9c8a671b9f3d9eeb74f9a2cf93e0b8d63d84e --- Riskeer/Integration/test/Riskeer.Integration.IO.Test/Riskeer.Integration.IO.Test.csproj (.../Riskeer.Integration.IO.Test.csproj) (revision 08e8d26a0715f0f3db57c1d3e86256aa06934db4) +++ Riskeer/Integration/test/Riskeer.Integration.IO.Test/Riskeer.Integration.IO.Test.csproj (.../Riskeer.Integration.IO.Test.csproj) (revision 63b9c8a671b9f3d9eeb74f9a2cf93e0b8d63d84e) @@ -8,6 +8,7 @@ +