// 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.IO;
using System.Linq;
using Core.Common.Base.Geometry;
using Core.Common.Base.IO;
using Core.Common.IO.Readers;
using Core.Common.TestUtil;
using Core.Common.Utils.Builders;
using NUnit.Framework;
using Ringtoets.Common.IO.Exceptions;
using Ringtoets.Common.IO.SoilProfile;
namespace Ringtoets.Common.IO.Test.SoilProfile
{
[TestFixture]
public class SegmentPointReaderTest
{
private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, nameof(SegmentPointReader));
[Test]
public void Constructor_NonExistingPath_ThrowsCriticalFileReadException()
{
// Setup
string testFile = Path.Combine(testDataPath, "does not exist");
// Call
TestDelegate test = () =>
{
using (new SegmentPointReader(testFile)) {}
};
// Assert
var exception = Assert.Throws(test);
string expectedMessage = new FileReaderErrorMessageBuilder(testFile).Build("Het bestand bestaat niet.");
Assert.AreEqual(expectedMessage, exception.Message);
}
[Test]
[TestCaseSource(typeof(InvalidPathHelper), nameof(InvalidPathHelper.InvalidPaths))]
public void Constructor_FileNullOrEmpty_ThrowsCriticalFileReadException(string fileName)
{
// Call
TestDelegate test = () =>
{
using (new SegmentPointReader(fileName)) {}
};
// Assert
Assert.Throws(test);
}
[Test]
public void Constructor_PathToExistingFile_ExpectedValues()
{
// Setup
string dbFile = Path.Combine(testDataPath, "emptySchema.soil");
// Call
using (var reader = new SegmentPointReader(dbFile))
{
// Assert
Assert.AreEqual(dbFile, reader.Path);
Assert.IsInstanceOf(reader);
}
Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile));
}
[Test]
public void Initialize_IncorrectFormatFile_ThrowsCriticalFileReadException()
{
// Setup
string dbFile = Path.Combine(testDataPath, "text.txt");
using (var reader = new SegmentPointReader(dbFile))
{
// Call
TestDelegate test = () => reader.Initialize();
// Assert
var exception = Assert.Throws(test);
string expectedMessage = new FileReaderErrorMessageBuilder(dbFile).Build(
"Kon geen stochastische ondergrondmodellen verkrijgen uit de database.");
Assert.AreEqual(expectedMessage, exception.Message);
}
Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile));
}
[Test]
public void MoveToStochasticSoilModel_EmptyDatabase_ReturnsFalse()
{
// Setup
string dbFile = Path.Combine(testDataPath, "emptySchema.soil");
using (var reader = new SegmentPointReader(dbFile))
{
reader.Initialize();
// Call
bool success = reader.MoveToStochasticSoilModel(1);
// Assert
Assert.IsFalse(success);
}
Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile));
}
[Test]
public void MoveToStochasticSoilModel_IdNotInDatabase_ReturnsFalse()
{
// Setup
string dbFile = Path.Combine(testDataPath, "complete.soil");
using (var reader = new SegmentPointReader(dbFile))
{
reader.Initialize();
// Call
bool success = reader.MoveToStochasticSoilModel(10000);
// Assert
Assert.IsFalse(success);
}
Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile));
}
[Test]
public void GivenMovedToIdNotInDatabase_WhenMoveToStochasticSoilModel_ThenRetunsTrue()
{
// Given
string dbFile = Path.Combine(testDataPath, "skippedStochasticSoilModel.soil");
using (var reader = new SegmentPointReader(dbFile))
{
reader.Initialize();
bool hasThree = reader.MoveToStochasticSoilModel(3);
// Precondition
Assert.IsFalse(hasThree);
// When
bool hasFour = reader.MoveToStochasticSoilModel(4);
// Then
Assert.IsTrue(hasFour);
}
Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile));
}
[Test]
public void ReadStochasticSoilModel_SegmentPointNull_ThrowsStochasticSoilModelException()
{
// Setup
string dbFile = Path.Combine(testDataPath, "invalidSegmentPoint.soil");
using (var reader = new SegmentPointReader(dbFile))
{
reader.Initialize();
reader.MoveToStochasticSoilModel(1);
// Call
TestDelegate test = () => reader.ReadSegmentPoints().ToArray();
// Assert
var exception = Assert.Throws(test);
Assert.AreEqual("Het stochastische ondergrondmodel 'StochasticSoilModelName' moet een geometrie bevatten.",
exception.Message);
}
Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile));
}
[Test]
public void ReadStochasticSoilModel_EmptyDatabase_ReturnsFalse()
{
// Setup
string dbFile = Path.Combine(testDataPath, "emptySchema.soil");
using (var reader = new SegmentPointReader(dbFile))
{
reader.Initialize();
// Call
Point2D[] segmentPoints = reader.ReadSegmentPoints().ToArray();
// Assert
CollectionAssert.IsEmpty(segmentPoints);
}
Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile));
}
[Test]
public void ReadSegmentPoints_FirstModelInCompleteDatabase_ReturnsExpectedSegmentPoints()
{
// Setup
string dbFile = Path.Combine(testDataPath, "complete.soil");
using (var reader = new SegmentPointReader(dbFile))
{
reader.Initialize();
reader.MoveToStochasticSoilModel(1);
// Call
Point2D[] segmentPoints = reader.ReadSegmentPoints().ToArray();
// Assert
Assert.AreEqual(1797, segmentPoints.Length);
}
Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile));
}
}
}