Index: Ringtoets/Common/test/Ringtoets.Common.Service.Test/DesignWaterLevelCalculationServiceTest.cs =================================================================== diff -u -r93044b3ed52a925c5321b68ff2beb886c6bf7068 -r797860a90e2e806327a86f77e20a368eaf25d6ed --- Ringtoets/Common/test/Ringtoets.Common.Service.Test/DesignWaterLevelCalculationServiceTest.cs (.../DesignWaterLevelCalculationServiceTest.cs) (revision 93044b3ed52a925c5321b68ff2beb886c6bf7068) +++ Ringtoets/Common/test/Ringtoets.Common.Service.Test/DesignWaterLevelCalculationServiceTest.cs (.../DesignWaterLevelCalculationServiceTest.cs) (revision 797860a90e2e806327a86f77e20a368eaf25d6ed) @@ -23,8 +23,10 @@ using System.IO; using System.Linq; using Core.Common.TestUtil; +using log4net.Core; using NUnit.Framework; using Rhino.Mocks; +using Ringtoets.Common.Data.Exceptions; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.Service.MessageProviders; using Ringtoets.Common.Service.TestUtil; @@ -128,7 +130,7 @@ [Test] [TestCase(true)] [TestCase(false)] - public void Calculate_ValidDesignWaterLevelCalculation_StartsCalculationWithRightParameters(bool readIllustrationPoints) + public void Calculate_ValidDesignWaterLevelCalculationAndConverges_StartsCalculationWithRightParametersAndLogs(bool readIllustrationPoints) { // Setup string validFilePath = Path.Combine(testDataPath, validFile); @@ -139,7 +141,8 @@ var calculator = new TestDesignWaterLevelCalculator { - IllustrationPointsResult = new TestGeneralResult() + IllustrationPointsResult = new TestGeneralResult(), + Converged = true }; var mockRepository = new MockRepository(); @@ -151,19 +154,28 @@ calculation.Expect(c => c.Id).Return(id); calculation.Expect(c => c.CalculateIllustrationPoints).Return(readIllustrationPoints); - var calculationMessageProvider = mockRepository.Stub(); - calculationMessageProvider.Stub(calc => calc.GetCalculatedNotConvergedMessage(locationName)).Return(string.Empty); + var calculationMessageProvider = mockRepository.StrictMock(); mockRepository.ReplayAll(); using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) { // Call - new DesignWaterLevelCalculationService().Calculate(calculation, - validFilePath, - norm, - calculationMessageProvider); + Action call = () => new DesignWaterLevelCalculationService().Calculate(calculation, + validFilePath, + norm, + calculationMessageProvider); // Assert + TestHelper.AssertLogMessages(call, messages => + { + string[] msgs = messages.ToArray(); + Assert.AreEqual(3, msgs.Length); + CalculationServiceTestHelper.AssertCalculationStartMessage(msgs[0]); + Assert.AreEqual($"Toetspeil berekening is uitgevoerd op de tijdelijke locatie '{calculator.OutputDirectory}'. " + + "Gedetailleerde invoer en uitvoer kan in de bestanden op deze locatie worden gevonden.", msgs[1]); + CalculationServiceTestHelper.AssertCalculationEndMessage(msgs[2]); + }); + AssessmentLevelCalculationInput expectedInput = CreateInput(id, norm); AssertInput(expectedInput, calculator.ReceivedInputs.Single()); Assert.IsFalse(calculator.IsCanceled); @@ -174,6 +186,129 @@ } [Test] + [TestCase(true)] + [TestCase(false)] + public void Calculate_ValidDesignWaterLevelCalculationAndDoesNotConverge_StartsCalculationWithRightParametersAndLogsWarning(bool readIllustrationPoints) + { + // Setup + string validFilePath = Path.Combine(testDataPath, validFile); + + const long id = 100; + const string locationName = "punt_flw_ 1"; + const double norm = 1.0 / 30; + + var calculator = new TestDesignWaterLevelCalculator + { + IllustrationPointsResult = new TestGeneralResult(), + Converged = false + }; + + var mockRepository = new MockRepository(); + var calculatorFactory = mockRepository.StrictMock(); + calculatorFactory.Expect(cf => cf.CreateDesignWaterLevelCalculator(testDataPath)).Return(calculator); + + var calculation = mockRepository.Stub(); + calculation.Stub(c => c.Name).Return(locationName); + calculation.Expect(c => c.Id).Return(id); + calculation.Expect(c => c.CalculateIllustrationPoints).Return(readIllustrationPoints); + + const string failedConvergenceMessage = "Did not converge"; + var calculationMessageProvider = mockRepository.StrictMock(); + calculationMessageProvider.Expect(c => c.GetCalculatedNotConvergedMessage(locationName)).Return(failedConvergenceMessage); + mockRepository.ReplayAll(); + + using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) + { + // Call + Action call = () => new DesignWaterLevelCalculationService().Calculate(calculation, + validFilePath, + norm, + calculationMessageProvider); + + // Assert + TestHelper.AssertLogMessages(call, messages => + { + string[] msgs = messages.ToArray(); + Assert.AreEqual(4, msgs.Length); + CalculationServiceTestHelper.AssertCalculationStartMessage(msgs[0]); + Assert.AreEqual(failedConvergenceMessage, msgs[1]); + Assert.AreEqual($"Toetspeil berekening is uitgevoerd op de tijdelijke locatie '{calculator.OutputDirectory}'. " + + "Gedetailleerde invoer en uitvoer kan in de bestanden op deze locatie worden gevonden.", msgs[2]); + CalculationServiceTestHelper.AssertCalculationEndMessage(msgs[3]); + }); + + AssessmentLevelCalculationInput expectedInput = CreateInput(id, norm); + AssertInput(expectedInput, calculator.ReceivedInputs.Single()); + Assert.IsFalse(calculator.IsCanceled); + Assert.IsNotNull(calculation.Output); + Assert.AreEqual(readIllustrationPoints, calculation.Output.HasIllustrationPoints); + } + mockRepository.VerifyAll(); + } + + [Test] + public void Calculate_ValidDesignWaterLevelCalculationButInvalidIllustrationPointResults_StartsCalculationWithRightParametersAndLogsWarning() + { + // Setup + string validFilePath = Path.Combine(testDataPath, validFile); + + const long id = 100; + const string locationName = "punt_flw_ 1"; + const double norm = 1.0 / 30; + + var calculator = new TestDesignWaterLevelCalculator + { + IllustrationPointsResult = TestGeneralResult.CreateGeneralResultWithFaultTreeIllustrationPoints(), + Converged = true + }; + + var mockRepository = new MockRepository(); + var calculatorFactory = mockRepository.StrictMock(); + calculatorFactory.Expect(cf => cf.CreateDesignWaterLevelCalculator(testDataPath)).Return(calculator); + + var calculation = mockRepository.Stub(); + calculation.Stub(c => c.Name).Return(locationName); + calculation.Expect(c => c.Id).Return(id); + calculation.Expect(c => c.CalculateIllustrationPoints).Return(true); + + var calculationMessageProvider = mockRepository.StrictMock(); + mockRepository.ReplayAll(); + + using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) + { + // Call + Action call = () => new DesignWaterLevelCalculationService().Calculate(calculation, + validFilePath, + norm, + calculationMessageProvider); + + // Assert + TestHelper.AssertLogMessagesAndLoggedExceptions(call, messages => + { + Tuple[] tupleArray = messages.ToArray(); + + string[] msgs = tupleArray.Select(tuple => tuple.Item1).ToArray(); + Assert.AreEqual(4, msgs.Length); + + CalculationServiceTestHelper.AssertCalculationStartMessage(msgs[0]); + Assert.AreEqual("Het uitlezen van illustratiepunten is mislukt.", msgs[1]); + Assert.AreEqual($"Toetspeil berekening is uitgevoerd op de tijdelijke locatie '{calculator.OutputDirectory}'. " + + "Gedetailleerde invoer en uitvoer kan in de bestanden op deze locatie worden gevonden.", msgs[2]); + CalculationServiceTestHelper.AssertCalculationEndMessage(msgs[3]); + + Assert.IsInstanceOf(tupleArray[1].Item3); + }); + + AssessmentLevelCalculationInput expectedInput = CreateInput(id, norm); + AssertInput(expectedInput, calculator.ReceivedInputs.Single()); + Assert.IsFalse(calculator.IsCanceled); + Assert.IsNotNull(calculation.Output); + Assert.IsFalse(calculation.Output.HasIllustrationPoints); + } + mockRepository.VerifyAll(); + } + + [Test] public void Calculate_ValidDesignWaterLevelCalculationThrowsException_ThrowsHydraRingFileParserException() { // Setup