Index: Riskeer/AssemblyTool/test/Riskeer.AssemblyTool.IO.TestUtil/ExportableFailureMechanismSectionTestFactory.cs =================================================================== diff -u -r54be8d72ca7e4cea39fe62f9ed711cc4ab53b4c9 -r1350d75cb215540cbef102a35923ea3bdf09a51e --- Riskeer/AssemblyTool/test/Riskeer.AssemblyTool.IO.TestUtil/ExportableFailureMechanismSectionTestFactory.cs (.../ExportableFailureMechanismSectionTestFactory.cs) (revision 54be8d72ca7e4cea39fe62f9ed711cc4ab53b4c9) +++ Riskeer/AssemblyTool/test/Riskeer.AssemblyTool.IO.TestUtil/ExportableFailureMechanismSectionTestFactory.cs (.../ExportableFailureMechanismSectionTestFactory.cs) (revision 1350d75cb215540cbef102a35923ea3bdf09a51e) @@ -53,11 +53,24 @@ /// A default instance of . public static ExportableFailureMechanismSection CreateExportableFailureMechanismSection() { + return CreateExportableFailureMechanismSection(1, 2); + } + + /// + /// Creates a with a given start and end distance. + /// + /// The start distance of the failure mechanism section between the section + /// and the start of the reference line in meters. + /// The end distance of the failure mechanism section between the section + /// and the start of the reference line in meters. + /// A . + public static ExportableFailureMechanismSection CreateExportableFailureMechanismSection(double startDistance, double endDistance) + { return new ExportableFailureMechanismSection("id", new[] { new Point2D(1, 1), new Point2D(2, 2) - }, 1, 2); + }, startDistance, endDistance); } /// @@ -66,11 +79,24 @@ /// A default instance of . public static ExportableCombinedFailureMechanismSection CreateExportableCombinedFailureMechanismSection() { + return CreateExportableCombinedFailureMechanismSection(1, 3); + } + + /// + /// Creates a with a given start and end distance. + /// + /// The start distance of the failure mechanism section between the section + /// and the start of the reference line in meters. + /// The end distance of the failure mechanism section between the section + /// and the start of the reference line in meters. + /// A . + public static ExportableCombinedFailureMechanismSection CreateExportableCombinedFailureMechanismSection(double startDistance, double endDistance) + { return new ExportableCombinedFailureMechanismSection("id", new[] { new Point2D(1, 1), new Point2D(3, 3) - }, 1, 3, ExportableAssemblyMethod.BOI3A1); + }, startDistance, endDistance, ExportableAssemblyMethod.BOI3A1); } } } \ No newline at end of file Index: Riskeer/Integration/src/Riskeer.Integration.IO/Helpers/ExportableCombinedFailureMechanismSectionHelper.cs =================================================================== diff -u --- Riskeer/Integration/src/Riskeer.Integration.IO/Helpers/ExportableCombinedFailureMechanismSectionHelper.cs (revision 0) +++ Riskeer/Integration/src/Riskeer.Integration.IO/Helpers/ExportableCombinedFailureMechanismSectionHelper.cs (revision 1350d75cb215540cbef102a35923ea3bdf09a51e) @@ -0,0 +1,87 @@ +// Copyright (C) Stichting Deltares 2022. 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 Riskeer.AssemblyTool.IO.Model; +using Riskeer.Common.Data.FailureMechanism; +using Riskeer.Integration.IO.Exceptions; + +namespace Riskeer.Integration.IO.Helpers +{ + /// + /// Helper class for the . + /// + public static class ExportableCombinedFailureMechanismSectionHelper + { + /// + /// Gets the matching that contains a section corresponding with + /// the . + /// + /// The that keeps track of the created items. + /// The collection of . + /// The to + /// find the corresponding for. + /// The with a section that corresponds with + /// . + /// Thrown when any argument is null. + /// Thrown when no matching + /// could be found. + public static ExportableFailureMechanismSectionAssemblyResult GetExportableFailureMechanismSectionAssemblyResult( + ExportableModelRegistry registry, IEnumerable sectionResults, + ExportableCombinedFailureMechanismSection exportableCombinedFailureMechanismSection) + { + if (registry == null) + { + throw new ArgumentNullException(nameof(registry)); + } + + if (sectionResults == null) + { + throw new ArgumentNullException(nameof(sectionResults)); + } + + if (exportableCombinedFailureMechanismSection == null) + { + throw new ArgumentNullException(nameof(exportableCombinedFailureMechanismSection)); + } + + foreach (FailureMechanismSectionResult sectionResult in sectionResults) + { + ExportableFailureMechanismSectionAssemblyResult exportableSectionResult = registry.Get(sectionResult); + ExportableFailureMechanismSection exportableFailureMechanismSection = exportableSectionResult.FailureMechanismSection; + if (IsMatchingSection(exportableFailureMechanismSection, exportableCombinedFailureMechanismSection)) + { + return exportableSectionResult; + } + } + + throw new AssemblyFactoryException($"No matching {typeof(ExportableFailureMechanismSectionAssemblyResult)} was found for the {nameof(exportableCombinedFailureMechanismSection)}."); + } + + private static bool IsMatchingSection(ExportableFailureMechanismSection exportableFailureMechanismSection, + ExportableCombinedFailureMechanismSection exportableCombinedFailureMechanismSection) + { + return exportableCombinedFailureMechanismSection.StartDistance >= exportableFailureMechanismSection.StartDistance + && exportableCombinedFailureMechanismSection.EndDistance <= exportableFailureMechanismSection.EndDistance; + } + } +} \ No newline at end of file Index: Riskeer/Integration/test/Riskeer.Integration.IO.Test/Helpers/ExportableCombinedFailureMechanismSectionHelperTest.cs =================================================================== diff -u --- Riskeer/Integration/test/Riskeer.Integration.IO.Test/Helpers/ExportableCombinedFailureMechanismSectionHelperTest.cs (revision 0) +++ Riskeer/Integration/test/Riskeer.Integration.IO.Test/Helpers/ExportableCombinedFailureMechanismSectionHelperTest.cs (revision 1350d75cb215540cbef102a35923ea3bdf09a51e) @@ -0,0 +1,148 @@ +// Copyright (C) Stichting Deltares 2022. 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.Linq; +using NUnit.Framework; +using Riskeer.AssemblyTool.IO.Model; +using Riskeer.AssemblyTool.IO.TestUtil; +using Riskeer.Common.Data.FailureMechanism; +using Riskeer.Common.Data.TestUtil; +using Riskeer.Integration.IO.Exceptions; +using Riskeer.Integration.IO.Helpers; + +namespace Riskeer.Integration.IO.Test.Helpers +{ + [TestFixture] + public class ExportableCombinedFailureMechanismSectionHelperTest + { + [Test] + public void GetExportableFailureMechanismSectionAssemblyResult_RegistryNull_ThrowsArgumentNullException() + { + // Call + void Call() => ExportableCombinedFailureMechanismSectionHelper.GetExportableFailureMechanismSectionAssemblyResult( + null, Enumerable.Empty(), ExportableFailureMechanismSectionTestFactory.CreateExportableCombinedFailureMechanismSection()); + + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual("registry", exception.ParamName); + } + + [Test] + public void GetExportableFailureMechanismSectionAssemblyResult_SectionResultsNull_ThrowsArgumentNullException() + { + // Call + void Call() => ExportableCombinedFailureMechanismSectionHelper.GetExportableFailureMechanismSectionAssemblyResult( + new ExportableModelRegistry(), null, ExportableFailureMechanismSectionTestFactory.CreateExportableCombinedFailureMechanismSection()); + + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual("sectionResults", exception.ParamName); + } + + [Test] + public void GetExportableFailureMechanismSectionAssemblyResult_ExportableCombinedFailureMechanismSectionNull_ThrowsArgumentNullException() + { + // Call + void Call() => ExportableCombinedFailureMechanismSectionHelper.GetExportableFailureMechanismSectionAssemblyResult( + new ExportableModelRegistry(), Enumerable.Empty(), null); + + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual("exportableCombinedFailureMechanismSection", exception.ParamName); + } + + [Test] + [TestCaseSource(nameof(GetSectionConfigurations))] + public void GetExportableFailureMechanismSectionAssemblyResult_WithVaryingCombinedSection_ReturnsExpectedAssemblyResult( + ExportableModelRegistry registry, IEnumerable sectionResults, + ExportableCombinedFailureMechanismSection combinedFailureMechanismSection, ExportableFailureMechanismSectionAssemblyResult expectedResult) + { + // Call + ExportableFailureMechanismSectionAssemblyResult result = + ExportableCombinedFailureMechanismSectionHelper.GetExportableFailureMechanismSectionAssemblyResult( + registry, sectionResults, combinedFailureMechanismSection); + + // Assert + Assert.AreSame(expectedResult, result); + } + + [Test] + public void GetExportableFailureMechanismSectionAssemblyResult_WithNoMatchingExportableFailureMechanismSection_ThrowsAssemblyFactoryException() + { + // Setup + AdoptableFailureMechanismSectionResult[] sectionResults = + { + FailureMechanismSectionResultTestFactory.CreateFailureMechanismSectionResult() + }; + + var registry = new ExportableModelRegistry(); + registry.Register(sectionResults[0], CreateResult(1, 2)); + + ExportableCombinedFailureMechanismSection combinedFailureMechanismSection = + ExportableFailureMechanismSectionTestFactory.CreateExportableCombinedFailureMechanismSection(3, 4); + + // Call + void Call() => ExportableCombinedFailureMechanismSectionHelper.GetExportableFailureMechanismSectionAssemblyResult( + registry, sectionResults, combinedFailureMechanismSection); + + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual($"No matching {typeof(ExportableFailureMechanismSectionAssemblyResult)} was found for the exportableCombinedFailureMechanismSection.", + exception.Message); + } + + private static ExportableFailureMechanismSectionAssemblyResult CreateResult(double startDistance, double endDistance) + { + ExportableFailureMechanismSection exportableSection = + ExportableFailureMechanismSectionTestFactory.CreateExportableFailureMechanismSection(startDistance, endDistance); + return ExportableFailureMechanismSectionAssemblyResultTestFactory.Create(exportableSection, 1); + } + + private static IEnumerable GetSectionConfigurations() + { + AdoptableFailureMechanismSectionResult[] sectionResults = + { + FailureMechanismSectionResultTestFactory.CreateFailureMechanismSectionResult(), + FailureMechanismSectionResultTestFactory.CreateFailureMechanismSectionResult(), + FailureMechanismSectionResultTestFactory.CreateFailureMechanismSectionResult() + }; + + var registry = new ExportableModelRegistry(); + registry.Register(sectionResults[0], CreateResult(1, 2)); + registry.Register(sectionResults[1], CreateResult(2, 3)); + registry.Register(sectionResults[2], CreateResult(3, 4)); + + ExportableCombinedFailureMechanismSection combinedSection = ExportableFailureMechanismSectionTestFactory.CreateExportableCombinedFailureMechanismSection(2, 3); + yield return new TestCaseData(registry, sectionResults, combinedSection, registry.Get(sectionResults[1])) + .SetName("CombinedSection exact overlap with section"); + + combinedSection = ExportableFailureMechanismSectionTestFactory.CreateExportableCombinedFailureMechanismSection(2, 2.5); + yield return new TestCaseData(registry, sectionResults, combinedSection, registry.Get(sectionResults[1])) + .SetName("CombinedSection partial overlap with section at end"); + + combinedSection = ExportableFailureMechanismSectionTestFactory.CreateExportableCombinedFailureMechanismSection(2.5, 3); + yield return new TestCaseData(registry, sectionResults, combinedSection, registry.Get(sectionResults[1])) + .SetName("CombinedSection partial overlap with section at start"); + } + } +} \ No newline at end of file