Index: Ringtoets/Common/src/Ringtoets.Common.Forms/GuiServices/HydraulicBoundaryLocationCalculationGuiService.cs =================================================================== diff -u -r7391d0059873c428ac191f830840d08a47d67393 -r4c288b5894b926ba5fdd7f6222c813c19b9701b8 --- Ringtoets/Common/src/Ringtoets.Common.Forms/GuiServices/HydraulicBoundaryLocationCalculationGuiService.cs (.../HydraulicBoundaryLocationCalculationGuiService.cs) (revision 7391d0059873c428ac191f830840d08a47d67393) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/GuiServices/HydraulicBoundaryLocationCalculationGuiService.cs (.../HydraulicBoundaryLocationCalculationGuiService.cs) (revision 4c288b5894b926ba5fdd7f6222c813c19b9701b8) @@ -21,15 +21,14 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Windows.Forms; using Core.Common.Base.Service; using Core.Common.Gui.Forms.ProgressDialog; using log4net; using Ringtoets.Common.Data.Hydraulics; +using Ringtoets.Common.Forms.Helpers; using Ringtoets.Common.Forms.Properties; using Ringtoets.Common.IO.HydraRing; -using Ringtoets.Common.Service; using Ringtoets.Common.Service.MessageProviders; namespace Ringtoets.Common.Forms.GuiServices @@ -75,11 +74,11 @@ RunActivities(hydraulicBoundaryDatabaseFilePath, preprocessorDirectory, - calculations.Select(calculation => new DesignWaterLevelCalculationActivity(calculation, - hydraulicBoundaryDatabaseFilePath, - preprocessorDirectory, - norm, - messageProvider)).ToArray()); + HydraulicBoundaryCalculationActivityHelper.CreateDesignWaterLevelCalculationActivities(hydraulicBoundaryDatabaseFilePath, + preprocessorDirectory, + calculations, + norm, + messageProvider)); } public void CalculateWaveHeights(string hydraulicBoundaryDatabaseFilePath, @@ -100,17 +99,24 @@ RunActivities(hydraulicBoundaryDatabaseFilePath, preprocessorDirectory, - calculations.Select(calculation => new WaveHeightCalculationActivity(calculation, - hydraulicBoundaryDatabaseFilePath, - preprocessorDirectory, - norm, - messageProvider)).ToArray()); + HydraulicBoundaryCalculationActivityHelper.CreateWaveHeightCalculationActivities(hydraulicBoundaryDatabaseFilePath, + preprocessorDirectory, + calculations, + norm, + messageProvider)); } - private void RunActivities(string hydraulicBoundaryDatabasePath, string preprocessorDirectory, - IEnumerable activities) where TActivity : Activity + public void RunActivities(string hydraulicBoundaryDatabaseFilePath, + string preprocessorDirectory, + IEnumerable activities) + where TActivity : Activity { - string validationProblem = HydraulicBoundaryDatabaseHelper.ValidateFilesForCalculation(hydraulicBoundaryDatabasePath, + if (activities == null) + { + throw new ArgumentNullException(nameof(activities)); + } + + string validationProblem = HydraulicBoundaryDatabaseHelper.ValidateFilesForCalculation(hydraulicBoundaryDatabaseFilePath, preprocessorDirectory); if (string.IsNullOrEmpty(validationProblem)) { Index: Ringtoets/Common/src/Ringtoets.Common.Forms/GuiServices/IHydraulicBoundaryLocationCalculationGuiService.cs =================================================================== diff -u -r7de6422eddd285a41f2bb8b7291928b9173bc160 -r4c288b5894b926ba5fdd7f6222c813c19b9701b8 --- Ringtoets/Common/src/Ringtoets.Common.Forms/GuiServices/IHydraulicBoundaryLocationCalculationGuiService.cs (.../IHydraulicBoundaryLocationCalculationGuiService.cs) (revision 7de6422eddd285a41f2bb8b7291928b9173bc160) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/GuiServices/IHydraulicBoundaryLocationCalculationGuiService.cs (.../IHydraulicBoundaryLocationCalculationGuiService.cs) (revision 4c288b5894b926ba5fdd7f6222c813c19b9701b8) @@ -21,6 +21,7 @@ using System; using System.Collections.Generic; +using Core.Common.Base.Service; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.Service.MessageProviders; @@ -34,7 +35,7 @@ /// /// Performs the provided design water level calculations. /// - /// The hydraulic boundary database file that should be used for performing the calculation. + /// The path of the hydraulic boundary database file. /// The preprocessor directory. /// The calculations to perform. /// The norm to use during the calculation. @@ -52,7 +53,7 @@ /// /// Performs the provided wave height calculations. /// - /// The hydraulic boundary database file that should be used for performing the calculation. + /// The path of the hydraulic boundary database file. /// The preprocessor directory. /// The calculations to perform. /// The norm to use during the calculation. @@ -66,5 +67,18 @@ IEnumerable calculations, double norm, ICalculationMessageProvider messageProvider); + + /// + /// Runs the given . + /// + /// The type of activity to run. + /// The path of the hydraulic boundary database file. + /// The preprocessor directory. + /// The collection of to run. + /// Thrown when is null. + void RunActivities(string hydraulicBoundaryDatabaseFilePath, + string preprocessorDirectory, + IEnumerable activities) + where TActivity : Activity; } } \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.Forms/Helpers/HydraulicBoundaryCalculationActivityHelper.cs =================================================================== diff -u --- Ringtoets/Common/src/Ringtoets.Common.Forms/Helpers/HydraulicBoundaryCalculationActivityHelper.cs (revision 0) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/Helpers/HydraulicBoundaryCalculationActivityHelper.cs (revision 4c288b5894b926ba5fdd7f6222c813c19b9701b8) @@ -0,0 +1,110 @@ +// 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.Hydraulics; +using Ringtoets.Common.Service; +using Ringtoets.Common.Service.MessageProviders; + +namespace Ringtoets.Common.Forms.Helpers +{ + /// + /// Helper methods related to hydraulic boundary calculation activities. + /// + public static class HydraulicBoundaryCalculationActivityHelper + { + /// + /// Creates a collection of based on the + /// given parameters. + /// + /// The path of the hydraulic boundary database file. + /// The preprocessor directory. + /// The collection of to create + /// the activities for. + /// The norm to use during the calculation. + /// The message provider for the activities. + /// A collection of . + /// Thrown when + /// or is null. + public static IEnumerable CreateWaveHeightCalculationActivities( + string hydraulicBoundaryDatabaseFilePath, + string preprocessorDirectory, + IEnumerable calculations, + double norm, + ICalculationMessageProvider messageProvider) + { + if (calculations == null) + { + throw new ArgumentNullException(nameof(calculations)); + } + + if (messageProvider == null) + { + throw new ArgumentNullException(nameof(messageProvider)); + } + + return calculations.Select(calculation => new WaveHeightCalculationActivity(calculation, + hydraulicBoundaryDatabaseFilePath, + preprocessorDirectory, + norm, + messageProvider)).ToArray(); + } + + /// + /// Creates a collection of based on the + /// given parameters. + /// + /// The hydraulic boundary database file that should be used by the calculations. + /// The preprocessor directory. + /// The collection of to create + /// the activities for. + /// The norm to use during the calculation. + /// The message provider for the activities. + /// A collection of . + /// Thrown when + /// or is null. + public static IEnumerable CreateDesignWaterLevelCalculationActivities( + string hydraulicBoundaryDatabaseFilePath, + string preprocessorDirectory, + IEnumerable calculations, + double norm, + ICalculationMessageProvider messageProvider) + { + if (calculations == null) + { + throw new ArgumentNullException(nameof(calculations)); + } + + if (messageProvider == null) + { + throw new ArgumentNullException(nameof(messageProvider)); + } + + return calculations.Select(calculation => new DesignWaterLevelCalculationActivity(calculation, + hydraulicBoundaryDatabaseFilePath, + preprocessorDirectory, + norm, + messageProvider)).ToArray(); + } + } +} \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.Forms/Ringtoets.Common.Forms.csproj =================================================================== diff -u -r91bfa12af7e319c18486ddc0b7431c9c654c77a7 -r4c288b5894b926ba5fdd7f6222c813c19b9701b8 --- Ringtoets/Common/src/Ringtoets.Common.Forms/Ringtoets.Common.Forms.csproj (.../Ringtoets.Common.Forms.csproj) (revision 91bfa12af7e319c18486ddc0b7431c9c654c77a7) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/Ringtoets.Common.Forms.csproj (.../Ringtoets.Common.Forms.csproj) (revision 4c288b5894b926ba5fdd7f6222c813c19b9701b8) @@ -51,6 +51,7 @@ + Index: Ringtoets/Common/test/Ringtoets.Common.Forms.Test/GuiServices/HydraulicBoundaryLocationCalculationGuiServiceTest.cs =================================================================== diff -u -r25af784999b04dc8156272632e7f6c638c55a18c -r4c288b5894b926ba5fdd7f6222c813c19b9701b8 --- Ringtoets/Common/test/Ringtoets.Common.Forms.Test/GuiServices/HydraulicBoundaryLocationCalculationGuiServiceTest.cs (.../HydraulicBoundaryLocationCalculationGuiServiceTest.cs) (revision 25af784999b04dc8156272632e7f6c638c55a18c) +++ Ringtoets/Common/test/Ringtoets.Common.Forms.Test/GuiServices/HydraulicBoundaryLocationCalculationGuiServiceTest.cs (.../HydraulicBoundaryLocationCalculationGuiServiceTest.cs) (revision 4c288b5894b926ba5fdd7f6222c813c19b9701b8) @@ -23,6 +23,7 @@ using System.IO; using System.Linq; using System.Windows.Forms; +using Core.Common.Base.Service; using Core.Common.TestUtil; using NUnit.Extensions.Forms; using NUnit.Framework; @@ -421,9 +422,100 @@ mockRepository.VerifyAll(); } + [Test] + public void RunActivities_ActivitiesNull_ThrowsArgumentNullException() + { + // Setup + using (var viewParent = new Form()) + { + var guiService = new HydraulicBoundaryLocationCalculationGuiService(viewParent); + + // Call + TestDelegate test = () => guiService.RunActivities(validFilePath, validPreprocessorDirectory, null); + + // Assert + string paramName = Assert.Throws(test).ParamName; + Assert.AreEqual("activities", paramName); + } + } + + [Test] + public void RunActivities_HydraulicDatabaseDoesNotExist_LogsError() + { + // Setup + using (var viewParent = new Form()) + { + var guiService = new HydraulicBoundaryLocationCalculationGuiService(viewParent); + + // Call + Action call = () => guiService.RunActivities("Does not exist", + validPreprocessorDirectory, + Enumerable.Empty()); + + // Assert + TestHelper.AssertLogMessages(call, messages => + { + string[] msgs = messages.ToArray(); + Assert.AreEqual(1, msgs.Length); + StringAssert.StartsWith("Berekeningen konden niet worden gestart. ", msgs.First()); + }); + } + } + + [Test] + public void RunActivities_WithActivities_RunsActivities() + { + // Setup + using (var viewParent = new Form()) + { + var guiService = new HydraulicBoundaryLocationCalculationGuiService(viewParent); + var activity = new TestActivity(); + + DialogBoxHandler = (name, wnd) => + { + // Expect an activity dialog which is automatically closed + }; + + // Precondition + Assert.IsFalse(activity.HasRun); + + // Call + Action call = () => guiService.RunActivities(validFilePath, + validPreprocessorDirectory, + new[] + { + activity + }); + + // Assert + TestHelper.AssertLogMessages(call, messages => + { + string[] msgs = messages.ToArray(); + Assert.AreEqual(2, msgs.Length); + StringAssert.AreNotEqualIgnoringCase("is gestart.", msgs[0]); + StringAssert.AreNotEqualIgnoringCase("is gelukt.", msgs[1]); + }); + Assert.IsTrue(activity.HasRun); + } + } + public override void Setup() { mockRepository = new MockRepository(); } + + private class TestActivity : Activity + { + public bool HasRun { get; private set; } + + protected override void OnRun() + { + HasRun = true; + } + + protected override void OnCancel() {} + + protected override void OnFinish() {} + } } } \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Helpers/HydraulicBoundaryCalculationActivityHelperTest.cs =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Helpers/HydraulicBoundaryCalculationActivityHelperTest.cs (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Helpers/HydraulicBoundaryCalculationActivityHelperTest.cs (revision 4c288b5894b926ba5fdd7f6222c813c19b9701b8) @@ -0,0 +1,256 @@ +// 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 Core.Common.Util; +using NUnit.Framework; +using Rhino.Mocks; +using Ringtoets.Common.Data.Hydraulics; +using Ringtoets.Common.Data.TestUtil; +using Ringtoets.Common.Forms.Helpers; +using Ringtoets.Common.Service; +using Ringtoets.Common.Service.MessageProviders; +using Ringtoets.Common.Service.TestUtil; +using Ringtoets.HydraRing.Calculation.Calculator.Factory; +using Ringtoets.HydraRing.Calculation.Data.Input.Hydraulics; +using Ringtoets.HydraRing.Calculation.TestUtil.Calculator; + +namespace Ringtoets.Common.Forms.Test.Helpers +{ + [TestFixture] + public class HydraulicBoundaryCalculationActivityHelperTest + { + 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"); + private static readonly string validPreprocessorDirectory = TestHelper.GetScratchPadPath(); + + [Test] + public void CreateWaveHeightCalculationActivities_CalculationsNull_ThrowsArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var messageProvider = mocks.Stub(); + mocks.ReplayAll(); + + // Call + TestDelegate test = () => HydraulicBoundaryCalculationActivityHelper.CreateWaveHeightCalculationActivities( + string.Empty, + string.Empty, + null, + new Random(12).NextDouble(), + messageProvider); + + // Assert + string paramName = Assert.Throws(test).ParamName; + Assert.AreEqual("calculations", paramName); + mocks.VerifyAll(); + } + + [Test] + public void CreateWaveHeightCalculationActivities_MesageProviderNull_ThrowsArgumentNullException() + { + // Call + TestDelegate test = () => HydraulicBoundaryCalculationActivityHelper.CreateWaveHeightCalculationActivities( + string.Empty, + string.Empty, + Enumerable.Empty(), + new Random(12).NextDouble(), + null); + + // Assert + string paramName = Assert.Throws(test).ParamName; + Assert.AreEqual("messageProvider", paramName); + } + + [Test] + public void CreateWaveHeightCalculationActivities_WithValidData_ReturnsExpectedActivity() + { + // Setup + const string locationName = "locationName"; + const string activityDescription = "activityDescription"; + const double norm = 1.0 / 30; + + var calculator = new TestWaveHeightCalculator + { + Converged = true + }; + + var mocks = new MockRepository(); + var calculatorFactory = mocks.StrictMock(); + calculatorFactory.Expect(cf => cf.CreateWaveHeightCalculator(testDataPath, validPreprocessorDirectory)).Return(calculator); + + var calculationMessageProvider = mocks.Stub(); + calculationMessageProvider.Stub(calc => calc.GetActivityDescription(locationName)).Return(activityDescription); + + var messageProvider = mocks.Stub(); + messageProvider.Stub(calc => calc.GetActivityDescription(locationName)).Return(activityDescription); + + mocks.ReplayAll(); + + var hydraulicBoundaryLocation = new TestHydraulicBoundaryLocation(locationName); + + // Call + IEnumerable activities = HydraulicBoundaryCalculationActivityHelper.CreateWaveHeightCalculationActivities( + validFilePath, + validPreprocessorDirectory, + new [] + { + new HydraulicBoundaryLocationCalculation(hydraulicBoundaryLocation) + }, + norm, + messageProvider); + + // Assert + WaveHeightCalculationActivity activity = activities.Single(); + using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) + { + Action call = () => activity.Run(); + + TestHelper.AssertLogMessages(call, m => + { + string[] messages = m.ToArray(); + Assert.AreEqual(6, messages.Length); + Assert.AreEqual($"{activityDescription} is gestart.", messages[0]); + CalculationServiceTestHelper.AssertValidationStartMessage(messages[1]); + CalculationServiceTestHelper.AssertValidationEndMessage(messages[2]); + CalculationServiceTestHelper.AssertCalculationStartMessage(messages[3]); + StringAssert.StartsWith("Golfhoogte berekening is uitgevoerd op de tijdelijke locatie", messages[4]); + CalculationServiceTestHelper.AssertCalculationEndMessage(messages[5]); + }); + WaveHeightCalculationInput waveHeightCalculationInput = calculator.ReceivedInputs.Single(); + + Assert.AreEqual(hydraulicBoundaryLocation.Id, waveHeightCalculationInput.HydraulicBoundaryLocationId); + Assert.AreEqual(StatisticsConverter.ProbabilityToReliability(norm), waveHeightCalculationInput.Beta); + } + + Assert.AreEqual(ActivityState.Executed, activity.State); + mocks.VerifyAll(); + } + + [Test] + public void CreateDesignWaterLevelCalculationActivities_CalculationsNull_ThrowsArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var messageProvider = mocks.Stub(); + mocks.ReplayAll(); + + // Call + TestDelegate test = () => HydraulicBoundaryCalculationActivityHelper.CreateDesignWaterLevelCalculationActivities( + string.Empty, + string.Empty, + null, + new Random(12).NextDouble(), + messageProvider); + + // Assert + string paramName = Assert.Throws(test).ParamName; + Assert.AreEqual("calculations", paramName); + mocks.VerifyAll(); + } + + [Test] + public void CreateDesignWaterLevelCalculationActivities_MesageProviderNull_ThrowsArgumentNullException() + { + // Call + TestDelegate test = () => HydraulicBoundaryCalculationActivityHelper.CreateDesignWaterLevelCalculationActivities( + string.Empty, + string.Empty, + Enumerable.Empty(), + new Random(12).NextDouble(), + null); + + // Assert + string paramName = Assert.Throws(test).ParamName; + Assert.AreEqual("messageProvider", paramName); + } + + [Test] + public void CreateDesignWaterLevelCalculationActivities_WithValidData_ReturnsExpectedActivity() + { + // Setup + const string locationName = "locationName"; + const string activityDescription = "activityDescription"; + const double norm = 1.0 / 30; + + var calculator = new TestDesignWaterLevelCalculator + { + Converged = true + }; + + var mocks = new MockRepository(); + var calculatorFactory = mocks.StrictMock(); + calculatorFactory.Expect(cf => cf.CreateDesignWaterLevelCalculator(testDataPath, validPreprocessorDirectory)).Return(calculator); + + var calculationMessageProvider = mocks.Stub(); + calculationMessageProvider.Stub(calc => calc.GetActivityDescription(locationName)).Return(activityDescription); + + var messageProvider = mocks.Stub(); + messageProvider.Stub(calc => calc.GetActivityDescription(locationName)).Return(activityDescription); + + mocks.ReplayAll(); + + var hydraulicBoundaryLocation = new TestHydraulicBoundaryLocation(locationName); + + // Call + IEnumerable activities = HydraulicBoundaryCalculationActivityHelper.CreateDesignWaterLevelCalculationActivities( + validFilePath, + validPreprocessorDirectory, + new [] + { + new HydraulicBoundaryLocationCalculation(hydraulicBoundaryLocation) + }, + norm, + messageProvider); + + // Assert + DesignWaterLevelCalculationActivity activity = activities.Single(); + using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) + { + Action call = () => activity.Run(); + + TestHelper.AssertLogMessages(call, m => + { + string[] messages = m.ToArray(); + Assert.AreEqual(6, messages.Length); + Assert.AreEqual($"{activityDescription} is gestart.", messages[0]); + CalculationServiceTestHelper.AssertValidationStartMessage(messages[1]); + CalculationServiceTestHelper.AssertValidationEndMessage(messages[2]); + CalculationServiceTestHelper.AssertCalculationStartMessage(messages[3]); + StringAssert.StartsWith("Toetspeil berekening is uitgevoerd op de tijdelijke locatie", messages[4]); + CalculationServiceTestHelper.AssertCalculationEndMessage(messages[5]); + }); + AssessmentLevelCalculationInput assessmentLevelCalculationInput = calculator.ReceivedInputs.Single(); + + Assert.AreEqual(hydraulicBoundaryLocation.Id, assessmentLevelCalculationInput.HydraulicBoundaryLocationId); + Assert.AreEqual(StatisticsConverter.ProbabilityToReliability(norm), assessmentLevelCalculationInput.Beta); + } + + Assert.AreEqual(ActivityState.Executed, activity.State); + mocks.VerifyAll(); + } + } +} \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Ringtoets.Common.Forms.Test.csproj =================================================================== diff -u -r91bfa12af7e319c18486ddc0b7431c9c654c77a7 -r4c288b5894b926ba5fdd7f6222c813c19b9701b8 --- Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Ringtoets.Common.Forms.Test.csproj (.../Ringtoets.Common.Forms.Test.csproj) (revision 91bfa12af7e319c18486ddc0b7431c9c654c77a7) +++ Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Ringtoets.Common.Forms.Test.csproj (.../Ringtoets.Common.Forms.Test.csproj) (revision 4c288b5894b926ba5fdd7f6222c813c19b9701b8) @@ -60,6 +60,7 @@ +