// 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.Data.SQLite; using Core.Common.IO; using NUnit.Framework; using Ringtoets.Common.Util; using Ringtoets.Integration.Data; using Ringtoets.Storage.Core.Properties; namespace Ringtoets.Storage.Core.TestUtil { /// /// This class is used to prepare test databases for test. /// public static class SqLiteDatabaseHelper { /// /// Creates a corrupt database file based on . /// /// The database file path. /// Thrown when /// is null or whitespace. public static void CreateCorruptDatabaseFile(string databaseFilePath) { CreateDatabaseFile(databaseFilePath, GetCorruptSchema()); AddVersionEntity(databaseFilePath, RingtoetsVersionHelper.GetCurrentDatabaseVersion()); } /// /// Creates the complete database file with a VersionEntity row but no project data. /// /// The database file path. /// Thrown when /// is null or whitespace. public static void CreateCompleteDatabaseFileWithoutProjectData(string databaseFilePath) { CreateCompleteDatabaseFileEmpty(databaseFilePath); AddVersionEntity(databaseFilePath, RingtoetsVersionHelper.GetCurrentDatabaseVersion()); } /// /// Creates the complete database file without any rows in any table. /// /// The database file path. public static void CreateCompleteDatabaseFileEmpty(string databaseFilePath) { CreateDatabaseFile(databaseFilePath, GetCompleteSchema()); } /// /// Adds a row to the VersionEntity table with a given database version. /// /// The database file path. /// The database version. /// Thrown when /// is null or whitespace. public static void AddVersionEntity(string databaseFilePath, string databaseVersion) { string addVersionRowCommand = GetAddVersionRowCommandText(databaseVersion); PerformCommandOnDatabase(databaseFilePath, addVersionRowCommand); } /// /// Creates a new Sqlite database file with the structure defined in . /// /// Path to database file. /// Script that contains the schema to execute on the database. /// Thrown when executing failed. /// Thrown when /// or is null or whitespace. public static void CreateDatabaseFile(string databaseFilePath, string databaseSchemaQuery) { if (string.IsNullOrWhiteSpace(databaseSchemaQuery)) { throw new ArgumentNullException(nameof(databaseSchemaQuery)); } if (string.IsNullOrWhiteSpace(databaseFilePath)) { throw new ArgumentNullException(nameof(databaseFilePath)); } SQLiteConnection.CreateFile(databaseFilePath); PerformCommandOnDatabase(databaseFilePath, databaseSchemaQuery); } /// /// Converts the into a new Ringtoets database file. /// /// Path to database file. /// to save. public static void CreateValidRingtoetsDatabase(string databaseFilePath, RingtoetsProject project) { try { var storageSqLite = new StorageSqLite(); storageSqLite.StageProject(project); storageSqLite.SaveProjectAs(databaseFilePath); } catch (Exception exception) { Assert.Fail("Precondition failed: creating database file failed due to {0}", exception); } finally { SQLiteConnection.ClearAllPools(); } } /// /// Returns a corrupt database schema that will pass validation. /// /// The corrupt database schema that will pass validation. public static string GetCorruptSchema() { return "DROP TABLE IF EXISTS 'VersionEntity'; " + "CREATE TABLE 'VersionEntity' ('VersionId' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," + "'Version' VARCHAR(20) NOT NULL,'Timestamp' DATETIME NOT NULL, 'FingerPrint' BLOB NOT NULL);"; } private static string GetCompleteSchema() { return Resources.DatabaseStructure; } /// /// Performs the command on a database. /// /// The file path to the database. /// The command text/query. /// Thrown when is null or whitespace. private static void PerformCommandOnDatabase(string databaseFilePath, string commandText) { string connectionString = SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(databaseFilePath, false); using (var dbContext = new SQLiteConnection(connectionString, true)) { dbContext.Open(); using (SQLiteCommand command = dbContext.CreateCommand()) { try { command.CommandText = commandText; command.ExecuteNonQuery(); } finally { SQLiteConnection.ClearAllPools(); dbContext.Close(); } } } } private static string GetAddVersionRowCommandText(string databaseVersion) { return "INSERT INTO VersionEntity (Version, Timestamp, FingerPrint) " + $"VALUES (\"{databaseVersion}\", '2016-08-10 10:55:48', 'QWERTY')"; } } }