Index: Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/RingtoetsPluginTest.cs =================================================================== diff -u -r1e2b39b01c3c7e64af8c8c2ab1b54b522c9fdd4b -r17ec0ca5a375889a40d67a6130b1c7f1d0a375fe --- Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/RingtoetsPluginTest.cs (.../RingtoetsPluginTest.cs) (revision 1e2b39b01c3c7e64af8c8c2ab1b54b522c9fdd4b) +++ Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/RingtoetsPluginTest.cs (.../RingtoetsPluginTest.cs) (revision 17ec0ca5a375889a40d67a6130b1c7f1d0a375fe) @@ -23,10 +23,13 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; +using System.Windows.Forms; using System.Windows.Threading; using Core.Common.Base; using Core.Common.Base.IO; using Core.Common.Base.Storage; +using Core.Common.Controls.DataGrid; using Core.Common.Controls.TreeView; using Core.Common.Gui; using Core.Common.Gui.ContextMenu; @@ -35,6 +38,7 @@ using Core.Common.Gui.Plugin; using Core.Common.Gui.Settings; using Core.Common.TestUtil; +using NUnit.Extensions.Forms; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.Common.Data; @@ -49,19 +53,23 @@ using Ringtoets.HydraRing.Data; using Ringtoets.Integration.Data; using Ringtoets.Integration.Data.StandAlone.SectionResults; +using Ringtoets.Integration.Forms; using Ringtoets.Integration.Forms.PresentationObjects; using Ringtoets.Integration.Forms.PropertyClasses; using Ringtoets.Integration.Forms.Views; using Ringtoets.Integration.Forms.Views.SectionResultViews; using Ringtoets.Integration.Plugin.FileImporters; +using PropertyInfo = Core.Common.Gui.Plugin.PropertyInfo; using RingtoetsFormsResources = Ringtoets.Integration.Forms.Properties.Resources; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; namespace Ringtoets.Integration.Plugin.Test { [TestFixture] - public class RingtoetsPluginTest + public class RingtoetsPluginTest : NUnitFormTest { + private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "ReferenceLineMetaImporter"); + [Test] [STAThread] // For creation of XAML UI component public void DefaultConstructor_ExpectedValues() @@ -638,5 +646,185 @@ // Assert CollectionAssert.AllItemsAreUnique(project.Items.Select(section => section.Name)); } + + [Test] + public void GetAssessmentSectionFromFile_NoShapeFileFound_LogsWarningReturnsNull() + { + // Setup + var mockRepository = new MockRepository(); + var windows = mockRepository.Stub(); + var guiMock = mockRepository.StrictMock(); + guiMock.Stub(g => g.SelectionChanged += null).IgnoreArguments(); + guiMock.Stub(g => g.SelectionChanged -= null).IgnoreArguments(); + guiMock.Expect(g => g.ProjectOpened += null).IgnoreArguments(); + guiMock.Stub(g => g.ProjectOpened -= null).IgnoreArguments(); + guiMock.Expect(g => g.MainWindow).Return(windows); + mockRepository.ReplayAll(); + + string nonExistingFolder = @"c:\folderDoesNotExist\"; + + using (var plugin = new RingtoetsPlugin()) + { + plugin.Gui = guiMock; + + SetShapeFileDirectory(plugin, nonExistingFolder); + + // Call + IAssessmentSection assessmentSection = null; + Action action = () => assessmentSection = plugin.GetAssessmentSectionFromFile(); + + // Assert + TestHelper.AssertLogMessageIsGenerated(action, string.Format("Fout bij het lezen van bestand '{0}': De map locatie is ongeldig.", nonExistingFolder)); + Assert.IsNull(assessmentSection); + } + + mockRepository.VerifyAll(); + } + + [Test] + public void GetAssessmentSectionFromFile_validDirectoryWithEmptyShapeFile_LogsWarningShowsMessageReturnsNull() + { + // Setup + var mockRepository = new MockRepository(); + var windows = mockRepository.Stub(); + var guiMock = mockRepository.StrictMock(); + guiMock.Stub(g => g.SelectionChanged += null).IgnoreArguments(); + guiMock.Stub(g => g.SelectionChanged -= null).IgnoreArguments(); + guiMock.Expect(g => g.ProjectOpened += null).IgnoreArguments(); + guiMock.Stub(g => g.ProjectOpened -= null).IgnoreArguments(); + guiMock.Expect(g => g.MainWindow).Return(windows); + mockRepository.ReplayAll(); + + string pathValidFolder = Path.Combine(testDataPath, "EmptyShapeFile"); + + using (var plugin = new RingtoetsPlugin()) + { + plugin.Gui = guiMock; + SetShapeFileDirectory(plugin, pathValidFolder); + + string messageText = null; + DialogBoxHandler = (name, wnd) => + { + var messageBox = new MessageBoxTester(wnd); + messageText = messageBox.Text; + messageBox.ClickOk(); + }; + + // Call + IAssessmentSection assessmentSection = null; + Action action = () => assessmentSection = plugin.GetAssessmentSectionFromFile(); + + // Assert + const string expectedErrorMessage = "Er kunnen geen trajecten gelezen worden uit het shape bestand."; + TestHelper.AssertLogMessageIsGenerated(action, expectedErrorMessage); + Assert.AreEqual(expectedErrorMessage, messageText); + Assert.IsNull(assessmentSection); + } + + mockRepository.VerifyAll(); + } + + [Test] + public void GetAssessmentSectionFromFile_validDirectoryOkClicked_ReturnsFirstAssessmentSection() + { + // Setup + var mockRepository = new MockRepository(); + var windows = mockRepository.Stub(); + var guiMock = mockRepository.StrictMock(); + guiMock.Stub(g => g.SelectionChanged += null).IgnoreArguments(); + guiMock.Stub(g => g.SelectionChanged -= null).IgnoreArguments(); + guiMock.Expect(g => g.ProjectOpened += null).IgnoreArguments(); + guiMock.Stub(g => g.ProjectOpened -= null).IgnoreArguments(); + guiMock.Expect(g => g.MainWindow).Return(windows); + mockRepository.ReplayAll(); + + string pathValidFolder = Path.Combine(testDataPath, "ValidShapeFile"); + + using (var plugin = new RingtoetsPlugin()) + { + plugin.Gui = guiMock; + SetShapeFileDirectory(plugin, pathValidFolder); + + DialogBoxHandler = (name, wnd) => + { + var selectionDialog = (ReferenceLineMetaSelectionDialog) new FormTester(name).TheObject; + var grid = (DataGridViewControl) new ControlTester("ReferenceLineMetaDataGridViewControl", selectionDialog).TheObject; + var dataGridView = grid.Controls.OfType().First(); + dataGridView[0, 0].Selected = true; + new ButtonTester("Ok", selectionDialog).Click(); + }; + + // Call + IAssessmentSection assessmentSection = plugin.GetAssessmentSectionFromFile(); + + // Assert + Assert.IsInstanceOf(assessmentSection); + Assert.AreEqual("1-2", assessmentSection.Id); + } + + mockRepository.VerifyAll(); + } + + [Test] + public void GetDataItemInfos_ReturnsExpectedDataItemDefinitions() + { + // Setup + var mockRepository = new MockRepository(); + var windows = mockRepository.Stub(); + var guiMock = mockRepository.StrictMock(); + guiMock.Stub(g => g.SelectionChanged += null).IgnoreArguments(); + guiMock.Stub(g => g.SelectionChanged -= null).IgnoreArguments(); + guiMock.Expect(g => g.ProjectOpened += null).IgnoreArguments(); + guiMock.Stub(g => g.ProjectOpened -= null).IgnoreArguments(); + guiMock.Expect(g => g.MainWindow).Return(windows); + mockRepository.ReplayAll(); + + string pathValidFolder = Path.Combine(testDataPath, "ValidShapeFile"); + + using (var plugin = new RingtoetsPlugin()) + { + plugin.Gui = guiMock; + + SetShapeFileDirectory(plugin, pathValidFolder); + + DialogBoxHandler = (name, wnd) => + { + var selectionDialog = (ReferenceLineMetaSelectionDialog)new FormTester(name).TheObject; + var grid = (DataGridViewControl)new ControlTester("ReferenceLineMetaDataGridViewControl", selectionDialog).TheObject; + var dataGridView = grid.Controls.OfType().First(); + dataGridView[0, 0].Selected = true; + new ButtonTester("Ok", selectionDialog).Click(); + }; + + // Call + var dataItemDefinitions = plugin.GetDataItemInfos().ToArray(); + + // Assert + Assert.AreEqual(1, dataItemDefinitions.Length); + + DataItemInfo assessmentSectionDataItemDefinition = dataItemDefinitions.Single(did => did.ValueType == typeof(IAssessmentSection)); + Assert.AreEqual("Traject", assessmentSectionDataItemDefinition.Name); + Assert.AreEqual("Algemeen", assessmentSectionDataItemDefinition.Category); + TestHelper.AssertImagesAreEqual(RingtoetsFormsResources.AssessmentSectionFolderIcon, assessmentSectionDataItemDefinition.Image); + Assert.IsNull(assessmentSectionDataItemDefinition.AdditionalOwnerCheck); + Assert.IsInstanceOf(assessmentSectionDataItemDefinition.CreateData(new RingtoetsProject())); + } + mockRepository.VerifyAll(); + } + + private static void SetShapeFileDirectory(RingtoetsPlugin plugin, string nonExistingFolder) + { + string privateShapeFileDirectoryName = "shapeFileDirectory"; + Type ringtoetsPluginType = plugin.GetType(); + FieldInfo fieldInfo = ringtoetsPluginType.GetField(privateShapeFileDirectoryName, BindingFlags.NonPublic | BindingFlags.Instance); + if (fieldInfo == null) + { + Assert.Fail("Unable to find private field '{0}'", privateShapeFileDirectoryName); + } + else + { + fieldInfo.SetValue(plugin, nonExistingFolder); + } + } } } \ No newline at end of file