public void Validate_WithoutImportedHydraulicBoundaryDatabase_LogsErrorAndReturnsFalse() { // Setup var mocks = new MockRepository(); IAssessmentSection assessmentSection = AssessmentSectionTestHelper.CreateAssessmentSectionStub(new TestCalculatableFailureMechanism(), mocks); mocks.ReplayAll(); var isValid = false; // Call void Call() => isValid = TestStructuresCalculationService.Validate(new TestStructuresCalculation(), assessmentSection); // Assert TestHelper.AssertLogMessages(Call, messages => { string[] msgs = messages.ToArray(); Assert.AreEqual(3, msgs.Length); CalculationServiceTestHelper.AssertValidationStartMessage(msgs[0]); Assert.AreEqual("Er is geen hydraulische belastingendatabase geïmporteerd.", msgs[1]); CalculationServiceTestHelper.AssertValidationEndMessage(msgs[2]); }); Assert.IsFalse(isValid); mocks.VerifyAll(); }
public void Validate_ValidCalculationValidHydraulicBoundaryDatabaseNoSettings_LogsErrorAndReturnsFalse() { // Setup var mocks = new MockRepository(); IAssessmentSection assessmentSection = AssessmentSectionTestHelper.CreateAssessmentSectionStub(new TestCalculatableFailureMechanism(), mocks, Path.Combine(testDataPath, "HRD nosettings.sqlite")); mocks.ReplayAll(); var calculation = new TestStructuresCalculation(); var isValid = false; // Call void Call() => isValid = TestStructuresCalculationService.Validate(calculation, assessmentSection); // 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 belastingendatabase is mislukt. " + "Fout bij het lezen van bestand", msgs[1]); CalculationServiceTestHelper.AssertValidationEndMessage(msgs[2]); }); Assert.IsFalse(isValid); mocks.VerifyAll(); }
public void Validate_ValidCalculationInvalidPreprocessorDirectory_LogsErrorAndReturnsFalse() { // Setup var mocks = new MockRepository(); IAssessmentSection assessmentSection = AssessmentSectionTestHelper.CreateAssessmentSectionStub( new TestCalculatableFailureMechanism(), mocks, validHydraulicBoundaryDatabaseFilePath); assessmentSection.HydraulicBoundaryDatabase.HydraulicLocationConfigurationSettings.CanUsePreprocessor = true; assessmentSection.HydraulicBoundaryDatabase.HydraulicLocationConfigurationSettings.UsePreprocessor = true; assessmentSection.HydraulicBoundaryDatabase.HydraulicLocationConfigurationSettings.PreprocessorDirectory = "NonExistingPreprocessorDirectory"; mocks.ReplayAll(); var calculation = new TestStructuresCalculation(); var isValid = true; // Call void Call() => isValid = TestStructuresCalculationService.Validate(calculation, assessmentSection); // 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(isValid); mocks.VerifyAll(); }
public void Calculate_ValidInputButIllustrationPointResultsOfIncorrectType_IllustrationPointsNotSetAndLogsWarning() { // Setup var mocks = new MockRepository(); var calculator = new TestStructuresCalculator <ExceedanceProbabilityCalculationInput> { OutputDirectory = validHydraulicBoundaryDatabaseFilePath, IllustrationPointsResult = TestGeneralResult.CreateGeneralResultWithSubMechanismIllustrationPoints() }; var calculatorFactory = mocks.StrictMock <IHydraRingCalculatorFactory>(); calculatorFactory.Expect(cf => cf.CreateStructuresCalculator <ExceedanceProbabilityCalculationInput>(null)) .IgnoreArguments() .Return(calculator); const string performedCalculationMessage = "Calculation successful"; var messageProvider = mocks.StrictMock <IStructuresCalculationMessageProvider>(); messageProvider.Expect(mp => mp.GetCalculationPerformedMessage(validHydraulicBoundaryDatabaseFilePath)).Return(performedCalculationMessage); mocks.ReplayAll(); var hydraulicBoundaryLocation = new TestHydraulicBoundaryLocation(); var calculation = new TestStructuresCalculation { InputParameters = { HydraulicBoundaryLocation = hydraulicBoundaryLocation, ShouldIllustrationPointsBeCalculated = true } }; using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) { var service = new TestStructuresCalculationService(messageProvider); // Call void Call() => service.Calculate(calculation, new GeneralTestInput(), CreateCalculationSettings()); // Assert TestHelper.AssertLogMessagesWithLevelAndLoggedExceptions(Call, messages => { Tuple <string, Level, Exception>[] 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(performedCalculationMessage, msgs[2]); CalculationServiceTestHelper.AssertCalculationEndMessage(msgs[3]); Assert.IsInstanceOf <IllustrationPointConversionException>(tupleArray[1].Item3); }); Assert.IsNotNull(calculation.Output); Assert.IsFalse(calculation.Output.HasGeneralResult); } mocks.VerifyAll(); }
public void Calculate_CalculationRanErrorInSettingIllustrationPoints_IllustrationPointsNotSetAndLogsWarning() { // Setup var mocks = new MockRepository(); var calculator = new TestStructuresCalculator <ExceedanceProbabilityCalculationInput> { OutputDirectory = validHydraulicBoundaryDatabaseFilePath, IllustrationPointsResult = GeneralResultTestFactory.CreateGeneralResultWithDuplicateStochasts() }; var calculatorFactory = mocks.StrictMock <IHydraRingCalculatorFactory>(); calculatorFactory.Expect(cf => cf.CreateStructuresCalculator <ExceedanceProbabilityCalculationInput>(null)) .IgnoreArguments() .Return(calculator); const string performedCalculationMessage = "Calculation successful"; var messageProvider = mocks.StrictMock <IStructuresCalculationMessageProvider>(); messageProvider.Expect(mp => mp.GetCalculationPerformedMessage(validHydraulicBoundaryDatabaseFilePath)).Return(performedCalculationMessage); mocks.ReplayAll(); var hydraulicBoundaryLocation = new TestHydraulicBoundaryLocation(); var calculation = new TestStructuresCalculation { InputParameters = { HydraulicBoundaryLocation = hydraulicBoundaryLocation, ShouldIllustrationPointsBeCalculated = true } }; using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) { var service = new TestStructuresCalculationService(messageProvider); // Call void Call() => service.Calculate(calculation, new GeneralTestInput(), CreateCalculationSettings()); // Assert TestHelper.AssertLogMessagesWithLevelAndLoggedExceptions(Call, messages => { Tuple <string, Level, Exception>[] tupleArray = messages.ToArray(); string[] msgs = tupleArray.Select(tuple => tuple.Item1).ToArray(); Assert.AreEqual(4, msgs.Length); CalculationServiceTestHelper.AssertCalculationStartMessage(msgs[0]); Assert.AreEqual("Fout bij het uitlezen van de illustratiepunten voor berekening Nieuwe berekening: " + "Een of meerdere stochasten hebben dezelfde naam. Het uitlezen van illustratiepunten wordt overgeslagen.", msgs[1]); Assert.AreEqual(performedCalculationMessage, msgs[2]); CalculationServiceTestHelper.AssertCalculationEndMessage(msgs[3]); }); Assert.IsNotNull(calculation.Output); Assert.IsFalse(calculation.Output.HasGeneralResult); } mocks.VerifyAll(); }
public void Calculate_ValidInputButIllustrationPointsNull_IllustrationPointsNotSetAndLogs() { // Setup const string parserMessage = "Parser error"; var mocks = new MockRepository(); var calculator = new TestStructuresCalculator <ExceedanceProbabilityCalculationInput> { OutputDirectory = validHydraulicBoundaryDatabaseFilePath, IllustrationPointsParserErrorMessage = parserMessage }; var calculatorFactory = mocks.StrictMock <IHydraRingCalculatorFactory>(); calculatorFactory.Expect(cf => cf.CreateStructuresCalculator <ExceedanceProbabilityCalculationInput>(null)) .IgnoreArguments() .Return(calculator); const string performedCalculationMessage = "Calculation successful"; var messageProvider = mocks.StrictMock <IStructuresCalculationMessageProvider>(); messageProvider.Expect(mp => mp.GetCalculationPerformedMessage(validHydraulicBoundaryDatabaseFilePath)).Return(performedCalculationMessage); mocks.ReplayAll(); var hydraulicBoundaryLocation = new TestHydraulicBoundaryLocation(); var calculation = new TestStructuresCalculation { InputParameters = { HydraulicBoundaryLocation = hydraulicBoundaryLocation, ShouldIllustrationPointsBeCalculated = true } }; using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) { var service = new TestStructuresCalculationService(messageProvider); // Call void Call() => service.Calculate(calculation, new GeneralTestInput(), CreateCalculationSettings()); // Assert TestHelper.AssertLogMessages(Call, messages => { string[] msgs = messages.ToArray(); Assert.AreEqual(4, msgs.Length); CalculationServiceTestHelper.AssertCalculationStartMessage(msgs[0]); Assert.AreEqual(parserMessage, msgs[1]); Assert.AreEqual(performedCalculationMessage, msgs[2]); CalculationServiceTestHelper.AssertCalculationEndMessage(msgs[3]); }); Assert.IsNotNull(calculation.Output); Assert.IsFalse(calculation.Output.HasGeneralResult); } mocks.VerifyAll(); }
public void Validate_AssessmentSectionNull_ThrowArgumentNullException() { // Setup var calculation = new TestStructuresCalculation(); // Call void Call() => TestStructuresCalculationService.Validate(calculation, null); // Assert var exception = Assert.Throws <ArgumentNullException>(Call); Assert.AreEqual("assessmentSection", exception.ParamName); }
public void Validate_CalculationNull_ThrowArgumentNullException() { // Setup var mocks = new MockRepository(); var assessmentSection = mocks.Stub <IAssessmentSection>(); mocks.ReplayAll(); // Call void Call() => TestStructuresCalculationService.Validate(null, assessmentSection); // Assert var exception = Assert.Throws <ArgumentNullException>(Call); Assert.AreEqual("calculation", exception.ParamName); mocks.VerifyAll(); }
public void Validate_ValidInputWithUseProcessorTrue_ReturnsTrue() { // Setup var mocks = new MockRepository(); IAssessmentSection assessmentSection = AssessmentSectionTestHelper.CreateAssessmentSectionStub( new TestCalculatableFailureMechanism(), mocks, validHydraulicBoundaryDatabaseFilePath); assessmentSection.HydraulicBoundaryDatabase.HydraulicLocationConfigurationSettings.CanUsePreprocessor = true; assessmentSection.HydraulicBoundaryDatabase.HydraulicLocationConfigurationSettings.UsePreprocessor = true; assessmentSection.HydraulicBoundaryDatabase.HydraulicLocationConfigurationSettings.PreprocessorDirectory = TestHelper.GetScratchPadPath(); mocks.ReplayAll(); var calculation = new TestStructuresCalculation { InputParameters = { Structure = new TestStructure(), HydraulicBoundaryLocation = new TestHydraulicBoundaryLocation(), IsValid = true } }; var isValid = false; // Call void Call() => isValid = TestStructuresCalculationService.Validate(calculation, assessmentSection); // Assert TestHelper.AssertLogMessages(Call, messages => { string[] msgs = messages.ToArray(); Assert.AreEqual(2, msgs.Length); CalculationServiceTestHelper.AssertValidationStartMessage(msgs[0]); CalculationServiceTestHelper.AssertValidationEndMessage(msgs[1]); }); Assert.IsTrue(isValid); mocks.VerifyAll(); }
public void Calculate_CancelCalculationWithValidInput_CancelsCalculatorAndHasNullOutput() { // Setup var hydraulicBoundaryLocation = new TestHydraulicBoundaryLocation(); var calculation = new TestStructuresCalculation { InputParameters = { HydraulicBoundaryLocation = hydraulicBoundaryLocation } }; var mocks = new MockRepository(); var calculator = new TestStructuresCalculator <ExceedanceProbabilityCalculationInput>(); var calculatorFactory = mocks.StrictMock <IHydraRingCalculatorFactory>(); calculatorFactory.Expect(cf => cf.CreateStructuresCalculator <ExceedanceProbabilityCalculationInput>(null)) .IgnoreArguments() .Return(calculator); var messageProvider = mocks.Stub <IStructuresCalculationMessageProvider>(); mocks.ReplayAll(); using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) { var service = new TestStructuresCalculationService(messageProvider); calculator.CalculationFinishedHandler += (s, e) => service.Cancel(); // Call service.Calculate(calculation, new GeneralTestInput(), CreateCalculationSettings()); // Assert Assert.IsNull(calculation.Output); Assert.IsTrue(calculator.IsCanceled); } mocks.VerifyAll(); }
public void Validate_InputInvalidAccordingToValidationRule_LogErrorAndReturnFalse() { // Setup var failureMechanism = new TestCalculatableFailureMechanism(); var mocks = new MockRepository(); IAssessmentSection assessmentSection = AssessmentSectionTestHelper.CreateAssessmentSectionStub( failureMechanism, mocks, validHydraulicBoundaryDatabaseFilePath); mocks.ReplayAll(); var calculation = new TestStructuresCalculation { InputParameters = { Structure = new TestStructure(), HydraulicBoundaryLocation = new TestHydraulicBoundaryLocation(), IsValid = false } }; var isValid = false; // Call void Call() => isValid = TestStructuresCalculationService.Validate(calculation, assessmentSection); // Assert TestHelper.AssertLogMessages(Call, messages => { string[] msgs = messages.ToArray(); Assert.AreEqual(3, msgs.Length); CalculationServiceTestHelper.AssertValidationStartMessage(msgs[0]); Assert.AreEqual("Error message", msgs[1]); CalculationServiceTestHelper.AssertValidationEndMessage(msgs[2]); }); Assert.IsFalse(isValid); mocks.VerifyAll(); }
public void Calculate_CalculationFailed_ThrowsHydraRingCalculationExceptionAndLogError(bool endInFailure, string lastErrorFileContent, string detailedReport) { // Setup var hydraulicBoundaryLocation = new TestHydraulicBoundaryLocation(); var calculation = new TestStructuresCalculation { InputParameters = { HydraulicBoundaryLocation = hydraulicBoundaryLocation } }; var mocks = new MockRepository(); var calculator = new TestStructuresCalculator <ExceedanceProbabilityCalculationInput> { EndInFailure = endInFailure, LastErrorFileContent = lastErrorFileContent }; var calculatorFactory = mocks.StrictMock <IHydraRingCalculatorFactory>(); calculatorFactory.Expect(cf => cf.CreateStructuresCalculator <ExceedanceProbabilityCalculationInput>(null)) .IgnoreArguments() .Return(calculator); const string calculationFailedMessage = "Calculation failed"; const string calculationPerformedMessage = "Calculation performed"; var messageProvider = mocks.StrictMock <IStructuresCalculationMessageProvider>(); if (endInFailure && string.IsNullOrEmpty(lastErrorFileContent)) { messageProvider.Expect(mp => mp.GetCalculationFailedMessage(calculation.Name)).Return(calculationFailedMessage); } else { messageProvider.Expect(mp => mp.GetCalculationFailedWithErrorReportMessage(calculation.Name, endInFailure && string.IsNullOrEmpty(lastErrorFileContent) ? calculator.HydraRingCalculationException.Message : lastErrorFileContent )).Return(calculationFailedMessage); } messageProvider.Expect(mp => mp.GetCalculationPerformedMessage(calculator.OutputDirectory)).Return(calculationPerformedMessage); mocks.ReplayAll(); using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) { var exceptionThrown = false; var structuresCalculationService = new TestStructuresCalculationService(messageProvider); // Call void Call() { try { structuresCalculationService.Calculate(calculation, new GeneralTestInput(), CreateCalculationSettings()); } catch (HydraRingCalculationException) { exceptionThrown = true; } } // 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(calculationPerformedMessage, msgs[2]); CalculationServiceTestHelper.AssertCalculationEndMessage(msgs[3]); }); Assert.IsTrue(exceptionThrown); Assert.IsNull(calculation.Output); } mocks.VerifyAll(); }
public void Calculate_ValidInput_InputPropertiesCorrectlySentToCalculator(bool usePreprocessor) { // Setup string preprocessorDirectory = usePreprocessor ? validPreprocessorDirectory : string.Empty; var calculationSettings = new HydraulicBoundaryCalculationSettings(validHydraulicBoundaryDatabaseFilePath, validHlcdFilePath, false, preprocessorDirectory); var mocks = new MockRepository(); var calculator = new TestStructuresCalculator <ExceedanceProbabilityCalculationInput> { OutputDirectory = validHydraulicBoundaryDatabaseFilePath }; var calculatorFactory = mocks.StrictMock <IHydraRingCalculatorFactory>(); calculatorFactory.Expect(cf => cf.CreateStructuresCalculator <ExceedanceProbabilityCalculationInput>( Arg <HydraRingCalculationSettings> .Is.NotNull)) .WhenCalled(invocation => { HydraRingCalculationSettingsTestHelper.AssertHydraRingCalculationSettings( calculationSettings, (HydraRingCalculationSettings)invocation.Arguments[0]); }) .Return(calculator); const string performedCalculationMessage = "Calculation successful"; var messageProvider = mocks.StrictMock <IStructuresCalculationMessageProvider>(); messageProvider.Expect(mp => mp.GetCalculationPerformedMessage(validHydraulicBoundaryDatabaseFilePath)).Return(performedCalculationMessage); mocks.ReplayAll(); var hydraulicBoundaryLocation = new TestHydraulicBoundaryLocation(); var calculation = new TestStructuresCalculation { InputParameters = { HydraulicBoundaryLocation = hydraulicBoundaryLocation } }; using (new HydraRingCalculatorFactoryConfig(calculatorFactory)) { var service = new TestStructuresCalculationService(messageProvider); // Call service.Calculate(calculation, new GeneralTestInput(), calculationSettings); // Assert ExceedanceProbabilityCalculationInput[] calculationInputs = calculator.ReceivedInputs.ToArray(); Assert.AreEqual(1, calculationInputs.Length); var expectedInput = new TestExceedanceProbabilityCalculationInput(hydraulicBoundaryLocation.Id); ExceedanceProbabilityCalculationInput actualInput = calculationInputs[0]; HydraRingDataEqualityHelper.AreEqual(expectedInput, actualInput); Assert.IsFalse(calculator.IsCanceled); } mocks.VerifyAll(); }