Index: Ringtoets/Common/src/Ringtoets.Common.IO/Ringtoets.Common.IO.csproj =================================================================== diff -u -r294e845538e8efe25b73287baa7f32861b813246 -rf23ce41a1dd004c56ab38179b6b28cd389edba36 --- Ringtoets/Common/src/Ringtoets.Common.IO/Ringtoets.Common.IO.csproj (.../Ringtoets.Common.IO.csproj) (revision 294e845538e8efe25b73287baa7f32861b813246) +++ Ringtoets/Common/src/Ringtoets.Common.IO/Ringtoets.Common.IO.csproj (.../Ringtoets.Common.IO.csproj) (revision f23ce41a1dd004c56ab38179b6b28cd389edba36) @@ -123,7 +123,8 @@ - + + Fisheye: Tag f23ce41a1dd004c56ab38179b6b28cd389edba36 refers to a dead (removed) revision in file `Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/PreconsolidationStressProperties.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/PreconsolidationStressReadValues.cs =================================================================== diff -u --- Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/PreconsolidationStressReadValues.cs (revision 0) +++ Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/PreconsolidationStressReadValues.cs (revision f23ce41a1dd004c56ab38179b6b28cd389edba36) @@ -0,0 +1,122 @@ +// 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 Core.Common.IO.Readers; +using Core.Common.Utils.Builders; +using Ringtoets.Common.IO.Exceptions; +using Ringtoets.Common.IO.Properties; +using Ringtoets.Common.IO.SoilProfile.Schema; + +namespace Ringtoets.Common.IO.SoilProfile +{ + /// + /// Class responsible for reading preconsolidation stress values from a + /// database reader. + /// + public class PreconsolidationStressReadValues + { + /// + /// Creates a new instance of + /// which contains properties that are required to create preconsolidation + /// stresses for a soil layer. + /// + /// The to obtain + /// the preconsolidation stress properties from. + /// The profile name used for generating exception messages + /// if reading property vailes fails. + /// Thrown when any of the parameters + /// are null. + /// Thrown when the values in the database + /// cannot be casted to the expected column types. + public PreconsolidationStressReadValues(IRowBasedDatabaseReader reader, string profileName) + { + if (reader == null) + { + throw new ArgumentNullException(nameof(reader)); + } + if (profileName == null) + { + throw new ArgumentNullException(nameof(profileName)); + } + + string readColumn = null; + try + { + readColumn = PreconsolidationStressTableDefinitions.PreconsolidationStressXCoordinate; + XCoordinate = reader.ReadOrDefault(readColumn); + readColumn = PreconsolidationStressTableDefinitions.PreconsolidationStressZCoordinate; + ZCoordinate = reader.ReadOrDefault(readColumn); + + readColumn = PreconsolidationStressTableDefinitions.PreconsolidationStressDistributionType; + PreconsolidationStressDistributionType = reader.ReadOrDefault(readColumn); + readColumn = PreconsolidationStressTableDefinitions.PreconsolidationStressMean; + PreconsolidationStressMean = reader.ReadOrDefault(readColumn); + readColumn = PreconsolidationStressTableDefinitions.PreconsolidationStressCoefficientOfVariation; + PreconsolidationStressCoefficientOfVariation = reader.ReadOrDefault(readColumn); + readColumn = PreconsolidationStressTableDefinitions.PreconsolidationStressShift; + PreconsolidationStressShift = reader.ReadOrDefault(readColumn); + } + catch (InvalidCastException e) + { + string message = new FileReaderErrorMessageBuilder(reader.Path) + .WithSubject(string.Format(Resources.SoilProfileReader_SoilProfileName_0_, profileName)) + .Build(string.Format(Resources.SoilProfileReader_Profile_has_invalid_value_on_Column_0_, readColumn)); + throw new SoilProfileReadException(message, profileName, e); + } + } + + /// + /// Gets the value representing the X coordinate of the preconsolidation stress location. + /// [m] + /// + public double? XCoordinate { get; } + + /// + /// Gets the value representing the Z coordinate of the preconsolidation stress location. + /// [m] + /// + public double? ZCoordinate { get; } + + /// + /// Gets the distribution type of the preconsolidation stress. + /// + public long? PreconsolidationStressDistributionType { get; } + + /// + /// Gets the value representing the mean of the distribution for the preconsolidation stress. + /// [kN/m�] + /// + public double? PreconsolidationStressMean { get; } + + /// + /// Gets the value representing the coefficient of variation of the distribution for the preconsolidation stress. + /// [kN/m�] + /// + public double? PreconsolidationStressCoefficientOfVariation { get; } + + /// + /// Gets the value representing the shift of the distribution for the preconsolidation stress. + /// [kN/m�] + /// + public double? PreconsolidationStressShift { get; } + } +} \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/PreconsolidationStressReader.cs =================================================================== diff -u --- Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/PreconsolidationStressReader.cs (revision 0) +++ Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/PreconsolidationStressReader.cs (revision f23ce41a1dd004c56ab38179b6b28cd389edba36) @@ -0,0 +1,206 @@ +// 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.Data; +using System.Data.SQLite; +using System.Linq; +using Core.Common.Base.IO; +using Core.Common.IO.Readers; +using Core.Common.Utils.Builders; +using Ringtoets.Common.IO.Exceptions; +using Ringtoets.Common.IO.SoilProfile.Schema; + +namespace Ringtoets.Common.IO.SoilProfile +{ + /// + /// This class reads a D-Soil Model file and reads the preconsolidation stresses from this database. + /// + public class PreconsolidationStressReader : SqLiteDatabaseReaderBase, IRowBasedDatabaseReader + { + private IDataReader dataReader; + + /// + /// Creates a new instance of + /// which will use the as its source. + /// + /// The path of the database file to open. + /// Thrown when: + /// + /// The contains invalid characters; + /// No file could be found at . + /// + /// + public PreconsolidationStressReader(string databaseFilePath) : base(databaseFilePath) {} + + /// + /// Gets a value indicating whether or not more preconsolidation stresses can be read + /// using the . + /// + public bool HasNext { get; private set; } + + /// + /// Initializes the database. + /// + /// Thrown when the query to + /// fetch preconsolidation stresses from the database failed. + public void Initialize() + { + CreateReader(); + MoveNext(); + } + + /// + /// Reads the preconsolidation stresses defined for the soil profile. + /// + /// A collection of preconsolidation stresses defined for the profile. + /// Thrown when the preconsolidation stress + /// could not be read. + public IEnumerable ReadPreconsolidationStresses() + { + if (!HasNext) + { + return Enumerable.Empty(); + } + + var stresses = new List(); + long soilProfileId = ReadSoilProfileId(); + while (HasNext && ReadSoilProfileId() == soilProfileId) + { + stresses.Add(ReadPreconsolidationStress()); + MoveNext(); + } + + return stresses; + } + + /// + /// Reads the soil profile id from the data reader. + /// + /// Thrown when the data reader does not point to a row. + public long ReadSoilProfileId() + { + if (!HasNext) + { + throw new InvalidOperationException("The reader does not have a row to read."); + } + + return Convert.ToInt64(dataReader[SoilProfileTableDefinitions.SoilProfileId]); + } + + public void MoveNext() + { + HasNext = MoveNext(dataReader); + } + + public T Read(string columnName) + { + return (T) dataReader[columnName]; + } + + public T ReadOrDefault(string columnName) + { + object valueObject = dataReader[columnName]; + if (valueObject.Equals(DBNull.Value)) + { + return default(T); + } + return (T) valueObject; + } + + protected override void Dispose(bool disposing) + { + if (dataReader != null) + { + dataReader.Close(); + dataReader.Dispose(); + dataReader = null; + } + base.Dispose(disposing); + } + + /// + /// Reads the preconsolidation stress + /// + /// + private PreconsolidationStress ReadPreconsolidationStress() + { + string profileName = Convert.ToString(dataReader[SoilProfileTableDefinitions.ProfileName]); + var stressReadValues = new PreconsolidationStressReadValues(this, profileName); + + var preconsolidationStress = new PreconsolidationStress(); + + if (stressReadValues.XCoordinate.HasValue) + { + preconsolidationStress.XCoordinate = stressReadValues.XCoordinate.Value; + } + if (stressReadValues.ZCoordinate.HasValue) + { + preconsolidationStress.ZCoordinate = stressReadValues.ZCoordinate.Value; + } + + if (stressReadValues.PreconsolidationStressDistributionType.HasValue) + { + preconsolidationStress.PreconsolidationStressDistributionType = + stressReadValues.PreconsolidationStressDistributionType.Value; + } + if (stressReadValues.PreconsolidationStressMean.HasValue) + { + preconsolidationStress.PreconsolidationStressMean = + stressReadValues.PreconsolidationStressMean.Value; + } + if (stressReadValues.PreconsolidationStressCoefficientOfVariation.HasValue) + { + preconsolidationStress.PreconsolidationStressCoefficientOfVariation = + stressReadValues.PreconsolidationStressCoefficientOfVariation.Value; + } + if (stressReadValues.PreconsolidationStressShift.HasValue) + { + preconsolidationStress.PreconsolidationStressShift = + stressReadValues.PreconsolidationStressShift.Value; + } + + return preconsolidationStress; + } + + /// + /// Creates a new . + /// + /// Thrown when the query to fetch stochastic + /// preconsolidation stresses from the database failed. + private void CreateReader() + { + string soilProfile2DPreconsolidationStressesQuery = + SoilDatabaseQueryBuilder.GetSoilProfile2DPreconsolidationStressesQuery(); + + try + { + dataReader = CreateDataReader(soilProfile2DPreconsolidationStressesQuery); + } + catch (SQLiteException exception) + { + string message = new FileReaderErrorMessageBuilder(Path).Build("Kon geen grensspanningen verkrijgen uit de database."); + throw new CriticalFileReadException(message, exception); + } + } + } +} \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/Schema/PreconsolidationStressTableDefinitions.cs =================================================================== diff -u -r6c0f3118c979a3461ec52ffcc452e9d6ec2b1b76 -rf23ce41a1dd004c56ab38179b6b28cd389edba36 --- Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/Schema/PreconsolidationStressTableDefinitions.cs (.../PreconsolidationStressTableDefinitions.cs) (revision 6c0f3118c979a3461ec52ffcc452e9d6ec2b1b76) +++ Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/Schema/PreconsolidationStressTableDefinitions.cs (.../PreconsolidationStressTableDefinitions.cs) (revision f23ce41a1dd004c56ab38179b6b28cd389edba36) @@ -45,7 +45,7 @@ /// /// The name of the preconsolidation stress distribution column. /// - public const string PreconsolidationStressDistribution = "PreconsolidationStressDistributionType"; + public const string PreconsolidationStressDistributionType = "PreconsolidationStressDistributionType"; /// /// The name of the preconsolidation stress mean column. Index: Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/SoilDatabaseQueryBuilder.cs =================================================================== diff -u -r6c0f3118c979a3461ec52ffcc452e9d6ec2b1b76 -rf23ce41a1dd004c56ab38179b6b28cd389edba36 --- Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/SoilDatabaseQueryBuilder.cs (.../SoilDatabaseQueryBuilder.cs) (revision 6c0f3118c979a3461ec52ffcc452e9d6ec2b1b76) +++ Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/SoilDatabaseQueryBuilder.cs (.../SoilDatabaseQueryBuilder.cs) (revision f23ce41a1dd004c56ab38179b6b28cd389edba36) @@ -341,10 +341,10 @@ return "SELECT " + "sp2d.SP2D_Name AS ProfileName, " + - $"SP2D_ID AS {SoilProfileTableDefinitions.SoilProfileId}, " + + $"{StochasticSoilProfileTableDefinitions.SoilProfile2DId} AS {SoilProfileTableDefinitions.SoilProfileId}, " + $"X AS {PreconsolidationStressTableDefinitions.PreconsolidationStressXCoordinate}, " + $"Z AS {PreconsolidationStressTableDefinitions.PreconsolidationStressZCoordinate}, " + - $"s.ST_Dist_Type AS {PreconsolidationStressTableDefinitions.PreconsolidationStressDistribution}, " + + $"s.ST_Dist_Type AS {PreconsolidationStressTableDefinitions.PreconsolidationStressDistributionType}, " + $"s.ST_Mean AS {PreconsolidationStressTableDefinitions.PreconsolidationStressMean}, " + $"s.ST_Variation AS {PreconsolidationStressTableDefinitions.PreconsolidationStressCoefficientOfVariation}, " + $"s.ST_Shift AS {PreconsolidationStressTableDefinitions.PreconsolidationStressShift} " + Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/Ringtoets.Common.IO.Test.csproj =================================================================== diff -u -r6c59d09636a0a170b03797b7bb1f778dd70ac9a7 -rf23ce41a1dd004c56ab38179b6b28cd389edba36 --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/Ringtoets.Common.IO.Test.csproj (.../Ringtoets.Common.IO.Test.csproj) (revision 6c59d09636a0a170b03797b7bb1f778dd70ac9a7) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/Ringtoets.Common.IO.Test.csproj (.../Ringtoets.Common.IO.Test.csproj) (revision f23ce41a1dd004c56ab38179b6b28cd389edba36) @@ -112,7 +112,8 @@ - + + Fisheye: Tag f23ce41a1dd004c56ab38179b6b28cd389edba36 refers to a dead (removed) revision in file `Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/PreconsolidationStressPropertiesTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/PreconsolidationStressReadValuesTest.cs =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/PreconsolidationStressReadValuesTest.cs (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/PreconsolidationStressReadValuesTest.cs (revision f23ce41a1dd004c56ab38179b6b28cd389edba36) @@ -0,0 +1,149 @@ +// 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 Core.Common.IO.Readers; +using NUnit.Framework; +using Rhino.Mocks; +using Ringtoets.Common.IO.Exceptions; +using Ringtoets.Common.IO.SoilProfile; +using Ringtoets.Common.IO.SoilProfile.Schema; + +namespace Ringtoets.Common.IO.Test.SoilProfile +{ + [TestFixture] + public class PreconsolidationStressReadValuesTest + { + [Test] + public void PreconsolidationStressProperties_ReaderNull_ThrowsArgumentNullException() + { + // Call + TestDelegate call = () => new PreconsolidationStressReadValues(null, string.Empty); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual("reader", exception.ParamName); + } + + [Test] + public void PreconsolidationStressProperties_ProfileNameNull_ThrowsArgumentNullException() + { + // Setup + var mockRepository = new MockRepository(); + var reader = mockRepository.Stub(); + mockRepository.ReplayAll(); + + // Call + TestDelegate call = () => new PreconsolidationStressReadValues(reader, null); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual("profileName", exception.ParamName); + + mockRepository.VerifyAll(); + } + + [Test] + public void PreconsolidationStressProperties_WithReaderAndProfileName_SetProperties() + { + // Setup + var random = new Random(42); + double xCoordinate = random.NextDouble(); + double zCoordinate = random.NextDouble(); + long preconsolidationStressDistributionType = random.Next(); + double preconsolidationStressMean = random.Next(); + double preconsolidationStressCoefficientOfVariation = random.Next(); + double preconsolidationStressShift = random.Next(); + + var mockRepository = new MockRepository(); + var reader = mockRepository.StrictMock(); + reader.Expect(r => r.ReadOrDefault(PreconsolidationStressTableDefinitions.PreconsolidationStressXCoordinate)).Return(xCoordinate); + reader.Expect(r => r.ReadOrDefault(PreconsolidationStressTableDefinitions.PreconsolidationStressZCoordinate)).Return(zCoordinate); + reader.Expect(r => r.ReadOrDefault(PreconsolidationStressTableDefinitions.PreconsolidationStressDistributionType)).Return(preconsolidationStressDistributionType); + reader.Expect(r => r.ReadOrDefault(PreconsolidationStressTableDefinitions.PreconsolidationStressMean)).Return(preconsolidationStressMean); + reader.Expect(r => r.ReadOrDefault(PreconsolidationStressTableDefinitions.PreconsolidationStressCoefficientOfVariation)).Return(preconsolidationStressCoefficientOfVariation); + reader.Expect(r => r.ReadOrDefault(PreconsolidationStressTableDefinitions.PreconsolidationStressShift)).Return(preconsolidationStressShift); + mockRepository.ReplayAll(); + + // Call + var properties = new PreconsolidationStressReadValues(reader, string.Empty); + + // Assert + Assert.AreEqual(xCoordinate, properties.XCoordinate); + Assert.AreEqual(zCoordinate, properties.ZCoordinate); + Assert.AreEqual(preconsolidationStressDistributionType, properties.PreconsolidationStressDistributionType); + Assert.AreEqual(preconsolidationStressMean, properties.PreconsolidationStressMean); + Assert.AreEqual(preconsolidationStressCoefficientOfVariation, properties.PreconsolidationStressCoefficientOfVariation); + Assert.AreEqual(preconsolidationStressShift, properties.PreconsolidationStressShift); + mockRepository.VerifyAll(); + } + + [Test] + [TestCaseSource(nameof(PreconsolidationStressProperties))] + public void LayerProperties_ReaderThrowsInvalidCastException_ThrowsSoilProfileReadException(string columnName) + { + // Setup + const string path = "path"; + const string profileName = "SomeProfile"; + + var invalidCastException = new InvalidCastException(); + + var mockRepository = new MockRepository(); + var reader = mockRepository.Stub(); + reader.Stub(r => r.ReadOrDefault(columnName)).Throw(invalidCastException); + reader.Stub(r => r.ReadOrDefault(columnName)).Throw(invalidCastException); + reader.Stub(r => r.ReadOrDefault(columnName)).Throw(invalidCastException); + + reader.Stub(r => r.ReadOrDefault(Arg.Matches(s => s != columnName))) + .Return(0); + reader.Stub(r => r.ReadOrDefault(Arg.Matches(s => s != columnName))) + .Return(0); + reader.Stub(r => r.ReadOrDefault(Arg.Matches(s => s != columnName))) + .Return(""); + reader.Expect(r => r.Path).Return(path); + mockRepository.ReplayAll(); + + // Call + TestDelegate test = () => new PreconsolidationStressReadValues(reader, profileName); + + // Assert + string expectedMessage = $"Fout bij het lezen van bestand '{path}' (ondergrondschematisatie '{profileName}'): " + + $"ondergrondschematisatie bevat geen geldige waarde in kolom '{columnName}'."; + + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + Assert.AreEqual(profileName, exception.ProfileName); + Assert.AreSame(invalidCastException, exception.InnerException); + mockRepository.VerifyAll(); + } + + private static IEnumerable PreconsolidationStressProperties() + { + yield return PreconsolidationStressTableDefinitions.PreconsolidationStressXCoordinate; + yield return PreconsolidationStressTableDefinitions.PreconsolidationStressZCoordinate; + yield return PreconsolidationStressTableDefinitions.PreconsolidationStressDistributionType; + yield return PreconsolidationStressTableDefinitions.PreconsolidationStressMean; + yield return PreconsolidationStressTableDefinitions.PreconsolidationStressCoefficientOfVariation; + yield return PreconsolidationStressTableDefinitions.PreconsolidationStressShift; + } + } +} \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/PreconsolidationStressReaderTest.cs =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/PreconsolidationStressReaderTest.cs (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/PreconsolidationStressReaderTest.cs (revision f23ce41a1dd004c56ab38179b6b28cd389edba36) @@ -0,0 +1,286 @@ +// 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.IO; +using System.Linq; +using Core.Common.Base.IO; +using Core.Common.IO.Readers; +using Core.Common.TestUtil; +using Core.Common.Utils.Builders; +using NUnit.Framework; +using Ringtoets.Common.IO.SoilProfile; + +namespace Ringtoets.Common.IO.Test.SoilProfile +{ + [TestFixture] + public class PreconsolidationStressReaderTest + { + private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, + nameof(PreconsolidationStressReader)); + + [Test] + public void Constructor_NonExistingFilePath_ThrowsCriticalFileReadException() + { + // Setup + string testFile = Path.Combine(testDataPath, "does not exist"); + + // Call + TestDelegate test = () => + { + using (new PreconsolidationStressReader(testFile)) {} + }; + + // Assert + var exception = Assert.Throws(test); + string expectedMessage = new FileReaderErrorMessageBuilder(testFile).Build("Het bestand bestaat niet."); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + [TestCaseSource(typeof(InvalidPathHelper), nameof(InvalidPathHelper.InvalidPaths))] + public void Constructor_InvalidPath_ThrowsCriticalFileReadException(string fileName) + { + // Call + TestDelegate test = () => + { + using (new PreconsolidationStressReader(fileName)) {} + }; + + // Assert + Assert.Throws(test); + } + + [Test] + public void Constructor_PathToExistingFile_ExpectedValues() + { + // Setup + string dbFile = Path.Combine(testDataPath, "emptySchema.soil"); + + // Call + using (var reader = new PreconsolidationStressReader(dbFile)) + { + // Assert + Assert.AreEqual(dbFile, reader.Path); + Assert.IsInstanceOf(reader); + Assert.IsInstanceOf(reader); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void Initialize_IncorrectFormatFile_ThrowsCriticalFileReadException() + { + // Setup + string dbFile = Path.Combine(testDataPath, "text.txt"); + + using (var reader = new PreconsolidationStressReader(dbFile)) + { + // Call + TestDelegate test = () => reader.Initialize(); + + // Assert + var exception = Assert.Throws(test); + + string expectedMessage = new FileReaderErrorMessageBuilder(dbFile).Build( + "Kon geen grensspanningen verkrijgen uit de database."); + Assert.AreEqual(expectedMessage, exception.Message); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void HasNext_EmptyDatabase_ReturnsFalse() + { + // Setup + string dbFile = Path.Combine(testDataPath, "emptySchema.soil"); + + using (var reader = new PreconsolidationStressReader(dbFile)) + { + reader.Initialize(); + + // Call + bool hasNext = reader.HasNext; + + // Assert + Assert.IsFalse(hasNext); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void HasNext_DatabaseWithStochasticSoilModels_ReturnsTrue() + { + // Setup + string dbFile = Path.Combine(testDataPath, "2dprofileWithPreconsolidationStresses.soil"); + + using (var reader = new PreconsolidationStressReader(dbFile)) + { + reader.Initialize(); + + // Call + bool hasNext = reader.HasNext; + + // Assert + Assert.IsTrue(hasNext); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void ReadStochasticSoilModelId_EmptyDatabase_ThrowsInvalidOperationException() + { + // Setup + string dbFile = Path.Combine(testDataPath, "emptySchema.soil"); + + using (var reader = new PreconsolidationStressReader(dbFile)) + { + reader.Initialize(); + + // Call + TestDelegate test = () => reader.ReadSoilProfileId(); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("The reader does not have a row to read.", exception.Message); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void ReadSoilProfileId_DatabaseWithPreconsolidationStresses_ReturnsCurrentId() + { + // Setup + string dbFile = Path.Combine(testDataPath, "2dprofileWithPreconsolidationStresses.soil"); + + using (var reader = new PreconsolidationStressReader(dbFile)) + { + reader.Initialize(); + + // Call + long id = reader.ReadSoilProfileId(); + + // Assert + Assert.AreEqual(1, id); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void ReadPreconsolidationStresses_FirstSoilProfileInCompleteDatabase_ReturnsExpectedPreconsolidationStresses() + { + // Setup + string dbFile = Path.Combine(testDataPath, "2dprofileWithPreconsolidationStresses.soil"); + + using (var reader = new PreconsolidationStressReader(dbFile)) + { + reader.Initialize(); + + // Call + PreconsolidationStress[] preconsolidationStresses = reader.ReadPreconsolidationStresses().ToArray(); + + // Assert + Assert.AreEqual(4, preconsolidationStresses.Length); + + CollectionAssert.AreEqual(new[] + { + 1, + 2, + 3, + 4 + }, preconsolidationStresses.Select(stress => stress.XCoordinate)); + CollectionAssert.AreEqual(new[] + { + 5, + 6, + 7, + 8 + }, preconsolidationStresses.Select(stress => stress.ZCoordinate)); + + CollectionAssert.AreEqual(new[] + { + 3, + 3, + 3, + 3 + }, preconsolidationStresses.Select(stress => stress.PreconsolidationStressDistributionType)); + CollectionAssert.AreEqual(new[] + { + 1337, + 3371, + 8.5, + 9.3 + }, preconsolidationStresses.Select(stress => stress.PreconsolidationStressMean)); + CollectionAssert.AreEqual(new[] + { + 0.0074794315632011965, + 0.0029664787896766538, + 1.8823529411764706, + 0.8064516129032258 + }, preconsolidationStresses.Select(stress => stress.PreconsolidationStressCoefficientOfVariation)); + CollectionAssert.AreEqual(new[] + { + 11, + 12, + 0, + 0 + }, preconsolidationStresses.Select(stress => stress.PreconsolidationStressShift)); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void ReadPreconsolidationStresses_FirstSoilProfileWithNullValuesInCompleteDatabase_ReturnsDefaultPreconsolidationStressValues() + { + // Setup + string dbFile = Path.Combine(testDataPath, "2dprofileWithPreconsolidationStressesNullValues.soil"); + + using (var reader = new PreconsolidationStressReader(dbFile)) + { + reader.Initialize(); + + // Call + PreconsolidationStress[] preconsolidationStresses = reader.ReadPreconsolidationStresses().ToArray(); + + // Assert + Assert.AreEqual(1, preconsolidationStresses.Length); + PreconsolidationStress actualPreconsolidationStress = preconsolidationStresses[0]; + + Assert.IsNaN(actualPreconsolidationStress.XCoordinate); + Assert.IsNaN(actualPreconsolidationStress.ZCoordinate); + Assert.IsNull(actualPreconsolidationStress.PreconsolidationStressDistributionType); + Assert.IsNaN(actualPreconsolidationStress.PreconsolidationStressMean); + Assert.IsNaN(actualPreconsolidationStress.PreconsolidationStressCoefficientOfVariation); + Assert.IsNaN(actualPreconsolidationStress.PreconsolidationStressShift); + + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + } +} \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/PreconsolidationStressReader/2dprofileWithPreconsolidationStresses.soil =================================================================== diff -u Binary files differ Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/PreconsolidationStressReader/2dprofileWithPreconsolidationStressesNullValues.soil =================================================================== diff -u Binary files differ Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/PreconsolidationStressReader/emptySchema.soil =================================================================== diff -u Binary files differ Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/PreconsolidationStressReader/text.txt =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/PreconsolidationStressReader/text.txt (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/PreconsolidationStressReader/text.txt (revision f23ce41a1dd004c56ab38179b6b28cd389edba36) @@ -0,0 +1 @@ +SomeText \ No newline at end of file