// 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.Common.Data;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Data.Probability;
using Ringtoets.Common.Forms.PresentationObjects;
using Ringtoets.GrassCoverErosionInwards.Data;
using Ringtoets.GrassCoverErosionInwards.Forms.PresentationObjects;
using Ringtoets.GrassCoverErosionInwards.Plugin;
using Ringtoets.GrassCoverErosionInwards.Plugin.Properties;
using Ringtoets.HydraRing.Data;
using Ringtoets.Integration.Data;
using GrassCoverErosionInwardsFormsResources = Ringtoets.GrassCoverErosionInwards.Forms.Properties.Resources;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
namespace Ringtoets.GrassCoverErosionInwards.Forms.Test.TreeNodeInfos
{
[TestFixture]
public class GrassCoverErosionInwardsCalculationContextTreeNodeInfoTest : NUnitFormTest
{
private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.HydraRing.IO, "HydraulicBoundaryLocationReader");
private MockRepository mocks;
private GrassCoverErosionInwardsGuiPlugin plugin;
private TreeNodeInfo info;
[SetUp]
public void SetUp()
{
mocks = new MockRepository();
plugin = new GrassCoverErosionInwardsGuiPlugin();
info = plugin.GetTreeNodeInfos().First(tni => tni.TagType == typeof(GrassCoverErosionInwardsCalculationContext));
}
[Test]
public void Initialized_Always_ExpectedPropertiesSet()
{
// Assert
Assert.AreEqual(typeof(GrassCoverErosionInwardsCalculationContext), 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()
{
// Call
var image = info.Image(null);
// Assert
TestHelper.AssertImagesAreEqual(GrassCoverErosionInwardsFormsResources.CalculationIcon, image);
}
[Test]
public void ChildNodeObjects_CalculationWithoutOutput_ReturnCollectionWithEmptyOutputObject()
{
var assessmentSectionMock = mocks.StrictMock();
mocks.ReplayAll();
var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput(), new NormProbabilityInput());
var failureMechanism = new GrassCoverErosionInwardsFailureMechanism();
var calculationContext = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock);
// Call
var children = info.ChildNodeObjects(calculationContext).ToArray();
// Assert
Assert.AreEqual(3, children.Length);
var commentContext = children[0] as CommentContext;
Assert.IsNotNull(commentContext);
Assert.AreSame(calculationContext.WrappedData, commentContext.CommentContainer);
var grassCoverErosionInwardsInputContext = children[1] as GrassCoverErosionInwardsInputContext;
Assert.IsNotNull(grassCoverErosionInwardsInputContext);
Assert.AreSame(calculationContext.WrappedData.InputParameters, grassCoverErosionInwardsInputContext.WrappedData);
var emptyOutput = children[2] as EmptyProbabilisticOutput;
Assert.IsNotNull(emptyOutput);
mocks.VerifyAll();
}
[Test]
public void ChildNodeObjects_CalculationWithOutput_ReturnCollectionWithOutputObject()
{
var assessmentSectionMock = mocks.StrictMock();
mocks.ReplayAll();
var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput(), new NormProbabilityInput())
{
Output = new ProbabilisticOutput(0, 0, 0, 0, 0)
};
var failureMechanism = new GrassCoverErosionInwardsFailureMechanism();
var calculationContext = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock);
// Call
var children = info.ChildNodeObjects(calculationContext).ToArray();
// Assert
Assert.AreEqual(3, children.Length);
var commentContext = children[0] as CommentContext;
Assert.IsNotNull(commentContext);
Assert.AreSame(calculationContext.WrappedData, commentContext.CommentContainer);
var grassCoverErosionInwardsInputContext = children[1] as GrassCoverErosionInwardsInputContext;
Assert.IsNotNull(grassCoverErosionInwardsInputContext);
Assert.AreSame(calculationContext.WrappedData.InputParameters, grassCoverErosionInwardsInputContext.WrappedData);
var output = children[2] as ProbabilisticOutput;
Assert.IsNotNull(output);
mocks.VerifyAll();
}
[Test]
public void ContextMenuStrip_Always_CallsContextMenuBuilderMethods()
{
// Setup
var guiMock = mocks.StrictMock();
var treeViewControlMock = mocks.StrictMock();
var failureMechanism = new GrassCoverErosionInwardsFailureMechanism();
var assessmentSectionMock = mocks.StrictMock();
var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput(),
new NormProbabilityInput());
var nodeData = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock);
var menuBuilderMock = mocks.Stub();
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.AddImportItem()).Return(menuBuilderMock);
menuBuilderMock.Expect(mb => mb.AddExportItem()).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);
guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControlMock)).Return(menuBuilderMock);
mocks.ReplayAll();
plugin.Gui = guiMock;
// Call
info.ContextMenuStrip(nodeData, null, treeViewControlMock);
// Assert
mocks.VerifyAll(); // Expect no calls on arguments
}
[Test]
public void ContextMenuStrip_Always_AddCustomItems()
{
// Setup
var guiMock = mocks.StrictMock();
var treeViewControlMock = mocks.StrictMock();
var failureMechanism = new GrassCoverErosionInwardsFailureMechanism();
var assessmentSectionMock = mocks.StrictMock();
var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput(),
new NormProbabilityInput());
var nodeData = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock);
var menuBuilder = new CustomItemsOnlyContextMenuBuilder();
string validFilePath = Path.Combine(testDataPath, "complete.sqlite");
guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControlMock)).Return(menuBuilder);
assessmentSectionMock.Stub(asm => asm.HydraulicBoundaryDatabase).Return(new HydraulicBoundaryDatabase
{
FilePath = validFilePath,
Version = "random"
});
mocks.ReplayAll();
plugin.Gui = guiMock;
failureMechanism.AddSection(new FailureMechanismSection("test", new[]
{
new Point2D(0, 0)
}));
// Call
var menu = info.ContextMenuStrip(nodeData, assessmentSectionMock, treeViewControlMock);
// Assert
Assert.AreEqual(6, menu.Items.Count);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuCalculateIndex,
RingtoetsCommonFormsResources.Calculate,
RingtoetsCommonFormsResources.Calculate_ToolTip,
RingtoetsCommonFormsResources.CalculateIcon);
TestHelper.AssertContextMenuStripContainsItem(menu, contextMenuClearIndex,
RingtoetsCommonFormsResources.Clear_output,
RingtoetsCommonFormsResources.ClearOutput_No_output_to_clear,
RingtoetsCommonFormsResources.ClearIcon,
false);
mocks.VerifyAll();
}
[Test]
public void ContextMenuStrip_NoFailureMechanismSections_ContextMenuItemPerformCalculationDisabledAndTooltipSet()
{
// Setup
var treeViewControlMock = mocks.StrictMock();
var failureMechanism = new GrassCoverErosionInwardsFailureMechanism();
var assessmentSectionMock = mocks.StrictMock();
var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput(),
new NormProbabilityInput());
var nodeData = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock);
var guiMock = mocks.StrictMock();
guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControlMock)).Return(new CustomItemsOnlyContextMenuBuilder());
mocks.ReplayAll();
plugin.Gui = guiMock;
// Call
var contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControlMock);
// Assert
mocks.VerifyAll(); // Expect no calls on arguments
TestHelper.AssertContextMenuStripContainsItem(contextMenu, 0,
RingtoetsCommonFormsResources.Calculate,
Resources.GrassCoverErosionInwardsGuiPlugin_AllDataAvailable_No_failure_mechanism_sections_imported,
RingtoetsCommonFormsResources.CalculateIcon,
false);
}
[Test]
public void ContextMenuStrip_FailureMechanismSectionsSetNoHydraulicBoundaryDatabase_ContextMenuItemPerformCalculationDisabledAndTooltipSet()
{
// Setup
var guiMock = mocks.StrictMock();
var treeViewControlMock = mocks.StrictMock();
var failureMechanism = new GrassCoverErosionInwardsFailureMechanism();
failureMechanism.AddSection(new FailureMechanismSection("test", new[]
{
new Point2D(0, 0)
}));
var assessmentSectionMock = mocks.StrictMock();
assessmentSectionMock.Expect(asm => asm.HydraulicBoundaryDatabase).Return(null);
var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput(),
new NormProbabilityInput());
var nodeData = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock);
guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControlMock)).Return(new CustomItemsOnlyContextMenuBuilder());
mocks.ReplayAll();
plugin.Gui = guiMock;
// Call
var contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControlMock);
// Assert
mocks.VerifyAll(); // Expect no calls on arguments
TestHelper.AssertContextMenuStripContainsItem(contextMenu, 0,
RingtoetsCommonFormsResources.Calculate,
Resources.GrassCoverErosionInwardsGuiPlugin_AllDataAvailable_No_hydraulic_boundary_database_imported,
RingtoetsCommonFormsResources.CalculateIcon,
false);
}
[Test]
public void ContextMenuStrip_FailureMechanismSetHydraulicBoundaryDatabaseNotValid_ContextMenuItemPerformCalculationDisabledAndTooltipSet()
{
// Setup
var guiMock = mocks.StrictMock();
var treeViewControlMock = mocks.StrictMock();
var failureMechanism = new GrassCoverErosionInwardsFailureMechanism();
failureMechanism.AddSection(new FailureMechanismSection("test", new[]
{
new Point2D(0, 0)
}));
var assessmentSectionMock = mocks.StrictMock();
assessmentSectionMock.Stub(asm => asm.HydraulicBoundaryDatabase).Return(new HydraulicBoundaryDatabase());
var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput(),
new NormProbabilityInput());
var nodeData = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock);
guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControlMock)).Return(new CustomItemsOnlyContextMenuBuilder());
mocks.ReplayAll();
plugin.Gui = guiMock;
// Call
ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControlMock);
// Assert
mocks.VerifyAll(); // Expect no calls on arguments
ToolStripItem contextMenuItem = contextMenu.Items[0];
Assert.AreEqual(RingtoetsCommonFormsResources.Calculate, contextMenuItem.Text);
StringAssert.Contains(String.Format(RingtoetsCommonFormsResources.GuiPlugin_VerifyHydraulicBoundaryDatabasePath_Hydraulic_boundary_database_connection_failed_0_, ""), contextMenuItem.ToolTipText);
TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.CalculateIcon, contextMenuItem.Image);
Assert.IsFalse(contextMenuItem.Enabled);
}
[Test]
public void ContextMenuStrip_FailureMechanismSectionsAndHydraulicBoundaryDatabaseSet_ContextMenuItemPerformCalculationEnabled()
{
// Setup
var guiMock = mocks.StrictMock();
var treeViewControlMock = mocks.StrictMock();
string validFilePath = Path.Combine(testDataPath, "complete.sqlite");
var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase
{
FilePath = validFilePath,
Version = "1.0"
};
var failureMechanism = new GrassCoverErosionInwardsFailureMechanism();
failureMechanism.AddSection(new FailureMechanismSection("test", new[]
{
new Point2D(0, 0)
}));
var assessmentSectionMock = mocks.StrictMock();
assessmentSectionMock.Stub(asm => asm.HydraulicBoundaryDatabase).Return(hydraulicBoundaryDatabase);
var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput(),
new NormProbabilityInput());
var nodeData = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSectionMock);
guiMock.Expect(cmp => cmp.Get(nodeData, treeViewControlMock)).Return(new CustomItemsOnlyContextMenuBuilder());
mocks.ReplayAll();
plugin.Gui = guiMock;
// Call
var contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControlMock);
// Assert
mocks.VerifyAll(); // Expect no calls on arguments
TestHelper.AssertContextMenuStripContainsItem(contextMenu, 0,
RingtoetsCommonFormsResources.Calculate,
RingtoetsCommonFormsResources.Calculate_ToolTip,
RingtoetsCommonFormsResources.CalculateIcon);
}
[Test]
public void GivenCalculationWithNonExistingFilePath_WhenCalculatingFromContextMenu_ThenOutputClearedLogMessagesAddedObserversNotNotified()
{
// Given
var gui = mocks.DynamicMock();
var mainWindow = mocks.DynamicMock();
var observerMock = mocks.StrictMock();
var treeViewControlMock = mocks.StrictMock();
var calculateContextMenuItemIndex = 0;
var section = new FailureMechanismSection("A", new[]
{
new Point2D(1, 2),
new Point2D(3, 4)
});
var failureMechanism = new GrassCoverErosionInwardsFailureMechanism();
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 assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike)
{
HydraulicBoundaryDatabase = hydraulicBoundaryDatabase
};
var calculation = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput(),
new NormProbabilityInput())
{
Output = new ProbabilisticOutput(double.NaN, double.NaN, double.NaN, double.NaN, double.NaN),
InputParameters =
{
HydraulicBoundaryLocation = hydraulicBoundaryLocation
}
};
var calculationContext = new GrassCoverErosionInwardsCalculationContext(calculation, failureMechanism, assessmentSection);
gui.Expect(g => g.Get(calculationContext, treeViewControlMock)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Expect(g => g.MainWindow).Return(mainWindow);
mocks.ReplayAll();
plugin.Gui = gui;
calculation.Attach(observerMock);
var contextMenuAdapter = info.ContextMenuStrip(calculationContext, null, treeViewControlMock);
DialogBoxHandler = (name, wnd) =>
{
// Expect an activity dialog which is automatically closed
};
// When
Action action = () => { contextMenuAdapter.Items[calculateContextMenuItemIndex].PerformClick(); };
// Then
TestHelper.AssertLogMessages(action, messages =>
{
var msgs = messages.GetEnumerator();
Assert.IsTrue(msgs.MoveNext());
StringAssert.StartsWith("Er is een fout opgetreden tijdens de berekening.", msgs.Current);
});
Assert.IsNull(calculation.Output);
mocks.VerifyAll();
}
[Test]
public void OnNodeRemoved_ParentIsCalculationGroupContext_RemoveCalculationFromGroup()
{
// Setup
var group = new CalculationGroup();
var elementToBeRemoved = new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput(),
new NormProbabilityInput());
var failureMechanism = new GrassCoverErosionInwardsFailureMechanism();
var observerMock = mocks.StrictMock();
var assessmentSectionMock = mocks.StrictMock();
var calculationContext = new GrassCoverErosionInwardsCalculationContext(elementToBeRemoved,
failureMechanism,
assessmentSectionMock);
var groupContext = new GrassCoverErosionInwardsCalculationGroupContext(group,
failureMechanism,
assessmentSectionMock);
observerMock.Expect(o => o.UpdateObserver());
mocks.ReplayAll();
group.Children.Add(elementToBeRemoved);
group.Children.Add(new GrassCoverErosionInwardsCalculation(new GeneralGrassCoverErosionInwardsInput(),
new NormProbabilityInput()));
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);
mocks.VerifyAll();
}
private const int contextMenuCalculateIndex = 0;
private const int contextMenuClearIndex = 1;
}
}