// Copyright (C) Stichting Deltares 2017. 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.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Windows.Threading; using Core.Common.Base.Data; using Core.Common.Base.Storage; using Core.Common.Controls.TreeView; using Core.Common.Gui; 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.Gui.TestUtil; 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.Contribution; using Ringtoets.Common.Data.DikeProfiles; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.Data.IllustrationPoints; using Ringtoets.Common.Data.Structures; using Ringtoets.Common.Forms.PresentationObjects; using Ringtoets.Common.Forms.PropertyClasses; using Ringtoets.Common.Forms.Views; using Ringtoets.Integration.Data; using Ringtoets.Integration.Data.StandAlone.SectionResults; using Ringtoets.Integration.Forms.PresentationObjects; using Ringtoets.Integration.Forms.PropertyClasses; using Ringtoets.Integration.Forms.Views; using Ringtoets.Integration.Forms.Views.SectionResultViews; using Ringtoets.Revetment.Data; using Ringtoets.Revetment.Forms.PresentationObjects; using Ringtoets.Revetment.Forms.Views; using RingtoetsCommonServiceResources = Ringtoets.Common.Service.Properties.Resources; namespace Ringtoets.Integration.Plugin.Test { [TestFixture] public class RingtoetsPluginTest : NUnitFormTest { [Test] [Apartment(ApartmentState.STA)] public void DefaultConstructor_ExpectedValues() { // Call using (var plugin = new RingtoetsPlugin()) { // Assert Assert.IsInstanceOf(plugin); Assert.IsNull(plugin.RibbonCommandHandler); } } [Test] [Apartment(ApartmentState.STA)] public void GivenPluginWithGuiSet_WhenProjectOnGuiChangesToProjectWithHydraulicBoundaryDatabaseNotCoupled_ThenNoWarning() { // Given var mocks = new MockRepository(); var projectStore = mocks.Stub(); var projectMigrator = mocks.Stub(); mocks.ReplayAll(); using (var gui = new GuiCore(new MainWindow(), projectStore, projectMigrator, new RingtoetsProjectFactory(), new GuiCoreSettings())) { using (var plugin = new RingtoetsPlugin()) { plugin.Gui = gui; gui.Run(); var project = new RingtoetsProject { AssessmentSections = { new AssessmentSection(AssessmentSectionComposition.Dike) } }; // When Action action = () => gui.SetProject(project, null); // Then TestHelper.AssertLogMessagesCount(action, 0); } } mocks.VerifyAll(); Dispatcher.CurrentDispatcher.InvokeShutdown(); } [Test] [Apartment(ApartmentState.STA)] public void GivenPluginWithGuiSet_WhenProjectOnGuiChangesToProjectWithHydraulicBoundaryDatabaseCoupledToExistingLocation_ThenNoWarning() { // Given var mocks = new MockRepository(); var projectStore = mocks.Stub(); var projectMigrator = mocks.Stub(); mocks.ReplayAll(); string testDataDir = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "HydraulicBoundaryDatabaseImporter"); string testFilePath = Path.Combine(testDataDir, "complete.sqlite"); using (var gui = new GuiCore(new MainWindow(), projectStore, projectMigrator, new RingtoetsProjectFactory(), new GuiCoreSettings())) { using (var plugin = new RingtoetsPlugin()) { plugin.Gui = gui; gui.Run(); var project = new RingtoetsProject { AssessmentSections = { new AssessmentSection(AssessmentSectionComposition.Dike) { HydraulicBoundaryDatabase = { FilePath = testFilePath } } } }; // When Action action = () => gui.SetProject(project, null); // Then TestHelper.AssertLogMessagesCount(action, 0); } } mocks.VerifyAll(); Dispatcher.CurrentDispatcher.InvokeShutdown(); } [Test] [Apartment(ApartmentState.STA)] public void GivenPluginWithGuiSet_WhenProjectOnGuiChangesToProjectWithHydraulicBoundaryDatabaseCoupledToNonExistingLocation_ThenWarning() { // Given var mocks = new MockRepository(); var projectStore = mocks.Stub(); var projectMigrator = mocks.Stub(); mocks.ReplayAll(); using (var gui = new GuiCore(new MainWindow(), projectStore, projectMigrator, new RingtoetsProjectFactory(), new GuiCoreSettings())) { using (var plugin = new RingtoetsPlugin()) { plugin.Gui = gui; gui.Run(); const string nonExistingFile = "not_existing_file"; var project = new RingtoetsProject { AssessmentSections = { new AssessmentSection(AssessmentSectionComposition.Dike) { HydraulicBoundaryDatabase = { FilePath = nonExistingFile } } } }; // When Action action = () => gui.SetProject(project, null); // Then string fileMissingMessage = $"Fout bij het lezen van bestand '{nonExistingFile}': het bestand bestaat niet."; string message = string.Format( RingtoetsCommonServiceResources.Hydraulic_boundary_database_connection_failed_0_, fileMissingMessage); TestHelper.AssertLogMessageWithLevelIsGenerated(action, Tuple.Create(message, LogLevelConstant.Warn)); } } mocks.VerifyAll(); Dispatcher.CurrentDispatcher.InvokeShutdown(); } [Test] public void GetPropertyInfos_ReturnsSupportedPropertyInfos() { // Setup using (var plugin = new RingtoetsPlugin()) { // Call PropertyInfo[] propertyInfos = plugin.GetPropertyInfos().ToArray(); // Assert Assert.AreEqual(19, propertyInfos.Length); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(IProject), typeof(RingtoetsProjectProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(IAssessmentSection), typeof(AssessmentSectionProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(BackgroundData), typeof(BackgroundDataProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(HydraulicBoundaryDatabaseContext), typeof(HydraulicBoundaryDatabaseProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(FailureMechanismContributionContext), typeof(FailureMechanismContributionProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(FailureMechanismContext), typeof(StandAloneFailureMechanismContextProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(ICalculationContext), typeof(CalculationGroupContextProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(ICalculationContext), typeof(CalculationContextProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(DesignWaterLevelLocationsContext), typeof(DesignWaterLevelLocationsContextProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(DesignWaterLevelLocationContext), typeof(DesignWaterLevelLocationContextProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(WaveHeightLocationsContext), typeof(WaveHeightLocationsContextProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(WaveHeightLocationContext), typeof(WaveHeightLocationContextProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(ForeshoreProfile), typeof(ForeshoreProfileProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(ForeshoreProfilesContext), typeof(ForeshoreProfileCollectionProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(SelectedTopLevelSubMechanismIllustrationPoint), typeof(TopLevelSubMechanismIllustrationPointProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(StructuresOutputContext), typeof(StructuresOutputProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(SelectedTopLevelFaultTreeIllustrationPoint), typeof(TopLevelFaultTreeIllustrationPointProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(IllustrationPointContext), typeof(FaultTreeIllustrationPointProperties)); PluginTestHelper.AssertPropertyInfoDefined( propertyInfos, typeof(IllustrationPointContext), typeof(SubMechanismIllustrationPointProperties)); } } [Test] public void GetExportInfos_ReturnsSupportedExportInfos() { // Setup using (var plugin = new RingtoetsPlugin()) { // Call ExportInfo[] exportInfos = plugin.GetExportInfos().ToArray(); // Assert Assert.AreEqual(2, exportInfos.Length); Assert.IsTrue(exportInfos.Any(ei => ei.DataType == typeof(ReferenceLineContext))); Assert.IsTrue(exportInfos.Any(ei => ei.DataType == typeof(HydraulicBoundaryDatabaseContext))); } } [Test] public void GetViewInfos_ReturnsSupportedViewInfos() { // Setup using (var plugin = new RingtoetsPlugin()) { // Call ViewInfo[] viewInfos = plugin.GetViewInfos().ToArray(); // Assert Assert.AreEqual(16, viewInfos.Length); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(FailureMechanismContributionContext), typeof(FailureMechanismContribution), typeof(FailureMechanismContributionView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(DesignWaterLevelLocationsContext), typeof(IEnumerable), typeof(DesignWaterLevelLocationsView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(WaveHeightLocationsContext), typeof(IEnumerable), typeof(WaveHeightLocationsView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(IAssessmentSection), typeof(AssessmentSectionView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(FailureMechanismContext), typeof(FailureMechanismView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(FailureMechanismSectionResultContext), typeof(IEnumerable), typeof(StrengthStabilityLengthwiseConstructionResultView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(FailureMechanismSectionResultContext), typeof(IEnumerable), typeof(WaterPressureAsphaltCoverResultView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(FailureMechanismSectionResultContext), typeof(IEnumerable), typeof(MacrostabilityOutwardsResultView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(FailureMechanismSectionResultContext), typeof(IEnumerable), typeof(GrassCoverSlipOffInwardsResultView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(FailureMechanismSectionResultContext), typeof(IEnumerable), typeof(GrassCoverSlipOffOutwardsResultView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(FailureMechanismSectionResultContext), typeof(IEnumerable), typeof(MicrostabilityResultView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(FailureMechanismSectionResultContext), typeof(IEnumerable), typeof(PipingStructureResultView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(FailureMechanismSectionResultContext), typeof(IEnumerable), typeof(TechnicalInnovationResultView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(Comment), typeof(CommentView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(WaveConditionsInputContext), typeof(ICalculation), typeof(WaveConditionsInputView)); PluginTestHelper.AssertViewInfoDefined( viewInfos, typeof(StructuresOutputContext), typeof(IStructuresCalculation), typeof(GeneralResultFaultTreeIllustrationPointView)); } } [Test] public void GetTreeNodeInfos_ReturnsSupportedTreeNodeInfos() { // Setup using (var plugin = new RingtoetsPlugin()) { // Call TreeNodeInfo[] treeNodeInfos = plugin.GetTreeNodeInfos().ToArray(); // Assert Assert.AreEqual(25, treeNodeInfos.Length); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(AssessmentSection))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(BackgroundData))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(ReferenceLineContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(FailureMechanismContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(CategoryTreeFolder))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(FailureMechanismSectionsContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(FailureMechanismContributionContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(HydraulicBoundaryDatabaseContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(DesignWaterLevelLocationsContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(WaveHeightLocationsContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(ForeshoreProfilesContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(DikeProfile))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(ForeshoreProfile))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(FailureMechanismSectionResultContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(FailureMechanismSectionResultContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(FailureMechanismSectionResultContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(FailureMechanismSectionResultContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(FailureMechanismSectionResultContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(FailureMechanismSectionResultContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(FailureMechanismSectionResultContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(FailureMechanismSectionResultContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(Comment))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(RingtoetsProject))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(WaveConditionsInputContext))); Assert.IsTrue(treeNodeInfos.Any(tni => tni.TagType == typeof(StructuresOutputContext))); } } [Test] public void GetChildDataWithViewDefinitions_AssessmentSection_ReturnFailureMechanismContribution() { // Setup var mocks = new MockRepository(); var assessmentSection = mocks.Stub(); mocks.ReplayAll(); using (var plugin = new RingtoetsPlugin()) { // Call IEnumerable childrenWithViewDefinitions = plugin.GetChildDataWithViewDefinitions(assessmentSection); // Assert CollectionAssert.AreEqual(new object[] { assessmentSection.FailureMechanismContribution }, childrenWithViewDefinitions); } mocks.VerifyAll(); } [Test] public void GetChildDataWithViewDefinitions_RingtoetsProjectWithChildren_ReturnChildren() { // Setup var project = new RingtoetsProject(); project.AssessmentSections.Add(new AssessmentSection(AssessmentSectionComposition.Dike)); project.AssessmentSections.Add(new AssessmentSection(AssessmentSectionComposition.Dike)); project.AssessmentSections.Add(new AssessmentSection(AssessmentSectionComposition.Dike)); using (var plugin = new RingtoetsPlugin()) { // Call IEnumerable childrenWithViewDefinitions = plugin.GetChildDataWithViewDefinitions(project); // Assert List expectedResult = project.AssessmentSections; CollectionAssert.AreEquivalent(expectedResult, childrenWithViewDefinitions); } } [Test] public void GetChildDataWithViewDefinitions_UnsupportedData_ReturnEmpty() { // Setup using (var plugin = new RingtoetsPlugin()) { // Call IEnumerable childrenWithViewDefinitions = plugin.GetChildDataWithViewDefinitions(1); // Assert CollectionAssert.IsEmpty(childrenWithViewDefinitions); } } [Test] public void GetImportInfos_ReturnsSupportedImportInfos() { // Setup using (var plugin = new RingtoetsPlugin()) { // Call ImportInfo[] importInfos = plugin.GetImportInfos().ToArray(); // Assert Assert.AreEqual(3, importInfos.Length); Assert.IsTrue(importInfos.Any(i => i.DataType == typeof(ReferenceLineContext))); Assert.IsTrue(importInfos.Any(i => i.DataType == typeof(FailureMechanismSectionsContext))); Assert.IsTrue(importInfos.Any(i => i.DataType == typeof(ForeshoreProfilesContext))); } } [Test] public void GetImportInfos_ReturnsSupportedUpdateInfos() { // Setup using (var plugin = new RingtoetsPlugin()) { // Call UpdateInfo[] updateInfos = plugin.GetUpdateInfos().ToArray(); // Assert Assert.AreEqual(1, updateInfos.Length); Assert.IsTrue(updateInfos.Any(i => i.DataType == typeof(ForeshoreProfilesContext))); } } [Test] public void Activate_WithoutGui_ThrowsInvalidOperationException() { // Setup using (var plugin = new RingtoetsPlugin()) { // Call TestDelegate test = () => plugin.Activate(); // Assert Assert.Throws(test); } } [Test] [Apartment(ApartmentState.STA)] public void Activate_WithGui_ExpectedProperties() { // Setup var mockRepository = new MockRepository(); var mainWindow = mockRepository.StrictMock(); var documentViewController = mockRepository.StrictMock(); var gui = mockRepository.StrictMock(); gui.Expect(g => g.MainWindow).Return(mainWindow).Repeat.AtLeastOnce(); gui.Expect(g => g.DocumentViewController).Return(documentViewController); gui.Expect(g => g.ProjectOpened += null).IgnoreArguments(); gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments(); mockRepository.ReplayAll(); using (var plugin = new RingtoetsPlugin()) { plugin.Gui = gui; // Call plugin.Activate(); // Assert Assert.IsInstanceOf(plugin.RibbonCommandHandler); } mockRepository.VerifyAll(); } } }