// 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.Linq; using Core.Common.Base; using Core.Common.Gui.Commands; using Core.Common.TestUtil; using NUnit.Extensions.Forms; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.ClosingStructures.Data; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.DuneErosion.Data; using Ringtoets.GrassCoverErosionInwards.Data; using Ringtoets.GrassCoverErosionOutwards.Data; using Ringtoets.HeightStructures.Data; using Ringtoets.Integration.Data; using Ringtoets.Integration.Forms.PropertyClasses; using Ringtoets.Integration.Plugin.Handlers; using Ringtoets.Integration.TestUtils; using Ringtoets.Piping.Data; using Ringtoets.StabilityPointStructures.Data; using Ringtoets.StabilityStoneCover.Data; using Ringtoets.WaveImpactAsphaltCover.Data; namespace Ringtoets.Integration.Plugin.Test.Handlers { [TestFixture] public class AssessmentSectionCompositionChangeHandlerTest : NUnitFormTest { [Test] public void Constructor_ViewCommandsNull_ThrowsArgumentNullException() { // Call TestDelegate test = () => new AssessmentSectionCompositionChangeHandler(null); // Assert var exception = Assert.Throws(test); Assert.AreEqual("viewCommands", exception.ParamName); } [Test] public void Constructor_WithViewCommands_ExpectedValues() { // Setup var mocks = new MockRepository(); var viewCommands = mocks.Stub(); mocks.ReplayAll(); // Call var handler = new AssessmentSectionCompositionChangeHandler(viewCommands); // Assert Assert.IsInstanceOf(handler); mocks.VerifyAll(); } [Test] public void ConfirmCompositionChange_Always_ShowMessageBox() { // Setup var mocks = new MockRepository(); var viewCommands = mocks.Stub(); mocks.ReplayAll(); var title = ""; var message = ""; DialogBoxHandler = (name, wnd) => { var tester = new MessageBoxTester(wnd); title = tester.Title; message = tester.Text; tester.ClickOk(); }; var handler = new AssessmentSectionCompositionChangeHandler(viewCommands); // Call handler.ConfirmCompositionChange(); // Assert Assert.AreEqual("Bevestigen", title); string expectedMessage = "Als u het trajecttype aanpast, dan worden alle rekenresultaten van alle relevante toetssporen verwijderd." + Environment.NewLine + Environment.NewLine + "Weet u zeker dat u wilt doorgaan?"; Assert.AreEqual(expectedMessage, message); mocks.VerifyAll(); } [Test] public void ConfirmCompositionChange_MessageBoxOk_ReturnTrue() { // Setup var mocks = new MockRepository(); var viewCommands = mocks.Stub(); mocks.ReplayAll(); DialogBoxHandler = (name, wnd) => { var tester = new MessageBoxTester(wnd); tester.ClickOk(); }; var handler = new AssessmentSectionCompositionChangeHandler(viewCommands); // Call bool result = handler.ConfirmCompositionChange(); // Assert Assert.IsTrue(result); mocks.VerifyAll(); } [Test] public void ConfirmCompositionChange_MessageBoxCancel_ReturnFalse() { // Setup var mocks = new MockRepository(); var viewCommands = mocks.Stub(); mocks.ReplayAll(); DialogBoxHandler = (name, wnd) => { var tester = new MessageBoxTester(wnd); tester.ClickCancel(); }; var handler = new AssessmentSectionCompositionChangeHandler(viewCommands); // Call bool result = handler.ConfirmCompositionChange(); // Assert Assert.IsFalse(result); mocks.VerifyAll(); } [Test] public void ChangeComposition_AssessmentSectionNull_ThrowArgumentNullException() { // Setup var mocks = new MockRepository(); var viewCommands = mocks.Stub(); mocks.ReplayAll(); var handler = new AssessmentSectionCompositionChangeHandler(viewCommands); // Call TestDelegate call = () => handler.ChangeComposition(null, AssessmentSectionComposition.Dike); // Assert string paramName = Assert.Throws(call).ParamName; Assert.AreEqual("assessmentSection", paramName); mocks.VerifyAll(); } [Test] public void ChangeComposition_ChangeToSameValue_DoNothing() { // Setup var mocks = new MockRepository(); var viewCommands = mocks.Stub(); mocks.ReplayAll(); AssessmentSection assessmentSection = TestDataGenerator.GetAssessmentSectionWithAllCalculationConfigurations(); AssessmentSectionComposition originalComposition = assessmentSection.Composition; ICalculation[] calculationsWithOutput = assessmentSection.GetFailureMechanisms() .SelectMany(fm => fm.Calculations) .Where(c => c.HasOutput) .ToArray(); DuneLocation[] duneLocationWithOutput = assessmentSection.GetFailureMechanisms().OfType() .SelectMany(f => f.DuneLocations) .Where(loc => loc.Output != null) .ToArray(); HydraulicBoundaryLocation[] hblWithOutput = assessmentSection.GetFailureMechanisms().OfType() .SelectMany(f => f.HydraulicBoundaryLocations) .Where(loc => loc.DesignWaterLevelCalculation.HasOutput || loc.WaveHeightCalculation.HasOutput) .ToArray(); var handler = new AssessmentSectionCompositionChangeHandler(viewCommands); // Call IEnumerable affectedObjects = handler.ChangeComposition(assessmentSection, originalComposition); // Assert Assert.True(calculationsWithOutput.All(c => c.HasOutput), "All calculations that had output still have them."); Assert.True(duneLocationWithOutput.All(loc => loc.Output != null)); Assert.True(hblWithOutput.All(loc => loc.DesignWaterLevelCalculation.HasOutput || loc.WaveHeightCalculation.HasOutput)); CollectionAssert.IsEmpty(affectedObjects); mocks.VerifyAll(); } [Test] [TestCase(AssessmentSectionComposition.Dike, AssessmentSectionComposition.Dune)] [TestCase(AssessmentSectionComposition.DikeAndDune, AssessmentSectionComposition.Dune)] public void ChangeComposition_ChangeToDuneComposition_ChangeCompositionAndClearAllCalculationOutputAndReturnsAllAffectedObjects(AssessmentSectionComposition oldComposition, AssessmentSectionComposition newComposition) { // Setup var mocks = new MockRepository(); var viewCommands = mocks.Stub(); mocks.ReplayAll(); AssessmentSection assessmentSection = TestDataGenerator.GetAssessmentSectionWithAllCalculationConfigurations(oldComposition); GrassCoverErosionOutwardsFailureMechanism grassCoverErosionOutwardsFailureMechanism = assessmentSection.GetFailureMechanisms() .OfType() .Single(); DuneErosionFailureMechanism duneErosionFailureMechanism = assessmentSection.GetFailureMechanisms() .OfType() .Single(); IEnumerable unaffectedObjects = GetDuneIrrelevantFailureMechanisms(assessmentSection) .SelectMany(fm => fm.Calculations) .Where(calc => calc.HasOutput) .ToList(); IEnumerable expectedAffectedObjects = duneErosionFailureMechanism.DuneLocations.Where(dl => dl.Output != null).Cast() .Concat(grassCoverErosionOutwardsFailureMechanism.HydraulicBoundaryLocations) .Concat(new IObservable[] { grassCoverErosionOutwardsFailureMechanism.HydraulicBoundaryLocations, duneErosionFailureMechanism.DuneLocations, assessmentSection }) .Concat(GetDuneRelevantFailureMechanisms(assessmentSection) .SelectMany(fm => fm.Calculations) .Where(calc => calc.HasOutput)) .ToList(); var handler = new AssessmentSectionCompositionChangeHandler(viewCommands); // Call IEnumerable affectedObjects = null; Action call = () => affectedObjects = handler.ChangeComposition(assessmentSection, newComposition); // Assert string[] expectedMessage = { "De resultaten van 24 berekeningen zijn verwijderd.", "Alle berekende hydraulische randvoorwaarden van de relevante toetssporen zijn verwijderd." }; TestHelper.AssertLogMessagesAreGenerated(call, expectedMessage, 2); Assert.AreEqual(newComposition, assessmentSection.Composition); AssertCorrectOutputClearedWhenCompositionDune(unaffectedObjects, assessmentSection); CollectionAssert.IsSubsetOf(expectedAffectedObjects, affectedObjects); foreach (HydraulicBoundaryLocation location in assessmentSection.GrassCoverErosionOutwards.HydraulicBoundaryLocations) { Assert.IsFalse(location.DesignWaterLevelCalculation.HasOutput); Assert.IsFalse(location.WaveHeightCalculation.HasOutput); } foreach (DuneLocation duneLocation in assessmentSection.DuneErosion.DuneLocations) { Assert.IsNull(duneLocation.Output); } mocks.VerifyAll(); } [Test] [TestCase(AssessmentSectionComposition.Dike, AssessmentSectionComposition.DikeAndDune, TestName = "ChangeComposition_ChangeToNonDuneComposition_ChangeCompositionClearRelevantOutputReturnsAllAffectedObjects(Dike,DikeDune)")] [TestCase(AssessmentSectionComposition.DikeAndDune, AssessmentSectionComposition.Dike, TestName = "ChangeComposition_ChangeToNonDuneComposition_ChangeCompositionClearRelevantOutputReturnsAllAffectedObjects(DikeDune,Dike)")] [TestCase(AssessmentSectionComposition.Dune, AssessmentSectionComposition.Dike, TestName = "ChangeComposition_ChangeToNonDuneComposition_ChangeCompositionClearRelevantOutputReturnsAllAffectedObjects(Dune,Dike)")] [TestCase(AssessmentSectionComposition.Dune, AssessmentSectionComposition.DikeAndDune, TestName = "ChangeComposition_ChangeToNonDuneComposition_ChangeCompositionClearRelevantOutputReturnsAllAffectedObjects(Dune,DikeDune)")] public void ChangeComposition_ChangeToNonDuneComposition_ChangeCompositionAndClearAllLocationOutputAndReturnsAllAffectedObjects(AssessmentSectionComposition oldComposition, AssessmentSectionComposition newComposition) { // Setup var mocks = new MockRepository(); var viewCommands = mocks.Stub(); mocks.ReplayAll(); AssessmentSection assessmentSection = TestDataGenerator.GetAssessmentSectionWithAllCalculationConfigurations(oldComposition); DuneErosionFailureMechanism duneErosionFailureMechanism = assessmentSection.GetFailureMechanisms() .OfType() .Single(); IEnumerable expectedUnaffectedObjects = assessmentSection.GetFailureMechanisms() .SelectMany(fm => fm.Calculations) .Where(calc => calc.HasOutput) .ToList(); IEnumerable expectedAffectedObjects = duneErosionFailureMechanism.DuneLocations.Where(dl => dl.Output != null) .Concat(new IObservable[] { assessmentSection, duneErosionFailureMechanism.DuneLocations }) .ToList(); var handler = new AssessmentSectionCompositionChangeHandler(viewCommands); // Call IEnumerable affectedObjects = null; Action call = () => affectedObjects = handler.ChangeComposition(assessmentSection, newComposition); // Assert const string expectedMessage = "Alle berekende hydraulische randvoorwaarden van de relevante toetssporen zijn verwijderd."; TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); Assert.AreEqual(newComposition, assessmentSection.Composition); CollectionAssert.IsSubsetOf(expectedAffectedObjects, affectedObjects); AssertOutputNotCleared(expectedUnaffectedObjects, assessmentSection.GetFailureMechanisms()); foreach (HydraulicBoundaryLocation location in assessmentSection.GrassCoverErosionOutwards.HydraulicBoundaryLocations) { Assert.IsTrue(location.DesignWaterLevelCalculation.HasOutput); Assert.IsTrue(location.WaveHeightCalculation.HasOutput); } foreach (DuneLocation duneLocation in assessmentSection.DuneErosion.DuneLocations) { Assert.IsNull(duneLocation.Output); } mocks.VerifyAll(); } [Test] [TestCase(AssessmentSectionComposition.Dike, AssessmentSectionComposition.Dune)] [TestCase(AssessmentSectionComposition.DikeAndDune, AssessmentSectionComposition.Dune)] public void ChangeComposition_ChangeToDuneAndNoCalculationsWithOutput_ChangeCompositionAndReturnsAllAffectedObjects(AssessmentSectionComposition oldComposition, AssessmentSectionComposition newComposition) { // Setup var mocks = new MockRepository(); var viewCommands = mocks.Stub(); mocks.ReplayAll(); AssessmentSection assessmentSection = TestDataGenerator.GetAssessmentSectionWithAllCalculationConfigurationsWithoutCalculationOutput(oldComposition); GrassCoverErosionOutwardsFailureMechanism grassCoverErosionOutwardsFailureMechanism = assessmentSection.GetFailureMechanisms() .OfType() .Single(); DuneErosionFailureMechanism duneErosionFailureMechanism = assessmentSection.GetFailureMechanisms() .OfType() .Single(); IEnumerable expectedAffectedObjects = duneErosionFailureMechanism.DuneLocations.Where(dl => dl.Output != null).Cast() .Concat(grassCoverErosionOutwardsFailureMechanism.HydraulicBoundaryLocations) .Concat(new IObservable[] { grassCoverErosionOutwardsFailureMechanism.HydraulicBoundaryLocations, duneErosionFailureMechanism.DuneLocations, assessmentSection }) .Concat(GetDuneRelevantFailureMechanisms(assessmentSection) .SelectMany(fm => fm.Calculations) .Where(calc => calc.HasOutput)) .ToList(); var handler = new AssessmentSectionCompositionChangeHandler(viewCommands); // Call IEnumerable affectedObjects = null; Action call = () => affectedObjects = handler.ChangeComposition(assessmentSection, newComposition); // Assert TestHelper.AssertLogMessageIsGenerated(call, "Alle berekende hydraulische randvoorwaarden van de relevante toetssporen zijn verwijderd.", 1); Assert.AreEqual(newComposition, assessmentSection.Composition); Assert.True(assessmentSection.GetFailureMechanisms().SelectMany(fm => fm.Calculations).All(c => !c.HasOutput)); CollectionAssert.IsSubsetOf(expectedAffectedObjects, affectedObjects); foreach (HydraulicBoundaryLocation location in assessmentSection.GrassCoverErosionOutwards.HydraulicBoundaryLocations) { Assert.IsFalse(location.DesignWaterLevelCalculation.HasOutput); Assert.IsFalse(location.WaveHeightCalculation.HasOutput); } foreach (DuneLocation duneLocation in assessmentSection.DuneErosion.DuneLocations) { Assert.IsNull(duneLocation.Output); } mocks.VerifyAll(); } [Test] [TestCase(AssessmentSectionComposition.Dune, AssessmentSectionComposition.Dike, TestName = "ChangeComposition_ChangeToNonDuneNoLocationsWithOutput_ChangeCompositionReturnsAllAffectedObjects(Dune,Dike)")] [TestCase(AssessmentSectionComposition.Dune, AssessmentSectionComposition.DikeAndDune, TestName = "ChangeComposition_ChangeToNonDuneNoLocationsWithOutput_ChangeCompositionReturnsAllAffectedObjects(Dune,DikeDune)")] public void ChangeComposition_ChangeToNonDuneAndNoCalculationsWithOutput_ChangeCompositionAndReturnsAllAffectedObjects(AssessmentSectionComposition oldComposition, AssessmentSectionComposition newComposition) { // Setup var mocks = new MockRepository(); var viewCommands = mocks.Stub(); mocks.ReplayAll(); AssessmentSection assessmentSection = TestDataGenerator.GetAssessmentSectionWithAllCalculationConfigurationsWithoutCalculationOutput(oldComposition); DuneErosionFailureMechanism duneErosionFailureMechanism = assessmentSection.GetFailureMechanisms() .OfType() .Single(); IEnumerable expectedAffectedObjects = duneErosionFailureMechanism.DuneLocations.Where(dl => dl.Output != null) .Concat(new IObservable[] { assessmentSection, duneErosionFailureMechanism.DuneLocations }) .ToList(); var handler = new AssessmentSectionCompositionChangeHandler(viewCommands); // Call IEnumerable affectedObjects = null; Action call = () => affectedObjects = handler.ChangeComposition(assessmentSection, newComposition); // Assert TestHelper.AssertLogMessageIsGenerated(call, "Alle berekende hydraulische randvoorwaarden van de relevante toetssporen zijn verwijderd.", 1); Assert.AreEqual(newComposition, assessmentSection.Composition); Assert.True(assessmentSection.GetFailureMechanisms().SelectMany(fm => fm.Calculations).All(c => !c.HasOutput)); CollectionAssert.IsSubsetOf(expectedAffectedObjects, affectedObjects); foreach (HydraulicBoundaryLocation location in assessmentSection.GrassCoverErosionOutwards.HydraulicBoundaryLocations) { Assert.IsTrue(location.DesignWaterLevelCalculation.HasOutput); Assert.IsTrue(location.WaveHeightCalculation.HasOutput); } foreach (DuneLocation duneLocation in assessmentSection.DuneErosion.DuneLocations) { Assert.IsNull(duneLocation.Output); } mocks.VerifyAll(); } [Test] [TestCase(AssessmentSectionComposition.Dike, AssessmentSectionComposition.Dune, TestName = "ChangeComposition_ChangeToDuneNoLocationsWithOutput_ChangeCompositionReturnsAllAffectedObjects(Dike,Dune)")] [TestCase(AssessmentSectionComposition.DikeAndDune, AssessmentSectionComposition.Dune, TestName = "ChangeComposition_ChangeToDuneNoLocationsWithOutput_ChangeCompositionReturnsAllAffectedObjects(DikeDune,Dune)")] public void ChangeComposition_ChangeToDuneAndNoHydraulicBoudaryLocationsWithOutput_ChangeCompositionAndReturnsAllAffectedObjects(AssessmentSectionComposition oldComposition, AssessmentSectionComposition newComposition) { // Setup var mocks = new MockRepository(); var viewCommands = mocks.Stub(); mocks.ReplayAll(); AssessmentSection assessmentSection = TestDataGenerator.GetAssessmentSectionWithAllCalculationConfigurationsWithoutHydraulicBoundaryLocationAndDuneOutput(oldComposition); IEnumerable notAffectedObjects = GetDuneIrrelevantFailureMechanisms(assessmentSection) .SelectMany(fm => fm.Calculations) .Where(calc => calc.HasOutput) .ToList(); IObservable[] expectedAffectedObjects = new IObservable[] { assessmentSection }.Concat(GetDuneRelevantFailureMechanisms(assessmentSection) .SelectMany(fm => fm.Calculations) .Where(c => c.HasOutput)).ToArray(); var handler = new AssessmentSectionCompositionChangeHandler(viewCommands); // Call IEnumerable affectedObjects = null; Action call = () => affectedObjects = handler.ChangeComposition(assessmentSection, newComposition); // Assert TestHelper.AssertLogMessageIsGenerated(call, "De resultaten van 24 berekeningen zijn verwijderd.", 1); Assert.AreEqual(newComposition, assessmentSection.Composition); // Assert AssertCorrectOutputClearedWhenCompositionDune(notAffectedObjects, assessmentSection); CollectionAssert.IsSubsetOf(expectedAffectedObjects, affectedObjects); mocks.VerifyAll(); } [Test] [TestCase(AssessmentSectionComposition.Dike, AssessmentSectionComposition.DikeAndDune, TestName = "ChangeComposition_ChangeToNonDuneNoLocationsWithOutput_ChangeCompositionReturnsAffectedObjects(Dike,DikeDune)")] [TestCase(AssessmentSectionComposition.DikeAndDune, AssessmentSectionComposition.Dike, TestName = "ChangeComposition_ChangeToNonDuneNoLocationsWithOutput_ChangeCompositionReturnsAffectedObjects(DikeDune,Dike)")] [TestCase(AssessmentSectionComposition.Dune, AssessmentSectionComposition.Dike, TestName = "ChangeComposition_ChangeToNonDuneNoLocationsWithOutput_ChangeCompositionReturnsAffectedObjects(Dune,Dike)")] [TestCase(AssessmentSectionComposition.Dune, AssessmentSectionComposition.DikeAndDune, TestName = "ChangeComposition_ChangeToNonDuneNoLocationsWithOutput_ChangeCompositionReturnsAffectedObjects(Dune,DikeDune)")] public void ChangeComposition_ChangeToNonDuneAndNoHydraulicBoudaryLocationsWithOutput_ChangeCompositionAndReturnsAllAffectedObjects(AssessmentSectionComposition oldComposition, AssessmentSectionComposition newComposition) { // Setup var mocks = new MockRepository(); var viewCommands = mocks.Stub(); mocks.ReplayAll(); AssessmentSection assessmentSection = TestDataGenerator.GetAssessmentSectionWithAllCalculationConfigurationsWithoutHydraulicBoundaryLocationAndDuneOutput(oldComposition); List expectedUnaffectedObjects = assessmentSection.GetFailureMechanisms() .SelectMany(fm => fm.Calculations) .Where(calc => calc.HasOutput) .ToList(); IObservable[] expectedAffectedObjects = { assessmentSection }; var handler = new AssessmentSectionCompositionChangeHandler(viewCommands); // Call IEnumerable affectedObjects = handler.ChangeComposition(assessmentSection, newComposition); // Assert Assert.AreEqual(newComposition, assessmentSection.Composition); AssertOutputNotCleared(expectedUnaffectedObjects, assessmentSection.GetFailureMechanisms()); CollectionAssert.IsSubsetOf(expectedAffectedObjects, affectedObjects); mocks.VerifyAll(); } [Test] [TestCase(AssessmentSectionComposition.Dike, AssessmentSectionComposition.DikeAndDune, TestName = "ChangeComposition_SetNewValue_ChangeRelevancyAndReturnAffectedObjects(Dike, DikeDune)")] [TestCase(AssessmentSectionComposition.Dike, AssessmentSectionComposition.Dune, TestName = "ChangeComposition_SetNewValue_ChangeRelevancyAndReturnAffectedObjects(Dike, Dune)")] [TestCase(AssessmentSectionComposition.DikeAndDune, AssessmentSectionComposition.Dike, TestName = "ChangeComposition_SetNewValue_ChangeRelevancyAndReturnAffectedObjects(DikeDune, Dike)")] [TestCase(AssessmentSectionComposition.DikeAndDune, AssessmentSectionComposition.Dune, TestName = "ChangeComposition_SetNewValue_ChangeRelevancyAndReturnAffectedObjects(DikeDune, Dune)")] [TestCase(AssessmentSectionComposition.Dune, AssessmentSectionComposition.Dike, TestName = "ChangeComposition_SetNewValue_ChangeRelevancyAndReturnAffectedObjects(Dune, Dike)")] [TestCase(AssessmentSectionComposition.Dune, AssessmentSectionComposition.DikeAndDune, TestName = "ChangeComposition_SetNewValue_ChangeRelevancyAndReturnAffectedObjects(Dune, DikeDune)")] public void ChangeComposition_SetNewValue_ChangeRelevancyAndReturnAffectedObjects(AssessmentSectionComposition oldComposition, AssessmentSectionComposition newComposition) { // Setup var mocks = new MockRepository(); var viewCommands = mocks.Stub(); mocks.ReplayAll(); var assessmentSection = new AssessmentSection(oldComposition); var handler = new AssessmentSectionCompositionChangeHandler(viewCommands); // Call IEnumerable affectedObjects = handler.ChangeComposition(assessmentSection, newComposition); // Assert IEnumerable expectedAffectedObjects = GetExpectedAffectedObjects(assessmentSection, oldComposition, newComposition); CollectionAssert.AreEquivalent(expectedAffectedObjects, affectedObjects); mocks.VerifyAll(); } private static IEnumerable GetExpectedAffectedObjects(AssessmentSection assessmentSection, AssessmentSectionComposition oldComposition, AssessmentSectionComposition newComposition) { var expectedAffectedObjects = new List { assessmentSection }; if (newComposition == AssessmentSectionComposition.Dike && oldComposition == AssessmentSectionComposition.Dune || newComposition == AssessmentSectionComposition.Dune && oldComposition == AssessmentSectionComposition.Dike || newComposition == AssessmentSectionComposition.DikeAndDune && oldComposition == AssessmentSectionComposition.Dune || newComposition == AssessmentSectionComposition.Dune && oldComposition == AssessmentSectionComposition.DikeAndDune) { expectedAffectedObjects.AddRange(new IFailureMechanism[] { assessmentSection.PipingFailureMechanism, assessmentSection.GrassCoverErosionInwards, assessmentSection.GrassCoverErosionOutwards, assessmentSection.ClosingStructures, assessmentSection.HeightStructures, assessmentSection.StabilityPointStructures, assessmentSection.StabilityStoneCover, assessmentSection.WaveImpactAsphaltCover, assessmentSection.MacroStabilityInwards }); } if (newComposition == AssessmentSectionComposition.Dike && oldComposition == AssessmentSectionComposition.DikeAndDune || newComposition == AssessmentSectionComposition.DikeAndDune && oldComposition == AssessmentSectionComposition.Dike || newComposition == AssessmentSectionComposition.Dike && oldComposition == AssessmentSectionComposition.Dune || newComposition == AssessmentSectionComposition.Dune && oldComposition == AssessmentSectionComposition.Dike) { expectedAffectedObjects.Add(assessmentSection.DuneErosion); } return expectedAffectedObjects.ToArray(); } /// /// Asserts whether the expected unaffected objects retain their outputs when cleared. /// /// The list of objects that should not have been affected. /// The failure mechanisms to assert. private static void AssertOutputNotCleared(IEnumerable expectedUnaffectedObjects, IEnumerable failureMechanisms) { CollectionAssert.AreEquivalent(expectedUnaffectedObjects, failureMechanisms.SelectMany(fm => fm.Calculations) .Where(calc => calc.HasOutput), "The calculation output within the failure mechanisms should not have been deleted in this scenario"); } /// /// Asserts whether the correct failure mechanism outputs are cleared when the /// changes to . /// /// The objects that should not have been affected. /// The assessment section to assert. private static void AssertCorrectOutputClearedWhenCompositionDune(IEnumerable unaffectedObjects, IAssessmentSection assessmentSection) { Assert.IsTrue(GetDuneRelevantFailureMechanisms(assessmentSection).SelectMany(fm => fm.Calculations) .All(calc => !calc.HasOutput)); AssertOutputNotCleared(unaffectedObjects, GetDuneIrrelevantFailureMechanisms(assessmentSection)); } #region LinQ queries for expectancies /// /// Retrieves the failure mechanisms that should be cleared when the /// changes to Dunes. /// /// The assessment section to retrieve the failure mechanisms from. /// A collection of affected failure mechanisms when changing to /// . private static IEnumerable GetDuneRelevantFailureMechanisms(IAssessmentSection assessmentSection) { var relevantFailureMechanisms = new List(); foreach (IFailureMechanism failureMechanism in assessmentSection.GetFailureMechanisms()) { var pipingFailureMechanism = failureMechanism as PipingFailureMechanism; var grassCoverErosionInwardsFailureMechanism = failureMechanism as GrassCoverErosionInwardsFailureMechanism; var grassCoverErosionOutwardsFailureMechanism = failureMechanism as GrassCoverErosionOutwardsFailureMechanism; var heightStructuresFailureMechanism = failureMechanism as HeightStructuresFailureMechanism; var closingStructuresFailureMechanism = failureMechanism as ClosingStructuresFailureMechanism; var stabilityPointStructuresFailureMechanism = failureMechanism as StabilityPointStructuresFailureMechanism; if (pipingFailureMechanism != null) { relevantFailureMechanisms.Add(pipingFailureMechanism); } if (grassCoverErosionInwardsFailureMechanism != null) { relevantFailureMechanisms.Add(grassCoverErosionInwardsFailureMechanism); } if (grassCoverErosionOutwardsFailureMechanism != null) { relevantFailureMechanisms.Add(grassCoverErosionOutwardsFailureMechanism); } if (heightStructuresFailureMechanism != null) { relevantFailureMechanisms.Add(heightStructuresFailureMechanism); } if (closingStructuresFailureMechanism != null) { relevantFailureMechanisms.Add(closingStructuresFailureMechanism); } if (stabilityPointStructuresFailureMechanism != null) { relevantFailureMechanisms.Add(stabilityPointStructuresFailureMechanism); } } return relevantFailureMechanisms; } /// /// Retrieves all failure mechanisms that should be untouched when the /// changes to Dunes. /// /// The assessment section to retrieve the failure mechanisms from. /// A collection of irrelevant (unaffected) failure mechanisms when changing the /// to . private static IEnumerable GetDuneIrrelevantFailureMechanisms(IAssessmentSection assessmentSection) { var failureMechanisms = new List(); foreach (IFailureMechanism failureMechanism in assessmentSection.GetFailureMechanisms()) { var waveImpactAsphaltCoverFailureMechanism = failureMechanism as WaveImpactAsphaltCoverFailureMechanism; var stabilityStoneCoverFailureMechanism = failureMechanism as StabilityStoneCoverFailureMechanism; if (waveImpactAsphaltCoverFailureMechanism != null) { failureMechanisms.Add(waveImpactAsphaltCoverFailureMechanism); } if (stabilityStoneCoverFailureMechanism != null) { failureMechanisms.Add(stabilityStoneCoverFailureMechanism); } } return failureMechanisms; } #endregion } }