// Copyright (C) Stichting Deltares 2023. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI 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.Linq; using Deltares.Dam.Data; using Deltares.Dam.Data.StiImporter; using NUnit.Framework; using Soil = Deltares.Geotechnics.Soils.Soil; using SoilLayer2D = Deltares.Geotechnics.Soils.SoilLayer2D; using SoilList = Deltares.Geotechnics.Soils.SoilList; using SoilProfile2D = Deltares.Geotechnics.Soils.SoilProfile2D; using GeotechnicsWaterPressureInterpolationModel = Deltares.Geotechnics.Soils.WaterpressureInterpolationModel; namespace Deltares.Dam.Tests.StiImporter { [TestFixture] public class SoilProfile2DImporterTest { private const string TestDataFolder = @"TestData\StiImporter\"; [Test] [TestCase(null)] [TestCase(" ")] [TestCase("")] public void Import_InvalidSoilProfileDirectory_ThrowsArgumentException(string invalidSoilProfileDirectory) { // Call TestDelegate call = () => SoilProfile2DImporter.Import(invalidSoilProfileDirectory, new Segment(), new SoilList()); // Assert Assert.That(call, Throws.ArgumentException .With.Message.EqualTo("'soilProfileDirectory' cannot be null or consist of whitespaces only.")); } [Test] public void Import_SegmentNull_ThrowsArgumentNullException() { // Call TestDelegate call = () => SoilProfile2DImporter.Import(string.Empty, null, new SoilList()); // Assert Assert.That(call, Throws.TypeOf() .With.Property(nameof(ArgumentNullException.ParamName)) .EqualTo("segment")); } [Test] public void Import_AvailableSoilsNull_ThrowsArgumentNullException() { // Call TestDelegate call = () => SoilProfile2DImporter.Import(string.Empty, new Segment(), null); // Assert Assert.That(call, Throws.TypeOf() .With.Property(nameof(ArgumentNullException.ParamName)) .EqualTo("availableSoils")); } [Test] public void Import_WithValidArguments_ReturnsExpectedSoilProfiles() { // Setup const string profileOneName = "SimpleProfile.sti"; const string profileTwoName = "Tutorial-1a 10.1.4.3.sti"; Segment segment = CreateSegmentWithProfiles(new[] { profileOneName, profileTwoName }); SoilList availableSoils = CreateSoilList(new[] { "Soft Clay", "Sand", "Peat", "Muck" }); // Call IEnumerable soilProfiles = SoilProfile2DImporter.Import(TestDataFolder, segment, availableSoils); // Assert Assert.That(soilProfiles, Has.Count.EqualTo(2)); SoilProfile2D soilProfileOne = soilProfiles.ElementAt(0); Assert.That(soilProfileOne.Name, Is.EqualTo(profileOneName)); CollectionAssert.AreEqual(new[] { "Soft Clay", "Muck" }, soilProfileOne.Surfaces.Select(s => s.Name)); AssertSoilLayerProperties(availableSoils, soilProfileOne.Surfaces); SoilProfile2D soilProfileTwo = soilProfiles.ElementAt(1); Assert.That(soilProfileTwo.Name, Is.EqualTo(profileTwoName)); CollectionAssert.AreEquivalent(new[] { "Soft Clay", "Sand", "Peat", "Soft Clay" }, soilProfileTwo.Surfaces.Select(s => s.Name)); // Check only whether all the surfaces are present as the order differs on different machines. AssertSoilLayerProperties(availableSoils, soilProfileTwo.Surfaces); } [Test] public void Import_WithProfileNamesNotHavingStiExtension_ReturnsExpectedSoilProfiles() { // Setup const string profileOneFileName = "SimpleProfile"; const string profileTwoFileName = "Tutorial-1a 10.1.4.3"; Segment segment = CreateSegmentWithProfiles(new[] { profileOneFileName, profileTwoFileName }); SoilList availableSoils = CreateSoilList(new[] { "Soft Clay", "Sand", "Peat", "Muck" }); // Call IEnumerable soilProfiles = SoilProfile2DImporter.Import(TestDataFolder, segment, availableSoils); // Assert Assert.That(soilProfiles, Has.Count.EqualTo(2)); SoilProfile2D soilProfileOne = soilProfiles.ElementAt(0); Assert.That(soilProfileOne.Name, Is.EqualTo(profileOneFileName)); CollectionAssert.AreEqual(new[] { "Soft Clay", "Muck" }, soilProfileOne.Surfaces.Select(s => s.Name)); AssertSoilLayerProperties(availableSoils, soilProfileOne.Surfaces); SoilProfile2D soilProfileTwo = soilProfiles.ElementAt(1); Assert.That(soilProfileTwo.Name, Is.EqualTo(profileTwoFileName)); CollectionAssert.AreEquivalent(new[] { "Soft Clay", "Sand", "Peat", "Soft Clay" }, soilProfileTwo.Surfaces.Select(s => s.Name)); // Check only whether all the surfaces are present as the order differs on different machines. AssertSoilLayerProperties(availableSoils, soilProfileTwo.Surfaces); } [Test] public void Import_WithIncompleteSoilList_ThrowsSoilProfileImporterException() { // Setup const string invalidSoilProfile = "Tutorial-1a 10.1.4.3.sti"; // Soil profile also contains peat and sand for its layers const string profileName = "SimpleProfile"; Segment segment = CreateSegmentWithProfiles(new[] { $"{profileName}.sti", invalidSoilProfile }); SoilList availableSoils = CreateSoilList(new[] { "Soft Clay", "Muck" }); // Call TestDelegate call = () => SoilProfile2DImporter.Import(TestDataFolder, segment, availableSoils); // Assert Assert.That(call, Throws.Exception.TypeOf() .With.Message.EqualTo($"'{invalidSoilProfile}' contains undefined soils.")); } [Test] public void Import_WithSoilProfileCausingReadException_ThrowsSoilProfileImporterException() { // Setup const string invalidSoilProfileName = "NonExistentSoilProfile"; Segment segment = CreateSegmentWithProfiles(new[] { "SimpleProfile.sti", invalidSoilProfileName }); SoilList availableSoils = CreateSoilList(new[] { "Soft Clay", "Muck" }); // Call TestDelegate call = () => SoilProfile2DImporter.Import(TestDataFolder, segment, availableSoils); // Assert var exception = Assert.Throws(call); Exception innerException = exception.InnerException; Assert.That(innerException, Is.Not.Null); Assert.That(exception.Message, Is.EqualTo($"Could not import soil profile '{invalidSoilProfileName}': {innerException.Message}.")); } private static Segment CreateSegmentWithProfiles(IEnumerable soilProfileNames) { var random = new Random(21); var segment = new Segment(); foreach (string soilProfileName in soilProfileNames) { segment.AddSoilGeometry2DProbability(soilProfileName, random.NextDouble(), null); } return segment; } private static void AssertSoilLayerProperties(SoilList soils, IEnumerable soilLayers) { foreach (SoilLayer2D soilLayer in soilLayers) { Soil soil = soils.Soils.Single(s => string.Equals(s.Name, soilLayer.Name)); bool expectedIsAquifer = soils.AquiferDictionary[soil]; Assert.That(soilLayer.IsAquifer, Is.EqualTo(expectedIsAquifer)); Assert.That(soilLayer.WaterpressureInterpolationModel, Is.EqualTo(GeotechnicsWaterPressureInterpolationModel.Hydrostatic)); } } private static SoilList CreateSoilList(IEnumerable soilNames) { var random = new Random(21); var soilList = new SoilList { AquiferDictionary = new Dictionary() }; foreach (string soilName in soilNames) { var soilToBeAdded = new Soil { Name = soilName }; soilList.Add(soilToBeAdded); bool isAquifer = Convert.ToBoolean(random.Next(0, 2)); soilList.AquiferDictionary.Add(soilToBeAdded, isAquifer); } return soilList; } } }