Index: Ringtoets/ClosingStructures/test/Ringtoets.ClosingStructures.Forms.Test/TreeNodeInfos/ClosingStructuresCalculationContextTreeNodeInfoTest.cs =================================================================== diff -u -r968cbe97d1a910b9bbb0fb52b22ece65126ed675 -r83024e3b1086e1dba93b83df874bd4781f67aa63 --- Ringtoets/ClosingStructures/test/Ringtoets.ClosingStructures.Forms.Test/TreeNodeInfos/ClosingStructuresCalculationContextTreeNodeInfoTest.cs (.../ClosingStructuresCalculationContextTreeNodeInfoTest.cs) (revision 968cbe97d1a910b9bbb0fb52b22ece65126ed675) +++ Ringtoets/ClosingStructures/test/Ringtoets.ClosingStructures.Forms.Test/TreeNodeInfos/ClosingStructuresCalculationContextTreeNodeInfoTest.cs (.../ClosingStructuresCalculationContextTreeNodeInfoTest.cs) (revision 83024e3b1086e1dba93b83df874bd4781f67aa63) @@ -19,6 +19,7 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; using System.IO; using System.Linq; using System.Windows.Forms; @@ -27,27 +28,33 @@ using Core.Common.Controls.TreeView; using Core.Common.Gui; using Core.Common.Gui.ContextMenu; +using Core.Common.Gui.Forms.MainWindow; using Core.Common.Gui.TestUtil.ContextMenu; using Core.Common.TestUtil; +using NUnit.Extensions.Forms; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.ClosingStructures.Data; +using Ringtoets.ClosingStructures.Data.TestUtil; using Ringtoets.ClosingStructures.Forms.PresentationObjects; using Ringtoets.ClosingStructures.Plugin; using Ringtoets.Common.Data; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Data.Calculation; +using Ringtoets.Common.Data.Contribution; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Data.Probability; using Ringtoets.Common.Forms.PresentationObjects; +using Ringtoets.HydraRing.Calculation.Calculator.Factory; +using Ringtoets.HydraRing.Calculation.TestUtil.Calculator; using Ringtoets.HydraRing.Data; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; using RingtoetsCommonServicesResources = Ringtoets.Common.Service.Properties.Resources; namespace Ringtoets.ClosingStructures.Forms.Test.TreeNodeInfos { [TestFixture] - public class ClosingStructuresCalculationContextTreeNodeInfoTest + public class ClosingStructuresCalculationContextTreeNodeInfoTest : NUnitFormTest { private const int contextMenuValidateIndex = 0; private const int contextMenuCalculateIndex = 1; @@ -73,7 +80,7 @@ } [TearDown] - public void TearDown() + public override void TearDown() { plugin.Dispose(); mocks.VerifyAll(); @@ -287,7 +294,7 @@ "Er is geen vakindeling geïmporteerd.", RingtoetsCommonFormsResources.CalculateIcon, false); - + TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuValidateIndex, "&Valideren", "Er is geen vakindeling geïmporteerd.", @@ -434,6 +441,223 @@ } [Test] + public void ContextMenuStrip_NoFailureMechanismSections_ContextMenuItemValidateCalculationDisabledAndTooltipSet() + { + // Setup + var failureMechanism = new ClosingStructuresFailureMechanism(); + var assessmentSectionStub = mocks.Stub(); + var calculation = new ClosingStructuresCalculation(); + + var nodeData = new ClosingStructuresCalculationContext(calculation, failureMechanism, assessmentSectionStub); + + using (var treeViewControl = new TreeViewControl()) + { + guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); + + mocks.ReplayAll(); + + plugin.Gui = guiMock; + + // Call + using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) + { + // Assert + TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuValidateIndex, + "&Valideren", + "Er is geen vakindeling geïmporteerd.", + RingtoetsCommonFormsResources.ValidateIcon, + false); + } + } + } + + [Test] + public void ContextMenuStrip_FailureMechanismSectionsSetNoHydraulicBoundaryDatabase_ContextMenuItemValidateCalculationDisabledAndTooltipSet() + { + // Setup + var failureMechanism = new ClosingStructuresFailureMechanism(); + failureMechanism.AddSection(new FailureMechanismSection("test", new[] + { + new Point2D(0, 0) + })); + + var assessmentSectionStub = mocks.Stub(); + + var calculation = new ClosingStructuresCalculation(); + var nodeData = new ClosingStructuresCalculationContext(calculation, failureMechanism, assessmentSectionStub); + + using (var treeViewControl = new TreeViewControl()) + { + guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); + + mocks.ReplayAll(); + + plugin.Gui = guiMock; + + // Call + using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) + { + // Assert + TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuValidateIndex, + RingtoetsCommonFormsResources.Validate, + RingtoetsCommonFormsResources.Plugin_AllDataAvailable_No_hydraulic_boundary_database_imported, + RingtoetsCommonFormsResources.ValidateIcon, + false); + } + } + } + + [Test] + public void ContextMenuStrip_FailureMechanismSectionsSetHydraulicBoundaryDatabaseNotValid_ContextMenuItemValidateCalculationDisabledAndTooltipSet() + { + // Setup + var failureMechanism = new ClosingStructuresFailureMechanism(); + failureMechanism.AddSection(new FailureMechanismSection("test", new[] + { + new Point2D(0, 0) + })); + + var assessmentSectionStub = mocks.Stub(); + + var calculation = new ClosingStructuresCalculation(); + var nodeData = new ClosingStructuresCalculationContext(calculation, failureMechanism, assessmentSectionStub); + + using (var treeViewControl = new TreeViewControl()) + { + guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); + + mocks.ReplayAll(); + + assessmentSectionStub.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + + plugin.Gui = guiMock; + + // Call + using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) + { + // Assert + ToolStripItem contextMenuItem = contextMenu.Items[contextMenuValidateIndex]; + + Assert.AreEqual(RingtoetsCommonFormsResources.Validate, contextMenuItem.Text); + StringAssert.Contains(string.Format(RingtoetsCommonServicesResources.Hydraulic_boundary_database_connection_failed_0_, ""), contextMenuItem.ToolTipText); + TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.ValidateIcon, contextMenuItem.Image); + Assert.IsFalse(contextMenuItem.Enabled); + } + } + } + + [Test] + public void ContextMenuStrip_FailureMechanismSectionsAndHydraulicBoundaryDatabaseSet_ContextMenuItemValidateCalculationEnabled() + { + // Setup + string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); + + var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase + { + FilePath = validFilePath, + Version = "1.0" + }; + + var failureMechanism = new ClosingStructuresFailureMechanism(); + failureMechanism.AddSection(new FailureMechanismSection("test", new[] + { + new Point2D(0, 0) + })); + + var assessmentSectionStub = mocks.Stub(); + + var calculation = new ClosingStructuresCalculation(); + var nodeData = new ClosingStructuresCalculationContext(calculation, failureMechanism, assessmentSectionStub); + + using (var treeViewControl = new TreeViewControl()) + { + guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); + + mocks.ReplayAll(); + + assessmentSectionStub.HydraulicBoundaryDatabase = hydraulicBoundaryDatabase; + + plugin.Gui = guiMock; + + // Call + using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl)) + { + // Assert + TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuValidateIndex, + RingtoetsCommonFormsResources.Validate, + RingtoetsCommonFormsResources.Validate_ToolTip, + RingtoetsCommonFormsResources.ValidateIcon); + } + } + } + + [Test] + public void GivenCalculationWithNonExistingFilePath_WhenValidatingFromContextMenu_ThenLogMessagesAdded() + { + // Given + var observerMock = mocks.StrictMock(); + + var section = new FailureMechanismSection("A", new[] + { + new Point2D(1, 2), + new Point2D(3, 4) + }); + + var failureMechanism = new ClosingStructuresFailureMechanism(); + failureMechanism.AddSection(section); + + string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); + + var hydraulicBoundaryLocation = new HydraulicBoundaryLocation(1, string.Empty, 0.0, 1.1); + var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase + { + FilePath = validFilePath, + Version = "random" + }; + hydraulicBoundaryDatabase.Locations.Add(hydraulicBoundaryLocation); + + var assessmentSectionStub = mocks.Stub(); + + var calculation = new TestClosingStructuresCalculation() + { + InputParameters = + { + HydraulicBoundaryLocation = hydraulicBoundaryLocation, + } + }; + + var calculationContext = new ClosingStructuresCalculationContext(calculation, failureMechanism, assessmentSectionStub); + + using (var treeViewControl = new TreeViewControl()) + { + guiMock.Expect(g => g.Get(calculationContext, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder()); + + mocks.ReplayAll(); + + assessmentSectionStub.HydraulicBoundaryDatabase = hydraulicBoundaryDatabase; + + plugin.Gui = guiMock; + + calculation.Attach(observerMock); + + using (ContextMenuStrip contextMenuStrip = info.ContextMenuStrip(calculationContext, null, treeViewControl)) + { + // When + Action action = () => contextMenuStrip.Items[contextMenuValidateIndex].PerformClick(); + + // Then + TestHelper.AssertLogMessages(action, messages => + { + var msgs = messages.ToArray(); + Assert.AreEqual(2, msgs.Length); + StringAssert.StartsWith(string.Format("Validatie van '{0}' gestart om: ", calculation.Name), msgs[0]); + StringAssert.StartsWith(string.Format("Validatie van '{0}' beëindigd om: ", calculation.Name), msgs[1]); + }); + } + } + } + + [Test] public void OnNodeRemoved_ParentIsCalculationGroupContext_RemoveCalculationFromGroup() { // Setup @@ -469,6 +693,39 @@ CollectionAssert.DoesNotContain(group.Children, elementToBeRemoved); } + [Test] + public void OnNodeRemoved_CalculationInGroupAssignedToSection_CalculationDetachedFromSection() + { + // Setup + var group = new CalculationGroup(); + var failureMechanism = new ClosingStructuresFailureMechanism(); + var elementToBeRemoved = new ClosingStructuresCalculation(); + var assessmentSectionStub = mocks.Stub(); + var calculationContext = new ClosingStructuresCalculationContext(elementToBeRemoved, + failureMechanism, + assessmentSectionStub); + var groupContext = new ClosingStructuresCalculationGroupContext(group, + failureMechanism, + assessmentSectionStub); + + mocks.ReplayAll(); + + group.Children.Add(elementToBeRemoved); + + failureMechanism.AddSection(new FailureMechanismSection("section", new[] + { + new Point2D(0, 0) + })); + + failureMechanism.SectionResults.First().Calculation = elementToBeRemoved; + + // Call + info.OnNodeRemoved(calculationContext, groupContext); + + // Assert + Assert.IsNull(failureMechanism.SectionResults.First().Calculation); + } + private class TestClosingStructuresOutput : ProbabilityAssessmentOutput { public TestClosingStructuresOutput() : base(0, 0, 0, 0, 0) {}