Index: Ringtoets/ClosingStructures/src/Ringtoets.ClosingStructures.Util/ClosingStructuresHelper.cs =================================================================== diff -u --- Ringtoets/ClosingStructures/src/Ringtoets.ClosingStructures.Util/ClosingStructuresHelper.cs (revision 0) +++ Ringtoets/ClosingStructures/src/Ringtoets.ClosingStructures.Util/ClosingStructuresHelper.cs (revision 7e3ea9fdd77f4efe3cd50c9adebbfcf369253bfd) @@ -0,0 +1,124 @@ +// Copyright (C) Stichting Deltares 2017. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets 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 Ringtoets.ClosingStructures.Data; +using Ringtoets.Common.Data.Structures; +using Ringtoets.Common.Util; + +namespace Ringtoets.ClosingStructures.Util +{ + /// + /// Class holds helper methods to match objects + /// with objects. + /// + public static class ClosingStructuresHelper + { + /// + /// Updates the for each element + /// of if required due to a change. + /// + /// The of + /// to possibly reassign a calculation to. + /// The of to try + /// and match with the . + /// All affected objects by the update. + /// Thrown when any input parameter is null or when an element + /// in is null. + /// Thrown when element in is + /// null. + public static IEnumerable UpdateCalculationToSectionResultAssignments( + IEnumerable sectionResults, + IEnumerable> calculations) + { + ValidateSectionResults(sectionResults); + + return AssignUnassignCalculations.Update(sectionResults.Select(AsCalculationAssignment), + AsCalculationsWithLocations(calculations)) + .Cast() + .ToArray(); + } + + /// + /// Transforms the into and filter out the calculations + /// for which a could not be made. + /// + /// The collection to transform. + /// A collection of . + /// Thrown when is null or when + /// an element in is null. + private static IEnumerable AsCalculationsWithLocations( + IEnumerable> calculations) + { + if (calculations == null) + { + throw new ArgumentNullException(nameof(calculations)); + } + + return calculations.Select(AsCalculationWithLocation).Where(c => c != null); + } + + /// + /// Validates the section results. + /// + /// The to validate. + /// Thrown when is null. + /// Thrown when contains null items. + private static void ValidateSectionResults(IEnumerable sectionResults) + { + if (sectionResults == null) + { + throw new ArgumentNullException(nameof(sectionResults)); + } + + if (sectionResults.Any(sr => sr == null)) + { + throw new ArgumentException(@"SectionResults contains an entry without value.", nameof(sectionResults)); + } + } + + private static CalculationWithLocation AsCalculationWithLocation(StructuresCalculation calculation) + { + if (calculation == null) + { + throw new ArgumentNullException(nameof(calculation)); + } + + if (calculation.InputParameters.Structure == null) + { + return null; + } + + return new CalculationWithLocation(calculation, calculation.InputParameters.Structure.Location); + } + + private static SectionResultWithCalculationAssignment AsCalculationAssignment( + ClosingStructuresFailureMechanismSectionResult failureMechanismSectionResult) + { + return new SectionResultWithCalculationAssignment( + failureMechanismSectionResult, + result => ((ClosingStructuresFailureMechanismSectionResult) result).Calculation, + (result, calculation) => ((ClosingStructuresFailureMechanismSectionResult) result).Calculation = (StructuresCalculation) calculation); + } + } +} \ No newline at end of file Index: Ringtoets/ClosingStructures/src/Ringtoets.ClosingStructures.Util/Ringtoets.ClosingStructures.Util.csproj =================================================================== diff -u -ra322ac9e8418713e77cf50d6dd80c8c6c29bd2c4 -r7e3ea9fdd77f4efe3cd50c9adebbfcf369253bfd --- Ringtoets/ClosingStructures/src/Ringtoets.ClosingStructures.Util/Ringtoets.ClosingStructures.Util.csproj (.../Ringtoets.ClosingStructures.Util.csproj) (revision a322ac9e8418713e77cf50d6dd80c8c6c29bd2c4) +++ Ringtoets/ClosingStructures/src/Ringtoets.ClosingStructures.Util/Ringtoets.ClosingStructures.Util.csproj (.../Ringtoets.ClosingStructures.Util.csproj) (revision 7e3ea9fdd77f4efe3cd50c9adebbfcf369253bfd) @@ -11,11 +11,34 @@ + Copying.licenseheader + + + {3bbfd65b-b277-4e50-ae6d-bd24c3434609} + Core.Common.Base + False + + + {d4200f43-3f72-4f42-af0a-8ced416a38ec} + Ringtoets.Common.Data + False + + + {6a074d65-a81c-4c1c-8e24-f36c916e4ed7} + Ringtoets.Common.Util + False + + + {c6309704-d67b-434c-bc98-9f8910bc1d10} + Ringtoets.ClosingStructures.Data + False + + \ No newline at end of file Index: Ringtoets/ClosingStructures/test/Ringtoets.ClosingStructures.Util.Test/ClosingStructuresHelperTest.cs =================================================================== diff -u --- Ringtoets/ClosingStructures/test/Ringtoets.ClosingStructures.Util.Test/ClosingStructuresHelperTest.cs (revision 0) +++ Ringtoets/ClosingStructures/test/Ringtoets.ClosingStructures.Util.Test/ClosingStructuresHelperTest.cs (revision 7e3ea9fdd77f4efe3cd50c9adebbfcf369253bfd) @@ -0,0 +1,187 @@ +// Copyright (C) Stichting Deltares 2017. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets 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 Core.Common.Base.Geometry; +using Core.Common.TestUtil; +using NUnit.Framework; +using Ringtoets.ClosingStructures.Data; +using Ringtoets.ClosingStructures.Data.TestUtil; +using Ringtoets.Common.Data.FailureMechanism; +using Ringtoets.Common.Data.Structures; + +namespace Ringtoets.ClosingStructures.Util.Test +{ + [TestFixture] + public class ClosingStructuresHelperTest + { + [Test] + public void UpdateCalculationToSectionResultAssignments_SectionResultsNull_ThrowsArgumentNullException() + { + // Call + TestDelegate test = () => ClosingStructuresHelper.UpdateCalculationToSectionResultAssignments( + null, + new[] + { + calculationInSectionB + }); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("sectionResults", exception.ParamName); + } + + [Test] + public void UpdateCalculationToSectionResultAssignments_SectionResultElementsNull_ThrowsArgumentException() + { + // Call + TestDelegate test = () => ClosingStructuresHelper.UpdateCalculationToSectionResultAssignments( + new ClosingStructuresFailureMechanismSectionResult[] + { + null, + null + }, + new[] + { + calculationInSectionB + }); + + // Assert + var exception = TestHelper.AssertThrowsArgumentExceptionAndTestMessage( + test, "SectionResults contains an entry without value."); + Assert.AreEqual("sectionResults", exception.ParamName); + } + + [Test] + public void UpdateCalculationToSectionResultAssignments_CalculationsNull_ThrowsArgumentNullException() + { + // Call + TestDelegate test = () => ClosingStructuresHelper.UpdateCalculationToSectionResultAssignments( + new[] + { + sectionResult + }, + null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("calculations", exception.ParamName); + } + + [Test] + public void UpdateCalculationToSectionResultAssignments_CalculationsElementNull_ThrowsArgumentNullException() + { + // Call + TestDelegate test = () => ClosingStructuresHelper.UpdateCalculationToSectionResultAssignments( + new[] + { + sectionResult + }, + new StructuresCalculation[] + { + null + }); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("calculation", exception.ParamName); + } + + [Test] + public void UpdateCalculationToSectionResultAssignments_SectionResultWithCalculationNoRemainingCalculations_SectionResultCalculationIsNull() + { + // Setup + var failureMechanismSectionResult = new ClosingStructuresFailureMechanismSectionResult( + failureMechanismSection) + { + Calculation = calculationInSectionA + }; + + var failureMechanismSectionResults = new[] + { + failureMechanismSectionResult + }; + + // Call + ClosingStructuresHelper.UpdateCalculationToSectionResultAssignments(failureMechanismSectionResults, + Enumerable.Empty>()); + + // Assert + Assert.IsNull(failureMechanismSectionResult.Calculation); + } + + [Test] + public void UpdateCalculationToSectionResultAssignments_SectionResultWithCalculationWithRemainingCalculations_SectionResultCalculationSetToRemainingCalculation() + { + // Setup + var failureMechanismSectionResult = new ClosingStructuresFailureMechanismSectionResult( + failureMechanismSection) + { + Calculation = calculationInSectionA + }; + + // Call + ClosingStructuresHelper.UpdateCalculationToSectionResultAssignments( + new[] + { + failureMechanismSectionResult + }, + new[] + { + calculationInSectionB + }); + + // Assert + Assert.AreSame(calculationInSectionB, failureMechanismSectionResult.Calculation); + } + + #region Prepared data + + private static readonly FailureMechanismSection failureMechanismSection = new FailureMechanismSection("A", new List + { + new Point2D(0.0, 0.0), + new Point2D(10.0, 10.0) + }); + + private static readonly ClosingStructuresFailureMechanismSectionResult sectionResult = new ClosingStructuresFailureMechanismSectionResult( + failureMechanismSection); + + private static readonly StructuresCalculation calculationInSectionA = new StructuresCalculation + { + InputParameters = + { + Structure = new TestClosingStructure(new Point2D(1.1, 2.2)) + } + }; + + private static readonly StructuresCalculation calculationInSectionB = new StructuresCalculation + { + InputParameters = + { + Structure = new TestClosingStructure(new Point2D(50.0, 66.0)) + } + }; + + #endregion + } +} \ No newline at end of file Index: Ringtoets/ClosingStructures/test/Ringtoets.ClosingStructures.Util.Test/Ringtoets.ClosingStructures.Util.Test.csproj =================================================================== diff -u -ra322ac9e8418713e77cf50d6dd80c8c6c29bd2c4 -r7e3ea9fdd77f4efe3cd50c9adebbfcf369253bfd --- Ringtoets/ClosingStructures/test/Ringtoets.ClosingStructures.Util.Test/Ringtoets.ClosingStructures.Util.Test.csproj (.../Ringtoets.ClosingStructures.Util.Test.csproj) (revision a322ac9e8418713e77cf50d6dd80c8c6c29bd2c4) +++ Ringtoets/ClosingStructures/test/Ringtoets.ClosingStructures.Util.Test/Ringtoets.ClosingStructures.Util.Test.csproj (.../Ringtoets.ClosingStructures.Util.Test.csproj) (revision 7e3ea9fdd77f4efe3cd50c9adebbfcf369253bfd) @@ -7,15 +7,46 @@ + + ..\..\..\..\packages\NUnit.3.8.1\lib\net40\nunit.framework.dll + + Copying.licenseheader + + + + {3BBFD65B-B277-4E50-AE6D-BD24C3434609} + Core.Common.Base + + + {D749EE4C-CE50-4C17-BF01-9A953028C126} + Core.Common.TestUtil + + + {D4200F43-3F72-4F42-AF0A-8CED416A38EC} + Ringtoets.Common.Data + + + {C6309704-D67B-434C-BC98-9F8910BC1D10} + Ringtoets.ClosingStructures.Data + + + {673B4A0B-96B1-4E98-BB49-E3C7BCB5F179} + Ringtoets.ClosingStructures.Util + + + {F5B43C29-6169-4E9A-859E-09090330B94E} + Ringtoets.ClosingStructures.Data.TestUtil + + \ No newline at end of file Index: Ringtoets/ClosingStructures/test/Ringtoets.ClosingStructures.Util.Test/packages.config =================================================================== diff -u --- Ringtoets/ClosingStructures/test/Ringtoets.ClosingStructures.Util.Test/packages.config (revision 0) +++ Ringtoets/ClosingStructures/test/Ringtoets.ClosingStructures.Util.Test/packages.config (revision 7e3ea9fdd77f4efe3cd50c9adebbfcf369253bfd) @@ -0,0 +1,26 @@ + + + + + \ No newline at end of file