// 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.Dam.Forms; using Deltares.Standard; using Deltares.Standard.EventPublisher; using Deltares.Standard.Language; using Deltares.Standard.Logging; using NUnit.Framework; namespace Deltares.Dam.IntegrationTests; [TestFixture] public class ImportTests { [Test] public void GivenConfigurationFileWhenCreatingNewProjectThenCorrectDataIsImported() { 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, SelectedDikeRingIds = ["970750"], 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.That(LogManager.Messages.Count, Is.EqualTo(24)); } /// /// 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.SelectedDikeRingIds, 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) { WaterBoardPostProcessRelativeProfiles.CreateAbsoluteProfiles(damProject.DamProjectData.WaterBoard, dataSourceContainer.SoilProfileCharacteristicPointReference); } damProject.DamProjectData.DataSources = DataSourceManager.DataSources; damProject.SaveXMLProject(damProject.ProjectFileName, damProject); } }