Index: Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Plugin/FileImporters/StabilityPointStructureReplaceStrategy.cs =================================================================== diff -u --- Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Plugin/FileImporters/StabilityPointStructureReplaceStrategy.cs (revision 0) +++ Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Plugin/FileImporters/StabilityPointStructureReplaceStrategy.cs (revision 61d70c3a93405d74b984ebea39a0ecc746dc03f5) @@ -0,0 +1,67 @@ +// 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 Ringtoets.Common.Data; +using Ringtoets.Common.Data.Structures; +using Ringtoets.Common.Data.UpdateDataStrategies; +using Ringtoets.Common.IO.Structures; +using Ringtoets.Common.Service; +using Ringtoets.StabilityPointStructures.Data; + +namespace Ringtoets.StabilityPointStructures.Plugin.FileImporters +{ + /// + /// An + /// to replace stability point structures with the imported stability point structures + /// + public class StabilityPointStructureReplaceStrategy : ReplaceDataStrategyBase, + IStructureUpdateStrategy + { + /// + /// Creates a new instance of . + /// + /// The failure mechanism in which the + /// are updated. + /// Thrown when + /// is null. + public StabilityPointStructureReplaceStrategy(StabilityPointStructuresFailureMechanism failureMechanism) + : base(failureMechanism) {} + + public IEnumerable UpdateStructuresWithImportedData(StructureCollection targetDataCollection, + IEnumerable readStructures, + string sourceFilePath) + { + return ReplaceTargetCollectionWithImportedData(targetDataCollection, readStructures, sourceFilePath); + } + + protected override IEnumerable ClearData() + { + return RingtoetsCommonDataSynchronizationService.RemoveAllStructures( + FailureMechanism.Calculations.OfType>(), + FailureMechanism.StabilityPointStructures, + FailureMechanism.SectionResults); + } + } +} \ No newline at end of file Index: Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Plugin/Ringtoets.StabilityPointStructures.Plugin.csproj =================================================================== diff -u -ra70cb733eb761229173777b9f6cd1b977830b24b -r61d70c3a93405d74b984ebea39a0ecc746dc03f5 --- Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Plugin/Ringtoets.StabilityPointStructures.Plugin.csproj (.../Ringtoets.StabilityPointStructures.Plugin.csproj) (revision a70cb733eb761229173777b9f6cd1b977830b24b) +++ Ringtoets/StabilityPointStructures/src/Ringtoets.StabilityPointStructures.Plugin/Ringtoets.StabilityPointStructures.Plugin.csproj (.../Ringtoets.StabilityPointStructures.Plugin.csproj) (revision 61d70c3a93405d74b984ebea39a0ecc746dc03f5) @@ -41,6 +41,7 @@ Properties\GlobalAssembly.cs + Index: Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Plugin.Test/FileImporters/StabilityPointStructureReplaceDataStrategyTest.cs =================================================================== diff -u --- Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Plugin.Test/FileImporters/StabilityPointStructureReplaceDataStrategyTest.cs (revision 0) +++ Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Plugin.Test/FileImporters/StabilityPointStructureReplaceDataStrategyTest.cs (revision 61d70c3a93405d74b984ebea39a0ecc746dc03f5) @@ -0,0 +1,430 @@ +// 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.Base.Geometry; +using NUnit.Framework; +using Ringtoets.Common.Data; +using Ringtoets.Common.Data.Exceptions; +using Ringtoets.Common.Data.FailureMechanism; +using Ringtoets.Common.Data.TestUtil; +using Ringtoets.Common.Data.UpdateDataStrategies; +using Ringtoets.Common.IO.Structures; +using Ringtoets.StabilityPointStructures.Data; +using Ringtoets.StabilityPointStructures.Data.TestUtil; +using Ringtoets.StabilityPointStructures.Plugin.FileImporters; + +namespace Ringtoets.StabilityPointStructures.Plugin.Test.FileImporters +{ + [TestFixture] + public class StabilityPointStructureReplaceDataStrategyTest + { + private const string sourcePath = "some/path/to/structures"; + + [Test] + public void Constructor_WithFailureMechanismNull_ThrowsArgumentNullException() + { + // Call + TestDelegate call = () => new StabilityPointStructureReplaceStrategy(null); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual("failureMechanism", exception.ParamName); + } + + [Test] + public void Constructor_WithFailureMechanism_CreatesNewInstance() + { + // Setup + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + + // Call + var strategy = new StabilityPointStructureReplaceStrategy(failureMechanism); + + // Assert + Assert.IsInstanceOf>(strategy); + Assert.IsInstanceOf>(strategy); + } + + [Test] + public void UpdateStructuresWithImportedData_TargetDataCollectionNull_ThrowsArgumentNullException() + { + // Setup + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + var strategy = new StabilityPointStructureReplaceStrategy(failureMechanism); + + // Call + TestDelegate call = () => strategy.UpdateStructuresWithImportedData( + null, + Enumerable.Empty(), + sourcePath); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual("targetDataCollection", exception.ParamName); + } + + [Test] + public void UpdateStructuresWithImportedData_ImportedDataCollectionNull_ThrowsArgumentNullException() + { + // Setup + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + var strategy = new StabilityPointStructureReplaceStrategy(failureMechanism); + + // Call + TestDelegate call = () => strategy.UpdateStructuresWithImportedData( + new StructureCollection(), + null, + sourcePath); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual("importedDataCollection", exception.ParamName); + } + + [Test] + public void UpdateStructuresWithImportedData_SourceFilePathNull_ThrowsArgumentNullException() + { + // Setup + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + var strategy = new StabilityPointStructureReplaceStrategy(failureMechanism); + + // Call + TestDelegate call = () => strategy.UpdateStructuresWithImportedData( + new StructureCollection(), + Enumerable.Empty(), + null); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual("sourceFilePath", exception.ParamName); + } + + [Test] + public void UpdateStructuresWithImportedData_DifferentSourcePath_UpdatesSourcePath() + { + // Setup + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + StructureCollection targetCollection = failureMechanism.StabilityPointStructures; + + var strategy = new StabilityPointStructureReplaceStrategy(failureMechanism); + + var newSourcePath = "some/other/path/toStructures"; + + // Call + IEnumerable affectedObjects = strategy.UpdateStructuresWithImportedData( + targetCollection, + Enumerable.Empty(), + newSourcePath); + + // Assert + Assert.AreEqual(newSourcePath, targetCollection.SourcePath); + CollectionAssert.AreEqual(new[] + { + targetCollection + }, affectedObjects); + } + + [Test] + public void UpdateStructuresWithImportedData_CurrentCollectionAndImportedCollectionEmpty_DoesNothing() + { + // Setup + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + var targetCollection = new StructureCollection(); + + var strategy = new StabilityPointStructureReplaceStrategy(failureMechanism); + + // Call + IEnumerable affectedObjects = strategy.UpdateStructuresWithImportedData( + targetCollection, + Enumerable.Empty(), + sourcePath); + + // Assert + CollectionAssert.IsEmpty(targetCollection); + CollectionAssert.AreEqual(new[] + { + targetCollection + }, affectedObjects); + } + + [Test] + public void UpdateStructuresWithImportedData_ImportedDataContainsDuplicateIds_ThrowsUpdateDataException() + { + // Setup + const string duplicateId = "I am a duplicate id"; + StabilityPointStructure[] importedClosingStructures = + { + new TestStabilityPointStructure("name", duplicateId), + new TestStabilityPointStructure("Other name", duplicateId) + }; + + var targetCollection = new StructureCollection(); + var strategy = new StabilityPointStructureReplaceStrategy(new StabilityPointStructuresFailureMechanism()); + + // Call + TestDelegate call = () => strategy.UpdateStructuresWithImportedData( + targetCollection, + importedClosingStructures, + sourcePath); + + // Assert + var exception = Assert.Throws(call); + string expectedMessage = "Kunstwerken moeten een unieke id hebben. " + + $"Gevonden dubbele elementen: {duplicateId}."; + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + public void UpdateStructuresWithImportedData_CurrentCollectionEmptyAndImportedCollectionNotEmpty_AddsNewStructures() + { + // Setup + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + StructureCollection targetCollection = failureMechanism.StabilityPointStructures; + + var strategy = new StabilityPointStructureReplaceStrategy(failureMechanism); + + var importedCollection = new[] + { + new TestStabilityPointStructure() + }; + + // Call + IEnumerable affectedObjects = strategy.UpdateStructuresWithImportedData( + targetCollection, + importedCollection, + sourcePath); + + // Assert + Assert.AreEqual(sourcePath, targetCollection.SourcePath); + CollectionAssert.AreEqual(new[] + { + targetCollection + }, affectedObjects); + CollectionAssert.AreEqual(importedCollection, targetCollection); + } + + [Test] + public void UpdateStructuresWithImportedData_CurrentCollectionNotEmptyAndImportedCollectionEmpty_ClearsCollection() + { + // Setup + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + StructureCollection targetCollection = failureMechanism.StabilityPointStructures; + targetCollection.AddRange(new[] + { + new TestStabilityPointStructure() + }, sourcePath); + + var strategy = new StabilityPointStructureReplaceStrategy(failureMechanism); + + // Call + IEnumerable affectedObjects = strategy.UpdateStructuresWithImportedData( + targetCollection, + Enumerable.Empty(), + sourcePath); + + // Assert + Assert.AreEqual(sourcePath, targetCollection.SourcePath); + CollectionAssert.AreEqual(new[] + { + targetCollection + }, affectedObjects); + CollectionAssert.IsEmpty(targetCollection); + } + + [Test] + public void UpdateStructuresWithImportedData_CurrentCollectionNotEmptyAndImportedCollectionNotEmpty_ReplacesCurrentWithImportedData() + { + // Setup + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + StructureCollection targetCollection = failureMechanism.StabilityPointStructures; + targetCollection.AddRange(new[] + { + new TestStabilityPointStructure() + }, sourcePath); + + var importedStructure = new TestStabilityPointStructure("a different id"); + + var strategy = new StabilityPointStructureReplaceStrategy(failureMechanism); + + // Call + IEnumerable affectedObjects = strategy.UpdateStructuresWithImportedData( + targetCollection, + new[] + { + importedStructure + }, + sourcePath); + + // Assert + CollectionAssert.AreEqual(new[] + { + targetCollection + }, affectedObjects); + + var expectedCollection = new[] + { + importedStructure + }; + CollectionAssert.AreEqual(expectedCollection, targetCollection); + } + + [Test] + public void UpdateStructuresWithImportedData_CalculationWithoutOutputAndStructure_CalculationUpdatedAndReturnsAffectedObject() + { + // Setup + var structure = new TestStabilityPointStructure(); + var calculation = new TestStabilityPointStructuresCalculation + { + InputParameters = + { + Structure = structure + } + }; + + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + failureMechanism.CalculationsGroup.Children.Add(calculation); + StructureCollection targetCollection = failureMechanism.StabilityPointStructures; + targetCollection.AddRange(new[] + { + structure + }, sourcePath); + + var strategy = new StabilityPointStructureReplaceStrategy(failureMechanism); + + // Call + IEnumerable affectedObjects = strategy.UpdateStructuresWithImportedData( + targetCollection, + Enumerable.Empty(), + sourcePath); + + // Assert + Assert.IsNull(calculation.InputParameters.Structure); + Assert.IsFalse(calculation.HasOutput); + + CollectionAssert.IsEmpty(targetCollection); + + CollectionAssert.AreEquivalent(new IObservable[] + { + calculation.InputParameters, + targetCollection + }, affectedObjects); + } + + [Test] + public void UpdateStructuresWithImportedData_CalculationWithOutputAndStructure_CalculationUpdatedAndReturnsAffectedObject() + { + // Setup + var structure = new TestStabilityPointStructure(); + var calculation = new TestStabilityPointStructuresCalculation + { + InputParameters = + { + Structure = structure + }, + Output = new TestStructuresOutput() + }; + + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + failureMechanism.CalculationsGroup.Children.Add(calculation); + StructureCollection targetCollection = failureMechanism.StabilityPointStructures; + targetCollection.AddRange(new[] + { + structure + }, sourcePath); + + var strategy = new StabilityPointStructureReplaceStrategy(failureMechanism); + + // Call + IEnumerable affectedObjects = strategy.UpdateStructuresWithImportedData( + targetCollection, + Enumerable.Empty(), + sourcePath); + + // Assert + Assert.IsNull(calculation.InputParameters.Structure); + Assert.IsFalse(calculation.HasOutput); + + CollectionAssert.IsEmpty(targetCollection); + + CollectionAssert.AreEquivalent(new IObservable[] + { + calculation, + calculation.InputParameters, + targetCollection + }, affectedObjects); + } + + [Test] + public void UpdateStructuresWithImportedData_CalculationWithSectionResultAndStructure_CalculationUpdatedAndReturnsAffectedObject() + { + // Setup + var location = new Point2D(12, 34); + var structure = new TestStabilityPointStructure(location); + var calculation = new TestStabilityPointStructuresCalculation + { + InputParameters = + { + Structure = structure + } + }; + + var failureMechanism = new StabilityPointStructuresFailureMechanism(); + failureMechanism.CalculationsGroup.Children.Add(calculation); + failureMechanism.AddSection(new FailureMechanismSection("SectionResult", new[] + { + location + })); + StabilityPointStructuresFailureMechanismSectionResult sectionResult = failureMechanism.SectionResults.First(); + sectionResult.Calculation = calculation; + + StructureCollection targetCollection = failureMechanism.StabilityPointStructures; + targetCollection.AddRange(new[] + { + structure + }, sourcePath); + + var strategy = new StabilityPointStructureReplaceStrategy(failureMechanism); + + // Call + IEnumerable affectedObjects = strategy.UpdateStructuresWithImportedData( + targetCollection, + Enumerable.Empty(), + sourcePath); + + // Assert + Assert.IsNull(calculation.InputParameters.Structure); + Assert.IsNull(sectionResult.Calculation); + + CollectionAssert.IsEmpty(targetCollection); + CollectionAssert.AreEquivalent(new IObservable[] + { + sectionResult, + calculation.InputParameters, + targetCollection + }, affectedObjects); + } + } +} \ No newline at end of file Index: Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Plugin.Test/Ringtoets.StabilityPointStructures.Plugin.Test.csproj =================================================================== diff -u -rb3cd93a57114ffa27d177cabb8e4635dfc71955c -r61d70c3a93405d74b984ebea39a0ecc746dc03f5 --- Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Plugin.Test/Ringtoets.StabilityPointStructures.Plugin.Test.csproj (.../Ringtoets.StabilityPointStructures.Plugin.Test.csproj) (revision b3cd93a57114ffa27d177cabb8e4635dfc71955c) +++ Ringtoets/StabilityPointStructures/test/Ringtoets.StabilityPointStructures.Plugin.Test/Ringtoets.StabilityPointStructures.Plugin.Test.csproj (.../Ringtoets.StabilityPointStructures.Plugin.Test.csproj) (revision 61d70c3a93405d74b984ebea39a0ecc746dc03f5) @@ -61,6 +61,7 @@ +