Index: Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/FailureMechanismSectionReplaceStrategy.cs =================================================================== diff -u -r8a1174adfd80faee4f4af7ed0a71f2a19d0f2378 -r47a96e6294e02b226c41cceb2cbf0a4fc41b058c --- Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/FailureMechanismSectionReplaceStrategy.cs (.../FailureMechanismSectionReplaceStrategy.cs) (revision 8a1174adfd80faee4f4af7ed0a71f2a19d0f2378) +++ Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/FailureMechanismSectionReplaceStrategy.cs (.../FailureMechanismSectionReplaceStrategy.cs) (revision 47a96e6294e02b226c41cceb2cbf0a4fc41b058c) @@ -21,12 +21,13 @@ using System; using System.Collections.Generic; +using Ringtoets.Common.Data.Exceptions; using Ringtoets.Common.Data.FailureMechanism; namespace Ringtoets.Common.IO.FileImporters { /// - /// A replace strategy that can be used to replace failure mechanism sections with + /// An that can be used to replace failure mechanism sections with /// imported failure mechanism sections. /// public class FailureMechanismSectionReplaceStrategy : IFailureMechanismSectionUpdateStrategy @@ -49,8 +50,6 @@ this.failureMechanism = failureMechanism; } - /// - /// Thrown when any parameter is null. public void UpdateSectionsWithImportedData(IEnumerable importedFailureMechanismSections, string sourcePath) { @@ -64,7 +63,14 @@ throw new ArgumentNullException(nameof(sourcePath)); } - failureMechanism.SetSections(importedFailureMechanismSections, sourcePath); + try + { + failureMechanism.SetSections(importedFailureMechanismSections, sourcePath); + } + catch (Exception e) + { + throw new UpdateDataException(e.Message, e); + } } } } \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/FailureMechanismSectionUpdateStrategy.cs =================================================================== diff -u -r09e38b33b4a1297a2136900cecaa471c2d917bea -r47a96e6294e02b226c41cceb2cbf0a4fc41b058c --- Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/FailureMechanismSectionUpdateStrategy.cs (.../FailureMechanismSectionUpdateStrategy.cs) (revision 09e38b33b4a1297a2136900cecaa471c2d917bea) +++ Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/FailureMechanismSectionUpdateStrategy.cs (.../FailureMechanismSectionUpdateStrategy.cs) (revision 47a96e6294e02b226c41cceb2cbf0a4fc41b058c) @@ -22,12 +22,13 @@ using System; using System.Collections.Generic; using System.Linq; +using Ringtoets.Common.Data.Exceptions; using Ringtoets.Common.Data.FailureMechanism; namespace Ringtoets.Common.IO.FileImporters { /// - /// An update strategy that can be used to update failure mechanism sections with + /// An that can be used to update failure mechanism sections with /// imported failure mechanism sections. /// /// The type of that will be updated. @@ -40,7 +41,7 @@ /// /// Creates a new instance of . /// - /// The to set the sections to. + /// The to update the secitons for. /// The to use when updating /// the section results. /// Thrown when any parameter is null. @@ -61,8 +62,6 @@ this.sectionResultUpdateStrategy = sectionResultUpdateStrategy; } - /// - /// Thrown when any parameter is null. public void UpdateSectionsWithImportedData(IEnumerable importedFailureMechanismSections, string sourcePath) { @@ -78,7 +77,14 @@ T[] oldSectionResults = failureMechanism.SectionResults.ToArray(); - failureMechanism.SetSections(importedFailureMechanismSections, sourcePath); + try + { + failureMechanism.SetSections(importedFailureMechanismSections, sourcePath); + } + catch (Exception e) + { + throw new UpdateDataException(e.Message, e); + } foreach (T sectionResult in failureMechanism.SectionResults) { Index: Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/FailureMechanismSectionsImporter.cs =================================================================== diff -u -r603210e73c97c052ba7fc2447b45cf57750a6ae4 -r47a96e6294e02b226c41cceb2cbf0a4fc41b058c --- Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/FailureMechanismSectionsImporter.cs (.../FailureMechanismSectionsImporter.cs) (revision 603210e73c97c052ba7fc2447b45cf57750a6ae4) +++ Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/FailureMechanismSectionsImporter.cs (.../FailureMechanismSectionsImporter.cs) (revision 47a96e6294e02b226c41cceb2cbf0a4fc41b058c) @@ -67,7 +67,8 @@ ReferenceLine referenceLine, string filePath, IFailureMechanismSectionUpdateStrategy failureMechanismSectionUpdateStrategy, - IImporterMessageProvider messageProvider) : base(filePath, importTarget) + IImporterMessageProvider messageProvider) + : base(filePath, importTarget) { if (referenceLine == null) { @@ -112,20 +113,20 @@ return false; } - if (Canceled) + IEnumerable orderedReadSections = OrderSections(readFailureMechanismSections, referenceLine); + if (!ArePointsSnapped(referenceLine.Points.Last(), orderedReadSections.Last().EndPoint)) { + LogCriticalError(Resources.FailureMechanismSectionsImporter_Import_File_contains_unchained_sections); return false; } - NotifyProgress(messageProvider.GetAddDataToModelProgressText(), 3, 3); - - IEnumerable orderedReadSections = OrderSections(readFailureMechanismSections, referenceLine); - if (!ArePointsSnapped(referenceLine.Points.Last(), orderedReadSections.Last().EndPoint)) + if (Canceled) { - LogCriticalError(Resources.FailureMechanismSectionsImporter_Import_File_contains_unchained_sections); return false; } + NotifyProgress(messageProvider.GetAddDataToModelProgressText(), 3, 3); + AddImportedDataToModel(orderedReadSections); return true; } @@ -163,11 +164,11 @@ } catch (CriticalFileReadException exception) { - Log.Error(exception.Message); + LogCriticalError(exception.Message); } catch (ArgumentException exception) { - Log.Error(exception.Message); + LogCriticalError(exception.Message); } return null; Index: Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/IFailureMechanismSectionResultUpdateStrategy.cs =================================================================== diff -u -r09e38b33b4a1297a2136900cecaa471c2d917bea -r47a96e6294e02b226c41cceb2cbf0a4fc41b058c --- Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/IFailureMechanismSectionResultUpdateStrategy.cs (.../IFailureMechanismSectionResultUpdateStrategy.cs) (revision 09e38b33b4a1297a2136900cecaa471c2d917bea) +++ Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/IFailureMechanismSectionResultUpdateStrategy.cs (.../IFailureMechanismSectionResultUpdateStrategy.cs) (revision 47a96e6294e02b226c41cceb2cbf0a4fc41b058c) @@ -19,6 +19,7 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; using Ringtoets.Common.Data.FailureMechanism; namespace Ringtoets.Common.IO.FileImporters @@ -37,6 +38,7 @@ /// /// The object to get the data from that will be put on . /// The object to update with data from . + /// Thrown when any parameter is null. void UpdateSectionResult(T origin, T target); } } \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/IFailureMechanismSectionUpdateStrategy.cs =================================================================== diff -u -r8a1174adfd80faee4f4af7ed0a71f2a19d0f2378 -r47a96e6294e02b226c41cceb2cbf0a4fc41b058c --- Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/IFailureMechanismSectionUpdateStrategy.cs (.../IFailureMechanismSectionUpdateStrategy.cs) (revision 8a1174adfd80faee4f4af7ed0a71f2a19d0f2378) +++ Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/IFailureMechanismSectionUpdateStrategy.cs (.../IFailureMechanismSectionUpdateStrategy.cs) (revision 47a96e6294e02b226c41cceb2cbf0a4fc41b058c) @@ -21,6 +21,7 @@ using System; using System.Collections.Generic; +using Ringtoets.Common.Data.Exceptions; using Ringtoets.Common.Data.FailureMechanism; namespace Ringtoets.Common.IO.FileImporters @@ -36,6 +37,13 @@ /// /// The imported failure mechanism sections. /// The source path from where the failure mechanism sections were imported. + /// Thrown when any parameter is null. + /// Thrown when: + /// + /// is not a valid file path. + /// contains sections that are not properly chained. + /// + /// void UpdateSectionsWithImportedData(IEnumerable importedFailureMechanismSections, string sourcePath); } Index: Ringtoets/Common/test/Ringtoets.Common.Forms.Test/UpdateInfos/RingtoetsUpdateInfoFactoryTest.cs =================================================================== diff -u -r0f065fd28bb73332728fbfccebe7ce647cd008b6 -r47a96e6294e02b226c41cceb2cbf0a4fc41b058c --- Ringtoets/Common/test/Ringtoets.Common.Forms.Test/UpdateInfos/RingtoetsUpdateInfoFactoryTest.cs (.../RingtoetsUpdateInfoFactoryTest.cs) (revision 0f065fd28bb73332728fbfccebe7ce647cd008b6) +++ Ringtoets/Common/test/Ringtoets.Common.Forms.Test/UpdateInfos/RingtoetsUpdateInfoFactoryTest.cs (.../RingtoetsUpdateInfoFactoryTest.cs) (revision 47a96e6294e02b226c41cceb2cbf0a4fc41b058c) @@ -20,6 +20,7 @@ // All rights reserved. using System; +using System.Linq; using Core.Common.Gui.Plugin; using Core.Common.TestUtil; using Core.Common.Util; @@ -66,17 +67,98 @@ FailureMechanismSectionsContext, TestFailureMechanism, FailureMechanismSectionResult>(sectionResultUpdateStrategy); // Assert - var failureMechanismSectionsContext = new FailureMechanismSectionsContext(new TestFailureMechanism(), assessmentSection); - Assert.IsInstanceOf(updateInfo.CreateFileImporter(failureMechanismSectionsContext, "")); Assert.AreEqual("Vakindeling", updateInfo.Name); Assert.AreEqual("Algemeen", updateInfo.Category); FileFilterGenerator fileFilterGenerator = updateInfo.FileFilterGenerator; Assert.AreEqual("Shapebestand (*.shp)|*.shp", fileFilterGenerator.Filter); TestHelper.AssertImagesAreEqual(Resources.SectionsIcon, updateInfo.Image); + Assert.IsNull(updateInfo.VerifyUpdates); + + mocks.VerifyAll(); + } + + [Test] + public void CreateFailureMechanismSectionsUpdateInfo_WithArguments_ReturnsExpectedCreatedFileImporter() + { + // Setup + var mocks = new MockRepository(); + var sectionResultUpdateStrategy = mocks.Stub>(); + var assessmentSection = mocks.Stub(); + assessmentSection.ReferenceLine = new ReferenceLine(); + mocks.ReplayAll(); + + // Call + UpdateInfo updateInfo = RingtoetsUpdateInfoFactory.CreateFailureMechanismSectionsUpdateInfo< + FailureMechanismSectionsContext, TestFailureMechanism, FailureMechanismSectionResult>(sectionResultUpdateStrategy); + + // Assert + var failureMechanismSectionsContext = new FailureMechanismSectionsContext(new TestFailureMechanism(), assessmentSection); + Assert.IsInstanceOf(updateInfo.CreateFileImporter(failureMechanismSectionsContext, "")); + + mocks.VerifyAll(); + } + + [Test] + public void CreateFailureMechanismSectionsUpdateInfo_WithSourcePath_ReturnsIsEnabledTrue() + { + // Setup + var mocks = new MockRepository(); + var sectionResultUpdateStrategy = mocks.Stub>(); + var assessmentSection = mocks.Stub(); + assessmentSection.ReferenceLine = new ReferenceLine(); + mocks.ReplayAll(); + + // Call + UpdateInfo updateInfo = RingtoetsUpdateInfoFactory.CreateFailureMechanismSectionsUpdateInfo< + FailureMechanismSectionsContext, TestFailureMechanism, FailureMechanismSectionResult>(sectionResultUpdateStrategy); + + // Assert + var testFailureMechanism = new TestFailureMechanism(); + testFailureMechanism.SetSections(Enumerable.Empty(), "path/to/sections"); + var failureMechanismSectionsContext = new FailureMechanismSectionsContext(testFailureMechanism, assessmentSection); + Assert.IsTrue(updateInfo.IsEnabled(failureMechanismSectionsContext)); + mocks.VerifyAll(); + } + + [Test] + public void CreateFailureMechanismSectionsUpdateInfo_WithoutSetSourcePath_ReturnsIsEnabledFalse() + { + // Setup + var mocks = new MockRepository(); + var sectionResultUpdateStrategy = mocks.Stub>(); + var assessmentSection = mocks.Stub(); + assessmentSection.ReferenceLine = new ReferenceLine(); + mocks.ReplayAll(); + + // Call + UpdateInfo updateInfo = RingtoetsUpdateInfoFactory.CreateFailureMechanismSectionsUpdateInfo< + FailureMechanismSectionsContext, TestFailureMechanism, FailureMechanismSectionResult>(sectionResultUpdateStrategy); + + // Assert + var failureMechanismSectionsContext = new FailureMechanismSectionsContext(new TestFailureMechanism(), assessmentSection); Assert.IsFalse(updateInfo.IsEnabled(failureMechanismSectionsContext)); + mocks.VerifyAll(); + } + [Test] + public void CreateFailureMechanismSectionsUpdateInfo_WithoutSetSourcePath_ReturnsNullPath() + { + // Setup + var mocks = new MockRepository(); + var sectionResultUpdateStrategy = mocks.Stub>(); + var assessmentSection = mocks.Stub(); + assessmentSection.ReferenceLine = new ReferenceLine(); + mocks.ReplayAll(); + + // Call + UpdateInfo updateInfo = RingtoetsUpdateInfoFactory.CreateFailureMechanismSectionsUpdateInfo< + FailureMechanismSectionsContext, TestFailureMechanism, FailureMechanismSectionResult>(sectionResultUpdateStrategy); + + // Assert + var failureMechanismSectionsContext = new FailureMechanismSectionsContext(new TestFailureMechanism(), assessmentSection); + Assert.IsNull(updateInfo.CurrentPath(failureMechanismSectionsContext)); mocks.VerifyAll(); } } Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/FailureMechanismSectionReplaceStrategyTest.cs =================================================================== diff -u -r34bad9c8d588c7ed43acdc1be4cd7725a3811fb8 -r47a96e6294e02b226c41cceb2cbf0a4fc41b058c --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/FailureMechanismSectionReplaceStrategyTest.cs (.../FailureMechanismSectionReplaceStrategyTest.cs) (revision 34bad9c8d588c7ed43acdc1be4cd7725a3811fb8) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/FailureMechanismSectionReplaceStrategyTest.cs (.../FailureMechanismSectionReplaceStrategyTest.cs) (revision 47a96e6294e02b226c41cceb2cbf0a4fc41b058c) @@ -20,10 +20,13 @@ // All rights reserved. using System; +using System.Collections.Generic; using System.Linq; +using Core.Common.Base.Geometry; using Core.Common.TestUtil; using NUnit.Framework; using Rhino.Mocks; +using Ringtoets.Common.Data.Exceptions; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Data.TestUtil; using Ringtoets.Common.IO.FileImporters; @@ -116,5 +119,68 @@ Assert.AreEqual(sourcePath, failureMechanism.FailureMechanismSectionSourcePath); Assert.AreEqual(sections.Single(), failureMechanism.Sections.Single()); } + + [Test] + public void UpdateSectionsWithImportedData_WithInvalidSections_ThrowsUpdateDataException() + { + // Setup + string sourcePath = TestHelper.GetScratchPadPath(); + + var failureMechanism = new TestFailureMechanism(); + FailureMechanismSection failureMechanismSection1 = FailureMechanismSectionTestFactory.CreateFailureMechanismSection(new[] + { + new Point2D(0.0, 0.0), + new Point2D(5.0, 5.0) + }); + FailureMechanismSection failureMechanismSection2 = FailureMechanismSectionTestFactory.CreateFailureMechanismSection(new[] + { + new Point2D(10.0, 10.0), + new Point2D(15.0, 15.0) + }); + + var failureMechanismSectionReplaceStrategy = new FailureMechanismSectionReplaceStrategy(failureMechanism); + + FailureMechanismSection[] sections = + { + failureMechanismSection1, + failureMechanismSection2 + }; + + // Call + TestDelegate call = () => failureMechanismSectionReplaceStrategy.UpdateSectionsWithImportedData(sections, sourcePath); + + // Assert + var exception = Assert.Throws(call); + Assert.IsInstanceOf(exception.InnerException); + Assert.AreEqual(exception.InnerException.Message, exception.Message); + } + + [Test] + public void UpdateSectionsWithImportedData_WithEmptyData_ClearsSectionsAndUpdatesPath() + { + // Setup + const string oldSourcePath = "old/path"; + string sourcePath = TestHelper.GetScratchPadPath(); + + var failureMechanism = new TestFailureMechanism(); + failureMechanism.SetSections(new[] + { + FailureMechanismSectionTestFactory.CreateFailureMechanismSection() + }, oldSourcePath); + + var failureMechanismSectionReplaceStrategy = new FailureMechanismSectionReplaceStrategy(failureMechanism); + + // Precondition + IEnumerable failureMechanismSections = failureMechanism.Sections; + Assert.AreEqual(1, failureMechanismSections.Count()); + Assert.AreEqual(oldSourcePath, failureMechanism.FailureMechanismSectionSourcePath); + + // Call + failureMechanismSectionReplaceStrategy.UpdateSectionsWithImportedData(Enumerable.Empty(), sourcePath); + + // Assert + Assert.AreEqual(sourcePath, failureMechanism.FailureMechanismSectionSourcePath); + Assert.IsEmpty(failureMechanismSections); + } } } \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/FailureMechanismSectionUpdateStrategyTest.cs =================================================================== diff -u -r09e38b33b4a1297a2136900cecaa471c2d917bea -r47a96e6294e02b226c41cceb2cbf0a4fc41b058c --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/FailureMechanismSectionUpdateStrategyTest.cs (.../FailureMechanismSectionUpdateStrategyTest.cs) (revision 09e38b33b4a1297a2136900cecaa471c2d917bea) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/FailureMechanismSectionUpdateStrategyTest.cs (.../FailureMechanismSectionUpdateStrategyTest.cs) (revision 47a96e6294e02b226c41cceb2cbf0a4fc41b058c) @@ -28,6 +28,7 @@ using NUnit.Framework; using Rhino.Mocks; using Ringtoets.Common.Data.Calculation; +using Ringtoets.Common.Data.Exceptions; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Data.TestUtil; using Ringtoets.Common.IO.FileImporters; @@ -42,11 +43,11 @@ { // Setup var mocks = new MockRepository(); - var sectionResultUpdateStrategy = mocks.Stub>(); + var sectionResultUpdateStrategy = mocks.Stub>(); mocks.ReplayAll(); // Call - TestDelegate call = () => new FailureMechanismSectionUpdateStrategy(null, sectionResultUpdateStrategy); + TestDelegate call = () => new FailureMechanismSectionUpdateStrategy(null, sectionResultUpdateStrategy); // Assert string paramName = Assert.Throws(call).ParamName; @@ -59,11 +60,11 @@ { // Setup var mocks = new MockRepository(); - var failureMechanism = mocks.Stub>(); + var failureMechanism = mocks.Stub>(); mocks.ReplayAll(); // Call - TestDelegate call = () => new FailureMechanismSectionUpdateStrategy(failureMechanism, null); + TestDelegate call = () => new FailureMechanismSectionUpdateStrategy(failureMechanism, null); // Assert string paramName = Assert.Throws(call).ParamName; @@ -75,12 +76,12 @@ { // Setup var mocks = new MockRepository(); - var failureMechanism = mocks.Stub>(); - var sectionResultUpdateStrategy = mocks.Stub>(); + var failureMechanism = mocks.Stub>(); + var sectionResultUpdateStrategy = mocks.Stub>(); mocks.ReplayAll(); // Call - var importer = new FailureMechanismSectionUpdateStrategy(failureMechanism, sectionResultUpdateStrategy); + var importer = new FailureMechanismSectionUpdateStrategy(failureMechanism, sectionResultUpdateStrategy); // Assert Assert.IsInstanceOf(importer); @@ -92,12 +93,12 @@ { // Setup var mocks = new MockRepository(); - var failureMechanism = mocks.Stub>(); - var sectionResultUpdateStrategy = mocks.Stub>(); + var failureMechanism = mocks.Stub>(); + var sectionResultUpdateStrategy = mocks.Stub>(); mocks.ReplayAll(); // Call - TestDelegate call = () => new FailureMechanismSectionUpdateStrategy(failureMechanism, sectionResultUpdateStrategy).UpdateSectionsWithImportedData(null, ""); + TestDelegate call = () => new FailureMechanismSectionUpdateStrategy(failureMechanism, sectionResultUpdateStrategy).UpdateSectionsWithImportedData(null, ""); // Assert string paramName = Assert.Throws(call).ParamName; @@ -110,11 +111,11 @@ { // Setup var mocks = new MockRepository(); - var failureMechanism = mocks.Stub>(); - var sectionResultUpdateStrategy = mocks.Stub>(); + var failureMechanism = mocks.Stub>(); + var sectionResultUpdateStrategy = mocks.Stub>(); mocks.ReplayAll(); - var failureMechanismSectionUpdateStrategy = new FailureMechanismSectionUpdateStrategy(failureMechanism, sectionResultUpdateStrategy); + var failureMechanismSectionUpdateStrategy = new FailureMechanismSectionUpdateStrategy(failureMechanism, sectionResultUpdateStrategy); // Call TestDelegate call = () => failureMechanismSectionUpdateStrategy.UpdateSectionsWithImportedData( @@ -149,19 +150,21 @@ failureMechanismSection2 }, sourcePath); - int testValue = new Random(39).Next(1, 20); - IObservableEnumerable failureMechanismSectionResults = failureMechanism.SectionResults; - failureMechanismSectionResults.First().TestValue = testValue; - failureMechanismSectionResults.ElementAt(1).TestValue = testValue; + IObservableEnumerable failureMechanismSectionResults = failureMechanism.SectionResults; + TestFailureMechanismSectionResult oldSectionResult = failureMechanismSectionResults.First(); var sectionResultUpdateStrategy = new TestUpdateFailureMechanismSectionResultUpdateStrategy(); - var failureMechanismSectionUpdateStrategy = new FailureMechanismSectionUpdateStrategy(failureMechanism, sectionResultUpdateStrategy); + var failureMechanismSectionUpdateStrategy = new FailureMechanismSectionUpdateStrategy(failureMechanism, sectionResultUpdateStrategy); FailureMechanismSection[] sections = { - failureMechanismSection1, FailureMechanismSectionTestFactory.CreateFailureMechanismSection(new[] { + new Point2D(0.0, 0.0), + new Point2D(5.0, 5.0) + }), + FailureMechanismSectionTestFactory.CreateFailureMechanismSection(new[] + { new Point2D(5.0, 5.0), new Point2D(15.0, 15.0) }) @@ -176,22 +179,90 @@ IEnumerable failureMechanismSections = failureMechanism.Sections; Assert.AreEqual(2, failureMechanismSections.Count()); CollectionAssert.AreEqual(sections, failureMechanismSections); - Assert.AreEqual(testValue, failureMechanismSectionResults.First().TestValue); - Assert.Zero(failureMechanismSectionResults.ElementAt(1).TestValue); + Assert.AreSame(oldSectionResult, sectionResultUpdateStrategy.Origin); + Assert.AreSame(failureMechanismSectionResults.First(), sectionResultUpdateStrategy.Target); } - private class TestUpdateFailureMechanism : FailureMechanismBase, IHasSectionResults + [Test] + public void UpdateSectionsWithImportedData_WithInvalidSections_ThrowsUpdateDataException() { - private readonly ObservableList sectionResults; + // Setup + string sourcePath = TestHelper.GetScratchPadPath(); + var failureMechanism = new TestUpdateFailureMechanism(); + FailureMechanismSection failureMechanismSection1 = FailureMechanismSectionTestFactory.CreateFailureMechanismSection(new[] + { + new Point2D(0.0, 0.0), + new Point2D(5.0, 5.0) + }); + FailureMechanismSection failureMechanismSection2 = FailureMechanismSectionTestFactory.CreateFailureMechanismSection(new[] + { + new Point2D(10.0, 10.0), + new Point2D(15.0, 15.0) + }); + + var sectionResultUpdateStrategy = new TestUpdateFailureMechanismSectionResultUpdateStrategy(); + var failureMechanismSectionUpdateStrategy = new FailureMechanismSectionUpdateStrategy(failureMechanism, sectionResultUpdateStrategy); + + FailureMechanismSection[] sections = + { + failureMechanismSection1, + failureMechanismSection2 + }; + + // Call + TestDelegate call = () => failureMechanismSectionUpdateStrategy.UpdateSectionsWithImportedData(sections, sourcePath); + + // Assert + var exception = Assert.Throws(call); + Assert.IsInstanceOf(exception.InnerException); + Assert.AreEqual(exception.InnerException.Message, exception.Message); + Assert.IsFalse(sectionResultUpdateStrategy.Updated); + } + + [Test] + public void UpdateSectionsWithImportedData_WithEmptyData_ClearsSectionsAndUpdatesPath() + { + // Setup + const string oldSourcePath = "old/path"; + string sourcePath = TestHelper.GetScratchPadPath(); + + var failureMechanism = new TestUpdateFailureMechanism(); + failureMechanism.SetSections(new[] + { + FailureMechanismSectionTestFactory.CreateFailureMechanismSection() + }, oldSourcePath); + + var sectionResultUpdateStrategy = new TestUpdateFailureMechanismSectionResultUpdateStrategy(); + var failureMechanismSectionUpdateStrategy = new FailureMechanismSectionUpdateStrategy( + failureMechanism, sectionResultUpdateStrategy); + + // Precondition + IEnumerable failureMechanismSections = failureMechanism.Sections; + Assert.AreEqual(1, failureMechanismSections.Count()); + Assert.AreEqual(oldSourcePath, failureMechanism.FailureMechanismSectionSourcePath); + + // Call + failureMechanismSectionUpdateStrategy.UpdateSectionsWithImportedData(Enumerable.Empty(), sourcePath); + + // Assert + Assert.AreEqual(sourcePath, failureMechanism.FailureMechanismSectionSourcePath); + Assert.IsEmpty(failureMechanismSections); + Assert.IsFalse(sectionResultUpdateStrategy.Updated); + } + + private class TestUpdateFailureMechanism : FailureMechanismBase, IHasSectionResults + { + private readonly ObservableList sectionResults; + public TestUpdateFailureMechanism() : base("Test", "TST", 2) { - sectionResults = new ObservableList(); + sectionResults = new ObservableList(); } public override IEnumerable Calculations { get; } - public IObservableEnumerable SectionResults + public IObservableEnumerable SectionResults { get { @@ -201,7 +272,7 @@ protected override void AddSectionResult(FailureMechanismSection section) { - sectionResults.Add(new TestUpdateFailureMechanismSectionResult(section)); + sectionResults.Add(new TestFailureMechanismSectionResult(section)); } protected override void ClearSectionResults() @@ -210,21 +281,17 @@ } } - private class TestUpdateFailureMechanismSectionResult : FailureMechanismSectionResult + private class TestUpdateFailureMechanismSectionResultUpdateStrategy : IFailureMechanismSectionResultUpdateStrategy { - public TestUpdateFailureMechanismSectionResult(FailureMechanismSection section) : base(section) - { - TestValue = 0; - } + public bool Updated { get; set; } + public TestFailureMechanismSectionResult Origin { get; set; } + public TestFailureMechanismSectionResult Target { get; set; } - public int TestValue { get; set; } - } - - private class TestUpdateFailureMechanismSectionResultUpdateStrategy : IFailureMechanismSectionResultUpdateStrategy - { - public void UpdateSectionResult(TestUpdateFailureMechanismSectionResult origin, TestUpdateFailureMechanismSectionResult target) + public void UpdateSectionResult(TestFailureMechanismSectionResult origin, TestFailureMechanismSectionResult target) { - target.TestValue = origin.TestValue; + Updated = true; + Origin = origin; + Target = target; } } } Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/FailureMechanismSectionsImporterTest.cs =================================================================== diff -u -r2c6d254976145b223c6055b0b8c0c8303f8038a1 -r47a96e6294e02b226c41cceb2cbf0a4fc41b058c --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/FailureMechanismSectionsImporterTest.cs (.../FailureMechanismSectionsImporterTest.cs) (revision 2c6d254976145b223c6055b0b8c0c8303f8038a1) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/FailureMechanismSectionsImporterTest.cs (.../FailureMechanismSectionsImporterTest.cs) (revision 47a96e6294e02b226c41cceb2cbf0a4fc41b058c) @@ -92,7 +92,10 @@ mocks.ReplayAll(); // Call - TestDelegate call = () => new FailureMechanismSectionsImporter(failureMechanism, new ReferenceLine(), "", new TestFailureMechanismSectionUpdateStrategy(), null); + TestDelegate call = () => new FailureMechanismSectionsImporter(failureMechanism, + new ReferenceLine(), + "", + new TestFailureMechanismSectionUpdateStrategy(), null); // Assert string paramName = Assert.Throws(call).ParamName; @@ -122,7 +125,7 @@ [Test] [TestCase("traject_1-1.shp", "traject_1-1_vakken.shp", 62)] [TestCase("traject_19-1.shp", "traject_19-1_vakken.shp", 17)] - public void Import_ValidFileCorrespondingToReferenceLineAndNoSectionImportedYet_ImportSections(string referenceLineFileName, string sectionsFileName, int sectionCount) + public void Import_ValidFileCorrespondingToReferenceLineAndNoSectionImportedYet_CallsUpdateStrategy(string referenceLineFileName, string sectionsFileName, int sectionCount) { // Setup var mocks = new MockRepository(); @@ -154,80 +157,10 @@ } [Test] - public void Import_ValidFileCorrespondingToReferenceLineAndHasSectionImported_ReplaceSections() - { - // Setup - var mocks = new MockRepository(); - var messageProvider = mocks.Stub(); - mocks.ReplayAll(); - - string referenceLineFilePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, - Path.Combine("ReferenceLine", "traject_1-1.shp")); - string sectionsFilePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, - Path.Combine("FailureMechanismSections", "traject_1-1_vakken.shp")); - - ReferenceLine importReferenceLine = ImportReferenceLine(referenceLineFilePath); - - var failureMechanism = new TestFailureMechanism(); - var oldSection = new FailureMechanismSection("A", importReferenceLine.Points); - FailureMechanismTestHelper.SetSections(failureMechanism, new[] - { - oldSection - }); - var updateStrategy = new TestFailureMechanismSectionUpdateStrategy(); - - var importer = new FailureMechanismSectionsImporter(failureMechanism, importReferenceLine, sectionsFilePath, updateStrategy, messageProvider); - - // Call - bool importSuccessful = importer.Import(); - - // Assert - Assert.IsTrue(importSuccessful); - - IEnumerable sections = updateStrategy.ImportedFailureMechanismSections; - CollectionAssert.DoesNotContain(sections, oldSection); - Assert.AreEqual(62, sections.Count()); - AssertSectionsAreValidForReferenceLine(sections, importReferenceLine); - mocks.VerifyAll(); - } - - [Test] - public void Import_ValidArtificialFileImperfectlyCorrespondingToReferenceLineAndNoSectionImportedYet_ImportSections() - { - // Setup - var mocks = new MockRepository(); - var messageProvider = mocks.Stub(); - mocks.ReplayAll(); - - string referenceLineFilePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, - Path.Combine("ReferenceLine", "Artificial_referencelijn_testA.shp")); - string sectionsFilePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, - Path.Combine("FailureMechanismSections", "Artificial_referencelijn_testA_ValidVakken.shp")); - - ReferenceLine importReferenceLine = ImportReferenceLine(referenceLineFilePath); - - var failureMechanism = new TestFailureMechanism(); - var updateStrategy = new TestFailureMechanismSectionUpdateStrategy(); - - var importer = new FailureMechanismSectionsImporter(failureMechanism, importReferenceLine, sectionsFilePath, updateStrategy, messageProvider); - - // Call - bool importSuccessful = importer.Import(); - - // Assert - Assert.IsTrue(importSuccessful); - - IEnumerable sections = updateStrategy.ImportedFailureMechanismSections; - Assert.AreEqual(7, sections.Count()); - AssertSectionsAreValidForReferenceLine(sections, importReferenceLine); - mocks.VerifyAll(); - } - - [Test] [TestCase("StartSectionReversedCoordinates")] [TestCase("EndSectionReversedCoordinates")] [TestCase("InBetweenSectionReversedCoordinates")] - public void Import_ValidArtificialFileWithReversedSectionCoordinatesImperfectlyCorrespondingToReferenceLineAndNoSectionImportedYet_ImportSections( + public void Import_ValidArtificialFileWithReversedSectionCoordinatesImperfectlyCorrespondingToReferenceLineAndNoSectionImportedYet_CallsUpdateStrategy( string affectedSection) { // Setup @@ -302,11 +235,12 @@ } [Test] - public void Import_FilePathIsDirectory_CancelImportWithErrorMessage() + public void Import_FilePathIsDirectory_AbortImportWithErrorMessage() { // Setup var mocks = new MockRepository(); var messageProvider = mocks.StrictMock(); + messageProvider.Expect(mp => mp.GetUpdateDataFailedLogMessageText(sectionsTypeDescriptor)).Return(expectedUpdateDataFailedText); mocks.ReplayAll(); string referenceLineFilePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, @@ -325,19 +259,21 @@ Action call = () => importSuccessful = importer.Import(); // Assert - string expectedMessage = $@"Fout bij het lezen van bestand '{sectionsFilePath}': bestandspad mag niet verwijzen naar een lege bestandsnaam."; + string expectedMessage = string.Format(expectedUpdateDataFailedText, + $@"Fout bij het lezen van bestand '{sectionsFilePath}': bestandspad mag niet verwijzen naar een lege bestandsnaam."); TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); Assert.IsFalse(importSuccessful); - CollectionAssert.IsEmpty(failureMechanism.Sections); + Assert.IsFalse(updateStrategy.Updated); mocks.VerifyAll(); } [Test] - public void Import_FileDoesNotExist_CancelImportWithErrorMessage() + public void Import_FileDoesNotExist_AbortImportWithErrorMessage() { // Setup var mocks = new MockRepository(); var messageProvider = mocks.StrictMock(); + messageProvider.Expect(mp => mp.GetUpdateDataFailedLogMessageText(sectionsTypeDescriptor)).Return(expectedUpdateDataFailedText); mocks.ReplayAll(); string referenceLineFilePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, @@ -356,15 +292,15 @@ Action call = () => importSuccessful = importer.Import(); // Assert - string expectedMessage = $@"Fout bij het lezen van bestand '{sectionsFilePath}': het bestand bestaat niet."; + string expectedMessage = string.Format(expectedUpdateDataFailedText, $@"Fout bij het lezen van bestand '{sectionsFilePath}': het bestand bestaat niet."); TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); Assert.IsFalse(importSuccessful); - CollectionAssert.IsEmpty(failureMechanism.Sections); + Assert.IsFalse(updateStrategy.Updated); mocks.VerifyAll(); } [Test] - public void Import_EmptyArtificialFile_CancelImportWithErrorMessage() + public void Import_EmptyArtificialFile_AbortImportWithErrorMessage() { // Setup var mocks = new MockRepository(); @@ -392,14 +328,14 @@ string expectedMessage = string.Format(expectedUpdateDataFailedText, "Het bestand heeft geen vakindeling"); TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); Assert.IsFalse(importSuccessful); - CollectionAssert.IsEmpty(failureMechanism.Sections); + Assert.IsFalse(updateStrategy.Updated); mocks.VerifyAll(); } [Test] [TestCase("StartTooFarFromReferenceline")] [TestCase("EndTooFarFromReferenceline")] - public void Import_InvalidArtificialFileBecauseOfStartEndPointsTooFarFromReferenceLine_CancelImportWithErrorMessage(string shapeCondition) + public void Import_InvalidArtificialFileBecauseOfStartEndPointsTooFarFromReferenceLine_AbortImportWithErrorMessage(string shapeCondition) { // Setup var mocks = new MockRepository(); @@ -425,17 +361,17 @@ Action call = () => importSuccessful = importer.Import(); // Assert - string expectedMessage = String.Format(expectedUpdateDataFailedText, "De geografische ligging van ieder vak moet overeenkomen met de ligging van (een deel van) de referentielijn"); + string expectedMessage = string.Format(expectedUpdateDataFailedText, "De geografische ligging van ieder vak moet overeenkomen met de ligging van (een deel van) de referentielijn"); TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); Assert.IsFalse(importSuccessful); - CollectionAssert.IsEmpty(failureMechanism.Sections); + Assert.IsFalse(updateStrategy.Updated); mocks.VerifyAll(); } [Test] [TestCase("StartTooFarFromReferencelineStart")] [TestCase("EndTooFarFromReferencelineEnd")] - public void Import_InvalidArtificialFileBecauseOfStartEndPointsTooFarFromStartEndOfReferenceLine_CancelImportWithErrorMessage(string shapeCondition) + public void Import_InvalidArtificialFileBecauseOfStartEndPointsTooFarFromStartEndOfReferenceLine_AbortImportWithErrorMessage(string shapeCondition) { // Setup var mocks = new MockRepository(); @@ -465,12 +401,12 @@ "De geografische ligging van ieder vak moet overeenkomen met de ligging van (een deel van) de referentielijn"); TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); Assert.IsFalse(importSuccessful); - CollectionAssert.IsEmpty(failureMechanism.Sections); + Assert.IsFalse(updateStrategy.Updated); mocks.VerifyAll(); } [Test] - public void Import_InvalidArtificialFileBecauseSectionsDoNotFullyCoverReferenceLine_CancelImportWithErrorMessage() + public void Import_InvalidArtificialFileBecauseSectionsDoNotFullyCoverReferenceLine_AbortImportWithErrorMessage() { // Setup var mocks = new MockRepository(); @@ -498,17 +434,16 @@ string expectedMessage = string.Format(expectedUpdateDataFailedText, "De opgetelde lengte van de vakken moet overeenkomen met de trajectlengte"); TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); Assert.IsFalse(importSuccessful); - CollectionAssert.IsEmpty(failureMechanism.Sections); + Assert.IsFalse(updateStrategy.Updated); mocks.VerifyAll(); } [Test] - public void Import_InvalidArtificialFileBecauseUnchainedSections_CancelImportWithErrorMessage() + public void Import_InvalidArtificialFileBecauseUnchainedSections_AbortImportWithErrorMessage() { // Setup var mocks = new MockRepository(); var messageProvider = mocks.StrictMock(); - messageProvider.Expect(mp => mp.GetAddDataToModelProgressText()).Return(string.Empty); messageProvider.Expect(mp => mp.GetUpdateDataFailedLogMessageText(sectionsTypeDescriptor)).Return(expectedUpdateDataFailedText); mocks.ReplayAll(); @@ -532,12 +467,12 @@ string expectedMessage = string.Format(expectedUpdateDataFailedText, "Het bestand moet vakken bevatten die allen op elkaar aansluiten"); TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); Assert.IsFalse(importSuccessful); - CollectionAssert.IsEmpty(failureMechanism.Sections); + Assert.IsFalse(updateStrategy.Updated); mocks.VerifyAll(); } [Test] - public void Import_InvalidArtificialFileBecauseSomePointsNotOnReferenceLine_CancelImportWithErrorMessage() + public void Import_InvalidArtificialFileBecauseSomePointsNotOnReferenceLine_AbortImportWithErrorMessage() { // Setup var mocks = new MockRepository(); @@ -565,12 +500,12 @@ string expectedMessage = string.Format(expectedUpdateDataFailedText, "De opgetelde lengte van de vakken moet overeenkomen met de trajectlengte"); TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); Assert.IsFalse(importSuccessful); - CollectionAssert.IsEmpty(failureMechanism.Sections); + Assert.IsFalse(updateStrategy.Updated); mocks.VerifyAll(); } [Test] - public void Import_MissingNameValue_CancelImportWithErrorMessage() + public void Import_MissingNameValue_AbortImportWithErrorMessage() { // Setup var mocks = new MockRepository(); @@ -593,7 +528,7 @@ string expectedMessage = $"Fout bij het lezen van bestand '{sectionsFilePath}': voor één of meerdere vakken is geen naam opgegeven."; TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); Assert.IsFalse(importSuccessful); - CollectionAssert.IsEmpty(failureMechanism.Sections); + Assert.IsFalse(updateStrategy.Updated); mocks.VerifyAll(); } @@ -633,12 +568,12 @@ // Assert TestHelper.AssertLogMessageIsGenerated(call, expectedCancelledText, 1); Assert.IsFalse(importSuccessful); - CollectionAssert.IsEmpty(failureMechanism.Sections); + Assert.IsFalse(updateStrategy.Updated); mocks.VerifyAll(); } [Test] - public void Import_CancelOfImportWhenValidatingImportedections_CancelsImportAndLogs() + public void Import_CancelOfImportWhenValidatingImportedSections_CancelsImportAndLogs() { // Setup var mocks = new MockRepository(); @@ -673,7 +608,7 @@ // Assert TestHelper.AssertLogMessageIsGenerated(call, expectedCancelledText, 1); Assert.IsFalse(importSuccessful); - CollectionAssert.IsEmpty(failureMechanism.Sections); + Assert.IsFalse(updateStrategy.Updated); mocks.VerifyAll(); } @@ -718,13 +653,11 @@ } [Test] - public void Import_ReuseOfCanceledImportToValidTargetWithValidFile_TrueAndLogMessagesAndExpectedImportedData() + public void Import_ReuseOfCanceledImportToValidTargetWithValidFile_TrueAndExpectedImportedData() { // Setup var mocks = new MockRepository(); - var messageProvider = mocks.StrictMock(); - messageProvider.Expect(mp => mp.GetCancelledLogMessageText(sectionsTypeDescriptor)).Return(string.Empty); - messageProvider.Expect(mp => mp.GetAddDataToModelProgressText()).Return(string.Empty); + var messageProvider = mocks.Stub(); mocks.ReplayAll(); string referenceLineFilePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, @@ -789,12 +722,15 @@ private class TestFailureMechanismSectionUpdateStrategy : IFailureMechanismSectionUpdateStrategy { + public bool Updated { get; private set; } + public string SourcePath { get; private set; } public IEnumerable ImportedFailureMechanismSections { get; private set; } public void UpdateSectionsWithImportedData(IEnumerable importedFailureMechanismSections, string sourcePath) { + Updated = true; SourcePath = sourcePath; ImportedFailureMechanismSections = importedFailureMechanismSections; } Index: Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/PipingFailureMechanismSectionResultUpdateStrategy.cs =================================================================== diff -u -r1a0da0d93e61ca3053f7ad34fe15e2131df69346 -r47a96e6294e02b226c41cceb2cbf0a4fc41b058c --- Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/PipingFailureMechanismSectionResultUpdateStrategy.cs (.../PipingFailureMechanismSectionResultUpdateStrategy.cs) (revision 1a0da0d93e61ca3053f7ad34fe15e2131df69346) +++ Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/PipingFailureMechanismSectionResultUpdateStrategy.cs (.../PipingFailureMechanismSectionResultUpdateStrategy.cs) (revision 47a96e6294e02b226c41cceb2cbf0a4fc41b058c) @@ -31,8 +31,6 @@ /// public class PipingFailureMechanismSectionResultUpdateStrategy : IFailureMechanismSectionResultUpdateStrategy { - /// - /// Thrown when any parameter is null. public void UpdateSectionResult(PipingFailureMechanismSectionResult origin, PipingFailureMechanismSectionResult target) { if (origin == null)