// Copyright (C) Stichting Deltares 2025. 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.IO; using System.Linq; using Deltares.Dam.Data; using Deltares.Dam.Data.DataPlugins.Configuration; using Deltares.Dam.Data.UISupport; using Deltares.Standard; using Deltares.Standard.EventPublisher; using Deltares.Standard.Logging; using NUnit.Framework; namespace Deltares.Dam.IntegrationTests; [TestFixture] public class ImportTests { [Test] public void GivenConfigurationFileIncludingShapeFilesWhenCreatingNewProjectThenCorrectDataIsImported() { const string damDataSourceFileName = @"TestData\StabilityOutside1DProfileTest\Actualisatie.defx"; const string damProjectFileName = @"TestData\StabilityOutside1DProfileTest\ActualisatieImported.damx"; var damProject = new DamProject(); damProject.ClearProject(); var damNewProjectData = new DAMNewProjectData { DamProjectType = DamProjectType.Design, DamDataSourceFileName = damDataSourceFileName, DamProjectFileName = damProjectFileName, DataSourceProjectionName = "+ellps=WGS84 +no_defs" }; damProject.ProjectFileName = damNewProjectData.DamProjectFileName; damProject.DamProjectData.DataSourceEsriProjection = damNewProjectData.DataSourceEsriProjection; ImportDamProjectData(damNewProjectData, damProject); // Write all log messages to a file File.WriteAllLines(Path.ChangeExtension(damDataSourceFileName, ".log"), LogManager.Messages.Select(m => m.ToString())); Assert.Multiple(() => { Assert.That(LogManager.Messages.Count, Is.EqualTo(24)); Assert.That(damProject.DamProjectData.Dike.DamSoils, Has.Count.EqualTo(14)); // Check 1 soil for import from CSV DamSoil soil = damProject.DamProjectData.Dike.DamSoils.First(s => s.Name == "DeL_Tss"); Assert.That(soil.Name, Is.EqualTo("DeL_Tss")); Assert.That(soil.BelowPhreaticLevel, Is.EqualTo(17.74)); Assert.That(soil.AbovePhreaticLevel, Is.EqualTo(16.05)); // Check location "RK215-10" Location location = damProject.DamProjectData.Dike.Locations.First(s => s.Name == "RK215-10"); // Values from shapefiles Assert.That(location.Name, Is.EqualTo("RK215-10")); Assert.That(location.Scenarios[0].HeadPl3, Is.EqualTo(-3.111)); // Value from polygon shapefile Assert.That(location.SegmentId, Is.EqualTo("14")); // Value from line shapefile // Values from csv files Assert.That(location.Scenarios[0].RequiredSafetyFactorStabilityInnerSlope, Is.EqualTo(0.9)); Assert.That(location.Scenarios[0].RequiredSafetyFactorStabilityOuterSlope, Is.EqualTo(0.9)); Assert.That(location.Scenarios[0].RequiredSafetyFactorPiping, Is.EqualTo(1.2)); }); } /// /// This code has partly been copied from DamPlugin.Import(). /// It is better to extract the code there and make it public, so it can be used in the tests. /// /// /// private static void ImportDamProjectData(DAMNewProjectData damNewProjectData, DamProject damProject) { LogManager.Clear(); DamProjectType projectType = damNewProjectData.DamProjectType; string fileName = damNewProjectData.DamDataSourceFileName; DataSourceContainer dataSourceContainer = DataSourceContainer.Deserialize(fileName); string damProjectFolder = Path.GetDirectoryName(fileName); dataSourceContainer.DataSourceEsriProjection ??= damNewProjectData.DataSourceEsriProjection; DataEventPublisher.ThreadPublisher = new ThreadPublisher(); // ATTENTION: Do not stop DataEventPublisher because it is used to determine the source of the data with DataSourceManager DataSourceManager.StartListening(); damProject.Import(damProjectFolder, dataSourceContainer, damNewProjectData.DamProjectType, null); // Importing data creates a new DamProjectData object so the values set in the dialog have to be reset. damProject.DamProjectData.DamProjectType = projectType; damProject.DamProjectData.DamDataSourceFileName = fileName; if (dataSourceContainer.MapSoilProfile2D != null) { // Make sure the 2D-geometrie map is assigned damProject.AssignGeometry2DMapnameIfNotAssigned(Path.Combine(damProjectFolder, dataSourceContainer.MapSoilProfile2D)); } // If profiles are defined as relative profiles, new absolute profiles will be generated for each location if (dataSourceContainer.IsImportAsRelativeProfiles) { DikePostProcessRelativeProfiles.CreateAbsoluteProfiles(damProject.DamProjectData.Dike, dataSourceContainer.SoilProfileCharacteristicPointReference); } damProject.DamProjectData.DataSources = DataSourceManager.DataSources; damProject.SaveXMLProject(damProject.ProjectFileName, damProject); } }