Index: Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/HydraulicBoundaryDatabaseImporter.cs =================================================================== diff -u -r640c6bce50d2ef282bef96ee5686fbe2f893384e -r85fb38b5ff4ef61c053ffef928570cd75cfa9357 --- Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/HydraulicBoundaryDatabaseImporter.cs (.../HydraulicBoundaryDatabaseImporter.cs) (revision 640c6bce50d2ef282bef96ee5686fbe2f893384e) +++ Ringtoets/Common/src/Ringtoets.Common.IO/FileImporters/HydraulicBoundaryDatabaseImporter.cs (.../HydraulicBoundaryDatabaseImporter.cs) (revision 85fb38b5ff4ef61c053ffef928570cd75cfa9357) @@ -31,7 +31,7 @@ using Ringtoets.Common.IO.HydraRing; using Ringtoets.Common.IO.Properties; using Ringtoets.HydraRing.IO.HydraulicBoundaryDatabase; -using Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabaseContext; +using Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabase; namespace Ringtoets.Common.IO.FileImporters { Index: Ringtoets/Common/src/Ringtoets.Common.IO/HydraRing/HydraulicBoundaryDatabaseHelper.cs =================================================================== diff -u -r640c6bce50d2ef282bef96ee5686fbe2f893384e -r85fb38b5ff4ef61c053ffef928570cd75cfa9357 --- Ringtoets/Common/src/Ringtoets.Common.IO/HydraRing/HydraulicBoundaryDatabaseHelper.cs (.../HydraulicBoundaryDatabaseHelper.cs) (revision 640c6bce50d2ef282bef96ee5686fbe2f893384e) +++ Ringtoets/Common/src/Ringtoets.Common.IO/HydraRing/HydraulicBoundaryDatabaseHelper.cs (.../HydraulicBoundaryDatabaseHelper.cs) (revision 85fb38b5ff4ef61c053ffef928570cd75cfa9357) @@ -27,7 +27,7 @@ using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.IO.Properties; using Ringtoets.HydraRing.IO.HydraulicBoundaryDatabase; -using Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabaseContext; +using Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabase; using HydraRingResources = Ringtoets.HydraRing.IO.Properties.Resources; namespace Ringtoets.Common.IO.HydraRing Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationDatabaseQueryBuilder.cs =================================================================== diff -u --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationDatabaseQueryBuilder.cs (revision 0) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationDatabaseQueryBuilder.cs (revision 85fb38b5ff4ef61c053ffef928570cd75cfa9357) @@ -0,0 +1,53 @@ +// 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.HydraRing.IO.HydraulicLocationConfigurationDatabase +{ + /// + /// Defines queries to execute on a hydraulic location configuration database. + /// + public static class HydraulicLocationConfigurationDatabaseQueryBuilder + { + /// + /// Gets the query to get location ids from the database. + /// + /// The query to get location ids from the database. + public static string GetLocationIdsByTrackIdQuery() + { + return $"SELECT {LocationsTableDefinitions.LocationId}, {LocationsTableDefinitions.HrdLocationId} " + + $"FROM {LocationsTableDefinitions.TableName} " + + $"WHERE {LocationsTableDefinitions.TrackId} = @{LocationsTableDefinitions.TrackId} " + + $"ORDER BY {LocationsTableDefinitions.HrdLocationId};"; + } + + /// + /// Gets the query to get region information from the database. + /// + /// The query to get region information from the database. + public static string GetRegionByTrackIdQuery() + { + return $"SELECT * FROM {RegionsTableDefinitions.TableName} " + + $"LEFT JOIN {TracksTableDefinitions.TableName} " + + $"ON {RegionsTableDefinitions.TableName}.{RegionsTableDefinitions.RegionId} = {TracksTableDefinitions.TableName}.{TracksTableDefinitions.RegionId} " + + $"WHERE {TracksTableDefinitions.TableName}.{TracksTableDefinitions.TrackId} = @{TracksTableDefinitions.TrackId}"; + } + } +} \ No newline at end of file Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationSqLiteDatabaseReader.cs =================================================================== diff -u --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationSqLiteDatabaseReader.cs (revision 0) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationSqLiteDatabaseReader.cs (revision 85fb38b5ff4ef61c053ffef928570cd75cfa9357) @@ -0,0 +1,189 @@ +// 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.Exceptions; +using Core.Common.IO.Readers; +using Core.Common.Utils.Builders; +using log4net; +using Ringtoets.HydraRing.IO.Properties; + +namespace Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabase +{ + /// + /// Class for reading information from a hydraulic location configuration database (HLCD). + /// + public class HydraulicLocationConfigurationSqLiteDatabaseReader : SqLiteDatabaseReaderBase + { + private static readonly ILog log = LogManager.GetLogger(typeof(HydraulicLocationConfigurationSqLiteDatabaseReader)); + + /// + /// 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 HydraulicLocationConfigurationSqLiteDatabaseReader(string databaseFilePath) : base(databaseFilePath) {} + + /// + /// Gets the location ids from the database, based upon . + /// + /// The hydraulic boundary track id. + /// A dictionary with pairs of Hrd location id (key) and location id (value) as found in the database. + /// Thrown when the database query failed. + /// Thrown when the database returned incorrect values for + /// required properties. + public Dictionary GetLocationIdsByTrackId(long trackId) + { + var trackParameter = new SQLiteParameter + { + DbType = DbType.String, + ParameterName = LocationsTableDefinitions.TrackId, + Value = trackId + }; + + try + { + return GetLocationIdsFromDatabase(trackParameter); + } + catch (SQLiteException exception) + { + string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicLocationConfigurationSqLiteDatabaseReader_Critical_Unexpected_Exception); + throw new CriticalFileReadException(message, exception); + } + catch (InvalidCastException exception) + { + string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); + throw new LineParseException(message, exception); + } + } + + /// + /// Gets whether the preprocessor can be used for the given . + /// + /// The hydraulic boundary track id. + /// The value found in the database; or false when the database + /// is valid but outdated (no UsePreprocessor column present). + /// Thrown when the database query failed or when no results could be found. + /// Thrown when the database returned incorrect values for + /// required properties. + public bool GetCanUsePreprocessorByTrackId(long trackId) + { + var trackParameter = new SQLiteParameter + { + DbType = DbType.String, + ParameterName = TracksTableDefinitions.TrackId, + Value = trackId + }; + + try + { + return GetCanUsePreprocessorFromDatabase(trackParameter); + } + catch (SQLiteException exception) + { + string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicLocationConfigurationSqLiteDatabaseReader_Critical_Unexpected_Exception); + throw new CriticalFileReadException(message, exception); + } + catch (FormatException exception) + { + string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicBoundaryDatabaseReader_Critical_Unexpected_value_on_column); + throw new LineParseException(message, exception); + } + } + + /// + /// Gets the location ids from the database, based upon . + /// + /// A parameter containing the hydraulic boundary track id. + /// A dictionary with pairs of Hrd location id (key) and location id (value) as found in the database. + /// Thrown when the database query failed. + /// Thrown when the database returned incorrect values for + /// required properties. + private Dictionary GetLocationIdsFromDatabase(SQLiteParameter trackParameter) + { + var dictionary = new Dictionary(); + string query = HydraulicLocationConfigurationDatabaseQueryBuilder.GetLocationIdsByTrackIdQuery(); + using (IDataReader dataReader = CreateDataReader(query, trackParameter)) + { + while (MoveNext(dataReader)) + { + long key = Convert.ToInt64(dataReader[LocationsTableDefinitions.HrdLocationId]); + long value = Convert.ToInt64(dataReader[LocationsTableDefinitions.LocationId]); + + // Must be unique + if (dictionary.ContainsKey(key)) + { + log.Warn(Resources.HydraulicLocationConfigurationSqLiteDatabaseReader_GetLocationIdFromDatabase_Ambiguous_Row_Found_Take_First); + } + else + { + dictionary.Add(key, value); + } + } + } + return dictionary; + } + + /// + /// Gets whether the preprocessor can be used for the given . + /// + /// A parameter containing the hydraulic boundary track id. + /// The value found in the database; or false when the database + /// is valid but outdated (no UsePreprocessor column present). + /// Thrown when the database query failed. + /// Thrown when the database returned incorrect values for + /// required properties. + /// Thrown when no results could be found. + private bool GetCanUsePreprocessorFromDatabase(SQLiteParameter trackParameter) + { + string query = HydraulicLocationConfigurationDatabaseQueryBuilder.GetRegionByTrackIdQuery(); + using (IDataReader dataReader = CreateDataReader(query, trackParameter)) + { + DataTable schemaTable = dataReader.GetSchemaTable(); + DataColumn columnName = schemaTable.Columns[schemaTable.Columns.IndexOf("ColumnName")]; + + if (schemaTable.Rows.Cast().All(row => row[columnName].ToString() != RegionsTableDefinitions.UsePreprocessor)) + { + return false; + } + + if (MoveNext(dataReader)) + { + return Convert.ToBoolean(dataReader[RegionsTableDefinitions.UsePreprocessor]); + } + + string message = new FileReaderErrorMessageBuilder(Path).Build(Resources.HydraulicLocationConfigurationSqLiteDatabaseReader_Critical_Unexpected_Exception); + throw new CriticalFileReadException(message); + } + } + } +} \ No newline at end of file Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/LocationsTableDefinitions.cs =================================================================== diff -u --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/LocationsTableDefinitions.cs (revision 0) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/LocationsTableDefinitions.cs (revision 85fb38b5ff4ef61c053ffef928570cd75cfa9357) @@ -0,0 +1,35 @@ +// 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.HydraRing.IO.HydraulicLocationConfigurationDatabase +{ + /// + /// Defines the table and column names of the table 'Locations' in the hydraulic location + /// configuration database. + /// + internal static class LocationsTableDefinitions + { + public const string TableName = "Locations"; + public const string LocationId = "LocationId"; + public const string HrdLocationId = "HRDLocationId"; + public const string TrackId = "TrackId"; + } +} \ No newline at end of file Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/RegionsTableDefinitions.cs =================================================================== diff -u --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/RegionsTableDefinitions.cs (revision 0) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/RegionsTableDefinitions.cs (revision 85fb38b5ff4ef61c053ffef928570cd75cfa9357) @@ -0,0 +1,34 @@ +// 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.HydraRing.IO.HydraulicLocationConfigurationDatabase +{ + /// + /// Defines the table and column names of the table 'Regions' in the hydraulic location + /// configuration database. + /// + internal static class RegionsTableDefinitions + { + public const string TableName = "Regions"; + public const string RegionId = "RegionId"; + public const string UsePreprocessor = "UsePreprocessor"; + } +} \ No newline at end of file Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/TracksTableDefinitions.cs =================================================================== diff -u --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/TracksTableDefinitions.cs (revision 0) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabase/TracksTableDefinitions.cs (revision 85fb38b5ff4ef61c053ffef928570cd75cfa9357) @@ -0,0 +1,34 @@ +// 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.HydraRing.IO.HydraulicLocationConfigurationDatabase +{ + /// + /// Defines the table and column names of the table 'Tracks' in the hydraulic location + /// configuration database. + /// + internal static class TracksTableDefinitions + { + public const string TableName = "Tracks"; + public const string TrackId = "TrackId"; + public const string RegionId = "RegionId"; + } +} \ No newline at end of file Fisheye: Tag 85fb38b5ff4ef61c053ffef928570cd75cfa9357 refers to a dead (removed) revision in file `Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabaseContext/HydraulicLocationConfigurationDatabaseQueryBuilder.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 85fb38b5ff4ef61c053ffef928570cd75cfa9357 refers to a dead (removed) revision in file `Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabaseContext/HydraulicLocationConfigurationSqLiteDatabaseReader.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 85fb38b5ff4ef61c053ffef928570cd75cfa9357 refers to a dead (removed) revision in file `Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabaseContext/LocationsTableDefinitions.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 85fb38b5ff4ef61c053ffef928570cd75cfa9357 refers to a dead (removed) revision in file `Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabaseContext/RegionsTableDefinitions.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 85fb38b5ff4ef61c053ffef928570cd75cfa9357 refers to a dead (removed) revision in file `Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicLocationConfigurationDatabaseContext/TracksTableDefinitions.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/Ringtoets.HydraRing.IO.csproj =================================================================== diff -u -r640c6bce50d2ef282bef96ee5686fbe2f893384e -r85fb38b5ff4ef61c053ffef928570cd75cfa9357 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/Ringtoets.HydraRing.IO.csproj (.../Ringtoets.HydraRing.IO.csproj) (revision 640c6bce50d2ef282bef96ee5686fbe2f893384e) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/Ringtoets.HydraRing.IO.csproj (.../Ringtoets.HydraRing.IO.csproj) (revision 85fb38b5ff4ef61c053ffef928570cd75cfa9357) @@ -26,11 +26,11 @@ - - - - - + + + + + True Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationDatabaseQueryBuilderTest.cs =================================================================== diff -u --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationDatabaseQueryBuilderTest.cs (revision 0) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationDatabaseQueryBuilderTest.cs (revision 85fb38b5ff4ef61c053ffef928570cd75cfa9357) @@ -0,0 +1,52 @@ +// 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.HydraRing.IO.HydraulicLocationConfigurationDatabase; + +namespace Ringtoets.HydraRing.IO.Test.HydraulicLocationConfigurationDatabase +{ + [TestFixture] + public class HydraulicLocationConfigurationDatabaseQueryBuilderTest + { + [Test] + public void GetLocationIdsByTrackIdQuery_Always_ReturnsExpectedValues() + { + // Call + string query = HydraulicLocationConfigurationDatabaseQueryBuilder.GetLocationIdsByTrackIdQuery(); + + // Assert + const string expectedQuery = "SELECT LocationId, HRDLocationId FROM Locations WHERE TrackId = @TrackId ORDER BY HRDLocationId;"; + Assert.AreEqual(expectedQuery, query); + } + + [Test] + public void GetRegionByTrackIdQuery_Always_ReturnsExpectedValues() + { + // Call + string query = HydraulicLocationConfigurationDatabaseQueryBuilder.GetRegionByTrackIdQuery(); + + // Assert + const string expectedQuery = "SELECT * FROM Regions LEFT JOIN Tracks ON Regions.RegionId = Tracks.RegionId WHERE Tracks.TrackId = @TrackId"; + Assert.AreEqual(expectedQuery, query); + } + } +} \ No newline at end of file Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationSqLiteDatabaseReaderTest.cs =================================================================== diff -u --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationSqLiteDatabaseReaderTest.cs (revision 0) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/HydraulicLocationConfigurationSqLiteDatabaseReaderTest.cs (revision 85fb38b5ff4ef61c053ffef928570cd75cfa9357) @@ -0,0 +1,252 @@ +// 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.SQLite; +using System.IO; +using Core.Common.Base.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.HydraRing.IO.HydraulicLocationConfigurationDatabase; + +namespace Ringtoets.HydraRing.IO.Test.HydraulicLocationConfigurationDatabase +{ + [TestFixture] + public class HydraulicLocationConfigurationSqLiteDatabaseReaderTest + { + private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.HydraRing.IO, "HydraulicLocationConfigurationDatabase"); + + [Test] + public void Constructor_InvalidFile_ThrowsCriticalFileReadException() + { + // Setup + string testFile = Path.Combine(testDataPath, "none.sqlite"); + + // Call + TestDelegate test = () => + { + using (new HydraulicLocationConfigurationSqLiteDatabaseReader(testFile)) {} + }; + + // Assert + string expectedMessage = new FileReaderErrorMessageBuilder(testFile).Build("Het bestand bestaat niet."); + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + public void Constructor_ValidFile_ExpectedValues() + { + // Setup + string dbFile = Path.Combine(testDataPath, "complete.sqlite"); + + // Call + using (var hydraulicBoundarySqLiteDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(dbFile)) + { + // Assert + Assert.IsInstanceOf(hydraulicBoundarySqLiteDatabaseReader); + } + } + + [Test] + [TestCase(18169, 1000, 1801000)] + [TestCase(6, 1000, 0)] + public void GetLocationIdsByTrackId_ValidFile_ExpectedValues(int trackId, int hrdLocationId, int expectedLocationId) + { + // Setup + string dbFile = Path.Combine(testDataPath, "complete.sqlite"); + + using (var hydraulicBoundarySqLiteDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(dbFile)) + { + // Call + Dictionary locationIdDictionary = hydraulicBoundarySqLiteDatabaseReader.GetLocationIdsByTrackId(trackId); + + // Assert + long locationId; + locationIdDictionary.TryGetValue(hrdLocationId, out locationId); + Assert.AreEqual(expectedLocationId, locationId); + } + } + + [Test] + public void GetLocationIdsByTrackId_AmbigousLocations_ReturnsFirstAndLogsWarning() + { + // Setup + string dbFile = Path.Combine(testDataPath, "ambigousLocation.sqlite"); + const int trackId = 18; + const int hrdLocationId = 1; + var locationIdDictionary = new Dictionary(); + + using (var hydraulicBoundarySqLiteDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(dbFile)) + { + // Call + Action call = () => locationIdDictionary = hydraulicBoundarySqLiteDatabaseReader.GetLocationIdsByTrackId(trackId); + + // Assert + const int expectedLocationId = 1800001; + const string expectedMessage = "Er zijn meerdere resultaten gevonden, wat niet voor zou mogen komen. Neem contact op met de leverancier. Het eerste resultaat zal worden gebruikt."; + TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); + long locationId; + locationIdDictionary.TryGetValue(hrdLocationId, out locationId); + Assert.AreEqual(expectedLocationId, locationId); + } + } + + [Test] + public void GetLocationIdsByTrackId_InvalidColumns_ThrowsLineParseException() + { + // Setup + string dbFile = Path.Combine(testDataPath, "corruptschema.sqlite"); + const int trackId = 1; + + using (var hydraulicBoundarySqLiteDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(dbFile)) + { + // Call + TestDelegate test = () => hydraulicBoundarySqLiteDatabaseReader.GetLocationIdsByTrackId(trackId); + + // Assert + string expectedMessage = new FileReaderErrorMessageBuilder(dbFile).Build("Kritieke fout opgetreden bij het uitlezen van waardes uit kolommen in de database."); + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } + } + + [Test] + public void GetLocationIdsByTrackId_EmptyFile_ThrowsCriticalFileReadException() + { + // Setup + string dbFile = Path.Combine(testDataPath, "empty.sqlite"); + const int trackId = 1; + + using (var hydraulicBoundarySqLiteDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(dbFile)) + { + // Call + TestDelegate test = () => hydraulicBoundarySqLiteDatabaseReader.GetLocationIdsByTrackId(trackId); + + // Assert + string expectedMessage = new FileReaderErrorMessageBuilder(dbFile).Build("Het bevragen van de database is mislukt."); + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } + } + + [Test] + [TestCase(1000, true)] + [TestCase(11, false)] + public void GetCanUsePreprocessorByTrackId_ValidFile_ExpectedValues(long trackId, bool expectedUsePreprocessor) + { + // Setup + string dbFile = Path.Combine(testDataPath, "complete.sqlite"); + + using (var hydraulicBoundarySqLiteDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(dbFile)) + { + // Call + bool usePreprocessor = hydraulicBoundarySqLiteDatabaseReader.GetCanUsePreprocessorByTrackId(trackId); + + // Assert + Assert.AreEqual(expectedUsePreprocessor, usePreprocessor); + } + } + + [Test] + public void GetCanUsePreprocessorByTrackId_ValidFileWithoutUsePreprocessorColumn_ReturnFalse() + { + // Setup + string dbFile = Path.Combine(testDataPath, "noUsePreprocessorColumn.sqlite"); + const int trackId = 1000; + + using (var hydraulicBoundarySqLiteDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(dbFile)) + { + // Call + bool usePreprocessor = hydraulicBoundarySqLiteDatabaseReader.GetCanUsePreprocessorByTrackId(trackId); + + // Assert + Assert.IsFalse(usePreprocessor); + } + } + + [Test] + public void GetCanUsePreprocessorByTrackId_UnknownRegionIdForTrackId_ThrowsCriticalFileReadException() + { + // Setup + string dbFile = Path.Combine(testDataPath, "complete.sqlite"); + const int trackId = 1; + + using (var hydraulicBoundarySqLiteDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(dbFile)) + { + // Call + TestDelegate test = () => hydraulicBoundarySqLiteDatabaseReader.GetCanUsePreprocessorByTrackId(trackId); + + // Assert + string expectedMessage = new FileReaderErrorMessageBuilder(dbFile).Build("Het bevragen van de database is mislukt."); + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsNull(exception.InnerException); + } + } + + [Test] + public void GetCanUsePreprocessorByTrackId_EmptyFile_ThrowsCriticalFileReadException() + { + // Setup + string dbFile = Path.Combine(testDataPath, "empty.sqlite"); + const int trackId = 1; + + using (var hydraulicBoundarySqLiteDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(dbFile)) + { + // Call + TestDelegate test = () => hydraulicBoundarySqLiteDatabaseReader.GetCanUsePreprocessorByTrackId(trackId); + + // Assert + string expectedMessage = new FileReaderErrorMessageBuilder(dbFile).Build("Het bevragen van de database is mislukt."); + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } + } + + [Test] + public void GetCanUsePreprocessorByTrackId_InvalidColumns_ThrowsLineParseException() + { + // Setup + string dbFile = Path.Combine(testDataPath, "corruptschema.sqlite"); + const int trackId = 1000; + + using (var hydraulicBoundarySqLiteDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(dbFile)) + { + // Call + TestDelegate test = () => hydraulicBoundarySqLiteDatabaseReader.GetCanUsePreprocessorByTrackId(trackId); + + // Assert + string expectedMessage = new FileReaderErrorMessageBuilder(dbFile).Build("Kritieke fout opgetreden bij het uitlezen van waardes uit kolommen in de database."); + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } + } + } +} \ No newline at end of file Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/LocationsTableDefinitionsTest.cs =================================================================== diff -u --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/LocationsTableDefinitionsTest.cs (revision 0) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/LocationsTableDefinitionsTest.cs (revision 85fb38b5ff4ef61c053ffef928570cd75cfa9357) @@ -0,0 +1,39 @@ +// 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.HydraRing.IO.HydraulicLocationConfigurationDatabase; + +namespace Ringtoets.HydraRing.IO.Test.HydraulicLocationConfigurationDatabase +{ + [TestFixture] + public class LocationsTableDefinitionsTest + { + [Test] + public void Constants_Always_ExpectedValues() + { + Assert.AreEqual("Locations", LocationsTableDefinitions.TableName); + Assert.AreEqual("HRDLocationId", LocationsTableDefinitions.HrdLocationId); + Assert.AreEqual("LocationId", LocationsTableDefinitions.LocationId); + Assert.AreEqual("TrackId", LocationsTableDefinitions.TrackId); + } + } +} \ No newline at end of file Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/RegionsTableDefinitionsTest.cs =================================================================== diff -u --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/RegionsTableDefinitionsTest.cs (revision 0) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/RegionsTableDefinitionsTest.cs (revision 85fb38b5ff4ef61c053ffef928570cd75cfa9357) @@ -0,0 +1,38 @@ +// 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.HydraRing.IO.HydraulicLocationConfigurationDatabase; + +namespace Ringtoets.HydraRing.IO.Test.HydraulicLocationConfigurationDatabase +{ + [TestFixture] + public class RegionsTableDefinitionsTest + { + [Test] + public void Constants_Always_ExpectedValues() + { + Assert.AreEqual("Regions", RegionsTableDefinitions.TableName); + Assert.AreEqual("RegionId", RegionsTableDefinitions.RegionId); + Assert.AreEqual("UsePreprocessor", RegionsTableDefinitions.UsePreprocessor); + } + } +} \ No newline at end of file Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/TracksTableDefinitionsTest.cs =================================================================== diff -u --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/TracksTableDefinitionsTest.cs (revision 0) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabase/TracksTableDefinitionsTest.cs (revision 85fb38b5ff4ef61c053ffef928570cd75cfa9357) @@ -0,0 +1,38 @@ +// 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.HydraRing.IO.HydraulicLocationConfigurationDatabase; + +namespace Ringtoets.HydraRing.IO.Test.HydraulicLocationConfigurationDatabase +{ + [TestFixture] + public class TracksTableDefinitionsTest + { + [Test] + public void Constants_Always_ExpectedValues() + { + Assert.AreEqual("Tracks", TracksTableDefinitions.TableName); + Assert.AreEqual("TrackId", TracksTableDefinitions.TrackId); + Assert.AreEqual("RegionId", TracksTableDefinitions.RegionId); + } + } +} \ No newline at end of file Fisheye: Tag 85fb38b5ff4ef61c053ffef928570cd75cfa9357 refers to a dead (removed) revision in file `Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabaseContext/HydraulicLocationConfigurationDatabaseQueryBuilderTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 85fb38b5ff4ef61c053ffef928570cd75cfa9357 refers to a dead (removed) revision in file `Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabaseContext/HydraulicLocationConfigurationSqLiteDatabaseReaderTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 85fb38b5ff4ef61c053ffef928570cd75cfa9357 refers to a dead (removed) revision in file `Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabaseContext/LocationsTableDefinitionsTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 85fb38b5ff4ef61c053ffef928570cd75cfa9357 refers to a dead (removed) revision in file `Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabaseContext/RegionsTableDefinitionsTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Fisheye: Tag 85fb38b5ff4ef61c053ffef928570cd75cfa9357 refers to a dead (removed) revision in file `Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicLocationConfigurationDatabaseContext/TracksTableDefinitionsTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/Ringtoets.HydraRing.IO.Test.csproj =================================================================== diff -u -r640c6bce50d2ef282bef96ee5686fbe2f893384e -r85fb38b5ff4ef61c053ffef928570cd75cfa9357 --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/Ringtoets.HydraRing.IO.Test.csproj (.../Ringtoets.HydraRing.IO.Test.csproj) (revision 640c6bce50d2ef282bef96ee5686fbe2f893384e) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/Ringtoets.HydraRing.IO.Test.csproj (.../Ringtoets.HydraRing.IO.Test.csproj) (revision 85fb38b5ff4ef61c053ffef928570cd75cfa9357) @@ -23,11 +23,11 @@ - - - - - + + + + +