Index: Ringtoets/GrassCoverErosionInwards/test/Ringtoets.GrassCoverErosionInwards.Utils.Test/GrassCoverErosionInwardsDataSynchronizationServiceTest.cs =================================================================== diff -u -raeb6e1a439617630e7613b9ed5af152c345fa2c6 -r6d3a034f41b0db321d53b5a4c7b6e1b77b257081 --- Ringtoets/GrassCoverErosionInwards/test/Ringtoets.GrassCoverErosionInwards.Utils.Test/GrassCoverErosionInwardsDataSynchronizationServiceTest.cs (.../GrassCoverErosionInwardsDataSynchronizationServiceTest.cs) (revision aeb6e1a439617630e7613b9ed5af152c345fa2c6) +++ Ringtoets/GrassCoverErosionInwards/test/Ringtoets.GrassCoverErosionInwards.Utils.Test/GrassCoverErosionInwardsDataSynchronizationServiceTest.cs (.../GrassCoverErosionInwardsDataSynchronizationServiceTest.cs) (revision 6d3a034f41b0db321d53b5a4c7b6e1b77b257081) @@ -23,9 +23,11 @@ using System.Collections.Generic; using System.Linq; using Core.Common.Base; +using Core.Common.Base.Geometry; using NUnit.Framework; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.DikeProfiles; +using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.Data.Probability; using Ringtoets.Common.Data.TestUtil; @@ -209,17 +211,51 @@ } [Test] - public void RemoveAllDikeProfiles_FailureMechanismNull_ThrowsArgumentNullException() + public void RemoveAllDikeProfile_CalculationsNull_ThrowsArgumentNullException() { // Call - TestDelegate call = () => GrassCoverErosionInwardsDataSynchronizationService.RemoveAllDikeProfiles(null); + TestDelegate call = () => + GrassCoverErosionInwardsDataSynchronizationService.RemoveAllDikeProfiles( + null, + new DikeProfileCollection(), + Enumerable.Empty()); // Assert var exception = Assert.Throws(call); - Assert.AreEqual("failureMechanism", exception.ParamName); + Assert.AreEqual("calculations", exception.ParamName); } [Test] + public void RemoveAllDikeProfile_DikeProfilesNull_ThrowsArgumentNullException() + { + // Call + TestDelegate call = () => + GrassCoverErosionInwardsDataSynchronizationService.RemoveAllDikeProfiles( + Enumerable.Empty(), + null, + Enumerable.Empty()); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual("dikeProfiles", exception.ParamName); + } + + [Test] + public void RemoveAllDikeProfile_SectionResultsNull_ThrowsArgumentNullException() + { + // Call + TestDelegate call = () => + GrassCoverErosionInwardsDataSynchronizationService.RemoveAllDikeProfiles( + Enumerable.Empty(), + new DikeProfileCollection(), + null); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual("sectionResults", exception.ParamName); + } + + [Test] public void RemoveAllDikeProfiles_FullyConfiguredFailureMechanism_RemovesAllDikeProfilesAndDependentData() { // Setup @@ -238,7 +274,10 @@ // Call IEnumerable affectedObjects = - GrassCoverErosionInwardsDataSynchronizationService.RemoveAllDikeProfiles(failureMechanism); + GrassCoverErosionInwardsDataSynchronizationService.RemoveAllDikeProfiles( + failureMechanism.Calculations.Cast(), + failureMechanism.DikeProfiles, + failureMechanism.SectionResults); // Assert // Note: To make sure the clear is performed regardless of what is done with @@ -261,35 +300,130 @@ } [Test] - public void RemoveDikeProfile_FailureMechanismNull_ThrowsArgumentNullException() + public void RemoveAllDikeProfiles_CalculationWithRemovedDikeProfileAndAssignedToSection_ReturnsAllAffectedCalculationsAndClearsDependentData() { + // Setup + var locationRemovedProfile = new Point2D(0, 0); + var removedProfile = new TestDikeProfile(locationRemovedProfile, "RemovedProfileId"); + + var calculationWitRemovedProfile = new GrassCoverErosionInwardsCalculation + { + InputParameters = + { + DikeProfile = removedProfile + } + }; + var calculations = new[] + { + calculationWitRemovedProfile + }; + + var dikeProfiles = new DikeProfileCollection(); + dikeProfiles.AddRange(new[] + { + removedProfile + }, "some/path"); + + var sectionResultWithAffectedCalculation = new GrassCoverErosionInwardsFailureMechanismSectionResult( + new FailureMechanismSection("Section", new[] + { + locationRemovedProfile + })) + { + Calculation = calculationWitRemovedProfile + }; + + var sectionResults = new[] + { + sectionResultWithAffectedCalculation + }; + // Call + IEnumerable affectedObjects = + GrassCoverErosionInwardsDataSynchronizationService.RemoveAllDikeProfiles(calculations, + dikeProfiles, + sectionResults); + + // Assert + CollectionAssert.DoesNotContain(dikeProfiles, removedProfile); + Assert.IsNull(sectionResultWithAffectedCalculation.Calculation); + + var expectedAffectedObjects = new IObservable[] + { + calculationWitRemovedProfile.InputParameters, + sectionResultWithAffectedCalculation, + dikeProfiles + }; + CollectionAssert.AreEquivalent(expectedAffectedObjects, affectedObjects); + } + + [Test] + public void RemoveDikeProfile_DikeProfileToRemove_ThrowsArgumentNullException() + { + // Call TestDelegate call = () => - GrassCoverErosionInwardsDataSynchronizationService.RemoveDikeProfile(null, new TestDikeProfile()); + GrassCoverErosionInwardsDataSynchronizationService.RemoveDikeProfile(null, + Enumerable.Empty(), + new DikeProfileCollection(), + Enumerable.Empty()); // Assert var exception = Assert.Throws(call); - Assert.AreEqual("failureMechanism", exception.ParamName); + Assert.AreEqual("dikeProfileToRemove", exception.ParamName); } [Test] - public void RemoveDikeProfile_DikeProfileNull_ThrowsArgumentNullException() + public void RemoveDikeProfile_CalculationsNull_ThrowsArgumentNullException() { // Call TestDelegate call = () => - GrassCoverErosionInwardsDataSynchronizationService.RemoveDikeProfile(new GrassCoverErosionInwardsFailureMechanism(), null); + GrassCoverErosionInwardsDataSynchronizationService.RemoveDikeProfile(new TestDikeProfile(), + null, + new DikeProfileCollection(), + Enumerable.Empty()); // Assert var exception = Assert.Throws(call); - Assert.AreEqual("dikeProfile", exception.ParamName); + Assert.AreEqual("calculations", exception.ParamName); } [Test] - public void RemoveDikeProfile_FullyConfiguredFailureMechanism_ReturnsOnlyAffectedCalculations() + public void RemoveDikeProfile_DikeProfilesNull_ThrowsArgumentNullException() { + // Call + TestDelegate call = () => + GrassCoverErosionInwardsDataSynchronizationService.RemoveDikeProfile(new TestDikeProfile(), + Enumerable.Empty(), + null, + Enumerable.Empty()); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual("dikeProfiles", exception.ParamName); + } + + [Test] + public void RemoveDikeProfile_SectionResultsNull_ThrowsArgumentNullException() + { + // Call + TestDelegate call = () => + GrassCoverErosionInwardsDataSynchronizationService.RemoveDikeProfile(new TestDikeProfile(), + Enumerable.Empty(), + new DikeProfileCollection(), + null); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual("sectionResults", exception.ParamName); + } + + [Test] + public void RemoveDikeProfile_VariousCalculationConfigurations_ReturnsOnlyAffectedCalculations() + { // Setup GrassCoverErosionInwardsFailureMechanism failureMechanism = CreateFullyConfiguredFailureMechanism(); DikeProfile profileToBeCleared = failureMechanism.DikeProfiles[0]; + DikeProfile profileToKeep = failureMechanism.DikeProfiles[1]; GrassCoverErosionInwardsCalculation[] affectedCalculationsWithProfile = failureMechanism.Calculations @@ -299,20 +433,27 @@ GrassCoverErosionInwardsCalculation[] affectedCalculationsWithOutput = affectedCalculationsWithProfile.Where(calc => calc.HasOutput).ToArray(); + GrassCoverErosionInwardsCalculation[] unaffectedCalculationsWithProfile = + failureMechanism.Calculations + .Cast() + .Where(calc => ReferenceEquals(profileToKeep, calc.InputParameters.DikeProfile)) + .ToArray(); + // Pre-condition CollectionAssert.IsNotEmpty(affectedCalculationsWithOutput); // Call IEnumerable affectedObjects = - GrassCoverErosionInwardsDataSynchronizationService.RemoveDikeProfile(failureMechanism, profileToBeCleared); + GrassCoverErosionInwardsDataSynchronizationService.RemoveDikeProfile(profileToBeCleared, + failureMechanism.Calculations.Cast(), + failureMechanism.DikeProfiles, + failureMechanism.SectionResults); // Assert CollectionAssert.DoesNotContain(failureMechanism.DikeProfiles, profileToBeCleared); - foreach (GrassCoverErosionInwardsCalculation calculation in affectedCalculationsWithOutput) - { - Assert.IsFalse(calculation.HasOutput); - } + Assert.True(affectedCalculationsWithOutput.All(calc => !calc.HasOutput)); + Assert.True(unaffectedCalculationsWithProfile.All(calc => calc.InputParameters.DikeProfile != null)); IEnumerable expectedAffectedObjects = affectedCalculationsWithProfile.Select(calc => calc.InputParameters) @@ -325,6 +466,94 @@ CollectionAssert.AreEquivalent(expectedAffectedObjects, affectedObjects); } + [Test] + public void RemoveDikeProfile_CalculationWithRemovedDikeProfileAndAssignedToSection_ReturnsOnlyAffectedCalculationsAndClearsDependentData() + { + // Setup + const string profileToBeRemovedId = "RemovedProfileId"; + const string profileToKeepId = "UnaffectedProfileId"; + + var locationRemovedProfile = new Point2D(0, 0); + var removedProfile = new TestDikeProfile(locationRemovedProfile, profileToBeRemovedId); + + var calculationWitRemovedProfile = new GrassCoverErosionInwardsCalculation + { + InputParameters = + { + DikeProfile = removedProfile + } + }; + + var locationUnaffectedProfile = new Point2D(5, 5); + var unaffectedProfile = new TestDikeProfile(locationUnaffectedProfile, profileToKeepId); + var calculationWithProfileToKeep = new GrassCoverErosionInwardsCalculation + { + InputParameters = + { + DikeProfile = unaffectedProfile + } + }; + + var calculations = new[] + { + calculationWitRemovedProfile, + calculationWithProfileToKeep + }; + + var dikeProfiles = new DikeProfileCollection(); + dikeProfiles.AddRange(new[] + { + removedProfile, + unaffectedProfile + }, "some/path"); + + var affectedSection = new FailureMechanismSection("Section", new[] + { + locationRemovedProfile, + locationUnaffectedProfile + }); + var sectionResultWithAffectedCalculation = new GrassCoverErosionInwardsFailureMechanismSectionResult(affectedSection) + { + Calculation = calculationWitRemovedProfile + }; + + var unaffectedSection = new FailureMechanismSection("Section2", new[] + { + locationUnaffectedProfile, + new Point2D(10, 10) + }); + var sectionResultWithUnaffectedCalculation = new GrassCoverErosionInwardsFailureMechanismSectionResult(unaffectedSection) + { + Calculation = calculationWithProfileToKeep + }; + + var sectionResults = new[] + { + sectionResultWithUnaffectedCalculation, + sectionResultWithAffectedCalculation + }; + + // Call + IEnumerable affectedObjects = + GrassCoverErosionInwardsDataSynchronizationService.RemoveDikeProfile(removedProfile, + calculations, + dikeProfiles, + sectionResults); + + // Assert + CollectionAssert.DoesNotContain(dikeProfiles, removedProfile); + Assert.AreSame(calculationWithProfileToKeep, sectionResultWithUnaffectedCalculation.Calculation); + Assert.IsNull(sectionResultWithAffectedCalculation.Calculation); + + var expectedAffectedObjects = new IObservable[] + { + calculationWitRemovedProfile.InputParameters, + sectionResultWithAffectedCalculation, + dikeProfiles + }; + CollectionAssert.AreEquivalent(expectedAffectedObjects, affectedObjects); + } + private static GrassCoverErosionInwardsFailureMechanism CreateFullyConfiguredFailureMechanism() { var testDikeProfile1 = new TestDikeProfile("Profile 1", "ID 1");