// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets 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 System.ComponentModel; using System.Linq; using Core.Common.Base; using Core.Common.Gui.PropertyBag; using Core.Common.TestUtil; using NUnit.Extensions.Forms; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Data.Contribution; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Integration.Data; using Ringtoets.Integration.Forms.PropertyClasses; using Ringtoets.Integration.Forms.Views; using Ringtoets.Integration.Plugin.Handlers; namespace Ringtoets.Integration.Forms.Test.PropertyClasses { [TestFixture] public class FailureMechanismContributionPropertiesTest : NUnitFormTest { [Test] public void DefaultConstructor_ExpectedValues() { // Call var properties = new FailureMechanismContributionProperties(); // Assert Assert.IsInstanceOf>(properties); Assert.IsNull(properties.Data); } [Test] public void Constructor_Always_PropertiesHaveExpectedAttributeValues() { // Call var failureMechanismContributionProperties = new FailureMechanismContributionProperties(); // Assert var dynamicPropertyBag = new DynamicPropertyBag(failureMechanismContributionProperties); PropertyDescriptorCollection dynamicProperties = dynamicPropertyBag.GetProperties(new Attribute[] { new BrowsableAttribute(true) }); Assert.AreEqual(2, dynamicProperties.Count); var expectedCategory = "Algemeen"; PropertyDescriptor compositionProperty = dynamicProperties[0]; PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(compositionProperty, expectedCategory, "Trajecttype", "Selecteer het type traject, bepalend voor de faalkansbegroting."); PropertyDescriptor returnPeriodProperty = dynamicProperties[1]; PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(returnPeriodProperty, expectedCategory, "Norm (terugkeertijd) [jaar]", "Terugkeertijd van de norm, gelijk aan 1/norm."); } [Test] public void GetProperties_WithData_ReturnExpectedValues() { // Setup var mocks = new MockRepository(); var assessmentSectionComposition = AssessmentSectionComposition.DikeAndDune; var assessmentSection = mocks.Stub(); assessmentSection.Stub(section => section.Composition).Return(assessmentSectionComposition); mocks.ReplayAll(); int returnPeriod = 30000; var failureMechanisms = Enumerable.Empty(); var contribution = new FailureMechanismContribution(failureMechanisms, 1.1, 1.0/returnPeriod); var properties = new FailureMechanismContributionProperties { Data = contribution, AssessmentSection = assessmentSection }; // Call int returnPeriodPropertyValue = properties.ReturnPeriod; AssessmentSectionComposition compositionPropertyValue = properties.AssessmentSectionComposition; // Assert Assert.AreEqual(returnPeriod, returnPeriodPropertyValue); Assert.AreEqual(assessmentSectionComposition, compositionPropertyValue); mocks.VerifyAll(); } [Test] public void NormChangeHandler_SetNull_ThrowsArgumentNullException() { // Call TestDelegate call = () => new FailureMechanismContributionProperties { NormChangeHandler = null }; // Assert ArgumentException exception = TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, "NormChangeHandler is null"); string paramName = exception.ParamName; Assert.AreEqual("value", paramName); } [Test] public void CompositionChangeHandler_SetNull_ThrowsArgumentNullException() { // Call TestDelegate call = () => new FailureMechanismContributionProperties { CompositionChangeHandler = null }; // Assert ArgumentException exception = TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, "CompositionChangeHandler is null"); string paramName = exception.ParamName; Assert.AreEqual("value", paramName); } [Test] public void GivenReturnPeriod_WhenConfirmingReturnPeriodValueChange_ThenReturnPeriodSetAndNotifiesObserver() { // Given AssessmentSection assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); FailureMechanismContribution failureMechanismContribution = assessmentSection.FailureMechanismContribution; var normChangeHandler = new FailureMechanismContributionNormChangeHandler(); var mocks = new MockRepository(); var compositionChangeHandler = mocks.Stub(); mocks.ReplayAll(); var observer = mocks.StrictMock(); failureMechanismContribution.Attach(observer); observer.Expect(o => o.UpdateObserver()); mocks.ReplayAll(); var properties = new FailureMechanismContributionProperties { Data = failureMechanismContribution, AssessmentSection = assessmentSection, NormChangeHandler = normChangeHandler, CompositionChangeHandler = compositionChangeHandler }; DialogBoxHandler = (name, wnd) => { var messageBox = new MessageBoxTester(wnd); messageBox.ClickOk(); }; // When const int newReturnPeriod = 200; properties.ReturnPeriod = newReturnPeriod; // Then Assert.AreEqual(newReturnPeriod, properties.ReturnPeriod); Assert.AreEqual(1.0/newReturnPeriod, failureMechanismContribution.Norm); mocks.VerifyAll(); } [Test] public void GivenReturnPeriod_WhenCancellingReturnPeriodValueChange_ThenDataSameObserversNotNotified() { // Given AssessmentSection assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); FailureMechanismContribution failureMechanismContribution = assessmentSection.FailureMechanismContribution; int originalReturnPeriod = Convert.ToInt32(1/failureMechanismContribution.Norm); var normChangeHandler = new FailureMechanismContributionNormChangeHandler(); var mocks = new MockRepository(); var compositionChangeHandler = mocks.Stub(); mocks.ReplayAll(); var observer = mocks.StrictMock(); failureMechanismContribution.Attach(observer); mocks.ReplayAll(); var properties = new FailureMechanismContributionProperties { Data = failureMechanismContribution, AssessmentSection = assessmentSection, NormChangeHandler = normChangeHandler, CompositionChangeHandler = compositionChangeHandler }; DialogBoxHandler = (name, wnd) => { var messageBox = new MessageBoxTester(wnd); messageBox.ClickCancel(); }; // When const int newReturnPeriod = 200; properties.ReturnPeriod = newReturnPeriod; // Then Assert.AreEqual(originalReturnPeriod, properties.ReturnPeriod); Assert.AreEqual(1.0/originalReturnPeriod, failureMechanismContribution.Norm); mocks.VerifyAll(); } [Test] public void GivenAssessmentSectionComposition_WhenConfirmingCompositionValueChange_ThenAssessmentSectionCompositionSetAndNotifiesObserver() { // Given const AssessmentSectionComposition originalComposition = AssessmentSectionComposition.Dike; AssessmentSection assessmentSection = new AssessmentSection(originalComposition); FailureMechanismContribution failureMechanismContribution = assessmentSection.FailureMechanismContribution; var compositionChangeHandler = new AssessmentSectionCompositionChangeHandler(); var mocks = new MockRepository(); var normChangeHandler = mocks.Stub(); var observer = mocks.StrictMock(); failureMechanismContribution.Attach(observer); observer.Expect(o => o.UpdateObserver()); mocks.ReplayAll(); var properties = new FailureMechanismContributionProperties() { Data = failureMechanismContribution, AssessmentSection = assessmentSection, NormChangeHandler = normChangeHandler, CompositionChangeHandler = compositionChangeHandler }; DialogBoxHandler = (name, wnd) => { var messageBox = new MessageBoxTester(wnd); messageBox.ClickOk(); }; // When const AssessmentSectionComposition newComposition = AssessmentSectionComposition.DikeAndDune; properties.AssessmentSectionComposition = newComposition; // Then Assert.AreEqual(newComposition, properties.AssessmentSectionComposition); Assert.AreEqual(newComposition, assessmentSection.Composition); mocks.VerifyAll(); } [Test] public void GivenAssessmentSectionComposition_WhenCancellingCompositionValueChange__ThenDataSameObserversNotNotified() { // Given const AssessmentSectionComposition originalComposition = AssessmentSectionComposition.Dike; AssessmentSection assessmentSection = new AssessmentSection(originalComposition); FailureMechanismContribution failureMechanismContribution = assessmentSection.FailureMechanismContribution; var compositionChangeHandler = new AssessmentSectionCompositionChangeHandler(); var mocks = new MockRepository(); var normChangeHandler = mocks.Stub(); var observer = mocks.StrictMock(); failureMechanismContribution.Attach(observer); mocks.ReplayAll(); var properties = new FailureMechanismContributionProperties() { Data = failureMechanismContribution, AssessmentSection = assessmentSection, NormChangeHandler = normChangeHandler, CompositionChangeHandler = compositionChangeHandler }; DialogBoxHandler = (name, wnd) => { var messageBox = new MessageBoxTester(wnd); messageBox.ClickCancel(); }; // When const AssessmentSectionComposition newComposition = AssessmentSectionComposition.DikeAndDune; properties.AssessmentSectionComposition = newComposition; // Then Assert.AreEqual(originalComposition, properties.AssessmentSectionComposition); Assert.AreEqual(originalComposition, assessmentSection.Composition); mocks.VerifyAll(); } [Test] public void ReturnPeriod_ValueChanges_NotifiesChangedObjectsInAssessmentSection() { // Setup const int returnPeriod = 200; const double norm = 1.0/returnPeriod; AssessmentSection assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); var mocks = new MockRepository(); var observable1 = mocks.StrictMock(); observable1.Expect(o => o.NotifyObservers()); var observable2 = mocks.StrictMock(); observable2.Expect(o => o.NotifyObservers()); var normChangeHandler = mocks.StrictMock(); normChangeHandler.Expect(h => h.ConfirmNormChange()).Return(true); normChangeHandler.Expect(h => h.ChangeNorm(assessmentSection, norm)) .Return(new[] { observable1, observable2 }); mocks.ReplayAll(); var properties = new FailureMechanismContributionProperties { AssessmentSection = assessmentSection, NormChangeHandler = normChangeHandler, Data = assessmentSection.FailureMechanismContribution }; // Call properties.ReturnPeriod = returnPeriod; // Assert mocks.VerifyAll(); } [Test] [TestCase(int.MinValue)] [TestCase(int.MaxValue)] [TestCase(99)] [TestCase(1000001)] public void ReturnPeriod_InvalidValue_ThrowsArgumentOutOfRangeException(int invalidReturnPeriod) { // Setup var mocks = new MockRepository(); var assessmentSectionComposition = AssessmentSectionComposition.DikeAndDune; var assessmentSection = mocks.Stub(); assessmentSection.Stub(section => section.Composition).Return(assessmentSectionComposition); mocks.ReplayAll(); var failureMechanisms = Enumerable.Empty(); var contribution = new FailureMechanismContribution(failureMechanisms, 1.1, 1.0/200); var properties = new FailureMechanismContributionProperties() { Data = contribution, AssessmentSection = assessmentSection }; // Call TestDelegate call = () => properties.ReturnPeriod = invalidReturnPeriod; // Assert string expectedMessage = "De waarde voor de 'Norm (terugkeertijd)' moet in het bereik [100, 1000000] liggen."; TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage); } [Test] [TestCase(AssessmentSectionComposition.Dike, AssessmentSectionComposition.Dune)] [TestCase(AssessmentSectionComposition.Dike, AssessmentSectionComposition.DikeAndDune)] [TestCase(AssessmentSectionComposition.Dune, AssessmentSectionComposition.Dike)] [TestCase(AssessmentSectionComposition.Dune, AssessmentSectionComposition.DikeAndDune)] [TestCase(AssessmentSectionComposition.DikeAndDune, AssessmentSectionComposition.Dike)] [TestCase(AssessmentSectionComposition.DikeAndDune, AssessmentSectionComposition.Dune)] public void AssessmentSectionComposition_CompositionValueChanges_NotifiesChangedObjectsInAssessmentSection( AssessmentSectionComposition initialComposition, AssessmentSectionComposition newComposition) { // Setup var assessmentSection = new AssessmentSection(initialComposition); var mocks = new MockRepository(); var observable1 = mocks.StrictMock(); observable1.Expect(o => o.NotifyObservers()); var observable2 = mocks.StrictMock(); observable2.Expect(o => o.NotifyObservers()); var compositionChangeHandler = mocks.StrictMock(); compositionChangeHandler.Expect(h => h.ConfirmCompositionChange()) .Return(true); compositionChangeHandler.Expect(h => h.ChangeComposition(assessmentSection, newComposition)) .Return(new[] { observable1, observable2 }); mocks.ReplayAll(); var properties = new FailureMechanismContributionProperties { AssessmentSection = assessmentSection, CompositionChangeHandler = compositionChangeHandler, Data = assessmentSection.FailureMechanismContribution }; // Call properties.AssessmentSectionComposition = newComposition; // Assert mocks.VerifyAll(); } } }