public void Run_InvalidCalculation_LogsErrorAndThrowException(bool endInFailure, string lastErrorFileContent) { // Setup const string locationName = "locationName"; const string calculationFailedMessage = "calculationFailedMessage"; var calculator = new TestDesignWaterLevelCalculator { LastErrorFileContent = lastErrorFileContent, EndInFailure = endInFailure }; var mockRepository = new MockRepository(); var calculatorFactory = mockRepository.StrictMock <IHydraRingCalculatorFactory>(); calculatorFactory.Expect(cf => cf.CreateDesignWaterLevelCalculator(null)) .IgnoreArguments() .Return(calculator); var calculationMessageProvider = mockRepository.Stub <ICalculationMessageProvider>(); if (endInFailure && string.IsNullOrEmpty(lastErrorFileContent)) { calculationMessageProvider.Expect(mp => mp.GetCalculationFailedMessage(locationName)).Return(calculationFailedMessage); } else { calculationMessageProvider.Expect(mp => mp.GetCalculationFailedWithErrorReportMessage(locationName, endInFailure && string.IsNullOrEmpty(lastErrorFileContent) ? calculator.HydraRingCalculationException.Message : lastErrorFileContent )).Return(calculationFailedMessage); } mockRepository.ReplayAll(); var hydraulicBoundaryLocationCalculation = new HydraulicBoundaryLocationCalculation(new TestHydraulicBoundaryLocation(locationName)); using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) { HydraRingCalculationException exception = null; // Call Action call = () => { try { new DesignWaterLevelCalculationService().Calculate(hydraulicBoundaryLocationCalculation, CreateCalculationSettings(), 1.0 / 30, calculationMessageProvider); } catch (HydraRingCalculationException e) { exception = e; } }; // Assert TestHelper.AssertLogMessages(call, messages => { string[] msgs = messages.ToArray(); Assert.AreEqual(4, msgs.Length); CalculationServiceTestHelper.AssertCalculationStartMessage(msgs[0]); Assert.AreEqual(calculationFailedMessage, msgs[1]); Assert.AreEqual($"Waterstand 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 <HydraRingCalculationException>(exception); } mockRepository.VerifyAll(); }
public void Calculate_ThreeCalculationsFail_ThrowsHydraRingCalculationExceptionAndLogError(bool endInFailure, string lastErrorFileContent, string detailedReport) { // Setup var waterLevel = (RoundedDouble)4.20; var waterLevelUpperBoundaryRevetment = new RoundedDouble(2, 4.00); var waterLevelLowerBoundaryRevetment = new RoundedDouble(2, 3.00); var input = new WaveConditionsInput { HydraulicBoundaryLocation = new TestHydraulicBoundaryLocation(), ForeshoreProfile = new TestForeshoreProfile(), UpperBoundaryRevetment = waterLevelUpperBoundaryRevetment, LowerBoundaryRevetment = waterLevelLowerBoundaryRevetment }; int nrOfCalculators = input.GetWaterLevels(waterLevel).Count(); var calculatorThatFails = new TestWaveConditionsCosineCalculator { EndInFailure = endInFailure, LastErrorFileContent = lastErrorFileContent }; var mockRepository = new MockRepository(); var calculatorFactory = mockRepository.StrictMock <IHydraRingCalculatorFactory>(); calculatorFactory.Expect(cf => cf.CreateWaveConditionsCosineCalculator(null)) .IgnoreArguments() .Return(calculatorThatFails) .Repeat .Times(nrOfCalculators); mockRepository.ReplayAll(); var a = (RoundedDouble)1.0; var b = (RoundedDouble)0.8; var c = (RoundedDouble)0.4; const double targetProbability = 0.2; HydraRingCalculationException exception = null; using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) { // Call void Call() { try { new TestWaveConditionsCalculationService().PublicCalculate(a, b, c, targetProbability, input, waterLevel, GetValidHydraulicBoundaryDatabase()); } catch (HydraRingCalculationException e) { exception = e; } } // Assert TestHelper.AssertLogMessages(Call, messages => { string[] msgs = messages.ToArray(); Assert.AreEqual(13, msgs.Length); var waterLevelMiddleRevetment = new RoundedDouble(2, 3.50); Assert.AreEqual($"Berekening voor waterstand '{waterLevelUpperBoundaryRevetment}' is gestart.", msgs[0]); Assert.AreEqual($"Berekening is mislukt voor waterstand '{waterLevelUpperBoundaryRevetment}'. {detailedReport}", msgs[1]); StringAssert.StartsWith("Golfcondities berekening is uitgevoerd op de tijdelijke locatie", msgs[2]); Assert.AreEqual($"Berekening voor waterstand '{waterLevelUpperBoundaryRevetment}' is beëindigd.", msgs[3]); Assert.AreEqual($"Berekening voor waterstand '{waterLevelMiddleRevetment}' is gestart.", msgs[4]); Assert.AreEqual($"Berekening is mislukt voor waterstand '{waterLevelMiddleRevetment}'. {detailedReport}", msgs[5]); StringAssert.StartsWith("Golfcondities berekening is uitgevoerd op de tijdelijke locatie", msgs[6]); Assert.AreEqual($"Berekening voor waterstand '{waterLevelMiddleRevetment}' is beëindigd.", msgs[7]); Assert.AreEqual($"Berekening voor waterstand '{waterLevelLowerBoundaryRevetment}' is gestart.", msgs[8]); Assert.AreEqual($"Berekening is mislukt voor waterstand '{waterLevelLowerBoundaryRevetment}'. {detailedReport}", msgs[9]); StringAssert.StartsWith("Golfcondities berekening is uitgevoerd op de tijdelijke locatie", msgs[10]); Assert.AreEqual($"Berekening voor waterstand '{waterLevelLowerBoundaryRevetment}' is beëindigd.", msgs[11]); Assert.AreEqual("Berekening is mislukt voor alle waterstanden.", msgs[12]); }); Assert.IsInstanceOf <HydraRingCalculationException>(exception); Assert.AreEqual("Berekening is mislukt voor alle waterstanden.", exception.Message); } mockRepository.VerifyAll(); }
public void Calculate_ThreeCalculationsFail_ThrowsHydraRingCalculationExceptionAndLogError(bool endInFailure, string lastErrorFileContent, string detailedReport) { // Setup var failureMechanism = new WaveImpactAsphaltCoverFailureMechanism(); var calculatorThatFails = new TestWaveConditionsCosineCalculator { EndInFailure = endInFailure, LastErrorFileContent = lastErrorFileContent }; var mockRepository = new MockRepository(); var calculatorFactory = mockRepository.StrictMock <IHydraRingCalculatorFactory>(); calculatorFactory.Expect(cf => cf.CreateWaveConditionsCosineCalculator(null)) .IgnoreArguments() .Return(calculatorThatFails) .Repeat .Times(3); mockRepository.ReplayAll(); IAssessmentSection assessmentSection = CreateAssessmentSectionWithHydraulicBoundaryOutput(); WaveImpactAsphaltCoverWaveConditionsCalculation calculation = GetValidCalculation(assessmentSection.HydraulicBoundaryDatabase.Locations.First()); using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) { HydraRingCalculationException exception = null; // Call void Call() { try { new WaveImpactAsphaltCoverWaveConditionsCalculationService().Calculate(calculation, assessmentSection, failureMechanism.GeneralInput); } catch (HydraRingCalculationException e) { exception = e; } } // Assert TestHelper.AssertLogMessages(Call, messages => { string[] msgs = messages.ToArray(); Assert.AreEqual(15, msgs.Length); CalculationServiceTestHelper.AssertCalculationStartMessage(msgs[0]); RoundedDouble[] waterLevels = GetWaterLevels(calculation, assessmentSection).ToArray(); RoundedDouble waterLevelUpperBoundaryRevetment = waterLevels[0]; RoundedDouble waterLevelMiddleRevetment = waterLevels[1]; RoundedDouble waterLevelLowerBoundaryRevetment = waterLevels[2]; Assert.AreEqual($"Berekening voor waterstand '{waterLevelUpperBoundaryRevetment}' is gestart.", msgs[1]); Assert.AreEqual($"Berekening is mislukt voor waterstand '{waterLevelUpperBoundaryRevetment}'. {detailedReport}", msgs[2]); StringAssert.StartsWith("Golfcondities berekening is uitgevoerd op de tijdelijke locatie", msgs[3]); Assert.AreEqual($"Berekening voor waterstand '{waterLevelUpperBoundaryRevetment}' is beëindigd.", msgs[4]); Assert.AreEqual($"Berekening voor waterstand '{waterLevelMiddleRevetment}' is gestart.", msgs[5]); Assert.AreEqual($"Berekening is mislukt voor waterstand '{waterLevelMiddleRevetment}'. {detailedReport}", msgs[6]); StringAssert.StartsWith("Golfcondities berekening is uitgevoerd op de tijdelijke locatie", msgs[7]); Assert.AreEqual($"Berekening voor waterstand '{waterLevelMiddleRevetment}' is beëindigd.", msgs[8]); Assert.AreEqual($"Berekening voor waterstand '{waterLevelLowerBoundaryRevetment}' is gestart.", msgs[9]); Assert.AreEqual($"Berekening is mislukt voor waterstand '{waterLevelLowerBoundaryRevetment}'. {detailedReport}", msgs[10]); StringAssert.StartsWith("Golfcondities berekening is uitgevoerd op de tijdelijke locatie", msgs[11]); Assert.AreEqual($"Berekening voor waterstand '{waterLevelLowerBoundaryRevetment}' is beëindigd.", msgs[12]); Assert.AreEqual("Berekening is mislukt voor alle waterstanden.", msgs[13]); CalculationServiceTestHelper.AssertCalculationEndMessage(msgs[14]); }); Assert.IsInstanceOf <HydraRingCalculationException>(exception); Assert.AreEqual("Berekening is mislukt voor alle waterstanden.", exception.Message); Assert.IsNull(calculation.Output); } mockRepository.VerifyAll(); }