// Copyright (C) Stichting Deltares 2018. 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.Linq; using System.Windows.Forms; using Core.Common.Base; using Core.Common.Base.Geometry; using Core.Common.TestUtil; using Core.Common.Util; using Core.Components.Gis.Data; using Core.Components.Gis.Features; using Core.Components.Gis.Forms; using Core.Components.Gis.Geometries; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.AssemblyTool.Data; using Ringtoets.AssemblyTool.Forms; using Ringtoets.AssemblyTool.KernelWrapper.Calculators; using Ringtoets.AssemblyTool.KernelWrapper.TestUtil.Calculators; using Ringtoets.AssemblyTool.KernelWrapper.TestUtil.Calculators.Assembly; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.Data.TestUtil; using Ringtoets.Common.Forms.TestUtil; using Ringtoets.Common.Forms.Views; using Ringtoets.Integration.Data; using Ringtoets.Integration.Data.Assembly; using Ringtoets.Integration.Forms.Views; using Ringtoets.Integration.Util; using Ringtoets.Piping.Data; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; namespace Ringtoets.Integration.Forms.Test.Views { [TestFixture] public class AssemblyResultPerSectionMapViewTest { private const int referenceLineIndex = 0; private const int hydraulicBoundaryLocationsIndex = 1; private const int assemblyResultsIndex = 2; [Test] public void Constructor_AssessmentSectionNull_ThrowsArgumentNullException() { // Call TestDelegate call = () => new AssemblyResultPerSectionMapView(null); // Assert var exception = Assert.Throws(call); Assert.AreEqual("assessmentSection", exception.ParamName); } [Test] public void Constructor_WithAssessmentSectionWithoutData_ExpectedValues() { // Setup AssessmentSection assessmentSection = CreateAssessmentSection(); // Call using (var view = new AssemblyResultPerSectionMapView(assessmentSection)) { // Assert Assert.IsInstanceOf(view); Assert.IsInstanceOf(view); Assert.IsNull(view.Data); Assert.AreEqual(1, view.Controls.Count); Assert.IsInstanceOf(view.Controls[0]); var layoutPanel = (TableLayoutPanel) view.Controls[0]; Assert.AreEqual(1, layoutPanel.ColumnCount); Assert.AreEqual(2, layoutPanel.RowCount); Assert.AreEqual(DockStyle.Fill, layoutPanel.Dock); var warningPanel = (Panel) layoutPanel.GetControlFromPosition(0, 0); Assert.AreEqual(DockStyle.Fill, warningPanel.Dock); Assert.IsFalse(warningPanel.Visible); var mapControl = (RingtoetsMapControl) layoutPanel.GetControlFromPosition(0, 1); Assert.AreSame(view.Map, mapControl.MapControl); Assert.AreEqual(DockStyle.Fill, ((Control) view.Map).Dock); Assert.AreSame(assessmentSection, view.AssessmentSection); MapDataCollection mapDataCollection = view.Map.Data; Assert.AreEqual("Assemblagekaart", mapDataCollection.Name); List mapDataList = mapDataCollection.Collection.ToList(); Assert.AreEqual(3, mapDataList.Count); var assemblyResultsLineMapData = (MapLineData) mapDataList[assemblyResultsIndex]; var hydraulicBoundaryLocationsMapData = (MapPointData) mapDataList[hydraulicBoundaryLocationsIndex]; var referenceLineMapData = (MapLineData) mapDataList[referenceLineIndex]; CollectionAssert.IsEmpty(assemblyResultsLineMapData.Features); CollectionAssert.IsEmpty(hydraulicBoundaryLocationsMapData.Features); CollectionAssert.IsEmpty(referenceLineMapData.Features); Assert.AreEqual("Gecombineerd vakoordeel", assemblyResultsLineMapData.Name); Assert.AreEqual("Hydraulische belastingen", hydraulicBoundaryLocationsMapData.Name); Assert.AreEqual("Referentielijn", referenceLineMapData.Name); } } [Test] public void Constructor_AssessmentSectionWithBackgroundData_BackgroundDataSet() { // Setup AssessmentSection assessmentSection = CreateAssessmentSection(); // Call using (var view = new AssemblyResultPerSectionMapView(assessmentSection)) { // Assert MapDataTestHelper.AssertImageBasedMapData(assessmentSection.BackgroundData, view.Map.BackgroundMapData); } } [Test] public void Constructor_WithAllData_DataUpdatedToCollectionOfFilledMapData() { // Setup var random = new Random(21); var referenceLine = new ReferenceLine(); referenceLine.SetGeometry(new[] { new Point2D(1.0, 2.0), new Point2D(2.0, 1.0) }); AssessmentSection assessmentSection = CreateAssessmentSection(); assessmentSection.ReferenceLine = referenceLine; assessmentSection.SetHydraulicBoundaryLocationCalculations(new[] { new HydraulicBoundaryLocation(1, "test", 1.0, 2.0) }); // Call using (new AssemblyToolCalculatorFactoryConfig()) { var calculatorFactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance; AssessmentSectionAssemblyCalculatorStub calculator = calculatorFactory.LastCreatedAssessmentSectionAssemblyCalculator; CombinedFailureMechanismSectionAssembly[] failureMechanismSectionAssembly = { CreateCombinedFailureMechanismSectionAssembly(assessmentSection, random.NextEnumValue()) }; calculator.CombinedFailureMechanismSectionAssemblyOutput = failureMechanismSectionAssembly; using (var view = new AssemblyResultPerSectionMapView(assessmentSection)) { // Assert Assert.IsInstanceOf(view.Map.Data); MapDataCollection mapData = view.Map.Data; Assert.IsNotNull(mapData); Assert.AreEqual(3, mapData.Collection.Count()); IEnumerable expectedResults = AssessmentSectionAssemblyFactory.AssembleCombinedPerFailureMechanismSection(assessmentSection, true); AssertCombinedFailureMechanismSectionAssemblyResultMapData(expectedResults, assessmentSection.ReferenceLine, mapData.Collection.ElementAt(assemblyResultsIndex)); MapData hydraulicBoundaryLocationsMapData = mapData.Collection.ElementAt(hydraulicBoundaryLocationsIndex); MapDataTestHelper.AssertHydraulicBoundaryLocationsMapData(assessmentSection, hydraulicBoundaryLocationsMapData); MapData referenceLineMapData = mapData.Collection.ElementAt(referenceLineIndex); AssertReferenceLineMapData(assessmentSection.ReferenceLine, referenceLineMapData); } } } [Test] [TestCaseSource(typeof(MapViewTestHelper), nameof(MapViewTestHelper.GetCalculationFuncs))] public void GivenViewWithHydraulicBoundaryLocationsData_WhenHydraulicBoundaryLocationCalculationUpdatedAndNotified_ThenMapDataUpdated( Func getCalculationFunc) { // Given var hydraulicBoundaryLocation = new HydraulicBoundaryLocation(1, "test1", 1.0, 2.0); AssessmentSection assessmentSection = CreateAssessmentSectionWithReferenceLine(); assessmentSection.SetHydraulicBoundaryLocationCalculations(new[] { hydraulicBoundaryLocation }); using (var view = new AssemblyResultPerSectionMapView(assessmentSection)) { IMapControl map = GetMapControl(view); var mocks = new MockRepository(); IObserver[] observers = AttachMapDataObservers(mocks, map.Data.Collection); observers[hydraulicBoundaryLocationsIndex].Expect(obs => obs.UpdateObserver()); mocks.ReplayAll(); MapData hydraulicBoundaryLocationsMapData = map.Data.Collection.ElementAt(hydraulicBoundaryLocationsIndex); // Precondition MapDataTestHelper.AssertHydraulicBoundaryLocationsMapData(assessmentSection, hydraulicBoundaryLocationsMapData); // When HydraulicBoundaryLocationCalculation calculation = getCalculationFunc(assessmentSection); calculation.Output = new TestHydraulicBoundaryLocationCalculationOutput(new Random(21).NextDouble()); calculation.NotifyObservers(); // Then MapDataTestHelper.AssertHydraulicBoundaryLocationsMapData(assessmentSection, hydraulicBoundaryLocationsMapData); mocks.VerifyAll(); } } [Test] public void GivenViewWithHydraulicBoundaryLocationsDatabase_WhenChangingHydraulicBoundaryLocationsDataAndObserversNotified_ThenMapDataUpdated() { // Given AssessmentSection assessmentSection = CreateAssessmentSectionWithReferenceLine(); assessmentSection.SetHydraulicBoundaryLocationCalculations(new[] { new HydraulicBoundaryLocation(1, "test1", 1.0, 2.0) }); using (var view = new AssemblyResultPerSectionMapView(assessmentSection)) { IMapControl map = GetMapControl(view); var mocks = new MockRepository(); IObserver[] observers = AttachMapDataObservers(mocks, map.Data.Collection); observers[hydraulicBoundaryLocationsIndex].Expect(obs => obs.UpdateObserver()); mocks.ReplayAll(); MapData hydraulicBoundaryLocationsMapData = map.Data.Collection.ElementAt(hydraulicBoundaryLocationsIndex); // Precondition MapDataTestHelper.AssertHydraulicBoundaryLocationsMapData(assessmentSection, hydraulicBoundaryLocationsMapData); // When assessmentSection.SetHydraulicBoundaryLocationCalculations(new[] { new HydraulicBoundaryLocation(2, "test2", 2.0, 3.0) }); assessmentSection.HydraulicBoundaryDatabase.Locations.NotifyObservers(); // Then MapDataTestHelper.AssertHydraulicBoundaryLocationsMapData(assessmentSection, hydraulicBoundaryLocationsMapData); mocks.VerifyAll(); } } [Test] public void UpdateObserver_ReferenceLineUpdated_MapDataUpdated() { // Setup var referenceLine = new ReferenceLine(); referenceLine.SetGeometry(new[] { new Point2D(1.0, 2.0), new Point2D(2.0, 1.0) }); var assessmentSection = new AssessmentSection(new Random(21).NextEnumValue()); assessmentSection.ReferenceLine = referenceLine; using (var view = new AssemblyResultPerSectionMapView(assessmentSection)) { IMapControl map = GetMapControl(view); var mocks = new MockRepository(); IObserver[] observers = AttachMapDataObservers(mocks, map.Data.Collection); observers[assemblyResultsIndex].Expect(obs => obs.UpdateObserver()); observers[referenceLineIndex].Expect(obs => obs.UpdateObserver()); mocks.ReplayAll(); MapData referenceLineMapData = map.Data.Collection.ElementAt(referenceLineIndex); // Precondition AssertReferenceLineMapData(assessmentSection.ReferenceLine, referenceLineMapData); // Call assessmentSection.ReferenceLine.SetGeometry(new[] { new Point2D(2.0, 5.0), new Point2D(4.0, 3.0) }); assessmentSection.NotifyObservers(); // Assert AssertReferenceLineMapData(assessmentSection.ReferenceLine, referenceLineMapData); mocks.VerifyAll(); } } [Test] public void GivenViewWithAssemblyResults_WhenFailureMechanismNotifiesObserver_ThenAssemblyResultsRefreshed() { // Given var random = new Random(21); AssessmentSection assessmentSection = CreateAssessmentSectionWithReferenceLine(); using (new AssemblyToolCalculatorFactoryConfig()) { var calculatorFactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance; AssessmentSectionAssemblyCalculatorStub calculator = calculatorFactory.LastCreatedAssessmentSectionAssemblyCalculator; calculator.CombinedFailureMechanismSectionAssemblyOutput = new[] { CreateCombinedFailureMechanismSectionAssembly(assessmentSection, FailureMechanismSectionAssemblyCategoryGroup.IIIv) }; using (var view = new AssemblyResultPerSectionMapView(assessmentSection)) { // Precondition MapDataCollection mapData = view.Map.Data; var mocks = new MockRepository(); IObserver[] observers = AttachMapDataObservers(mocks, mapData.Collection); observers[assemblyResultsIndex].Expect(obs => obs.UpdateObserver()); mocks.ReplayAll(); IEnumerable expectedResults = AssessmentSectionAssemblyFactory.AssembleCombinedPerFailureMechanismSection(assessmentSection, true); AssertCombinedFailureMechanismSectionAssemblyResultMapData(expectedResults, assessmentSection.ReferenceLine, mapData.Collection.ElementAt(assemblyResultsIndex)); // When calculator.CombinedFailureMechanismSectionAssemblyOutput = new[] { CreateCombinedFailureMechanismSectionAssembly(assessmentSection, FailureMechanismSectionAssemblyCategoryGroup.Iv) }; IEnumerable failureMechanisms = assessmentSection.GetFailureMechanisms(); failureMechanisms.ElementAt(random.Next(failureMechanisms.Count())).NotifyObservers(); // Then expectedResults = AssessmentSectionAssemblyFactory.AssembleCombinedPerFailureMechanismSection(assessmentSection, true); AssertCombinedFailureMechanismSectionAssemblyResultMapData(expectedResults, assessmentSection.ReferenceLine, mapData.Collection.ElementAt(assemblyResultsIndex)); mocks.VerifyAll(); } } } [Test] public void GivenViewWithAssemblyResults_WhenAssessmentSectionNotifiesObserver_ThenAssemblyResultsRefreshed() { // Given AssessmentSection assessmentSection = CreateAssessmentSectionWithReferenceLine(); using (new AssemblyToolCalculatorFactoryConfig()) { var calculatorFactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance; AssessmentSectionAssemblyCalculatorStub calculator = calculatorFactory.LastCreatedAssessmentSectionAssemblyCalculator; calculator.CombinedFailureMechanismSectionAssemblyOutput = new[] { CreateCombinedFailureMechanismSectionAssembly(assessmentSection, FailureMechanismSectionAssemblyCategoryGroup.IIIv) }; using (var view = new AssemblyResultPerSectionMapView(assessmentSection)) { // Precondition MapDataCollection mapData = view.Map.Data; var mocks = new MockRepository(); IObserver[] observers = AttachMapDataObservers(mocks, mapData.Collection); observers[assemblyResultsIndex].Expect(obs => obs.UpdateObserver()); observers[referenceLineIndex].Expect(obs => obs.UpdateObserver()); mocks.ReplayAll(); IEnumerable expectedResults = AssessmentSectionAssemblyFactory.AssembleCombinedPerFailureMechanismSection(assessmentSection, true); AssertCombinedFailureMechanismSectionAssemblyResultMapData(expectedResults, assessmentSection.ReferenceLine, mapData.Collection.ElementAt(assemblyResultsIndex)); // When calculator.CombinedFailureMechanismSectionAssemblyOutput = new[] { CreateCombinedFailureMechanismSectionAssembly(assessmentSection, FailureMechanismSectionAssemblyCategoryGroup.Iv) }; assessmentSection.NotifyObservers(); // Then expectedResults = AssessmentSectionAssemblyFactory.AssembleCombinedPerFailureMechanismSection(assessmentSection, true); AssertCombinedFailureMechanismSectionAssemblyResultMapData(expectedResults, assessmentSection.ReferenceLine, mapData.Collection.ElementAt(assemblyResultsIndex)); mocks.VerifyAll(); } } } [Test] [TestCase(true)] [TestCase(false)] public void GivenViewWithManualAssemblyResults_WhenShowingView_ThenExpectedWarningSet(bool hasManualAssembly) { // Given AssessmentSection assessmentSection = CreateAssessmentSectionWithReferenceLine(); PipingFailureMechanism failureMechanism = assessmentSection.Piping; FailureMechanismTestHelper.AddSections(failureMechanism, 1); failureMechanism.SectionResults.Single().UseManualAssembly = hasManualAssembly; // When using (var view = new AssemblyResultPerSectionMapView(assessmentSection)) { // Then var layoutPanel = (TableLayoutPanel) view.Controls[0]; var warningPanel = (Panel) layoutPanel.GetControlFromPosition(0, 0); Assert.AreEqual(hasManualAssembly, warningPanel.Visible); AssertWarningPanel(warningPanel); } } [Test] public void GivenViewWithoutManualAssemblyResults_WhenAssessmentSectionWithManualAssemblyResultsAndNotifiesObservers_ThenExpectedWarningSet() { // Given AssessmentSection assessmentSection = CreateAssessmentSectionWithReferenceLine(); using (var view = new AssemblyResultPerSectionMapView(assessmentSection)) { // Precondition var layoutPanel = (TableLayoutPanel) view.Controls[0]; var warningPanel = (Panel) layoutPanel.GetControlFromPosition(0, 0); Assert.IsFalse(warningPanel.Visible); // When PipingFailureMechanism failureMechanism = assessmentSection.Piping; FailureMechanismTestHelper.AddSections(failureMechanism, 1); failureMechanism.SectionResults.Single().UseManualAssembly = true; failureMechanism.NotifyObservers(); // Then Assert.IsTrue(warningPanel.Visible); AssertWarningPanel(warningPanel); } } [Test] public void GivenViewWithManualAssemblyResults_WhenAssessmentSectionWithoutManualAssemblyResultsAndNotifiesObservers_ThenExpectedWarningSet() { // Given AssessmentSection assessmentSection = CreateAssessmentSectionWithReferenceLine(); PipingFailureMechanism failureMechanism = assessmentSection.Piping; FailureMechanismTestHelper.AddSections(failureMechanism, 1); failureMechanism.SectionResults.Single().UseManualAssembly = true; using (var view = new AssemblyResultPerSectionMapView(assessmentSection)) { // Precondition var layoutPanel = (TableLayoutPanel) view.Controls[0]; var warningPanel = (Panel) layoutPanel.GetControlFromPosition(0, 0); Assert.IsTrue(warningPanel.Visible); AssertWarningPanel(warningPanel); // When failureMechanism.SectionResults.Single().UseManualAssembly = false; failureMechanism.NotifyObservers(); // Then Assert.IsFalse(warningPanel.Visible); } } private static AssessmentSection CreateAssessmentSection() { var random = new Random(21); return new AssessmentSection(random.NextEnumValue()); } private static AssessmentSection CreateAssessmentSectionWithReferenceLine() { var referenceLine = new ReferenceLine(); referenceLine.SetGeometry(new[] { new Point2D(1.0, 2.0), new Point2D(2.0, 1.0) }); AssessmentSection assessmentSection = CreateAssessmentSection(); assessmentSection.ReferenceLine = referenceLine; return assessmentSection; } private static CombinedFailureMechanismSectionAssembly CreateCombinedFailureMechanismSectionAssembly(AssessmentSection assessmentSection, FailureMechanismSectionAssemblyCategoryGroup totalResult) { var random = new Random(37); return new CombinedFailureMechanismSectionAssembly(new CombinedAssemblyFailureMechanismSection(random.NextDouble(), random.NextDouble(), totalResult), assessmentSection.GetFailureMechanisms() .Where(fm => fm.IsRelevant) .Select(fm => random.NextEnumValue()).ToArray()); } private static void AssertReferenceLineMapData(ReferenceLine referenceLine, MapData referenceLineMapData) { MapDataTestHelper.AssertReferenceLineMapData(referenceLine, referenceLineMapData); Assert.IsTrue(referenceLineMapData.IsVisible); } private static void AssertCombinedFailureMechanismSectionAssemblyResultMapData(IEnumerable expectedAssemblyResults, ReferenceLine referenceLine, MapData mapData) { Assert.IsInstanceOf(mapData); var assemblyResultMapData = (MapLineData) mapData; int expectedNrOfResults = expectedAssemblyResults.Count(); IEnumerable mapFeatures = assemblyResultMapData.Features; Assert.AreEqual(expectedNrOfResults, mapFeatures.Count()); for (var i = 0; i < expectedNrOfResults; i++) { CombinedFailureMechanismSectionAssemblyResult expectedAssemblyResult = expectedAssemblyResults.ElementAt(i); MapFeature actualFeature = mapFeatures.ElementAt(i); MapGeometry mapGeometry = actualFeature.MapGeometries.Single(); AssertEqualPointCollections(referenceLine, expectedAssemblyResult, mapGeometry); Assert.AreEqual(2, actualFeature.MetaData.Keys.Count); Assert.AreEqual(expectedAssemblyResult.SectionNumber, actualFeature.MetaData["Vaknummer"]); Assert.AreEqual(new EnumDisplayWrapper( DisplayFailureMechanismSectionAssemblyCategoryGroupConverter.Convert(expectedAssemblyResult.TotalResult)).DisplayName, mapFeatures.ElementAt(i).MetaData["Categorie"]); } } private static void AssertWarningPanel(Panel warningPanel) { Assert.AreEqual(2, warningPanel.Controls.Count); var warningIcon = (PictureBox) warningPanel.Controls.Find("warningIcon", false).Single(); TestHelper.AssertImagesAreEqual(RingtoetsCommonFormsResources.PencilWarning.ToBitmap(), warningIcon.Image); Assert.AreEqual(DockStyle.Left, warningIcon.Dock); Assert.AreEqual(16, warningIcon.MaximumSize.Height); Assert.AreEqual(16, warningIcon.MaximumSize.Width); Assert.AreEqual(PictureBoxSizeMode.StretchImage, warningIcon.SizeMode); var warningText = (Label) warningPanel.Controls.Find("warningText", false).Single(); Assert.AreEqual("Toetsoordeel is (deels) gebaseerd op handmatig overschreven toetsoordelen.", warningText.Text); } private static IMapControl GetMapControl(AssemblyResultPerSectionMapView view) { var layoutPanel = (TableLayoutPanel) view.Controls[0]; var ringtoetsMapControl = (RingtoetsMapControl) layoutPanel.GetControlFromPosition(0, 1); return ringtoetsMapControl.MapControl; } private static void AssertEqualPointCollections(ReferenceLine referenceLine, CombinedFailureMechanismSectionAssemblyResult sectionAssemblyResult, MapGeometry geometry) { IEnumerable expectedGeometry = FailureMechanismSectionHelper.GetFailureMechanismSectionGeometry( referenceLine, sectionAssemblyResult.SectionStart, sectionAssemblyResult.SectionEnd).ToArray(); CollectionAssert.IsNotEmpty(expectedGeometry); CollectionAssert.AreEqual(expectedGeometry, geometry.PointCollections.Single()); } /// /// Attaches mocked observers to all map data components. /// /// The . /// The map data collection containing the /// elements. /// An array of mocked observers attached to the data in . private static IObserver[] AttachMapDataObservers(MockRepository mocks, IEnumerable mapData) { MapData[] mapDataArray = mapData.ToArray(); var referenceLineMapDataObserver = mocks.StrictMock(); mapDataArray[referenceLineIndex].Attach(referenceLineMapDataObserver); var hydraulicBoundaryLocationsMapDataObserver = mocks.StrictMock(); mapDataArray[hydraulicBoundaryLocationsIndex].Attach(hydraulicBoundaryLocationsMapDataObserver); var assemblyResultsObserver = mocks.StrictMock(); mapDataArray[assemblyResultsIndex].Attach(assemblyResultsObserver); return new[] { referenceLineMapDataObserver, hydraulicBoundaryLocationsMapDataObserver, assemblyResultsObserver }; } } }