Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/ReferenceLineConverter.cs =================================================================== diff -u -rd7c94e90643cacf26292824cdbef90b0cddc53ff -r1d52c0ffc913d7803070bd9797d811a991b5c365 --- Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/ReferenceLineConverter.cs (.../ReferenceLineConverter.cs) (revision d7c94e90643cacf26292824cdbef90b0cddc53ff) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/ReferenceLineConverter.cs (.../ReferenceLineConverter.cs) (revision 1d52c0ffc913d7803070bd9797d811a991b5c365) @@ -31,8 +31,12 @@ return null; } var line = new ReferenceLine(); - - var geometry = entityCollection.Select(entity => new Point2D(decimal.ToDouble(entity.X), decimal.ToDouble(entity.Y))); + var geometry = new Point2D[entityCollection.Count]; + foreach (var entity in entityCollection) + { + var point = new Point2D(decimal.ToDouble(entity.X), decimal.ToDouble(entity.Y)); + geometry[entity.Order] = point; + } line.SetGeometry(geometry); return line; Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ReferenceLinePersistor.cs =================================================================== diff -u -rd7c94e90643cacf26292824cdbef90b0cddc53ff -r1d52c0ffc913d7803070bd9797d811a991b5c365 --- Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ReferenceLinePersistor.cs (.../ReferenceLinePersistor.cs) (revision d7c94e90643cacf26292824cdbef90b0cddc53ff) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ReferenceLinePersistor.cs (.../ReferenceLinePersistor.cs) (revision 1d52c0ffc913d7803070bd9797d811a991b5c365) @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Data.Entity; using System.Linq; using Application.Ringtoets.Storage.Converters; using Application.Ringtoets.Storage.DbContext; @@ -11,7 +10,7 @@ public class ReferenceLinePersistor { private readonly ReferenceLineConverter converter; - private IRingtoetsEntities context; + private readonly IRingtoetsEntities context; /// /// Creates a new instance of . @@ -35,15 +34,21 @@ /// The collection where the entities are added. /// The reference line which will be added tot the /// as entities. + /// is null. public void InsertModel(ICollection entityCollection, ReferenceLine referenceLine) { - if (referenceLine != null) + if (entityCollection == null) { - if (entityCollection == null) - { - throw new ArgumentNullException("entityCollection"); - } + throw new ArgumentNullException("entityCollection"); + } + + if (entityCollection.Any()) + { context.Set().RemoveRange(entityCollection); + } + + if (referenceLine != null) + { converter.ConvertModelToEntity(referenceLine, entityCollection); } } Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj =================================================================== diff -u -rd242e89e13ef602facae6a1ef91660242bcef340 -r1d52c0ffc913d7803070bd9797d811a991b5c365 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj (.../Application.Ringtoets.Storage.Test.csproj) (revision d242e89e13ef602facae6a1ef91660242bcef340) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj (.../Application.Ringtoets.Storage.Test.csproj) (revision 1d52c0ffc913d7803070bd9797d811a991b5c365) @@ -91,6 +91,7 @@ + Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/ReferenceLineConverterTest.cs =================================================================== diff -u -r3296448aeb47b8958143a00e05db76cd55627e97 -r1d52c0ffc913d7803070bd9797d811a991b5c365 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/ReferenceLineConverterTest.cs (.../ReferenceLineConverterTest.cs) (revision 3296448aeb47b8958143a00e05db76cd55627e97) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/ReferenceLineConverterTest.cs (.../ReferenceLineConverterTest.cs) (revision 1d52c0ffc913d7803070bd9797d811a991b5c365) @@ -58,7 +58,7 @@ } [Test] - public void ConvertEntityToModel_ValidEntityValidModel_ReturnsTheEntityAsModel() + public void ConvertEntityToModel_ValidOrderedEntityValidModel_ReturnsTheEntityAsModel() { // Setup var random = new Random(21); @@ -69,7 +69,44 @@ new Point2D(random.NextDouble(), random.NextDouble()), new Point2D(random.NextDouble(), random.NextDouble()) }; + var entityCollection = points.Select((point, i) => new ReferenceLinePointEntity + { + X = Convert.ToDecimal(point.X), + Y = Convert.ToDecimal(point.Y), + Order = i + }).ToList(); + var converter = new ReferenceLineConverter(); + + // Call + ReferenceLine location = converter.ConvertEntityToModel(entityCollection); + + // Assert + Assert.AreNotEqual(points, location.Points); + for (var i = 0; i < entityCollection.Count; i++) + { + Assert.AreEqual(Decimal.ToDouble(entityCollection[i].X), points[i].X, 1e-8); + Assert.AreEqual(Decimal.ToDouble(entityCollection[i].Y), points[i].Y, 1e-8); + } + } + + [Test] + public void ConvertEntityToModel_ValidUnorderedEntityValidModel_ReturnsTheEntityAsModel() + { + // Setup + var random = new Random(21); + + IList points = new [] + { + new Point2D(random.NextDouble(), random.NextDouble()), + new Point2D(random.NextDouble(), random.NextDouble()), + new Point2D(random.NextDouble(), random.NextDouble()) + }; var entityCollection = points.Select(p => new ReferenceLinePointEntity { X = Convert.ToDecimal(p.X), Y = Convert.ToDecimal(p.Y) }).ToList(); + + entityCollection[0].Order = 1; + entityCollection[1].Order = 2; + entityCollection[2].Order = 0; + var converter = new ReferenceLineConverter(); // Call @@ -139,6 +176,7 @@ { Assert.AreEqual(points[i].X, Decimal.ToDouble(entity[i].X), 1e-8); Assert.AreEqual(points[i].Y, Decimal.ToDouble(entity[i].Y), 1e-8); + Assert.AreEqual(i, entity[i].Order); } } } Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/DbContext/DbTestSet.cs =================================================================== diff -u -rb50153c7b2f1c9c34f4575a599605f68844f43ca -r1d52c0ffc913d7803070bd9797d811a991b5c365 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/DbContext/DbTestSet.cs (.../DbTestSet.cs) (revision b50153c7b2f1c9c34f4575a599605f68844f43ca) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/DbContext/DbTestSet.cs (.../DbTestSet.cs) (revision 1d52c0ffc913d7803070bd9797d811a991b5c365) @@ -10,7 +10,7 @@ public static IDbSet GetDbTestSet(MockRepository mockRepository, ObservableCollection data) where T : class { var queryable = data.AsQueryable(); - var dbSet = mockRepository.StrictMock>(); + var dbSet = mockRepository.Stub>(); dbSet.Stub(m => m.Provider).Return(queryable.Provider); dbSet.Stub(m => m.Expression).Return(queryable.Expression); Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/IntegrationTests/StorageSqLiteIntegrationTest.cs =================================================================== diff -u -rcb5d2139148256919e697cc6b25011513ac8d6ad -r1d52c0ffc913d7803070bd9797d811a991b5c365 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/IntegrationTests/StorageSqLiteIntegrationTest.cs (.../StorageSqLiteIntegrationTest.cs) (revision cb5d2139148256919e697cc6b25011513ac8d6ad) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/IntegrationTests/StorageSqLiteIntegrationTest.cs (.../StorageSqLiteIntegrationTest.cs) (revision 1d52c0ffc913d7803070bd9797d811a991b5c365) @@ -70,6 +70,7 @@ Assert.AreEqual(firstProjectDike[i].Name, secondProjectDike[i].Name); AssertHydraulicBoundaryDatabase(firstProjectDike[i], secondProjectDike[i]); + AssertReferenceLine(firstProjectDike[i], secondProjectDike[i]); } var firstProjectDune = firstProject.Items.OfType().ToList(); @@ -110,6 +111,7 @@ var assessmentSection = loadedProject.Items.OfType().FirstOrDefault(); Assert.IsNotNull(assessmentSection); AssertHydraulicBoundaryDatabase(fullProject.Items.OfType().FirstOrDefault(), assessmentSection); + AssertReferenceLine(fullProject.Items.OfType().FirstOrDefault(), assessmentSection); } [Test] @@ -145,6 +147,7 @@ var assessmentSection = gui.Project.Items.OfType().FirstOrDefault(); Assert.IsNotNull(assessmentSection); AssertHydraulicBoundaryDatabase(fullProject.Items.OfType().FirstOrDefault(), assessmentSection); + AssertReferenceLine(fullProject.Items.OfType().FirstOrDefault(), assessmentSection); } // TearDown @@ -227,6 +230,20 @@ } } + + private static void AssertReferenceLine(AssessmentSectionBase expectedProject, AssessmentSectionBase project) + { + Assert.IsNotNull(expectedProject.ReferenceLine); + + for (int i = 0; i < expectedProject.ReferenceLine.Points.Count(); i++) + { + var expectedPoint = expectedProject.ReferenceLine.Points.ElementAt(i); + var resultingPoint = project.ReferenceLine.Points.ElementAt(i); + Assert.AreEqual(expectedPoint.X, resultingPoint.X); + Assert.AreEqual(expectedPoint.Y, resultingPoint.Y); + } + } + private void TearDownTempRingtoetsFile(string filePath) { GC.Collect(); Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/ReferenceLinePersistorTest.cs =================================================================== diff -u --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/ReferenceLinePersistorTest.cs (revision 0) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/ReferenceLinePersistorTest.cs (revision 1d52c0ffc913d7803070bd9797d811a991b5c365) @@ -0,0 +1,290 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Application.Ringtoets.Storage.DbContext; +using Application.Ringtoets.Storage.Persistors; +using Core.Common.Base.Geometry; +using NUnit.Framework; +using Rhino.Mocks; +using Ringtoets.Common.Data; + +namespace Application.Ringtoets.Storage.Test.Persistors +{ + [TestFixture] + public class ReferenceLinePersistorTest + { + [Test] + public void Constructor_WithoutContext_ThrowsArgumentNullException() + { + // Call + TestDelegate call = () => new ReferenceLinePersistor(null); + + // Assert + var parameter = Assert.Throws(call).ParamName; + Assert.AreEqual("ringtoetsContext", parameter); + } + + [Test] + public void Constructor_WithContext_DoesNotThrow() + { + // Setup + var mocks = new MockRepository(); + var context = mocks.StrictMock(); + mocks.ReplayAll(); + + // Call + TestDelegate call = () => new ReferenceLinePersistor(context); + + // Assert + Assert.DoesNotThrow(call); + mocks.VerifyAll(); + } + + [Test] + public void InsertModel_WithoutEntityCollection_ThrowsArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var context = mocks.StrictMock(); + mocks.ReplayAll(); + + var persistor = new ReferenceLinePersistor(context); + + // Call + TestDelegate call = () => persistor.InsertModel(null, null); + + // Assert + var parameter = Assert.Throws(call).ParamName; + Assert.AreEqual("entityCollection", parameter); + mocks.VerifyAll(); + } + + [Test] + public void InsertModel_WithEmptyEntityCollectionWithoutReferenceLine_NoChange() + { + // Setup + var mocks = new MockRepository(); + var context = mocks.StrictMock(); + var entities = new List(); + mocks.ReplayAll(); + + var persistor = new ReferenceLinePersistor(context); + + // Call + persistor.InsertModel(entities, null); + + // Assert + mocks.VerifyAll(); + } + + [Test] + public void InsertModel_WithEmptyEntityCollectionWithReferenceLine_AddsNewEntitiesToContext() + { + // Setup + var mocks = new MockRepository(); + var context = mocks.StrictMock(); + var entities = new List(); + mocks.ReplayAll(); + + var persistor = new ReferenceLinePersistor(context); + var referenceLine = new ReferenceLine(); + referenceLine.SetGeometry(new [] + { + new Point2D(1,1), + new Point2D(3,2), + new Point2D(1,3) + }); + + // Call + persistor.InsertModel(entities, referenceLine); + + // Assert + AssertCreatedEntities(entities, referenceLine); + mocks.VerifyAll(); + } + + [Test] + public void InsertModel_WithNonEmptyEntityCollectionWithReferenceLine_EntityCollectionClearedAddsNewEntitiesToContext() + { + // Setup + var entities = new List(); + for (int i = 0; i < 3; i++) + { + entities.Add(new ReferenceLinePointEntity + { + Order = i + }); + } + + var mocks = new MockRepository(); + var context = mocks.StrictMock(); + var set = mocks.StrictMock>(); + context.Expect(c => c.Set()).Return(set); + set.Expect(s => s.RemoveRange(entities)).WhenCalled(z => entities.Clear()); + + mocks.ReplayAll(); + + var persistor = new ReferenceLinePersistor(context); + var referenceLine = new ReferenceLine(); + referenceLine.SetGeometry(new[] + { + new Point2D(1,1), + new Point2D(3,2), + new Point2D(1,3) + }); + + // Call + persistor.InsertModel(entities, referenceLine); + + // Assert + AssertCreatedEntities(entities, referenceLine); + mocks.VerifyAll(); + } + + [Test] + public void LoadModel_WithoutEntityCollection_ThrowsArgumentException() + { + // Setup + var mocks = new MockRepository(); + var context = mocks.StrictMock(); + mocks.ReplayAll(); + + var persistor = new ReferenceLinePersistor(context); + + // Call + TestDelegate call = () => persistor.LoadModel(null); + + // Assert + var parameter = Assert.Throws(call).ParamName; + Assert.AreEqual("entityCollection", parameter); + mocks.VerifyAll(); + } + + [Test] + public void LoadModel_EmptyEntityCollection_ReturnsNull() + { + // Setup + var mocks = new MockRepository(); + var context = mocks.StrictMock(); + mocks.ReplayAll(); + + var entities = new List(); + var persistor = new ReferenceLinePersistor(context); + + // Call + var referenceLine = persistor.LoadModel(entities); + + // Assert + Assert.IsNull(referenceLine); + mocks.VerifyAll(); + } + + [Test] + public void LoadModel_EntityCollectionWithOrderedElements_ReturnsEqualReferenceLineInstance() + { + // Setup + var mocks = new MockRepository(); + var context = mocks.StrictMock(); + mocks.ReplayAll(); + + var entities = new List + { + new ReferenceLinePointEntity + { + Order = 0, + X = 1, + Y = 2 + }, + new ReferenceLinePointEntity + { + Order = 1, + X = 3, + Y = 2 + }, + new ReferenceLinePointEntity + { + Order = 2, + X = 5, + Y = 3 + } + }; + var persistor = new ReferenceLinePersistor(context); + + // Call + var referenceLine = persistor.LoadModel(entities); + + // Assert + AssertCreatedReferenceLine(referenceLine, entities); + mocks.VerifyAll(); + } + + [Test] + public void LoadModel_EntityCollectionWithUnorderedElements_ReturnsEqualReferenceLineInstance() + { + // Setup + var mocks = new MockRepository(); + var context = mocks.StrictMock(); + mocks.ReplayAll(); + + var entities = new List + { + new ReferenceLinePointEntity + { + Order = 0, + X = 1, + Y = 2 + }, + new ReferenceLinePointEntity + { + Order = 2, + X = 5, + Y = 3 + }, + new ReferenceLinePointEntity + { + Order = 1, + X = 3, + Y = 2 + } + }; + var persistor = new ReferenceLinePersistor(context); + + // Call + var referenceLine = persistor.LoadModel(entities); + + // Assert + AssertCreatedReferenceLine(referenceLine, entities); + mocks.VerifyAll(); + } + + private void AssertCreatedEntities(List entities, ReferenceLine referenceLine) + { + Assert.AreEqual(entities.Count, referenceLine.Points.Count()); + for (int i = 0; i < entities.Count; i++) + { + var entity = entities[i]; + var point = referenceLine.Points.ElementAt(i); + + Assert.AreEqual(point.X, entity.X); + Assert.AreEqual(referenceLine.Points.ElementAt(i).Y, entity.Y); + Assert.AreEqual(i, entity.Order); + Assert.AreEqual(0, entity.ReferenceLinePointEntityId); + } + } + + private void AssertCreatedReferenceLine(ReferenceLine referenceLine, List entities) + { + Assert.AreEqual(entities.Count, referenceLine.Points.Count()); + for (int i = 0; i < referenceLine.Points.Count(); i++) + { + var point = referenceLine.Points.ElementAt(i); + var entity = entities.First(e => e.Order == i); + + Assert.AreEqual(entity.X, point.X); + Assert.AreEqual(entity.Y, point.Y); + } + } + } +} \ No newline at end of file Index: Application/Ringtoets/test/Application.Ringtoets.Storage.TestUtil/RingtoetsProjectHelper.cs =================================================================== diff -u -r503a1871546f1694465c356bf06c167bc67740dd -r1d52c0ffc913d7803070bd9797d811a991b5c365 --- Application/Ringtoets/test/Application.Ringtoets.Storage.TestUtil/RingtoetsProjectHelper.cs (.../RingtoetsProjectHelper.cs) (revision 503a1871546f1694465c356bf06c167bc67740dd) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.TestUtil/RingtoetsProjectHelper.cs (.../RingtoetsProjectHelper.cs) (revision 1d52c0ffc913d7803070bd9797d811a991b5c365) @@ -19,7 +19,10 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System.Collections.Generic; using Core.Common.Base.Data; +using Core.Common.Base.Geometry; +using Ringtoets.Common.Data; using Ringtoets.HydraRing.Data; using Ringtoets.Integration.Data; @@ -45,12 +48,28 @@ new DikeAssessmentSection { Name = "dikeAssessmentSection", - HydraulicBoundaryDatabase = GetHydraulicBoundaryDatabase() + HydraulicBoundaryDatabase = GetHydraulicBoundaryDatabase(), + ReferenceLine = GetReferenceLine() } } }; } + private static ReferenceLine GetReferenceLine() + { + IEnumerable points = new[] + { + new Point2D(2, 3), + new Point2D(5, 4), + new Point2D(5, 8), + new Point2D(-3, 2) + }; + + var referenceLine = new ReferenceLine(); + referenceLine.SetGeometry(points); + return referenceLine; + } + private static HydraulicBoundaryDatabase GetHydraulicBoundaryDatabase() { var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase