// Copyright (C) Stichting Deltares 2024. 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 Deltares.Geotechnics.Soils;
using NUnit.Framework;
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.Count, Is.EqualTo(2));
SoilProfile2D soilProfileOne = soilProfiles.ElementAt(0);
Assert.That(soilProfileOne.Name, Is.EqualTo(profileOneName));
Assert.That(soilProfileOne.Surfaces.Select(s => s.Name), Is.EqualTo(new[]
{
"Soft Clay",
"Muck"
}).AsCollection);
AssertSoilLayerProperties(soilProfileOne.Surfaces);
SoilProfile2D soilProfileTwo = soilProfiles.ElementAt(1);
Assert.That(soilProfileTwo.Name, Is.EqualTo(profileTwoName));
Assert.That(soilProfileTwo.Surfaces.Select(s => s.Name), Is.EquivalentTo(new[]
{
"Soft Clay",
"Sand",
"Peat",
"Soft Clay"
})); // Check only whether all the surfaces are present as the order differs on different machines.
AssertSoilLayerProperties(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.Count, Is.EqualTo(2));
SoilProfile2D soilProfileOne = soilProfiles.ElementAt(0);
Assert.That(soilProfileOne.Name, Is.EqualTo(profileOneFileName));
Assert.That(soilProfileOne.Surfaces.Select(s => s.Name), Is.EqualTo(new[]
{
"Soft Clay",
"Muck"
}).AsCollection);
AssertSoilLayerProperties(soilProfileOne.Surfaces);
SoilProfile2D soilProfileTwo = soilProfiles.ElementAt(1);
Assert.That(soilProfileTwo.Name, Is.EqualTo(profileTwoFileName));
Assert.That(soilProfileTwo.Surfaces.Select(s => s.Name), Is.EquivalentTo(new[]
{
"Soft Clay",
"Sand",
"Peat",
"Soft Clay"
})); // Check only whether all the surfaces are present as the order differs on different machines.
AssertSoilLayerProperties(soilProfileTwo.Surfaces);
}
[Test]
public void Import_WithIncompleteSoilList_ThrowsSoilProfileImporterException()
{
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"
});
Assert.That(() => SoilProfile2DImporter.Import(TestDataFolder, segment, availableSoils),
Throws.Exception.TypeOf().With.Message.EqualTo($"'{invalidSoilProfile}' contains the undefined soil: Peat."));
}
[Test]
public void Import_WithSoilProfileCausingReadException_ThrowsSoilProfileImporterException()
{
const string invalidSoilProfileName = "NonExistentSoilProfile";
Segment segment = CreateSegmentWithProfiles(new[]
{
"SimpleProfile.sti",
invalidSoilProfileName
});
SoilList availableSoils = CreateSoilList(new[]
{
"Soft Clay",
"Muck"
});
Assert.That(() => SoilProfile2DImporter.Import(TestDataFolder, segment, availableSoils),
Throws.TypeOf().With.Message.Contains($"Could not import soil profile '{invalidSoilProfileName}': "));
}
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(IEnumerable soilLayers)
{
foreach (SoilLayer2D soilLayer in soilLayers)
{
Assert.That(soilLayer.WaterpressureInterpolationModel, Is.EqualTo(GeotechnicsWaterPressureInterpolationModel.Hydrostatic));
}
}
private static SoilList CreateSoilList(IEnumerable soilNames)
{
var soilList = new SoilList();
foreach (string soilName in soilNames)
{
var soilToBeAdded = new Soil
{
Name = soilName
};
soilList.Add(soilToBeAdded);
}
return soilList;
}
}
}