Index: Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsPlugin.cs =================================================================== diff -u -r1eed3e3f652618c52a462edc502cfd4250772314 -r1e2b39b01c3c7e64af8c8c2ab1b54b522c9fdd4b --- Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsPlugin.cs (.../RingtoetsPlugin.cs) (revision 1eed3e3f652618c52a462edc502cfd4250772314) +++ Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsPlugin.cs (.../RingtoetsPlugin.cs) (revision 1e2b39b01c3c7e64af8c8c2ab1b54b522c9fdd4b) @@ -253,18 +253,23 @@ public void SetAssessmentSectionToProject(RingtoetsProject ringtoetsProject, AssessmentSection assessmentSection) { + if (ringtoetsProject == null) + { + throw new ArgumentNullException("ringtoetsProject"); + } if (assessmentSection == null) { - return; + throw new ArgumentNullException("assessmentSection"); } + assessmentSection.Name = GetUniqueForAssessmentSectionName(ringtoetsProject.Items, assessmentSection.Name); ringtoetsProject.Items.Add(assessmentSection); ringtoetsProject.NotifyObservers(); - if (Gui != null && (Gui.Selection == null || Gui.Selection.Equals(assessmentSection))) + if (Gui != null && (Gui.Selection == null || !Gui.Selection.Equals(assessmentSection))) { Gui.Selection = assessmentSection; - Gui.DocumentViewController.OpenViewForData(Gui.Selection); + Gui.DocumentViewController.OpenViewForData(assessmentSection); } } @@ -372,6 +377,7 @@ if (assessmentSection == null) { + return Enumerable.Empty(); } return new DataItemInfo[] @@ -556,6 +562,10 @@ private void SetAssessmentSectionFromFileToProject(RingtoetsProject ringtoetsProject) { var assessmentSection = GetAssessmentSectionFromFile(); + if (!(assessmentSection is AssessmentSection)) + { + return; + } SetAssessmentSectionToProject(ringtoetsProject, (AssessmentSection) assessmentSection); } Index: Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/RingtoetsPluginTest.cs =================================================================== diff -u -r1eed3e3f652618c52a462edc502cfd4250772314 -r1e2b39b01c3c7e64af8c8c2ab1b54b522c9fdd4b --- Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/RingtoetsPluginTest.cs (.../RingtoetsPluginTest.cs) (revision 1eed3e3f652618c52a462edc502cfd4250772314) +++ Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/RingtoetsPluginTest.cs (.../RingtoetsPluginTest.cs) (revision 1e2b39b01c3c7e64af8c8c2ab1b54b522c9fdd4b) @@ -24,13 +24,14 @@ using System.IO; using System.Linq; using System.Windows.Threading; -using Core.Common.Base.Data; +using Core.Common.Base; using Core.Common.Base.IO; using Core.Common.Base.Storage; using Core.Common.Controls.TreeView; using Core.Common.Gui; using Core.Common.Gui.ContextMenu; using Core.Common.Gui.Forms.MainWindow; +using Core.Common.Gui.Forms.ViewHost; using Core.Common.Gui.Plugin; using Core.Common.Gui.Settings; using Core.Common.TestUtil; @@ -427,43 +428,213 @@ public void GetChildDataWithViewDefinitions_UnsupportedData_ReturnEmpty() { // Setup - var plugin = new RingtoetsPlugin(); + using (var plugin = new RingtoetsPlugin()) + { + // Call + var childrenWithViewDefinitions = plugin.GetChildDataWithViewDefinitions(1); - // Call - var childrenWithViewDefinitions = plugin.GetChildDataWithViewDefinitions(1); + // Assert + CollectionAssert.IsEmpty(childrenWithViewDefinitions); + } + } + [Test] + public void GetFileImporters_ReturnsExpectedFileImporters() + { + // Setup + using (var plugin = new RingtoetsPlugin()) + { + // Call + IFileImporter[] importers = plugin.GetFileImporters().ToArray(); + + // Assert + Assert.AreEqual(2, importers.Length); + Assert.AreEqual(1, importers.Count(i => i is ReferenceLineImporter)); + Assert.AreEqual(1, importers.Count(i => i is FailureMechanismSectionsImporter)); + } + } + + [Test] + public void SetAssessmentSectionToProject_ProjectIsNull_ThrowsArgumentNullException() + { + // Setup + var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); + using (var plugin = new RingtoetsPlugin()) + { + // Call + TestDelegate call = () => plugin.SetAssessmentSectionToProject(null, assessmentSection); + + // Assert + var paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("ringtoetsProject", paramName); + } + } + + [Test] + public void SetAssessmentSectionToProject_AssessmentSectionIsNull_ThrowsArgumentNullException() + { + // Setup + var project = new RingtoetsProject(); + using (var plugin = new RingtoetsPlugin()) + { + // Call + TestDelegate call = () => plugin.SetAssessmentSectionToProject(project, null); + + // Assert + var paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("assessmentSection", paramName); + } + } + + [Test] + public void SetAssessmentSectionToProject_WithData_UpdateDataAndNotifyObservers() + { + // Setup + var mockRepository = new MockRepository(); + var projectObserver = mockRepository.StrictMock(); + projectObserver.Expect(o => o.UpdateObserver()); + mockRepository.ReplayAll(); + + var project = new RingtoetsProject(); + var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); + using (var plugin = new RingtoetsPlugin()) + { + project.Attach(projectObserver); + + // Precondition + CollectionAssert.IsEmpty(project.Items); + + // Call + plugin.SetAssessmentSectionToProject(project, assessmentSection); + } // Assert - CollectionAssert.IsEmpty(childrenWithViewDefinitions); + Assert.AreEqual(1, project.Items.Count); + CollectionAssert.Contains(project.Items, assessmentSection); + mockRepository.VerifyAll(); } [Test] - public void GetFileImporters_ReturnsExpectedFileImporters() + public void SetAssessmentSectionToProject_NoSelectionSet_SelectionSetUpdated() { // Setup - var plugin = new RingtoetsPlugin(); + var mockRepository = new MockRepository(); + var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); - // Call - IFileImporter[] importers = plugin.GetFileImporters().ToArray(); + var documentViewControllerMock = mockRepository.StrictMock(); + documentViewControllerMock.Expect(d => d.OpenViewForData(assessmentSection)).Return(true); + 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.Selection).SetPropertyWithArgument(assessmentSection); + guiMock.Expect(g => g.Selection).Return(null); + guiMock.Expect(g => g.DocumentViewController).Return(documentViewControllerMock); + mockRepository.ReplayAll(); + + var project = new RingtoetsProject(); + + using (var plugin = new RingtoetsPlugin()) + { + plugin.Gui = guiMock; + + // Call + plugin.SetAssessmentSectionToProject(project, assessmentSection); + } + // Assert - Assert.AreEqual(2, importers.Length); - Assert.AreEqual(1, importers.Count(i => i is ReferenceLineImporter)); - Assert.AreEqual(1, importers.Count(i => i is FailureMechanismSectionsImporter)); + Assert.AreEqual(1, project.Items.Count); + CollectionAssert.Contains(project.Items, assessmentSection); + mockRepository.VerifyAll(); } [Test] + public void SetAssessmentSectionToProject_SelectionAlreadySetToSameAssessmentSection_SelectionNotUpdated() + { + // Setup + var mockRepository = new MockRepository(); + var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); + + 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.Selection).Return(assessmentSection).Repeat.AtLeastOnce(); + + mockRepository.ReplayAll(); + + var project = new RingtoetsProject(); + + using (var plugin = new RingtoetsPlugin()) + { + plugin.Gui = guiMock; + + // Call + plugin.SetAssessmentSectionToProject(project, assessmentSection); + } + + // Assert + Assert.AreEqual(1, project.Items.Count); + CollectionAssert.Contains(project.Items, assessmentSection); + mockRepository.VerifyAll(); + } + + [Test] + public void SetAssessmentSectionToProject_SelectionAlreadySetToDifferentAssessmentSection_SelectionUpdated() + { + // Setup + var mockRepository = new MockRepository(); + var previousAssessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); + var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike); + + var documentViewControllerMock = mockRepository.StrictMock(); + documentViewControllerMock.Expect(d => d.OpenViewForData(assessmentSection)).Return(true); + + 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.Selection).SetPropertyWithArgument(assessmentSection); + guiMock.Expect(g => g.Selection).Return(previousAssessmentSection).Repeat.AtLeastOnce(); + guiMock.Expect(g => g.DocumentViewController).Return(documentViewControllerMock); + mockRepository.ReplayAll(); + + var project = new RingtoetsProject(); + + using (var plugin = new RingtoetsPlugin()) + { + plugin.Gui = guiMock; + + // Call + plugin.SetAssessmentSectionToProject(project, assessmentSection); + } + + // Assert + Assert.AreEqual(1, project.Items.Count); + CollectionAssert.Contains(project.Items, assessmentSection); + mockRepository.VerifyAll(); + } + + [Test] public void WhenAddingAssessmentSection_GivenProjectHasAssessmentSection_ThenAddedAssessmentSectionHasUniqueName() { // Setup var project = new RingtoetsProject(); - var plugin = new RingtoetsPlugin(); var assessmentSection1 = new AssessmentSection(AssessmentSectionComposition.Dike); var assessmentSection2 = new AssessmentSection(AssessmentSectionComposition.Dike); - plugin.SetAssessmentSectionToProject(project, assessmentSection1); - // Call - plugin.SetAssessmentSectionToProject(project, assessmentSection2); + using (var plugin = new RingtoetsPlugin()) + { + plugin.SetAssessmentSectionToProject(project, assessmentSection1); + // Call + plugin.SetAssessmentSectionToProject(project, assessmentSection2); + } + // Assert CollectionAssert.AllItemsAreUnique(project.Items.Select(section => section.Name)); }