Fisheye: Tag 7d7a01ed91dd10bc4795528179b5f35e5f59f1bd refers to a dead (removed) revision in file `Ringtoets/DuneErosion/src/Ringtoets.DuneErosion.Service/DuneErosionBoundaryCalculationService.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/DuneErosion/src/Ringtoets.DuneErosion.Service/DuneLocationCalculationActivity.cs =================================================================== diff -u -rf0a2ec89952a42ebdedbea6efac5fe7196f3b46a -r7d7a01ed91dd10bc4795528179b5f35e5f59f1bd --- Ringtoets/DuneErosion/src/Ringtoets.DuneErosion.Service/DuneLocationCalculationActivity.cs (.../DuneLocationCalculationActivity.cs) (revision f0a2ec89952a42ebdedbea6efac5fe7196f3b46a) +++ Ringtoets/DuneErosion/src/Ringtoets.DuneErosion.Service/DuneLocationCalculationActivity.cs (.../DuneLocationCalculationActivity.cs) (revision 7d7a01ed91dd10bc4795528179b5f35e5f59f1bd) @@ -37,7 +37,7 @@ private readonly string hydraulicBoundaryDatabaseFilePath; private readonly string preprocessorDirectory; private readonly double norm; - private readonly DuneErosionBoundaryCalculationService calculationService; + private readonly DuneLocationCalculationService calculationService; /// /// Creates a new instance of . @@ -69,7 +69,7 @@ Description = string.Format(Resources.DuneErosionBoundaryCalculationActivity_Calculate_hydraulic_boundary_conditions_for_DuneLocation_with_name_0_, duneLocation.Name); - calculationService = new DuneErosionBoundaryCalculationService(); + calculationService = new DuneLocationCalculationService(); } protected override bool Validate() @@ -80,7 +80,7 @@ return true; } - return DuneErosionBoundaryCalculationService.Validate(hydraulicBoundaryDatabaseFilePath, preprocessorDirectory); + return DuneLocationCalculationService.Validate(hydraulicBoundaryDatabaseFilePath, preprocessorDirectory); } protected override void PerformCalculation() Index: Ringtoets/DuneErosion/src/Ringtoets.DuneErosion.Service/DuneLocationCalculationService.cs =================================================================== diff -u --- Ringtoets/DuneErosion/src/Ringtoets.DuneErosion.Service/DuneLocationCalculationService.cs (revision 0) +++ Ringtoets/DuneErosion/src/Ringtoets.DuneErosion.Service/DuneLocationCalculationService.cs (revision 7d7a01ed91dd10bc4795528179b5f35e5f59f1bd) @@ -0,0 +1,263 @@ +// 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.IO; +using Core.Common.Base.IO; +using Core.Common.Util; +using log4net; +using Ringtoets.Common.Data.Hydraulics; +using Ringtoets.Common.IO.HydraRing; +using Ringtoets.Common.Service; +using Ringtoets.DuneErosion.Data; +using Ringtoets.DuneErosion.Service.Properties; +using Ringtoets.HydraRing.Calculation.Calculator; +using Ringtoets.HydraRing.Calculation.Calculator.Factory; +using Ringtoets.HydraRing.Calculation.Data.Input.Hydraulics; +using Ringtoets.HydraRing.Calculation.Exceptions; +using RingtoetsCommonServiceResources = Ringtoets.Common.Service.Properties.Resources; + +namespace Ringtoets.DuneErosion.Service +{ + /// + /// Service that provides methods for performing Hydra-Ring calculations for dune locations. + /// + public class DuneLocationCalculationService + { + private static readonly ILog log = LogManager.GetLogger(typeof(DuneLocationCalculationService)); + + private bool canceled; + private IDunesBoundaryConditionsCalculator calculator; + + /// + /// Performs validation on the given and . + /// Error and status information is logged during the execution of the operation. + /// + /// The file path of the hydraulic boundary database file which to validate. + /// The preprocessor directory to validate. + /// true if there were no validation errors; false otherwise. + public static bool Validate(string hydraulicBoundaryDatabaseFilePath, string preprocessorDirectory) + { + var isValid = true; + + CalculationServiceHelper.LogValidationBegin(); + + string databaseFilePathValidationProblem = HydraulicBoundaryDatabaseHelper.ValidateFilesForCalculation(hydraulicBoundaryDatabaseFilePath, + preprocessorDirectory); + if (!string.IsNullOrEmpty(databaseFilePathValidationProblem)) + { + CalculationServiceHelper.LogMessagesAsError(RingtoetsCommonServiceResources.Hydraulic_boundary_database_connection_failed_0_, + new[] + { + databaseFilePathValidationProblem + }); + + isValid = false; + } + + string preprocessorDirectoryValidationProblem = HydraulicBoundaryDatabaseHelper.ValidatePreprocessorDirectory(preprocessorDirectory); + if (!string.IsNullOrEmpty(preprocessorDirectoryValidationProblem)) + { + CalculationServiceHelper.LogMessagesAsError(new[] + { + preprocessorDirectoryValidationProblem + }); + + isValid = false; + } + + CalculationServiceHelper.LogValidationEnd(); + + return isValid; + } + + /// + /// Performs a dune location calculation based on the supplied + /// and sets its output if the calculation is successful. + /// Error and status information is logged during the execution of the operation. + /// + /// The to perform. + /// The norm to use during the calculation. + /// The path which points to the hydraulic + /// boundary database file. + /// The preprocessor directory. + /// Preprocessing is disabled when equals . + /// Thrown when is null. + /// Thrown when: + /// + /// The contains invalid characters. + /// The contribution of the failure mechanism is zero. + /// The target probability or the calculated probability falls outside the [0.0, 1.0] + /// range and is not . + /// + /// Thrown when: + /// + /// No settings database file could be found at the location of + /// with the same name. + /// Unable to open settings database file. + /// Unable to read required data from database file. + /// + /// Thrown when an error occurs while performing + /// the calculation. + public void Calculate(DuneLocationCalculation duneLocationCalculation, + double norm, + string hydraulicBoundaryDatabaseFilePath, + string preprocessorDirectory) + { + if (duneLocationCalculation == null) + { + throw new ArgumentNullException(nameof(duneLocationCalculation)); + } + + string hlcdDirectory = Path.GetDirectoryName(hydraulicBoundaryDatabaseFilePath); + + DuneLocation duneLocation = duneLocationCalculation.DuneLocation; + string duneLocationName = duneLocation.Name; + + CalculationServiceHelper.LogCalculationBegin(); + + calculator = HydraRingCalculatorFactory.Instance.CreateDunesBoundaryConditionsCalculator(hlcdDirectory, preprocessorDirectory); + + var exceptionThrown = false; + + try + { + DunesBoundaryConditionsCalculationInput calculationInput = CreateInput(duneLocation, + norm, + hydraulicBoundaryDatabaseFilePath, + !string.IsNullOrEmpty(preprocessorDirectory)); + calculator.Calculate(calculationInput); + + if (string.IsNullOrEmpty(calculator.LastErrorFileContent)) + { + duneLocationCalculation.Output = CreateDuneLocationCalculationOutput(duneLocationName, calculationInput.Beta, norm); + } + } + catch (HydraRingCalculationException) + { + if (!canceled) + { + string lastErrorContent = calculator.LastErrorFileContent; + log.Error(string.IsNullOrEmpty(lastErrorContent) + ? string.Format(Resources.DuneErosionBoundaryCalculationService_Calculate_Error_in_DuneErosionBoundaryCalculation_0_no_error_report, + duneLocationName) + : string.Format(Resources.DuneErosionBoundaryCalculationService_Calculate_Error_in_DuneErosionBoundaryCalculation_0_click_details_for_last_error_report_1, + duneLocationName, lastErrorContent)); + + exceptionThrown = true; + throw; + } + } + finally + { + string lastErrorFileContent = calculator.LastErrorFileContent; + bool hasErrorOccurred = CalculationServiceHelper.HasErrorOccurred(canceled, exceptionThrown, lastErrorFileContent); + if (hasErrorOccurred) + { + log.ErrorFormat(Resources.DuneErosionBoundaryCalculationService_Calculate_Error_in_DuneErosionBoundaryCalculation_0_click_details_for_last_error_report_1, + duneLocationName, lastErrorFileContent); + } + + log.InfoFormat(Resources.DuneErosionBoundaryCalculationService_Calculate_Calculation_temporary_directory_can_be_found_on_location_0, + calculator.OutputDirectory); + CalculationServiceHelper.LogCalculationEnd(); + + if (hasErrorOccurred) + { + throw new HydraRingCalculationException(lastErrorFileContent); + } + } + } + + /// + /// Cancels any ongoing dune location calculation. + /// + public void Cancel() + { + calculator?.Cancel(); + canceled = true; + } + + /// + /// Create the output of the calculation. + /// + /// The name of the location. + /// The target reliability for the calculation. + /// The target probability for the calculation. + /// A . + /// Thrown when + /// or the calculated probability falls outside the [0.0, 1.0] range and is not . + private DuneLocationCalculationOutput CreateDuneLocationCalculationOutput(string duneLocationName, double targetReliability, double targetProbability) + { + double reliability = calculator.ReliabilityIndex; + double probability = StatisticsConverter.ReliabilityToProbability(reliability); + + CalculationConvergence converged = RingtoetsCommonDataCalculationService.GetCalculationConvergence(calculator.Converged); + + if (converged != CalculationConvergence.CalculatedConverged) + { + log.WarnFormat(Resources.DuneErosionBoundaryCalculationService_CreateDuneLocationOutput_Calculation_for_DuneLocation_0_not_converged, duneLocationName); + } + + return new DuneLocationCalculationOutput(converged, + new DuneLocationCalculationOutput.ConstructionProperties + { + WaterLevel = calculator.WaterLevel, + WaveHeight = calculator.WaveHeight, + WavePeriod = calculator.WavePeriod, + TargetProbability = targetProbability, + TargetReliability = targetReliability, + CalculatedProbability = probability, + CalculatedReliability = reliability + }); + } + + /// + /// Creates the input used in the calculation. + /// + /// The to create the input for. + /// The norm of the failure mechanism to use. + /// The file path to the hydraulic + /// boundary database. + /// Indicator whether to use the preprocessor in the calculation. + /// A with all needed + /// input data. + /// Thrown when the + /// contains invalid characters. + /// Thrown when: + /// + /// No settings database file could be found at the location of + /// with the same name. + /// Unable to open settings database file. + /// Unable to read required data from database file. + /// + /// + private static DunesBoundaryConditionsCalculationInput CreateInput(DuneLocation duneLocation, + double norm, + string hydraulicBoundaryDatabaseFilePath, + bool usePreprocessor) + { + var dunesBoundaryConditionsCalculationInput = new DunesBoundaryConditionsCalculationInput(1, duneLocation.Id, norm); + HydraRingSettingsDatabaseHelper.AssignSettingsFromDatabase(dunesBoundaryConditionsCalculationInput, hydraulicBoundaryDatabaseFilePath, usePreprocessor); + return dunesBoundaryConditionsCalculationInput; + } + } +} \ No newline at end of file Index: Ringtoets/DuneErosion/src/Ringtoets.DuneErosion.Service/Ringtoets.DuneErosion.Service.csproj =================================================================== diff -u -rf0a2ec89952a42ebdedbea6efac5fe7196f3b46a -r7d7a01ed91dd10bc4795528179b5f35e5f59f1bd --- Ringtoets/DuneErosion/src/Ringtoets.DuneErosion.Service/Ringtoets.DuneErosion.Service.csproj (.../Ringtoets.DuneErosion.Service.csproj) (revision f0a2ec89952a42ebdedbea6efac5fe7196f3b46a) +++ Ringtoets/DuneErosion/src/Ringtoets.DuneErosion.Service/Ringtoets.DuneErosion.Service.csproj (.../Ringtoets.DuneErosion.Service.csproj) (revision 7d7a01ed91dd10bc4795528179b5f35e5f59f1bd) @@ -16,7 +16,7 @@ - + Fisheye: Tag 7d7a01ed91dd10bc4795528179b5f35e5f59f1bd refers to a dead (removed) revision in file `Ringtoets/DuneErosion/test/Ringtoets.DuneErosion.Service.Test/DuneErosionBoundaryCalculationServiceTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/DuneErosion/test/Ringtoets.DuneErosion.Service.Test/DuneLocationCalculationServiceTest.cs =================================================================== diff -u --- Ringtoets/DuneErosion/test/Ringtoets.DuneErosion.Service.Test/DuneLocationCalculationServiceTest.cs (revision 0) +++ Ringtoets/DuneErosion/test/Ringtoets.DuneErosion.Service.Test/DuneLocationCalculationServiceTest.cs (revision 7d7a01ed91dd10bc4795528179b5f35e5f59f1bd) @@ -0,0 +1,534 @@ +// 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.IO; +using System.Linq; +using Core.Common.Base.Geometry; +using Core.Common.TestUtil; +using Core.Common.Util; +using NUnit.Framework; +using Rhino.Mocks; +using Ringtoets.Common.Data.TestUtil; +using Ringtoets.Common.Service.TestUtil; +using Ringtoets.DuneErosion.Data; +using Ringtoets.DuneErosion.Data.TestUtil; +using Ringtoets.HydraRing.Calculation.Calculator.Factory; +using Ringtoets.HydraRing.Calculation.Data.Input.Hydraulics; +using Ringtoets.HydraRing.Calculation.Exceptions; +using Ringtoets.HydraRing.Calculation.TestUtil.Calculator; + +namespace Ringtoets.DuneErosion.Service.Test +{ + [TestFixture] + public class DuneLocationCalculationServiceTest + { + 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 Validate_ValidPaths_ReturnsTrue() + { + // Setup + var valid = false; + + // Call + Action call = () => valid = DuneLocationCalculationService.Validate(validFilePath, validPreprocessorDirectory); + + // Assert + TestHelper.AssertLogMessages(call, messages => + { + string[] msgs = messages.ToArray(); + Assert.AreEqual(2, msgs.Length); + CalculationServiceTestHelper.AssertValidationStartMessage(msgs[0]); + CalculationServiceTestHelper.AssertValidationEndMessage(msgs[1]); + }); + Assert.IsTrue(valid); + } + + [Test] + public void Validate_InvalidHydraulicBoundaryDatabasePath_LogsErrorAndReturnsFalse() + { + // Setup + string notValidFilePath = Path.Combine(testDataPath, "notexisting.sqlite"); + var valid = true; + + // Call + Action call = () => valid = DuneLocationCalculationService.Validate(notValidFilePath, validPreprocessorDirectory); + + // Assert + TestHelper.AssertLogMessages(call, messages => + { + string[] msgs = messages.ToArray(); + Assert.AreEqual(3, msgs.Length); + CalculationServiceTestHelper.AssertValidationStartMessage(msgs[0]); + StringAssert.StartsWith("Herstellen van de verbinding met de hydraulische randvoorwaardendatabase is mislukt. Fout bij het lezen van bestand", msgs[1]); + CalculationServiceTestHelper.AssertValidationEndMessage(msgs[2]); + }); + Assert.IsFalse(valid); + } + + [Test] + public void Validate_ValidHydraulicBoundaryDatabaseWithoutSettings_LogsErrorAndReturnsFalse() + { + // Setup + string notValidFilePath = Path.Combine(testDataPath, "HRD nosettings.sqlite"); + var valid = false; + + // Call + Action call = () => valid = DuneLocationCalculationService.Validate(notValidFilePath, validPreprocessorDirectory); + + // Assert + TestHelper.AssertLogMessages(call, messages => + { + string[] msgs = messages.ToArray(); + Assert.AreEqual(3, msgs.Length); + CalculationServiceTestHelper.AssertValidationStartMessage(msgs[0]); + StringAssert.StartsWith("Herstellen van de verbinding met de hydraulische randvoorwaardendatabase is mislukt. Fout bij het lezen van bestand", msgs[1]); + CalculationServiceTestHelper.AssertValidationEndMessage(msgs[2]); + }); + Assert.IsFalse(valid); + } + + [Test] + public void Validate_InvalidPreprocessorDirectory_LogsErrorAndReturnsFalse() + { + // Setup + const string notValidPreprocessorDirectory = "NonExistingPreprocessorDirectory"; + var valid = true; + + // Call + Action call = () => valid = DuneLocationCalculationService.Validate(validFilePath, notValidPreprocessorDirectory); + + // Assert + TestHelper.AssertLogMessages(call, messages => + { + string[] msgs = messages.ToArray(); + Assert.AreEqual(3, msgs.Length); + CalculationServiceTestHelper.AssertValidationStartMessage(msgs[0]); + Assert.AreEqual("De bestandsmap waar de preprocessor bestanden opslaat is ongeldig. De bestandsmap bestaat niet.", msgs[1]); + CalculationServiceTestHelper.AssertValidationEndMessage(msgs[2]); + }); + Assert.IsFalse(valid); + } + + [Test] + public void Calculate_DuneLocationCalculationNull_ThrowArgumentNullException() + { + // Call + TestDelegate test = () => new DuneLocationCalculationService().Calculate(null, + 1, + validFilePath, + validPreprocessorDirectory); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("duneLocationCalculation", exception.ParamName); + } + + [Test] + [TestCase(true)] + [TestCase(false)] + public void Calculate_ValidData_CalculationStartedWithRightParameters(bool usePreprocessor) + { + // Setup + const double norm = 1.0 / 30; + string preprocessorDirectory = usePreprocessor + ? validPreprocessorDirectory + : string.Empty; + + var calculator = new TestDunesBoundaryConditionsCalculator(); + + var mockRepository = new MockRepository(); + var calculatorFactory = mockRepository.StrictMock(); + calculatorFactory.Expect(cf => cf.CreateDunesBoundaryConditionsCalculator(testDataPath, preprocessorDirectory)) + .Return(calculator); + mockRepository.ReplayAll(); + + var duneLocation = new DuneLocation(1300001, "test", new Point2D(0, 0), + new DuneLocation.ConstructionProperties + { + CoastalAreaId = 0, + Offset = 0, + Orientation = 0, + D50 = 0.000007 + }); + + using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) + { + // Call + new DuneLocationCalculationService().Calculate(new DuneLocationCalculation(duneLocation), + norm, + validFilePath, + preprocessorDirectory); + + // Assert + DunesBoundaryConditionsCalculationInput expectedInput = CreateInput(duneLocation, norm); + DunesBoundaryConditionsCalculationInput actualInput = calculator.ReceivedInputs.Single(); + AssertInput(expectedInput, actualInput); + Assert.AreEqual(usePreprocessor, actualInput.PreprocessorSetting.RunPreprocessor); + } + + mockRepository.VerifyAll(); + } + + [Test] + public void Calculate_CalculationRan_SetOutput() + { + // Setup + const double norm = 1.0 / 30; + var calculator = new TestDunesBoundaryConditionsCalculator + { + ReliabilityIndex = 3.27052, + WaterLevel = 4.82912, + WaveHeight = 2.88936, + WavePeriod = 10.65437, + Converged = true + }; + + var mockRepository = new MockRepository(); + var calculatorFactory = mockRepository.StrictMock(); + calculatorFactory.Expect(cf => cf.CreateDunesBoundaryConditionsCalculator(testDataPath, validPreprocessorDirectory)) + .Return(calculator); + mockRepository.ReplayAll(); + + var duneLocationCalculation = new DuneLocationCalculation(new TestDuneLocation()); + + // Precondition + Assert.IsNull(duneLocationCalculation.Output); + + using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) + { + // Call + Action test = () => new DuneLocationCalculationService().Calculate(duneLocationCalculation, + norm, + validFilePath, + validPreprocessorDirectory); + + // Assert + TestHelper.AssertLogMessages( + test, + messages => + { + string[] msgs = messages.ToArray(); + Assert.AreEqual(3, msgs.Length); + + CalculationServiceTestHelper.AssertCalculationStartMessage(msgs[0]); + StringAssert.StartsWith("Hydraulische randvoorwaarden berekening is uitgevoerd op de tijdelijke locatie", msgs[1]); + CalculationServiceTestHelper.AssertCalculationEndMessage(msgs[2]); + }); + double targetReliability = StatisticsConverter.ProbabilityToReliability(norm); + double calculatedProbability = StatisticsConverter.ReliabilityToProbability(calculator.ReliabilityIndex); + + DuneLocationCalculationOutput actualCalculationOutput = duneLocationCalculation.Output; + Assert.IsNotNull(actualCalculationOutput); + Assert.AreEqual(calculator.ReliabilityIndex, actualCalculationOutput.CalculatedReliability.Value); + Assert.AreEqual(calculatedProbability, actualCalculationOutput.CalculatedProbability); + Assert.AreEqual(norm, actualCalculationOutput.TargetProbability); + Assert.AreEqual(targetReliability, actualCalculationOutput.TargetReliability, actualCalculationOutput.TargetReliability.GetAccuracy()); + Assert.AreEqual(calculator.WaterLevel, actualCalculationOutput.WaterLevel, actualCalculationOutput.WaterLevel.GetAccuracy()); + Assert.AreEqual(calculator.WaveHeight, actualCalculationOutput.WaveHeight, actualCalculationOutput.WaveHeight.GetAccuracy()); + Assert.AreEqual(calculator.WavePeriod, actualCalculationOutput.WavePeriod, actualCalculationOutput.WavePeriod.GetAccuracy()); + } + + mockRepository.VerifyAll(); + } + + [Test] + public void Calculate_CalculationRanNotConverged_LogMessage() + { + // Setup + const double norm = 1.0 / 30; + var calculator = new TestDunesBoundaryConditionsCalculator + { + ReliabilityIndex = 0.01 + }; + + var mockRepository = new MockRepository(); + var calculatorFactory = mockRepository.StrictMock(); + calculatorFactory.Expect(cf => cf.CreateDunesBoundaryConditionsCalculator(testDataPath, validPreprocessorDirectory)) + .Return(calculator); + mockRepository.ReplayAll(); + + var failureMechanism = new DuneErosionFailureMechanism + { + Contribution = 10 + }; + + var duneLocation = new TestDuneLocation("Name"); + var duneLocationCalculation = new DuneLocationCalculation(duneLocation); + + using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) + { + // Call + Action test = () => new DuneLocationCalculationService().Calculate(duneLocationCalculation, + norm, + validFilePath, + validPreprocessorDirectory); + + // Assert + TestHelper.AssertLogMessages( + test, + messages => + { + string[] msgs = messages.ToArray(); + Assert.AreEqual(4, msgs.Length); + + CalculationServiceTestHelper.AssertCalculationStartMessage(msgs[0]); + Assert.AreEqual($"Hydraulische randvoorwaarden berekening voor locatie '{duneLocation.Name}' is niet geconvergeerd.", msgs[1]); + StringAssert.StartsWith("Hydraulische randvoorwaarden berekening is uitgevoerd op de tijdelijke locatie", msgs[2]); + CalculationServiceTestHelper.AssertCalculationEndMessage(msgs[3]); + }); + } + + mockRepository.VerifyAll(); + } + + [Test] + public void Calculate_CancelCalculationWithValidInput_CancelsCalculator() + { + // Setup + const double norm = 1.0 / 30; + var calculator = new TestDunesBoundaryConditionsCalculator(); + + var mockRepository = new MockRepository(); + var calculatorFactory = mockRepository.StrictMock(); + calculatorFactory.Expect(cf => cf.CreateDunesBoundaryConditionsCalculator(testDataPath, validPreprocessorDirectory)) + .Return(calculator); + mockRepository.ReplayAll(); + + var duneLocationCalculation = new DuneLocationCalculation(new TestDuneLocation()); + + using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) + { + var service = new DuneLocationCalculationService(); + calculator.CalculationFinishedHandler += (s, e) => service.Cancel(); + + // Call + service.Calculate(duneLocationCalculation, + norm, + validFilePath, + validPreprocessorDirectory); + + // Assert + Assert.IsTrue(calculator.IsCanceled); + } + + mockRepository.VerifyAll(); + } + + [Test] + public void Calculate_CalculationFailedWithExceptionAndLastErrorPresent_LogErrorAndThrowException() + { + // Setup + const double norm = 1.0 / 30; + var calculator = new TestDunesBoundaryConditionsCalculator + { + LastErrorFileContent = "An error occurred", + EndInFailure = true + }; + + var mockRepository = new MockRepository(); + var calculatorFactory = mockRepository.StrictMock(); + calculatorFactory.Expect(cf => cf.CreateDunesBoundaryConditionsCalculator(testDataPath, validPreprocessorDirectory)) + .Return(calculator); + mockRepository.ReplayAll(); + + var duneLocation = new TestDuneLocation("Name"); + var duneLocationCalculation = new DuneLocationCalculation(duneLocation); + + using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) + { + var exceptionThrown = false; + + // Call + Action call = () => + { + try + { + new DuneLocationCalculationService().Calculate(duneLocationCalculation, + norm, + validFilePath, + validPreprocessorDirectory); + } + catch (HydraRingCalculationException) + { + exceptionThrown = true; + } + }; + + // Assert + TestHelper.AssertLogMessages( + call, + messages => + { + string[] msgs = messages.ToArray(); + Assert.AreEqual(4, msgs.Length); + + CalculationServiceTestHelper.AssertCalculationStartMessage(msgs[0]); + StringAssert.StartsWith($"Hydraulische randvoorwaarden berekening voor locatie '{duneLocation.Name}' is mislukt. Bekijk het foutrapport door op details te klikken.", msgs[1]); + StringAssert.StartsWith("Hydraulische randvoorwaarden berekening is uitgevoerd op de tijdelijke locatie", msgs[2]); + CalculationServiceTestHelper.AssertCalculationEndMessage(msgs[3]); + }); + Assert.IsTrue(exceptionThrown); + Assert.IsNull(duneLocation.Calculation.Output); + } + + mockRepository.VerifyAll(); + } + + [Test] + public void Calculate_CalculationFailedWithExceptionAndNoLastErrorPresent_LogErrorAndThrowException() + { + // Setup + const double norm = 1.0 / 30; + var calculator = new TestDunesBoundaryConditionsCalculator + { + EndInFailure = true + }; + + var mockRepository = new MockRepository(); + var calculatorFactory = mockRepository.StrictMock(); + calculatorFactory.Expect(cf => cf.CreateDunesBoundaryConditionsCalculator(testDataPath, validPreprocessorDirectory)) + .Return(calculator); + mockRepository.ReplayAll(); + + var duneLocation = new TestDuneLocation("Name"); + var duneLocationCalculation = new DuneLocationCalculation(duneLocation); + + using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) + { + var exceptionThrown = false; + + // Call + Action call = () => + { + try + { + new DuneLocationCalculationService().Calculate(duneLocationCalculation, + norm, + validFilePath, + validPreprocessorDirectory); + } + catch (HydraRingCalculationException) + { + exceptionThrown = true; + } + }; + + // Assert + TestHelper.AssertLogMessages( + call, + messages => + { + string[] msgs = messages.ToArray(); + Assert.AreEqual(4, msgs.Length); + + CalculationServiceTestHelper.AssertCalculationStartMessage(msgs[0]); + Assert.AreEqual($"Hydraulische randvoorwaarden berekening voor locatie '{duneLocation.Name}' is mislukt. Er is geen foutrapport beschikbaar.", msgs[1]); + StringAssert.StartsWith("Hydraulische randvoorwaarden berekening is uitgevoerd op de tijdelijke locatie", msgs[2]); + CalculationServiceTestHelper.AssertCalculationEndMessage(msgs[3]); + }); + Assert.IsTrue(exceptionThrown); + Assert.IsNull(duneLocation.Calculation.Output); + } + + mockRepository.VerifyAll(); + } + + [Test] + public void Calculate_CalculationFailedWithoutExceptionAndWithLastErrorPresent_LogErrorAndThrowException() + { + // Setup + const double norm = 1.0 / 30; + var calculator = new TestDunesBoundaryConditionsCalculator + { + EndInFailure = false, + LastErrorFileContent = "An error occurred" + }; + + var mockRepository = new MockRepository(); + var calculatorFactory = mockRepository.StrictMock(); + calculatorFactory.Expect(cf => cf.CreateDunesBoundaryConditionsCalculator(testDataPath, validPreprocessorDirectory)) + .Return(calculator); + mockRepository.ReplayAll(); + + var duneLocation = new TestDuneLocation("Name"); + var duneLocationCalculation = new DuneLocationCalculation(duneLocation); + + using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) + { + var exceptionThrown = false; + string exceptionMessage = string.Empty; + + // Call + Action call = () => + { + try + { + new DuneLocationCalculationService().Calculate(duneLocationCalculation, + norm, + validFilePath, + validPreprocessorDirectory); + } + catch (HydraRingCalculationException e) + { + exceptionThrown = true; + exceptionMessage = e.Message; + } + }; + + // Assert + TestHelper.AssertLogMessages( + call, + messages => + { + string[] msgs = messages.ToArray(); + Assert.AreEqual(4, msgs.Length); + + CalculationServiceTestHelper.AssertCalculationStartMessage(msgs[0]); + StringAssert.StartsWith($"Hydraulische randvoorwaarden berekening voor locatie '{duneLocation.Name}' is mislukt. Bekijk het foutrapport door op details te klikken.", msgs[1]); + StringAssert.StartsWith("Hydraulische randvoorwaarden berekening is uitgevoerd op de tijdelijke locatie", msgs[2]); + CalculationServiceTestHelper.AssertCalculationEndMessage(msgs[3]); + }); + Assert.IsTrue(exceptionThrown); + Assert.IsNull(duneLocation.Calculation.Output); + Assert.AreEqual(calculator.LastErrorFileContent, exceptionMessage); + } + + mockRepository.VerifyAll(); + } + + private static void AssertInput(DunesBoundaryConditionsCalculationInput expectedInput, DunesBoundaryConditionsCalculationInput actualInput) + { + Assert.AreEqual(expectedInput.Section.SectionId, actualInput.Section.SectionId); + Assert.AreEqual(expectedInput.Section.CrossSectionNormal, actualInput.Section.CrossSectionNormal); + Assert.AreEqual(expectedInput.HydraulicBoundaryLocationId, actualInput.HydraulicBoundaryLocationId); + Assert.AreEqual(expectedInput.Beta, actualInput.Beta); + } + + private static DunesBoundaryConditionsCalculationInput CreateInput(DuneLocation duneLocation, double norm) + { + return new DunesBoundaryConditionsCalculationInput(1, duneLocation.Id, norm); + } + } +} \ No newline at end of file Index: Ringtoets/DuneErosion/test/Ringtoets.DuneErosion.Service.Test/Ringtoets.DuneErosion.Service.Test.csproj =================================================================== diff -u -rf0a2ec89952a42ebdedbea6efac5fe7196f3b46a -r7d7a01ed91dd10bc4795528179b5f35e5f59f1bd --- Ringtoets/DuneErosion/test/Ringtoets.DuneErosion.Service.Test/Ringtoets.DuneErosion.Service.Test.csproj (.../Ringtoets.DuneErosion.Service.Test.csproj) (revision f0a2ec89952a42ebdedbea6efac5fe7196f3b46a) +++ Ringtoets/DuneErosion/test/Ringtoets.DuneErosion.Service.Test/Ringtoets.DuneErosion.Service.Test.csproj (.../Ringtoets.DuneErosion.Service.Test.csproj) (revision 7d7a01ed91dd10bc4795528179b5f35e5f59f1bd) @@ -20,7 +20,7 @@ - +