// Copyright (C) Stichting Deltares 2017. 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.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Core.Common.Base;
using Core.Common.Base.Geometry;
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.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.Hydraulics;
using Ringtoets.Common.Data.Structures;
using Ringtoets.Common.Data.TestUtil;
using Ringtoets.Common.Forms.PresentationObjects;
using Ringtoets.Common.Service.TestUtil;
using Ringtoets.HydraRing.Calculation.Calculator.Factory;
using Ringtoets.HydraRing.Calculation.Data.Input.Structures;
using Ringtoets.HydraRing.Calculation.TestUtil.Calculator;
using Ringtoets.StabilityPointStructures.Data;
using Ringtoets.StabilityPointStructures.Data.TestUtil;
using Ringtoets.StabilityPointStructures.Forms.PresentationObjects;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
namespace Ringtoets.StabilityPointStructures.Plugin.Test.TreeNodeInfos
{
[TestFixture]
public class StabilityPointStructuresCalculationContextTreeNodeInfoTest : NUnitFormTest
{
private const int contextMenuDuplicateIndex = 2;
private const int contextMenuUpdateForeshoreProfileIndex = 5;
private const int contextMenuUpdateStructureIndex = 6;
private const int contextMenuValidateIndex = 8;
private const int contextMenuCalculateIndex = 9;
private const int contextMenuClearIndex = 11;
private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "HydraulicBoundaryDatabaseImporter");
private MockRepository mocks;
private StabilityPointStructuresPlugin plugin;
private TreeNodeInfo info;
[Test]
public void Initialized_Always_ExpectedPropertiesSet()
{
// Setup
mocks.ReplayAll();
// Assert
Assert.IsNotNull(info.Text);
Assert.IsNull(info.ForeColor);
Assert.IsNotNull(info.Image);
Assert.IsNotNull(info.ContextMenuStrip);
Assert.IsNotNull(info.EnsureVisibleOnCreate);
Assert.IsNull(info.ExpandOnCreate);
Assert.IsNotNull(info.ChildNodeObjects);
Assert.IsNotNull(info.CanRename);
Assert.IsNotNull(info.OnNodeRenamed);
Assert.IsNotNull(info.CanRemove);
Assert.IsNotNull(info.OnNodeRemoved);
Assert.IsNull(info.CanCheck);
Assert.IsNull(info.IsChecked);
Assert.IsNull(info.OnNodeChecked);
Assert.IsNotNull(info.CanDrag);
Assert.IsNull(info.CanDrop);
Assert.IsNull(info.CanInsert);
Assert.IsNull(info.OnDrop);
}
[Test]
public void Image_Always_ReturnsCalculationIcon()
{
// Setup
mocks.ReplayAll();
// Call
Image image = info.Image(null);
// Assert
TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.CalculationIcon, image);
}
[Test]
[TestCase(true)]
[TestCase(false)]
public void ChildNodeObjects_Always_ReturnsChildrenOfData(bool hasOutput)
{
var assessmentSection = mocks.Stub();
mocks.ReplayAll();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation
{
Output = hasOutput ? new TestStructuresOutput() : null
};
var failureMechanism = new StabilityPointStructuresFailureMechanism();
var calculationContext = new StabilityPointStructuresCalculationContext(calculation, parent, failureMechanism, assessmentSection);
// Call
object[] children = info.ChildNodeObjects(calculationContext).ToArray();
// Assert
Assert.AreEqual(3, children.Length);
var comment = children[0] as Comment;
Assert.AreSame(calculationContext.WrappedData.Comments, comment);
var stabilityPointStructuresInputContext = children[1] as StabilityPointStructuresInputContext;
Assert.IsNotNull(stabilityPointStructuresInputContext);
Assert.AreSame(calculationContext.WrappedData.InputParameters, stabilityPointStructuresInputContext.WrappedData);
var structuresOutputContext = children[2] as StructuresOutputContext;
Assert.IsNotNull(structuresOutputContext);
Assert.AreSame(calculationContext.WrappedData, structuresOutputContext.WrappedData);
}
[Test]
public void ContextMenuStrip_Always_CallsContextMenuBuilderMethods()
{
// Setup
var failureMechanism = new StabilityPointStructuresFailureMechanism();
var assessmentSection = mocks.Stub();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation();
var nodeData = new StabilityPointStructuresCalculationContext(calculation, parent, failureMechanism, assessmentSection);
var menuBuilder = mocks.StrictMock();
using (mocks.Ordered())
{
menuBuilder.Expect(mb => mb.AddExportItem()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddSeparator()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddSeparator()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddRenameItem()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddSeparator()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddSeparator()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddDeleteItem()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddSeparator()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddCollapseAllItem()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddExpandAllItem()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddSeparator()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.AddPropertiesItem()).Return(menuBuilder);
menuBuilder.Expect(mb => mb.Build()).Return(null);
}
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder);
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
// Call
info.ContextMenuStrip(nodeData, null, treeViewControl);
}
// Assert
// Assert expectancies called in TearDown()
}
[Test]
public void ContextMenuStrip_Always_AddCustomItems()
{
// Setup
string validFilePath = Path.Combine(testDataPath, "complete.sqlite");
var assessmentSection = mocks.Stub();
assessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase
{
FilePath = validFilePath,
Version = "random"
};
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation();
var nodeData = new StabilityPointStructuresCalculationContext(calculation, parent, failureMechanism, assessmentSection);
var menuBuilder = new CustomItemsOnlyContextMenuBuilder();
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder);
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
// Call
using (ContextMenuStrip menu = info.ContextMenuStrip(nodeData, assessmentSection, treeViewControl))
{
// Assert
Assert.AreEqual(18, menu.Items.Count);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuDuplicateIndex,
"D&upliceren",
"Dupliceer deze berekening.",
RingtoetsCommonFormsResources.CopyHS);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuUpdateForeshoreProfileIndex,
"&Bijwerken voorlandprofiel...",
"Er moet een voorlandprofiel geselecteerd zijn.",
RingtoetsCommonFormsResources.UpdateItemIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuUpdateStructureIndex,
"&Bijwerken kunstwerk...",
"Er moet een kunstwerk geselecteerd zijn.",
RingtoetsCommonFormsResources.UpdateItemIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuValidateIndex,
"&Valideren",
"Valideer de invoer voor deze berekening.",
RingtoetsCommonFormsResources.ValidateIcon);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuCalculateIndex,
"Be&rekenen",
"Voer deze berekening uit.",
RingtoetsCommonFormsResources.CalculateIcon);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuClearIndex,
"&Wis uitvoer...",
"Deze berekening heeft geen uitvoer om te wissen.",
RingtoetsCommonFormsResources.ClearIcon,
false);
}
}
}
[Test]
public void ContextMenuStrip_CalculationWithoutStructure_ContextMenuItemUpdateStructureDisabledAndToolTipSet()
{
// Setup
var assessmentSection = mocks.Stub();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation();
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var nodeData = new StabilityPointStructuresCalculationContext(calculation, parent, failureMechanism, assessmentSection);
var menuBuilder = new CustomItemsOnlyContextMenuBuilder();
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder);
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
// Call
using (ContextMenuStrip menu = info.ContextMenuStrip(nodeData, assessmentSection, treeViewControl))
{
// Assert
TestHelper.AssertContextMenuStripContainsItem(menu,
contextMenuUpdateStructureIndex,
"&Bijwerken kunstwerk...",
"Er moet een kunstwerk geselecteerd zijn.",
RingtoetsCommonFormsResources.UpdateItemIcon,
false);
}
}
}
[Test]
public void ContextMenuStrip_CalculationWithStructureAndInputInSync_ContextMenuItemUpdateStructureDisabledAndToolTipSet()
{
// Setup
var assessmentSection = mocks.Stub();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation
{
InputParameters =
{
Structure = new TestStabilityPointStructure()
}
};
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var nodeData = new StabilityPointStructuresCalculationContext(calculation, parent, failureMechanism, assessmentSection);
var menuBuilder = new CustomItemsOnlyContextMenuBuilder();
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder);
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
// Call
using (ContextMenuStrip menu = info.ContextMenuStrip(nodeData, assessmentSection, treeViewControl))
{
// Assert
TestHelper.AssertContextMenuStripContainsItem(menu,
contextMenuUpdateStructureIndex,
"&Bijwerken kunstwerk...",
"Er zijn geen wijzigingen om bij te werken.",
RingtoetsCommonFormsResources.UpdateItemIcon,
false);
}
}
}
[Test]
public void ContextMenuStrip_CalculationWithStructureAndInputOutOfSync_ContextMenuItemUpdateStructureEnabledAndToolTipSet()
{
// Setup
var assessmentSection = mocks.Stub();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation
{
InputParameters =
{
Structure = new TestStabilityPointStructure()
}
};
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var nodeData = new StabilityPointStructuresCalculationContext(calculation, parent, failureMechanism, assessmentSection);
var menuBuilder = new CustomItemsOnlyContextMenuBuilder();
ChangeStructure(calculation.InputParameters.Structure);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder);
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
// Call
using (ContextMenuStrip menu = info.ContextMenuStrip(nodeData, assessmentSection, treeViewControl))
{
// Assert
TestHelper.AssertContextMenuStripContainsItem(menu,
contextMenuUpdateStructureIndex,
"&Bijwerken kunstwerk...",
"Berekening bijwerken met het kunstwerk.",
RingtoetsCommonFormsResources.UpdateItemIcon);
}
}
}
[Test]
public void GivenCalculationWithoutOutputAndWithInputOutOfSync_WhenUpdateStructureClicked_ThenNoInquiryAndCalculationUpdatedAndInputObserverNotified()
{
// Given
var calculationObserver = mocks.StrictMock();
var calculationInputObserver = mocks.StrictMock();
calculationInputObserver.Expect(o => o.UpdateObserver());
var assessmentSection = mocks.Stub();
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation
{
InputParameters =
{
Structure = new TestStabilityPointStructure()
}
};
var nodeData = new StabilityPointStructuresCalculationContext(calculation,
parent,
failureMechanism,
assessmentSection);
calculation.Attach(calculationObserver);
calculation.InputParameters.Attach(calculationInputObserver);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(g => g.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
ChangeStructure(calculation.InputParameters.Structure);
using (ContextMenuStrip contextMenuStrip = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// When
contextMenuStrip.Items[contextMenuUpdateStructureIndex].PerformClick();
// Then
Assert.IsTrue(calculation.InputParameters.IsStructureInputSynchronized);
}
}
}
[Test]
[TestCase(true)]
[TestCase(false)]
public void GivenCalculationWithOutputAndWithInputOutOfSync_WhenUpdateStructureClicked_ThenInquiryAndExpectedOutputAndNotifications(bool continuation)
{
// Given
var calculationObserver = mocks.StrictMock();
var calculationInputObserver = mocks.StrictMock();
var assessmentSection = mocks.Stub();
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation
{
InputParameters =
{
Structure = new TestStabilityPointStructure()
},
Output = new TestStructuresOutput()
};
var nodeData = new StabilityPointStructuresCalculationContext(calculation,
parent,
failureMechanism,
assessmentSection);
calculation.Attach(calculationObserver);
calculation.InputParameters.Attach(calculationInputObserver);
if (continuation)
{
calculationObserver.Expect(o => o.UpdateObserver());
calculationInputObserver.Expect(o => o.UpdateObserver());
}
var messageBoxText = "";
DialogBoxHandler = (name, wnd) =>
{
var helper = new MessageBoxTester(wnd);
messageBoxText = helper.Text;
if (continuation)
{
helper.ClickOk();
}
else
{
helper.ClickCancel();
}
};
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(g => g.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
ChangeStructure(calculation.InputParameters.Structure);
using (ContextMenuStrip contextMenuStrip = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// When
contextMenuStrip.Items[contextMenuUpdateStructureIndex].PerformClick();
// Then
Assert.AreEqual(continuation, calculation.InputParameters.IsStructureInputSynchronized);
Assert.AreEqual(!continuation, calculation.HasOutput);
}
}
string expectedMessageBoxText = "Als u kiest voor bijwerken, dan wordt het resultaat van deze berekening " +
$"verwijderd.{Environment.NewLine}{Environment.NewLine}Weet u zeker dat u wilt doorgaan?";
Assert.AreEqual(expectedMessageBoxText, messageBoxText);
}
[Test]
public void ContextMenuStrip_NoHydraulicBoundaryDatabase_ContextMenuItemPerformCalculationAndValidationDisabledAndTooltipSet()
{
// Setup
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var assessmentSection = mocks.Stub();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation();
var nodeData = new StabilityPointStructuresCalculationContext(calculation, parent, failureMechanism, assessmentSection);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
// Call
using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// Assert
TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuCalculateIndex,
"Be&rekenen",
"Er is geen hydraulische randvoorwaardendatabase geïmporteerd.",
RingtoetsCommonFormsResources.CalculateIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuValidateIndex,
"&Valideren",
"Er is geen hydraulische randvoorwaardendatabase geïmporteerd.",
RingtoetsCommonFormsResources.ValidateIcon,
false);
}
}
}
[Test]
public void ContextMenuStrip_HydraulicBoundaryDatabaseNotValid_ContextMenuItemPerformCalculationAndValidationDisabledAndTooltipSet()
{
// Setup
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var assessmentSection = mocks.Stub();
assessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation();
var nodeData = new StabilityPointStructuresCalculationContext(calculation, parent, failureMechanism, assessmentSection);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
// Call
using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// Assert
ToolStripItem calculateContextMenuItem = contextMenu.Items[contextMenuCalculateIndex];
Assert.AreEqual("Be&rekenen", calculateContextMenuItem.Text);
StringAssert.Contains("Herstellen van de verbinding met de hydraulische randvoorwaardendatabase is mislukt. ", calculateContextMenuItem.ToolTipText);
TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.CalculateIcon, calculateContextMenuItem.Image);
Assert.IsFalse(calculateContextMenuItem.Enabled);
ToolStripItem validateContextMenuItem = contextMenu.Items[contextMenuValidateIndex];
Assert.AreEqual("&Valideren", validateContextMenuItem.Text);
StringAssert.Contains("Herstellen van de verbinding met de hydraulische randvoorwaardendatabase is mislukt.", validateContextMenuItem.ToolTipText);
TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.ValidateIcon, validateContextMenuItem.Image);
Assert.IsFalse(validateContextMenuItem.Enabled);
}
}
}
[Test]
public void ContextMenuStrip_FailureMechanismContributionZero_ContextMenuItemPerformCalculationAndValidationDisabledAndTooltipSet()
{
// Setup
string validFilePath = Path.Combine(testDataPath, "complete.sqlite");
var assessmentSection = mocks.Stub();
assessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase
{
FilePath = validFilePath,
Version = "1.0"
};
var failureMechanism = new StabilityPointStructuresFailureMechanism();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation();
var nodeData = new StabilityPointStructuresCalculationContext(calculation, parent, failureMechanism, assessmentSection);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
// Call
using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// Assert
TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuCalculateIndex,
"Be&rekenen",
"De bijdrage van dit toetsspoor is nul.",
RingtoetsCommonFormsResources.CalculateIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuValidateIndex,
"&Valideren",
"De bijdrage van dit toetsspoor is nul.",
RingtoetsCommonFormsResources.ValidateIcon,
false);
}
}
}
[Test]
public void ContextMenuStrip_AllRequiredInputSet_ContextMenuItemPerformCalculationAndValidationEnabled()
{
// Setup
string validFilePath = Path.Combine(testDataPath, "complete.sqlite");
var assessmentSection = mocks.Stub();
assessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase
{
FilePath = validFilePath,
Version = "1.0"
};
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation();
var nodeData = new StabilityPointStructuresCalculationContext(calculation, parent, failureMechanism, assessmentSection);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
// Call
using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// Assert
TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuCalculateIndex,
"Be&rekenen",
"Voer deze berekening uit.",
RingtoetsCommonFormsResources.CalculateIcon);
TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuValidateIndex,
"&Valideren",
"Valideer de invoer voor deze berekening.",
RingtoetsCommonFormsResources.ValidateIcon);
}
}
}
[Test]
public void ContextMenuStrip_CalculationWithoutForeshoreProfile_ContextMenuItemUpdateForeshoreProfileDisabledAndToolTipSet()
{
// Setup
var assessmentSection = mocks.Stub();
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation();
var nodeData = new StabilityPointStructuresCalculationContext(calculation,
parent,
failureMechanism,
assessmentSection);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
// Call
using (ContextMenuStrip menu = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// Assert
TestHelper.AssertContextMenuStripContainsItem(
menu,
contextMenuUpdateForeshoreProfileIndex,
"&Bijwerken voorlandprofiel...",
"Er moet een voorlandprofiel geselecteerd zijn.",
RingtoetsCommonFormsResources.UpdateItemIcon,
false);
}
}
}
[Test]
public void ContextMenuStrip_CalculationWithForeshoreProfileAndInputInSync_ContextMenuItemUpdateForeshoreProfileDisabledAndToolTipSet()
{
// Setup
var assessmentSection = mocks.Stub();
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation();
calculation.InputParameters.ForeshoreProfile = new TestForeshoreProfile();
var nodeData = new StabilityPointStructuresCalculationContext(calculation,
parent,
failureMechanism,
assessmentSection);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
// Call
using (ContextMenuStrip menu = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// Assert
TestHelper.AssertContextMenuStripContainsItem(
menu,
contextMenuUpdateForeshoreProfileIndex,
"&Bijwerken voorlandprofiel...",
"Er zijn geen wijzigingen om bij te werken.",
RingtoetsCommonFormsResources.UpdateItemIcon,
false);
}
}
}
[Test]
public void ContextMenuStrip_CalculationWithForeshoreProfileAndInputOutSync_ContextMenuItemUpdateForeshoreProfileEnabledAndToolTipSet()
{
// Setup
var assessmentSection = mocks.Stub();
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var foreshoreProfileInput = new TestForeshoreProfile();
var parent = new CalculationGroup();
var calculation = new StructuresCalculation();
calculation.InputParameters.ForeshoreProfile = foreshoreProfileInput;
TestForeshoreProfile.ChangeBreakWaterProperties(foreshoreProfileInput);
var nodeData = new StabilityPointStructuresCalculationContext(calculation,
parent,
failureMechanism,
assessmentSection);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
// Call
using (ContextMenuStrip menu = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// Assert
TestHelper.AssertContextMenuStripContainsItem(
menu,
contextMenuUpdateForeshoreProfileIndex,
"&Bijwerken voorlandprofiel...",
"Berekening bijwerken met het voorlandprofiel.",
RingtoetsCommonFormsResources.UpdateItemIcon);
}
}
}
[Test]
public void GivenCalculationWithoutOutputAndWithInputOutOfSync_WhenUpdateForeshoreProfileClicked_ThenNoInquiryAndCalculationUpdatedAndInputObserverNotified()
{
// Given
var calculationObserver = mocks.StrictMock();
var calculationInputObserver = mocks.StrictMock();
calculationInputObserver.Expect(o => o.UpdateObserver());
var assessmentSection = mocks.Stub();
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var foreshoreProfileInput = new TestForeshoreProfile(true);
var parent = new CalculationGroup();
var calculation = new StructuresCalculation
{
InputParameters =
{
ForeshoreProfile = foreshoreProfileInput
}
};
var nodeData = new StabilityPointStructuresCalculationContext(calculation,
parent,
failureMechanism,
assessmentSection);
calculation.Attach(calculationObserver);
calculation.InputParameters.Attach(calculationInputObserver);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(g => g.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
TestForeshoreProfile.ChangeBreakWaterProperties(foreshoreProfileInput);
// Precondition
Assert.IsFalse(calculation.InputParameters.IsForeshoreProfileInputSynchronized);
using (ContextMenuStrip contextMenuStrip = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// When
contextMenuStrip.Items[contextMenuUpdateForeshoreProfileIndex].PerformClick();
// Then
Assert.IsTrue(calculation.InputParameters.IsForeshoreProfileInputSynchronized);
}
}
}
[Test]
[TestCase(true)]
[TestCase(false)]
public void GivenCalculationWithOutputAndWithInputOutOfSync_WhenUpdateForeshoreProfileClicked_ThenInquiryAndExpectedOutputAndNotifications(bool continuation)
{
// Given
var calculationObserver = mocks.StrictMock();
var calculationInputObserver = mocks.StrictMock();
var assessmentSection = mocks.Stub();
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var foreshoreProfileInput = new TestForeshoreProfile(true);
var parent = new CalculationGroup();
var calculation = new StructuresCalculation
{
InputParameters =
{
ForeshoreProfile = foreshoreProfileInput
},
Output = new TestStructuresOutput()
};
var nodeData = new StabilityPointStructuresCalculationContext(calculation,
parent,
failureMechanism,
assessmentSection);
calculation.Attach(calculationObserver);
calculation.InputParameters.Attach(calculationInputObserver);
if (continuation)
{
calculationObserver.Expect(o => o.UpdateObserver());
calculationInputObserver.Expect(o => o.UpdateObserver());
}
var messageBoxText = "";
DialogBoxHandler = (name, wnd) =>
{
var helper = new MessageBoxTester(wnd);
messageBoxText = helper.Text;
if (continuation)
{
helper.ClickOk();
}
else
{
helper.ClickCancel();
}
};
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(g => g.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
TestForeshoreProfile.ChangeBreakWaterProperties(foreshoreProfileInput);
// Precondition
Assert.IsFalse(calculation.InputParameters.IsForeshoreProfileInputSynchronized);
using (ContextMenuStrip contextMenuStrip = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// When
contextMenuStrip.Items[contextMenuUpdateForeshoreProfileIndex].PerformClick();
// Then
Assert.AreEqual(continuation, calculation.InputParameters.IsForeshoreProfileInputSynchronized);
Assert.AreEqual(!continuation, calculation.HasOutput);
}
}
string expectedMessageBoxText = "Als u kiest voor bijwerken, dan wordt het resultaat van deze berekening " +
$"verwijderd.{Environment.NewLine}{Environment.NewLine}Weet u zeker dat u wilt doorgaan?";
Assert.AreEqual(expectedMessageBoxText, messageBoxText);
}
[Test]
public void GivenCalculationThatSucceeds_WhenCalculatingFromContextMenu_ThenOutputSetLogMessagesAddedAndUpdateObserver()
{
// Given
var mainWindow = mocks.Stub();
var observer = mocks.StrictMock();
observer.Expect(o => o.UpdateObserver());
string validFilePath = Path.Combine(testDataPath, "complete.sqlite");
var hydraulicBoundaryLocation = new TestHydraulicBoundaryLocation();
var assessmentSection = mocks.Stub();
assessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase
{
FilePath = validFilePath,
Version = "random",
Locations =
{
hydraulicBoundaryLocation
}
};
assessmentSection.Stub(a => a.Id).Return(string.Empty);
assessmentSection.Stub(a => a.FailureMechanismContribution).Return(new FailureMechanismContribution(Enumerable.Empty(), 1));
var initialStructuresOutput = new TestStructuresOutput();
var parent = new CalculationGroup();
var calculation = new TestStabilityPointStructuresCalculation
{
Output = initialStructuresOutput,
InputParameters =
{
HydraulicBoundaryLocation = hydraulicBoundaryLocation,
InflowModelType = StabilityPointStructureInflowModelType.LowSill,
LoadSchematizationType = LoadSchematizationType.Linear
}
};
calculation.Attach(observer);
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var calculationContext = new StabilityPointStructuresCalculationContext(calculation, parent, failureMechanism, assessmentSection);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(g => g.Get(calculationContext, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(g => g.MainWindow).Return(mainWindow);
var calculatorFactory = mocks.Stub();
calculatorFactory.Expect(cf => cf.CreateStructuresCalculator(testDataPath))
.Return(new TestStructuresCalculator());
mocks.ReplayAll();
plugin.Gui = gui;
DialogBoxHandler = (name, wnd) =>
{
// Expect an activity dialog which is automatically closed
};
using (ContextMenuStrip contextMenuStrip = info.ContextMenuStrip(calculationContext, null, treeViewControl))
using (new HydraRingCalculatorFactoryConfig(calculatorFactory))
{
// When
Action action = () => contextMenuStrip.Items[contextMenuCalculateIndex].PerformClick();
// Then
TestHelper.AssertLogMessages(action, messages =>
{
string[] msgs = messages.ToArray();
Assert.AreEqual(7, msgs.Length);
Assert.AreEqual($"Uitvoeren van berekening '{calculation.Name}' is gestart.", msgs[0]);
CalculationServiceTestHelper.AssertValidationStartMessage(msgs[1]);
CalculationServiceTestHelper.AssertValidationEndMessage(msgs[2]);
CalculationServiceTestHelper.AssertCalculationStartMessage(msgs[3]);
StringAssert.StartsWith("Puntconstructies berekening is uitgevoerd op de tijdelijke locatie", msgs[4]);
CalculationServiceTestHelper.AssertCalculationEndMessage(msgs[5]);
Assert.AreEqual($"Uitvoeren van berekening '{calculation.Name}' is gelukt.", msgs[6]);
});
Assert.AreNotSame(initialStructuresOutput, calculation.Output);
}
}
}
[Test]
public void GivenCalculationWithNonExistingFilePath_WhenValidatingFromContextMenu_ThenLogMessagesAdded()
{
// Given
var observer = mocks.StrictMock();
string validFilePath = Path.Combine(testDataPath, "complete.sqlite");
var hydraulicBoundaryLocation = new TestHydraulicBoundaryLocation();
var assessmentSection = mocks.Stub();
assessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase
{
FilePath = validFilePath,
Version = "random",
Locations =
{
hydraulicBoundaryLocation
}
};
var parent = new CalculationGroup();
var calculation = new TestStabilityPointStructuresCalculation
{
InputParameters =
{
HydraulicBoundaryLocation = hydraulicBoundaryLocation,
InflowModelType = StabilityPointStructureInflowModelType.LowSill,
LoadSchematizationType = LoadSchematizationType.Linear
}
};
calculation.Attach(observer);
var failureMechanism = new TestStabilityPointStructuresFailureMechanism();
var calculationContext = new StabilityPointStructuresCalculationContext(calculation, parent, failureMechanism, assessmentSection);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(g => g.Get(calculationContext, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
plugin.Gui = gui;
using (ContextMenuStrip contextMenuStrip = info.ContextMenuStrip(calculationContext, null, treeViewControl))
{
// When
Action action = () => contextMenuStrip.Items[contextMenuValidateIndex].PerformClick();
// Then
TestHelper.AssertLogMessages(action, messages =>
{
string[] msgs = messages.ToArray();
Assert.AreEqual(2, msgs.Length);
CalculationServiceTestHelper.AssertValidationStartMessage(msgs[0]);
CalculationServiceTestHelper.AssertValidationEndMessage(msgs[1]);
});
}
}
}
[Test]
public void OnNodeRemoved_ParentIsCalculationGroupContext_RemoveCalculationFromGroup()
{
// Setup
var group = new CalculationGroup();
var elementToBeRemoved = new StructuresCalculation();
var failureMechanism = new StabilityPointStructuresFailureMechanism();
var observer = mocks.StrictMock();
var assessmentSection = mocks.Stub();
var calculationContext = new StabilityPointStructuresCalculationContext(elementToBeRemoved,
group,
failureMechanism,
assessmentSection);
var groupContext = new StabilityPointStructuresCalculationGroupContext(group,
null,
failureMechanism,
assessmentSection);
observer.Expect(o => o.UpdateObserver());
mocks.ReplayAll();
group.Children.Add(elementToBeRemoved);
group.Children.Add(new StructuresCalculation());
group.Attach(observer);
// Precondition
Assert.IsTrue(info.CanRemove(calculationContext, groupContext));
Assert.AreEqual(2, group.Children.Count);
// Call
info.OnNodeRemoved(calculationContext, groupContext);
// Assert
Assert.AreEqual(1, group.Children.Count);
CollectionAssert.DoesNotContain(group.Children, elementToBeRemoved);
}
[Test]
public void OnNodeRemoved_CalculationInGroupAssignedToSection_CalculationDetachedFromSection()
{
// Setup
var group = new CalculationGroup();
var failureMechanism = new StabilityPointStructuresFailureMechanism();
var elementToBeRemoved = new StructuresCalculation();
var assessmentSection = mocks.Stub();
var calculationContext = new StabilityPointStructuresCalculationContext(elementToBeRemoved,
group,
failureMechanism,
assessmentSection);
var groupContext = new StabilityPointStructuresCalculationGroupContext(group,
null,
failureMechanism,
assessmentSection);
mocks.ReplayAll();
group.Children.Add(elementToBeRemoved);
failureMechanism.AddSection(new FailureMechanismSection("section", new[]
{
new Point2D(0, 0)
}));
StabilityPointStructuresFailureMechanismSectionResult result = failureMechanism.SectionResults.First();
result.Calculation = elementToBeRemoved;
// Call
info.OnNodeRemoved(calculationContext, groupContext);
// Assert
Assert.IsNull(result.Calculation);
}
public override void Setup()
{
mocks = new MockRepository();
plugin = new StabilityPointStructuresPlugin();
info = plugin.GetTreeNodeInfos().First(tni => tni.TagType == typeof(StabilityPointStructuresCalculationContext));
}
public override void TearDown()
{
plugin.Dispose();
mocks.VerifyAll();
base.TearDown();
}
private static void ChangeStructure(StabilityPointStructure structure)
{
structure.CopyProperties(new StabilityPointStructure(
new StabilityPointStructure.ConstructionProperties
{
Id = structure.Id,
Name = structure.Name,
Location = structure.Location
}));
}
}
}