Index: Riskeer/Integration/src/Riskeer.Integration.Forms/Factories/FailureMechanismAssemblyResultRowFactory.cs =================================================================== diff -u --- Riskeer/Integration/src/Riskeer.Integration.Forms/Factories/FailureMechanismAssemblyResultRowFactory.cs (revision 0) +++ Riskeer/Integration/src/Riskeer.Integration.Forms/Factories/FailureMechanismAssemblyResultRowFactory.cs (revision 5f5ac7dfe21242c07f2f4bf9e2171e53c0e80122) @@ -0,0 +1,84 @@ +// Copyright (C) Stichting Deltares 2021. All rights reserved. +// +// This file is part of Riskeer. +// +// Riskeer 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 Riskeer.Common.Data.Exceptions; +using Riskeer.Common.Data.FailurePath; +using Riskeer.Common.Forms.Helpers; +using Riskeer.Integration.Forms.Views; + +namespace Riskeer.Integration.Forms.Factories +{ + /// + /// Factory to create instances of . + /// + internal static class FailureMechanismAssemblyResultRowFactory + { + /// + /// Creates a based on its input arguments. + /// + /// The to create the row for. + /// Performs the assembly for . + /// A . + /// Thrown when any parameter is null. + public static FailureMechanismAssemblyResultRow CreateRow(IFailurePath failureMechanism, + Func performAssemblyFunc) + { + if (failureMechanism == null) + { + throw new ArgumentNullException(nameof(failureMechanism)); + } + + if (performAssemblyFunc == null) + { + throw new ArgumentNullException(nameof(performAssemblyFunc)); + } + + return failureMechanism.AssemblyResult.IsManualProbability() + ? CreateManualAssemblyRow(failureMechanism) + : CreateAutomaticAssemblyRow(failureMechanism, performAssemblyFunc); + } + + private static FailureMechanismAssemblyResultRow CreateManualAssemblyRow(IFailurePath failureMechanism) + { + FailurePathAssemblyResult assemblyResult = failureMechanism.AssemblyResult; + + string validationError = FailurePathAssemblyResultValidationHelper.GetValidationError(assemblyResult); + return !string.IsNullOrEmpty(validationError) + ? new FailureMechanismAssemblyResultRow(failureMechanism, validationError) + : new FailureMechanismAssemblyResultRow(failureMechanism, assemblyResult.ManualFailurePathAssemblyProbability); + } + + private static FailureMechanismAssemblyResultRow CreateAutomaticAssemblyRow(IFailurePath failureMechanism, + Func performAssemblyFunc) + { + try + { + double assemblyResult = performAssemblyFunc(); + return new FailureMechanismAssemblyResultRow(failureMechanism, assemblyResult); + } + catch (AssemblyException e) + { + return new FailureMechanismAssemblyResultRow(failureMechanism, e.Message); + } + } + } +} \ No newline at end of file Index: Riskeer/Integration/test/Riskeer.Integration.Forms.Test/Factories/FailureMechanismAssemblyResultRowFactoryTest.cs =================================================================== diff -u --- Riskeer/Integration/test/Riskeer.Integration.Forms.Test/Factories/FailureMechanismAssemblyResultRowFactoryTest.cs (revision 0) +++ Riskeer/Integration/test/Riskeer.Integration.Forms.Test/Factories/FailureMechanismAssemblyResultRowFactoryTest.cs (revision 5f5ac7dfe21242c07f2f4bf9e2171e53c0e80122) @@ -0,0 +1,197 @@ +// Copyright (C) Stichting Deltares 2021. All rights reserved. +// +// This file is part of Riskeer. +// +// Riskeer 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 NUnit.Framework; +using Rhino.Mocks; +using Riskeer.Common.Data.Exceptions; +using Riskeer.Common.Data.FailurePath; +using Riskeer.Integration.Forms.Factories; +using Riskeer.Integration.Forms.Views; + +namespace Riskeer.Integration.Forms.Test.Factories +{ + [TestFixture] + public class FailureMechanismAssemblyResultRowFactoryTest + { + private const int probabilityIndex = 2; + + [Test] + public void CreateRow_FailureMechanismNull_ThrowsArgumentNullException() + { + // Call + void Call() => FailureMechanismAssemblyResultRowFactory.CreateRow(null, () => double.NaN); + + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual("failureMechanism", exception.ParamName); + } + + [Test] + public void CreateRow_PerformAssemblyFuncNull_ThrowsArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var failureMechanism = mocks.Stub(); + mocks.ReplayAll(); + + // Call + void Call() => FailureMechanismAssemblyResultRowFactory.CreateRow(failureMechanism, null); + + // Assert + var exception = Assert.Throws(Call); + Assert.AreEqual("performAssemblyFunc", exception.ParamName); + mocks.VerifyAll(); + } + + [Test] + public void CreateRow_FailurePathWithPerformAssemblyFuncReturningResult_ReturnsExpectedRow() + { + // Setup + const string failureMechanismName = "Failure Mechanism Name"; + const string failureMechanismCode = "Code"; + + var mocks = new MockRepository(); + var failureMechanism = mocks.Stub(); + failureMechanism.Stub(fm => fm.Name).Return(failureMechanismName); + failureMechanism.Stub(fm => fm.Code).Return(failureMechanismCode); + failureMechanism.Stub(fm => fm.AssemblyResult).Return(new FailurePathAssemblyResult + { + ProbabilityResultType = FailurePathAssemblyProbabilityResultType.Automatic + }); + mocks.ReplayAll(); + + var random = new Random(21); + double assemblyResult = random.NextDouble(); + + // Call + FailureMechanismAssemblyResultRow row = FailureMechanismAssemblyResultRowFactory.CreateRow(failureMechanism, () => assemblyResult); + + // Assert + Assert.IsEmpty(row.ColumnStateDefinitions[probabilityIndex].ErrorText); + + Assert.AreEqual(failureMechanismName, row.Name); + Assert.AreEqual(failureMechanismCode, row.Code); + Assert.AreEqual(assemblyResult, row.Probability); + + mocks.VerifyAll(); + } + + [Test] + public void CreateRow_FailurePathWithPerformAssemblyFuncThrowingAssemblyException_ReturnsExpectedRow() + { + // Setup + const string failureMechanismName = "Failure Mechanism Name"; + const string failureMechanismCode = "Code"; + const string errorMessage = "I am an error"; + + var mocks = new MockRepository(); + var failureMechanism = mocks.Stub(); + failureMechanism.Stub(fm => fm.Name).Return(failureMechanismName); + failureMechanism.Stub(fm => fm.Code).Return(failureMechanismCode); + failureMechanism.Stub(fm => fm.AssemblyResult).Return(new FailurePathAssemblyResult + { + ProbabilityResultType = FailurePathAssemblyProbabilityResultType.Automatic + }); + mocks.ReplayAll(); + + // Call + FailureMechanismAssemblyResultRow row = FailureMechanismAssemblyResultRowFactory.CreateRow( + failureMechanism, () => throw new AssemblyException(errorMessage)); + + // Assert + Assert.AreEqual(errorMessage, row.ColumnStateDefinitions[probabilityIndex].ErrorText); + + Assert.AreEqual(failureMechanismName, row.Name); + Assert.AreEqual(failureMechanismCode, row.Code); + Assert.IsNaN(row.Probability); + + mocks.VerifyAll(); + } + + [Test] + public void CreateRow_FailurePathWithValidManualAssembly_ReturnsExpectedRow() + { + // Setup + const string failureMechanismName = "Failure Mechanism Name"; + const string failureMechanismCode = "Code"; + + var random = new Random(21); + double assemblyResult = random.NextDouble(); + + var mocks = new MockRepository(); + var failureMechanism = mocks.Stub(); + failureMechanism.Stub(fm => fm.Name).Return(failureMechanismName); + failureMechanism.Stub(fm => fm.Code).Return(failureMechanismCode); + failureMechanism.Stub(fm => fm.AssemblyResult).Return(new FailurePathAssemblyResult + { + ManualFailurePathAssemblyProbability = assemblyResult, + ProbabilityResultType = FailurePathAssemblyProbabilityResultType.Manual + }); + mocks.ReplayAll(); + + // Call + FailureMechanismAssemblyResultRow row = FailureMechanismAssemblyResultRowFactory.CreateRow(failureMechanism, () => double.NaN); + + // Assert + Assert.IsEmpty(row.ColumnStateDefinitions[probabilityIndex].ErrorText); + + Assert.AreEqual(failureMechanismName, row.Name); + Assert.AreEqual(failureMechanismCode, row.Code); + Assert.AreEqual(assemblyResult, row.Probability); + + mocks.VerifyAll(); + } + + [Test] + public void CreateRow_FailurePathWithInvalidManualAssembly_ReturnsExpectedRow() + { + // Setup + const string failureMechanismName = "Failure Mechanism Name"; + const string failureMechanismCode = "Code"; + + var mocks = new MockRepository(); + var failureMechanism = mocks.Stub(); + failureMechanism.Stub(fm => fm.Name).Return(failureMechanismName); + failureMechanism.Stub(fm => fm.Code).Return(failureMechanismCode); + failureMechanism.Stub(fm => fm.AssemblyResult).Return(new FailurePathAssemblyResult + { + ManualFailurePathAssemblyProbability = double.NaN, + ProbabilityResultType = FailurePathAssemblyProbabilityResultType.Manual + }); + mocks.ReplayAll(); + + var random = new Random(21); + + // Call + FailureMechanismAssemblyResultRow row = FailureMechanismAssemblyResultRowFactory.CreateRow(failureMechanism, () => random.NextDouble()); + + // Assert + Assert.AreEqual("Er moet een waarde worden ingevuld voor de faalkans.", row.ColumnStateDefinitions[probabilityIndex].ErrorText); + + Assert.AreEqual(failureMechanismName, row.Name); + Assert.AreEqual(failureMechanismCode, row.Code); + Assert.IsNaN(row.Probability); + + mocks.VerifyAll(); + } + } +} \ No newline at end of file