Index: Core/Common/test/Core.Common.IO.Test/Readers/SqLiteDatabaseReaderBaseTest.cs =================================================================== diff -u -rc6dd26c14af11a7e13f783f578466e46b463165a -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 --- Core/Common/test/Core.Common.IO.Test/Readers/SqLiteDatabaseReaderBaseTest.cs (.../SqLiteDatabaseReaderBaseTest.cs) (revision c6dd26c14af11a7e13f783f578466e46b463165a) +++ Core/Common/test/Core.Common.IO.Test/Readers/SqLiteDatabaseReaderBaseTest.cs (.../SqLiteDatabaseReaderBaseTest.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -53,16 +53,37 @@ } [Test] + public void Constructor_WithInvalidPath_ThrowsCriticalFileReadException() + { + // Setup + var testFile = Path.Combine(testDataPath, "empty.sqlite"); + + var invalidCharacters = Path.GetInvalidPathChars(); + + var corruptPath = testFile.Replace('e', invalidCharacters[0]); + + // Call + TestDelegate test = () => new TestReader(corruptPath).Dispose(); + + // Assert + var expectedMessage = new FileReaderErrorMessageBuilder(corruptPath) + .Build(String.Format(UtilsResources.Error_Path_cannot_contain_Characters_0_, + String.Join(", ", Path.GetInvalidFileNameChars()))); + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] public void Constructor_NonExistingPath_ThrowsCriticalFileReadException() { // Setup var testFile = Path.Combine(testDataPath, "none.sqlite"); - var expectedMessage = new FileReaderErrorMessageBuilder(testFile).Build(UtilsResources.Error_File_does_not_exist); // Call TestDelegate test = () => new TestReader(testFile).Dispose(); // Assert + var expectedMessage = new FileReaderErrorMessageBuilder(testFile).Build(UtilsResources.Error_File_does_not_exist); var exception = Assert.Throws(test); Assert.AreEqual(expectedMessage, exception.Message); } Index: Core/Common/test/Core.Common.TestUtil.Test/TestHelperTest.cs =================================================================== diff -u -r1a6f65407809a58d25b871af1fc77d6323c6c83a -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 --- Core/Common/test/Core.Common.TestUtil.Test/TestHelperTest.cs (.../TestHelperTest.cs) (revision 1a6f65407809a58d25b871af1fc77d6323c6c83a) +++ Core/Common/test/Core.Common.TestUtil.Test/TestHelperTest.cs (.../TestHelperTest.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -115,6 +115,29 @@ } [Test] + public void TestAssertLogMessageWithLevel() + { + TestHelper.AssertLogMessageWithLevelIsGenerated(() => log.Error("test 1"), Tuple.Create("test 1", LogLevelConstant.Error)); + TestHelper.AssertLogMessageWithLevelIsGenerated(() => log.Warn("test 2"), Tuple.Create("test 2", LogLevelConstant.Warn)); + TestHelper.AssertLogMessageWithLevelIsGenerated(() => log.Info("test 3"), Tuple.Create("test 3", LogLevelConstant.Info)); + } + + [Test] + public void TestAssertLogMessagesWithLevelAreGenerated() + { + TestHelper.AssertLogMessagesWithLevelAreGenerated(() => + { + log.Error("test 1"); + log.Warn("test 2"); + log.Info("test 3"); + }, new [] { + Tuple.Create("test 1", LogLevelConstant.Error), + Tuple.Create("test 2", LogLevelConstant.Warn), + Tuple.Create("test 3", LogLevelConstant.Info) + }); + } + + [Test] public void AssertImagesAreEqual_TwoIdenticalImages_NoAssertionErrors() { // Setup Index: Demo/Ringtoets/src/Demo.Ringtoets/Commands/AddNewDemoAssessmentSectionCommand.cs =================================================================== diff -u -rcda9bb0707f49cfb8e685d3ec04da01240c73f26 -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 --- Demo/Ringtoets/src/Demo.Ringtoets/Commands/AddNewDemoAssessmentSectionCommand.cs (.../AddNewDemoAssessmentSectionCommand.cs) (revision cda9bb0707f49cfb8e685d3ec04da01240c73f26) +++ Demo/Ringtoets/src/Demo.Ringtoets/Commands/AddNewDemoAssessmentSectionCommand.cs (.../AddNewDemoAssessmentSectionCommand.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -84,8 +84,8 @@ { using (var hydraulicBoundaryDatabaseImporter = new HydraulicBoundaryDatabaseImporter()) { - hydraulicBoundaryDatabaseImporter.ValidateAndConnectTo(Path.Combine(embeddedResourceFileWriter.TargetFolderPath, "HRD dutch coast south.sqlite")); - hydraulicBoundaryDatabaseImporter.Import(demoAssessmentSection); + var filePath = Path.Combine(embeddedResourceFileWriter.TargetFolderPath, "HRD dutch coast south.sqlite"); + hydraulicBoundaryDatabaseImporter.Import(demoAssessmentSection, filePath); } } Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicDatabaseHelper.cs =================================================================== diff -u --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicDatabaseHelper.cs (revision 0) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/HydraulicDatabaseHelper.cs (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -0,0 +1,36 @@ +using System.IO; +using Core.Common.IO.Exceptions; +using log4net; +using Ringtoets.HydraRing.IO.HydraulicBoundaryDatabaseContext; +using Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabaseContext; + +namespace Ringtoets.HydraRing.IO +{ + public static class HydraulicDatabaseHelper + { + /// + /// Attempts to connect to the as if it is a Hydraulic Boundary Locations + /// database with a Hydraulic Location Configurations database next to it. + /// + /// The path of the Hydraulic Boundary Locations database file. + /// A describing the problem when trying to connect to the + /// or null if a connection could be correctly made. + public static string ValidatePathForCalculation(string filePath) + { + try + { + using (var db = new HydraulicBoundarySqLiteDatabaseReader(filePath)) + { + db.GetVersion(); + } + string hlcdFilePath = Path.Combine(Path.GetDirectoryName(filePath), "hlcd.sqlite"); + new HydraulicLocationConfigurationSqLiteDatabaseReader(hlcdFilePath).Dispose(); + } + catch (CriticalFileReadException e) + { + return e.Message; + } + return null; + } + } +} \ No newline at end of file Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/Ringtoets.HydraRing.IO.csproj =================================================================== diff -u -r44055100aa3c3f382227becdaeae7d97c75d386c -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/Ringtoets.HydraRing.IO.csproj (.../Ringtoets.HydraRing.IO.csproj) (revision 44055100aa3c3f382227becdaeae7d97c75d386c) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.IO/Ringtoets.HydraRing.IO.csproj (.../Ringtoets.HydraRing.IO.csproj) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -58,6 +58,7 @@ + Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicDatabaseHelperTest.cs =================================================================== diff -u --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicDatabaseHelperTest.cs (revision 0) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/HydraulicDatabaseHelperTest.cs (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -0,0 +1,80 @@ +using System.IO; +using Core.Common.TestUtil; +using NUnit.Framework; + +namespace Ringtoets.HydraRing.IO.Test +{ + [TestFixture] + public class HydraulicDatabaseHelperTest + { + private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.HydraRing.IO, "HydraulicBoundaryLocationReader"); + + [Test] + public void ValidateAndConnectTo_ExistingFileWithHlcd_ReturnsTrue() + { + // Setup + string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); + + // Call + var result = HydraulicDatabaseHelper.ValidatePathForCalculation(validFilePath); + + // Assert + Assert.IsNull(result); + } + + [Test] + public void ValidateAndConnectTo_NonExistingFile_ReturnsFalse() + { + // Setup + var nonexistingSqlite = "nonexisting.sqlite"; + string filePath = Path.Combine(testDataPath, nonexistingSqlite); + + // Call + var result = HydraulicDatabaseHelper.ValidatePathForCalculation(filePath); + + // Assert + StringAssert.StartsWith(string.Format("Fout bij het lezen van bestand '{0}':", filePath), result); + } + + [Test] + public void ValidateAndConnectTo_InvalidFile_ReturnsFalse() + { + // Setup + var invalidPath = Path.Combine(testDataPath, "complete.sqlite"); + invalidPath = invalidPath.Replace('c', Path.GetInvalidPathChars()[0]); + + // Call + var result = HydraulicDatabaseHelper.ValidatePathForCalculation(invalidPath); + + // Assert + StringAssert.StartsWith(string.Format("Fout bij het lezen van bestand '{0}':", invalidPath), result); + } + + [Test] + public void ValidateAndConnectTo_FileIsDirectory_ReturnsFalse() + { + // Setup + string filePath = Path.Combine(testDataPath, "/"); + + // Call + var result = HydraulicDatabaseHelper.ValidatePathForCalculation(filePath); + + // Assert + StringAssert.StartsWith(string.Format("Fout bij het lezen van bestand '{0}':", filePath), result); + } + + [Test] + public void ValidateAndConnectTo_ExistingFileWithoutHlcd_ReturnsFalse() + { + // Setup + string validFilePath = Path.Combine(testDataPath, "withoutHLCD", "empty.sqlite"); + + // Call + var result = HydraulicDatabaseHelper.ValidatePathForCalculation(validFilePath); + + // Assert + StringAssert.StartsWith(string.Format("Fout bij het lezen van bestand '{0}':", validFilePath), result); + } + + } +} \ No newline at end of file Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/Ringtoets.HydraRing.IO.Test.csproj =================================================================== diff -u -r733d0d51adf737c52704cd80705507fdb34a2ba3 -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/Ringtoets.HydraRing.IO.Test.csproj (.../Ringtoets.HydraRing.IO.Test.csproj) (revision 733d0d51adf737c52704cd80705507fdb34a2ba3) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.IO.Test/Ringtoets.HydraRing.IO.Test.csproj (.../Ringtoets.HydraRing.IO.Test.csproj) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -56,6 +56,7 @@ + Index: Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/HydraulicBoundaryDatabaseImporter.cs =================================================================== diff -u -rfc38d18fc6ff1749476da0ea43281d5d80568283 -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 --- Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/HydraulicBoundaryDatabaseImporter.cs (.../HydraulicBoundaryDatabaseImporter.cs) (revision fc38d18fc6ff1749476da0ea43281d5d80568283) +++ Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/HydraulicBoundaryDatabaseImporter.cs (.../HydraulicBoundaryDatabaseImporter.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -22,14 +22,13 @@ using System; using System.IO; using Core.Common.IO.Exceptions; +using Core.Common.Utils; using Core.Common.Utils.Builders; using log4net; -using Ringtoets.Common.Data; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.HydraRing.Data; using Ringtoets.HydraRing.IO.HydraulicBoundaryDatabaseContext; using Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabaseContext; -using Ringtoets.Integration.Forms.PresentationObjects; using Ringtoets.Integration.Plugin.Properties; namespace Ringtoets.Integration.Plugin.FileImporters @@ -41,43 +40,44 @@ public class HydraulicBoundaryDatabaseImporter : IDisposable { private readonly ILog log = LogManager.GetLogger(typeof(HydraulicBoundaryDatabaseImporter)); - private string hydraulicBoundaryDatabaseFilePath; - private HydraulicBoundarySqLiteDatabaseReader hydraulicBoundaryDatabaseReader; private HydraulicLocationConfigurationSqLiteDatabaseReader hydraulicLocationConfigurationDatabaseReader; /// /// Validates the file and opens a connection. /// /// The path to the file to read. - /// Thrown when: + /// Thrown when: + /// /// The given file at cannot be read. - /// The file 'HLCD.sqlite' in the same folder as cannot be read. - public void ValidateAndConnectTo(string filePath) + /// The file 'HLCD.sqlite' in the same folder as cannot be read. + /// + /// + private void ValidateAndConnectTo(string filePath) { hydraulicBoundaryDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(filePath); - hydraulicBoundaryDatabaseFilePath = filePath; - var fileDirectory = Path.GetDirectoryName(filePath); - if (fileDirectory == null) + string hlcdFilePath = Path.Combine(Path.GetDirectoryName(filePath), "hlcd.sqlite"); + + try { - throw new ArgumentNullException("filePath"); + hydraulicLocationConfigurationDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(hlcdFilePath); } - var hlcdFilePath = Path.Combine(fileDirectory, "hlcd.sqlite"); - if (!File.Exists(hlcdFilePath)) + catch (CriticalFileReadException) { var message = new FileReaderErrorMessageBuilder(filePath).Build(Resources.HydraulicBoundaryDatabaseImporter_HLCD_sqlite_Not_Found); throw new CriticalFileReadException(message); } - hydraulicLocationConfigurationDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(hlcdFilePath); } /// /// Gets the version of the database. /// + /// The path to the database to obtain the version for. /// The database version. /// Thrown when the version could not be obtained from the database. - public string GetHydraulicBoundaryDatabaseVersion() + public string GetHydraulicBoundaryDatabaseVersion(string filePath) { + ValidateAndConnectTo(filePath); return hydraulicBoundaryDatabaseReader.GetVersion(); } @@ -87,17 +87,19 @@ /// /// to set the newly /// created . + /// The path of the hydraulic boundary database file to import from. /// True if the import was successful, false otherwise. - /// The reader has not been initialized by calling - /// . - public bool Import(IAssessmentSection targetItem) + /// Thrown when: + /// + /// The given file at cannot be read. + /// The file 'HLCD.sqlite' in the same folder as cannot be read. + /// + /// + public bool Import(IAssessmentSection targetItem, string filePath) { - if (hydraulicBoundaryDatabaseReader == null) - { - throw new InvalidOperationException(Resources.HydraulicBoundaryDatabaseImporter_File_not_opened); - } + ValidateAndConnectTo(filePath); - var importResult = GetHydraulicBoundaryDatabase(hydraulicBoundaryDatabaseFilePath); + var importResult = GetHydraulicBoundaryDatabase(); if (importResult == null) { @@ -129,7 +131,7 @@ log.Error(message); } - private HydraulicBoundaryDatabase GetHydraulicBoundaryDatabase(string path) + private HydraulicBoundaryDatabase GetHydraulicBoundaryDatabase() { // Get region var regionId = GetRegionId(); @@ -142,7 +144,7 @@ { var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase { - FilePath = path, + FilePath = hydraulicBoundaryDatabaseReader.Path, Version = hydraulicBoundaryDatabaseReader.GetVersion() }; Index: Ringtoets/Integration/src/Ringtoets.Integration.Plugin/Properties/Resources.Designer.cs =================================================================== diff -u -rfc38d18fc6ff1749476da0ea43281d5d80568283 -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 --- Ringtoets/Integration/src/Ringtoets.Integration.Plugin/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision fc38d18fc6ff1749476da0ea43281d5d80568283) +++ Ringtoets/Integration/src/Ringtoets.Integration.Plugin/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -199,12 +199,22 @@ } /// - /// Looks up a localized string similar to Hydraulische randvoorwaarden database kon niet gevonden worden op locatie {0}.. + /// Looks up a localized string similar to Kon geen berekeningen starten. {0}. /// - public static string RingtoetsGuiPlugin_VerifyHydraulicBoundaryDatabasePath_Hydraulic_boundary_database_could_not_be_found_on_path_0_ { + public static string RingtoetsGuiPlugin_HydraulicBoundaryDatabaseContextMenuStrip_Start_calculation_failed_0_ { get { + return ResourceManager.GetString("RingtoetsGuiPlugin_HydraulicBoundaryDatabaseContextMenuStrip_Start_calculation_fa" + + "iled_0_", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Herstellen van de connectie naar de hydraulische randvoorwaarden database mislukt. {0}. + /// + public static string RingtoetsGuiPlugin_VerifyHydraulicBoundaryDatabasePath_Hydraulic_boundary_database_connection_failed_0_ { + get { return ResourceManager.GetString("RingtoetsGuiPlugin_VerifyHydraulicBoundaryDatabasePath_Hydraulic_boundary_databas" + - "e_could_not_be_found_on_path_0_", resourceCulture); + "e_connection_failed_0_", resourceCulture); } } } Index: Ringtoets/Integration/src/Ringtoets.Integration.Plugin/Properties/Resources.resx =================================================================== diff -u -rfc38d18fc6ff1749476da0ea43281d5d80568283 -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 --- Ringtoets/Integration/src/Ringtoets.Integration.Plugin/Properties/Resources.resx (.../Resources.resx) (revision fc38d18fc6ff1749476da0ea43281d5d80568283) +++ Ringtoets/Integration/src/Ringtoets.Integration.Plugin/Properties/Resources.resx (.../Resources.resx) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -163,7 +163,10 @@ Er is een fout opgetreden tijdens de berekening: inspecteer het logbestand. - - Hydraulische randvoorwaarden database kon niet gevonden worden op locatie {0}. + + Herstellen van de connectie naar de hydraulische randvoorwaarden database mislukt. {0} + + Kon geen berekeningen starten. {0} + \ No newline at end of file Index: Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsGuiPlugin.cs =================================================================== diff -u -r638c0c354f63b4c8be69feb448712b004d8bb933 -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 --- Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsGuiPlugin.cs (.../RingtoetsGuiPlugin.cs) (revision 638c0c354f63b4c8be69feb448712b004d8bb933) +++ Ringtoets/Integration/src/Ringtoets.Integration.Plugin/RingtoetsGuiPlugin.cs (.../RingtoetsGuiPlugin.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -46,6 +46,7 @@ using Ringtoets.HydraRing.Calculation.Data.Input.Hydraulics; using Ringtoets.HydraRing.Calculation.Data.Output; using Ringtoets.HydraRing.Data; +using Ringtoets.HydraRing.IO; using Ringtoets.Integration.Data.Placeholders; using Ringtoets.Integration.Forms.PresentationObjects; using Ringtoets.Integration.Forms.PropertyClasses; @@ -65,9 +66,9 @@ /// /// The GUI plugin for the Ringtoets application. /// - public class RingtoetsGuiPlugin : Core.Common.Gui.Plugin.GuiPlugin + public class RingtoetsGuiPlugin : GuiPlugin { - private static readonly ILog log = LogManager.GetLogger(typeof(Core.Common.Gui.Plugin.GuiPlugin)); + private static readonly ILog log = LogManager.GetLogger(typeof(GuiPlugin)); public override IRibbonCommandHandler RibbonCommandHandler { @@ -113,14 +114,13 @@ foreach (IAssessmentSection section in sectionsWithDatabase) { string selectedFile = section.HydraulicBoundaryDatabase.FilePath; - try + var validationProblem = HydraulicDatabaseHelper.ValidatePathForCalculation(selectedFile); + if(validationProblem != null) { - CreateHydraulicDatabaseImporter(selectedFile); + log.WarnFormat( + Resources.RingtoetsGuiPlugin_VerifyHydraulicBoundaryDatabasePath_Hydraulic_boundary_database_connection_failed_0_, + validationProblem); } - catch (CriticalFileReadException) - { - log.WarnFormat(Resources.RingtoetsGuiPlugin_VerifyHydraulicBoundaryDatabasePath_Hydraulic_boundary_database_could_not_be_found_on_path_0_, selectedFile); - } } } @@ -561,14 +561,22 @@ RingtoetsFormsResources.FailureMechanismIcon, (sender, args) => { - var hlcdDirectory = Path.GetDirectoryName(nodeData.Parent.HydraulicBoundaryDatabase.FilePath); - var activities = nodeData.Parent.HydraulicBoundaryDatabase.Locations.Select(hbl => CreateHydraRingActivity(nodeData.Parent, hbl, hlcdDirectory)).ToList(); + var hrdFile = nodeData.Parent.HydraulicBoundaryDatabase.FilePath; + var validationProblem = HydraulicDatabaseHelper.ValidatePathForCalculation(hrdFile); + if (validationProblem == null) + { + var hlcdDirectory = Path.GetDirectoryName(hrdFile); + var activities = nodeData.Parent.HydraulicBoundaryDatabase.Locations.Select(hbl => CreateHydraRingActivity(nodeData.Parent, hbl, hlcdDirectory)).ToList(); - ActivityProgressDialogRunner.Run(Gui.MainWindow, activities); + ActivityProgressDialogRunner.Run(Gui.MainWindow, activities); - nodeData.Parent.NotifyObservers(); - } - ); + nodeData.Parent.NotifyObservers(); + } + else + { + log.ErrorFormat(Resources.RingtoetsGuiPlugin_HydraulicBoundaryDatabaseContextMenuStrip_Start_calculation_failed_0_, validationProblem); + } + }); if (nodeData.Parent.HydraulicBoundaryDatabase == null) { @@ -598,7 +606,7 @@ Multiselect = false, Title = windowTitle, RestoreDirectory = true, - CheckFileExists = false, + CheckFileExists = false }) { if (dialog.ShowDialog(Gui.MainWindow) == DialogResult.OK) @@ -624,44 +632,32 @@ /// Thrown when the importer could not be created for the selected file. private static void ValidateAndImportSelectedFile(string selectedFile, IAssessmentSection assessmentSection) { - var hydraulicBoundaryLocationsImporter = CreateHydraulicDatabaseImporter(selectedFile); - - if (assessmentSection.HydraulicBoundaryDatabase != null) + using (var hydraulicBoundaryLocationsImporter = new HydraulicBoundaryDatabaseImporter()) { - var newVersion = hydraulicBoundaryLocationsImporter.GetHydraulicBoundaryDatabaseVersion(); + if (assessmentSection.HydraulicBoundaryDatabase != null) + { + var newVersion = hydraulicBoundaryLocationsImporter.GetHydraulicBoundaryDatabaseVersion(selectedFile); - var currentVersion = assessmentSection.HydraulicBoundaryDatabase.Version; - var currentFilePath = assessmentSection.HydraulicBoundaryDatabase.FilePath; + var currentVersion = assessmentSection.HydraulicBoundaryDatabase.Version; + var currentFilePath = assessmentSection.HydraulicBoundaryDatabase.FilePath; - if (currentVersion != newVersion && HasClearCalculationConfirmation()) - { - ClearCalculations(assessmentSection); - ImportSelectedFile(assessmentSection, hydraulicBoundaryLocationsImporter); + if (currentVersion != newVersion && HasClearCalculationConfirmation()) + { + ClearCalculations(assessmentSection); + ImportSelectedFile(assessmentSection, selectedFile, hydraulicBoundaryLocationsImporter); + } + else if (currentFilePath != selectedFile) + { + SetBoundaryDatabaseData(assessmentSection, selectedFile); + } } - else if (currentFilePath != selectedFile) + else { - SetBoundaryDatabaseData(assessmentSection, selectedFile); + ImportSelectedFile(assessmentSection, selectedFile, hydraulicBoundaryLocationsImporter); } } - else - { - ImportSelectedFile(assessmentSection, hydraulicBoundaryLocationsImporter); - } } - /// - /// Attempts to connect a new to the . - /// - /// The file to use to import a from. - /// A new which is connected to the . - /// Thrown when the importer could not be created for the selected file. - private static HydraulicBoundaryDatabaseImporter CreateHydraulicDatabaseImporter(string selectedFile) - { - var hydraulicBoundaryLocationsImporter = new HydraulicBoundaryDatabaseImporter(); - hydraulicBoundaryLocationsImporter.ValidateAndConnectTo(selectedFile); - return hydraulicBoundaryLocationsImporter; - } - private static TargetProbabilityCalculationActivity CreateHydraRingActivity(IAssessmentSection assessmentSection, HydraulicBoundaryLocation hydraulicBoundaryLocation, string hlcdDirectory) { return HydraRingActivityFactory.Create( @@ -710,9 +706,9 @@ log.Info(RingtoetsFormsResources.Calculations_Deleted); } - private static void ImportSelectedFile(IAssessmentSection assessmentSection, HydraulicBoundaryDatabaseImporter hydraulicBoundaryLocationsImporter) + private static void ImportSelectedFile(IAssessmentSection assessmentSection, string hydraulicDatabasePath, HydraulicBoundaryDatabaseImporter hydraulicBoundaryLocationsImporter) { - if (hydraulicBoundaryLocationsImporter.Import(assessmentSection)) + if (hydraulicBoundaryLocationsImporter.Import(assessmentSection, hydraulicDatabasePath)) { SetBoundaryDatabaseData(assessmentSection); } Index: Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/TreeNodeInfos/HydraulicBoundaryDatabaseContextTreeNodeInfoTest.cs =================================================================== diff -u -rfc38d18fc6ff1749476da0ea43281d5d80568283 -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 --- Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/TreeNodeInfos/HydraulicBoundaryDatabaseContextTreeNodeInfoTest.cs (.../HydraulicBoundaryDatabaseContextTreeNodeInfoTest.cs) (revision fc38d18fc6ff1749476da0ea43281d5d80568283) +++ Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/TreeNodeInfos/HydraulicBoundaryDatabaseContextTreeNodeInfoTest.cs (.../HydraulicBoundaryDatabaseContextTreeNodeInfoTest.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -11,9 +11,7 @@ using NUnit.Extensions.Forms; using NUnit.Framework; using Rhino.Mocks; -using Ringtoets.Common.Data; using Ringtoets.Common.Data.AssessmentSection; -using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.HydraRing.Data; using Ringtoets.Integration.Data; using Ringtoets.Integration.Forms.PresentationObjects; @@ -24,7 +22,7 @@ namespace Ringtoets.Integration.Forms.Test.TreeNodeInfos { [TestFixture] - public class HydraulicBoundaryDatabaseContextTreeNodeInfoTest : NUnitFormTest + public class HydraulicBoundaryDatabaseContextTreeNodeInfoTest { private MockRepository mocks; @@ -223,14 +221,11 @@ } [Test] - public void GivenHydraulicBoundaryDatabaseWithNonExistingFilePath_WhenCalculatingAssessmentLevelFromContextMenu_ThenLogMessagesAddedPreviousOutputNotAffectedAndObserversNotified() + public void GivenHydraulicBoundaryDatabaseWithNonExistingFilePath_WhenCalculatingAssessmentLevelFromContextMenu_ThenLogMessagesAddedPreviousOutputNotAffected() { // Given var gui = mocks.DynamicMock(); - var hydraulicBoundaryDatabaseContextObserver = mocks.StrictMock(); - - var mainWindow = mocks.DynamicMock(); var treeViewControlMock = mocks.StrictMock(); var contextMenuRunAssessmentLevelCalculationsIndex = 3; @@ -258,42 +253,24 @@ }; var hydraulicBoundaryDatabaseContext = new HydraulicBoundaryDatabaseContext(assessmentSectionMock); - hydraulicBoundaryDatabaseContextObserver.Expect(o => o.UpdateObserver()); gui.Expect(cmp => cmp.Get(hydraulicBoundaryDatabaseContext, treeViewControlMock)).Return(new CustomItemsOnlyContextMenuBuilder()); - gui.Expect(g => g.MainWindow).Return(mainWindow); mocks.ReplayAll(); - hydraulicBoundaryDatabaseContext.Attach(hydraulicBoundaryDatabaseContextObserver); - using (var plugin = new RingtoetsGuiPlugin()) { var info = GetInfo(plugin); plugin.Gui = gui; var contextMenuAdapter = info.ContextMenuStrip(hydraulicBoundaryDatabaseContext, null, treeViewControlMock); - DialogBoxHandler = (name, wnd) => - { - // Don't care about dialogs in this test. - }; - // When Action action = () => { contextMenuAdapter.Items[contextMenuRunAssessmentLevelCalculationsIndex].PerformClick(); }; // Then - TestHelper.AssertLogMessages(action, messages => - { - var msgs = messages.GetEnumerator(); - Assert.IsTrue(msgs.MoveNext()); - StringAssert.StartsWith("Er is een fout opgetreden tijdens de berekening: inspecteer het logbestand.", msgs.Current); - Assert.IsTrue(msgs.MoveNext()); - StringAssert.StartsWith("Uitvoeren van 'Toetspeil berekenen voor locatie '100001'' is mislukt.", msgs.Current); - Assert.IsTrue(msgs.MoveNext()); - StringAssert.StartsWith("Er is een fout opgetreden tijdens de berekening: inspecteer het logbestand.", msgs.Current); - Assert.IsTrue(msgs.MoveNext()); - StringAssert.StartsWith("Uitvoeren van 'Toetspeil berekenen voor locatie '100002'' is mislukt.", msgs.Current); - }); + string message = string.Format("Kon geen berekeningen starten. Fout bij het lezen van bestand '{0}': Het bestand bestaat niet.", + hydraulicBoundaryDatabase.FilePath); + TestHelper.AssertLogMessageWithLevelIsGenerated(action, new Tuple(message, LogLevelConstant.Error)); Assert.IsNaN(hydraulicBoundaryLocation1.DesignWaterLevel); // No result set Assert.AreEqual(4.2, hydraulicBoundaryLocation2.DesignWaterLevel); // Previous result not cleared Index: Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/FileImporters/HydraulicBoundaryDatabaseImporterTest.cs =================================================================== diff -u -rfc38d18fc6ff1749476da0ea43281d5d80568283 -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 --- Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/FileImporters/HydraulicBoundaryDatabaseImporterTest.cs (.../HydraulicBoundaryDatabaseImporterTest.cs) (revision fc38d18fc6ff1749476da0ea43281d5d80568283) +++ Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/FileImporters/HydraulicBoundaryDatabaseImporterTest.cs (.../HydraulicBoundaryDatabaseImporterTest.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -28,13 +28,12 @@ using Core.Common.Utils.Builders; using NUnit.Framework; using Rhino.Mocks; -using Ringtoets.Common.Data; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.HydraRing.Data; -using Ringtoets.Integration.Forms.PresentationObjects; using Ringtoets.Integration.Plugin.FileImporters; using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources; using RingtoetsFormsResources = Ringtoets.Common.Forms.Properties.Resources; +using UtilsResources = Core.Common.Utils.Properties.Resources; namespace Ringtoets.Integration.Plugin.Test.FileImporters { @@ -66,94 +65,128 @@ } [Test] - public void ValidateAndConnectTo_ExistingFile_DoesNotThrowException() + public void GetHydraulicBoundaryDatabaseVersion_ValidFile_GetDatabaseVersion() { // Setup string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); // Call - TestDelegate test = () => importer.ValidateAndConnectTo(validFilePath); + string version = importer.GetHydraulicBoundaryDatabaseVersion(validFilePath); // Assert - Assert.DoesNotThrow(test); + Assert.IsNotNullOrEmpty(version); } [Test] - public void ValidateAndConnectTo_NonExistingFile_ThrowsCriticalFileReadException() + public void Import_ExistingFile_DoesNotThrowException() { // Setup - string filePath = Path.Combine(testDataPath, "nonexisting.sqlite"); - var expectedExceptionMessage = String.Format("Fout bij het lezen van bestand '{0}': Het bestand bestaat niet.", filePath); + var mocks = new MockRepository(); + var assessmentSection = mocks.Stub(); + assessmentSection.Expect(section => section.NotifyObservers()); + mocks.ReplayAll(); + string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); + // Call - TestDelegate test = () => importer.ValidateAndConnectTo(filePath); + TestDelegate test = () => importer.Import(assessmentSection, validFilePath); // Assert - CriticalFileReadException exception = Assert.Throws(test); - Assert.AreEqual(expectedExceptionMessage, exception.Message); + Assert.DoesNotThrow(test); + + mocks.VerifyAll(); } [Test] - public void ValidateAndConnectTo_InvalidFile_ThrowsCriticalFileReadException() + public void Import_NonExistingFile_ThrowsCriticalFileReadException() { // Setup - string filePath = Path.Combine(testDataPath, "/"); - var expectedExceptionMessage = String.Format("Fout bij het lezen van bestand '{0}': Bestandspad mag niet naar een map verwijzen.", filePath); + var mocks = new MockRepository(); + var assessmentSection = mocks.Stub(); + assessmentSection.Expect(section => section.NotifyObservers()).Repeat.Never(); + mocks.ReplayAll(); + string filePath = Path.Combine(testDataPath, "nonexisting.sqlite"); + var expectedExceptionMessage = String.Format("Fout bij het lezen van bestand '{0}': Het bestand bestaat niet.", filePath); + // Call - TestDelegate test = () => importer.ValidateAndConnectTo(filePath); + TestDelegate test = () => importer.Import(assessmentSection, filePath); // Assert CriticalFileReadException exception = Assert.Throws(test); Assert.AreEqual(expectedExceptionMessage, exception.Message); - Assert.IsInstanceOf(exception.InnerException); + + mocks.VerifyAll(); } [Test] - public void ValidateAndConnectTo_ExistingFileWithoutHlcd_ThrowCriticalFileReadException() + public void Import_InvalidFile_ThrowsCriticalFileReadException() { // Setup - string validFilePath = Path.Combine(testDataPath, "withoutHLCD", "empty.sqlite"); - string expectedMessage = new FileReaderErrorMessageBuilder(validFilePath).Build("Het bijbehorende HLCD bestand is niet gevonden in dezelfde map als het HRD bestand."); + var mocks = new MockRepository(); + var assessmentSection = mocks.Stub(); + assessmentSection.Expect(section => section.NotifyObservers()).Repeat.Never(); + mocks.ReplayAll(); + var invalidPath = Path.Combine(testDataPath, "complete.sqlite"); + invalidPath = invalidPath.Replace('c', Path.GetInvalidPathChars()[0]); + // Call - TestDelegate test = () => importer.ValidateAndConnectTo(validFilePath); + TestDelegate test = () => importer.Import(assessmentSection, invalidPath); // Assert + var expectedMessage = new FileReaderErrorMessageBuilder(invalidPath) + .Build(String.Format(UtilsResources.Error_Path_cannot_contain_Characters_0_, + String.Join(", ", Path.GetInvalidFileNameChars()))); CriticalFileReadException exception = Assert.Throws(test); Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsInstanceOf(exception.InnerException); + + mocks.VerifyAll(); } [Test] - public void GetHydraulicBoundaryDatabaseVersion_ValidFile_GetDatabaseVersion() + public void Import_FileIsDirectory_ThrowsCriticalFileReadException() { // Setup - string validFilePath = Path.Combine(testDataPath, "complete.sqlite"); - importer.ValidateAndConnectTo(validFilePath); + var mocks = new MockRepository(); + var assessmentSection = mocks.Stub(); + assessmentSection.Expect(section => section.NotifyObservers()).Repeat.Never(); + mocks.ReplayAll(); + string filePath = Path.Combine(testDataPath, "/"); + var expectedExceptionMessage = String.Format("Fout bij het lezen van bestand '{0}': Bestandspad mag niet naar een map verwijzen.", filePath); + // Call - string version = importer.GetHydraulicBoundaryDatabaseVersion(); + TestDelegate test = () => importer.Import(assessmentSection, filePath); // Assert - Assert.IsNotNullOrEmpty(version); + CriticalFileReadException exception = Assert.Throws(test); + Assert.AreEqual(expectedExceptionMessage, exception.Message); + Assert.IsInstanceOf(exception.InnerException); + + mocks.VerifyAll(); } [Test] - public void Import_ConnectionNotOpened_ThrowsInValidOperationException() + public void Import_ExistingFileWithoutHlcd_ThrowCriticalFileReadException() { // Setup var mocks = new MockRepository(); var assessmentSection = mocks.Stub(); assessmentSection.Expect(section => section.NotifyObservers()).Repeat.Never(); mocks.ReplayAll(); + string validFilePath = Path.Combine(testDataPath, "withoutHLCD", "empty.sqlite"); + // Call - TestDelegate call = () => importer.Import(assessmentSection); + TestDelegate test = () => importer.Import(assessmentSection, validFilePath); // Assert - var exception = Assert.Throws(call); - var expectedMessage = "Er is nog geen bestand geopend."; + string expectedMessage = new FileReaderErrorMessageBuilder(validFilePath).Build("Het bijbehorende HLCD bestand is niet gevonden in dezelfde map als het HRD bestand."); + CriticalFileReadException exception = Assert.Throws(test); Assert.AreEqual(expectedMessage, exception.Message); + mocks.VerifyAll(); } @@ -171,11 +204,9 @@ // Precondition Assert.IsTrue(File.Exists(validFilePath), string.Format("Precodition failed. File does not exist: {0}", validFilePath)); - importer.ValidateAndConnectTo(validFilePath); - // Call var importResult = false; - Action call = () => importResult = importer.Import(assessmentSection); + Action call = () => importResult = importer.Import(assessmentSection, validFilePath); // Assert TestHelper.AssertLogMessages(call, messages => @@ -205,10 +236,8 @@ var importResult = true; - importer.ValidateAndConnectTo(corruptPath); - // Call - Action call = () => importResult = importer.Import(assessmentSection); + Action call = () => importResult = importer.Import(assessmentSection, corruptPath); // Assert TestHelper.AssertLogMessageIsGenerated(call, expectedLogMessage, 1); @@ -230,12 +259,8 @@ string validFilePath = Path.Combine(testDataPath, "corruptschema.sqlite"); var importResult = true; - // Precondition - TestDelegate precondition = () => importer.ValidateAndConnectTo(validFilePath); - Assert.DoesNotThrow(precondition, "Precodition failed: ValidateAndConnectTo failed"); - // Call - Action call = () => importResult = importer.Import(assessmentSection); + Action call = () => importResult = importer.Import(assessmentSection, validFilePath); // Assert string expectedMessage = new FileReaderErrorMessageBuilder(validFilePath) Index: Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/RingtoetsGuiPluginTest.cs =================================================================== diff -u -r638c0c354f63b4c8be69feb448712b004d8bb933 -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 --- Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/RingtoetsGuiPluginTest.cs (.../RingtoetsGuiPluginTest.cs) (revision 638c0c354f63b4c8be69feb448712b004d8bb933) +++ Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/RingtoetsGuiPluginTest.cs (.../RingtoetsGuiPluginTest.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -142,9 +142,10 @@ }; // Assert + var fileMissingMessage = string.Format("Fout bij het lezen van bestand '{0}': Het bestand bestaat niet.", notExistingFile); string message = string.Format( - Properties.Resources.RingtoetsGuiPlugin_VerifyHydraulicBoundaryDatabasePath_Hydraulic_boundary_database_could_not_be_found_on_path_0_, - notExistingFile); + Properties.Resources.RingtoetsGuiPlugin_VerifyHydraulicBoundaryDatabasePath_Hydraulic_boundary_database_connection_failed_0_, + fileMissingMessage); TestHelper.AssertLogMessageWithLevelIsGenerated(action, Tuple.Create(message, LogLevelConstant.Warn)); } } Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SurfaceLines/PipingSurfaceLinesCsvReaderTest.cs =================================================================== diff -u -r5b575c7019f5ec9d0db2784fda5cb5c7b4df9a90 -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SurfaceLines/PipingSurfaceLinesCsvReaderTest.cs (.../PipingSurfaceLinesCsvReaderTest.cs) (revision 5b575c7019f5ec9d0db2784fda5cb5c7b4df9a90) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SurfaceLines/PipingSurfaceLinesCsvReaderTest.cs (.../PipingSurfaceLinesCsvReaderTest.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -5,7 +5,6 @@ using Core.Common.TestUtil; using Core.Common.Utils.Builders; using NUnit.Framework; -using Ringtoets.Piping.IO.Exceptions; using Ringtoets.Piping.IO.SurfaceLines; using IOResources = Ringtoets.Piping.IO.Properties.Resources; using UtilsResources = Core.Common.Utils.Properties.Resources; Index: Ringtoets/Piping/test/Ringtoets.Piping.Integration.Test/PipingCalculationsViewIntegrationTest.cs =================================================================== diff -u -rcda9bb0707f49cfb8e685d3ec04da01240c73f26 -r2cf2214c4069c98c3cb21708729fcbc2ca964ab6 --- Ringtoets/Piping/test/Ringtoets.Piping.Integration.Test/PipingCalculationsViewIntegrationTest.cs (.../PipingCalculationsViewIntegrationTest.cs) (revision cda9bb0707f49cfb8e685d3ec04da01240c73f26) +++ Ringtoets/Piping/test/Ringtoets.Piping.Integration.Test/PipingCalculationsViewIntegrationTest.cs (.../PipingCalculationsViewIntegrationTest.cs) (revision 2cf2214c4069c98c3cb21708729fcbc2ca964ab6) @@ -156,8 +156,8 @@ { using (var hydraulicBoundaryDatabaseImporter = new HydraulicBoundaryDatabaseImporter()) { - hydraulicBoundaryDatabaseImporter.ValidateAndConnectTo(Path.Combine(embeddedResourceFileWriter.TargetFolderPath, "HRD dutch coast south.sqlite")); - hydraulicBoundaryDatabaseImporter.Import(assessmentSection); + var filePath = Path.Combine(embeddedResourceFileWriter.TargetFolderPath, "HRD dutch coast south.sqlite"); + hydraulicBoundaryDatabaseImporter.Import(assessmentSection, filePath); } } }