// 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.Collections.Generic; using System.Collections.ObjectModel; using System.Drawing; using System.IO; using System.Linq; 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 SoilProfile2DReaderTest { private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, nameof(SoilProfile2DReader)); [Test] public void Constructor_NonExistingPath_ThrowsCriticalFileReadException() { // Setup string testFile = Path.Combine(testDataPath, "does not exist"); // Call TestDelegate test = () => { using (new SoilProfile2DReader(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_InvalidPath_ThrowsCriticalFileReadException(string fileName) { // Call TestDelegate test = () => { using (new SoilProfile2DReader(fileName)) {} }; // Assert Assert.Throws(test); } [Test] public void Constructor_PathToExistingFile_ExpectedValues() { // Setup string dbFile = Path.Combine(testDataPath, "emptySchema.soil"); // Call using (var reader = new SoilProfile2DReader(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 SoilProfile2DReader(dbFile)) { // Call TestDelegate test = () => reader.Initialize(); // Assert var exception = Assert.Throws(test); string expectedMessage = new FileReaderErrorMessageBuilder(dbFile).Build( "Kon geen ondergrondschematisaties verkrijgen uit de database."); Assert.AreEqual(expectedMessage, exception.Message); } Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); } [Test] public void ReadSoilProfile_DatabaseWith2DSoilProfile_ReturnOneProfile() { // Setup string dbFile = Path.Combine(testDataPath, "2dprofile.soil"); var result = new Collection(); using (var reader = new SoilProfile2DReader(dbFile)) { reader.Initialize(); // Call while (reader.HasNext) { result.Add(reader.ReadSoilProfile()); } } // Assert Assert.AreEqual(1, result.Count); SoilProfile2D soilProfile2D = result[0]; Assert.AreEqual(1, soilProfile2D.Id); Assert.AreEqual("Profile", soilProfile2D.Name); Assert.AreEqual(85.2, soilProfile2D.IntersectionX); Assert.AreEqual(3, soilProfile2D.Layers.Count()); CollectionAssert.AreEqual(new[] { true, false, false }, soilProfile2D.Layers.Select(l => l.IsAquifer)); CollectionAssert.AreEqual(new[] { Color.FromArgb(70, 130, 180), Color.FromArgb(255, 0, 0), Color.FromArgb(128, 255, 128) }, soilProfile2D.Layers.Select(l => l.Color)); CollectionAssert.AreEqual(new[] { 0.21, 0.71, 3.88 }, soilProfile2D.Layers.Select(l => l.BelowPhreaticLevelMean)); CollectionAssert.AreEqual(new[] { 0.001, 0.02, 0.08 }, soilProfile2D.Layers.Select(l => l.BelowPhreaticLevelDeviation)); CollectionAssert.AreEqual(new[] { 0.3, 0.32, 0.4 }, soilProfile2D.Layers.Select(l => l.BelowPhreaticLevelShift)); CollectionAssert.AreEqual(new[] { 0.51, 0.01, 11.3 }, soilProfile2D.Layers.Select(l => l.DiameterD70Mean)); CollectionAssert.AreEqual(new[] { 0.029411, 0.1, 0.017699 }, soilProfile2D.Layers.Select(l => l.DiameterD70CoefficientOfVariation)); CollectionAssert.AreEqual(new[] { 1.01, 9.99, 5.21 }, soilProfile2D.Layers.Select(l => l.PermeabilityMean)); CollectionAssert.AreEqual(new[] { 0.024752, 0.01001, 0.057582 }, soilProfile2D.Layers.Select(l => l.PermeabilityCoefficientOfVariation)); Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); } [Test] public void ReadSoilProfile_IntersectionXFor2DSoilProfileInvalid_ThrowsSoilProfileReadException() { // Setup string dbFile = Path.Combine(testDataPath, "2dProfileWithXInvalid.soil"); using (var reader = new SoilProfile2DReader(dbFile)) { reader.Initialize(); // Call TestDelegate test = () => reader.ReadSoilProfile(); // Assert var exception = Assert.Throws(test); string expectedMessage = new FileReaderErrorMessageBuilder(dbFile) .WithSubject("ondergrondschematisatie 'Profile'") .Build("Ondergrondschematisatie bevat geen geldige waarde in kolom 'IntersectionX'."); Assert.AreEqual(expectedMessage, exception.Message); Assert.IsInstanceOf(exception.InnerException); } } [Test] public void GivenReadSoilProfileThrowsException_WhenReadingNextProfile_ReturnsNextProfile() { // Given string dbFile = Path.Combine(testDataPath, "2dprofileWithInvalidLayerProperty.soil"); SoilProfileReadException exception = null; var readSoilProfiles = new List(); using (var reader = new SoilProfile2DReader(dbFile)) { reader.Initialize(); // When try { reader.ReadSoilProfile(); } catch (SoilProfileReadException e) { exception = e; } // Then readSoilProfiles.Add(reader.ReadSoilProfile()); } Assert.IsInstanceOf(exception); Assert.AreEqual("Profile", exception.ProfileName); Assert.AreEqual(1, readSoilProfiles.Count); Assert.AreEqual("Profile2", readSoilProfiles[0].Name); Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); } [Test] public void ReadSoilProfile_IntersectionXFor2DSoilProfileNull_ReturnOneProfile() { // Setup string dbFile = Path.Combine(testDataPath, "2dProfileWithXNull.soil"); var result = new Collection(); using (var reader = new SoilProfile2DReader(dbFile)) { reader.Initialize(); // Call while (reader.HasNext) { result.Add(reader.ReadSoilProfile()); } } // Assert Assert.AreEqual(1, result.Count); SoilProfile2D soilProfile2D = result[0]; Assert.AreEqual(1, soilProfile2D.Id); Assert.AreEqual("Profile", soilProfile2D.Name); Assert.IsNaN(soilProfile2D.IntersectionX); Assert.AreEqual(3, soilProfile2D.Layers.Count()); } } }