Index: Riskeer/Integration/src/Riskeer.Integration.IO/Exporters/HydraulicBoundaryLocationCalculationsExporter.cs =================================================================== diff -u -r6f3dc4c0604a47896eb338dd94fcf30080a90924 -r8fa6146e0eb76dc692cc15b9452a022da2b542ff --- Riskeer/Integration/src/Riskeer.Integration.IO/Exporters/HydraulicBoundaryLocationCalculationsExporter.cs (.../HydraulicBoundaryLocationCalculationsExporter.cs) (revision 6f3dc4c0604a47896eb338dd94fcf30080a90924) +++ Riskeer/Integration/src/Riskeer.Integration.IO/Exporters/HydraulicBoundaryLocationCalculationsExporter.cs (.../HydraulicBoundaryLocationCalculationsExporter.cs) (revision 8fa6146e0eb76dc692cc15b9452a022da2b542ff) @@ -22,8 +22,11 @@ using System; using System.Collections.Generic; using Core.Common.Base.IO; +using Core.Common.IO.Exceptions; using Core.Common.Util; +using log4net; using Riskeer.Common.Data.Hydraulics; +using RiskeerCommonIOResources = Riskeer.Common.IO.Properties.Resources; namespace Riskeer.Integration.IO.Exporters { @@ -32,33 +35,56 @@ /// public class HydraulicBoundaryLocationCalculationsExporter : IFileExporter { + private static readonly ILog log = LogManager.GetLogger(typeof(HydraulicBoundaryLocationCalculationsExporter)); + private readonly IEnumerable calculations; private readonly string filePath; + private readonly string outputMetaDataHeader; /// /// Creates a new instance of . /// /// The calculations to export. /// The path of the file to export to. + /// /// Thrown when - /// is null. + /// or is null. /// Thrown when is invalid. - public HydraulicBoundaryLocationCalculationsExporter(IEnumerable calculations, string filePath) + public HydraulicBoundaryLocationCalculationsExporter(IEnumerable calculations, + string filePath, string outputMetaDataHeader) { if (calculations == null) { throw new ArgumentNullException(nameof(calculations)); } + if (outputMetaDataHeader == null) + { + throw new ArgumentNullException(nameof(outputMetaDataHeader)); + } + IOUtils.ValidateFilePath(filePath); - + this.calculations = calculations; this.filePath = filePath; + this.outputMetaDataHeader = outputMetaDataHeader; } public bool Export() { - return false; + try + { + HydraulicBoundaryLocationCalculationsWriter.WriteHydraulicBoundaryLocationCalculations( + calculations, filePath, outputMetaDataHeader); + } + catch (CriticalFileWriteException e) + { + log.ErrorFormat(RiskeerCommonIOResources.HydraulicBoundaryLocationsExporter_Error_Exception_0_no_HydraulicBoundaryLocations_exported, + e.Message); + return false; + } + + return true; } } } \ No newline at end of file Index: Riskeer/Integration/test/Riskeer.Integration.IO.Test/Exporters/HydraulicBoundaryLocationCalculationsExporterTest.cs =================================================================== diff -u -r6f3dc4c0604a47896eb338dd94fcf30080a90924 -r8fa6146e0eb76dc692cc15b9452a022da2b542ff --- Riskeer/Integration/test/Riskeer.Integration.IO.Test/Exporters/HydraulicBoundaryLocationCalculationsExporterTest.cs (.../HydraulicBoundaryLocationCalculationsExporterTest.cs) (revision 6f3dc4c0604a47896eb338dd94fcf30080a90924) +++ Riskeer/Integration/test/Riskeer.Integration.IO.Test/Exporters/HydraulicBoundaryLocationCalculationsExporterTest.cs (.../HydraulicBoundaryLocationCalculationsExporterTest.cs) (revision 8fa6146e0eb76dc692cc15b9452a022da2b542ff) @@ -22,10 +22,12 @@ using System; using System.IO; using System.Linq; +using System.Security.AccessControl; using Core.Common.Base.IO; using Core.Common.TestUtil; using NUnit.Framework; using Riskeer.Common.Data.Hydraulics; +using Riskeer.Common.IO.TestUtil; using Riskeer.Integration.IO.Exporters; namespace Riskeer.Integration.IO.Test.Exporters @@ -37,24 +39,36 @@ public void Constructor_CalculationsNull_ThrowsArgumentNullException() { // Call - void Call() => new HydraulicBoundaryLocationCalculationsExporter(null, string.Empty); + void Call() => new HydraulicBoundaryLocationCalculationsExporter(null, string.Empty, string.Empty); // Assert var exception = Assert.Throws(Call); Assert.AreEqual("calculations", exception.ParamName); } - + [Test] + public void Constructor_OutputMetaDataHeaderNull_ThrowsArgumentNullException() + { + // Call + void Call() => new HydraulicBoundaryLocationCalculationsExporter( + Enumerable.Empty(), string.Empty, null); + + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual("outputMetaDataHeader", exception.ParamName); + } + + [Test] public void Constructor_FilePathNull_ThrowsArgumentException() { // Call void Call() => new HydraulicBoundaryLocationCalculationsExporter( - Enumerable.Empty(), null); + Enumerable.Empty(), null, string.Empty); // Assert Assert.Throws(Call); } - + [Test] public void Constructor_ExpectedValues() { @@ -63,10 +77,84 @@ // Call var exporter = new HydraulicBoundaryLocationCalculationsExporter( - Enumerable.Empty(), filePath); + Enumerable.Empty(), filePath, string.Empty); // Assert Assert.IsInstanceOf(exporter); } + + [Test] + public void Export_ValidData_ReturnsTrueAndWritesCorrectData() + { + // Setup + const string fileName = "test"; + + string directoryPath = TestHelper.GetScratchPadPath(nameof(Export_ValidData_ReturnsTrueAndWritesCorrectData)); + Directory.CreateDirectory(directoryPath); + string filePath = Path.Combine(directoryPath, $"{fileName}.shp"); + + var exporter = new HydraulicBoundaryLocationCalculationsExporter(new[] + { + new HydraulicBoundaryLocationCalculation(new HydraulicBoundaryLocation(123, "aName", 1.1, 2.2)) + }, filePath, "Waterlevel"); + + // 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(HydraulicBoundaryLocationCalculationsExporter)), + "ExpectedExport", 28, 8, 628); + Assert.IsTrue(isExported); + } + finally + { + Directory.Delete(directoryPath, true); + } + } + + [Test] + public void Export_InvalidDirectoryRights_LogErrorAndReturnFalse() + { + // Setup + const string fileName = "test"; + + string directoryPath = TestHelper.GetScratchPadPath(nameof(Export_InvalidDirectoryRights_LogErrorAndReturnFalse)); + Directory.CreateDirectory(directoryPath); + string filePath = Path.Combine(directoryPath, $"{fileName}.shp"); + + var exporter = new HydraulicBoundaryLocationCalculationsExporter(new[] + { + new HydraulicBoundaryLocationCalculation(new HydraulicBoundaryLocation(123, "aName", 1.1, 2.2)) + }, filePath, "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/HydraulicBoundaryLocationCalculationsExporter/ExpectedExport.dbf =================================================================== diff -u Binary files differ Index: Riskeer/Integration/test/Riskeer.Integration.IO.Test/test-data/HydraulicBoundaryLocationCalculationsExporter/ExpectedExport.shp =================================================================== diff -u Binary files differ Index: Riskeer/Integration/test/Riskeer.Integration.IO.Test/test-data/HydraulicBoundaryLocationCalculationsExporter/ExpectedExport.shx =================================================================== diff -u Binary files differ