// 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.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.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.Hydraulics;
using Ringtoets.Common.Data.Probability;
using Ringtoets.Common.Data.Structures;
using Ringtoets.HydraRing.Calculation.TestUtil.Calculator;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
using RingtoetsCommonServicesResources = Ringtoets.Common.Service.Properties.Resources;
namespace Ringtoets.ClosingStructures.Forms.Test.TreeNodeInfos
{
[TestFixture]
public class ClosingStructuresCalculationContextTreeNodeInfoTest : NUnitFormTest
{
private const int contextMenuValidateIndex = 0;
private const int contextMenuCalculateIndex = 1;
private const int contextMenuClearIndex = 2;
private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "HydraulicBoundaryDatabaseImporter");
private IGui guiMock;
private TreeNodeInfo info;
private MockRepository mocks;
private ClosingStructuresPlugin plugin;
[SetUp]
public void SetUp()
{
mocks = new MockRepository();
guiMock = mocks.Stub();
plugin = new ClosingStructuresPlugin
{
Gui = guiMock
};
info = plugin.GetTreeNodeInfos().First(tni => tni.TagType == typeof(ClosingStructuresCalculationContext));
}
[TearDown]
public override void TearDown()
{
plugin.Dispose();
mocks.VerifyAll();
}
[Test]
public void Initialized_Always_ExpectedPropertiesSet()
{
// Setup
mocks.ReplayAll();
// Assert
Assert.AreEqual(typeof(ClosingStructuresCalculationContext), info.TagType);
Assert.IsNotNull(info.Text);
Assert.IsNotNull(info.Image);
Assert.IsNotNull(info.EnsureVisibleOnCreate);
Assert.IsNotNull(info.ChildNodeObjects);
Assert.IsNotNull(info.ContextMenuStrip);
Assert.IsNotNull(info.CanRename);
Assert.IsNotNull(info.OnNodeRenamed);
Assert.IsNotNull(info.CanRemove);
Assert.IsNotNull(info.OnNodeRemoved);
Assert.IsNotNull(info.CanDrag);
Assert.IsNull(info.ForeColor);
Assert.IsNull(info.CanCheck);
Assert.IsNull(info.IsChecked);
Assert.IsNull(info.OnNodeChecked);
Assert.IsNull(info.CanDrop);
Assert.IsNull(info.CanInsert);
Assert.IsNull(info.OnDrop);
}
[Test]
public void Image_Always_ReturnsCalculationIcon()
{
// Setup
mocks.ReplayAll();
// Call
var image = info.Image(null);
// Assert
TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.CalculationIcon, image);
}
[Test]
public void ChildNodeObjects_CalculationWithoutOutput_ReturnCollectionWithEmptyOutputObject()
{
var assessmentSectionStub = mocks.Stub();
mocks.ReplayAll();
var failureMechanism = new ClosingStructuresFailureMechanism();
var calculation = new StructuresCalculation();
var calculationContext = new ClosingStructuresCalculationContext(calculation, failureMechanism, assessmentSectionStub);
// Call
var children = info.ChildNodeObjects(calculationContext).ToArray();
// Assert
Assert.AreEqual(3, children.Length);
var comment = children[0] as Comment;
Assert.AreSame(calculationContext.WrappedData.Comments, comment);
var closingStructuresInputContext = children[1] as ClosingStructuresInputContext;
Assert.IsNotNull(closingStructuresInputContext);
Assert.AreSame(calculationContext.WrappedData.InputParameters, closingStructuresInputContext.WrappedData);
Assert.IsInstanceOf(children[2]);
}
[Test]
public void ChildNodeObjects_CalculationWithOutput_ReturnCollectionWithOutputObject()
{
var assessmentSectionStub = mocks.Stub();
mocks.ReplayAll();
var failureMechanism = new ClosingStructuresFailureMechanism();
var calculation = new StructuresCalculation
{
Output = new TestClosingStructuresOutput()
};
var calculationContext = new ClosingStructuresCalculationContext(calculation, failureMechanism, assessmentSectionStub);
// Call
var children = info.ChildNodeObjects(calculationContext).ToArray();
// Assert
Assert.AreEqual(3, children.Length);
var comment = children[0] as Comment;
Assert.AreSame(calculationContext.WrappedData.Comments, comment);
var closingStructuresInputContext = children[1] as ClosingStructuresInputContext;
Assert.IsNotNull(closingStructuresInputContext);
Assert.AreSame(calculationContext.WrappedData.InputParameters, closingStructuresInputContext.WrappedData);
Assert.IsInstanceOf(children[2]);
}
[Test]
public void ContextMenuStrip_Always_CallsContextMenuBuilderMethods()
{
// Setup
var failureMechanism = new ClosingStructuresFailureMechanism();
var assessmentSectionStub = mocks.Stub();
var calculation = new StructuresCalculation();
var nodeData = new ClosingStructuresCalculationContext(calculation, failureMechanism, assessmentSectionStub);
var menuBuilderMock = mocks.StrictMock();
menuBuilderMock.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().Return(menuBuilderMock);
menuBuilderMock.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().Return(menuBuilderMock);
menuBuilderMock.Expect(mb => mb.AddCustomItem(null)).IgnoreArguments().Return(menuBuilderMock);
menuBuilderMock.Expect(mb => mb.AddSeparator()).Return(menuBuilderMock);
menuBuilderMock.Expect(mb => mb.AddRenameItem()).Return(menuBuilderMock);
menuBuilderMock.Expect(mb => mb.AddDeleteItem()).Return(menuBuilderMock);
menuBuilderMock.Expect(mb => mb.AddSeparator()).Return(menuBuilderMock);
menuBuilderMock.Expect(mb => mb.AddExpandAllItem()).Return(menuBuilderMock);
menuBuilderMock.Expect(mb => mb.AddCollapseAllItem()).Return(menuBuilderMock);
menuBuilderMock.Expect(mb => mb.AddSeparator()).Return(menuBuilderMock);
menuBuilderMock.Expect(mb => mb.AddPropertiesItem()).Return(menuBuilderMock);
menuBuilderMock.Expect(mb => mb.Build()).Return(null);
using (var treeViewControl = new TreeViewControl())
{
guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilderMock);
mocks.ReplayAll();
plugin.Gui = guiMock;
// Call
info.ContextMenuStrip(nodeData, null, treeViewControl);
}
// Assert
// Assert expectancies are called in TearDown()
}
[Test]
public void ContextMenuStrip_Always_AddCustomItems()
{
// Setup
var failureMechanism = new ClosingStructuresFailureMechanism();
var assessmentSectionStub = mocks.Stub();
var calculation = new StructuresCalculation();
var nodeData = new ClosingStructuresCalculationContext(calculation, failureMechanism, assessmentSectionStub);
var menuBuilder = new CustomItemsOnlyContextMenuBuilder();
using (var treeViewControl = new TreeViewControl())
{
guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControl)).Return(menuBuilder);
mocks.ReplayAll();
plugin.Gui = guiMock;
// Call
using (ContextMenuStrip menu = info.ContextMenuStrip(nodeData, assessmentSectionStub, treeViewControl))
{
// Assert
Assert.AreEqual(11, menu.Items.Count);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuValidateIndex,
"&Valideren",
"Er is geen vakindeling geïmporteerd.",
RingtoetsCommonFormsResources.ValidateIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuCalculateIndex,
"Be&rekenen",
"Er is geen vakindeling geïmporteerd.",
RingtoetsCommonFormsResources.CalculateIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuClearIndex,
"&Wis uitvoer...",
"Deze berekening heeft geen uitvoer om te wissen.",
RingtoetsCommonFormsResources.ClearIcon,
false);
}
}
}
[Test]
public void ContextMenuStrip_NoFailureMechanismSections_ContextMenuItemPerformCalculationAndValidationDisabledAndTooltipSet()
{
// Setup
var failureMechanism = new ClosingStructuresFailureMechanism();
var assessmentSectionStub = mocks.Stub();
var calculation = new StructuresCalculation();
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, contextMenuCalculateIndex,
"Be&rekenen",
"Er is geen vakindeling geïmporteerd.",
RingtoetsCommonFormsResources.CalculateIcon,
false);
TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuValidateIndex,
"&Valideren",
"Er is geen vakindeling geïmporteerd.",
RingtoetsCommonFormsResources.ValidateIcon,
false);
}
}
}
[Test]
public void ContextMenuStrip_FailureMechanismSectionsSetNoHydraulicBoundaryDatabase_ContextMenuItemPerformCalculationAndValidationDisabledAndTooltipSet()
{
// Setup
var failureMechanism = new ClosingStructuresFailureMechanism();
failureMechanism.AddSection(new FailureMechanismSection("test", new[]
{
new Point2D(0, 0)
}));
var assessmentSectionStub = mocks.Stub();
var calculation = new StructuresCalculation();
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, 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_FailureMechanismSectionsSetHydraulicBoundaryDatabaseNotValid_ContextMenuItemPerformCalculationAndValidationDisabledAndTooltipSet()
{
// Setup
var failureMechanism = new ClosingStructuresFailureMechanism();
failureMechanism.AddSection(new FailureMechanismSection("test", new[]
{
new Point2D(0, 0)
}));
var assessmentSectionStub = mocks.Stub();
assessmentSectionStub.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase();
var calculation = new StructuresCalculation();
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
ToolStripItem calculateContextMenuItem = contextMenu.Items[contextMenuCalculateIndex];
Assert.AreEqual("Be&rekenen", calculateContextMenuItem.Text);
Assert.AreEqual("Er moet een koppeling bestaan met een hydraulische randvoorwaardendatabase.", calculateContextMenuItem.ToolTipText);
TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.CalculateIcon, calculateContextMenuItem.Image);
Assert.IsFalse(calculateContextMenuItem.Enabled);
ToolStripItem validateContextMenuItem = contextMenu.Items[contextMenuValidateIndex];
Assert.AreEqual("&Valideren", validateContextMenuItem.Text);
Assert.AreEqual("Er moet een koppeling bestaan met een hydraulische randvoorwaardendatabase.", validateContextMenuItem.ToolTipText);
TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.ValidateIcon, validateContextMenuItem.Image);
Assert.IsFalse(validateContextMenuItem.Enabled);
}
}
}
[Test]
public void ContextMenuStrip_FailureMechanismSectionsAndHydraulicBoundaryDatabaseSet_ContextMenuItemPerformCalculationAndValidationEnabled()
{
// 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();
assessmentSectionStub.HydraulicBoundaryDatabase = hydraulicBoundaryDatabase;
var calculation = new StructuresCalculation();
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, 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 GivenSuccessfulCalculation_WhenCalculatingFromContextMenu_ThenOutputSetLogMessagesAddedAndUpdateObserver()
{
// Given
var mainWindow = mocks.Stub();
var observerMock = mocks.StrictMock();
observerMock.Expect(o => o.UpdateObserver());
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();
assessmentSectionStub.HydraulicBoundaryDatabase = hydraulicBoundaryDatabase;
assessmentSectionStub.Stub(a => a.Id).Return(string.Empty);
assessmentSectionStub.Stub(a => a.FailureMechanismContribution).Return(new FailureMechanismContribution(Enumerable.Empty(), 1, 1));
var initialOutput = new ProbabilityAssessmentOutput(double.NaN, double.NaN, double.NaN, double.NaN, double.NaN);
var calculation = new TestClosingStructuresCalculation
{
Output = initialOutput,
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());
guiMock.Expect(g => g.MainWindow).Return(mainWindow);
mocks.ReplayAll();
plugin.Gui = guiMock;
calculation.Attach(observerMock);
DialogBoxHandler = (name, wnd) =>
{
// Expect an activity dialog which is automatically closed
};
using (ContextMenuStrip contextMenuStrip = info.ContextMenuStrip(calculationContext, null, treeViewControl))
using (new HydraRingCalculatorFactoryConfig())
{
// When
Action action = () => contextMenuStrip.Items[contextMenuCalculateIndex].PerformClick();
// Then
TestHelper.AssertLogMessages(action, messages =>
{
var msgs = messages.ToArray();
Assert.AreEqual(6, 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]);
StringAssert.StartsWith(string.Format("Berekening van '{0}' gestart om: ", calculation.Name), msgs[2]);
StringAssert.StartsWith("Betrouwbaarheid sluiting kunstwerk berekening is uitgevoerd op de tijdelijke locatie", msgs[3]);
StringAssert.StartsWith(string.Format("Berekening van '{0}' beëindigd om: ", calculation.Name), msgs[4]);
StringAssert.StartsWith(string.Format("Uitvoeren van '{0}' is gelukt.", calculation.Name), msgs[5]);
});
Assert.AreNotSame(initialOutput, calculation.Output);
}
}
}
[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
var group = new CalculationGroup();
var failureMechanism = new ClosingStructuresFailureMechanism();
var elementToBeRemoved = new StructuresCalculation();
var observerMock = mocks.StrictMock();
var assessmentSectionStub = mocks.Stub();
var calculationContext = new ClosingStructuresCalculationContext(elementToBeRemoved,
failureMechanism,
assessmentSectionStub);
var groupContext = new ClosingStructuresCalculationGroupContext(group,
failureMechanism,
assessmentSectionStub);
observerMock.Expect(o => o.UpdateObserver());
mocks.ReplayAll();
group.Children.Add(elementToBeRemoved);
group.Children.Add(new StructuresCalculation());
group.Attach(observerMock);
// 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 ClosingStructuresFailureMechanism();
var elementToBeRemoved = new StructuresCalculation();
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)
}));
ClosingStructuresFailureMechanismSectionResult result = failureMechanism.SectionResults.First();
result.Calculation = elementToBeRemoved;
// Call
info.OnNodeRemoved(calculationContext, groupContext);
// Assert
Assert.IsNull(result.Calculation);
}
private class TestClosingStructuresOutput : ProbabilityAssessmentOutput
{
public TestClosingStructuresOutput() : base(0, 0, 0, 0, 0) {}
}
}
}