Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/ConvergenceParser.cs =================================================================== diff -u -r9bc5d8bc179ba0be25c9746859da3726b8b6a33e -r377ab5754aaebdd884c70b39bc44f1669d01efa8 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/ConvergenceParser.cs (.../ConvergenceParser.cs) (revision 9bc5d8bc179ba0be25c9746859da3726b8b6a33e) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/ConvergenceParser.cs (.../ConvergenceParser.cs) (revision 377ab5754aaebdd884c70b39bc44f1669d01efa8) @@ -20,8 +20,9 @@ // All rights reserved. using System; +using System.Collections.Generic; +using Ringtoets.HydraRing.Calculation.Exceptions; using Ringtoets.HydraRing.Calculation.Properties; -using Ringtoets.HydraRing.Calculation.Readers; namespace Ringtoets.HydraRing.Calculation.Parsers { @@ -47,22 +48,30 @@ public void Parse(string workingDirectory, int sectionId) { - HydraRingDatabaseParseHelper.Parse(workingDirectory, - getLastResultQuery, - sectionId, - Resources.Parse_No_convergence_found_in_output_file, - ReadResult); + Dictionary result = HydraRingDatabaseParseHelper.ReadSingleLine(workingDirectory, + getLastResultQuery, + sectionId, + Resources.Parse_No_convergence_found_in_output_file); + + ReadResult(result); } /// - /// Reads the result of the . + /// Reads the . /// - /// The reader to get the result from. - /// Thrown when the result + /// The result from the database read. + /// Thrown when the result /// cannot be converted to the output format. - private void ReadResult(HydraRingDatabaseReader reader) + private void ReadResult(IDictionary result) { - Output = Convert.ToBoolean(reader.ReadColumn(convergedColumnName)); + try + { + Output = Convert.ToBoolean(result[convergedColumnName]); + } + catch (InvalidCastException e) + { + throw new HydraRingFileParserException(Resources.Parse_No_convergence_found_in_output_file, e); + } } } } \ No newline at end of file Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/DunesBoundaryConditionsCalculationParser.cs =================================================================== diff -u -r9bc5d8bc179ba0be25c9746859da3726b8b6a33e -r377ab5754aaebdd884c70b39bc44f1669d01efa8 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/DunesBoundaryConditionsCalculationParser.cs (.../DunesBoundaryConditionsCalculationParser.cs) (revision 9bc5d8bc179ba0be25c9746859da3726b8b6a33e) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/DunesBoundaryConditionsCalculationParser.cs (.../DunesBoundaryConditionsCalculationParser.cs) (revision 377ab5754aaebdd884c70b39bc44f1669d01efa8) @@ -20,9 +20,10 @@ // All rights reserved. using System; +using System.Collections.Generic; using Ringtoets.HydraRing.Calculation.Data.Output; +using Ringtoets.HydraRing.Calculation.Exceptions; using Ringtoets.HydraRing.Calculation.Properties; -using Ringtoets.HydraRing.Calculation.Readers; namespace Ringtoets.HydraRing.Calculation.Parsers { @@ -53,28 +54,37 @@ public void Parse(string workingDirectory, int sectionId) { - HydraRingDatabaseParseHelper.Parse(workingDirectory, - query, - sectionId, - Resources.DunesBoundaryConditionsCalculationParser_Parse_No_dunes_hydraulic_boundaries_found_in_output_file, - ReadResult); + Dictionary result = HydraRingDatabaseParseHelper.ReadSingleLine( + workingDirectory, + query, + sectionId, + Resources.DunesBoundaryConditionsCalculationParser_Parse_No_dunes_hydraulic_boundaries_found_in_output_file); + + ReadResult(result); } /// - /// Reads the result of the . + /// Reads the . /// - /// The reader to get the result from. - /// Thrown when the result + /// The result from the database read. + /// Thrown when the result /// cannot be converted to the output format. - private void ReadResult(HydraRingDatabaseReader reader) + private void ReadResult(IDictionary result) { - double waveHeight = Convert.ToDouble(reader.ReadColumn(waveHeightColumnName)); - double wavePeriod = Convert.ToDouble(reader.ReadColumn(wavePeriodColumnName)); - double waterLevel = Convert.ToDouble(reader.ReadColumn(waterLevelColumnName)); + try + { + double waveHeight = Convert.ToDouble(result[waveHeightColumnName]); + double wavePeriod = Convert.ToDouble(result[wavePeriodColumnName]); + double waterLevel = Convert.ToDouble(result[waterLevelColumnName]); - Output = new DunesBoundaryConditionsCalculationOutput(waterLevel, - waveHeight, - wavePeriod); + Output = new DunesBoundaryConditionsCalculationOutput(waterLevel, + waveHeight, + wavePeriod); + } + catch (InvalidCastException e) + { + throw new HydraRingFileParserException(Resources.DunesBoundaryConditionsCalculationParser_Parse_No_dunes_hydraulic_boundaries_found_in_output_file, e); + } } } } \ No newline at end of file Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/ExceedanceProbabilityCalculationParser.cs =================================================================== diff -u -r9bc5d8bc179ba0be25c9746859da3726b8b6a33e -r377ab5754aaebdd884c70b39bc44f1669d01efa8 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/ExceedanceProbabilityCalculationParser.cs (.../ExceedanceProbabilityCalculationParser.cs) (revision 9bc5d8bc179ba0be25c9746859da3726b8b6a33e) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/ExceedanceProbabilityCalculationParser.cs (.../ExceedanceProbabilityCalculationParser.cs) (revision 377ab5754aaebdd884c70b39bc44f1669d01efa8) @@ -20,8 +20,9 @@ // All rights reserved. using System; +using System.Collections.Generic; +using Ringtoets.HydraRing.Calculation.Exceptions; using Ringtoets.HydraRing.Calculation.Properties; -using Ringtoets.HydraRing.Calculation.Readers; namespace Ringtoets.HydraRing.Calculation.Parsers { @@ -46,22 +47,31 @@ public void Parse(string workingDirectory, int sectionId) { - HydraRingDatabaseParseHelper.Parse(workingDirectory, - query, - sectionId, - Resources.ExceedanceProbabilityCalculationParser_Parse_No_beta_found_in_output_file, - ReadResult); + Dictionary result = HydraRingDatabaseParseHelper.ReadSingleLine( + workingDirectory, + query, + sectionId, + Resources.ExceedanceProbabilityCalculationParser_Parse_No_beta_found_in_output_file); + + ReadResult(result); } /// - /// Reads the result of the . + /// Reads the . /// - /// The reader to get the result from. - /// Thrown when the result + /// The result from the database read. + /// Thrown when the result /// cannot be converted to the output format. - private void ReadResult(HydraRingDatabaseReader reader) + private void ReadResult(IDictionary result) { - Output = Convert.ToDouble(reader.ReadColumn(betaColumnName)); + try + { + Output = Convert.ToDouble(result[betaColumnName]); + } + catch (InvalidCastException e) + { + throw new HydraRingFileParserException(Resources.ExceedanceProbabilityCalculationParser_Parse_No_beta_found_in_output_file, e); + } } } } \ No newline at end of file Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/HydraRingDatabaseParseHelper.cs =================================================================== diff -u -r9bc5d8bc179ba0be25c9746859da3726b8b6a33e -r377ab5754aaebdd884c70b39bc44f1669d01efa8 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/HydraRingDatabaseParseHelper.cs (.../HydraRingDatabaseParseHelper.cs) (revision 9bc5d8bc179ba0be25c9746859da3726b8b6a33e) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/HydraRingDatabaseParseHelper.cs (.../HydraRingDatabaseParseHelper.cs) (revision 377ab5754aaebdd884c70b39bc44f1669d01efa8) @@ -20,6 +20,7 @@ // All rights reserved. using System; +using System.Collections.Generic; using System.Data.SQLite; using Ringtoets.HydraRing.Calculation.Exceptions; using Ringtoets.HydraRing.Calculation.Properties; @@ -40,40 +41,37 @@ /// The query to perform when reading the database. /// The section id to get the output for. /// The exception message when there is no result. - /// The action to perform for parsing the results from the database. + /// A with the key of the column and the value. /// Thrown when any input parameter is null. /// Thrown when the reader encounters an error while /// reading the database. - public static void Parse(string workingDirectory, - string query, - int sectionId, - string exceptionMessage, - Action readResultAction) + public static Dictionary ReadSingleLine(string workingDirectory, + string query, + int sectionId, + string exceptionMessage) { - ValidateParameters(workingDirectory, query, exceptionMessage, readResultAction); + ValidateParameters(workingDirectory, query, exceptionMessage); try { using (var reader = new HydraRingDatabaseReader(workingDirectory, query, sectionId)) { - reader.Execute(); - readResultAction(reader); + return reader.ReadLine(); } } catch (SQLiteException e) { throw new HydraRingFileParserException(Resources.Parse_Cannot_read_result_in_output_file, e); } - catch (Exception e) when (e is HydraRingDatabaseReaderException || e is InvalidCastException) + catch (HydraRingDatabaseReaderException e) { throw new HydraRingFileParserException(exceptionMessage, e); } } private static void ValidateParameters(string workingDirectory, string query, - string exceptionMessage, - Action readResultAction) + string exceptionMessage) { if (workingDirectory == null) { @@ -87,10 +85,6 @@ { throw new ArgumentNullException(nameof(exceptionMessage)); } - if (readResultAction == null) - { - throw new ArgumentNullException(nameof(readResultAction)); - } } } } \ No newline at end of file Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/ReliabilityIndexCalculationParser.cs =================================================================== diff -u -r9bc5d8bc179ba0be25c9746859da3726b8b6a33e -r377ab5754aaebdd884c70b39bc44f1669d01efa8 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/ReliabilityIndexCalculationParser.cs (.../ReliabilityIndexCalculationParser.cs) (revision 9bc5d8bc179ba0be25c9746859da3726b8b6a33e) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Parsers/ReliabilityIndexCalculationParser.cs (.../ReliabilityIndexCalculationParser.cs) (revision 377ab5754aaebdd884c70b39bc44f1669d01efa8) @@ -20,9 +20,10 @@ // All rights reserved. using System; +using System.Collections.Generic; using Ringtoets.HydraRing.Calculation.Data.Output; +using Ringtoets.HydraRing.Calculation.Exceptions; using Ringtoets.HydraRing.Calculation.Properties; -using Ringtoets.HydraRing.Calculation.Readers; namespace Ringtoets.HydraRing.Calculation.Parsers { @@ -49,24 +50,33 @@ public void Parse(string workingDirectory, int sectionId) { - HydraRingDatabaseParseHelper.Parse(workingDirectory, - query, - sectionId, - Resources.ReliabilityIndexCalculationParser_Parse_No_reliability_found_in_output_file, - ReadResult); + Dictionary result = HydraRingDatabaseParseHelper.ReadSingleLine( + workingDirectory, + query, + sectionId, + Resources.ReliabilityIndexCalculationParser_Parse_No_reliability_found_in_output_file); + + ReadResult(result); } /// - /// Reads the result of the . + /// Reads the . /// - /// The reader to get the result from. - /// Thrown when the result + /// The result from the database read. + /// Thrown when the result /// cannot be converted to the output format. - private void ReadResult(HydraRingDatabaseReader reader) + private void ReadResult(IDictionary result) { - Output = new ReliabilityIndexCalculationOutput( - Convert.ToDouble(reader.ReadColumn(valueColumnName)), - Convert.ToDouble(reader.ReadColumn(betaColumnName))); + try + { + Output = new ReliabilityIndexCalculationOutput( + Convert.ToDouble(result[valueColumnName]), + Convert.ToDouble(result[betaColumnName])); + } + catch (InvalidCastException e) + { + throw new HydraRingFileParserException(Resources.ReliabilityIndexCalculationParser_Parse_No_reliability_found_in_output_file, e); + } } } } \ No newline at end of file Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Readers/HydraRingDatabaseReader.cs =================================================================== diff -u -r9bc5d8bc179ba0be25c9746859da3726b8b6a33e -r377ab5754aaebdd884c70b39bc44f1669d01efa8 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Readers/HydraRingDatabaseReader.cs (.../HydraRingDatabaseReader.cs) (revision 9bc5d8bc179ba0be25c9746859da3726b8b6a33e) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Readers/HydraRingDatabaseReader.cs (.../HydraRingDatabaseReader.cs) (revision 377ab5754aaebdd884c70b39bc44f1669d01efa8) @@ -38,13 +38,11 @@ { private const string sectionIdParameterName = "@sectionId"; private readonly string workingDirectory; + private readonly SQLiteConnection connection; private SQLiteDataReader reader; private SQLiteCommand command; - private SQLiteConnection connection; - private Dictionary results; - /// /// Creates a new instance of . /// @@ -86,42 +84,27 @@ } /// - /// Executes the query on the database. + /// Executes the query on the database and reads the next row. /// + /// A with the key + /// of the column and the value. /// Thrown when /// an error encounters while reading the database. - public void Execute() + public Dictionary ReadLine() { - results = new Dictionary(); - if (reader.Read()) { + var results = new Dictionary(); + for (var i = 0; i < reader.FieldCount; i++) { results.Add(reader.GetName(i), reader[i]); } - } - else - { - throw new HydraRingDatabaseReaderException(Resources.HydraRingDatabaseReader_Execute_No_result_found_in_output_file); - } - } - /// - /// Reads the object of the given column. - /// - /// The name of the column to read. - /// The object that is stored in the given column. - /// Thrown when - /// is null. - public object ReadColumn(string columnName) - { - if (columnName == null) - { - throw new ArgumentNullException(nameof(columnName)); + return results; } - return results[columnName]; + throw new HydraRingDatabaseReaderException(Resources.HydraRingDatabaseReader_Execute_No_result_found_in_output_file); } public void Dispose() Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/Parsers/HydraRingDatabaseParseHelperTest.cs =================================================================== diff -u -r9bc5d8bc179ba0be25c9746859da3726b8b6a33e -r377ab5754aaebdd884c70b39bc44f1669d01efa8 --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/Parsers/HydraRingDatabaseParseHelperTest.cs (.../HydraRingDatabaseParseHelperTest.cs) (revision 9bc5d8bc179ba0be25c9746859da3726b8b6a33e) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/Parsers/HydraRingDatabaseParseHelperTest.cs (.../HydraRingDatabaseParseHelperTest.cs) (revision 377ab5754aaebdd884c70b39bc44f1669d01efa8) @@ -20,6 +20,7 @@ // All rights reserved. using System; +using System.Collections.Generic; using System.IO; using Core.Common.TestUtil; using NUnit.Framework; @@ -45,7 +46,7 @@ public void Parse_WorkingDirectoryNull_ThrowArgumentNullException() { // Call - TestDelegate test = () => HydraRingDatabaseParseHelper.Parse(null, "", 0, "", reader => { }); + TestDelegate test = () => HydraRingDatabaseParseHelper.ReadSingleLine(null, "", 0, ""); // Assert var exception = Assert.Throws(test); @@ -56,7 +57,7 @@ public void Parse_QueryNull_ThrowArgumentNullException() { // Call - TestDelegate test = () => HydraRingDatabaseParseHelper.Parse("", null, 0, "", reader => { }); + TestDelegate test = () => HydraRingDatabaseParseHelper.ReadSingleLine("", null, 0, ""); // Assert var exception = Assert.Throws(test); @@ -67,32 +68,21 @@ public void Parse_ExceptionMessageNull_ThrowArgumentNullException() { // Call - TestDelegate test = () => HydraRingDatabaseParseHelper.Parse("", "", 0, null, reader => { }); + TestDelegate test = () => HydraRingDatabaseParseHelper.ReadSingleLine("", "", 0, null); // Assert var exception = Assert.Throws(test); Assert.AreEqual("exceptionMessage", exception.ParamName); } [Test] - public void Parse_ReadResultActionNull_ThrowArgumentNullException() - { - // Call - TestDelegate test = () => HydraRingDatabaseParseHelper.Parse("", "", 0, "", null); - - // Assert - var exception = Assert.Throws(test); - Assert.AreEqual("readResultAction", exception.ParamName); - } - - [Test] public void Parse_ReaderThrowsSQLiteException_ThrowHydraRingFileParserException() { // Setup string directory = Path.Combine(testDirectory, emptyWorkingDirectory); // Call - TestDelegate test = () => HydraRingDatabaseParseHelper.Parse(directory, query, 0, "", reader => { }); + TestDelegate test = () => HydraRingDatabaseParseHelper.ReadSingleLine(directory, query, 0, ""); // Assert var exception = Assert.Throws(test); @@ -107,40 +97,24 @@ string directory = Path.Combine(testDirectory, emptyDatabase); // Call - TestDelegate test = () => HydraRingDatabaseParseHelper.Parse(directory, query, 0, customMessage, reader => { }); + TestDelegate test = () => HydraRingDatabaseParseHelper.ReadSingleLine(directory, query, 0, customMessage); // Assert var exception = Assert.Throws(test); Assert.AreEqual(customMessage, exception.Message); } [Test] - public void Parse_ReadResultActionThrowsInvalidCastException_ThrowHydraRingFileParserExceptionWithCustomMessage() + public void Parse_ValidData_ReturnResult() { // Setup - const string customMessage = "Exception message"; string directory = Path.Combine(testDirectory, validFile); // Call - TestDelegate test = () => HydraRingDatabaseParseHelper.Parse(directory, query, 0, customMessage, reader => { throw new InvalidCastException(); }); + Dictionary result = HydraRingDatabaseParseHelper.ReadSingleLine(directory, query, 0, ""); // Assert - var exception = Assert.Throws(test); - Assert.AreEqual(customMessage, exception.Message); + Assert.AreEqual(20, result.Count); } - - [Test] - public void Parse_ValidData_ReadResultActionPerformed() - { - // Setup - string directory = Path.Combine(testDirectory, validFile); - var performed = false; - - // Call - HydraRingDatabaseParseHelper.Parse(directory, query, 0, "", reader => { performed = true; }); - - // Assert - Assert.IsTrue(performed); - } } } \ No newline at end of file Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/Readers/HydraRingDatabaseReaderTest.cs =================================================================== diff -u -r9bc5d8bc179ba0be25c9746859da3726b8b6a33e -r377ab5754aaebdd884c70b39bc44f1669d01efa8 --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/Readers/HydraRingDatabaseReaderTest.cs (.../HydraRingDatabaseReaderTest.cs) (revision 9bc5d8bc179ba0be25c9746859da3726b8b6a33e) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/Readers/HydraRingDatabaseReaderTest.cs (.../HydraRingDatabaseReaderTest.cs) (revision 377ab5754aaebdd884c70b39bc44f1669d01efa8) @@ -119,47 +119,12 @@ using (var reader = new HydraRingDatabaseReader(directory, query, 0)) { // Call - TestDelegate test = () => reader.Execute(); + TestDelegate test = () => reader.ReadLine(); // Assert var exception = Assert.Throws(test); Assert.AreEqual("Er is geen resultaat gevonden in de Hydra-Ring uitvoerdatabase.", exception.Message); } } - - [Test] - public void ReadColumn_ColumnNameNull_ThrowArgumentNullException() - { - // Setup - string directory = Path.Combine(testDirectory, validDatabase); - - using (var reader = new HydraRingDatabaseReader(directory, query, 0)) - { - reader.Execute(); - - // Call - TestDelegate test = () => reader.ReadColumn(null); - - // Assert - var exception = Assert.Throws(test); - Assert.AreEqual("columnName", exception.ParamName); - } - } - - [Test] - public void ReadColumn_ValidDatabaseAndWithColumnName_ReturnColumnValue() - { - // Setup - string directory = Path.Combine(testDirectory, validDatabase); - - using (var reader = new HydraRingDatabaseReader(directory, query, 0)) - { - reader.Execute(); - object value = reader.ReadColumn("OuterIterationId"); - - // Assert - Assert.AreEqual(6, value); - } - } } } \ No newline at end of file