// Copyright (C) Stichting Deltares 2016. 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 System.IO;
using System.Security.AccessControl;
using Core.Common.TestUtil;
using NUnit.Framework;
using Ringtoets.HydraRing.Calculation.Exceptions;
using Ringtoets.HydraRing.Calculation.Parsers;
namespace Ringtoets.HydraRing.Calculation.Test.Parsers
{
[TestFixture]
public class ReliabilityIndexCalculationParserTest
{
private const string validFile = "ValidFile";
private readonly string testDirectory = Path.Combine(TestHelper.GetTestDataPath(TestDataPath.Ringtoets.HydraRing.Calculation, "Parsers"),
nameof(ReliabilityIndexCalculationParser));
[Test]
public void Constructor_ExpectedValues()
{
// Call
var parser = new ReliabilityIndexCalculationParser();
// Assert
Assert.IsInstanceOf(parser);
Assert.IsNull(parser.Output);
}
[Test]
public void Parse_WorkingDirectoryNull_ThrowsArgumentNullException()
{
// Setup
var parser = new ReliabilityIndexCalculationParser();
// Call
TestDelegate test = () => parser.Parse(null, 1);
// Assert
var exception = Assert.Throws(test);
Assert.AreEqual("workingDirectory", exception.ParamName);
}
[Test]
public void Parse_WithWorkingDirectoryWithoutExpectedFile_ThrowsHydraRingFileParserException()
{
// Setup
string path = Path.Combine(testDirectory, "EmptyWorkingDirectory");
var parser = new ReliabilityIndexCalculationParser();
// Call
TestDelegate test = () => parser.Parse(path, 1);
// Assert
var exception = Assert.Throws(test);
Assert.IsInstanceOf(exception.InnerException);
}
[Test]
public void Parse_WithWorkingDirectoryWithInvalidOutputFile_ThrowsHydraRingFileParserException()
{
// Setup
string path = Path.Combine(testDirectory, "InvalidFile");
var parser = new ReliabilityIndexCalculationParser();
// Call
TestDelegate test = () => parser.Parse(path, 1);
// Assert
var exception = Assert.Throws(test);
Assert.AreEqual("Er kon geen resultaat gelezen worden uit de Hydra-Ring uitvoerdatabase.", exception.Message);
Assert.IsInstanceOf(exception.InnerException);
}
[Test]
public void Parse_WithWorkingDirectoryWithEmptyFile_ThrowsHydraRingFileParserException()
{
// Setup
string path = Path.Combine(testDirectory, "EmptyDatabase");
var parser = new ReliabilityIndexCalculationParser();
// Call
TestDelegate test = () => parser.Parse(path, 1);
// Assert
var exception = Assert.Throws(test);
Assert.AreEqual("Er is geen resultaat voor de betrouwbaarheidsindex van de berekende kans van voorkomen gevonden in de Hydra-Ring uitvoerdatabase.", exception.Message);
Assert.IsInstanceOf(exception.InnerException);
}
[Test]
public void Parse_WithBetaAndValueResultOnOtherSection_ThrowsHydraRingFileParserException()
{
// Setup
string path = Path.Combine(testDirectory, validFile);
var parser = new ReliabilityIndexCalculationParser();
// Call
TestDelegate test = () => parser.Parse(path, 0);
// Assert
var exception = Assert.Throws(test);
Assert.AreEqual("Er is geen resultaat voor de betrouwbaarheidsindex van de berekende kans van voorkomen gevonden in de Hydra-Ring uitvoerdatabase.", exception.Message);
Assert.IsInstanceOf(exception.InnerException);
}
[Test]
public void Parse_ValidFileWithResults_OutputSet()
{
// Setup
string path = Path.Combine(testDirectory, validFile);
var parser = new ReliabilityIndexCalculationParser();
// Call
parser.Parse(path, 1);
// Assert
Assert.AreEqual(1.24846, parser.Output.Result);
Assert.AreEqual(3.4037, parser.Output.CalculatedReliabilityIndex);
}
[Test]
[TestCase("BetaNull")]
[TestCase("ValueNull")]
public void Parse_BetaOrValueNull_ThrowsHydraRingFileParserException(string subFolder)
{
// Setup
string path = Path.Combine(testDirectory, subFolder);
var parser = new ReliabilityIndexCalculationParser();
// Call
TestDelegate test = () => parser.Parse(path, 1);
// Assert
var exception = Assert.Throws(test);
Assert.AreEqual("Er is geen resultaat voor de betrouwbaarheidsindex van de berekende kans van voorkomen gevonden in de Hydra-Ring uitvoerdatabase.", exception.Message);
Assert.IsInstanceOf(exception.InnerException);
}
[Test]
public void Parse_ErrorWhileReadingFile_ThrowsHydraRingFileParserException()
{
// Setup
var parser = new ReliabilityIndexCalculationParser();
string workingDirectory = Path.Combine(testDirectory, validFile);
using (new DirectoryPermissionsRevoker(testDirectory, FileSystemRights.ReadData))
{
// Call
TestDelegate call = () => parser.Parse(workingDirectory, 1);
// Assert
var exception = Assert.Throws(call);
var expectedMessage = "Er kon geen resultaat gelezen worden uit de Hydra-Ring uitvoerdatabase.";
Assert.AreEqual(expectedMessage, exception.Message);
Assert.IsInstanceOf(exception.InnerException);
}
}
}
}