Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/Exceptions/StochasticSoilProfileReadException.cs =================================================================== diff -u --- Ringtoets/Piping/src/Ringtoets.Piping.IO/Exceptions/StochasticSoilProfileReadException.cs (revision 0) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/Exceptions/StochasticSoilProfileReadException.cs (revision dc338e85283e37af4984f737a9f7ae0d213596be) @@ -0,0 +1,48 @@ +// 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; + +namespace Ringtoets.Piping.IO.Exceptions +{ + /// + /// The exception that is thrown when failed to read a from the DSoil-database. + /// + public class StochasticSoilProfileReadException : Exception + { + /// + /// Initializes a new instance of the class + /// with a specified error message. + /// + /// The error message that explains the reason for the exception. + public StochasticSoilProfileReadException(string message) : base(message) {} + + /// + /// Initializes a new instance of the class + /// with a specified error message and a reference to the inner exception that is + /// the cause of this exception. + /// + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception, + /// or a null reference if no inner exception is specified. + public StochasticSoilProfileReadException(string message, Exception innerException) : base(message, innerException) {} + } +} \ No newline at end of file Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/Properties/Resources.Designer.cs =================================================================== diff -u -r3bf7e84ac84de7dd0be38be7e9f2372878e733f6 -rdc338e85283e37af4984f737a9f7ae0d213596be --- Ringtoets/Piping/src/Ringtoets.Piping.IO/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision 3bf7e84ac84de7dd0be38be7e9f2372878e733f6) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision dc338e85283e37af4984f737a9f7ae0d213596be) @@ -344,6 +344,24 @@ } /// + /// Looks up a localized string similar to Het stochastisch ondergrondsprofiel bevat geen geldige waarde.. + /// + public static string StochasticSoilProfileDatabaseReader_StochasticSoilProfile_has_invalid_value { + get { + return ResourceManager.GetString("StochasticSoilProfileDatabaseReader_StochasticSoilProfile_has_invalid_value", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to . + /// + public static string String1 { + get { + return ResourceManager.GetString("String1", resourceCulture); + } + } + + /// /// Looks up a localized string similar to op regel {0}. /// public static string TextFile_On_LineNumber_0_ { Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/Properties/Resources.resx =================================================================== diff -u -r3bf7e84ac84de7dd0be38be7e9f2372878e733f6 -rdc338e85283e37af4984f737a9f7ae0d213596be --- Ringtoets/Piping/src/Ringtoets.Piping.IO/Properties/Resources.resx (.../Resources.resx) (revision 3bf7e84ac84de7dd0be38be7e9f2372878e733f6) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/Properties/Resources.resx (.../Resources.resx) (revision dc338e85283e37af4984f737a9f7ae0d213596be) @@ -213,4 +213,10 @@ De coördinaten van het stochastisch ondergrondsmodel bevatten geen geldige waarde. + + Het stochastisch ondergrondsprofiel bevat geen geldige waarde. + + + + \ No newline at end of file Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/Ringtoets.Piping.IO.csproj =================================================================== diff -u -r10779bb6a6db2d00f4627b2bc190e7e35e1fee3e -rdc338e85283e37af4984f737a9f7ae0d213596be --- Ringtoets/Piping/src/Ringtoets.Piping.IO/Ringtoets.Piping.IO.csproj (.../Ringtoets.Piping.IO.csproj) (revision 10779bb6a6db2d00f4627b2bc190e7e35e1fee3e) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/Ringtoets.Piping.IO.csproj (.../Ringtoets.Piping.IO.csproj) (revision dc338e85283e37af4984f737a9f7ae0d213596be) @@ -49,14 +49,16 @@ + - + + @@ -110,10 +112,6 @@ - - ..\..\..\..\packages\log4net.2.0.4\lib\net40-full\log4net.dll - True - C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.dll Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/PipingSoilProfileReader.cs =================================================================== diff -u -r10779bb6a6db2d00f4627b2bc190e7e35e1fee3e -rdc338e85283e37af4984f737a9f7ae0d213596be --- Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/PipingSoilProfileReader.cs (.../PipingSoilProfileReader.cs) (revision 10779bb6a6db2d00f4627b2bc190e7e35e1fee3e) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/PipingSoilProfileReader.cs (.../PipingSoilProfileReader.cs) (revision dc338e85283e37af4984f737a9f7ae0d213596be) @@ -196,13 +196,7 @@ /// Prepares a new data reader with queries for obtaining the profiles and updates the reader /// so that it points to the first row of the result set. /// - /// Thrown when - /// - /// Version of the database does not match the required version. - /// Version of the database could not be read. - /// Amount of profiles in database could not be read. - /// - /// + /// Thrown when the amount of profiles in database could not be read. /// A query could not be executed on the database schema. private void InitializeReader() { Fisheye: Tag dc338e85283e37af4984f737a9f7ae0d213596be refers to a dead (removed) revision in file `Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/StochasticSoilModelDatabaseReader.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/StochasticSoilModelReader.cs =================================================================== diff -u --- Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/StochasticSoilModelReader.cs (revision 0) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/StochasticSoilModelReader.cs (revision dc338e85283e37af4984f737a9f7ae0d213596be) @@ -0,0 +1,183 @@ +// 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.Data; +using System.Data.SQLite; +using Core.Common.Base.Geometry; +using Core.Common.IO.Exceptions; +using Core.Common.IO.Readers; +using Core.Common.Utils.Builders; +using Ringtoets.Piping.Data; +using Ringtoets.Piping.IO.Builders; +using Ringtoets.Piping.IO.Exceptions; +using Ringtoets.Piping.IO.Properties; + +namespace Ringtoets.Piping.IO.SoilProfile +{ + /// + /// This class reads a DSoil database file and reads from this database. + /// + public class StochasticSoilModelReader : SqLiteDatabaseReaderBase + { + private const string pipingMechanismName = "Piping"; + + private SQLiteDataReader 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 . + /// The database version could not be read. + /// The database version is incorrect. + /// + public StochasticSoilModelReader(string databaseFilePath) : base(databaseFilePath) + { + VerifyVersion(databaseFilePath); + InitializeReader(); + } + + /// + /// Gets a value indicating whether or not more stochastic soil profiles can be read using + /// the . + /// + public bool HasNext { get; private set; } + + public StochasticSoilModel ReadStochasticSoilModel() + { + try + { + return ReadPipingStochasticSoilModel(); + } + catch (SystemException exception) + { + if (exception is FormatException || exception is OverflowException || exception is InvalidCastException) + { + var message = new FileReaderErrorMessageBuilder(Path).Build(Resources.StochasticSoilProfileDatabaseReader_StochasticSoilProfile_has_invalid_value); + throw new StochasticSoilProfileReadException(message, exception); + } + throw; + } + } + + public override void Dispose() + { + if (dataReader != null) + { + dataReader.Dispose(); + } + base.Dispose(); + } + + private StochasticSoilModel ReadPipingStochasticSoilModel() + { + var stochasticSoilModelSegment = ReadStochasticSoilModelSegment(); + var currentSegmentSoilModelId = stochasticSoilModelSegment.SegmentSoilModelId; + do + { + // Read Points + var point2D = ReadSegmentPoint(dataReader); + if (point2D != null) + { + stochasticSoilModelSegment.Geometry.Add(point2D); + } + MoveNext(); + } while (HasNext && ReadStochasticSoilModelSegment().SegmentSoilModelId == currentSegmentSoilModelId); + + return stochasticSoilModelSegment; + } + + /// + /// Prepares a new data reader with queries for obtaining the models and updates the reader + /// so that it points to the first row of the result set. + /// + /// A query could not be executed on the database schema. + private void InitializeReader() + { + CreateDataReader(); + MoveNext(); + } + + /// + /// Moves the reader to the next record in the database. + /// + private void MoveNext() + { + HasNext = MoveNext(dataReader); + } + + private void CreateDataReader() + { + var stochasticSoilModelSegmentsQuery = SoilDatabaseQueryBuilder.GetStochasticSoilModelOfMechanismQuery(); + var sqliteParameter = new SQLiteParameter + { + DbType = DbType.String, + ParameterName = String.Format("@{0}", MechanismDatabaseColumns.MechanismName), + Value = pipingMechanismName + }; + try + { + dataReader = CreateDataReader(stochasticSoilModelSegmentsQuery, sqliteParameter); + } + catch (SQLiteException exception) + { + CloseConnection(); + var message = new FileReaderErrorMessageBuilder(Path).Build(Resources.StochasticSoilModelDatabaseReader_Failed_to_read_database); + throw new CriticalFileReadException(message, exception); + } + } + + private void VerifyVersion(string databaseFilePath) + { + using (var versionReader = new SoilDatabaseVersionReader(databaseFilePath)) + { + try + { + versionReader.VerifyVersion(); + } + catch (CriticalFileReadException) + { + CloseConnection(); + throw; + } + } + } + + private StochasticSoilModel ReadStochasticSoilModelSegment() + { + var stochasticSoilModelId = Convert.ToInt64(dataReader[StochasticSoilModelDatabaseColumns.StochasticSoilModelId]); + var stochasticSoilModelName = Convert.ToString(dataReader[StochasticSoilModelDatabaseColumns.StochasticSoilModelName]); + var segmentName = Convert.ToString(dataReader[SegmentDatabaseColumns.SegmentName]); + return new StochasticSoilModel(stochasticSoilModelId, stochasticSoilModelName, segmentName); + } + + private static Point2D ReadSegmentPoint(SQLiteDataReader dataReader) + { + double coordinateX = Convert.ToDouble(dataReader[SegmentPointsDatabaseColumns.CoordinateX]); + double coordinateY = Convert.ToDouble(dataReader[SegmentPointsDatabaseColumns.CoordinateY]); + return new Point2D(coordinateX, coordinateY); + } + } +} \ No newline at end of file Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/StochasticSoilProfileReader.cs =================================================================== diff -u --- Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/StochasticSoilProfileReader.cs (revision 0) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/StochasticSoilProfileReader.cs (revision dc338e85283e37af4984f737a9f7ae0d213596be) @@ -0,0 +1,209 @@ +// 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.Data.SQLite; +using Core.Common.IO.Exceptions; +using Core.Common.IO.Readers; +using Core.Common.Utils.Builders; +using Ringtoets.Piping.Data; +using Ringtoets.Piping.IO.Builders; +using Ringtoets.Piping.IO.Exceptions; +using Ringtoets.Piping.IO.Properties; + +namespace Ringtoets.Piping.IO.SoilProfile +{ + /// + /// This class reads a DSoil database file and reads from this database. + /// + public class StochasticSoilProfileReader : SqLiteDatabaseReaderBase + { + private SQLiteDataReader 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 . + /// The database version could not be read. + /// The database version is incorrect. + /// + public StochasticSoilProfileReader(string databaseFilePath) : base(databaseFilePath) + { + VerifyVersion(databaseFilePath); + InitializeReader(); + } + + /// + /// Gets a value indicating whether or not more stochastic soil profiles can be read using + /// the . + /// + public bool HasNext { get; private set; } + + + public StochasticSoilProfile ReadStochasticSoilProfile(long stochasticSoilModelId) + { + MoveToStochasticSoilModelId(stochasticSoilModelId); + + try + { + StochasticSoilProfile stochasticSoilProfile = ReadStochasticSoilProfileProbability(); + MoveToNextStochasticSoilModelId(stochasticSoilModelId); + return stochasticSoilProfile; + } + catch (SystemException exception) + { + if (exception is FormatException || exception is OverflowException || exception is InvalidCastException) + { + var message = new FileReaderErrorMessageBuilder(Path).Build(Resources.StochasticSoilProfileDatabaseReader_StochasticSoilProfile_has_invalid_value); + throw new StochasticSoilProfileReadException(message, exception); + } + throw; + } + } + + public override void Dispose() + { + if (dataReader != null) + { + dataReader.Dispose(); + } + base.Dispose(); + } + + /// + /// Prepares a new data reader with queries for obtaining the profiles and updates the reader + /// so that it points to the first row of the result set. + /// + /// A query could not be executed on the database schema. + private void InitializeReader() + { + CreateDataReader(); + MoveNext(); + } + + /// + /// Moves the reader to the next record in the database. + /// + private void MoveNext() + { + HasNext = MoveNext(dataReader); + } + + private void CreateDataReader() + { + var stochasticSoilProfileQuery = SoilDatabaseQueryBuilder.GetAllStochasticSoilProfileQuery(); + try + { + dataReader = CreateDataReader(stochasticSoilProfileQuery); + } + catch (SQLiteException exception) + { + CloseConnection(); + var message = new FileReaderErrorMessageBuilder(Path).Build(Resources.StochasticSoilModelDatabaseReader_Failed_to_read_database); + throw new CriticalFileReadException(message, exception); + } + } + + private void MoveToStochasticSoilModelId(long stochasticSoilModelId) + { + while (HasNext && ReadStochasticSoilModelId() < stochasticSoilModelId) + { + MoveNext(); + } + } + + private void MoveToNextStochasticSoilModelId(long stochasticSoilModelId) + { + while (HasNext && ReadStochasticSoilModelId() == stochasticSoilModelId) + { + MoveNext(); + } + } + + private void VerifyVersion(string databaseFilePath) + { + using (var versionReader = new SoilDatabaseVersionReader(databaseFilePath)) + { + try + { + versionReader.VerifyVersion(); + } + catch (CriticalFileReadException) + { + CloseConnection(); + throw; + } + } + } + + private long ReadStochasticSoilModelId() + { + return Convert.ToInt64(dataReader[StochasticSoilProfileDatabaseColumns.StochasticSoilModelId]); + } + + private StochasticSoilProfile ReadStochasticSoilProfileProbability() + { + var valueProbability = dataReader[StochasticSoilProfileDatabaseColumns.Probability]; + var probability = (valueProbability.Equals(DBNull.Value)) ? 0 : Convert.ToDouble(valueProbability); + + var soilProfile1DId = ReadSoilProfile1DId(probability); + if (soilProfile1DId != null) + { + return soilProfile1DId; + } + + var soilProfile2DId = ReadSoilProfile2DId(probability); + if (soilProfile2DId != null) + { + return soilProfile2DId; + } + + var message = new FileReaderErrorMessageBuilder(Path).Build(Resources.StochasticSoilProfileDatabaseReader_StochasticSoilProfile_has_invalid_value); + throw new StochasticSoilProfileReadException(message); + } + + private StochasticSoilProfile ReadSoilProfile2DId(double probability) + { + var valueSoilProfile2DId = dataReader[StochasticSoilProfileDatabaseColumns.SoilProfile2DId]; + if (!valueSoilProfile2DId.Equals(DBNull.Value)) + { + var soilProfileId = Convert.ToInt64(valueSoilProfile2DId); + return new StochasticSoilProfile(probability, SoilProfileType.SoilProfile2D, soilProfileId); + } + return null; + } + + private StochasticSoilProfile ReadSoilProfile1DId(double probability) + { + var valueSoilProfile1DId = dataReader[StochasticSoilProfileDatabaseColumns.SoilProfile1DId]; + if (!valueSoilProfile1DId.Equals(DBNull.Value)) + { + var soilProfileId = Convert.ToInt64(valueSoilProfile1DId); + return new StochasticSoilProfile(probability, SoilProfileType.SoilProfile1D, soilProfileId); + } + return null; + } + } +} \ No newline at end of file Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/packages.config =================================================================== diff -u -r14db78eb3fc065c42f2d92c60d9d6b4b43dc9bd7 -rdc338e85283e37af4984f737a9f7ae0d213596be --- Ringtoets/Piping/src/Ringtoets.Piping.IO/packages.config (.../packages.config) (revision 14db78eb3fc065c42f2d92c60d9d6b4b43dc9bd7) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/packages.config (.../packages.config) (revision dc338e85283e37af4984f737a9f7ae0d213596be) @@ -23,6 +23,5 @@ All rights reserved. --> - \ No newline at end of file Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Exceptions/StochasticSoilProfileReadExceptionTest.cs =================================================================== diff -u --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Exceptions/StochasticSoilProfileReadExceptionTest.cs (revision 0) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Exceptions/StochasticSoilProfileReadExceptionTest.cs (revision dc338e85283e37af4984f737a9f7ae0d213596be) @@ -0,0 +1,62 @@ +// 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 NUnit.Framework; +using Ringtoets.Piping.IO.Exceptions; + +namespace Ringtoets.Piping.IO.Test.Exceptions +{ + [TestFixture] + public class StochasticSoilProfileReadExceptionTest + { + [Test] + public void Constructor_WithCustomMessage_InnerExceptionNullAndMessageSetToCustom() + { + // Setup + const string expectedMessage = "Some exception message"; + + // Call + StochasticSoilProfileReadException exception = new StochasticSoilProfileReadException(expectedMessage); + + // Assert + Assert.IsInstanceOf(exception); + Assert.IsNull(exception.InnerException); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + public void Constructor_WithCustomMessageAndInnerException_InnerExceptionSetAndMessageSetToCustom() + { + // Setup + const string expectedMessage = "Some exception message"; + Exception expectedInnerException = new Exception(); + + // Call + StochasticSoilProfileReadException exception = new StochasticSoilProfileReadException(expectedMessage, expectedInnerException); + + // Assert + Assert.IsInstanceOf(exception); + Assert.AreSame(expectedInnerException, exception.InnerException); + Assert.AreEqual(expectedMessage, exception.Message); + } + } +} \ No newline at end of file Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Ringtoets.Piping.IO.Test.csproj =================================================================== diff -u -r10779bb6a6db2d00f4627b2bc190e7e35e1fee3e -rdc338e85283e37af4984f737a9f7ae0d213596be --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Ringtoets.Piping.IO.Test.csproj (.../Ringtoets.Piping.IO.Test.csproj) (revision 10779bb6a6db2d00f4627b2bc190e7e35e1fee3e) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Ringtoets.Piping.IO.Test.csproj (.../Ringtoets.Piping.IO.Test.csproj) (revision dc338e85283e37af4984f737a9f7ae0d213596be) @@ -67,8 +67,10 @@ + - + + Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/SoilDatabaseVersionReaderTest.cs =================================================================== diff -u -r0415d97c8733babb1a31bc11de23ee4b0c5038b6 -rdc338e85283e37af4984f737a9f7ae0d213596be --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/SoilDatabaseVersionReaderTest.cs (.../SoilDatabaseVersionReaderTest.cs) (revision 0415d97c8733babb1a31bc11de23ee4b0c5038b6) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/SoilDatabaseVersionReaderTest.cs (.../SoilDatabaseVersionReaderTest.cs) (revision dc338e85283e37af4984f737a9f7ae0d213596be) @@ -34,7 +34,7 @@ [TestFixture] public class SoilDatabaseVersionReaderTest { - private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Piping.IO, "StochasticSoilModelDatabaseReader"); + private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Piping.IO, "SoilDatabaseVersionReader"); [Test] public void Constructor_NonExistingPath_ThrowsCriticalFileReadException() Fisheye: Tag dc338e85283e37af4984f737a9f7ae0d213596be refers to a dead (removed) revision in file `Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/StochasticSoilModelDatabaseReaderTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/StochasticSoilModelReaderTest.cs =================================================================== diff -u --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/StochasticSoilModelReaderTest.cs (revision 0) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/StochasticSoilModelReaderTest.cs (revision dc338e85283e37af4984f737a9f7ae0d213596be) @@ -0,0 +1,245 @@ +// 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.IO; +using Core.Common.Base.Geometry; +using Core.Common.IO.Exceptions; +using Core.Common.IO.Readers; +using Core.Common.TestUtil; +using Core.Common.Utils.Builders; +using NUnit.Framework; +using Ringtoets.Piping.Data; +using Ringtoets.Piping.IO.Exceptions; +using Ringtoets.Piping.IO.Properties; +using Ringtoets.Piping.IO.SoilProfile; +using UtilsResources = Core.Common.Utils.Properties.Resources; + +namespace Ringtoets.Piping.IO.Test.SoilProfile +{ + [TestFixture] + public class StochasticSoilModelReaderTest + { + private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Piping.IO, "StochasticSoilModelDatabaseReader"); + + [Test] + public void Constructor_NonExistingPath_ThrowsCriticalFileReadException() + { + // Setup + var testFile = Path.Combine(testDataPath, "none.soil"); + + // Call + TestDelegate test = () => { using (new StochasticSoilModelReader(testFile)) {} }; + + // Assert + var exception = Assert.Throws(test); + var expectedMessage = new FileReaderErrorMessageBuilder(testFile).Build(UtilsResources.Error_File_does_not_exist); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + [TestCase(null)] + [TestCase("")] + public void Constructor_FileNullOrEmpty_ThrowsCriticalFileReadException(string fileName) + { + // Setup + var expectedMessage = String.Format("Fout bij het lezen van bestand '{0}': {1}", + fileName, UtilsResources.Error_Path_must_be_specified); + // Call + TestDelegate test = () => { using (new StochasticSoilModelReader(fileName)) {} }; + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + [TestCase("text.txt")] + [TestCase("empty.soil")] + public void Constructor_IncorrectFormatFileOrInvalidSchema_ThrowsPipingCriticalFileReadException(string dbName) + { + // Setup + var dbFile = Path.Combine(testDataPath, dbName); + var expectedMessage = new FileReaderErrorMessageBuilder(dbFile). + Build(String.Format(Resources.PipingSoilProfileReader_Critical_Unexpected_value_on_column, dbName)); + + // Precondition + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile), "Precondition: file can be opened for edits."); + + // Call + TestDelegate test = () => { using (new StochasticSoilModelReader(dbFile)) {} }; + + // Assert + CriticalFileReadException exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + [TestCase("withoutSoilModelTables.soil")] + public void Constructor_InvalidSchemaThatPassesValidation_ThrowsPipingCriticalFileReadException(string dbName) + { + // Setup + var dbFile = Path.Combine(testDataPath, dbName); + var expectedMessage = new FileReaderErrorMessageBuilder(dbFile). + Build(String.Format(Resources.StochasticSoilModelDatabaseReader_Failed_to_read_database, dbName)); + + // Precondition + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile), "Precondition: file can be opened for edits."); + + // Call + TestDelegate test = () => { using (new StochasticSoilModelReader(dbFile)) {} }; + + // Assert + CriticalFileReadException exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void ParameteredConstructor_PathToExistingFile_ExpectedValues() + { + // Setup + var dbName = "emptyschema.soil"; + string dbFile = Path.Combine(testDataPath, dbName); + + // Call + using (var stochasticSoilModelDatabaseReader = new StochasticSoilModelReader(dbFile)) + { + // Assert + Assert.AreEqual(dbFile, stochasticSoilModelDatabaseReader.Path); + Assert.IsInstanceOf(stochasticSoilModelDatabaseReader); + } + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void Constructor_IncorrectVersion_ThrowsCriticalFileReadException() + { + // Setup + const string version = "15.0.5.0"; + string expectedVersionMessage = String.Format(Resources.PipingSoilProfileReader_Database_incorrect_version_requires_Version_0_, version); + const string dbName = "incorrectversion.soil"; + var dbFile = Path.Combine(testDataPath, dbName); + + // Precondition + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile), "Precondition: file can be opened for edits."); + + // Call + TestDelegate test = () => { using (var s = new StochasticSoilModelReader(dbFile)) {} }; + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual(expectedVersionMessage, exception.Message); + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void HasNext_EmptyDatabase_ReturnsFalse() + { + // Setup + var dbName = "emptyschema.soil"; + string dbFile = Path.Combine(testDataPath, dbName); + bool isPrepared = true; + + using (var stochasticSoilModelDatabaseReader = new StochasticSoilModelReader(dbFile)) + { + // Call + isPrepared = stochasticSoilModelDatabaseReader.HasNext; + } + + // Assert + Assert.IsFalse(isPrepared); + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void HasNext_CompleteDatabase_ReturnsTrue() + { + // Setup + var dbName = "complete.soil"; + string dbFile = Path.Combine(testDataPath, dbName); + bool hasNext = false; + + using (var stochasticSoilModelDatabaseReader = new StochasticSoilModelReader(dbFile)) + { + // Call + hasNext = stochasticSoilModelDatabaseReader.HasNext; + } + + // Assert + Assert.IsTrue(hasNext); + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void ReadStochasticSoilProfile_InvalidSegmentPoint_ThrowsStochasticSoilModelReadException() + { + // Setup + string dbName = "invalidSegmentPoint.soil"; + string dbFile = Path.Combine(testDataPath, dbName); + string expectedMessage = new FileReaderErrorMessageBuilder(dbFile) + .Build(Resources.StochasticSoilProfileDatabaseReader_StochasticSoilProfile_has_invalid_value); + + using (var stochasticSoilModelDatabaseReader = new StochasticSoilModelReader(dbFile)) + { + // Call + TestDelegate test = () => stochasticSoilModelDatabaseReader.ReadStochasticSoilModel(); + + // Assert + StochasticSoilProfileReadException exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void ReadStochasticSoilProfile_CompleteDatabase_ReturnsExpectedValues() + { + // Setup + string dbName = "complete.soil"; + string dbFile = Path.Combine(testDataPath, dbName); + const string expectedSegmentName = "36005_Piping"; + const string expectedSegmentSoilModelName = "36005_Piping"; + const long expectedSegmentSoilModelId = 2; + const long expectedSegmentSoilModelPoints = 1797; + + using (var stochasticSoilModelDatabaseReader = new StochasticSoilModelReader(dbFile)) + { + // Call + StochasticSoilModel stochasticSoilModel = stochasticSoilModelDatabaseReader.ReadStochasticSoilModel(); + + // Assert + Assert.IsNotNull(stochasticSoilModel); + Assert.AreEqual(expectedSegmentName, stochasticSoilModel.SegmentName); + Assert.AreEqual(expectedSegmentSoilModelName, stochasticSoilModel.SegmentSoilModelName); + Assert.AreEqual(expectedSegmentSoilModelId, stochasticSoilModel.SegmentSoilModelId); + Assert.AreEqual(expectedSegmentSoilModelPoints, stochasticSoilModel.Geometry.Count); + CollectionAssert.AllItemsAreInstancesOfType(stochasticSoilModel.Geometry, typeof(Point2D)); + CollectionAssert.AllItemsAreInstancesOfType(stochasticSoilModel.StochasticSoilProfileProbabilities, typeof(StochasticSoilProfile)); + Assert.IsTrue(stochasticSoilModelDatabaseReader.HasNext); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + } +} \ No newline at end of file Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/StochasticSoilProfileReaderTest.cs =================================================================== diff -u --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/StochasticSoilProfileReaderTest.cs (revision 0) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/StochasticSoilProfileReaderTest.cs (revision dc338e85283e37af4984f737a9f7ae0d213596be) @@ -0,0 +1,238 @@ +using System; +using System.IO; +using Core.Common.IO.Exceptions; +using Core.Common.IO.Readers; +using Core.Common.TestUtil; +using Core.Common.Utils.Builders; +using NUnit.Framework; +using Ringtoets.Piping.Data; +using Ringtoets.Piping.IO.Exceptions; +using Ringtoets.Piping.IO.Properties; +using Ringtoets.Piping.IO.SoilProfile; +using UtilsResources = Core.Common.Utils.Properties.Resources; + +namespace Ringtoets.Piping.IO.Test.SoilProfile +{ + [TestFixture] + public class StochasticSoilProfileReaderTest + { + private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Piping.IO, "StochasticSoilProfileReader"); + + [Test] + public void Constructor_NonExistingPath_ThrowsCriticalFileReadException() + { + // Setup + var testFile = Path.Combine(testDataPath, "none.soil"); + + // Call + TestDelegate test = () => { using (new StochasticSoilProfileReader(testFile)) {} }; + + // Assert + var exception = Assert.Throws(test); + var expectedMessage = new FileReaderErrorMessageBuilder(testFile).Build(UtilsResources.Error_File_does_not_exist); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + [TestCase(null)] + [TestCase("")] + public void Constructor_FileNullOrEmpty_ThrowsCriticalFileReadException(string fileName) + { + // Setup + var expectedMessage = String.Format("Fout bij het lezen van bestand '{0}': {1}", + fileName, UtilsResources.Error_Path_must_be_specified); + // Call + TestDelegate test = () => { using (new StochasticSoilProfileReader(fileName)) {} }; + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + [TestCase("text.txt")] + [TestCase("empty.soil")] + public void Constructor_IncorrectFormatFileOrInvalidSchema_ThrowsPipingCriticalFileReadException(string dbName) + { + // Setup + var dbFile = Path.Combine(testDataPath, dbName); + var expectedMessage = new FileReaderErrorMessageBuilder(dbFile). + Build(String.Format(Resources.PipingSoilProfileReader_Critical_Unexpected_value_on_column, dbName)); + + // Precondition + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile), "Precondition: file can be opened for edits."); + + // Call + TestDelegate test = () => { using (new StochasticSoilProfileReader(dbFile)) {} }; + + // Assert + CriticalFileReadException exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void ParameteredConstructor_PathToExistingFile_ExpectedValues() + { + // Setup + var dbName = "emptyschema.soil"; + string dbFile = Path.Combine(testDataPath, dbName); + + // Call + using (var stochasticSoilModelDatabaseReader = new StochasticSoilProfileReader(dbFile)) + { + // Assert + Assert.AreEqual(dbFile, stochasticSoilModelDatabaseReader.Path); + Assert.IsInstanceOf(stochasticSoilModelDatabaseReader); + } + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void Constructor_IncorrectVersion_ThrowsCriticalFileReadException() + { + // Setup + const string version = "15.0.5.0"; + string expectedVersionMessage = String.Format(Resources.PipingSoilProfileReader_Database_incorrect_version_requires_Version_0_, version); + const string dbName = "incorrectversion.soil"; + var dbFile = Path.Combine(testDataPath, dbName); + + // Precondition + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile), "Precondition: file can be opened for edits."); + + // Call + TestDelegate test = () => { using (var s = new StochasticSoilProfileReader(dbFile)) {} }; + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual(expectedVersionMessage, exception.Message); + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void Constructor_CorruptDatabase_ThrowsException() + { + // Setup + var dbName = "corruptStochasticSoilProfile.soil"; + string dbFile = Path.Combine(testDataPath, dbName); + + // Call + TestDelegate test = () => { using (new StochasticSoilProfileReader(dbFile)) {} }; + + // Assert + Assert.Throws(test); + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void HasNext_EmptyDatabase_ReturnsFalse() + { + // Setup + var dbName = "emptyschema.soil"; + string dbFile = Path.Combine(testDataPath, dbName); + bool isPrepared = true; + + using (var stochasticSoilProfileReader = new StochasticSoilProfileReader(dbFile)) + { + // Call + isPrepared = stochasticSoilProfileReader.HasNext; + } + + // Assert + Assert.IsFalse(isPrepared); + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void HasNext_CompleteDatabase_ReturnsTrue() + { + // Setup + var dbName = "complete.soil"; + string dbFile = Path.Combine(testDataPath, dbName); + bool hasNext = false; + + using (var stochasticSoilProfileReader = new StochasticSoilProfileReader(dbFile)) + { + // Call + hasNext = stochasticSoilProfileReader.HasNext; + } + + // Assert + Assert.IsTrue(hasNext); + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + [TestCase("invalidStochasticSoilProfile1d.soil")] + [TestCase("invalidStochasticSoilProfile2d.soil")] + public void ReadStochasticSoilProfile_DataNotValidToCast_ThrowsStochasticSoilProfileReadException(string dbName) + { + // Setup + string dbFile = Path.Combine(testDataPath, dbName); + const long stochasticProfileId = 1; + string expectedMessage = new FileReaderErrorMessageBuilder(dbFile) + .Build(Resources.StochasticSoilProfileDatabaseReader_StochasticSoilProfile_has_invalid_value); + + using (var stochasticSoilProfileReader = new StochasticSoilProfileReader(dbFile)) + { + // Call + TestDelegate test = () => stochasticSoilProfileReader.ReadStochasticSoilProfile(stochasticProfileId); + + // Assert + StochasticSoilProfileReadException exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + public void ReadStochasticSoilProfile_NoProfileIdsSet_ThrowsStochasticSoilProfileReadException() + { + // Setup + string dbName = "invalidStochasticSoilProfiles.soil"; + string dbFile = Path.Combine(testDataPath, dbName); + const long stochasticProfileId = 1; + string expectedMessage = new FileReaderErrorMessageBuilder(dbFile) + .Build(Resources.StochasticSoilProfileDatabaseReader_StochasticSoilProfile_has_invalid_value); + + using (var stochasticSoilProfileReader = new StochasticSoilProfileReader(dbFile)) + { + // Call + TestDelegate test = () => stochasticSoilProfileReader.ReadStochasticSoilProfile(stochasticProfileId); + + // Assert + StochasticSoilProfileReadException exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + + [Test] + [TestCase(1, 0.15, SoilProfileType.SoilProfile2D, 2)] + [TestCase(2, 0.15, SoilProfileType.SoilProfile1D, 1)] + public void ReadStochasticSoilProfile_CompleteDatabase_ReturnsExpectedValues(long profileId, double probability, SoilProfileType soilProfileType, long soilProfileId) + { + // Setup + var dbName = "complete.soil"; + string dbFile = Path.Combine(testDataPath, dbName); + + using (var stochasticSoilProfileReader = new StochasticSoilProfileReader(dbFile)) + { + // Call + StochasticSoilProfile stochasticSoilProfile = stochasticSoilProfileReader.ReadStochasticSoilProfile(profileId); + + // Assert + Assert.IsNotNull(stochasticSoilProfile); + Assert.AreEqual(probability, stochasticSoilProfile.Probability); + Assert.AreEqual(soilProfileType, stochasticSoilProfile.SoilProfileType); + Assert.AreEqual(soilProfileId, stochasticSoilProfile.SoilProfileId); + Assert.IsTrue(stochasticSoilProfileReader.HasNext); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + } +} \ No newline at end of file Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/SoilDatabaseVersionReader/incorrectversion.soil =================================================================== diff -u Binary files differ Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/StochasticSoilProfileReader/complete.soil =================================================================== diff -u Binary files differ Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/StochasticSoilProfileReader/corruptStochasticSoilProfile.soil =================================================================== diff -u Binary files differ Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/StochasticSoilProfileReader/empty.soil =================================================================== diff -u Binary files differ Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/StochasticSoilProfileReader/emptyschema.soil =================================================================== diff -u Binary files differ Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/StochasticSoilProfileReader/incorrectversion.soil =================================================================== diff -u Binary files differ Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/StochasticSoilProfileReader/invalidStochasticSoilProfile1d.soil =================================================================== diff -u Binary files differ Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/StochasticSoilProfileReader/invalidStochasticSoilProfile2d.soil =================================================================== diff -u Binary files differ Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/StochasticSoilProfileReader/invalidStochasticSoilProfiles.soil =================================================================== diff -u Binary files differ Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/StochasticSoilProfileReader/text.txt =================================================================== diff -u --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/StochasticSoilProfileReader/text.txt (revision 0) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/StochasticSoilProfileReader/text.txt (revision dc338e85283e37af4984f737a9f7ae0d213596be) @@ -0,0 +1 @@ +SomeText \ No newline at end of file