Index: Riskeer/Common/src/Riskeer.Common.Data/Structures/StructuresCalculationScenario.cs =================================================================== diff -u -red53a0191c99f145f92dc3fff3c419e11be2e769 -r7f3ed17a0e466320f1eacc1d55fd4f99016ae6cf --- Riskeer/Common/src/Riskeer.Common.Data/Structures/StructuresCalculationScenario.cs (.../StructuresCalculationScenario.cs) (revision ed53a0191c99f145f92dc3fff3c419e11be2e769) +++ Riskeer/Common/src/Riskeer.Common.Data/Structures/StructuresCalculationScenario.cs (.../StructuresCalculationScenario.cs) (revision 7f3ed17a0e466320f1eacc1d55fd4f99016ae6cf) @@ -19,8 +19,10 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; using Core.Common.Base.Data; using Riskeer.Common.Data.Calculation; +using RiskeerCommonDataResources = Riskeer.Common.Data.Properties.Resources; namespace Riskeer.Common.Data.Structures { @@ -31,6 +33,9 @@ where T : IStructuresCalculationInput, new() { private RoundedDouble contribution; + private const int contributionNumberOfDecimalPlaces = 4; + private readonly Range contributionValidityRange = new Range(new RoundedDouble(contributionNumberOfDecimalPlaces), + new RoundedDouble(contributionNumberOfDecimalPlaces, 1.0)); /// /// Creates a new instance of . @@ -46,7 +51,17 @@ public RoundedDouble Contribution { get => contribution; - set => contribution = value.ToPrecision(contribution.NumberOfDecimalPlaces); + set + { + RoundedDouble newValue = value.ToPrecision(contributionNumberOfDecimalPlaces); + + if (!contributionValidityRange.InRange(newValue)) + { + throw new ArgumentOutOfRangeException(null, string.Format(RiskeerCommonDataResources.Contribution_must_be_within_Range_0_and_100)); + } + + contribution = newValue; + } } } } \ No newline at end of file Index: Riskeer/Common/test/Riskeer.Common.Data.Test/Structures/StructuresCalculationScenarioTest.cs =================================================================== diff -u -r4f10f77136d509ab74d4ffa22cd4d6bfc3bea60c -r7f3ed17a0e466320f1eacc1d55fd4f99016ae6cf --- Riskeer/Common/test/Riskeer.Common.Data.Test/Structures/StructuresCalculationScenarioTest.cs (.../StructuresCalculationScenarioTest.cs) (revision 4f10f77136d509ab74d4ffa22cd4d6bfc3bea60c) +++ Riskeer/Common/test/Riskeer.Common.Data.Test/Structures/StructuresCalculationScenarioTest.cs (.../StructuresCalculationScenarioTest.cs) (revision 7f3ed17a0e466320f1eacc1d55fd4f99016ae6cf) @@ -50,20 +50,43 @@ } [Test] - public void Contribution_Always_ReturnsSetValue() + [SetCulture("nl-NL")] + [TestCase(double.NaN)] + [TestCase(double.PositiveInfinity)] + [TestCase(double.NegativeInfinity)] + [TestCase(-0.1)] + [TestCase(1.0001)] + [TestCase(1.1)] + public void Contribution_SetInvalidValue_ThrowArgumentException(double newValue) { // Setup - var random = new Random(21); - RoundedDouble contribution = random.NextRoundedDouble(); + var calculationScenario = new StructuresCalculationScenario(); - var scenario = new StructuresCalculationScenario(); + // Call + void Call() => calculationScenario.Contribution = (RoundedDouble) newValue; + // Assert + TestHelper.AssertThrowsArgumentExceptionAndTestMessage(Call, "De waarde moet binnen het bereik [0% en 100%] liggen."); + } + + [Test] + [SetCulture("nl-NL")] + [TestCase(0.0)] + [TestCase(0.00001)] + [TestCase(0.0001)] + [TestCase(1.0)] + [TestCase(1.00001)] + public void Contribution_SetValidValue_ValueSetAndSandParticlesVolumicWeightUpdated(double newValue) + { + // Setup + var calculationScenario = new StructuresCalculationScenario(); + // Call - scenario.Contribution = contribution; + calculationScenario.Contribution = (RoundedDouble) newValue; // Assert - Assert.AreEqual(4, scenario.Contribution.NumberOfDecimalPlaces); - Assert.AreEqual(contribution, scenario.Contribution, scenario.Contribution.GetAccuracy()); + Assert.AreEqual(4, calculationScenario.Contribution.NumberOfDecimalPlaces); + Assert.AreEqual(newValue, calculationScenario.Contribution, calculationScenario.Contribution.GetAccuracy()); } [Test]