// Copyright (C) Stichting Deltares 2017. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Linq; using Application.Ringtoets.Storage.Create; using Application.Ringtoets.Storage.DbContext; using Application.Ringtoets.Storage.TestUtil; using Core.Common.Base.Geometry; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.ClosingStructures.Data; using Ringtoets.ClosingStructures.Data.TestUtil; using Ringtoets.Common.Data.DikeProfiles; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.Data.Structures; using Ringtoets.Common.Data.TestUtil; using Ringtoets.DuneErosion.Data; using Ringtoets.DuneErosion.Data.TestUtil; using Ringtoets.GrassCoverErosionInwards.Data; using Ringtoets.HeightStructures.Data; using Ringtoets.HeightStructures.Data.TestUtil; using Ringtoets.MacroStabilityInwards.Data.SoilProfile; using Ringtoets.MacroStabilityInwards.Data.TestUtil; using Ringtoets.MacroStabilityInwards.Data.TestUtil.SoilProfile; using Ringtoets.MacroStabilityInwards.Primitives; using Ringtoets.Piping.Data.SoilProfile; using Ringtoets.Piping.Primitives; using Ringtoets.Piping.Primitives.TestUtil; using Ringtoets.StabilityPointStructures.Data; using Ringtoets.StabilityPointStructures.Data.TestUtil; namespace Application.Ringtoets.Storage.Test.Create { [TestFixture] public class PersistenceRegistryTest { /// /// Test class to test the for the combination of /// and . /// /// The data model. /// The database entity. private abstract class RegistryTest where TDataModel : class where TEntity : class, new() { private readonly Action registerToRegistry; private readonly Func containsInRegistry; private readonly Func getFromRegistry; /// /// Creates a new instance of . /// /// The action to perform to register the data model /// to the registry. /// The action to perform to check whether the data /// model is registered in the registry. /// The action to perform to get the data model from /// the registry. /// Thrown when any parameter is null. /// /// public DerivedRegistryTest() : base( /// (r, e, m) => r.Register(e, m), /// (r, m) => r.Contains(m), /// (r, m) => r.Get(m)) {} /// /// protected RegistryTest(Action registerToRegistry, Func containsInRegistry, Func getFromRegistry) { if (registerToRegistry == null) { throw new ArgumentNullException(nameof(registerToRegistry)); } if (containsInRegistry == null) { throw new ArgumentNullException(nameof(containsInRegistry)); } if (getFromRegistry == null) { throw new ArgumentNullException(nameof(getFromRegistry)); } this.registerToRegistry = registerToRegistry; this.containsInRegistry = containsInRegistry; this.getFromRegistry = getFromRegistry; } [Test] public void Register_WithNullEntity_ThrowsArgumentNullException() { // Setup var registry = new PersistenceRegistry(); // Call TestDelegate test = () => registerToRegistry(registry, null, CreateDataModel()); // Assert string paramName = Assert.Throws(test).ParamName; Assert.AreEqual("entity", paramName); } [Test] public void Register_WithNullDataModel_ThrowsArgumentNullException() { // Setup var registry = new PersistenceRegistry(); // Call TestDelegate test = () => registerToRegistry(registry, new TEntity(), null); // Assert string paramName = Assert.Throws(test).ParamName; Assert.AreEqual("model", paramName); } [Test] public void Contains_DataModelNull_ThrowsArgumentNullException() { // Setup var registry = new PersistenceRegistry(); // Call TestDelegate test = () => containsInRegistry(registry, null); // Assert string paramName = Assert.Throws(test).ParamName; Assert.AreEqual("model", paramName); } [Test] public void Contains_DataModelAdded_ReturnsTrue() { // Setup var registry = new PersistenceRegistry(); TDataModel dataModel = CreateDataModel(); registerToRegistry(registry, new TEntity(), dataModel); // Call bool result = containsInRegistry(registry, dataModel); // Assert Assert.IsTrue(result); } [Test] public void Contains_OtherDataModelAdded_ReturnsFalse() { // Setup var registry = new PersistenceRegistry(); registerToRegistry(registry, new TEntity(), CreateDataModel()); // Call bool result = containsInRegistry(registry, CreateDataModel()); // Assert Assert.IsFalse(result); } [Test] public void Contains_PersistenceRegistryEmpty_ReturnsFalse() { // Setup var registry = new PersistenceRegistry(); TDataModel dataModel = CreateDataModel(); // Call bool result = containsInRegistry(registry, dataModel); // Assert Assert.IsFalse(result); } [Test] public void Get_DataModelNull_ThrowsArgumentNullException() { // Setup var registry = new PersistenceRegistry(); // Call TestDelegate test = () => getFromRegistry(registry, null); // Assert string paramName = Assert.Throws(test).ParamName; Assert.AreEqual("model", paramName); } [Test] public void Get_DataModelAdded_ReturnsEntity() { // Setup var registry = new PersistenceRegistry(); TDataModel dataModel = CreateDataModel(); var entity = new TEntity(); registerToRegistry(registry, entity, dataModel); // Call TEntity result = getFromRegistry(registry, dataModel); // Assert Assert.AreSame(entity, result); } [Test] public void Get_NoDataModelAdded_ThrowsInvalidOperationException() { // Setup var registry = new PersistenceRegistry(); TDataModel dataModel = CreateDataModel(); // Call TestDelegate test = () => getFromRegistry(registry, dataModel); // Assert Assert.Throws(test); } [Test] public void Get_OtherDataModelAdded_ThrowsInvalidOperationException() { // Setup var registry = new PersistenceRegistry(); registerToRegistry(registry, new TEntity(), CreateDataModel()); // Call TestDelegate test = () => getFromRegistry(registry, CreateDataModel()); // Assert Assert.Throws(test); } /// /// Creates a new instance of . /// /// An instance of . protected abstract TDataModel CreateDataModel(); } [TestFixture] private class HydraulicBoundaryLocationRegistryTest : RegistryTest { public HydraulicBoundaryLocationRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override HydraulicBoundaryLocation CreateDataModel() { return new TestHydraulicBoundaryLocation(nameof(HydraulicBoundaryLocation)); } } [TestFixture] private class FailureMechanismSectionRegistryTest : RegistryTest { public FailureMechanismSectionRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override FailureMechanismSection CreateDataModel() { return new TestFailureMechanismSection(); } } [TestFixture] private class DikeProfileRegistryTest : RegistryTest { public DikeProfileRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override DikeProfile CreateDataModel() { return new TestDikeProfile(); } } [TestFixture] private class ForeshoreProfileRegistryTest : RegistryTest { public ForeshoreProfileRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override ForeshoreProfile CreateDataModel() { return new TestForeshoreProfile(); } } #region DuneErosion [TestFixture] private class DuneLocationRegistryTest : RegistryTest { public DuneLocationRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override DuneLocation CreateDataModel() { return new TestDuneLocation(nameof(DuneLocation)); } } #endregion #region GrassCoverErosionInwards [TestFixture] private class GrassCoverErosionInwardsCalculationRegistryTest : RegistryTest< GrassCoverErosionInwardsCalculation, GrassCoverErosionInwardsCalculationEntity> { public GrassCoverErosionInwardsCalculationRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override GrassCoverErosionInwardsCalculation CreateDataModel() { return new GrassCoverErosionInwardsCalculation(); } } #endregion #region GrassCoverErosionOutwards [TestFixture] private class GrassCoverErosionOutwardsHydraulicBoundaryLocationRegistryTest : RegistryTest< HydraulicBoundaryLocation, GrassCoverErosionOutwardsHydraulicLocationEntity> { public GrassCoverErosionOutwardsHydraulicBoundaryLocationRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override HydraulicBoundaryLocation CreateDataModel() { return new TestHydraulicBoundaryLocation(nameof(HydraulicBoundaryLocation)); } } #endregion #region Piping [TestFixture] private class PipingStochasticSoilModelRegistryTest : RegistryTest { public PipingStochasticSoilModelRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override PipingStochasticSoilModel CreateDataModel() { return new PipingStochasticSoilModel(nameof(PipingStochasticSoilModel)); } } [TestFixture] private class PipingStochasticSoilProfileRegistryTest : RegistryTest { public PipingStochasticSoilProfileRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override PipingStochasticSoilProfile CreateDataModel() { return new PipingStochasticSoilProfile(0.2, PipingSoilProfileTestFactory.CreatePipingSoilProfile()); } } [TestFixture] private class PipingSoilProfileRegistryTest : RegistryTest { public PipingSoilProfileRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override PipingSoilProfile CreateDataModel() { return PipingSoilProfileTestFactory.CreatePipingSoilProfile(); } } [TestFixture] private class PipingSurfaceLineRegistryTest : RegistryTest { public PipingSurfaceLineRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override PipingSurfaceLine CreateDataModel() { return new PipingSurfaceLine(nameof(PipingSurfaceLine)); } } #endregion #region MacroStabilityInwards [TestFixture] private class MacroStabilityInwardsStochasticSoilModelRegistryTest : RegistryTest { public MacroStabilityInwardsStochasticSoilModelRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override MacroStabilityInwardsStochasticSoilModel CreateDataModel() { return MacroStabilityInwardsStochasticSoilModelTestFactory.CreateValidStochasticSoilModel(); } } [TestFixture] private class MacroStabilityInwardsStochasticSoilProfileRegistryTest : RegistryTest { private MockRepository mockRepository; [SetUp] public void Setup() { mockRepository = new MockRepository(); } [TearDown] public void TearDown() { mockRepository.VerifyAll(); } public MacroStabilityInwardsStochasticSoilProfileRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override MacroStabilityInwardsStochasticSoilProfile CreateDataModel() { var soilProfile = mockRepository.Stub(); mockRepository.ReplayAll(); return new MacroStabilityInwardsStochasticSoilProfile(0, soilProfile); } } [TestFixture] private class MacroStabilityInwardsSoilProfileOneDRegistryTest : RegistryTest { public MacroStabilityInwardsSoilProfileOneDRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override MacroStabilityInwardsSoilProfile1D CreateDataModel() { return MacroStabilityInwardsSoilProfile1DTestFactory.CreateMacroStabilityInwardsSoilProfile1D(); } } [TestFixture] private class MacroStabilityInwardsSoilProfileTwoDRegistryTest : RegistryTest { public MacroStabilityInwardsSoilProfileTwoDRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override MacroStabilityInwardsSoilProfile2D CreateDataModel() { return new MacroStabilityInwardsSoilProfile2D("", new[] { new MacroStabilityInwardsSoilLayer2D(new Ring(new[] { new Point2D(0, 0), new Point2D(1, 1) }), new Ring[0]) }, Enumerable.Empty()); } } [TestFixture] private class MacroStabilityInwardsSurfaceLineRegistryTest : RegistryTest { public MacroStabilityInwardsSurfaceLineRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override MacroStabilityInwardsSurfaceLine CreateDataModel() { return new MacroStabilityInwardsSurfaceLine(nameof(MacroStabilityInwardsSurfaceLine)); } } #endregion #region HeightStructures #region StabilityPointStructures [TestFixture] private class StabilityPointStructureStructureRegistryTest : RegistryTest< StabilityPointStructure, StabilityPointStructureEntity> { public StabilityPointStructureStructureRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override StabilityPointStructure CreateDataModel() { return new TestStabilityPointStructure(); } } [TestFixture] private class StabilityPointStructuresCalculationRegistryTest : RegistryTest< StructuresCalculation, StabilityPointStructuresCalculationEntity> { public StabilityPointStructuresCalculationRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override StructuresCalculation CreateDataModel() { return new StructuresCalculation(); } } #endregion [TestFixture] private class HeightStructureRegistryTest : RegistryTest< HeightStructure, HeightStructureEntity> { public HeightStructureRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override HeightStructure CreateDataModel() { return new TestHeightStructure(); } } [TestFixture] private class HeightStructuresCalculationRegistryTest : RegistryTest< StructuresCalculation, HeightStructuresCalculationEntity> { public HeightStructuresCalculationRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override StructuresCalculation CreateDataModel() { return new StructuresCalculation(); } } #endregion #region ClosingStructures [TestFixture] private class ClosingStructureRegistryTest : RegistryTest< ClosingStructure, ClosingStructureEntity> { public ClosingStructureRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override ClosingStructure CreateDataModel() { return new TestClosingStructure(); } } [TestFixture] private class ClosingStructuresCalculationRegistryTest : RegistryTest< StructuresCalculation, ClosingStructuresCalculationEntity> { public ClosingStructuresCalculationRegistryTest() : base( (r, e, m) => r.Register(e, m), (r, m) => r.Contains(m), (r, m) => r.Get(m)) {} protected override StructuresCalculation CreateDataModel() { return new StructuresCalculation(); } } #endregion } }