Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj =================================================================== diff -u -rd242e89e13ef602facae6a1ef91660242bcef340 -rbbe8871cfabad25988ec8f94115da2c485267118 --- Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj (.../Application.Ringtoets.Storage.csproj) (revision d242e89e13ef602facae6a1ef91660242bcef340) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj (.../Application.Ringtoets.Storage.csproj) (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -52,10 +52,10 @@ Properties\GlobalAssembly.cs - - + + - + @@ -77,11 +77,11 @@ RingtoetsEntities.tt - - - - - + + + + + Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/DikeAssessmentSectionConverter.cs =================================================================== diff -u --- Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/DikeAssessmentSectionConverter.cs (revision 0) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/DikeAssessmentSectionConverter.cs (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -0,0 +1,96 @@ +// Copyright (C) Stichting Deltares 2016. 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 Application.Ringtoets.Storage.DbContext; +using Ringtoets.HydraRing.Data; +using Ringtoets.Integration.Data; + +namespace Application.Ringtoets.Storage.Converters +{ + /// + /// Converter for to + /// and to . + /// + public class DikeAssessmentSectionConverter : IEntityConverter + { + /// + /// Converts to . + /// + /// The to convert. + /// The to obtain the model. + /// A new instance of , based on the properties of . + /// Thrown when or is null. + public DikeAssessmentSection ConvertEntityToModel(DikeAssessmentSectionEntity entity) + { + if (entity == null) + { + throw new ArgumentNullException("entity"); + } + + var dikeAssessmentSection = new DikeAssessmentSection(); + dikeAssessmentSection.StorageId = entity.DikeAssessmentSectionEntityId; + dikeAssessmentSection.Name = entity.Name ?? string.Empty; + dikeAssessmentSection.FailureMechanismContribution.Norm = entity.Norm; + + if (entity.HydraulicDatabaseLocation != null && entity.HydraulicDatabaseVersion != null) + { + dikeAssessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase + { + FilePath = entity.HydraulicDatabaseLocation, + Version = entity.HydraulicDatabaseVersion + }; + } + + return dikeAssessmentSection; + } + + /// + /// Converts to . + /// + /// The to convert. + /// A reference to the to be saved. + /// Thrown when: + /// is null + /// is null. + /// + public void ConvertModelToEntity(DikeAssessmentSection modelObject, DikeAssessmentSectionEntity entity) + { + if (modelObject == null) + { + throw new ArgumentNullException("modelObject"); + } + if (entity == null) + { + throw new ArgumentNullException("entity"); + } + entity.DikeAssessmentSectionEntityId = modelObject.StorageId; + entity.Name = modelObject.Name; + entity.Norm = modelObject.FailureMechanismContribution.Norm; + + if (modelObject.HydraulicBoundaryDatabase != null) + { + entity.HydraulicDatabaseLocation = modelObject.HydraulicBoundaryDatabase.FilePath; + entity.HydraulicDatabaseVersion = modelObject.HydraulicBoundaryDatabase.Version; + } + } + } +} \ No newline at end of file Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/DikeAssessmentSectionEntityConverter.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/PipingFailureMechanismConverter.cs =================================================================== diff -u --- Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/PipingFailureMechanismConverter.cs (revision 0) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/PipingFailureMechanismConverter.cs (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -0,0 +1,70 @@ +// Copyright (C) Stichting Deltares 2016. 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 Application.Ringtoets.Storage.DbContext; +using Ringtoets.Piping.Data; + +namespace Application.Ringtoets.Storage.Converters +{ + /// + /// Converter for to + /// and to . + /// + public class PipingFailureMechanismConverter : IEntityConverter + { + public PipingFailureMechanism ConvertEntityToModel(FailureMechanismEntity entity) + { + if (entity == null) + { + throw new ArgumentNullException("entity"); + } + + if (entity.FailureMechanismType != (int) FailureMechanismType.DikesPipingFailureMechanism) + { + throw new ArgumentException(@"Incorrect modelType", "entity"); + } + + var failureMechanism = new PipingFailureMechanism + { + StorageId = entity.FailureMechanismEntityId + }; + + return failureMechanism; + } + + public void ConvertModelToEntity(PipingFailureMechanism modelObject, FailureMechanismEntity entity) + { + if (modelObject == null) + { + throw new ArgumentNullException("modelObject"); + } + + if (entity == null) + { + throw new ArgumentNullException("entity"); + } + + entity.FailureMechanismEntityId = modelObject.StorageId; + entity.FailureMechanismType = (int) FailureMechanismType.DikesPipingFailureMechanism; + } + } +} \ No newline at end of file Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/PipingFailureMechanismEntityConverter.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/ProjectConverter.cs =================================================================== diff -u --- Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/ProjectConverter.cs (revision 0) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/ProjectConverter.cs (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -0,0 +1,79 @@ +// Copyright (C) Stichting Deltares 2016. 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 Application.Ringtoets.Storage.DbContext; +using Core.Common.Base.Data; + +namespace Application.Ringtoets.Storage.Converters +{ + /// + /// Converter for to + /// and to . + /// + public class ProjectConverter : IEntityConverter + { + /// + /// Converts to . + /// + /// The to convert. + /// A new instance of , based on the properties of . + /// Thrown when is null. + public Project ConvertEntityToModel(ProjectEntity entity) + { + if (entity == null) + { + throw new ArgumentNullException("entity"); + } + + var project = new Project + { + StorageId = entity.ProjectEntityId, + Description = entity.Description + }; + + return project; + } + + /// + /// Converts to . + /// + /// The to convert. + /// A reference to the to be saved. + /// Thrown when: + /// is null + /// is null. + /// + public void ConvertModelToEntity(Project modelObject, ProjectEntity entity) + { + if (modelObject == null) + { + throw new ArgumentNullException("modelObject"); + } + if (entity == null) + { + throw new ArgumentNullException("entity"); + } + entity.ProjectEntityId = modelObject.StorageId; + entity.Description = modelObject.Description; + } + } +} \ No newline at end of file Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/ProjectEntityConverter.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/DikeAssessmentSectionEntityPersistor.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/DikeAssessmentSectionPersistor.cs =================================================================== diff -u --- Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/DikeAssessmentSectionPersistor.cs (revision 0) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/DikeAssessmentSectionPersistor.cs (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -0,0 +1,274 @@ +// Copyright (C) Stichting Deltares 2016. 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.Data.Entity; +using System.Diagnostics; +using System.Linq; +using Application.Ringtoets.Storage.Converters; +using Application.Ringtoets.Storage.DbContext; +using Application.Ringtoets.Storage.Exceptions; +using Application.Ringtoets.Storage.Properties; +using Ringtoets.Integration.Data; + +namespace Application.Ringtoets.Storage.Persistors +{ + /// + /// Persistor for . + /// + public class DikeAssessmentSectionPersistor + { + private readonly DbSet dikeAssessmentSectionSet; + private readonly DikeAssessmentSectionConverter converter; + private readonly Dictionary insertedList = new Dictionary(); + private readonly ICollection modifiedList = new List(); + + private readonly PipingFailureMechanismPersistor dikePipingFailureMechanismEntityPersistor; + private readonly HydraulicBoundaryLocationPersistor hydraulicLocationEntityPersistor; + private readonly ReferenceLinePersistor referenceLinePersistor; + + /// + /// New instance of . + /// + /// The storage context. + /// Thrown when is null. + public DikeAssessmentSectionPersistor(IRingtoetsEntities ringtoetsContext) + { + if (ringtoetsContext == null) + { + throw new ArgumentNullException("ringtoetsContext"); + } + dikeAssessmentSectionSet = ringtoetsContext.DikeAssessmentSectionEntities; + + converter = new DikeAssessmentSectionConverter(); + + dikePipingFailureMechanismEntityPersistor = new PipingFailureMechanismPersistor(ringtoetsContext); + hydraulicLocationEntityPersistor = new HydraulicBoundaryLocationPersistor(ringtoetsContext); + referenceLinePersistor = new ReferenceLinePersistor(ringtoetsContext); + } + + /// + /// Loads the as . + /// + /// The to load. + /// A new instance of , based on the properties of . + public DikeAssessmentSection LoadModel(DikeAssessmentSectionEntity entity) + { + var dikeAssessmentSection = converter.ConvertEntityToModel(entity); + + if (dikeAssessmentSection.HydraulicBoundaryDatabase != null) + { + dikeAssessmentSection.HydraulicBoundaryDatabase.Locations.AddRange(hydraulicLocationEntityPersistor.LoadModel(entity.HydraulicLocationEntities)); + } + + foreach (var failureMechanismEntity in entity.FailureMechanismEntities) + { + if (failureMechanismEntity.FailureMechanismType == (int) FailureMechanismType.DikesPipingFailureMechanism) + { + dikePipingFailureMechanismEntityPersistor.LoadModel(failureMechanismEntity, dikeAssessmentSection.PipingFailureMechanism); + } + } + + dikeAssessmentSection.ReferenceLine = referenceLinePersistor.LoadModel(entity.ReferenceLinePointEntities); + + return dikeAssessmentSection; + } + + /// + /// Ensures that the model is added as in the . + /// + /// Collection where objects can be added. Usually, this collection is a navigation property of a . + /// to be saved in the storage. + /// Value used for sorting. + /// Thrown when: + /// is null. + /// is null. + /// + public void InsertModel(ICollection parentNavigationProperty, DikeAssessmentSection model, int order) + { + if (parentNavigationProperty == null) + { + throw new ArgumentNullException("parentNavigationProperty"); + } + if (model == null) + { + throw new ArgumentNullException("model"); + } + var entity = PerformInsertModel(model, parentNavigationProperty, order); + + InsertChildren(model, entity); + } + + /// + /// Ensures that the is set as in the . + /// + /// Collection where objects can be searched and added. Usually, this collection is a navigation property of a . + /// to be saved in the storage. + /// Value used for sorting. + /// Thrown when: + /// is null. + /// is null. + /// + /// Thrown when the is read-only. + /// Thrown when the storageId of > 0 and: + /// More than one element found in that should have been unique. + /// No such element exists in . + /// + public void UpdateModel(ICollection parentNavigationProperty, DikeAssessmentSection model, int order) + { + if (parentNavigationProperty == null) + { + throw new ArgumentNullException("parentNavigationProperty"); + } + if (model == null) + { + throw new ArgumentNullException("model"); + } + var entity = model.StorageId > 0 ? PerformUpdateModel(model, parentNavigationProperty, order) : PerformInsertModel(model, parentNavigationProperty, order); + + UpdateChildren(model, entity); + } + + /// + /// All unmodified in will be removed. + /// + /// List where objects can be searched. Usually, this collection is a navigation property of a . + /// Thrown when the is read-only. + public void RemoveUnModifiedEntries(ICollection parentNavigationProperty) + { + var untouchedModifiedList = parentNavigationProperty.Where(e => e.DikeAssessmentSectionEntityId > 0 && !modifiedList.Contains(e)); + dikeAssessmentSectionSet.RemoveRange(untouchedModifiedList); + + modifiedList.Clear(); + } + + /// + /// Perform actions that can only be executed after has been called. + /// + public void PerformPostSaveActions() + { + UpdateStorageIdsInModel(); + + dikePipingFailureMechanismEntityPersistor.PerformPostSaveActions(); + hydraulicLocationEntityPersistor.PerformPostSaveActions(); + } + + /// + /// Updates the children of , in reference to , in the storage. + /// + /// The of which children need to be updated. + /// Referenced . + private void UpdateChildren(DikeAssessmentSection model, DikeAssessmentSectionEntity entity) + { + dikePipingFailureMechanismEntityPersistor.UpdateModel(entity.FailureMechanismEntities, model.PipingFailureMechanism); + dikePipingFailureMechanismEntityPersistor.RemoveUnModifiedEntries(entity.FailureMechanismEntities); + + hydraulicLocationEntityPersistor.UpdateModel(entity.HydraulicLocationEntities, model.HydraulicBoundaryDatabase); + referenceLinePersistor.InsertModel(entity.ReferenceLinePointEntities, model.ReferenceLine); + } + + /// + /// Inserts the children of , in reference to , in the storage. + /// + /// The of which children need to be inserted. + /// Referenced . + private void InsertChildren(DikeAssessmentSection model, DikeAssessmentSectionEntity entity) + { + dikePipingFailureMechanismEntityPersistor.InsertModel(entity.FailureMechanismEntities, model.PipingFailureMechanism); + dikePipingFailureMechanismEntityPersistor.RemoveUnModifiedEntries(entity.FailureMechanismEntities); + + hydraulicLocationEntityPersistor.InsertModel(entity.HydraulicLocationEntities, model.HydraulicBoundaryDatabase); + referenceLinePersistor.InsertModel(entity.ReferenceLinePointEntities, model.ReferenceLine); + } + + /// + /// Performs the update of to . + /// + /// to update. + /// Collection where the can be found. + /// Value used for sorting. + /// The to . + private DikeAssessmentSectionEntity PerformUpdateModel(DikeAssessmentSection model, ICollection parentNavigationProperty, int order) + { + DikeAssessmentSectionEntity entity; + try + { + entity = parentNavigationProperty.SingleOrDefault(db => db.DikeAssessmentSectionEntityId == model.StorageId); + } + catch (InvalidOperationException exception) + { + throw new EntityNotFoundException(String.Format(Resources.Error_Entity_Not_Found_0_1, "DikeAssessmentSectionEntity", model.StorageId), exception); + } + if (entity == null) + { + throw new EntityNotFoundException(String.Format(Resources.Error_Entity_Not_Found_0_1, "DikeAssessmentSectionEntity", model.StorageId)); + } + + modifiedList.Add(entity); + + converter.ConvertModelToEntity(model, entity); + entity.Order = order; + + return entity; + } + + /// + /// Inserts the as in . + /// + /// to be added. + /// Collection where to add the as . + /// Value used for sorting. + /// The added as . + /// Thrown when is null. + /// Thrown when the is read-only. + private DikeAssessmentSectionEntity PerformInsertModel(DikeAssessmentSection model, ICollection parentNavigationProperty, int order) + { + var entity = new DikeAssessmentSectionEntity(); + parentNavigationProperty.Add(entity); + insertedList.Add(entity, model); + + converter.ConvertModelToEntity(model, entity); + entity.Order = order; + + if (model.StorageId > 0) + { + modifiedList.Add(entity); + } + + return entity; + } + + /// + /// Updates the StorageId of each inserted to the DikeAssessmentSectionEntityId of the corresponding . + /// + /// must have been called to update the ids. + private void UpdateStorageIdsInModel() + { + foreach (var entry in insertedList) + { + Debug.Assert(entry.Key.DikeAssessmentSectionEntityId > 0, "DikeAssessmentSectionEntityId is not set. Have you called IRingtoetsEntities.SaveChanges?"); + entry.Value.StorageId = entry.Key.DikeAssessmentSectionEntityId; + } + insertedList.Clear(); + } + } +} \ No newline at end of file Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/DikesPipingFailureMechanismEntityPersistor.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/FailureMechanismEntityPersistorBase.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/FailureMechanismPersistorBase.cs =================================================================== diff -u --- Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/FailureMechanismPersistorBase.cs (revision 0) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/FailureMechanismPersistorBase.cs (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -0,0 +1,188 @@ +// Copyright (C) Stichting Deltares 2016. 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.Data.Entity; +using System.Linq; +using Application.Ringtoets.Storage.Converters; +using Application.Ringtoets.Storage.DbContext; +using Application.Ringtoets.Storage.Exceptions; +using Application.Ringtoets.Storage.Properties; +using Ringtoets.Common.Data; + +namespace Application.Ringtoets.Storage.Persistors +{ + /// + /// Persistor for classes derived from . + /// + public abstract class FailureMechanismPersistorBase where T : IFailureMechanism + { + private readonly DbSet failureMechanismSet; + private readonly Dictionary insertedList = new Dictionary(); + private readonly ICollection modifiedList = new List(); + + private readonly IEntityConverter converter; + + /// + /// New instance of . + /// + /// The storage context. + /// An implementation of the to use in the persistor. + /// Thrown when is null. + protected FailureMechanismPersistorBase(IRingtoetsEntities ringtoetsContext, IEntityConverter converter) + { + if (ringtoetsContext == null) + { + throw new ArgumentNullException("ringtoetsContext"); + } + failureMechanismSet = ringtoetsContext.FailureMechanismEntities; + this.converter = converter; + } + + /// + /// Loads the as . + /// + /// to load from. + /// The to load data in. + /// Thrown when: + /// is null. + /// is null. + /// + public void LoadModel(FailureMechanismEntity entity, IFailureMechanism failureMechanism) + { + if (entity == null) + { + throw new ArgumentNullException("entity"); + } + if (failureMechanism == null) + { + throw new ArgumentNullException("failureMechanism"); + } + + var model = converter.ConvertEntityToModel(entity); + + failureMechanism.StorageId = model.StorageId; + } + + /// + /// Ensures that the is set as in the . + /// + /// Collection where objects can be searched and added. Usually, this collection is a navigation property of a . + /// to be saved in the storage. + /// Thrown when: + /// is null. + /// is null. + /// + /// Thrown when the is read-only. + /// Thrown when the storageId of > 0 and: + /// More than one element found in that should have been unique. + /// No such element exists in . + /// + public void UpdateModel(ICollection parentNavigationProperty, T model) + { + if (model == null) + { + throw new ArgumentNullException("model"); + } + if (parentNavigationProperty == null) + { + throw new ArgumentNullException("parentNavigationProperty"); + } + + if (model.StorageId < 1) + { + InsertModel(parentNavigationProperty, model); + return; + } + FailureMechanismEntity entity; + try + { + entity = parentNavigationProperty.SingleOrDefault(db => db.FailureMechanismEntityId == model.StorageId); + } + catch (InvalidOperationException exception) + { + throw new EntityNotFoundException(String.Format(Resources.Error_Entity_Not_Found_0_1, "FailureMechanismEntity", model.StorageId), exception); + } + if (entity == null) + { + throw new EntityNotFoundException(String.Format(Resources.Error_Entity_Not_Found_0_1, "FailureMechanismEntity", model.StorageId)); + } + + modifiedList.Add(entity); + + converter.ConvertModelToEntity(model, entity); + } + + /// + /// Ensures that the model is added as in the . + /// + /// Collection where objects can be added. Usually, this collection is a navigation property of a . + /// to be saved in the storage. + /// Thrown when: + /// is null. + /// is null. + /// + public void InsertModel(ICollection parentNavigationProperty, T model) + { + if (parentNavigationProperty == null) + { + throw new ArgumentNullException("parentNavigationProperty"); + } + + var entity = new FailureMechanismEntity(); + parentNavigationProperty.Add(entity); + insertedList.Add(entity, model); + + converter.ConvertModelToEntity(model, entity); + + if (model.StorageId > 0) + { + modifiedList.Add(entity); + } + } + + /// + /// All unmodified in will be removed. + /// + /// List where objects can be searched. Usually, this collection is a navigation property of a . + /// Thrown when the is read-only. + public void RemoveUnModifiedEntries(ICollection parentNavigationProperty) + { + var untouchedModifiedList = parentNavigationProperty.Where(e => e.FailureMechanismEntityId > 0 && !modifiedList.Contains(e)); + failureMechanismSet.RemoveRange(untouchedModifiedList); + + modifiedList.Clear(); + } + + /// + /// Perform actions that can only be executed after has been called. + /// + public void PerformPostSaveActions() + { + foreach (var entry in insertedList) + { + entry.Value.StorageId = entry.Key.FailureMechanismEntityId; + } + insertedList.Clear(); + } + } +} \ No newline at end of file Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/HydraulicBoundaryLocationPersistor.cs =================================================================== diff -u --- Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/HydraulicBoundaryLocationPersistor.cs (revision 0) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/HydraulicBoundaryLocationPersistor.cs (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -0,0 +1,207 @@ +// Copyright (C) Stichting Deltares 2016. 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.Data.Entity; +using System.Linq; +using Application.Ringtoets.Storage.Converters; +using Application.Ringtoets.Storage.DbContext; +using Application.Ringtoets.Storage.Exceptions; +using Application.Ringtoets.Storage.Properties; +using Ringtoets.HydraRing.Data; + +namespace Application.Ringtoets.Storage.Persistors +{ + /// + /// The persistor for . + /// + public class HydraulicBoundaryLocationPersistor + { + private readonly DbSet hydraulicLocationSet; + private readonly HydraulicLocationConverter converter; + + private readonly Dictionary insertedList = new Dictionary(); + private readonly ICollection modifiedList = new List(); + + /// + /// Creates a new instance of . + /// + /// The storage context. + /// Thrown when is null. + public HydraulicBoundaryLocationPersistor(IRingtoetsEntities ringtoetsContext) + { + if (ringtoetsContext == null) + { + throw new ArgumentNullException("ringtoetsContext"); + } + + hydraulicLocationSet = ringtoetsContext.HydraulicLocationEntities; + + converter = new HydraulicLocationConverter(); + } + + /// + /// Loads the as . + /// + /// The to load. + /// A new instance of , based on the properties of . + /// Thrown when is null. + public IEnumerable LoadModel(ICollection entities) + { + if (entities == null) + { + throw new ArgumentNullException("entities"); + } + + return converter.ConvertEntityToModel(entities); + } + + /// + /// Ensures that the is set as in the . + /// + /// Collection where objects can be searched and added. + /// Usually, this collection is a navigation property of a . + /// The to be saved in the storage. + /// Thrown when cannot be converted. + public void UpdateModel(ICollection parentNavigationProperty, HydraulicBoundaryDatabase model) + { + if (model == null) + { + return; + } + + if (parentNavigationProperty == null) + { + throw new ArgumentNullException("parentNavigationProperty"); + } + + foreach (var location in model.Locations) + { + if (location == null) + { + throw new ArgumentException("Null location cannot be added"); + } + + if (location.StorageId < 1) + { + InsertLocation(parentNavigationProperty, location); + continue; + } + + HydraulicLocationEntity entity; + try + { + entity = parentNavigationProperty.SingleOrDefault(db => db.HydraulicLocationEntityId == location.StorageId); + } + catch (InvalidOperationException exception) + { + throw new EntityNotFoundException(String.Format(Resources.Error_Entity_Not_Found_0_1, "HydraulicLocationEntity", location.StorageId), exception); + } + + if (entity == null) + { + throw new EntityNotFoundException(String.Format(Resources.Error_Entity_Not_Found_0_1, "HydraulicLocationEntity", location.StorageId)); + } + + modifiedList.Add(entity); + + converter.ConvertModelToEntity(location, entity); + } + + RemoveUnModifiedEntries(parentNavigationProperty); + } + + /// + /// Ensures that the is added as in the . + /// + /// Collection where objects can be added. + /// Usually, this collection is a navigation property of a . + /// The to be saved in the storage. + /// Thrown when cannot be converted. + public void InsertModel(ICollection parentNavigationProperty, HydraulicBoundaryDatabase hydraulicBoundaryDatabase) + { + if (parentNavigationProperty == null) + { + throw new ArgumentNullException("parentNavigationProperty"); + } + + if (hydraulicBoundaryDatabase == null) + { + return; + } + + foreach (var location in hydraulicBoundaryDatabase.Locations) + { + InsertLocation(parentNavigationProperty, location); + } + + RemoveUnModifiedEntries(parentNavigationProperty); + } + + /// + /// Perform actions that can only be executed after has been called. + /// + public void PerformPostSaveActions() + { + foreach (var entry in insertedList) + { + entry.Value.StorageId = entry.Key.HydraulicLocationEntityId; + } + insertedList.Clear(); + } + + /// + /// Removes all entities from that are not marked as 'updated'. + /// + /// List where objects can be searched. + /// Usually, this collection is a navigation property of a . + private void RemoveUnModifiedEntries(ICollection parentNavigationProperty) + { + var untouchedModifiedList = parentNavigationProperty.Where(e => e.HydraulicLocationEntityId > 0 && !modifiedList.Contains(e)); + hydraulicLocationSet.RemoveRange(untouchedModifiedList); + + modifiedList.Clear(); + } + + private void InsertLocation(ICollection parentNavigationProperty, HydraulicBoundaryLocation location) + { + if (location == null) + { + throw new ArgumentNullException("location"); + } + + var entity = new HydraulicLocationEntity(); + parentNavigationProperty.Add(entity); + + converter.ConvertModelToEntity(location, entity); + + if (location.StorageId > 0) + { + modifiedList.Add(entity); + } + else + { + insertedList.Add(entity, location); + } + } + } +} \ No newline at end of file Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/HydraulicLocationEntityPersistor.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 8dec462b18aff76313f2836309ef24ddd2f70b50 refers to a dead (removed) revision in file `Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/PipingFailureMechanismPersistor.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ProjectEntityPersistor.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ProjectPersistor.cs =================================================================== diff -u --- Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ProjectPersistor.cs (revision 0) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ProjectPersistor.cs (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -0,0 +1,229 @@ +// Copyright (C) Stichting Deltares 2016. 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.Data.Entity; +using System.Diagnostics; +using System.Linq; +using Application.Ringtoets.Storage.Converters; +using Application.Ringtoets.Storage.DbContext; +using Application.Ringtoets.Storage.Exceptions; +using Application.Ringtoets.Storage.Properties; +using Core.Common.Base.Data; +using Ringtoets.Integration.Data; + +namespace Application.Ringtoets.Storage.Persistors +{ + /// + /// Persistor for . + /// + public class ProjectPersistor + { + private readonly DbSet projectEntitySet; + private readonly ProjectConverter converter; + private readonly Dictionary insertedList = new Dictionary(); + private readonly ICollection modifiedList = new List(); + + private readonly DikeAssessmentSectionPersistor dikeAssessmentSectionEntityPersistor; + + /// + /// Instantiate a new . + /// + /// The storage context. + /// Thrown when is null. + public ProjectPersistor(IRingtoetsEntities ringtoetsContext) + { + if (ringtoetsContext == null) + { + throw new ArgumentNullException("ringtoetsContext"); + } + projectEntitySet = ringtoetsContext.ProjectEntities; + + converter = new ProjectConverter(); + + dikeAssessmentSectionEntityPersistor = new DikeAssessmentSectionPersistor(ringtoetsContext); + } + + /// + /// Gets the only as from the sequence. + /// + /// A new , loaded from the sequence, or null when not found. + /// Thrown when there are more than one elements in the sequence. + public Project GetEntityAsModel() + { + var entry = projectEntitySet.SingleOrDefault(); + if (entry == null) + { + return null; + } + var project = converter.ConvertEntityToModel(entry); + + var nrOfItems = entry.DikeAssessmentSectionEntities.Count; + var assessmentSections = new object[nrOfItems]; + + foreach (var sectionEntity in entry.DikeAssessmentSectionEntities) + { + assessmentSections[sectionEntity.Order] = dikeAssessmentSectionEntityPersistor.LoadModel(sectionEntity); + } + + // Add to items sorted + foreach (var assessmentSection in assessmentSections) + { + project.Items.Add(assessmentSection); + } + + return project; + } + + /// + /// Insert the , based upon the , in the sequence. + /// + /// Execute afterwards to update the storage. + /// to be inserted in the sequence. + /// New instance of . + /// Thrown when is null. + /// The parentNavigationProperty is read-only. + public void InsertModel(Project project) + { + if (project == null) + { + throw new ArgumentNullException("project", @"Cannot update databaseSet when no project is set."); + } + + var entity = new ProjectEntity(); + projectEntitySet.Add(entity); + insertedList.Add(entity, project); + + converter.ConvertModelToEntity(project, entity); + + if (project.StorageId > 0) + { + modifiedList.Add(entity); + } + + InsertChildren(project, entity); + } + + /// + /// Updates the , based upon the , in the sequence. + /// + /// Execute afterwards to update the storage. + /// The to be saved in the storage. + /// The . + /// Thrown when is null. + /// Thrown when: + /// is not found. + /// More than one satisfies the condition in predicate. + /// + /// The parentNavigationProperty is read-only. + public void UpdateModel(Project model) + { + if (model == null) + { + throw new ArgumentNullException("model", @"Cannot update databaseSet when no project is set."); + } + ProjectEntity entity; + try + { + entity = projectEntitySet.SingleOrDefault(db => db.ProjectEntityId == model.StorageId); + } + catch (InvalidOperationException exception) + { + throw new EntityNotFoundException(String.Format(Resources.Error_Entity_Not_Found_0_1, "ProjectEntity", model.StorageId), exception); + } + if (entity == null) + { + throw new EntityNotFoundException(String.Format(Resources.Error_Entity_Not_Found_0_1, "ProjectEntity", model.StorageId)); + } + modifiedList.Add(entity); + converter.ConvertModelToEntity(model, entity); + + UpdateChildren(model, entity); + } + + /// + /// Removes all entities from that are not marked as 'updated'. + /// + public void RemoveUnModifiedEntries() + { + var untouchedList = projectEntitySet.ToList().Where(e => !modifiedList.Contains(e)); + projectEntitySet.RemoveRange(untouchedList); + + modifiedList.Clear(); + } + + /// + /// Perform actions that can only be executed after has been called. + /// + public void PerformPostSaveActions() + { + UpdateStorageIdsInModel(); + dikeAssessmentSectionEntityPersistor.PerformPostSaveActions(); + } + + /// + /// Updates the children of , in reference to , in the storage. + /// + /// The of which children need to be updated. + /// Referenced . + private void UpdateChildren(Project project, ProjectEntity entity) + { + var order = 0; + foreach (var item in project.Items.Where(i => i is DikeAssessmentSection).Cast()) + { + dikeAssessmentSectionEntityPersistor.UpdateModel(entity.DikeAssessmentSectionEntities, item, order); + order++; + } + dikeAssessmentSectionEntityPersistor.RemoveUnModifiedEntries(entity.DikeAssessmentSectionEntities); + } + + /// + /// Inserts the children of , in reference to , in the storage. + /// + /// The of which children need to be inserted. + /// Referenced . + private void InsertChildren(Project project, ProjectEntity entity) + { + var order = 0; + foreach (var item in project.Items.Where(i => i is DikeAssessmentSection).Cast()) + { + dikeAssessmentSectionEntityPersistor.InsertModel(entity.DikeAssessmentSectionEntities, item, order); + order++; + } + dikeAssessmentSectionEntityPersistor.RemoveUnModifiedEntries(entity.DikeAssessmentSectionEntities); + } + + /// + /// Updates the StorageId of each inserted to the DikeAssessmentSectionEntityId of the corresponding . + /// + /// must have been called to update the ids. + private void UpdateStorageIdsInModel() + { + foreach (var entry in insertedList) + { + Debug.Assert(entry.Key.ProjectEntityId > 0, "ProjectEntityId is not set. Have you called IRingtoetsEntities.SaveChanges?"); + entry.Value.StorageId = entry.Key.ProjectEntityId; + } + insertedList.Clear(); + } + } +} \ No newline at end of file Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ReferenceLinePersistor.cs =================================================================== diff -u -r5b63cfab474523f97be999403eb4906a0c376a3d -rbbe8871cfabad25988ec8f94115da2c485267118 --- Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ReferenceLinePersistor.cs (.../ReferenceLinePersistor.cs) (revision 5b63cfab474523f97be999403eb4906a0c376a3d) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ReferenceLinePersistor.cs (.../ReferenceLinePersistor.cs) (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -1,4 +1,25 @@ -using System; +// Copyright (C) Stichting Deltares 2016. 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.Data.Entity; using System.Linq; @@ -8,13 +29,16 @@ namespace Application.Ringtoets.Storage.Persistors { + /// + /// Persistor for . + /// public class ReferenceLinePersistor { private readonly ReferenceLineConverter converter; private readonly DbSet referenceLineEntities; /// - /// Creates a new instance of . + /// Creates a new instance of . /// /// The storage context. /// Thrown when is null. Index: Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs =================================================================== diff -u -rf8ff9c1004791467d458ed2818f03be0b9e86552 -rbbe8871cfabad25988ec8f94115da2c485267118 --- Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs (.../StorageSqLite.cs) (revision f8ff9c1004791467d458ed2818f03be0b9e86552) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs (.../StorageSqLite.cs) (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -72,7 +72,7 @@ SetConnectionToNewFile(databaseFilePath); using (var dbContext = new RingtoetsEntities(connectionString)) { - var projectEntityPersistor = new ProjectEntityPersistor(dbContext); + var projectEntityPersistor = new ProjectPersistor(dbContext); try { projectEntityPersistor.InsertModel(project); @@ -125,7 +125,7 @@ } using (var dbContext = new RingtoetsEntities(connectionString)) { - var projectEntityPersistor = new ProjectEntityPersistor(dbContext); + var projectEntityPersistor = new ProjectPersistor(dbContext); try { projectEntityPersistor.UpdateModel(project); @@ -174,7 +174,7 @@ { using (var dbContext = new RingtoetsEntities(connectionString)) { - var projectEntityPersistor = new ProjectEntityPersistor(dbContext); + var projectEntityPersistor = new ProjectPersistor(dbContext); var project = projectEntityPersistor.GetEntityAsModel(); project.Name = Path.GetFileNameWithoutExtension(databaseFilePath); @@ -200,7 +200,7 @@ using (var dbContext = new RingtoetsEntities(connectionString)) { - var projectEntityPersistor = new ProjectEntityPersistor(dbContext); + var projectEntityPersistor = new ProjectPersistor(dbContext); try { Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj =================================================================== diff -u -r5b63cfab474523f97be999403eb4906a0c376a3d -rbbe8871cfabad25988ec8f94115da2c485267118 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj (.../Application.Ringtoets.Storage.Test.csproj) (revision 5b63cfab474523f97be999403eb4906a0c376a3d) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj (.../Application.Ringtoets.Storage.Test.csproj) (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -82,18 +82,18 @@ - + - - - + + + - - - + + + Code Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/DikeAssessmentSectionConverterTest.cs =================================================================== diff -u --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/DikeAssessmentSectionConverterTest.cs (revision 0) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/DikeAssessmentSectionConverterTest.cs (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -0,0 +1,161 @@ +// Copyright (C) Stichting Deltares 2016. 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 Application.Ringtoets.Storage.Converters; +using Application.Ringtoets.Storage.DbContext; +using NUnit.Framework; +using Ringtoets.HydraRing.Data; +using Ringtoets.Integration.Data; + +namespace Application.Ringtoets.Storage.Test.Converters +{ + [TestFixture] + public class DikeAssessmentSectionConverterTest + { + [Test] + public void DefaultConstructor_Always_NewDikeAssessmentSectionEntityConverter() + { + // Call + DikeAssessmentSectionConverter converter = new DikeAssessmentSectionConverter(); + + // Assert + Assert.IsInstanceOf>(converter); + } + + [Test] + public void ConvertEntityToModel_NullEntity_ThrowsArgumentNullException() + { + // Setup + DikeAssessmentSectionConverter converter = new DikeAssessmentSectionConverter(); + + // Call + TestDelegate test = () => converter.ConvertEntityToModel(null); + + // Assert + Assert.Throws(test); + } + + [Test] + public void ConvertEntityToModel_ValidDikeAssessmentSectionEntity_ReturnsTheDikeAssessmentSectionEntityAsDikeAssessmentSection() + { + // Setup + const long storageId = 1234L; + const long projectId = 1L; + const int norm = 30000; + const string name = "test"; + const string hydraulicDatabaseVersion = "1.0"; + const string hydraulicDatabasePath = "testPath"; + DikeAssessmentSectionEntity dikeAssessmentSectionEntity = new DikeAssessmentSectionEntity() + { + DikeAssessmentSectionEntityId = storageId, + Name = name, + ProjectEntityId = projectId, + Norm = norm, + HydraulicDatabaseVersion = hydraulicDatabaseVersion, + HydraulicDatabaseLocation = hydraulicDatabasePath + }; + DikeAssessmentSectionConverter converter = new DikeAssessmentSectionConverter(); + + // Call + DikeAssessmentSection assessmentSection = converter.ConvertEntityToModel(dikeAssessmentSectionEntity); + + // Assert + Assert.AreNotEqual(dikeAssessmentSectionEntity, assessmentSection); + Assert.AreEqual(storageId, assessmentSection.StorageId); + Assert.AreEqual(name, assessmentSection.Name); + Assert.AreEqual(norm, assessmentSection.FailureMechanismContribution.Norm); + Assert.AreEqual(hydraulicDatabaseVersion, assessmentSection.HydraulicBoundaryDatabase.Version); + Assert.AreEqual(hydraulicDatabasePath, assessmentSection.HydraulicBoundaryDatabase.FilePath); + } + + [Test] + public void ConvertModelToEntity_NullEntity_ThrowsArgumentNullException() + { + // Setup + DikeAssessmentSectionConverter converter = new DikeAssessmentSectionConverter(); + DikeAssessmentSection dikeAssessmentSection = new DikeAssessmentSection(); + + // Call + TestDelegate test = () => converter.ConvertModelToEntity(dikeAssessmentSection, null); + + // Assert + Assert.Throws(test); + } + + [Test] + public void ConvertModelToEntity_NullModel_ThrowsArgumentNullException() + { + // Setup + DikeAssessmentSectionConverter converter = new DikeAssessmentSectionConverter(); + DikeAssessmentSectionEntity dikeAssessmentSectionEntity = new DikeAssessmentSectionEntity(); + + // Call + TestDelegate test = () => converter.ConvertModelToEntity(null, dikeAssessmentSectionEntity); + + // Assert + Assert.Throws(test); + } + + [Test] + public void ConvertModelToEntity_ValidDikeAssessmentSection_UpdatesTheDikeAssessmentSectionAsDikeAssessmentSectionEntity() + { + // Setup + const long storageId = 1234L; + const long projectId = 1L; + const int norm = 30000; + const string name = "test"; + const string hydraulicDatabaseVersion = "1.0"; + const string hydraulicDatabasePath = "testPath"; + DikeAssessmentSection dikeAssessmentSection = new DikeAssessmentSection + { + StorageId = storageId, + Name = name, + FailureMechanismContribution = + { + Norm = norm + }, + HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase + { + Version = hydraulicDatabaseVersion, + FilePath = hydraulicDatabasePath + } + }; + DikeAssessmentSectionEntity dikeAssessmentSectionEntity = new DikeAssessmentSectionEntity + { + ProjectEntityId = projectId + }; + DikeAssessmentSectionConverter converter = new DikeAssessmentSectionConverter(); + + // Call + converter.ConvertModelToEntity(dikeAssessmentSection, dikeAssessmentSectionEntity); + + // Assert + Assert.AreNotEqual(dikeAssessmentSectionEntity, dikeAssessmentSection); + Assert.AreEqual(storageId, dikeAssessmentSectionEntity.DikeAssessmentSectionEntityId); + Assert.AreEqual(projectId, dikeAssessmentSectionEntity.ProjectEntityId); + Assert.AreEqual(name, dikeAssessmentSectionEntity.Name); + Assert.AreEqual(norm, dikeAssessmentSectionEntity.Norm); + Assert.AreEqual(hydraulicDatabaseVersion, dikeAssessmentSectionEntity.HydraulicDatabaseVersion); + Assert.AreEqual(hydraulicDatabasePath, dikeAssessmentSectionEntity.HydraulicDatabaseLocation); + } + } +} \ No newline at end of file Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/DikeAssessmentSectionEntityConverterTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/PipingFailureMechanismConverterTest.cs =================================================================== diff -u --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/PipingFailureMechanismConverterTest.cs (revision 0) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/PipingFailureMechanismConverterTest.cs (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -0,0 +1,152 @@ +// Copyright (C) Stichting Deltares 2016. 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 Application.Ringtoets.Storage.Converters; +using Application.Ringtoets.Storage.DbContext; +using NUnit.Framework; +using Rhino.Mocks; +using Ringtoets.Common.Data; +using Ringtoets.Piping.Data; + +namespace Application.Ringtoets.Storage.Test.Converters +{ + [TestFixture] + public class PipingFailureMechanismConverterTest + { + [Test] + public void DefaultConstructor_Always_NewFailureMechanismEntityConverter() + { + // Call + PipingFailureMechanismConverter converter = new PipingFailureMechanismConverter(); + + // Assert + Assert.IsInstanceOf>(converter); + } + + [Test] + public void ConvertEntityToModel_NullEntity_ThrowsArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var failureMechanism = mocks.Stub(); + PipingFailureMechanismConverter converter = new PipingFailureMechanismConverter(); + mocks.ReplayAll(); + + // Call + TestDelegate test = () => converter.ConvertEntityToModel(null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("entity", exception.ParamName); + mocks.VerifyAll(); + } + + [Test] + public void ConvertEntityToModel_ValidEntityValidModel_ReturnsEntityAsModel() + { + // Setup + PipingFailureMechanismConverter converter = new PipingFailureMechanismConverter(); + + const long storageId = 1234L; + FailureMechanismEntity entity = new FailureMechanismEntity + { + FailureMechanismEntityId = storageId, + FailureMechanismType = (int) FailureMechanismType.DikesPipingFailureMechanism, + }; + + // Call + PipingFailureMechanism failureMechanism = converter.ConvertEntityToModel(entity); + + // Assert + Assert.AreEqual(entity.FailureMechanismEntityId, failureMechanism.StorageId); + } + + [Test] + public void ConvertEntityToModel_EntityWithIncorrectType_ThrowsArgumentException() + { + // Setup + PipingFailureMechanismConverter converter = new PipingFailureMechanismConverter(); + + const long storageId = 1234L; + FailureMechanismEntity entity = new FailureMechanismEntity + { + FailureMechanismEntityId = storageId, + FailureMechanismType = (int) FailureMechanismType.DikesMacrostabilityInwardsFailureMechanism, + }; + + // Call + TestDelegate test = () => converter.ConvertEntityToModel(entity); + + // Assert + Assert.Throws(test); + } + + [Test] + public void ConvertModelToEntity_NullModel_ThrowsArgumentNullException() + { + // Setup + PipingFailureMechanismConverter converter = new PipingFailureMechanismConverter(); + + // Call + TestDelegate test = () => converter.ConvertModelToEntity(null, new FailureMechanismEntity()); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("modelObject", exception.ParamName); + } + + [Test] + public void ConvertModelToEntity_NullEntity_ThrowsArgumentNullException() + { + // Setup + PipingFailureMechanismConverter converter = new PipingFailureMechanismConverter(); + + // Call + TestDelegate test = () => converter.ConvertModelToEntity(new PipingFailureMechanism(), null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("entity", exception.ParamName); + } + + [Test] + public void ConvertModelToEntity_ValidModelValidEntity_ReturnsModelAsEntity() + { + // Setup + PipingFailureMechanismConverter converter = new PipingFailureMechanismConverter(); + + const long storageId = 1234L; + var entity = new FailureMechanismEntity(); + + var model = new PipingFailureMechanism + { + StorageId = storageId + }; + + // Call + converter.ConvertModelToEntity(model, entity); + + // Assert + Assert.AreEqual(model.StorageId, entity.FailureMechanismEntityId); + } + } +} \ No newline at end of file Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/PipingFailureMechanismEntityConverterTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/ProjectConverterTest.cs =================================================================== diff -u --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/ProjectConverterTest.cs (revision 0) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/ProjectConverterTest.cs (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -0,0 +1,129 @@ +// Copyright (C) Stichting Deltares 2016. 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 Application.Ringtoets.Storage.Converters; +using Application.Ringtoets.Storage.DbContext; +using Core.Common.Base.Data; +using NUnit.Framework; + +namespace Application.Ringtoets.Storage.Test.Converters +{ + [TestFixture] + public class ProjectConverterTest + { + [Test] + public void DefaultConstructor_Always_NewProjectConverter() + { + // Call + ProjectConverter converter = new ProjectConverter(); + + // Assert + Assert.IsInstanceOf>(converter); + } + + [Test] + public void ConvertEntityToModel_NullEntity_ThrowsArgumentNullException() + { + // SetUp + ProjectConverter converter = new ProjectConverter(); + + // Call + TestDelegate test = () => converter.ConvertEntityToModel(null); + + // Assert + Assert.Throws(test); + } + + [Test] + public void ConvertEntityToModel_ValidProjectEntity_ReturnsTheProjectEntityAsProject() + { + // SetUp + const long storageId = 1234L; + const string description = "Description"; + ProjectEntity projectEntity = new ProjectEntity() + { + ProjectEntityId = storageId, + Description = description + }; + ProjectConverter converter = new ProjectConverter(); + + // Call + Project project = converter.ConvertEntityToModel(projectEntity); + + // Assert + Assert.AreNotEqual(projectEntity, project); + Assert.AreEqual(storageId, project.StorageId); + Assert.AreEqual(description, project.Description); + } + + [Test] + public void ConvertModelToEntity_NullEntity_ThrowsArgumentNullException() + { + // SetUp + ProjectConverter converter = new ProjectConverter(); + Project project = new Project(); + + // Call + TestDelegate test = () => converter.ConvertModelToEntity(project, null); + + // Assert + Assert.Throws(test); + } + + [Test] + public void ConvertModelToEntity_NullModel_ThrowsArgumentNullException() + { + // SetUp + ProjectConverter converter = new ProjectConverter(); + ProjectEntity projectEntity = new ProjectEntity(); + + // Call + TestDelegate test = () => converter.ConvertModelToEntity(null, projectEntity); + + // Assert + Assert.Throws(test); + } + + [Test] + public void ConvertModelToEntity_ValidProject_UpdatesTheProjectAsProjectEntity() + { + // SetUp + const long storageId = 1234L; + const string description = "Description"; + Project project = new Project + { + StorageId = storageId, + Description = description + }; + ProjectEntity projectEntity = new ProjectEntity(); + ProjectConverter converter = new ProjectConverter(); + + // Call + converter.ConvertModelToEntity(project, projectEntity); + + // Assert + Assert.AreNotEqual(projectEntity, project); + Assert.AreEqual(storageId, projectEntity.ProjectEntityId); + Assert.AreEqual(description, projectEntity.Description); + } + } +} \ No newline at end of file Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/ProjectEntityConverterTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/DikeAssessmentSectionEntityPersistorTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/DikeAssessmentSectionPersistorTest.cs =================================================================== diff -u --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/DikeAssessmentSectionPersistorTest.cs (revision 0) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/DikeAssessmentSectionPersistorTest.cs (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -0,0 +1,947 @@ +// Copyright (C) Stichting Deltares 2016. 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.Collections.ObjectModel; +using System.Linq; +using Application.Ringtoets.Storage.DbContext; +using Application.Ringtoets.Storage.Exceptions; +using Application.Ringtoets.Storage.Persistors; +using Application.Ringtoets.Storage.TestUtil; +using NUnit.Framework; +using Rhino.Mocks; +using Ringtoets.HydraRing.Data; +using Ringtoets.Integration.Data; + +namespace Application.Ringtoets.Storage.Test.Persistors +{ + [TestFixture] + public class DikeAssessmentSectionPersistorTest + { + private MockRepository mockRepository; + + [SetUp] + public void SetUp() + { + mockRepository = new MockRepository(); + } + + [Test] + [ExpectedException(typeof(ArgumentNullException))] + public void Constructor_NullDataSet_ThrowsArgumentNullException() + { + // Call + DikeAssessmentSectionPersistor p = new DikeAssessmentSectionPersistor(null); + } + + [Test] + public void Constructor_EmptyDataset_NewInstance() + { + // Setup + var ringtoetsEntities = mockRepository.Stub(); + mockRepository.ReplayAll(); + + // Call + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + + // Assert + Assert.IsInstanceOf(persistor); + + mockRepository.VerifyAll(); + } + + [Test] + public void LoadModel_NullEntity_ThrowsArgumentNullException() + { + // Setup + var ringtoetsEntities = mockRepository.Stub(); + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + mockRepository.ReplayAll(); + + // Call + TestDelegate test = () => persistor.LoadModel(null); + + // Assert + Assert.Throws(test); + + mockRepository.VerifyAll(); + } + + [Test] + public void LoadModel_ValidEntity_EntityAsModel() + { + // Setup + const long storageId = 1234L; + const string name = "test"; + const int norm = 30000; + const long pipingFailureMechanismStorageId = 1L; + + const string hydraulicDatabaseVersion = "1.0"; + const string hydraulicDatabasePath = "/temp/test"; + const string hydraulicDatabaseLocationName = "test"; + const double hydraulicDatabaseLocationDesignWaterLevel = 15.6; + const long hydraulicDatabaseLocationLocationId = 1300001; + const long hydraulicDatabaseLocationStorageId = 1234L; + const decimal hydraulicDatabaseLocationX = 253; + const decimal hydraulicDatabaseLocationY = 123; + + const string otherHydraulicDatabaseLocationName = "test2"; + const double otherHydraulicDatabaseLocationDesignWaterLevel = 18.6; + const long otherHydraulicDatabaseLocationLocationId = 1300005; + const long otherHydraulicDatabaseLocationStorageId = 4321L; + const decimal otherHydraulicDatabaseLocationX = 3927; + const decimal otherHydraulicDatabaseLocationY = 372; + + var ringtoetsEntities = mockRepository.Stub(); + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + var entity = new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = storageId, Name = name, Norm = norm, FailureMechanismEntities = new List + { + new FailureMechanismEntity + { + FailureMechanismType = (int) FailureMechanismType.DikesPipingFailureMechanism, FailureMechanismEntityId = pipingFailureMechanismStorageId + } + }, + HydraulicDatabaseVersion = hydraulicDatabaseVersion, HydraulicDatabaseLocation = hydraulicDatabasePath, + HydraulicLocationEntities = new List + { + new HydraulicLocationEntity + { + Name = hydraulicDatabaseLocationName, DesignWaterLevel = hydraulicDatabaseLocationDesignWaterLevel, HydraulicLocationEntityId = hydraulicDatabaseLocationStorageId, + LocationId = hydraulicDatabaseLocationLocationId, LocationX = hydraulicDatabaseLocationX, LocationY = hydraulicDatabaseLocationY, + }, + new HydraulicLocationEntity + { + Name = otherHydraulicDatabaseLocationName, DesignWaterLevel = otherHydraulicDatabaseLocationDesignWaterLevel, HydraulicLocationEntityId = otherHydraulicDatabaseLocationStorageId, + LocationId = otherHydraulicDatabaseLocationLocationId, LocationX = otherHydraulicDatabaseLocationX, LocationY = otherHydraulicDatabaseLocationY, + } + } + }; + mockRepository.ReplayAll(); + + // Call + DikeAssessmentSection section = persistor.LoadModel(entity); + + // Assert + Assert.AreEqual(storageId, section.StorageId); + Assert.AreEqual(name, section.Name); + Assert.AreEqual(norm, section.FailureMechanismContribution.Norm); + Assert.AreEqual(pipingFailureMechanismStorageId, section.PipingFailureMechanism.StorageId); + Assert.AreEqual(hydraulicDatabaseVersion, section.HydraulicBoundaryDatabase.Version); + Assert.AreEqual(hydraulicDatabasePath, section.HydraulicBoundaryDatabase.FilePath); + Assert.AreEqual(2, section.HydraulicBoundaryDatabase.Locations.Count); + + var firstLocation = section.HydraulicBoundaryDatabase.Locations.First(); + Assert.AreEqual(hydraulicDatabaseLocationName, firstLocation.Name); + Assert.AreEqual(hydraulicDatabaseLocationDesignWaterLevel, firstLocation.DesignWaterLevel); + Assert.AreEqual(hydraulicDatabaseLocationStorageId, firstLocation.StorageId); + Assert.AreEqual(hydraulicDatabaseLocationLocationId, firstLocation.Id); + Assert.AreEqual(hydraulicDatabaseLocationX, firstLocation.Location.X); + Assert.AreEqual(hydraulicDatabaseLocationY, firstLocation.Location.Y); + + var secondLocation = section.HydraulicBoundaryDatabase.Locations.ElementAt(1); + Assert.AreEqual(otherHydraulicDatabaseLocationName, secondLocation.Name); + Assert.AreEqual(otherHydraulicDatabaseLocationDesignWaterLevel, secondLocation.DesignWaterLevel); + Assert.AreEqual(otherHydraulicDatabaseLocationStorageId, secondLocation.StorageId); + Assert.AreEqual(otherHydraulicDatabaseLocationLocationId, secondLocation.Id); + Assert.AreEqual(otherHydraulicDatabaseLocationX, secondLocation.Location.X); + Assert.AreEqual(otherHydraulicDatabaseLocationY, secondLocation.Location.Y); + + mockRepository.VerifyAll(); + } + + [Test] + public void LoadModel_MultipleEntitiesInDataset_EntitiesAsModel() + { + // Setup + var ringtoetsEntities = mockRepository.Stub(); + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + ICollection parentNavigationProperty = new List + { + new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = 1, Name = "test1", Norm = 12, + HydraulicDatabaseVersion = "1.0", HydraulicDatabaseLocation = "temp/test", + HydraulicLocationEntities = new List + { + new HydraulicLocationEntity + { + Name = "test", DesignWaterLevel = 15.6, HydraulicLocationEntityId = 1234L, LocationId = 1300001, LocationX = 253, LocationY = 123 + } + } + }, + new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = 2, Name = "test2", Norm = 22, + HydraulicDatabaseVersion = "2.0", HydraulicDatabaseLocation = "test", + HydraulicLocationEntities = new List + { + new HydraulicLocationEntity + { + Name = "test2", DesignWaterLevel = 135.6, HydraulicLocationEntityId = 134L, LocationId = 1400001, LocationX = 23, LocationY = 23 + } + } + } + }; + mockRepository.ReplayAll(); + + // Call + var loadedModels = parentNavigationProperty.Select(entity => persistor.LoadModel(entity)); + + // Assert + var parentNavigationPropertyList = parentNavigationProperty.ToList(); + var loadedModelsList = loadedModels.ToList(); + Assert.AreEqual(parentNavigationPropertyList.Count, loadedModelsList.Count); + for (var i = 0; i < loadedModelsList.Count; i++) + { + Assert.AreEqual(parentNavigationPropertyList[i].DikeAssessmentSectionEntityId, loadedModelsList[i].StorageId); + Assert.AreEqual(parentNavigationPropertyList[i].Name, loadedModelsList[i].Name); + Assert.AreEqual(parentNavigationPropertyList[i].Norm, loadedModelsList[i].FailureMechanismContribution.Norm); + Assert.AreEqual(parentNavigationPropertyList[i].HydraulicDatabaseVersion, loadedModelsList[i].HydraulicBoundaryDatabase.Version); + Assert.AreEqual(parentNavigationPropertyList[i].HydraulicDatabaseLocation, loadedModelsList[i].HydraulicBoundaryDatabase.FilePath); + + var locations = parentNavigationPropertyList[i].HydraulicLocationEntities.ToList(); + + for (int j = 0; j < loadedModelsList[i].HydraulicBoundaryDatabase.Locations.Count; j++) + { + Assert.AreEqual(locations[j].HydraulicLocationEntityId, loadedModelsList[i].HydraulicBoundaryDatabase.Locations[j].StorageId); + Assert.AreEqual(locations[j].DesignWaterLevel, loadedModelsList[i].HydraulicBoundaryDatabase.Locations[j].DesignWaterLevel); + Assert.AreEqual(locations[j].Name, loadedModelsList[i].HydraulicBoundaryDatabase.Locations[j].Name); + Assert.AreEqual(locations[j].LocationId, loadedModelsList[i].HydraulicBoundaryDatabase.Locations[j].Id); + Assert.AreEqual(locations[j].LocationX, loadedModelsList[i].HydraulicBoundaryDatabase.Locations[j].Location.X); + Assert.AreEqual(locations[j].LocationY, loadedModelsList[i].HydraulicBoundaryDatabase.Locations[j].Location.Y); + } + } + + mockRepository.VerifyAll(); + } + + [Test] + public void InsertModel_NullParentNavigationProperty_ThrowsArgumentNullException() + { + // Setup + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + var dikeAssessmentSection = new DikeAssessmentSection(); + + // Call + TestDelegate test = () => persistor.InsertModel(null, dikeAssessmentSection, 0); + + // Assert + Assert.Throws(test); + } + + [Test] + public void InsertModel_NulldikeAssessmentSection_ThrowsArgumentNullException() + { + // Setup + var ringtoetsEntities = mockRepository.Stub(); + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + var parentNavigationProperty = mockRepository.StrictMock>(); + mockRepository.ReplayAll(); + + // Call + TestDelegate test = () => persistor.InsertModel(parentNavigationProperty, null, 0); + + // Assert + Assert.Throws(test); + + mockRepository.VerifyAll(); + } + + [Test] + public void InsertModel_EmptyParentNavigationPropertySingleDikeAssessmentSectionWithoutStorageId_DikeAssessmentSectionAsEntityInParentNavigationProperty() + { + // Setup + const string name = "test"; + const int norm = 30000; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ICollection parentNavigationProperty = new List(); + DikeAssessmentSection dikeAssessmentSection = + new DikeAssessmentSection + { + Name = name, + FailureMechanismContribution = + { + Norm = norm + }, + HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase() + }; + + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + + // Call + persistor.InsertModel(parentNavigationProperty, dikeAssessmentSection, 0); + + // Assert + Assert.AreEqual(1, parentNavigationProperty.Count); + var parentNavigationPropertyList = parentNavigationProperty.ToList(); + var entity = parentNavigationPropertyList[0]; + Assert.AreEqual(0, entity.DikeAssessmentSectionEntityId); + Assert.AreEqual(name, entity.Name); + Assert.AreEqual(norm, entity.Norm); + + mockRepository.VerifyAll(); + } + + [Test] + public void InsertModel_SingleEntityInParentNavigationPropertySingleDikeAssessmentSectionWithSameStorageId_DikeAssessmentSectionAsEntityInParentNavigationProperty() + { + // Setup + const string name = "test"; + const long storageId = 1234L; + const int norm = 30000; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + DikeAssessmentSectionEntity entityToDelete = new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = storageId, + Name = "Entity to delete" + }; + IList parentNavigationProperty = new List + { + entityToDelete + }; + + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + + DikeAssessmentSection dikeAssessmentSection = new DikeAssessmentSection + { + StorageId = storageId, + Name = name, + FailureMechanismContribution = + { + Norm = norm + } + }; + + // Call + persistor.InsertModel(parentNavigationProperty, dikeAssessmentSection, 0); + + // Assert + Assert.AreEqual(2, parentNavigationProperty.Count); + var parentNavigationPropertyList = parentNavigationProperty.ToList(); + var entity = parentNavigationPropertyList[1]; + Assert.AreEqual(storageId, entity.DikeAssessmentSectionEntityId); + Assert.AreEqual(name, entity.Name); + Assert.AreEqual(norm, entity.Norm); + + mockRepository.VerifyAll(); + } + + [Test] + public void InsertModel_ValidDikeAssessmentSectionWithChildren_InsertedDikeAssessmentSectionWithChildren() + { + // Setup + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + + const string name = "test"; + const double designWaterLevel = 15.6; + const long hydraulicLocationEntityId = 1234L; + const long locationId = 1300001; + const double locationX = 253; + const double locationY = 123; + + DikeAssessmentSection dikeAssessmentSection = new DikeAssessmentSection(); + dikeAssessmentSection.HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + dikeAssessmentSection.HydraulicBoundaryDatabase.Locations.Add(new HydraulicBoundaryLocation(locationId, name, locationX, locationY) + { + StorageId = hydraulicLocationEntityId, DesignWaterLevel = designWaterLevel + }); + + IList parentNavigationProperty = new List(); + + // Call + persistor.InsertModel(parentNavigationProperty, dikeAssessmentSection, 0); + + // Assert + Assert.AreEqual(1, parentNavigationProperty.Count); + var entity = parentNavigationProperty.ToList()[0]; + Assert.AreNotEqual(dikeAssessmentSection, entity); + + Assert.AreEqual(1, entity.FailureMechanismEntities.Count); + Assert.AreEqual(1, entity.FailureMechanismEntities.Count(db => db.FailureMechanismType.Equals((int) FailureMechanismType.DikesPipingFailureMechanism))); + Assert.AreEqual(1, entity.HydraulicLocationEntities.Count); + var locationEntity = entity.HydraulicLocationEntities.First(); + Assert.AreEqual(name, locationEntity.Name); + Assert.AreEqual(designWaterLevel, locationEntity.DesignWaterLevel); + Assert.AreEqual(hydraulicLocationEntityId, locationEntity.HydraulicLocationEntityId); + Assert.AreEqual(locationId, locationEntity.LocationId); + Assert.AreEqual(locationX, locationEntity.LocationX); + Assert.AreEqual(locationY, locationEntity.LocationY); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_NullParentNavigationProperty_ThrowsArgumentNullException() + { + // Setup + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + var dikeAssessmentSection = new DikeAssessmentSection(); + + // Call + TestDelegate test = () => persistor.UpdateModel(null, dikeAssessmentSection, 0); + + // Assert + Assert.Throws(test); + } + + [Test] + public void UpdateModel_NulldikeAssessmentSection_ThrowsArgumentNullException() + { + // Setup + var ringtoetsEntities = mockRepository.Stub(); + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + var parentNavigationProperty = mockRepository.StrictMock>(); + mockRepository.ReplayAll(); + + // Call + TestDelegate test = () => persistor.UpdateModel(parentNavigationProperty, null, 0); + + // Assert + Assert.Throws(test); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_EmptyParentNavigationPropertySingleDikeAssessmentSectionWithoutStorageId_DikeAssessmentSectionAsEntityInParentNavigationProperty() + { + // Setup + const string name = "test"; + const int norm = 30000; + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + + mockRepository.ReplayAll(); + + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + ICollection parentNavigationProperty = new List(); + DikeAssessmentSection dikeAssessmentSection = + new DikeAssessmentSection + { + Name = name, + FailureMechanismContribution = + { + Norm = norm + }, + HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase() + } + ; + + // Call + persistor.UpdateModel(parentNavigationProperty, dikeAssessmentSection, 0); + + // Assert + Assert.AreEqual(1, parentNavigationProperty.Count); + var parentNavigationPropertyList = parentNavigationProperty.ToList(); + var entity = parentNavigationPropertyList[0]; + Assert.AreEqual(0, entity.DikeAssessmentSectionEntityId); + Assert.AreEqual(name, entity.Name); + Assert.AreEqual(norm, entity.Norm); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_EmptyParentNavigationPropertySingleDikeAssessmentSectionWithStorageId_ThrowsEntityNotFoundException() + { + // Setup + const string name = "test"; + const long storageId = 1234L; + const int norm = 30000; + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + ICollection parentNavigationProperty = new List(); + DikeAssessmentSection dikeAssessmentSection = + new DikeAssessmentSection + { + StorageId = storageId, + Name = name, + FailureMechanismContribution = + { + Norm = norm + } + }; + + // Call + TestDelegate test = () => persistor.UpdateModel(parentNavigationProperty, dikeAssessmentSection, 0); + + // Assert + Assert.Throws(test); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_SingleEntityInParentNavigationPropertySingleDikeAssessmentSectionWithUnknownStorageId_ThrowsEntityNotFoundException() + { + // Setup + const long storageId = 1234L; + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + ICollection parentNavigationProperty = new List + { + new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = 4567L + } + }; + DikeAssessmentSection dikeAssessmentSection = + new DikeAssessmentSection + { + StorageId = storageId + }; + + // Call + TestDelegate test = () => persistor.UpdateModel(parentNavigationProperty, dikeAssessmentSection, 0); + + // Assert + Assert.Throws(test); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_DuplucateEntityInParentNavigationPropertySingleDikeAssessmentSectionWithStorageId_ThrowsEntityNotFoundException() + { + // Setup + const long storageId = 1234L; + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + ICollection parentNavigationProperty = new List + { + new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = storageId + }, + new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = storageId + } + }; + DikeAssessmentSection dikeAssessmentSection = + new DikeAssessmentSection + { + StorageId = storageId + }; + + // Call + TestDelegate test = () => persistor.UpdateModel(parentNavigationProperty, dikeAssessmentSection, 0); + + // Assert + EntityNotFoundException exception = Assert.Throws(test); + Assert.IsInstanceOf(exception); + Assert.IsInstanceOf(exception.InnerException); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_SingleEntityInParentNavigationPropertySingleDikeAssessmentSectionWithStorageId_UpdatedDikeAssessmentSectionAsEntityInParentNavigationProperty() + { + // Setup + const string name = "test"; + const long storageId = 1234L; + const int norm = 30000; + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + ICollection parentNavigationProperty = new List + { + new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = storageId, + Name = "old name", + Norm = 1 + } + }; + DikeAssessmentSection dikeAssessmentSection = + new DikeAssessmentSection + { + StorageId = storageId, + Name = name, + FailureMechanismContribution = + { + Norm = norm + }, + HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase() + }; + + // Call + persistor.UpdateModel(parentNavigationProperty, dikeAssessmentSection, 0); + + // Assert + Assert.AreEqual(1, parentNavigationProperty.Count); + var parentNavigationPropertyList = parentNavigationProperty.ToList(); + var entity = parentNavigationPropertyList[0]; + Assert.AreEqual(storageId, entity.DikeAssessmentSectionEntityId); + Assert.AreEqual(name, entity.Name); + Assert.AreEqual(norm, entity.Norm); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_MultipleEntitiesInParentNavigationPropertySingleDikeAssessmentSectionWithStorageId_UpdatedDikeAssessmentSectionAsEntityInParentNavigationProperty() + { + // Setup + const string name = "UpdatedName"; + const long storageId = 1234L; + const int norm = 30000; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + DikeAssessmentSectionEntity entityToDelete = new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = 4567L, + Name = "Entity to delete" + }; + IList parentNavigationProperty = new List + { + entityToDelete, + new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = storageId, + Name = "Entity to update", + Norm = 1 + } + }; + + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + + DikeAssessmentSection dikeAssessmentSection = + new DikeAssessmentSection + { + StorageId = storageId, + Name = name, + FailureMechanismContribution = + { + Norm = norm + }, + HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase() + }; + + // Call + persistor.UpdateModel(parentNavigationProperty, dikeAssessmentSection, 0); + + // Assert + Assert.AreEqual(2, parentNavigationProperty.Count); + var entity = parentNavigationProperty.SingleOrDefault(x => x.DikeAssessmentSectionEntityId == storageId); + Assert.IsInstanceOf(entity); + Assert.AreEqual(storageId, entity.DikeAssessmentSectionEntityId); + Assert.AreEqual(name, entity.Name); + Assert.AreEqual(norm, entity.Norm); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_ValidDikeAssessmentSectionWithChildren_UpdatedDikeAssessmentSectionWithChildren() + { + // Setup + const long storageId = 1234L; + const long hydraulicLocationEntityId = 5678L; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + DikeAssessmentSection dikeAssessmentSection = new DikeAssessmentSection + { + StorageId = storageId, + HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase() + }; + dikeAssessmentSection.HydraulicBoundaryDatabase.Locations.Add(new HydraulicBoundaryLocation(1300001, "test", 253, 123) + { + StorageId = hydraulicLocationEntityId, + }); + + IList parentNavigationProperty = new List + { + new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = storageId, + HydraulicLocationEntities = new List + { + new HydraulicLocationEntity + { + HydraulicLocationEntityId = hydraulicLocationEntityId + } + } + } + }; + + // Call + persistor.UpdateModel(parentNavigationProperty, dikeAssessmentSection, 0); + + // Assert + Assert.AreEqual(1, parentNavigationProperty.Count); + var entity = parentNavigationProperty.ToList()[0]; + Assert.AreNotEqual(dikeAssessmentSection, entity); + + Assert.AreEqual(1, entity.FailureMechanismEntities.Count); + Assert.AreEqual(1, entity.FailureMechanismEntities.Count(db => db.FailureMechanismType.Equals((int) FailureMechanismType.DikesPipingFailureMechanism))); + Assert.AreEqual(1, entity.HydraulicLocationEntities.Count); + + var hydraulicLocationEntity = entity.HydraulicLocationEntities.First(); + Assert.AreEqual(hydraulicLocationEntityId, hydraulicLocationEntity.HydraulicLocationEntityId); + + mockRepository.VerifyAll(); + } + + [Test] + public void RemoveUnModifiedEntries_SingleEntityInParentNavigationPropertyModelWithoutStorageId_UpdatedDikeAssessmentSectionAsEntityInParentNavigationPropertyAndOthersDeletedInDbSet() + { + // Setup + const string name = "test"; + const long storageId = 0L; // Newly inserted entities have Id = 0 untill they are saved + const int norm = 30000; + DikeAssessmentSectionEntity entityToDelete = new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = 4567L, + Name = "Entity to delete" + }; + + ObservableCollection parentNavigationProperty = new ObservableCollection + { + entityToDelete + }; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ringtoetsEntities.DikeAssessmentSectionEntities.Add(entityToDelete); + + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + DikeAssessmentSection dikeAssessmentSection = + new DikeAssessmentSection + { + Name = name, + FailureMechanismContribution = + { + Norm = norm + }, + HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase() + }; + + persistor.InsertModel(parentNavigationProperty, dikeAssessmentSection, 0); + + // Call + persistor.RemoveUnModifiedEntries(parentNavigationProperty); + + // Assert + CollectionAssert.IsEmpty(ringtoetsEntities.DikeAssessmentSectionEntities); + mockRepository.VerifyAll(); + } + + [Test] + public void RemoveUnModifiedEntries_MultipleEntitiesInParentNavigationPropertySingleModelStorageId_UpdatedDikeAssessmentSectionAsEntityAndOtherDeletedInDbSet() + { + // Setup + const string name = "test"; + const long storageId = 1234L; + const int norm = 30000; + DikeAssessmentSectionEntity entityToUpdate = new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = storageId, + Name = "Entity to update" + }; + DikeAssessmentSectionEntity entityToDelete = new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = 4567L, + Name = "First entity to delete" + }; + + ObservableCollection parentNavigationProperty = new ObservableCollection + { + entityToDelete, + entityToUpdate + }; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ringtoetsEntities.DikeAssessmentSectionEntities.Add(entityToDelete); + ringtoetsEntities.DikeAssessmentSectionEntities.Add(entityToUpdate); + + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + DikeAssessmentSection dikeAssessmentSection = new DikeAssessmentSection + { + Name = name, + FailureMechanismContribution = + { + Norm = norm + }, + StorageId = storageId, + HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase() + }; + mockRepository.ReplayAll(); + + TestDelegate updateTest = () => persistor.UpdateModel(parentNavigationProperty, dikeAssessmentSection, 0); + Assert.DoesNotThrow(updateTest, "Precondition failed: Update should not throw exception."); + + // Call + persistor.RemoveUnModifiedEntries(parentNavigationProperty); + + // Assert + Assert.AreEqual(1, ringtoetsEntities.DikeAssessmentSectionEntities.Count()); + Assert.AreEqual(entityToUpdate, ringtoetsEntities.DikeAssessmentSectionEntities.FirstOrDefault()); + + mockRepository.VerifyAll(); + } + + [Test] + public void RemoveUnModifiedEntries_MultipleEntitiesInParentNavigationPropertyEmptyDikeAssessmentSection_EmptyDatabaseSet() + { + // Setup + DikeAssessmentSectionEntity firstEntityToDelete = new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = 1234L, + Name = "First entity to delete" + }; + DikeAssessmentSectionEntity secondEntityToDelete = new DikeAssessmentSectionEntity + { + DikeAssessmentSectionEntityId = 4567L, + Name = "Second entity to delete" + }; + ObservableCollection parentNavigationProperty = new ObservableCollection + { + firstEntityToDelete, + secondEntityToDelete + }; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ringtoetsEntities.DikeAssessmentSectionEntities.Add(firstEntityToDelete); + ringtoetsEntities.DikeAssessmentSectionEntities.Add(secondEntityToDelete); + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + + DikeAssessmentSection dikeAssessmentSection = new DikeAssessmentSection + { + HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase() + }; + mockRepository.ReplayAll(); + + TestDelegate test = () => persistor.InsertModel(parentNavigationProperty, dikeAssessmentSection, 0); + Assert.DoesNotThrow(test, "Precondition failed: InsertModel"); + + // Call + persistor.RemoveUnModifiedEntries(parentNavigationProperty); + + // Assert + mockRepository.VerifyAll(); + } + + [Test] + public void PerformPostSaveActions_NoInserts_DoesNotThrowException() + { + // Setup + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + + // Call + TestDelegate test = () => persistor.PerformPostSaveActions(); + + // Assert + Assert.DoesNotThrow(test); + + mockRepository.VerifyAll(); + } + + [Test] + [TestCase(0)] + [TestCase(1)] + [TestCase(2)] + public void PerformPostSaveActions_MultipleModelsInsertedWithoutStorageId_ModelsWithStorageId(int numberOfInserts) + { + // Setup + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var insertedDikeAssessmentSectionEntities = new List(); + + IList dikeAssessmentSections = new List(); + + DikeAssessmentSectionPersistor persistor = new DikeAssessmentSectionPersistor(ringtoetsEntities); + + for (var i = 0; i < numberOfInserts; i++) + { + dikeAssessmentSections.Add(new DikeAssessmentSection + { + StorageId = 0L, + HydraulicBoundaryDatabase = new HydraulicBoundaryDatabase() + }); + } + + foreach (var dikeAssessmentSection in dikeAssessmentSections) + { + try + { + persistor.UpdateModel(insertedDikeAssessmentSectionEntities, dikeAssessmentSection, 0); + } + catch (Exception) + { + Assert.Fail("Precondition failed: persistor.UpdateModel"); + } + } + + // Call + for (var i = 0; i < insertedDikeAssessmentSectionEntities.Count; i++) + { + insertedDikeAssessmentSectionEntities[i].DikeAssessmentSectionEntityId = 1L + i; + } + persistor.PerformPostSaveActions(); + + // Assert + Assert.AreEqual(dikeAssessmentSections.Count, insertedDikeAssessmentSectionEntities.Count); + foreach (var entity in insertedDikeAssessmentSectionEntities) + { + var insertedModel = dikeAssessmentSections.SingleOrDefault(x => x.StorageId == entity.DikeAssessmentSectionEntityId); + Assert.IsInstanceOf(insertedModel); + } + + mockRepository.VerifyAll(); + } + } +} \ No newline at end of file Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/DikesPipingFailureMechanismEntityPersistorTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/HydraulicBoundaryLocationPersistorTest.cs =================================================================== diff -u --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/HydraulicBoundaryLocationPersistorTest.cs (revision 0) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/HydraulicBoundaryLocationPersistorTest.cs (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -0,0 +1,577 @@ +// Copyright (C) Stichting Deltares 2016. 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.Collections.ObjectModel; +using System.Linq; +using Application.Ringtoets.Storage.DbContext; +using Application.Ringtoets.Storage.Exceptions; +using Application.Ringtoets.Storage.Persistors; +using Application.Ringtoets.Storage.TestUtil; +using NUnit.Framework; +using Rhino.Mocks; +using Ringtoets.HydraRing.Data; + +namespace Application.Ringtoets.Storage.Test.Persistors +{ + [TestFixture] + public class HydraulicBoundaryLocationPersistorTest + { + private MockRepository mockRepository; + + [SetUp] + public void Setup() + { + mockRepository = new MockRepository(); + } + + [Test] + public void Constructor_NullDataSet_ThrowsAgrumentNullException() + { + // Call + TestDelegate test = () => new HydraulicBoundaryLocationPersistor(null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("ringtoetsContext", exception.ParamName); + } + + [Test] + public void Constructor_EmptyDataSet_NewInstance() + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + // Call + HydraulicBoundaryLocationPersistor persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + + // Assert + Assert.IsInstanceOf(persistor); + + mockRepository.VerifyAll(); + } + + [Test] + public void LoadModel_NullEntity_ThrowsArgumentNullException() + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + + // Call + TestDelegate test = () => persistor.LoadModel(null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("entities", exception.ParamName); + mockRepository.VerifyAll(); + } + + [Test] + public void LoadModel_ValidEntityValidModel_EntityAsModel() + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + + const string name = "test"; + const double designWaterLevel = 15.6; + const long locationId = 1300001; + const long storageId = 1234L; + const decimal locationX = 253; + const decimal locationY = 123; + var entity = new HydraulicLocationEntity() + { + LocationId = locationId, + Name = name, + DesignWaterLevel = designWaterLevel, + HydraulicLocationEntityId = storageId, + LocationX = locationX, + LocationY = locationY + }; + + // Call + List locations = persistor.LoadModel(new List + { + entity + }).ToList(); + + // Assert + Assert.AreEqual(1, locations.Count); + var location = locations[0]; + Assert.AreEqual(locationId, location.Id); + Assert.AreEqual(name, location.Name); + Assert.AreEqual(designWaterLevel, location.DesignWaterLevel); + Assert.AreEqual(locationX, location.Location.X); + Assert.AreEqual(locationY, location.Location.Y); + Assert.AreEqual(storageId, location.StorageId); + + mockRepository.VerifyAll(); + } + + [Test] + public void InsertModel_NullParentNavigationProperty_ThrowsArgumentNullException() + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + HydraulicBoundaryDatabase hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + hydraulicBoundaryDatabase.Locations.Add(new HydraulicBoundaryLocation(1, "test", 1, 1)); + + // Call + TestDelegate test = () => persistor.InsertModel(null, hydraulicBoundaryDatabase); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("parentNavigationProperty", exception.ParamName); + + mockRepository.VerifyAll(); + } + + [Test] + public void InsertModel_NullModel_DoesNotAddEntityInParentNavigationProperty() + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + var parentNavigationProperty = new List(); + + // Call + TestDelegate test = () => persistor.InsertModel(parentNavigationProperty, null); + + // Assert + Assert.DoesNotThrow(test); + CollectionAssert.AreEquivalent(new List(), parentNavigationProperty); + + mockRepository.VerifyAll(); + } + + [Test] + public void InsertModel_SingleEntityInParentNavigationPropertyHydraulicLocationWithSameStorageId_HydraulicLocationAsEntityInParentNavigationProperty() + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + + const long storageId = 1234L; + HydraulicLocationEntity entityToDelete = new HydraulicLocationEntity + { + HydraulicLocationEntityId = storageId + }; + + IList parentNavigationProperty = new List + { + entityToDelete + }; + + HydraulicBoundaryLocation model = new HydraulicBoundaryLocation(13001, "test", 13, 52) + { + StorageId = storageId + }; + + HydraulicBoundaryDatabase hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + hydraulicBoundaryDatabase.Locations.Add(model); + + // Call + persistor.InsertModel(parentNavigationProperty, hydraulicBoundaryDatabase); + + // Assert + Assert.AreEqual(2, parentNavigationProperty.Count); + var parentNavigationPropertyList = parentNavigationProperty.ToList(); + var entity = parentNavigationPropertyList[1]; + Assert.AreEqual(storageId, entity.HydraulicLocationEntityId); + + mockRepository.VerifyAll(); + } + + [Test] + public void InsertModel_LocationNull_ThrowsArgumentNullException() + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + IList parentNavigationProperty = new List(); + + HydraulicBoundaryDatabase hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + hydraulicBoundaryDatabase.Locations.Add(null); + + // Call + TestDelegate test = () => persistor.InsertModel(parentNavigationProperty, hydraulicBoundaryDatabase); + + // Assert + Assert.Throws(test); + + mockRepository.VerifyAll(); + } + + [Test] + public void InsertModel_LocationToBig_ThrowsOverflowException() + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + IList parentNavigationProperty = new List(); + + HydraulicBoundaryDatabase hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + hydraulicBoundaryDatabase.Locations.Add(new HydraulicBoundaryLocation(1, "name", Double.PositiveInfinity, 1)); + + // Call + TestDelegate test = () => persistor.InsertModel(parentNavigationProperty, hydraulicBoundaryDatabase); + + // Assert + Assert.Throws(test); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_NullDatasetValidModel_ThrowsArgumentNullException() + { + // Setup + const long storageId = 1234L; + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + + HydraulicBoundaryLocation model = new HydraulicBoundaryLocation(13001, "test", 13, 52) + { + StorageId = storageId + }; + + HydraulicBoundaryDatabase hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + hydraulicBoundaryDatabase.Locations.Add(model); + + // Call + TestDelegate test = () => persistor.UpdateModel(null, hydraulicBoundaryDatabase); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("parentNavigationProperty", exception.ParamName); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_EmptyDatasetNullModel_DoesNotThrow() + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + IList parentNavigationProperty = new List(); + + // Call + TestDelegate test = () => persistor.UpdateModel(parentNavigationProperty, null); + + // Assert + Assert.DoesNotThrow(test); + } + + [Test] + public void UpdateModel_EmptyDataset_ThrowsEntityNotFoundException() + { + // Setup + const long storageId = 1234L; + + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + IList parentNavigationProperty = new List(); + + HydraulicBoundaryLocation model = new HydraulicBoundaryLocation(13001, "test", 13, 52) + { + StorageId = storageId + }; + + HydraulicBoundaryDatabase hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + hydraulicBoundaryDatabase.Locations.Add(model); + + // Call + TestDelegate test = () => persistor.UpdateModel(parentNavigationProperty, hydraulicBoundaryDatabase); + + // Assert + var expectedMessage = String.Format("Het object 'HydraulicLocationEntity' met id '{0}' is niet gevonden.", storageId); + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_DuplicateEntityInDataset_ThrowsEntityNotFoundException() + { + // Setup + const long storageId = 1234L; + + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + IList parentNavigationProperty = new List + { + new HydraulicLocationEntity + { + HydraulicLocationEntityId = storageId + }, + new HydraulicLocationEntity + { + HydraulicLocationEntityId = storageId + } + }; + + HydraulicBoundaryLocation model = new HydraulicBoundaryLocation(13001, "test", 13, 52) + { + StorageId = storageId + }; + + HydraulicBoundaryDatabase hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + hydraulicBoundaryDatabase.Locations.Add(model); + + // Call + TestDelegate test = () => persistor.UpdateModel(parentNavigationProperty, hydraulicBoundaryDatabase); + + // Assert + var expectedMessage = String.Format("Het object 'HydraulicLocationEntity' met id '{0}' is niet gevonden.", storageId); + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_SingleEntityInParentNavigationPropertySingleHydraulicLocationWithStorageId_UpdatedHydraulicLocationAsEntityInParentNavigationProperty() + { + // Setup + const long storageId = 1234L; + + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + IList parentNavigationProperty = new List + { + new HydraulicLocationEntity + { + HydraulicLocationEntityId = storageId + } + }; + + HydraulicBoundaryLocation model = new HydraulicBoundaryLocation(13001, "test", 13, 52) + { + StorageId = storageId + }; + + HydraulicBoundaryDatabase hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + hydraulicBoundaryDatabase.Locations.Add(model); + + // Call + persistor.UpdateModel(parentNavigationProperty, hydraulicBoundaryDatabase); + + // Assert + Assert.AreEqual(1, parentNavigationProperty.Count); + var parentNavigationPropertyList = parentNavigationProperty.ToList(); + var entity = parentNavigationPropertyList[0]; + Assert.AreEqual(storageId, entity.HydraulicLocationEntityId); + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_NoStorageIdSet_InsertNewEntity() + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + + IList parentNavigationProperty = new List(); + HydraulicBoundaryLocation model = new HydraulicBoundaryLocation(13001, "test", 13, 52) + { + StorageId = 0 + }; + HydraulicBoundaryDatabase hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + hydraulicBoundaryDatabase.Locations.Add(model); + + // Precondition + Assert.AreEqual(0, parentNavigationProperty.Count, "Precondition failed: parentNavigationProperty should be empty"); + + // Call + persistor.UpdateModel(parentNavigationProperty, hydraulicBoundaryDatabase); + + // Assert + Assert.AreEqual(1, parentNavigationProperty.Count); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_LocationToBig_ThrowsOverflowException() + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + IList parentNavigationProperty = new List(); + + HydraulicBoundaryDatabase hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + hydraulicBoundaryDatabase.Locations.Add(new HydraulicBoundaryLocation(1, "name", Double.PositiveInfinity, 1)); + + // Call + TestDelegate test = () => persistor.UpdateModel(parentNavigationProperty, hydraulicBoundaryDatabase); + + // Assert + Assert.Throws(test); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_SingleEntityInParentNavigationPropertySingleHydraulicLocationWithoutStorageId_UpdatedHydraulicLocationAsEntityInParentNavigationPropertyAndOthersDeletedInDbSet() + { + // Setup + const long storageId = 0L; // Newly inserted entities have Id = 0 untill they are saved + + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + HydraulicLocationEntity entityToDelete = new HydraulicLocationEntity + { + HydraulicLocationEntityId = 4567L, + Name = "Entity to delete" + }; + + ringtoetsEntitiesMock.HydraulicLocationEntities.Add(entityToDelete); + + ObservableCollection parentNavigationProperty = new ObservableCollection + { + entityToDelete + }; + + mockRepository.ReplayAll(); + + HydraulicBoundaryLocationPersistor persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + HydraulicBoundaryLocation location = new HydraulicBoundaryLocation(13001, "test", 13, 52); + HydraulicBoundaryDatabase hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + hydraulicBoundaryDatabase.Locations.Add(location); + + // Call + persistor.UpdateModel(parentNavigationProperty, hydraulicBoundaryDatabase); + + // Assert + CollectionAssert.IsEmpty(ringtoetsEntitiesMock.HydraulicLocationEntities); + Assert.AreEqual(2, parentNavigationProperty.Count); + HydraulicLocationEntity entity = parentNavigationProperty.SingleOrDefault(x => x.HydraulicLocationEntityId == storageId); + Assert.IsNotNull(entity); + Assert.AreEqual(storageId, entity.HydraulicLocationEntityId); + + mockRepository.VerifyAll(); + } + + [Test] + public void PerformPostSaveActions_NoInserts_DoesNotThrowException() + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + HydraulicBoundaryLocationPersistor persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + + // Call + TestDelegate test = () => persistor.PerformPostSaveActions(); + + // Assert + Assert.DoesNotThrow(test); + + mockRepository.VerifyAll(); + } + + [Test] + [TestCase(0)] + [TestCase(1)] + [TestCase(2)] + public void PerformPostSaveActions_MultipleModelsInsertedWithoutStorageId_ModelsWithStorageId(int numberOfInserts) + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var parentNavigationProperty = new List(); + + IList hydraulicLocations = new List(); + for (var i = 0; i < numberOfInserts; i++) + { + hydraulicLocations.Add(new HydraulicBoundaryLocation(13001, "test", 13, 52) + { + StorageId = 0L + }); + } + + HydraulicBoundaryDatabase hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase(); + hydraulicBoundaryDatabase.Locations.AddRange(hydraulicLocations); + + HydraulicBoundaryLocationPersistor persistor = new HydraulicBoundaryLocationPersistor(ringtoetsEntitiesMock); + + try + { + persistor.UpdateModel(parentNavigationProperty, hydraulicBoundaryDatabase); + } + catch (Exception) + { + Assert.Fail("Precondition failed: persistor.UpdateModel"); + } + + // Call + for (var i = 0; i < parentNavigationProperty.Count; i++) + { + parentNavigationProperty[i].HydraulicLocationEntityId = 1L + i; + } + persistor.PerformPostSaveActions(); + + // Assert + Assert.AreEqual(hydraulicLocations.Count, parentNavigationProperty.Count); + foreach (var entity in parentNavigationProperty) + { + HydraulicBoundaryLocation insertedModel = hydraulicLocations.SingleOrDefault(x => x.StorageId == entity.HydraulicLocationEntityId); + Assert.IsNotNull(insertedModel); + } + + mockRepository.VerifyAll(); + } + } +} \ No newline at end of file Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/HydraulicLocationEntityPersistorTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 8dec462b18aff76313f2836309ef24ddd2f70b50 refers to a dead (removed) revision in file `Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/PipingFailureMechanismPersistorTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag bbe8871cfabad25988ec8f94115da2c485267118 refers to a dead (removed) revision in file `Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/ProjectEntityPersistorTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/ProjectPersistorTest.cs =================================================================== diff -u --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/ProjectPersistorTest.cs (revision 0) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/ProjectPersistorTest.cs (revision bbe8871cfabad25988ec8f94115da2c485267118) @@ -0,0 +1,610 @@ +// Copyright (C) Stichting Deltares 2016. 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 Application.Ringtoets.Storage.DbContext; +using Application.Ringtoets.Storage.Exceptions; +using Application.Ringtoets.Storage.Persistors; +using Application.Ringtoets.Storage.TestUtil; +using Core.Common.Base.Data; +using NUnit.Framework; +using Rhino.Mocks; +using Ringtoets.Integration.Data; + +namespace Application.Ringtoets.Storage.Test.Persistors +{ + [TestFixture] + public class ProjectPersistorTest + { + private MockRepository mockRepository; + + [SetUp] + public void SetUp() + { + mockRepository = new MockRepository(); + } + + [Test] + [ExpectedException(typeof(ArgumentNullException))] + public void Constructor_NullDataSet_ThrowsArgumentNullException() + { + // Call + ProjectPersistor p = new ProjectPersistor(null); + } + + [Test] + public void Constructor_EmptyDataset_NewInstance() + { + // Setup + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + // Call + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Assert + Assert.IsInstanceOf(persistor); + } + + [Test] + public void GetEntityAsModel_EmptyDataset_DoesNotThrowException() + { + // Setup + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Call + TestDelegate test = () => persistor.GetEntityAsModel(); + + // Assert + Assert.DoesNotThrow(test); + + mockRepository.VerifyAll(); + } + + [Test] + public void GetEntityAsModel_SingleEntityInDataSet_ProjectEntityFromDataSetAsModel() + { + // Setup + const long storageId = 1234L; + const string description = "description"; + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ringtoetsEntities.ProjectEntities.Add( + new ProjectEntity + { + ProjectEntityId = storageId, + Description = description + }); + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Call + Project model = persistor.GetEntityAsModel(); + + // Assert + Assert.IsInstanceOf(model); + Assert.AreEqual(storageId, model.StorageId); + Assert.AreEqual(description, model.Description); + + mockRepository.VerifyAll(); + } + + [Test] + public void GetEntityAsModel_NoProjectNameSet_ProjectEntityFromDataSetAsModelWithDefaultName() + { + // Setup + const long storageId = 1234L; + const string description = "description"; + string defaultProjectName = new Project().Name; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ringtoetsEntities.ProjectEntities.Add( + new ProjectEntity + { + ProjectEntityId = storageId, + Description = description + }); + + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Call + Project model = persistor.GetEntityAsModel(); + + // Assert + Assert.IsInstanceOf(model); + Assert.AreEqual(storageId, model.StorageId); + Assert.AreEqual(defaultProjectName, model.Name); + Assert.AreEqual(description, model.Description); + + mockRepository.VerifyAll(); + } + + [Test] + public void GetEntityAsModel_MultipleEntitiesInDataSet_ThrowsInvalidOperationException() + { + // Setup + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ringtoetsEntities.ProjectEntities.Add( + new ProjectEntity + { + ProjectEntityId = 1 + }); + ringtoetsEntities.ProjectEntities.Add( + new ProjectEntity + { + ProjectEntityId = 2 + }); + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Call + TestDelegate test = () => persistor.GetEntityAsModel(); + + // Assert + Assert.Throws(test); + + mockRepository.VerifyAll(); + } + + [Test] + public void GetEntityAsModel_SingleEntityWithChildrenInDataSet_ProjectEntityFromDataSetAsModel() + { + // Setup + const long storageId = 1234L; + const string description = "description"; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ringtoetsEntities.ProjectEntities.Add( + new ProjectEntity + { + ProjectEntityId = storageId, + Description = description, + DikeAssessmentSectionEntities = new List + { + new DikeAssessmentSectionEntity + { + Norm = 1, + Order = 0 + } + } + }); + + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Call + Project model = persistor.GetEntityAsModel(); + + // Assert + Assert.IsInstanceOf(model); + Assert.AreEqual(storageId, model.StorageId); + Assert.AreEqual(description, model.Description); + Assert.AreEqual(1, model.Items.Count); + Assert.AreEqual(1, model.Items.Count(i => i is DikeAssessmentSection)); + + mockRepository.VerifyAll(); + } + + [Test] + public void InsertModel_NullData_ThrowsArgumentNullException() + { + // Setup + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Call + TestDelegate test = () => persistor.InsertModel(null); + + // Assert + Assert.Throws(test); + Assert.AreEqual(0, ringtoetsEntities.ProjectEntities.Count()); + + mockRepository.VerifyAll(); + } + + [Test] + public void InsertModel_ValidProject_UpdatedDataSet() + { + // Setup + const long storageId = 1234L; + const string description = "description"; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + Project project = new Project + { + StorageId = storageId, + Description = description + }; + + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Call + persistor.InsertModel(project); + + // Assert + var projectEntity = ringtoetsEntities.ProjectEntities.First(); + Assert.AreNotEqual(project, projectEntity); + Assert.AreEqual(storageId, projectEntity.ProjectEntityId); + Assert.AreEqual(description, projectEntity.Description); + + mockRepository.VerifyAll(); + } + + [Test] + public void InsertModel_ValidProjectWithChildren_UpdatedProjectEntityWithChildren() + { + // Setup + const long storageId = 1234L; + const string description = "description"; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + Project project = new Project + { + StorageId = storageId, + Description = description, + Items = + { + new DikeAssessmentSection() + } + }; + + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Call + persistor.InsertModel(project); + + // Assert + var projectEntity = ringtoetsEntities.ProjectEntities.First(); + Assert.AreNotEqual(project, projectEntity); + Assert.AreEqual(storageId, projectEntity.ProjectEntityId); + Assert.AreEqual(description, projectEntity.Description); + Assert.AreEqual(1, projectEntity.DikeAssessmentSectionEntities.Count); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_NullData_ThrowsArgumentNullException() + { + // Setup + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Call + TestDelegate test = () => persistor.UpdateModel(null); + + // Assert + Assert.Throws(test); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_UnknownProject_ThrowsEntityNotFoundException() + { + // Setup + const long storageId = 1234L; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + Project project = new Project + { + StorageId = storageId + }; + ringtoetsEntities.ProjectEntities.Add( + new ProjectEntity + { + ProjectEntityId = 2 + }); + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Call + TestDelegate test = () => persistor.UpdateModel(project); + + // Assert + var expectedMessage = String.Format("Het object 'ProjectEntity' met id '{0}' is niet gevonden.", storageId); + EntityNotFoundException exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_MultipleEqualEntitiesInDbSet_ThrownEntityNotFoundException() + { + // Setup + const long storageId = 1234L; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + Project project = new Project + { + StorageId = storageId + }; + ringtoetsEntities.ProjectEntities.Add( + new ProjectEntity + { + ProjectEntityId = storageId + }); + ringtoetsEntities.ProjectEntities.Add( + new ProjectEntity + { + ProjectEntityId = storageId + }); + + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Call + TestDelegate test = () => persistor.UpdateModel(project); + + // Assert + var expectedMessage = String.Format("Het object 'ProjectEntity' met id '{0}' is niet gevonden.", storageId); + var expectedInnerMessage = "Sequence contains more than one matching element"; + + EntityNotFoundException exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + + Assert.IsInstanceOf(exception.InnerException); + Assert.AreEqual(expectedInnerMessage, exception.InnerException.Message); + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_ValidProject_UpdatedDataSet() + { + // Setup + const long storageId = 1234L; + const string description = ""; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + Project project = new Project + { + StorageId = storageId, + Description = description + }; + ProjectEntity entity = new ProjectEntity + { + ProjectEntityId = storageId + }; + + ringtoetsEntities.ProjectEntities.Add(entity); + + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Call + persistor.UpdateModel(project); + + // Assert + Assert.IsInstanceOf(entity); + Assert.AreEqual(project.StorageId, entity.ProjectEntityId); + Assert.AreEqual(project.Description, entity.Description); + + mockRepository.VerifyAll(); + } + + [Test] + public void UpdateModel_ValidProjectWithChildren_UpdatedProjectEntityWithChildren() + { + // Setup + const long storageId = 1234L; + const string description = "description"; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ProjectEntity projectEntity = new ProjectEntity + { + ProjectEntityId = storageId + }; + Project project = new Project + { + StorageId = storageId, + Description = description, + Items = + { + new DikeAssessmentSection() + } + }; + + ringtoetsEntities.ProjectEntities.Add(projectEntity); + + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Call + persistor.UpdateModel(project); + + // Assert + Assert.AreNotEqual(project, projectEntity); + Assert.AreEqual(storageId, projectEntity.ProjectEntityId); + Assert.AreEqual(description, projectEntity.Description); + Assert.AreEqual(1, projectEntity.DikeAssessmentSectionEntities.Count); + + mockRepository.VerifyAll(); + } + + [Test] + public void PerformPostSaveActions_NoInserts_DoesNotThrowException() + { + // Setup + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + // Call + TestDelegate test = () => persistor.PerformPostSaveActions(); + + // Assert + Assert.DoesNotThrow(test); + + mockRepository.VerifyAll(); + } + + [Test] + public void PerformPostSaveActions_ModelInsertedWithoutStorageId_ModelWithStorageId() + { + // Setup + const long storageId = 1234L; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + Project project = new Project + { + StorageId = 0L + }; + + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + TestDelegate insertTest = () => persistor.InsertModel(project); + Assert.DoesNotThrow(insertTest, "Precondition failed: InsertModel failed"); + + var insertedProjectEntity = ringtoetsEntities.ProjectEntities.First(); + insertedProjectEntity.ProjectEntityId = storageId; + Assert.AreEqual(0L, project.StorageId, "Precondition failed: Id should not have been set already"); + + // Call + persistor.PerformPostSaveActions(); + + // Assert + Assert.IsInstanceOf(project); + Assert.AreEqual(storageId, project.StorageId); + + mockRepository.VerifyAll(); + } + + [Test] + public void RemoveUnModifiedEntries_SingleEntityInDbSetSingleProjectWithoutStorageId_UpdatedProjectAsEntityInDbSetAndOthersDeletedInDbSet() + { + // Setup + const string description = "test"; + const long storageId = 1L; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ProjectEntity entityToDelete = new ProjectEntity + { + ProjectEntityId = 4567L, + Description = "Entity to delete" + }; + + var entityToUpdate = new ProjectEntity + { + ProjectEntityId = storageId, + Description = "Entity to update" + }; + + ringtoetsEntities.ProjectEntities.Add(entityToDelete); + ringtoetsEntities.ProjectEntities.Add(entityToUpdate); + + Project project = new Project + { + StorageId = storageId, + Description = description + }; + + mockRepository.ReplayAll(); + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + TestDelegate test = () => persistor.UpdateModel(project); + Assert.DoesNotThrow(test, "Precondition failed: UpdateModel"); + + // Call + persistor.RemoveUnModifiedEntries(); + + // Assert + Assert.AreEqual(1, ringtoetsEntities.ProjectEntities.Count()); + Assert.AreEqual(entityToUpdate, ringtoetsEntities.ProjectEntities.FirstOrDefault()); + + mockRepository.VerifyAll(); + } + + [Test] + public void RemoveUnModifiedEntries_MultipleEntitiesInDbSetEmptyProject_EmptyDbSet() + { + // Setup + const long storageId = 1L; + + var ringtoetsEntities = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + ProjectEntity firstEntityToDelete = new ProjectEntity + { + ProjectEntityId = 1234L, + Description = "First entity to delete" + }; + ProjectEntity secondEntityToDelete = new ProjectEntity + { + ProjectEntityId = 4567L, + Description = "Second entity to delete" + }; + ProjectEntity entityToUpdate = new ProjectEntity + { + ProjectEntityId = storageId, + Description = "Entity to update" + }; + + ringtoetsEntities.ProjectEntities.Add(firstEntityToDelete); + ringtoetsEntities.ProjectEntities.Add(secondEntityToDelete); + ringtoetsEntities.ProjectEntities.Add(entityToUpdate); + + Project project = new Project + { + StorageId = storageId + }; + + ProjectPersistor persistor = new ProjectPersistor(ringtoetsEntities); + + TestDelegate test = () => persistor.UpdateModel(project); + Assert.DoesNotThrow(test, "Precondition failed: UpdateModel"); + + // Call + persistor.RemoveUnModifiedEntries(); + + // Assert + Assert.AreEqual(1, ringtoetsEntities.ProjectEntities.Count()); + Assert.AreEqual(entityToUpdate, ringtoetsEntities.ProjectEntities.FirstOrDefault()); + mockRepository.VerifyAll(); + } + } +} \ No newline at end of file