Index: Riskeer/MacroStabilityInwards/src/Riskeer.MacroStabilityInwards.IO/Exporters/MacroStabilityInwardsCalculationGroupExporter.cs =================================================================== diff -u -rd18dd79703a120a289e1bffff72a03ea0a49be60 -r9bc99bcba3174753901344b318919aba37c9f899 --- Riskeer/MacroStabilityInwards/src/Riskeer.MacroStabilityInwards.IO/Exporters/MacroStabilityInwardsCalculationGroupExporter.cs (.../MacroStabilityInwardsCalculationGroupExporter.cs) (revision d18dd79703a120a289e1bffff72a03ea0a49be60) +++ Riskeer/MacroStabilityInwards/src/Riskeer.MacroStabilityInwards.IO/Exporters/MacroStabilityInwardsCalculationGroupExporter.cs (.../MacroStabilityInwardsCalculationGroupExporter.cs) (revision 9bc99bcba3174753901344b318919aba37c9f899) @@ -20,6 +20,7 @@ // All rights reserved. using System; +using System.IO; using Components.Persistence.Stability; using Core.Common.Base.Data; using Core.Common.Base.IO; @@ -37,6 +38,7 @@ private readonly CalculationGroup calculationGroup; private readonly IPersistenceFactory persistenceFactory; private readonly string folderPath; + private readonly string fileExtension; private readonly Func getNormativeAssessmentLevelFunc; /// @@ -45,6 +47,7 @@ /// The calculation group to export. /// The persistence factory to use. /// The folder path to export to. + /// The extension of the files. /// /// for obtaining the normative assessment level. /// Thrown when , @@ -56,8 +59,8 @@ /// does not contain an invalid character, /// is not too long. /// - public MacroStabilityInwardsCalculationGroupExporter(CalculationGroup calculationGroup, IPersistenceFactory persistenceFactory, - string folderPath, Func getNormativeAssessmentLevelFunc) + public MacroStabilityInwardsCalculationGroupExporter(CalculationGroup calculationGroup, IPersistenceFactory persistenceFactory, string folderPath, + string fileExtension, Func getNormativeAssessmentLevelFunc) { if (calculationGroup == null) { @@ -79,12 +82,31 @@ this.calculationGroup = calculationGroup; this.persistenceFactory = persistenceFactory; this.folderPath = folderPath; + this.fileExtension = fileExtension; this.getNormativeAssessmentLevelFunc = getNormativeAssessmentLevelFunc; } public bool Export() { - return false; + var exportSucceeded = true; + + foreach (ICalculationBase calculationItem in calculationGroup.Children) + { + if (calculationItem is MacroStabilityInwardsCalculation calculation) + { + exportSucceeded = Export(calculation); + } + } + + return exportSucceeded; } + + private bool Export(MacroStabilityInwardsCalculation calculation) + { + string fileNameWithExtension = $"{calculation.Name}.{fileExtension}"; + var exporter = new MacroStabilityInwardsCalculationExporter(calculation, persistenceFactory, Path.Combine(folderPath, fileNameWithExtension), + () => getNormativeAssessmentLevelFunc(calculation)); + return exporter.Export(); + } } } \ No newline at end of file Index: Riskeer/MacroStabilityInwards/src/Riskeer.MacroStabilityInwards.Plugin/MacroStabilityInwardsPlugin.cs =================================================================== diff -u -rd18dd79703a120a289e1bffff72a03ea0a49be60 -r9bc99bcba3174753901344b318919aba37c9f899 --- Riskeer/MacroStabilityInwards/src/Riskeer.MacroStabilityInwards.Plugin/MacroStabilityInwardsPlugin.cs (.../MacroStabilityInwardsPlugin.cs) (revision d18dd79703a120a289e1bffff72a03ea0a49be60) +++ Riskeer/MacroStabilityInwards/src/Riskeer.MacroStabilityInwards.Plugin/MacroStabilityInwardsPlugin.cs (.../MacroStabilityInwardsPlugin.cs) (revision 9bc99bcba3174753901344b318919aba37c9f899) @@ -183,9 +183,9 @@ yield return new ExportInfo { Name = Resources.MacroStabilityInwardsCalculationExporter_DisplayName, - Extension = Resources.Stix_file_filter_extension, - CreateFileExporter = (context, filePath) => new MacroStabilityInwardsCalculationGroupExporter(context.WrappedData, new PersistenceFactory(), filePath, - (calculation) => GetNormativeAssessmentLevel(context.AssessmentSection, calculation)), + Extension = StixFileFilterExtension, + CreateFileExporter = (context, filePath) => new MacroStabilityInwardsCalculationGroupExporter(context.WrappedData, new PersistenceFactory(), filePath, StixFileFilterExtension, + calculation => GetNormativeAssessmentLevel(context.AssessmentSection, calculation)), IsEnabled = context => context.WrappedData.HasOutput(), GetExportPath = () => ExportHelper.GetFolderPath(GetInquiryHelper()) }; @@ -430,6 +430,12 @@ }; } + #region ExportInfos + + private static string StixFileFilterExtension => Resources.Stix_file_filter_extension; + + #endregion + private static RoundedDouble GetNormativeAssessmentLevel(IAssessmentSection assessmentSection, MacroStabilityInwardsCalculation calculation) { return assessmentSection.GetNormativeAssessmentLevel(calculation.InputParameters.HydraulicBoundaryLocation); Index: Riskeer/MacroStabilityInwards/test/Riskeer.MacroStabilityInwards.IO.Test/Exporters/MacroStabilityInwardsCalculationGroupExporterTest.cs =================================================================== diff -u -rd18dd79703a120a289e1bffff72a03ea0a49be60 -r9bc99bcba3174753901344b318919aba37c9f899 --- Riskeer/MacroStabilityInwards/test/Riskeer.MacroStabilityInwards.IO.Test/Exporters/MacroStabilityInwardsCalculationGroupExporterTest.cs (.../MacroStabilityInwardsCalculationGroupExporterTest.cs) (revision d18dd79703a120a289e1bffff72a03ea0a49be60) +++ Riskeer/MacroStabilityInwards/test/Riskeer.MacroStabilityInwards.IO.Test/Exporters/MacroStabilityInwardsCalculationGroupExporterTest.cs (.../MacroStabilityInwardsCalculationGroupExporterTest.cs) (revision 9bc99bcba3174753901344b318919aba37c9f899) @@ -1,4 +1,4 @@ -// Copyright (C) Stichting Deltares 2019. All rights reserved. +// Copyright (C) Stichting Deltares 2019. All rights reserved. // // This file is part of Riskeer. // @@ -20,19 +20,27 @@ // All rights reserved. using System; +using System.IO; using Components.Persistence.Stability; using Core.Common.Base.IO; +using Core.Common.TestUtil; using NUnit.Framework; using Rhino.Mocks; using Riskeer.Common.Data.Calculation; using Riskeer.Common.Data.TestUtil; +using Riskeer.MacroStabilityInwards.Data; +using Riskeer.MacroStabilityInwards.Data.TestUtil; using Riskeer.MacroStabilityInwards.IO.Exporters; +using Riskeer.MacroStabilityInwards.IO.TestUtil; +using Riskeer.MacroStabilityInwards.KernelWrapper.TestUtil.Calculators; namespace Riskeer.MacroStabilityInwards.IO.Test.Exporters { [TestFixture] public class MacroStabilityInwardsCalculationGroupExporterTest { + private const string fileExtension = "stix"; + [Test] public void Constructor_ExpectedValues() { @@ -42,7 +50,7 @@ mocks.ReplayAll(); // Call - var exporter = new MacroStabilityInwardsCalculationGroupExporter(new CalculationGroup(), persistenceFactory, "ValidFolderPath", c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); + var exporter = new MacroStabilityInwardsCalculationGroupExporter(new CalculationGroup(), persistenceFactory, "ValidFolderPath", "extension", c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); // Assert Assert.IsInstanceOf(exporter); @@ -58,7 +66,7 @@ mocks.ReplayAll(); // Call - void Call() => new MacroStabilityInwardsCalculationGroupExporter(null, persistenceFactory, string.Empty, c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); + void Call() => new MacroStabilityInwardsCalculationGroupExporter(null, persistenceFactory, string.Empty, string.Empty, c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); // Assert var exception = Assert.Throws(Call); @@ -70,7 +78,7 @@ public void Constructor_PersistenceFactoryNull_ThrowsArgumentNullException() { // Call - void Call() => new MacroStabilityInwardsCalculationGroupExporter(new CalculationGroup(), null, string.Empty, c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); + void Call() => new MacroStabilityInwardsCalculationGroupExporter(new CalculationGroup(), null, string.Empty, string.Empty, c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); // Assert var exception = Assert.Throws(Call); @@ -86,7 +94,7 @@ mocks.ReplayAll(); // Call - void Call() => new MacroStabilityInwardsCalculationGroupExporter(new CalculationGroup(), persistenceFactory, string.Empty, null); + void Call() => new MacroStabilityInwardsCalculationGroupExporter(new CalculationGroup(), persistenceFactory, string.Empty, string.Empty, null); // Assert var exception = Assert.Throws(Call); @@ -107,29 +115,93 @@ mocks.ReplayAll(); // Call - void Call() => new MacroStabilityInwardsCalculationGroupExporter(new CalculationGroup(), persistenceFactory, folderPath, c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); + void Call() => new MacroStabilityInwardsCalculationGroupExporter(new CalculationGroup(), persistenceFactory, folderPath, fileExtension, c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); // Assert Assert.Throws(Call); mocks.VerifyAll(); } [Test] - public void Export_Always_ReturnsFalse() + public void Export_CalculationExporterReturnsFalse_ReturnsFalse() { // Setup - var mocks = new MockRepository(); - var persistenceFactory = mocks.Stub(); - mocks.ReplayAll(); + string folderPath = TestHelper.GetScratchPadPath($"{nameof(MacroStabilityInwardsCalculationGroupExporterTest)}.{nameof(Export_CalculationExporterReturnsFalse_ReturnsFalse)}"); + Directory.CreateDirectory(folderPath); - var exporter = new MacroStabilityInwardsCalculationGroupExporter(new CalculationGroup(), persistenceFactory, "ValidFolderPath", c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); + MacroStabilityInwardsCalculationScenario calculation = MacroStabilityInwardsCalculationScenarioTestFactory.CreateMacroStabilityInwardsCalculationScenarioWithValidInput(new TestHydraulicBoundaryLocation()); + calculation.Output = MacroStabilityInwardsOutputTestFactory.CreateRandomOutput(); - // Call - bool exportResult = exporter.Export(); + var calculationGroup = new CalculationGroup(); + calculationGroup.Children.Add(calculation); - // Assert - Assert.IsFalse(exportResult); - mocks.VerifyAll(); + var persistenceFactory = new MacroStabilityInwardsTestPersistenceFactory + { + ThrowException = true + }; + + var exporter = new MacroStabilityInwardsCalculationGroupExporter(calculationGroup, persistenceFactory, folderPath, fileExtension, c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); + + try + { + // Call + var exportResult = true; + void Call() => exportResult = exporter.Export(); + + // Assert + string filePath = Path.Combine(folderPath, $"{calculation.Name}.stix"); + string expectedMessage = $"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'. Er is geen D-GEO Suite Stability Project geëxporteerd."; + TestHelper.AssertLogMessageWithLevelIsGenerated(Call, new Tuple(expectedMessage, LogLevelConstant.Error)); + Assert.IsFalse(exportResult); + } + finally + { + Directory.Delete(folderPath, true); + } } + + [Test] + public void Export_RunsSuccessful_WritesFilesAndRemovesTempFile() + { + // Setup + string folderPath = TestHelper.GetScratchPadPath($"{nameof(MacroStabilityInwardsCalculationGroupExporterTest)}.{nameof(Export_RunsSuccessful_WritesFilesAndRemovesTempFile)}"); + Directory.CreateDirectory(folderPath); + + MacroStabilityInwardsCalculationScenario calculation1 = MacroStabilityInwardsCalculationScenarioTestFactory.CreateMacroStabilityInwardsCalculationScenarioWithValidInput(new TestHydraulicBoundaryLocation()); + calculation1.Name = "Calculation1"; + calculation1.Output = MacroStabilityInwardsOutputTestFactory.CreateRandomOutput(); + MacroStabilityInwardsCalculationScenario calculation2 = MacroStabilityInwardsCalculationScenarioTestFactory.CreateMacroStabilityInwardsCalculationScenarioWithValidInput(new TestHydraulicBoundaryLocation()); + calculation2.Name = "Calculation2"; + calculation2.Output = MacroStabilityInwardsOutputTestFactory.CreateRandomOutput(); + + var calculationGroup = new CalculationGroup(); + calculationGroup.Children.Add(calculation1); + calculationGroup.Children.Add(calculation2); + + var exporter = new MacroStabilityInwardsCalculationGroupExporter(calculationGroup, new PersistenceFactory(), folderPath, fileExtension, c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); + + try + { + using (new MacroStabilityInwardsCalculatorFactoryConfig()) + { + // Call + bool exportResult = exporter.Export(); + + // Assert + Assert.IsTrue(exportResult); + string filePath1 = Path.Combine(folderPath, $"{calculation1.Name}.{fileExtension}"); + Assert.IsTrue(File.Exists(filePath1)); + Assert.IsFalse(File.Exists($"{filePath1}.temp")); + + string filePath2 = Path.Combine(folderPath, $"{calculation2.Name}.{fileExtension}"); + Assert.IsTrue(File.Exists(filePath2)); + Assert.IsFalse(File.Exists($"{filePath2}.temp")); + } + } + finally + { + Directory.Delete(folderPath, true); + } + } } } \ No newline at end of file