Index: Core/Common/src/Core.Common.IO/Readers/SqLiteDatabaseReaderBase.cs =================================================================== diff -u -r7516bd32d4eea4904a5ed0ba43a05ad8e5c868f3 -r1fa1ff1080811742abffbd23346b23cf1a8aef3d --- Core/Common/src/Core.Common.IO/Readers/SqLiteDatabaseReaderBase.cs (.../SqLiteDatabaseReaderBase.cs) (revision 7516bd32d4eea4904a5ed0ba43a05ad8e5c868f3) +++ Core/Common/src/Core.Common.IO/Readers/SqLiteDatabaseReaderBase.cs (.../SqLiteDatabaseReaderBase.cs) (revision 1fa1ff1080811742abffbd23346b23cf1a8aef3d) @@ -106,7 +106,7 @@ /// The query to execute. /// Parameters the is depended on. /// A new instance of . - /// The execution of failed. + /// Thrown when the execution of failed. protected IDataReader CreateDataReader(string queryString, params SQLiteParameter[] parameters) { using (var query = new SQLiteCommand(Connection) Index: Ringtoets/Common/src/Ringtoets.Common.IO/Properties/Resources.Designer.cs =================================================================== diff -u -rc231e2ac0709fa09b47af14f12b8f617c3c6d101 -r1fa1ff1080811742abffbd23346b23cf1a8aef3d --- Ringtoets/Common/src/Ringtoets.Common.IO/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision c231e2ac0709fa09b47af14f12b8f617c3c6d101) +++ Ringtoets/Common/src/Ringtoets.Common.IO/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision 1fa1ff1080811742abffbd23346b23cf1a8aef3d) @@ -1571,6 +1571,24 @@ } /// + /// Looks up a localized string similar to Kritieke fout opgetreden bij het uitlezen van waardes uit kolommen in de database.. + /// + public static string SoilProfileReader_Critical_Unexpected_value_on_column { + get { + return ResourceManager.GetString("SoilProfileReader_Critical_Unexpected_value_on_column", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to De database heeft niet de vereiste versie informatie. Vereiste versie is '{0}'.. + /// + public static string SoilProfileReader_Database_incorrect_version_requires_Version_0 { + get { + return ResourceManager.GetString("SoilProfileReader_Database_incorrect_version_requires_Version_0", resourceCulture); + } + } + + /// /// Looks up a localized string similar to <?xml version="1.0" encoding="utf-8"?><!-- ///Copyright (C) Stichting Deltares 2017. All rights reserved. /// Index: Ringtoets/Common/src/Ringtoets.Common.IO/Properties/Resources.resx =================================================================== diff -u -rc231e2ac0709fa09b47af14f12b8f617c3c6d101 -r1fa1ff1080811742abffbd23346b23cf1a8aef3d --- Ringtoets/Common/src/Ringtoets.Common.IO/Properties/Resources.resx (.../Resources.resx) (revision c231e2ac0709fa09b47af14f12b8f617c3c6d101) +++ Ringtoets/Common/src/Ringtoets.Common.IO/Properties/Resources.resx (.../Resources.resx) (revision 1fa1ff1080811742abffbd23346b23cf1a8aef3d) @@ -791,4 +791,10 @@ Het uittredepunt moet landwaarts van het intredepunt liggen voor locatie '{0}'. + + Kritieke fout opgetreden bij het uitlezen van waardes uit kolommen in de database. + + + De database heeft niet de vereiste versie informatie. Vereiste versie is '{0}'. + \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.IO/Ringtoets.Common.IO.csproj =================================================================== diff -u -r7516bd32d4eea4904a5ed0ba43a05ad8e5c868f3 -r1fa1ff1080811742abffbd23346b23cf1a8aef3d --- Ringtoets/Common/src/Ringtoets.Common.IO/Ringtoets.Common.IO.csproj (.../Ringtoets.Common.IO.csproj) (revision 7516bd32d4eea4904a5ed0ba43a05ad8e5c868f3) +++ Ringtoets/Common/src/Ringtoets.Common.IO/Ringtoets.Common.IO.csproj (.../Ringtoets.Common.IO.csproj) (revision 1fa1ff1080811742abffbd23346b23cf1a8aef3d) @@ -116,6 +116,8 @@ + + Index: Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/Schema/MetaTableDefinitions.cs =================================================================== diff -u --- Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/Schema/MetaTableDefinitions.cs (revision 0) +++ Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/Schema/MetaTableDefinitions.cs (revision 1fa1ff1080811742abffbd23346b23cf1a8aef3d) @@ -0,0 +1,44 @@ +// 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. + +namespace Ringtoets.Common.IO.SoilProfile.Schema +{ + /// + /// Defines the table and column names of the table '_MetaData' in the DSoil-Model database. + /// + internal static class MetaTableDefinitions + { + /// + /// Gets the name of the table. + /// + public const string TableName = "_MetaData"; + + /// + /// Gets the name of the 'key' column. + /// + public const string Key = "Key"; + + /// + /// Gets the name of the 'value' column. + /// + public const string Value = "Value"; + } +} \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/SoilDatabaseQueryBuilder.cs =================================================================== diff -u --- Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/SoilDatabaseQueryBuilder.cs (revision 0) +++ Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/SoilDatabaseQueryBuilder.cs (revision 1fa1ff1080811742abffbd23346b23cf1a8aef3d) @@ -0,0 +1,45 @@ +// 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 Ringtoets.Common.IO.SoilProfile.Schema; + +namespace Ringtoets.Common.IO.SoilProfile +{ + /// + /// Defines queries to execute on the DSoil-Model database. + /// + internal static class SoilDatabaseQueryBuilder + { + /// + /// Returns the SQL query to execute to check if version of the DSoil-Model database is as expected. + /// + /// The SQL query to execute. + /// "@" + /// needs to be defined as the required database version. + public static string GetCheckVersionQuery() + { + return $"SELECT {MetaTableDefinitions.Value} " + + $"FROM {MetaTableDefinitions.TableName} " + + $"WHERE {MetaTableDefinitions.Key} = 'VERSION' " + + $"AND {MetaTableDefinitions.Value} = @{MetaTableDefinitions.Value};"; + } + } +} \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/SoilDatabaseVersionReader.cs =================================================================== diff -u -r7516bd32d4eea4904a5ed0ba43a05ad8e5c868f3 -r1fa1ff1080811742abffbd23346b23cf1a8aef3d --- Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/SoilDatabaseVersionReader.cs (.../SoilDatabaseVersionReader.cs) (revision 7516bd32d4eea4904a5ed0ba43a05ad8e5c868f3) +++ Ringtoets/Common/src/Ringtoets.Common.IO/SoilProfile/SoilDatabaseVersionReader.cs (.../SoilDatabaseVersionReader.cs) (revision 1fa1ff1080811742abffbd23346b23cf1a8aef3d) @@ -19,8 +19,13 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System.Data; +using System.Data.SQLite; using Core.Common.Base.IO; using Core.Common.IO.Readers; +using Core.Common.Utils.Builders; +using Ringtoets.Common.IO.Properties; +using Ringtoets.Common.IO.SoilProfile.Schema; namespace Ringtoets.Common.IO.SoilProfile { @@ -29,17 +34,72 @@ /// public class SoilDatabaseVersionReader : SqLiteDatabaseReaderBase { + private const string databaseRequiredVersion = "15.0.6.0"; + /// /// Creates a new instance of , /// which will use the as its source. /// /// The path of the database file to open. - /// Thrown when: + /// Thrown when: + /// /// The contains invalid characters. /// No file could be found at . + /// + /// + public SoilDatabaseVersionReader(string databaseFilePath) : base(databaseFilePath) {} + + /// + /// Verifies if the database has the required version. + /// + /// Thrown when: + /// /// The database version could not be read. /// The database version is incorrect. - /// - public SoilDatabaseVersionReader(string databaseFilePath) : base(databaseFilePath) {} + /// + /// + public void VerifyVersion() + { + string checkVersionQuery = SoilDatabaseQueryBuilder.GetCheckVersionQuery(); + var sqliteParameter = new SQLiteParameter + { + DbType = DbType.String, + ParameterName = $"@{MetaTableDefinitions.Value}", + Value = databaseRequiredVersion + }; + + try + { + ReadVersion(checkVersionQuery, sqliteParameter); + } + catch (SQLiteException exception) + { + string exceptionMessage = new FileReaderErrorMessageBuilder(Path).Build( + Resources.SoilProfileReader_Critical_Unexpected_value_on_column); + throw new CriticalFileReadException(exceptionMessage, exception); + } + } + + /// + /// Reads if the required version was found in the database. + /// + /// The query to execute. + /// The parameter containing the required version. + /// Thrown when the execution of + /// failed. + /// Thrown when the database version is incorrect. + private void ReadVersion(string checkVersionQuery, SQLiteParameter sqliteParameter) + { + using (IDataReader dataReader = CreateDataReader(checkVersionQuery, sqliteParameter)) + { + if (!dataReader.Read()) + { + string exceptionMessage = new FileReaderErrorMessageBuilder(Path).Build( + string.Format(Resources.SoilProfileReader_Database_incorrect_version_requires_Version_0, + databaseRequiredVersion)); + throw new CriticalFileReadException(exceptionMessage); + } + } + } } } \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/Ringtoets.Common.IO.Test.csproj =================================================================== diff -u -r7516bd32d4eea4904a5ed0ba43a05ad8e5c868f3 -r1fa1ff1080811742abffbd23346b23cf1a8aef3d --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/Ringtoets.Common.IO.Test.csproj (.../Ringtoets.Common.IO.Test.csproj) (revision 7516bd32d4eea4904a5ed0ba43a05ad8e5c868f3) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/Ringtoets.Common.IO.Test.csproj (.../Ringtoets.Common.IO.Test.csproj) (revision 1fa1ff1080811742abffbd23346b23cf1a8aef3d) @@ -109,6 +109,7 @@ + Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/SoilDatabaseQueryBuilderTest.cs =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/SoilDatabaseQueryBuilderTest.cs (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/SoilDatabaseQueryBuilderTest.cs (revision 1fa1ff1080811742abffbd23346b23cf1a8aef3d) @@ -0,0 +1,44 @@ +// 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 NUnit.Framework; +using Ringtoets.Common.IO.SoilProfile; + +namespace Ringtoets.Common.IO.Test.SoilProfile +{ + [TestFixture] + public class SoilDatabaseQueryBuilderTest + { + [Test] + public void GetCheckVersionQuery_Always_ReturnsExpectedValues() + { + // Call + string query = SoilDatabaseQueryBuilder.GetCheckVersionQuery(); + + // Assert + const string expectedQuery = "SELECT Value " + + "FROM _MetaData " + + "WHERE Key = 'VERSION' " + + "AND Value = @Value;"; + Assert.AreEqual(expectedQuery, query); + } + } +} \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/SoilDatabaseVersionReaderTest.cs =================================================================== diff -u -r7516bd32d4eea4904a5ed0ba43a05ad8e5c868f3 -r1fa1ff1080811742abffbd23346b23cf1a8aef3d --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/SoilDatabaseVersionReaderTest.cs (.../SoilDatabaseVersionReaderTest.cs) (revision 7516bd32d4eea4904a5ed0ba43a05ad8e5c868f3) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/SoilDatabaseVersionReaderTest.cs (.../SoilDatabaseVersionReaderTest.cs) (revision 1fa1ff1080811742abffbd23346b23cf1a8aef3d) @@ -70,8 +70,7 @@ public void Constructor_PathToExistingFile_ExpectedValues() { // Setup - const string dbName = "emptyschema.soil"; - string dbFile = Path.Combine(testDataPath, dbName); + string dbFile = Path.Combine(testDataPath, "emptySchema.soil"); // Call using (var reader = new SoilDatabaseVersionReader(dbFile)) @@ -80,7 +79,69 @@ Assert.AreEqual(dbFile, reader.Path); Assert.IsInstanceOf(reader); } + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); } + + [Test] + public void Constructor_IncorrectSchema_ThrowsCriticalFileReadException() + { + // Setup + string filePath = Path.Combine(testDataPath, "incorrectSchema.soil"); + using (var versionReader = new SoilDatabaseVersionReader(filePath)) + { + // Call + TestDelegate test = () => versionReader.VerifyVersion(); + + // Assert + string message = $"Fout bij het lezen van bestand '{filePath}':" + + " kritieke fout opgetreden bij het uitlezen van waardes uit kolommen in de database."; + + var exception = Assert.Throws(test); + Assert.AreEqual(message, exception.Message); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(filePath)); + } + + [Test] + public void Constructor_IncorrectVersion_ThrowsCriticalFileReadException() + { + // Setup + string filePath = Path.Combine(testDataPath, "incorrectVersion.soil"); + using (var versionReader = new SoilDatabaseVersionReader(filePath)) + { + // Call + TestDelegate test = () => versionReader.VerifyVersion(); + + // Assert + const string requiredVersion = "15.0.6.0"; + string expectedVersionMessage = $"Fout bij het lezen van bestand '{filePath}': " + + "de database heeft niet de vereiste versie informatie. " + + $"Vereiste versie is '{requiredVersion}'."; + + var exception = Assert.Throws(test); + Assert.AreEqual(expectedVersionMessage, exception.Message); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(filePath)); + } + + [Test] + public void Constructor_CorrectVersion_DoesNotThrowException() + { + // Setup + string filePath = Path.Combine(testDataPath, "correctVersion.soil"); + using (var versionReader = new SoilDatabaseVersionReader(filePath)) + { + // Call + TestDelegate test = () => versionReader.VerifyVersion(); + + // Assert + Assert.DoesNotThrow(test); + } + + Assert.IsTrue(TestHelper.CanOpenFileForWrite(filePath)); + } } } \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/SoilDatabaseVersionReader/correctVersion.soil =================================================================== diff -u Binary files differ Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/SoilDatabaseVersionReader/empty.soil =================================================================== diff -u -r7516bd32d4eea4904a5ed0ba43a05ad8e5c868f3 -r1fa1ff1080811742abffbd23346b23cf1a8aef3d Binary files differ Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/SoilDatabaseVersionReader/incorrectSchema.soil =================================================================== diff -u Binary files differ Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/test-data/SoilDatabaseVersionReader/incorrectVersion.soil =================================================================== diff -u Binary files differ