Index: Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Service/Ringtoets.StabilityPointStructures.Service.csproj =================================================================== diff -u -r951c6fb790f3ea3e0a89771b62ad323967746455 -r85a53d9ddff4eb0281b4a62fba378afcd9ebbc00 --- Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Service/Ringtoets.StabilityPointStructures.Service.csproj (.../Ringtoets.StabilityPointStructures.Service.csproj) (revision 951c6fb790f3ea3e0a89771b62ad323967746455) +++ Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Service/Ringtoets.StabilityPointStructures.Service.csproj (.../Ringtoets.StabilityPointStructures.Service.csproj) (revision 85a53d9ddff4eb0281b4a62fba378afcd9ebbc00) @@ -11,6 +11,7 @@ + True Index: Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Service/StabilityPointStructuresCalculationActivityFactory.cs =================================================================== diff -u --- Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Service/StabilityPointStructuresCalculationActivityFactory.cs (revision 0) +++ Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Service/StabilityPointStructuresCalculationActivityFactory.cs (revision 85a53d9ddff4eb0281b4a62fba378afcd9ebbc00) @@ -0,0 +1,133 @@ +// 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.Common.Data.AssessmentSection; +using Ringtoets.Common.Data.Calculation; +using Ringtoets.Common.Data.Structures; +using Ringtoets.Common.Service; +using Ringtoets.StabilityPointStructures.Data; + +namespace Ringtoets.StabilityPointStructures.Service +{ + /// + /// This class defines factory methods that can be used to create instances of for + /// stability point structures calculations. + /// + public static class StabilityPointStructuresCalculationActivityFactory + { + /// + /// Creates a collection of based on the calculations in + /// . + /// + /// The failure mechanism containing the calculations to create + /// activities for. + /// The assessment section the belongs to. + /// A collection of . + /// Thrown when any parameter is null. + public static IEnumerable CreateCalculationActivities(StabilityPointStructuresFailureMechanism failureMechanism, + IAssessmentSection assessmentSection) + { + if (failureMechanism == null) + { + throw new ArgumentNullException(nameof(failureMechanism)); + } + + if (assessmentSection == null) + { + throw new ArgumentNullException(nameof(assessmentSection)); + } + + return CreateCalculationActivities(failureMechanism.CalculationsGroup, failureMechanism, assessmentSection); + } + + /// + /// Creates a collection of based on the calculations in + /// . + /// + /// The calculation group to create activities for. + /// The failure mechanism the calculations belongs to. + /// The assessment section the calculations in + /// belong to. + /// A collection of . + /// Thrown when any parameter is null. + public static IEnumerable CreateCalculationActivities(CalculationGroup calculationGroup, + StabilityPointStructuresFailureMechanism failureMechanism, + IAssessmentSection assessmentSection) + { + if (calculationGroup == null) + { + throw new ArgumentNullException(nameof(calculationGroup)); + } + + if (failureMechanism == null) + { + throw new ArgumentNullException(nameof(failureMechanism)); + } + + if (assessmentSection == null) + { + throw new ArgumentNullException(nameof(assessmentSection)); + } + + return calculationGroup.GetCalculations() + .Cast>() + .Select(calc => CreateCalculationActivity(calc, failureMechanism, assessmentSection)) + .ToArray(); + } + + /// + /// Creates a based on the given . + /// + /// The calculation to create an activity for. + /// The failure mechanism the calculation belongs to. + /// The assessment section the + /// belongs to. + /// A . + /// Thrown when any parameter is null. + public static CalculatableActivity CreateCalculationActivity(StructuresCalculation calculation, + StabilityPointStructuresFailureMechanism failureMechanism, + IAssessmentSection assessmentSection) + { + if (calculation == null) + { + throw new ArgumentNullException(nameof(calculation)); + } + + if (failureMechanism == null) + { + throw new ArgumentNullException(nameof(failureMechanism)); + } + + if (assessmentSection == null) + { + throw new ArgumentNullException(nameof(assessmentSection)); + } + + return new StabilityPointStructuresCalculationActivity(calculation, + assessmentSection.HydraulicBoundaryDatabase.FilePath, + failureMechanism, + assessmentSection); + } + } +} \ No newline at end of file Index: Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Service.Test/Ringtoets.StabilityPointStructures.Service.Test.csproj =================================================================== diff -u -r8b8e62bfddfca997d2ed5df4a0c9c72648f1b5b4 -r85a53d9ddff4eb0281b4a62fba378afcd9ebbc00 --- Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Service.Test/Ringtoets.StabilityPointStructures.Service.Test.csproj (.../Ringtoets.StabilityPointStructures.Service.Test.csproj) (revision 8b8e62bfddfca997d2ed5df4a0c9c72648f1b5b4) +++ Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Service.Test/Ringtoets.StabilityPointStructures.Service.Test.csproj (.../Ringtoets.StabilityPointStructures.Service.Test.csproj) (revision 85a53d9ddff4eb0281b4a62fba378afcd9ebbc00) @@ -19,6 +19,7 @@ + Index: Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Service.Test/StabilityPointStructuresCalculationActivityFactoryTest.cs =================================================================== diff -u --- Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Service.Test/StabilityPointStructuresCalculationActivityFactoryTest.cs (revision 0) +++ Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Service.Test/StabilityPointStructuresCalculationActivityFactoryTest.cs (revision 85a53d9ddff4eb0281b4a62fba378afcd9ebbc00) @@ -0,0 +1,312 @@ +// 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.IO; +using System.Linq; +using Core.Common.Base.Service; +using Core.Common.TestUtil; +using NUnit.Framework; +using Rhino.Mocks; +using Ringtoets.Common.Data.AssessmentSection; +using Ringtoets.Common.Data.Calculation; +using Ringtoets.Common.Data.Hydraulics; +using Ringtoets.Common.Data.Structures; +using Ringtoets.Common.Data.TestUtil; +using Ringtoets.Common.Service; +using Ringtoets.HydraRing.Calculation.Calculator.Factory; +using Ringtoets.HydraRing.Calculation.Data.Input.Structures; +using Ringtoets.HydraRing.Calculation.TestUtil.Calculator; +using Ringtoets.StabilityPointStructures.Data; +using Ringtoets.StabilityPointStructures.Data.TestUtil; + +namespace Ringtoets.StabilityPointStructures.Service.Test +{ + [TestFixture] + public class StabilityPointStructuresCalculationActivityFactoryTest + { + private static readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Integration.Service, "HydraRingCalculation"); + private static readonly string validFilePath = Path.Combine(testDataPath, "HRD dutch coast south.sqlite"); + + [Test] + public void CreateCalculationActivity_CalculationNull_ThrowsArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var assessmentSection = mocks.Stub(); + mocks.ReplayAll(); + + // Call + TestDelegate test = () => StabilityPointStructuresCalculationActivityFactory.CreateCalculationActivity(null, + new StabilityPointStructuresFailureMechanism(), + assessmentSection); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("calculation", exception.ParamName); + mocks.VerifyAll(); + } + + [Test] + public void CreateCalculationActivity_FailureMechanismNull_ThrowsArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var assessmentSection = mocks.Stub(); + mocks.ReplayAll(); + + // Call + TestDelegate test = () => StabilityPointStructuresCalculationActivityFactory.CreateCalculationActivity(new StructuresCalculation(), + null, + assessmentSection); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("failureMechanism", exception.ParamName); + mocks.VerifyAll(); + } + + [Test] + public void CreateCalculationActivity_AssessmentSectionNull_ThrowsArgumentNullException() + { + // Setup + var calculation = new StructuresCalculation(); + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + + // Call + TestDelegate test = () => StabilityPointStructuresCalculationActivityFactory.CreateCalculationActivity(calculation, failureMechanism, null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("assessmentSection", exception.ParamName); + } + + [Test] + public void CreateCalculationActivity_WithValidCalculation_ReturnsStabilityPointStructuresCalculationActivityWithParametersSet() + { + // Setup + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + + var mocks = new MockRepository(); + IAssessmentSection assessmentSection = AssessmentSectionHelper.CreateAssessmentSectionStub(failureMechanism, + mocks, + validFilePath); + + mocks.ReplayAll(); + + StructuresCalculation calculation = CreateCalculation(); + + // Call + CalculatableActivity activity = StabilityPointStructuresCalculationActivityFactory.CreateCalculationActivity(calculation, + failureMechanism, + assessmentSection); + + // Assert + Assert.IsInstanceOf(activity); + AssertStabilityPointStructuresCalculationActivity(activity, calculation); + mocks.VerifyAll(); + } + + [Test] + public void CreateCalculationActivitiesForCalculationGroup_CalculationGroupNull_ThrowsArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var assessmentSection = mocks.Stub(); + mocks.ReplayAll(); + + // Call + TestDelegate test = () => StabilityPointStructuresCalculationActivityFactory.CreateCalculationActivities(null, + new StabilityPointStructuresFailureMechanism(), + assessmentSection); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("calculationGroup", exception.ParamName); + mocks.VerifyAll(); + } + + [Test] + public void CreateCalculationActivitiesForCalculationGroup_FailureMechanismNull_ThrowsArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var assessmentSection = mocks.Stub(); + mocks.ReplayAll(); + + // Call + TestDelegate test = () => StabilityPointStructuresCalculationActivityFactory.CreateCalculationActivities(new CalculationGroup(), + null, + assessmentSection); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("failureMechanism", exception.ParamName); + mocks.VerifyAll(); + } + + [Test] + public void CreateCalculationActivitiesForCalculationGroup_AssessmentSectionNull_ThrowsArgumentNullException() + { + // Call + TestDelegate test = () => StabilityPointStructuresCalculationActivityFactory.CreateCalculationActivities(new CalculationGroup(), + new StabilityPointStructuresFailureMechanism(), + null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("assessmentSection", exception.ParamName); + } + + [Test] + public void CreateCalculationActivitiesForCalculationGroup_WithValidCalculations_ReturnsStabilityPointStructuresCalculationActivitiesWithParametersSet() + { + // Setup + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + + var mocks = new MockRepository(); + IAssessmentSection assessmentSection = AssessmentSectionHelper.CreateAssessmentSectionStub(failureMechanism, + mocks, + validFilePath); + mocks.ReplayAll(); + + StructuresCalculation calculation1 = CreateCalculation(); + StructuresCalculation calculation2 = CreateCalculation(); + + var calculations = new CalculationGroup + { + Children = + { + calculation1, + calculation2 + } + }; + + // Call + IEnumerable activities = StabilityPointStructuresCalculationActivityFactory.CreateCalculationActivities( + calculations, failureMechanism, assessmentSection); + + // Assert + CollectionAssert.AllItemsAreInstancesOfType(activities, typeof(StabilityPointStructuresCalculationActivity)); + Assert.AreEqual(2, activities.Count()); + + AssertStabilityPointStructuresCalculationActivity(activities.First(), calculation1); + AssertStabilityPointStructuresCalculationActivity(activities.ElementAt(1), calculation2); + mocks.VerifyAll(); + } + + [Test] + public void CreateCalculationActivitiesForFailureMechanism_FailureMechanismNull_ThrowsArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var assessmentSection = mocks.Stub(); + mocks.ReplayAll(); + + // Call + TestDelegate test = () => StabilityPointStructuresCalculationActivityFactory.CreateCalculationActivities(null, assessmentSection); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("failureMechanism", exception.ParamName); + mocks.VerifyAll(); + } + + [Test] + public void CreateCalculationActivitiesForFailureMechanism_AssessmentSectionNull_ThrowsArgumentNullException() + { + // Call + TestDelegate test = () => StabilityPointStructuresCalculationActivityFactory.CreateCalculationActivities(new StabilityPointStructuresFailureMechanism(), null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("assessmentSection", exception.ParamName); + } + + [Test] + public void CreateCalculationActivitiesForFailureMechanism_WithValidCalculations_ReturnsStabilityPointStructuresCalculationActivitiesWithParametersSet() + { + // Setup + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + + var mocks = new MockRepository(); + IAssessmentSection assessmentSection = AssessmentSectionHelper.CreateAssessmentSectionStub(failureMechanism, + mocks, + validFilePath); + + mocks.ReplayAll(); + + StructuresCalculation calculation1 = CreateCalculation(); + StructuresCalculation calculation2 = CreateCalculation(); + + failureMechanism.CalculationsGroup.Children.AddRange(new[] + { + calculation1, + calculation2 + }); + + // Call + IEnumerable activities = StabilityPointStructuresCalculationActivityFactory.CreateCalculationActivities( + failureMechanism, assessmentSection); + + // Assert + CollectionAssert.AllItemsAreInstancesOfType(activities, typeof(StabilityPointStructuresCalculationActivity)); + Assert.AreEqual(2, activities.Count()); + + AssertStabilityPointStructuresCalculationActivity(activities.First(), calculation1); + AssertStabilityPointStructuresCalculationActivity(activities.ElementAt(1), calculation2); + mocks.VerifyAll(); + } + + private static StructuresCalculation CreateCalculation() + { + return new TestStabilityPointStructuresCalculation + { + InputParameters = + { + HydraulicBoundaryLocation = new HydraulicBoundaryLocation(1, "name", 2, 2), + FailureProbabilityStructureWithErosion = new Random(39).NextDouble() + } + }; + } + + private static void AssertStabilityPointStructuresCalculationActivity(Activity activity, + ICalculation calculation) + { + var mocks = new MockRepository(); + var testCalculator = new TestStructuresCalculator(); + var calculatorFactory = mocks.StrictMock(); + calculatorFactory.Expect(cf => cf.CreateStructuresCalculator(testDataPath, "")) + .Return(testCalculator); + mocks.ReplayAll(); + + using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) + { + activity.Run(); + + Assert.AreEqual(calculation.InputParameters.FailureProbabilityStructureWithErosion, testCalculator.ReceivedInputs.Single().Variables.ElementAt(11).Value); + } + + mocks.VerifyAll(); + } + } +} \ No newline at end of file