// Copyright (C) Stichting Deltares 2017. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections.Generic; using System.Linq; using Application.Ringtoets.Storage.DbContext; using Application.Ringtoets.Storage.Read; using Application.Ringtoets.Storage.TestUtil; using Core.Common.Base.Geometry; using NUnit.Framework; 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.Data.TestUtil; using Ringtoets.Piping.Primitives; using Ringtoets.Piping.Primitives.TestUtil; using Ringtoets.StabilityPointStructures.Data; using Ringtoets.StabilityPointStructures.Data.TestUtil; namespace Application.Ringtoets.Storage.Test.Read { [TestFixture] public class ReadConversionCollectorTest { /// /// Test class to test the for the combination of /// and . /// /// The data model. /// The database entity. private abstract class CollectorTest where TDataModel : class where TEntity : class, new() { private readonly Action registerToCollector; private readonly Func containsInCollector; private readonly Func getFromCollector; /// /// Creates a new instance of . /// /// The action to perform to register the entity /// to the collector. /// The action to perform to check whether the entity /// is registered in the collector. /// The action to perform to get the entity from /// the collector. /// Thrown when any input parameter is null. /// /// public CollectorTest() : base( /// (c, e, m) => c.Read(e, m), /// (c, e) => c.Contains(e), /// (c, e) => c.Get(e)) {} /// /// protected CollectorTest(Action registerToCollector, Func containsInCollector, Func getFromCollector) { if (registerToCollector == null) { throw new ArgumentNullException(nameof(registerToCollector)); } if (containsInCollector == null) { throw new ArgumentNullException(nameof(containsInCollector)); } if (getFromCollector == null) { throw new ArgumentNullException(nameof(getFromCollector)); } this.registerToCollector = registerToCollector; this.containsInCollector = containsInCollector; this.getFromCollector = getFromCollector; } [Test] public void Contains_EntityNull_ThrowsArgumentNullException() { // Setup var collector = new ReadConversionCollector(); // Call TestDelegate test = () => containsInCollector(collector, null); // Assert string paramName = Assert.Throws(test).ParamName; Assert.AreEqual("entity", paramName); } [Test] public void Contains_DataModelAdded_ReturnsTrue() { // Setup var collector = new ReadConversionCollector(); var entity = new TEntity(); registerToCollector(collector, entity, CreateDataModel()); // Call bool result = containsInCollector(collector, entity); // Assert Assert.IsTrue(result); } [Test] public void Contains_EmptyReadConversionCollector_ReturnsFalse() { // Setup var collector = new ReadConversionCollector(); var entity = new TEntity(); // Call bool result = containsInCollector(collector, entity); // Assert Assert.IsFalse(result); } [Test] public void Contains_OtherEntityAdded_ReturnsFalse() { // Setup var collector = new ReadConversionCollector(); registerToCollector(collector, new TEntity(), CreateDataModel()); // Call bool result = containsInCollector(collector, new TEntity()); // Assert Assert.IsFalse(result); } [Test] public void Get_EntityNull_ThrowsArgumentNullException() { // Setup var collector = new ReadConversionCollector(); // Call TestDelegate test = () => getFromCollector(collector, null); // Assert string paramName = Assert.Throws(test).ParamName; Assert.AreEqual("entity", paramName); } [Test] public void Get_DataModelAdded_ReturnsEntity() { // Setup var collector = new ReadConversionCollector(); TDataModel dataModel = CreateDataModel(); var entity = new TEntity(); registerToCollector(collector, entity, dataModel); // Call TDataModel result = getFromCollector(collector, entity); // Assert Assert.AreSame(dataModel, result); } [Test] public void Get_NoDataModelAdded_ThrowsInvalidOperationException() { // Setup var collector = new ReadConversionCollector(); var entity = new TEntity(); // Call TestDelegate test = () => getFromCollector(collector, entity); // Assert Assert.Throws(test); } [Test] public void Get_OtherDataModelAdded_ThrowsInvalidOperationException() { // Setup var collector = new ReadConversionCollector(); registerToCollector(collector, new TEntity(), CreateDataModel()); // Call TestDelegate test = () => getFromCollector(collector, new TEntity()); // Assert Assert.Throws(test); } [Test] public void Read_EntityNull_ThrowsArgumentNullException() { // Setup var collector = new ReadConversionCollector(); // Call TestDelegate test = () => registerToCollector(collector, null, CreateDataModel()); // Assert string paramName = Assert.Throws(test).ParamName; Assert.AreEqual("entity", paramName); } [Test] public void Read_DataModelNull_ThrowsArgumentNullException() { // Setup var collector = new ReadConversionCollector(); // Call TestDelegate test = () => registerToCollector(collector, new TEntity(), null); // Assert string paramName = Assert.Throws(test).ParamName; Assert.AreEqual("model", paramName); } /// /// Creates a new instance of . /// /// An instance of . protected abstract TDataModel CreateDataModel(); } [TestFixture] private class HydraulicBoundaryLocationCollectorTest : CollectorTest { public HydraulicBoundaryLocationCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override HydraulicBoundaryLocation CreateDataModel() { return new TestHydraulicBoundaryLocation(); } } #region DuneErosion [TestFixture] private class DuneLocationCollectorTest : CollectorTest { public DuneLocationCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override DuneLocation CreateDataModel() { return new TestDuneLocation(); } } #endregion [TestFixture] private class FailureMechanismSectionCollectorTest : CollectorTest { public FailureMechanismSectionCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override FailureMechanismSection CreateDataModel() { return new TestFailureMechanismSection(); } } [TestFixture] private class DikeProfileCollectorTest : CollectorTest { public DikeProfileCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override DikeProfile CreateDataModel() { return DikeProfileTestFactory.CreateDikeProfile(); } } [TestFixture] private class ForeshoreProfileCollectorTest : CollectorTest { public ForeshoreProfileCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override ForeshoreProfile CreateDataModel() { return new TestForeshoreProfile(); } } #region GrassCoverErosionInwards [TestFixture] private class GrassCoverErosionInwardsCalculationCollectorTest : CollectorTest { public GrassCoverErosionInwardsCalculationCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override GrassCoverErosionInwardsCalculation CreateDataModel() { return new GrassCoverErosionInwardsCalculation(); } } #endregion #region HeightStructure [TestFixture] private class HeightStructureCollectorTest : CollectorTest { public HeightStructureCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override HeightStructure CreateDataModel() { return new TestHeightStructure(); } } [TestFixture] private class HeightStructureCalculationCollectorTest : CollectorTest, HeightStructuresCalculationEntity> { public HeightStructureCalculationCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override StructuresCalculation CreateDataModel() { return new StructuresCalculation(); } } #endregion #region ClosingStructure [TestFixture] private class ClosingStructureCollectorTest : CollectorTest { public ClosingStructureCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override ClosingStructure CreateDataModel() { return new TestClosingStructure(); } } [TestFixture] private class ClosingStructureCalculationCollectorTest : CollectorTest, ClosingStructuresCalculationEntity> { public ClosingStructureCalculationCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override StructuresCalculation CreateDataModel() { return new StructuresCalculation(); } } #endregion #region StabilityPointStructure [TestFixture] private class StabilityPointStructureCollectorTest : CollectorTest { public StabilityPointStructureCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override StabilityPointStructure CreateDataModel() { return new TestStabilityPointStructure(); } } [TestFixture] private class StabilityPointStructureCalculationCollectorTest : CollectorTest, StabilityPointStructuresCalculationEntity> { public StabilityPointStructureCalculationCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override StructuresCalculation CreateDataModel() { return new StructuresCalculation(); } } #endregion #region Piping [TestFixture] private class PipingSoilProfileCollectorTest : CollectorTest { public PipingSoilProfileCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override PipingSoilProfile CreateDataModel() { return PipingSoilProfileTestFactory.CreatePipingSoilProfile(); } } [TestFixture] private class PipingStochasticSoilProfileCollectorTest : CollectorTest { public PipingStochasticSoilProfileCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override PipingStochasticSoilProfile CreateDataModel() { return new PipingStochasticSoilProfile(1, PipingSoilProfileTestFactory.CreatePipingSoilProfile()); } } [TestFixture] private class PipingStochasticSoilModelCollectorTest : CollectorTest { public PipingStochasticSoilModelCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.ContainsPipingStochasticSoilModel(e), (c, e) => c.GetPipingStochasticSoilModel(e)) {} protected override PipingStochasticSoilModel CreateDataModel() { return PipingStochasticSoilModelTestFactory.CreatePipingStochasticSoilModel(nameof(PipingStochasticSoilModel)); } } [TestFixture] private class PipingSurfaceLineCollectorTest : CollectorTest { public PipingSurfaceLineCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.ContainsPipingSurfaceLine(e), (c, e) => c.GetPipingSurfaceLine(e)) {} protected override PipingSurfaceLine CreateDataModel() { return new PipingSurfaceLine(nameof(PipingSurfaceLine)); } } #endregion #region MacroStabilityInwards [TestFixture] private class MacroStabilityInwardsSoilProfileOneDCollectorTest : CollectorTest { public MacroStabilityInwardsSoilProfileOneDCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override MacroStabilityInwardsSoilProfile1D CreateDataModel() { return new MacroStabilityInwardsSoilProfile1D(nameof(MacroStabilityInwardsSoilProfile1D), 0.0, new[] { new MacroStabilityInwardsSoilLayer1D(0.0) { Data = { IsAquifer = true } } }); } } [TestFixture] private class MacroStabilityInwardsSoilProfileTwoDCollectorTest : CollectorTest { public MacroStabilityInwardsSoilProfileTwoDCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override MacroStabilityInwardsSoilProfile2D CreateDataModel() { return new MacroStabilityInwardsSoilProfile2D(nameof(MacroStabilityInwardsSoilProfile1D), CreateLayers2D(), Enumerable.Empty()); } private static IEnumerable CreateLayers2D() { var outerRing = new Ring(new[] { new Point2D(3, 2), new Point2D(3, 5) }); return new[] { new MacroStabilityInwardsSoilLayer2D(outerRing) }; } } [TestFixture] private class MacroStabilityInwardsStochasticSoilProfileCollectorTest : CollectorTest { public MacroStabilityInwardsStochasticSoilProfileCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.Contains(e), (c, e) => c.Get(e)) {} protected override MacroStabilityInwardsStochasticSoilProfile CreateDataModel() { return new MacroStabilityInwardsStochasticSoilProfile(1, MacroStabilityInwardsSoilProfile1DTestFactory.CreateMacroStabilityInwardsSoilProfile1D()); } } [TestFixture] private class MacroStabilityInwardsStochasticSoilModelCollectorTest : CollectorTest { public MacroStabilityInwardsStochasticSoilModelCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.ContainsMacroStabilityInwardsStochasticSoilModel(e), (c, e) => c.GetMacroStabilityInwardsStochasticSoilModel(e)) {} protected override MacroStabilityInwardsStochasticSoilModel CreateDataModel() { return MacroStabilityInwardsStochasticSoilModelTestFactory.CreateValidStochasticSoilModel(nameof(MacroStabilityInwardsStochasticSoilModel)); } } [TestFixture] private class MacroStabilityInwardsSurfaceLineCollectorTest : CollectorTest { public MacroStabilityInwardsSurfaceLineCollectorTest() : base( (c, e, m) => c.Read(e, m), (c, e) => c.ContainsMacroStabilityInwardsSurfaceLine(e), (c, e) => c.GetMacroStabilityInwardsSurfaceLine(e)) {} protected override MacroStabilityInwardsSurfaceLine CreateDataModel() { return new MacroStabilityInwardsSurfaceLine(nameof(MacroStabilityInwardsSurfaceLine)); } } #endregion } }