// Copyright (C) Stichting Deltares 2020. 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 SoilList = Deltares.Geotechnics.Soils.SoilList; using SoilProfile2D = Deltares.Geotechnics.Soils.SoilProfile2D; 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 profileOneFileName = "SimpleProfile.sti"; const string profileTwoFileName = "Tutorial-1a 10.1.4.3.sti"; 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)); 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. } [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 SoilList CreateSoilList(IEnumerable soilNames) { var soilList = new SoilList(); foreach (string soilName in soilNames) { soilList.Add(new Soil { Name = soilName }); } return soilList; } } }