Index: Riskeer/MacroStabilityInwards/src/Riskeer.MacroStabilityInwards.IO/Exporters/MacroStabilityInwardsCalculationGroupExporter.cs =================================================================== diff -u -r349fb98a7545060b1dd16dfb4298997fa9ad9461 -rbb94fb97e474cdc6877fffc97a4e186bfda61469 --- Riskeer/MacroStabilityInwards/src/Riskeer.MacroStabilityInwards.IO/Exporters/MacroStabilityInwardsCalculationGroupExporter.cs (.../MacroStabilityInwardsCalculationGroupExporter.cs) (revision 349fb98a7545060b1dd16dfb4298997fa9ad9461) +++ Riskeer/MacroStabilityInwards/src/Riskeer.MacroStabilityInwards.IO/Exporters/MacroStabilityInwardsCalculationGroupExporter.cs (.../MacroStabilityInwardsCalculationGroupExporter.cs) (revision bb94fb97e474cdc6877fffc97a4e186bfda61469) @@ -22,6 +22,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using Components.Persistence.Stability; using Core.Common.Base.Data; using Core.Common.Base.IO; @@ -110,7 +111,7 @@ { switch (calculationItem) { - case CalculationGroup nestedGroup: + case CalculationGroup nestedGroup when HasChildrenWithOutput(nestedGroup): continueExport = ExportCalculationGroup(nestedGroup, currentFolderPath, exportedGroups); break; case MacroStabilityInwardsCalculation calculation when !calculation.HasOutput: @@ -130,6 +131,13 @@ return true; } + private static bool HasChildrenWithOutput(CalculationGroup nestedGroup) + { + MacroStabilityInwardsCalculation[] calculations = nestedGroup.Children.OfType() + .ToArray(); + return calculations.Any() && calculations.All(calculation => calculation.HasOutput); + } + private static void CreateDirectory(string currentFolderPath) { if (!Directory.Exists(currentFolderPath)) Index: Riskeer/MacroStabilityInwards/test/Riskeer.MacroStabilityInwards.IO.Test/Exporters/MacroStabilityInwardsCalculationGroupExporterTest.cs =================================================================== diff -u -r02bb3f034254e688fb76ea43a0be6c0d406a44d6 -rbb94fb97e474cdc6877fffc97a4e186bfda61469 --- Riskeer/MacroStabilityInwards/test/Riskeer.MacroStabilityInwards.IO.Test/Exporters/MacroStabilityInwardsCalculationGroupExporterTest.cs (.../MacroStabilityInwardsCalculationGroupExporterTest.cs) (revision 02bb3f034254e688fb76ea43a0be6c0d406a44d6) +++ Riskeer/MacroStabilityInwards/test/Riskeer.MacroStabilityInwards.IO.Test/Exporters/MacroStabilityInwardsCalculationGroupExporterTest.cs (.../MacroStabilityInwardsCalculationGroupExporterTest.cs) (revision bb94fb97e474cdc6877fffc97a4e186bfda61469) @@ -440,6 +440,174 @@ } [Test] + public void Export_NestedCalculationGroupWithoutCalculations_FolderNotExportedAndReturnsTrue() + { + // Setup + string folderPath = TestHelper.GetScratchPadPath($"{nameof(MacroStabilityInwardsCalculationGroupExporterTest)}.{nameof(Export_CalculationGroupsWithSameName_WritesFilesAndReturnsTrue)}"); + Directory.CreateDirectory(folderPath); + + var rootGroup = new CalculationGroup + { + Name = "root" + }; + var nestedGroup1 = new CalculationGroup + { + Name = "group1" + }; + rootGroup.Children.Add(nestedGroup1); + + var exporter = new MacroStabilityInwardsCalculationGroupExporter(rootGroup, new PersistenceFactory(), folderPath, fileExtension, c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); + + try + { + using (new MacroStabilityInwardsCalculatorFactoryConfig()) + { + // Call + bool exportResult = exporter.Export(); + + // Assert + Assert.IsTrue(exportResult); + Assert.IsFalse(Directory.Exists(Path.Combine(folderPath, nestedGroup1.Name))); + } + } + finally + { + Directory.Delete(folderPath, true); + } + } + + [Test] + public void Export_NestedCalculationGroupWithEmptyCalculationGroups_FolderNotExportedAndReturnsTrue() + { + // Setup + string folderPath = TestHelper.GetScratchPadPath($"{nameof(MacroStabilityInwardsCalculationGroupExporterTest)}.{nameof(Export_NestedCalculationGroupWithEmptyCalculationGroups_FolderNotExportedAndReturnsTrue)}"); + Directory.CreateDirectory(folderPath); + + var rootGroup = new CalculationGroup + { + Name = "root" + }; + var nestedGroup1 = new CalculationGroup + { + Name = "group1" + }; + var nestedGroup2 = new CalculationGroup + { + Name = "group2" + }; + nestedGroup1.Children.Add(nestedGroup2); + rootGroup.Children.Add(nestedGroup1); + + var exporter = new MacroStabilityInwardsCalculationGroupExporter(rootGroup, new PersistenceFactory(), folderPath, fileExtension, c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); + + try + { + using (new MacroStabilityInwardsCalculatorFactoryConfig()) + { + // Call + bool exportResult = exporter.Export(); + + // Assert + Assert.IsTrue(exportResult); + Assert.IsFalse(Directory.Exists(Path.Combine(folderPath, nestedGroup1.Name, nestedGroup2.Name))); + Assert.IsFalse(Directory.Exists(Path.Combine(folderPath, nestedGroup1.Name))); + } + } + finally + { + Directory.Delete(folderPath, true); + } + } + + [Test] + public void Export_NestedCalculationGroupWithCalculationsWithoutOutput_FolderNotExportedAndReturnsTrue() + { + // Setup + string folderPath = TestHelper.GetScratchPadPath($"{nameof(MacroStabilityInwardsCalculationGroupExporterTest)}.{nameof(Export_NestedCalculationGroupWithCalculationsWithoutOutput_FolderNotExportedAndReturnsTrue)}"); + Directory.CreateDirectory(folderPath); + + MacroStabilityInwardsCalculationScenario calculation1 = CreateCalculation("calculation1", false); + + var rootGroup = new CalculationGroup + { + Name = "root" + }; + var nestedGroup1 = new CalculationGroup + { + Name = "group1" + }; + nestedGroup1.Children.Add(calculation1); + rootGroup.Children.Add(nestedGroup1); + + var exporter = new MacroStabilityInwardsCalculationGroupExporter(rootGroup, new PersistenceFactory(), folderPath, fileExtension, c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); + + try + { + using (new MacroStabilityInwardsCalculatorFactoryConfig()) + { + // Call + bool exportResult = exporter.Export(); + + // Assert + Assert.IsTrue(exportResult); + Assert.IsFalse(Directory.Exists(Path.Combine(folderPath, nestedGroup1.Name))); + } + } + finally + { + Directory.Delete(folderPath, true); + } + } + + [Test] + public void Export_NestedCalculationGroupWithGroupsWithCalculationsWithoutOutput_FolderNotExportedAndReturnsTrue() + { + // Setup + string folderPath = TestHelper.GetScratchPadPath($"{nameof(MacroStabilityInwardsCalculationGroupExporterTest)}.{nameof(Export_NestedCalculationGroupWithGroupsWithCalculationsWithoutOutput_FolderNotExportedAndReturnsTrue)}"); + Directory.CreateDirectory(folderPath); + + MacroStabilityInwardsCalculationScenario calculation1 = CreateCalculation("calculation1", false); + MacroStabilityInwardsCalculationScenario calculation2 = CreateCalculation("calculation2", false); + + var rootGroup = new CalculationGroup + { + Name = "root" + }; + var nestedGroup1 = new CalculationGroup + { + Name = "group1" + }; + var nestedGroup2 = new CalculationGroup + { + Name = "group2" + }; + nestedGroup2.Children.Add(calculation2); + nestedGroup1.Children.Add(calculation1); + nestedGroup1.Children.Add(nestedGroup2); + rootGroup.Children.Add(nestedGroup1); + + var exporter = new MacroStabilityInwardsCalculationGroupExporter(rootGroup, new PersistenceFactory(), folderPath, fileExtension, c => AssessmentSectionTestHelper.GetTestAssessmentLevel()); + + try + { + using (new MacroStabilityInwardsCalculatorFactoryConfig()) + { + // Call + bool exportResult = exporter.Export(); + + // Assert + Assert.IsTrue(exportResult); + Assert.IsFalse(Directory.Exists(Path.Combine(folderPath, nestedGroup1.Name, nestedGroup2.Name))); + Assert.IsFalse(Directory.Exists(Path.Combine(folderPath, nestedGroup1.Name))); + } + } + finally + { + Directory.Delete(folderPath, true); + } + } + + [Test] public void Export_ErrorDuringSingleCalculationExport_LogsErrorAndReturnsFalse() { // Setup