// Copyright (C) Stichting Deltares 2021. All rights reserved.
//
// This file is part of Riskeer.
//
// Riskeer 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.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Core.Common.Base;
using Core.Common.Base.Storage;
using Core.Common.Controls.TreeView;
using Core.Common.TestUtil;
using Core.Gui;
using Core.Gui.Commands;
using Core.Gui.ContextMenu;
using Core.Gui.Forms.Main;
using Core.Gui.Forms.ViewHost;
using Core.Gui.Plugin;
using Core.Gui.TestUtil;
using Core.Gui.TestUtil.ContextMenu;
using NUnit.Extensions.Forms;
using NUnit.Framework;
using Rhino.Mocks;
using Riskeer.Common.Data.AssessmentSection;
using Riskeer.Common.Data.Hydraulics;
using Riskeer.Common.Data.TestUtil;
using Riskeer.Common.Data.TestUtil.IllustrationPoints;
using Riskeer.Common.Forms.PresentationObjects;
using Riskeer.Common.Forms.TypeConverters;
using Riskeer.Common.Plugin.TestUtil;
using Riskeer.Common.Service.TestUtil;
using Riskeer.HydraRing.Calculation.Calculator.Factory;
using Riskeer.HydraRing.Calculation.Data.Input;
using Riskeer.HydraRing.Calculation.TestUtil.Calculator;
using Riskeer.Integration.Data;
using Riskeer.Integration.Forms.PresentationObjects;
using RiskeerCommonFormsResources = Riskeer.Common.Forms.Properties.Resources;
namespace Riskeer.Integration.Plugin.Test.TreeNodeInfos
{
[TestFixture]
public class HydraulicBoundaryDatabaseContextTreeNodeInfoTest : NUnitFormTest
{
private const int contextMenuImportHydraulicBoundaryDatabaseIndex = 0;
private const int contextMenuCalculateAllIndex = 3;
private const int contextMenuClearIllustrationPointsIndex = 5;
private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Riskeer.Integration.Forms, "HydraulicBoundaryDatabase");
private MockRepository mocks;
[Test]
public void Initialized_Always_ExpectedPropertiesSet()
{
// Setup
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Assert
Assert.IsNotNull(info.Text);
Assert.IsNotNull(info.ForeColor);
Assert.IsNotNull(info.Image);
Assert.IsNotNull(info.ContextMenuStrip);
Assert.IsNull(info.EnsureVisibleOnCreate);
Assert.IsNull(info.ExpandOnCreate);
Assert.IsNotNull(info.ChildNodeObjects);
Assert.IsNull(info.CanRename);
Assert.IsNull(info.OnNodeRenamed);
Assert.IsNull(info.CanRemove);
Assert.IsNull(info.OnNodeRemoved);
Assert.IsNull(info.CanCheck);
Assert.IsNull(info.CheckedState);
Assert.IsNull(info.OnNodeChecked);
Assert.IsNull(info.CanDrag);
Assert.IsNull(info.CanDrop);
Assert.IsNull(info.CanInsert);
Assert.IsNull(info.OnDrop);
}
}
[Test]
public void Text_Always_ReturnsSetName()
{
// Setup
const string name = "Hydraulische belastingen";
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
var context = new HydraulicBoundaryDatabaseContext(assessmentSection.HydraulicBoundaryDatabase,
assessmentSection);
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
string text = info.Text(context);
// Assert
Assert.AreEqual(name, text);
}
}
[Test]
public void Image_Always_ReturnsGeneralFolderIcon()
{
// Setup
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
Image image = info.Image(null);
// Assert
TestHelper.AssertImagesAreEqual(RiskeerCommonFormsResources.GeneralFolderIcon, image);
}
}
[Test]
public void ContextMenuStrip_Always_CallsContextMenuBuilderMethods()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
var context = new HydraulicBoundaryDatabaseContext(assessmentSection.HydraulicBoundaryDatabase,
assessmentSection);
var menuBuilder = mocks.StrictMock();
using (mocks.Ordered())
{
menuBuilder.Expect(mb => mb.AddImportItem(null, null, null)).IgnoreArguments().Return(menuBuilder);
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.AddCustomItem(null)).IgnoreArguments().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(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
gui.Stub(cmp => cmp.Get(context, treeViewControl)).Return(menuBuilder);
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
plugin.Gui = gui;
// Call
info.ContextMenuStrip(context, null, treeViewControl);
}
}
// Assert
mocks.VerifyAll();
}
[Test]
public void ContextMenuStrip_Always_AddImportItem()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
var context = new HydraulicBoundaryDatabaseContext(assessmentSection.HydraulicBoundaryDatabase,
assessmentSection);
var applicationFeatureCommands = mocks.Stub();
var importCommandHandler = mocks.Stub();
importCommandHandler.Stub(ich => ich.GetSupportedImportInfos(null)).IgnoreArguments().Return(new[]
{
new ImportInfo()
});
var exportCommandHandler = mocks.Stub();
var updateCommandHandler = mocks.Stub();
var viewCommands = mocks.Stub();
using (var treeViewControl = new TreeViewControl())
{
var builder = new ContextMenuBuilder(applicationFeatureCommands,
importCommandHandler,
exportCommandHandler,
updateCommandHandler,
viewCommands,
context,
treeViewControl);
var gui = mocks.Stub();
gui.Stub(g => g.Get(context, treeViewControl)).Return(builder);
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
mocks.ReplayAll();
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
plugin.Gui = gui;
// Call
using (ContextMenuStrip contextMenu = info.ContextMenuStrip(context, assessmentSection, treeViewControl))
{
TestHelper.AssertContextMenuStripContainsItem(contextMenu, contextMenuImportHydraulicBoundaryDatabaseIndex,
"&Koppel aan database...",
"Koppel aan hydraulische belastingendatabase.",
RiskeerCommonFormsResources.DatabaseIcon);
}
}
}
// Assert
mocks.VerifyAll();
}
[Test]
public void ForeColor_HydraulicBoundaryDatabaseNotLinked_ReturnDisabledColor()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
var hydraulicBoundaryDatabaseContext = new HydraulicBoundaryDatabaseContext(assessmentSection.HydraulicBoundaryDatabase,
assessmentSection);
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
Color color = info.ForeColor(hydraulicBoundaryDatabaseContext);
// Assert
Assert.AreEqual(Color.FromKnownColor(KnownColor.GrayText), color);
}
}
[Test]
public void ForeColor_HydraulicBoundaryDatabaseLinked_ReturnEnabledColor()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike)
{
HydraulicBoundaryDatabase =
{
FilePath = "databaseFile"
}
};
var hydraulicBoundaryDatabaseContext = new HydraulicBoundaryDatabaseContext(assessmentSection.HydraulicBoundaryDatabase,
assessmentSection);
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
Color color = info.ForeColor(hydraulicBoundaryDatabaseContext);
// Assert
Assert.AreEqual(Color.FromKnownColor(KnownColor.ControlText), color);
}
}
[Test]
public void ChildNodeObjects_HydraulicBoundaryDatabaseNotLinked_ReturnsEmpty()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
var hydraulicBoundaryDatabaseContext = new HydraulicBoundaryDatabaseContext(assessmentSection.HydraulicBoundaryDatabase,
assessmentSection);
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
object[] objects = info.ChildNodeObjects(hydraulicBoundaryDatabaseContext).ToArray();
// Assert
Assert.AreEqual(0, objects.Length);
}
}
[Test]
public void ChildNodeObjects_HydraulicBoundaryDatabaseLinked_ReturnsChildrenOfData()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike)
{
HydraulicBoundaryDatabase =
{
FilePath = "databaseFile"
}
};
var hydraulicBoundaryDatabaseContext = new HydraulicBoundaryDatabaseContext(assessmentSection.HydraulicBoundaryDatabase,
assessmentSection);
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
// Call
object[] objects = info.ChildNodeObjects(hydraulicBoundaryDatabaseContext).ToArray();
// Assert
Assert.AreEqual(5, objects.Length);
var waterLevelCalculationsForNormTargetProbabilitiesGroupContext = (WaterLevelCalculationsForNormTargetProbabilitiesGroupContext) objects[0];
Assert.AreSame(assessmentSection.HydraulicBoundaryDatabase.Locations, waterLevelCalculationsForNormTargetProbabilitiesGroupContext.WrappedData);
Assert.AreSame(assessmentSection, waterLevelCalculationsForNormTargetProbabilitiesGroupContext.AssessmentSection);
var waterLevelCalculationsForUserDefinedTargetProbabilitiesGroupContext = (WaterLevelCalculationsForUserDefinedTargetProbabilitiesGroupContext) objects[1];
Assert.AreSame(assessmentSection.WaterLevelCalculationsForUserDefinedTargetProbabilities, waterLevelCalculationsForUserDefinedTargetProbabilitiesGroupContext.WrappedData);
Assert.AreSame(assessmentSection, waterLevelCalculationsForUserDefinedTargetProbabilitiesGroupContext.AssessmentSection);
var waveHeightCalculationsForUserDefinedTargetProbabilitiesGroupContext = (WaveHeightCalculationsForUserDefinedTargetProbabilitiesGroupContext) objects[2];
Assert.AreSame(assessmentSection.WaveHeightCalculationsForUserDefinedTargetProbabilities, waveHeightCalculationsForUserDefinedTargetProbabilitiesGroupContext.WrappedData);
Assert.AreSame(assessmentSection, waveHeightCalculationsForUserDefinedTargetProbabilitiesGroupContext.AssessmentSection);
var designWaterLevelCalculationsGroupContext = (DesignWaterLevelCalculationsGroupContext) objects[3];
Assert.AreSame(assessmentSection.HydraulicBoundaryDatabase.Locations, designWaterLevelCalculationsGroupContext.WrappedData);
Assert.AreSame(assessmentSection, designWaterLevelCalculationsGroupContext.AssessmentSection);
var waveHeightCalculationsGroupContext = (WaveHeightCalculationsGroupContext) objects[4];
Assert.AreSame(assessmentSection.HydraulicBoundaryDatabase.Locations, waveHeightCalculationsGroupContext.WrappedData);
Assert.AreSame(assessmentSection, waveHeightCalculationsGroupContext.AssessmentSection);
}
}
[Test]
public void ContextMenuStrip_HydraulicBoundaryDatabaseNotLinked_ContextMenuItemCalculateAllDisabledAndTooltipSet()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
var hydraulicBoundaryLocation = new TestHydraulicBoundaryLocation("locationName");
assessmentSection.SetHydraulicBoundaryLocationCalculations(new[]
{
hydraulicBoundaryLocation
});
var nodeData = new HydraulicBoundaryDatabaseContext(assessmentSection.HydraulicBoundaryDatabase, assessmentSection);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
mocks.ReplayAll();
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
plugin.Gui = gui;
// Call
using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// Assert
ToolStripItem contextMenuItem = contextMenu.Items[contextMenuCalculateAllIndex];
Assert.AreEqual("Alles be&rekenen", contextMenuItem.Text);
StringAssert.Contains("Er is geen hydraulische belastingendatabase geïmporteerd.", contextMenuItem.ToolTipText);
TestHelper.AssertImagesAreEqual(RiskeerCommonFormsResources.CalculateAllIcon, contextMenuItem.Image);
Assert.IsFalse(contextMenuItem.Enabled);
}
}
}
mocks.VerifyAll(); // Expect no calls on arguments
}
[Test]
public void ContextMenuStrip_HydraulicBoundaryDatabaseLinkedToInvalidFile_ContextMenuItemCalculateAllDisabledAndTooltipSet()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike)
{
HydraulicBoundaryDatabase =
{
FilePath = "invalidFilePath"
}
};
var hydraulicBoundaryLocation = new TestHydraulicBoundaryLocation("locationName");
assessmentSection.SetHydraulicBoundaryLocationCalculations(new[]
{
hydraulicBoundaryLocation
});
var nodeData = new HydraulicBoundaryDatabaseContext(assessmentSection.HydraulicBoundaryDatabase, assessmentSection);
using (var treeViewControl = new TreeViewControl())
{
var gui = mocks.Stub();
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
gui.Stub(cmp => cmp.MainWindow).Return(mocks.Stub());
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
mocks.ReplayAll();
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
plugin.Gui = gui;
// Call
using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// Assert
ToolStripItem contextMenuItem = contextMenu.Items[contextMenuCalculateAllIndex];
Assert.AreEqual("Alles be&rekenen", contextMenuItem.Text);
StringAssert.Contains("Herstellen van de verbinding met de hydraulische belastingendatabase is mislukt.", contextMenuItem.ToolTipText);
TestHelper.AssertImagesAreEqual(RiskeerCommonFormsResources.CalculateAllIcon, contextMenuItem.Image);
Assert.IsFalse(contextMenuItem.Enabled);
}
}
}
mocks.VerifyAll(); // Expect no calls on arguments
}
[Test]
[TestCaseSource(nameof(GetHydraulicBoundaryLocationCalculations))]
public void ContextMenuStrip_HydraulicBoundaryLocationCalculationsWithIllustrationPoints_ContextMenuItemClearAllIllustrationPointsEnabledAndTooltipSet(
Func getHydraulicBoundaryLocationCalculationFunc)
{
// Setup
var random = new Random(21);
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
assessmentSection.SetHydraulicBoundaryLocationCalculations(new[]
{
new TestHydraulicBoundaryLocation()
});
HydraulicBoundaryLocationCalculation calculation = getHydraulicBoundaryLocationCalculationFunc(assessmentSection);
calculation.Output = new TestHydraulicBoundaryLocationCalculationOutput(random.NextDouble(), new TestGeneralResultSubMechanismIllustrationPoint());
var nodeData = new HydraulicBoundaryDatabaseContext(assessmentSection.HydraulicBoundaryDatabase, assessmentSection);
var mockRepository = new MockRepository();
using (var treeViewControl = new TreeViewControl())
{
var gui = mockRepository.Stub();
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(cmp => cmp.MainWindow).Return(mockRepository.Stub());
mockRepository.ReplayAll();
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
plugin.Gui = gui;
// Call
using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// Assert
ToolStripItem contextMenuItem = contextMenu.Items[contextMenuClearIllustrationPointsIndex];
Assert.AreEqual("Wis alle illustratiepunten...", contextMenuItem.Text);
Assert.AreEqual("Wis alle berekende illustratiepunten.", contextMenuItem.ToolTipText);
TestHelper.AssertImagesAreEqual(RiskeerCommonFormsResources.ClearIllustrationPointsIcon, contextMenuItem.Image);
Assert.IsTrue(contextMenuItem.Enabled);
}
}
}
mockRepository.VerifyAll(); // Expect no calls on arguments
}
[Test]
public void ContextMenuStrip_HydraulicBoundaryLocationCalculationsWithoutIllustrationPoints_ContextMenuItemClearAllIllustrationPointsDisabled()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
assessmentSection.SetHydraulicBoundaryLocationCalculations(new[]
{
new TestHydraulicBoundaryLocation()
});
var nodeData = new HydraulicBoundaryDatabaseContext(assessmentSection.HydraulicBoundaryDatabase, assessmentSection);
var mockRepository = new MockRepository();
using (var treeViewControl = new TreeViewControl())
{
var gui = mockRepository.Stub();
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
gui.Stub(cmp => cmp.Get(nodeData, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(cmp => cmp.MainWindow).Return(mockRepository.Stub());
mockRepository.ReplayAll();
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
plugin.Gui = gui;
// Call
using (ContextMenuStrip contextMenu = info.ContextMenuStrip(nodeData, null, treeViewControl))
{
// Assert
ToolStripItem contextMenuItem = contextMenu.Items[contextMenuClearIllustrationPointsIndex];
Assert.IsFalse(contextMenuItem.Enabled);
}
}
}
mockRepository.VerifyAll(); // Expect no calls on arguments
}
[Test]
[Apartment(ApartmentState.STA)]
public void GivenValidCalculations_WhenCalculatingAllFromContextMenu_ThenAllCalculationsScheduled()
{
// Given
string validFilePath = Path.Combine(testDataPath, "HRD dutch coast south.sqlite");
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike)
{
HydraulicBoundaryDatabase =
{
FilePath = validFilePath
}
};
HydraulicBoundaryDatabaseTestHelper.SetHydraulicBoundaryLocationConfigurationSettings(assessmentSection.HydraulicBoundaryDatabase);
var hydraulicBoundaryLocation = new TestHydraulicBoundaryLocation("locationName");
assessmentSection.SetHydraulicBoundaryLocationCalculations(new[]
{
hydraulicBoundaryLocation
});
var context = new HydraulicBoundaryDatabaseContext(assessmentSection.HydraulicBoundaryDatabase, assessmentSection);
using (var treeViewControl = new TreeViewControl())
using (var plugin = new RiskeerPlugin())
{
IMainWindow mainWindow = MainWindowTestHelper.CreateMainWindowStub(mocks);
var gui = mocks.Stub();
gui.Stub(g => g.MainWindow).Return(mainWindow);
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
gui.Stub(cmp => cmp.Get(context, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
gui.Stub(g => g.DocumentViewController).Return(mocks.Stub());
gui.Stub(g => g.ViewCommands).Return(mocks.Stub());
gui.Stub(g => g.ProjectStore).Return(mocks.Stub());
var calculatorFactory = mocks.Stub();
var designWaterLevelCalculator = new TestDesignWaterLevelCalculator
{
Converged = false
};
var waveHeightCalculator = new TestWaveHeightCalculator
{
Converged = false
};
HydraulicBoundaryCalculationSettings expectedSettings = HydraulicBoundaryCalculationSettingsFactory.CreateSettings(assessmentSection.HydraulicBoundaryDatabase);
calculatorFactory.Expect(cf => cf.CreateDesignWaterLevelCalculator(Arg.Is.NotNull))
.WhenCalled(invocation =>
{
HydraRingCalculationSettingsTestHelper.AssertHydraRingCalculationSettings(
expectedSettings, (HydraRingCalculationSettings) invocation.Arguments[0]);
})
.Return(designWaterLevelCalculator)
.Repeat
.Times(4);
calculatorFactory.Expect(cf => cf.CreateWaveHeightCalculator(Arg.Is.NotNull))
.WhenCalled(invocation =>
{
HydraRingCalculationSettingsTestHelper.AssertHydraRingCalculationSettings(
expectedSettings, (HydraRingCalculationSettings) invocation.Arguments[0]);
}).Return(waveHeightCalculator)
.Repeat
.Times(2);
mocks.ReplayAll();
TreeNodeInfo info = GetInfo(plugin);
plugin.Gui = gui;
plugin.Activate();
DialogBoxHandler = (name, wnd) =>
{
// Expect an activity dialog which is automatically closed
};
using (ContextMenuStrip contextMenuAdapter = info.ContextMenuStrip(context, null, treeViewControl))
using (new HydraRingCalculatorFactoryConfig(calculatorFactory))
{
// When
void Call() => contextMenuAdapter.Items[contextMenuCalculateAllIndex].PerformClick();
// Then
TestHelper.AssertLogMessages(Call, messages =>
{
string[] msgs = messages.ToArray();
Assert.AreEqual(48, msgs.Length);
const string designWaterLevelCalculationTypeDisplayName = "Waterstand";
const string designWaterLevelCalculationDisplayName = "Waterstand berekening";
var noProbabilityValueDoubleConverter = new NoProbabilityValueDoubleConverter();
HydraulicBoundaryLocationCalculationActivityLogTestHelper.AssertHydraulicBoundaryLocationCalculationMessages(
hydraulicBoundaryLocation.Name, designWaterLevelCalculationTypeDisplayName, designWaterLevelCalculationDisplayName,
noProbabilityValueDoubleConverter.ConvertToString(assessmentSection.FailureMechanismContribution.SignalingNorm),
msgs, 0);
HydraulicBoundaryLocationCalculationActivityLogTestHelper.AssertHydraulicBoundaryLocationCalculationMessages(
hydraulicBoundaryLocation.Name, designWaterLevelCalculationTypeDisplayName, designWaterLevelCalculationDisplayName,
noProbabilityValueDoubleConverter.ConvertToString(assessmentSection.FailureMechanismContribution.LowerLimitNorm),
msgs, 8);
HydraulicBoundaryLocationCalculationActivityLogTestHelper.AssertHydraulicBoundaryLocationCalculationMessages(
hydraulicBoundaryLocation.Name, designWaterLevelCalculationTypeDisplayName, designWaterLevelCalculationDisplayName,
noProbabilityValueDoubleConverter.ConvertToString(assessmentSection.WaterLevelCalculationsForUserDefinedTargetProbabilities[0].TargetProbability),
msgs, 16);
HydraulicBoundaryLocationCalculationActivityLogTestHelper.AssertHydraulicBoundaryLocationCalculationMessages(
hydraulicBoundaryLocation.Name, designWaterLevelCalculationTypeDisplayName, designWaterLevelCalculationDisplayName,
noProbabilityValueDoubleConverter.ConvertToString(assessmentSection.WaterLevelCalculationsForUserDefinedTargetProbabilities[1].TargetProbability),
msgs, 24);
const string waveHeightCalculationTypeDisplayName = "Golfhoogte";
const string waveHeightCalculationDisplayName = "Golfhoogte berekening";
HydraulicBoundaryLocationCalculationActivityLogTestHelper.AssertHydraulicBoundaryLocationCalculationMessages(
hydraulicBoundaryLocation.Name, waveHeightCalculationTypeDisplayName, waveHeightCalculationDisplayName,
noProbabilityValueDoubleConverter.ConvertToString(assessmentSection.WaveHeightCalculationsForUserDefinedTargetProbabilities[0].TargetProbability),
msgs, 32);
HydraulicBoundaryLocationCalculationActivityLogTestHelper.AssertHydraulicBoundaryLocationCalculationMessages(
hydraulicBoundaryLocation.Name, waveHeightCalculationTypeDisplayName, waveHeightCalculationDisplayName,
noProbabilityValueDoubleConverter.ConvertToString(assessmentSection.WaveHeightCalculationsForUserDefinedTargetProbabilities[1].TargetProbability),
msgs, 40);
});
}
}
mocks.VerifyAll();
}
[Test]
[TestCaseSource(nameof(GetHydraulicBoundaryLocationCalculations))]
[Apartment(ApartmentState.STA)]
public void GivenCalculationsWithIllustrationPoints_WhenClearIllustrationPointsClickedAndDoNotContinue_ThenInquiryAndIllustrationPointsNotCleared(
Func getHydraulicLocationCalculationFunc)
{
// Given
var random = new Random(21);
AssessmentSection assessmentSection = GetAssessmentSectionWithHydraulicBoundaryLocationCalculationOutputs();
HydraulicBoundaryLocationCalculation calculation = getHydraulicLocationCalculationFunc(assessmentSection);
calculation.Output = new TestHydraulicBoundaryLocationCalculationOutput(random.NextDouble(), new TestGeneralResultSubMechanismIllustrationPoint());
HydraulicBoundaryLocationCalculation[] calculationsWithOutput = GetAllHydraulicLocationCalculationsWithOutput(assessmentSection).ToArray();
var messageBoxText = "";
DialogBoxHandler = (name, wnd) =>
{
var helper = new MessageBoxTester(wnd);
messageBoxText = helper.Text;
helper.ClickCancel();
};
var context = new HydraulicBoundaryDatabaseContext(assessmentSection.HydraulicBoundaryDatabase, assessmentSection);
var mockRepository = new MockRepository();
using (var treeViewControl = new TreeViewControl())
{
var affectedCalculationObserver = mockRepository.StrictMock();
var gui = mockRepository.Stub();
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
gui.Stub(cmp => cmp.MainWindow).Return(mockRepository.Stub());
gui.Stub(cmp => cmp.Get(context, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
mockRepository.ReplayAll();
calculation.Attach(affectedCalculationObserver);
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
plugin.Gui = gui;
using (ContextMenuStrip contextMenuAdapter = info.ContextMenuStrip(context, null, treeViewControl))
{
// When
contextMenuAdapter.Items[contextMenuClearIllustrationPointsIndex].PerformClick();
// Then
const string expectedMessage = "Weet u zeker dat u alle berekende illustratiepunten bij 'Hydraulische belastingen' wilt wissen?";
Assert.AreEqual(expectedMessage, messageBoxText);
Assert.IsTrue(calculationsWithOutput.All(calc => calc.HasOutput));
Assert.IsTrue(calculation.Output.HasGeneralResult);
}
}
}
mockRepository.VerifyAll();
}
[Test]
[TestCaseSource(nameof(GetHydraulicBoundaryLocationCalculations))]
[Apartment(ApartmentState.STA)]
public void GivenCalculationsWithIllustrationPoints_WhenClearIllustrationPointsClickedAndContinue_ThenInquiryAndIllustrationPointsCleared(
Func getHydraulicLocationCalculationFunc)
{
// Given
var random = new Random(21);
AssessmentSection assessmentSection = GetAssessmentSectionWithHydraulicBoundaryLocationCalculationOutputs();
HydraulicBoundaryLocationCalculation calculation = getHydraulicLocationCalculationFunc(assessmentSection);
calculation.Output = new TestHydraulicBoundaryLocationCalculationOutput(random.NextDouble(), new TestGeneralResultSubMechanismIllustrationPoint());
HydraulicBoundaryLocationCalculation unaffectedCalculation = assessmentSection.WaveHeightCalculationsForFactorizedLowerLimitNorm.ElementAt(1);
HydraulicBoundaryLocationCalculation[] calculationsWithOutput = GetAllHydraulicLocationCalculationsWithOutput(assessmentSection).ToArray();
var messageBoxText = "";
DialogBoxHandler = (name, wnd) =>
{
var helper = new MessageBoxTester(wnd);
messageBoxText = helper.Text;
helper.ClickOk();
};
var context = new HydraulicBoundaryDatabaseContext(assessmentSection.HydraulicBoundaryDatabase, assessmentSection);
var mockRepository = new MockRepository();
using (var treeViewControl = new TreeViewControl())
{
var affectedCalculationObserver = mockRepository.StrictMock();
affectedCalculationObserver.Expect(o => o.UpdateObserver());
var unAffectedCalculationObserver = mockRepository.StrictMock();
var gui = mockRepository.Stub();
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
gui.Stub(cmp => cmp.MainWindow).Return(mockRepository.Stub());
gui.Stub(cmp => cmp.Get(context, treeViewControl)).Return(new CustomItemsOnlyContextMenuBuilder());
mockRepository.ReplayAll();
calculation.Attach(affectedCalculationObserver);
unaffectedCalculation.Attach(unAffectedCalculationObserver);
using (var plugin = new RiskeerPlugin())
{
TreeNodeInfo info = GetInfo(plugin);
plugin.Gui = gui;
using (ContextMenuStrip contextMenuAdapter = info.ContextMenuStrip(context, null, treeViewControl))
{
// When
contextMenuAdapter.Items[contextMenuClearIllustrationPointsIndex].PerformClick();
// Then
const string expectedMessage = "Weet u zeker dat u alle berekende illustratiepunten bij 'Hydraulische belastingen' wilt wissen?";
Assert.AreEqual(expectedMessage, messageBoxText);
Assert.IsTrue(calculationsWithOutput.All(calc => calc.HasOutput));
Assert.IsFalse(calculation.Output.HasGeneralResult);
}
}
}
mockRepository.VerifyAll();
}
private static AssessmentSection GetAssessmentSectionWithHydraulicBoundaryLocationCalculationOutputs()
{
var random = new Random(21);
var assessmentSection = new AssessmentSection(random.NextEnumValue());
assessmentSection.SetHydraulicBoundaryLocationCalculations(new[]
{
new TestHydraulicBoundaryLocation(),
new TestHydraulicBoundaryLocation()
});
SetHydraulicBoundaryLocationOutput(assessmentSection.WaterLevelCalculationsForSignalingNorm);
SetHydraulicBoundaryLocationOutput(assessmentSection.WaterLevelCalculationsForLowerLimitNorm);
SetHydraulicBoundaryLocationOutput(assessmentSection.WaterLevelCalculationsForUserDefinedTargetProbabilities[0].HydraulicBoundaryLocationCalculations);
SetHydraulicBoundaryLocationOutput(assessmentSection.WaterLevelCalculationsForUserDefinedTargetProbabilities[1].HydraulicBoundaryLocationCalculations);
SetHydraulicBoundaryLocationOutput(assessmentSection.WaveHeightCalculationsForUserDefinedTargetProbabilities[0].HydraulicBoundaryLocationCalculations);
SetHydraulicBoundaryLocationOutput(assessmentSection.WaveHeightCalculationsForUserDefinedTargetProbabilities[1].HydraulicBoundaryLocationCalculations);
return assessmentSection;
}
private static void SetHydraulicBoundaryLocationOutput(IEnumerable calculations)
{
var random = new Random(21);
foreach (HydraulicBoundaryLocationCalculation calculation in calculations)
{
calculation.Output = new TestHydraulicBoundaryLocationCalculationOutput(random.NextDouble());
}
}
public override void Setup()
{
mocks = new MockRepository();
}
private static IEnumerable GetAllHydraulicLocationCalculationsWithOutput(IAssessmentSection assessmentSection)
{
return assessmentSection.WaterLevelCalculationsForFactorizedSignalingNorm
.Concat(assessmentSection.WaterLevelCalculationsForSignalingNorm)
.Concat(assessmentSection.WaterLevelCalculationsForLowerLimitNorm)
.Concat(assessmentSection.WaterLevelCalculationsForUserDefinedTargetProbabilities[0].HydraulicBoundaryLocationCalculations)
.Concat(assessmentSection.WaterLevelCalculationsForUserDefinedTargetProbabilities[1].HydraulicBoundaryLocationCalculations)
.Concat(assessmentSection.WaveHeightCalculationsForUserDefinedTargetProbabilities[0].HydraulicBoundaryLocationCalculations)
.Concat(assessmentSection.WaveHeightCalculationsForUserDefinedTargetProbabilities[1].HydraulicBoundaryLocationCalculations)
.Where(calc => calc.HasOutput);
}
private static IEnumerable GetHydraulicBoundaryLocationCalculations()
{
yield return new TestCaseData(new Func(
section => section.WaterLevelCalculationsForSignalingNorm.First()));
yield return new TestCaseData(new Func(
section => section.WaterLevelCalculationsForLowerLimitNorm.First()));
yield return new TestCaseData(new Func(
section => section.WaterLevelCalculationsForUserDefinedTargetProbabilities[0].HydraulicBoundaryLocationCalculations.First()));
yield return new TestCaseData(new Func(
section => section.WaterLevelCalculationsForUserDefinedTargetProbabilities[1].HydraulicBoundaryLocationCalculations.First()));
yield return new TestCaseData(new Func(
section => section.WaveHeightCalculationsForUserDefinedTargetProbabilities[0].HydraulicBoundaryLocationCalculations.First()));
yield return new TestCaseData(new Func(
section => section.WaveHeightCalculationsForUserDefinedTargetProbabilities[1].HydraulicBoundaryLocationCalculations.First()));
}
private static TreeNodeInfo GetInfo(RiskeerPlugin plugin)
{
return plugin.GetTreeNodeInfos().First(tni => tni.TagType == typeof(HydraulicBoundaryDatabaseContext));
}
}
}